wgpu icon indicating copy to clipboard operation
wgpu copied to clipboard

Allow unconsumed inputs in fragment shaders

Open Imberflur opened this issue 1 year ago • 2 comments

By removing them from vertex outputs when generating HLSL.

  • Add naga::back::hlsl::FragmentEntryPoint for providing information about the fragment entry point when generating vertex entry points via naga::back::hlsl::Writer::write. Vertex outputs not consumed by the fragment entry point are omitted in the final output struct.
  • Add naga snapshot test for this new feature,
  • Remove Features::SHADER_UNUSED_VERTEX_OUTPUT, StageError::InputNotConsumed, and associated validation logic.
  • Make wgpu dx12 backend pass fragment shader info when generating vertex HLSL.
  • Add wgpu regression test for allowing unconsumed inputs.

Connections Fixes https://github.com/gfx-rs/wgpu/issues/3748

Description My case affected by this is passing in SPIRV from shaderc which can optimize out unused inputs from the fragment shader. The shaders are dynamically configured with a pre-processor based on various settings from the user so it can be hard to predict when certain inter-stage variables are unused, and my attempt at configuring out particular outputs from vertex shaders quickly became overly verbose.

As noted in the linked issue, this validation was originally in the WebGPU spec but was removed. The validation was necessary for generating HLSL with matching inter-stage interfaces but we can work around that by adjusting the HLSL generation to account for any unconsumed inputs.

After investigating this I found two potential ways to account for this in the generated HLSL:

  1. Pass info about vertex outputs when generating fragment inputs and add in the missing fields to the fragment input struct.
  2. Pass info about fragment inputs when generating vertex outputs and omit unconsumed fields from the vertex output struct.

I went with the second option since it seemed simpler to implement than generating new fields and nicely removed unnecessary work passing unused values (although I assume drivers can probably optimize this out).

Note: This is a breaking change.

Testing I added a test to for whether the wgpu validation logic now allows unconsumed inputs and a naga snapshot test for removing the unconsumed vertex outputs when generating HLSL.

~~I have not tested on a windows machine with the dx12 backend!~~ So here are some testing TODOs:

  • [x] Run cargo xtask test on machine with dx12.
  • [ ] Cherry-pick this to branch I'm currently using for veloren and test if it works there.

Checklist

  • [x] Run cargo fmt.
  • [x] Run cargo clippy. If applicable, add:
    • [ ] --target wasm32-unknown-unknown
    • [ ] --target wasm32-unknown-emscripten
  • [x] Run cargo xtask test to run tests.
  • [x] Add change to CHANGELOG.md. See simple instructions inside file.

Imberflur avatar Apr 14 '24 18:04 Imberflur

Great to see work here! Marking un-draft so it gets added to the review queue

cwfitzgerald avatar Apr 17 '24 05:04 cwfitzgerald

@Imberflur could you rebase the PR?

teoxoy avatar Jun 24 '24 14:06 teoxoy

@teoxoy rebased!

Imberflur avatar Jul 04 '24 04:07 Imberflur