Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unconsumed inputs in fragment shaders #5531

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

Imberflur
Copy link
Contributor

@Imberflur Imberflur commented Apr 14, 2024

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 #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:

  • 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

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

@Imberflur Imberflur force-pushed the allow-unconsumed-inputs branch 2 times, most recently from 1ed945e to f226525 Compare April 14, 2024 18:45
Copy link
Contributor Author

@Imberflur Imberflur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some questions and notes for reviewers.

Comment on lines 28 to 33
// TODO: log error if binding is none?
// technically, this should always be `Some`
binding: Option<crate::Binding>,
Copy link
Contributor Author

@Imberflur Imberflur Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note to remind myself here and in several places below.

Should this log an error? Are there cases where we actually expect None?

I can:

  1. Remove these notes if logging isn't desired.
  2. Otherwise, add some logging (and remove notes).
  3. Or, remove notes and defer question/implementation to a separate issue/PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm adding a debug_assert!(member.binding.is_some()) into write_interface_struct

Comment on lines 504 to 557
TypeInner::Struct { ref members, .. } => {
// TODO: what about nested structs? Is that possible? Maybe try an unwrap on
// the binding?
for member in members.iter() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I saw something about it being possible to nest structs here. Is this possible? Should we open an issue for handling that? I could also attempt handling it in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot nest - should probably note this in the code

From the spec

Each user-defined input and output must have an explicitly specified IO location. Each structure member in the entry point IO must be one of either a built-in value (see § 12.3.1.1 Built-in Inputs and Outputs), or assigned a location.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And looks like this is checked when validating the Module by VaryingContext::validate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also implies that binding is always Some... I'm tempted to add at least a debug assertion for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note

wgpu-core/src/validation.rs Outdated Show resolved Hide resolved
wgpu-core/src/validation.rs Outdated Show resolved Hide resolved
tests/tests/regression/issue_3748.rs Outdated Show resolved Hide resolved
Comment on lines +936 to +962
// Uses separate wgsl files to make sure the tested code doesn't accidentally rely on
// the fragment entry point being from the same parsed content (e.g. accidentally using the
// wrong `Module` when looking up info). We also don't just create a module from the same file
// twice since everything would probably be stored behind the same keys.
let (input, mut module) = load_and_parse("unconsumed_vertex_outputs_vert");
let (frag_input, mut frag_module) = load_and_parse("unconsumed_vertex_outputs_frag");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this was a bit awkward since it didn't align very well with the existing tests. I needed a new parameter on check_targets. It looks like I could have added a new field to the Parameters struct to specify the fragment entry point. However, I wanted to specifically test the case of the fragment shader being from a separate Module.

@Imberflur Imberflur force-pushed the allow-unconsumed-inputs branch 2 times, most recently from c3caa2d to 0a04bf3 Compare April 15, 2024 01:14
@cwfitzgerald cwfitzgerald marked this pull request as ready for review April 17, 2024 05:31
@cwfitzgerald cwfitzgerald requested review from a team as code owners April 17, 2024 05:31
@cwfitzgerald
Copy link
Member

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

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wgpu side looks fine, added some comments some of the naga things

tests/tests/regression/issue_3748.rs Outdated Show resolved Hide resolved
tests/tests/regression/issue_3748.rs Outdated Show resolved Hide resolved
Comment on lines 504 to 557
TypeInner::Struct { ref members, .. } => {
// TODO: what about nested structs? Is that possible? Maybe try an unwrap on
// the binding?
for member in members.iter() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot nest - should probably note this in the code

From the spec

Each user-defined input and output must have an explicitly specified IO location. Each structure member in the entry point IO must be one of either a built-in value (see § 12.3.1.1 Built-in Inputs and Outputs), or assigned a location.

wgpu-core/src/validation.rs Outdated Show resolved Hide resolved
@Imberflur Imberflur force-pushed the allow-unconsumed-inputs branch 3 times, most recently from 9ae8bb7 to 3f11d08 Compare April 22, 2024 01:30
outputs when generating HLSL.

Fixes gfx-rs#3748

* 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.
* Add note that nesting structs for the inter-stage interface can't
  happen.
* Remove new TODO notes (some addressed and some transferred to an issue
  gfx-rs#5577)
* Changed issue that regression test refers to 3748 -> 5553
* Add debug_assert that binding.is_some() in hlsl writer
* Fix typos caught in CI

Also, fix compiling snapshot test when hlsl-out feature is not enabled.
Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just clearing from my review queue, need someone from naga to look at this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow disabling the StageError::InputNotConsumed error
2 participants