You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible for the vertex shader to only use a subset of the vertex attributes hooked up by the API?
Is it possible for the fragment shader to only use a subset of the attribute data the vertex shader outputs?
It seems that this is something that an author would naturally want to be able to do. @kenrussell even described during today's call how he's found that many authors want to do these things. Let's see if it's possible.
OpenGL has - basically forever - supported the scenario that the fragment shader might not consume all of the outputs of the vertex shader. In this situation glGetAttribLocation returns -1. glVertexAttribPointer is specified to be a no-op if an index of -1 is passed.
This arose during development of the WebGL specification. For a brief period of time the WebGL working group attempted to make this more strict, and generate an error if an index of -1 was passed to glVertexAttribPointer. This broke a lot of OpenGL content being ported to WebGL, and the working group abandoned this direction.
For this reason I think it's common for graphics applications and engines to generate shader permutations where the fragment shader uses a subset of the vertex shader's outputs.
Metal does not allow attribute numbers > 30. However, it does allow the attribute numbers to be non-contiguous.
Metal does allow you to specify more vertex streams than are named in the vertex shader. Metal also allows the vertex shader to output more attributes than the fragment shader names.
Is it possible for the vertex shader to only use a subset of the vertex attributes hooked up by the API?
That's allowed in the current spec.
Is it possible for the fragment shader to only use a subset of the attribute data the vertex shader outputs?
D3D12 has strong constraints on this: FS can use a subset of the VS's outputs but it has to be contiguous starting from the first locations (so you can't just use location 0, 1 and 3 but leave 2 out). If we don't lift this constraint from WebGPU, then this will force shader modules to be compiled in createPipeline at least in some cases. IMHO this is fine because it is already the case because of the pipeline layout and some other things.
OpenGL has - basically forever - supported the scenario that the fragment shader might not consume all of the outputs of the vertex shader. In this situation glGetAttribLocation returns -1. glVertexAttribPointer is specified to be a no-op if an index of -1 is passed.
@kenrussell isn't glGetAttribLocation for vertex inputs only? In any case, as Corentin said, we already allow that. (There's no -1 issue because attributes are always explicitly numbered, there is no glGetAttribLocation equivalent.)
As for vertex-fragment matching, in handwritten WGSL code there's no concern that fragment inputs will end up becoming unused or optimized away - they're always explicitly listed in the entry point inputs (fwiw this is also true in OpenGL GLSL since there's only one entry point). However for GLSL->WGSL translation I'd worry that tooling will have a hard time holding onto the correct inputs to make them match exactly. Also - I don't know if this is possible, but - GLSL code with multiple entry points would have to decide fragment inputs based on static usage (at best) since there's no input<->entrypoint association.
@kenrussell isn't glGetAttribLocation for vertex inputs only? In any case, as Corentin said, we already allow that. (There's no -1 issue because attributes are always explicitly numbered, there is no glGetAttribLocation equivalent.)
Yes, glGetAttribLocation's for vertex inputs. It's related to this topic because the vertex input became unused only because the fragment shader didn't use the vertex shader's output. Good to know that WebGPU allows this already.
As for vertex-fragment matching, in handwritten WGSL code there's no concern that fragment inputs will end up becoming unused or optimized away - they're always explicitly listed in the entry point inputs (fwiw this is also true in OpenGL GLSL since there's only one entry point). However for GLSL->WGSL translation I'd worry that tooling will have a hard time holding onto the correct inputs to make them match exactly. Also - I don't know if this is possible, but - GLSL code with multiple entry points would have to decide fragment inputs based on static usage (at best) since there's no input<->entrypoint association.
Most likely the only GLSL that WebGPU should consider supporting for backward compatibility reasons is WebGL GLSL, and that only has a single entry point.
It seems to me that enforcing the strictest constraints to be most compatible with all of the shading languages' restrictions is the best approach for WebGPU. Developers will be able to adapt to them. If a design choice is made now that implicitly requires attribute renumbering and run-time shader compilation to be performed, that will have performance consequences for applications down the road. (I've been diagnosing such application startup time problems with a WebGL partner recently, and they're difficult to mitigate.)
GLSL -> WGSL translation is already complicated, similar to how SPIRV -> WGSL is complicated, and it's not a primary design goal (but maybe a secondary?) for WGSL (or WebGPU) to make this path seamless.
What Naga's GLSL -> WGSL does today is using all of the "in" global variables as inputs to the entry point regardless of static usage.
It seems that this is something that an author would naturally want to be able to do
I think this contradicts the story of HLSL, which arguably is the most popular shading language, and it requires the set of VS outputs and FS inputs to (mostly) match. I think HLSL example is also ideologically closer to WGSL since there are multiple entry points, and inputs/outputs are spelled out in the interface of entry point functions.
TL;DR: let's keep the current restriction in place until we know more about DXIL as a target.
Thank you for writing this down!
If I have the shaders, and I want to manually check their stage compatibility according to this algorithm, I'd need to have a depth=3 nested loop:
For each |input| in |inputs|,
...
For each |output| in |outputs|,
...
|inputs| must contain an input whose [=location=] is |outputLocation|
This isn't very intuitive, it's hard to reason about.
For comparison, the current matching algorithm is depth=2, but it has a property of being really intuitive: "the sets have to match exactly!", so I don't need to read the algorithm and step through it in order to check my stages.
Let's chat more about this during the office hours.
+1, thank you for writing it down. This is a bit more validation and semantic complexity than I would have hoped. TBH I don't think developers will find it easy to use this new flexibility that works only in specific cases. Like you have a shader and as soon as you start using [[builtin(position)]] you have to rethink your compilation pipeline so that input/outputs match exactly.
JN: any inputs to later stage that are not system values needs to match in ordering the same subset from the previous stage. Inputting 3 attribs in PS coming from VS - need to be the first 3 attribs in the same order from VS>
MM: makes sense.
CW: all need to be present in FS?
JN: no. Can have outputs from earlier stage not in later stage.
MM: if consumer consumes 3rd attribute - it needs to consume the first 2 attribs too?
JN: yes. You can't skip attribs.
CW: assume float4 output 0, 1, 2. Want to use input 3 as float4 in frag shader - I need to declare 1, 2? Or want to use 2, don't need to declare 3?
JN: correct.
MM: think that makes sense.
CW: builtin values FS uses? SV_Position for example? Does position of SV_Position in input structure need to match its location in VS output structure?
JN: yes, if written by previous stage like SV_Position - assumed same order between stages. Is_FrontFace, for example, can be at the end.
MM: that's the trick - if producer produces 5 outputs and an SV_ value, and consumer wants middle output and SV_ value, consumer needs all values in their struct.
JN: correct. Well - doesn't need to be last - needs to be in same position on consumer side.
CW: ah we want to put it last because otherwise we have to declare it in FS.
In #3073, variables are ordered by location, and position is put at the end (not how the spec is written exactly, but it's the effect). That's based on this:
MM: Seems like we have a resolution, up to editors to spec this. Not dealing with resources, not dealing with bindgroups. Attribs are one-dimensional; shader interfaces strictly ordered, etc., so not intractable.
CW: problem - in D3D you can put SV_Position wherever you want. If you use it in frag shader you still need to spec everything before/after it. Have to decide how this'll work in WGSL. Does it always go in the end? Have to specify everything in FS then.
MM: makes sense. We also have static use rules. Shouldn't rely on source order.
CW: also entry points take things as arguments.
JN: how it is in HLSL too.
CW: ordering between structures-in-structures, parameters, etc.?
JN: also packing rules where vectors can combine. Here, concerned about packing WGSL args to match lower-level D3D linkages - vec4s.
I spoke further with @jenatali about this by email. He says the D3D12 runtime doesn't actually care about this rule - at the DXBC/DXIL level, each output/input has specific register numbers (there are 32 of them). The issue comes from the fact that the HLSL compilers are responsible for register allocation among the 32 registers.
Given this, we'd be encoding the current behavior of HLSL compilers into our spec, in a way. The register allocation in the compiler isn't even guaranteed to be stable - it just has to be consistent between vertex outputs and fragment inputs. If DXC changed its register allocation it could invalidate our rules.
I think we should not make this spec change (EDIT: instead make one to just require subsetting: #3122). Instead, Tint/Naga - if outputting HLSL - would be responsible for translating WGSL to HLSL in such a way that the HLSL compiler they use produces consistent register allocations for vertex and fragment so they match.
My implementation proposal for this is, when outputting HLSL, given either a list of vertex outputs or a list of fragment inputs where the max location is L:
Generate, in order by location, L+1 HLSL declarations (0..L).
If the list doesn't have an item at that location, generate a placeholder item.
(WebGPU itself of course continues to validate that the fragment inputs are a subset of the vertex outputs, and the declarations match if they exist in both.)
I think this should straightforwardly result in the HLSL compiler's register allocation producing the right results. One question about this is whether the HLSL compilers need there to be a static use of the variable for it to be included in register allocation. I would imagine not, though, because otherwise it seems impossible for normal HLSL users today to get the same register allocation between two shaders.
Oh yeah, also have to think about builtins. I think the only builtin in both input and output is SV_Position. I believe position and front_facing/sample_index participate in register allocation, based on the meeting notes and Jiawei's investigation. So it may be necessary to pad out the user-defined inputs all the way to 30 and then put those builtins at the end. I don't know if that has a cost.
They could go at the beginning but it's possible that declaring sample_index (even if not statically used) will trigger sample-rate shading.
(WebGPU itself of course continues to validate that the vertex inputs are a subset of the fragment outputs, and the declarations match if they exist in both.)
I think you inverted FS and VS, but also right now WebGPU checks that the VS outputs match the FS input exactly.
Thanks ton for gathering this info, it may give an opportunity to have the "subsetting" rule in WebGPU instead of the "prefix" one. The big question is about the performance cost of doing this, or whether the runtimes are going to be able to do cross-stage dead code elimination. @jenatali do you know if we can reasonably expect D3D12 (and 11) drivers to do this kind of optimization, or should we bench?
I would not expect D3D11 drivers to do cross-stage DCE. I would expect some D3D12 drivers would, and others would not.
There's a problem that would potentially arise if L (in @kainino0x's example) was too large, such that having L+1 declarations would violate the limits of what D3D can support. I'm not sure if WebGPU defines a maximum location value that can be used? But perhaps that's the only spec change that's actually needed.
L is bounded by maxInterStageShaderVariables so I think it's ok. IMHO the "subset" rule is so much nicer that even if not all drivers do DCE I think we should consider it if there's a viable path to it.
Yes, we limit the location numbers to all be less than maxInterStageShaderVariables which on D3D12 is 30 (see #1962 (comment)). So that won't be a problem.
It sounds like the performance of my proposed polyfill could be pretty bad on some architectures (e.g. if you use ONLY location 29 for some reason), at least due to extra shared memory allocation, likely other extra bandwidth (e.g. when spilling), and maybe interpolation hardware usage. But this is something implementations will be able to fix over time by moving away from FXC, or simply by generating HLSL at pipeline-creation time (instead of shader-compilation time).
I did some implementations following #2038 (comment)
And found some issues on D3D12 with FXC generating DXBC so we might need to choose an alternative implementation (I think a per pipeline transform)
The DXBC vertex output (and fragment input) signature looks like this:
// Vertex Output signature:
//
// Name Index Mask Register SysValue Format Used
// -------------------- ----- ------ -------- -------- ------- ------
// TEXCOORD 1 x 0 NONE float x
// TEXCOORD 7 y 0 NONE float y
// TEXCOORD 3 x 1 NONE uint x
// TEXCOORD 5 y 1 NONE int y
// TEXCOORD 9 z 1 NONE uint z
// TEXCOORD 0 xyzw 2 NONE float xyzw
// TEXCOORD 2 xyzw 3 NONE float xyzw
// TEXCOORD 4 xyzw 4 NONE float xyzw
// TEXCOORD 6 xyzw 5 NONE float xyzw
// TEXCOORD 8 xyzw 6 NONE float xyzw
// SV_Position 0 xyzw 7 POS float xyzw
Based on test results, inconsistence in these fields could lead to "D3D12 create graphics pipeline state failed with E_INVALIDARG (0x80070057)":
Index (float3 a : TEXCOORD1; in VS output vs float3 a : TEXCOORD0 in FS input)
Mask (xyz vs xy is okay, xy vs xyz fails)
Register
our proposal implementation of padding with placeholders and all elements to vec4 tries to address this issue
SysValue (NONE vs POS)
Format (float4 vs uint4)
Even if we do the simple hlsl shader writing padding, we could still fail the case where there's nointerpolation uint4 a : TEXCOORD0; in VS output, but missing in FS input, and the padding adds a placeholder say float4 a : TEXCOORD0. The format doesn't match cuz we don't have the format information.
Activity
litherum commentedon Aug 16, 2021
(from #1962)
kenrussell commentedon Aug 16, 2021
To write down the background:
OpenGL has - basically forever - supported the scenario that the fragment shader might not consume all of the outputs of the vertex shader. In this situation glGetAttribLocation returns -1. glVertexAttribPointer is specified to be a no-op if an index of -1 is passed.
This arose during development of the WebGL specification. For a brief period of time the WebGL working group attempted to make this more strict, and generate an error if an index of -1 was passed to glVertexAttribPointer. This broke a lot of OpenGL content being ported to WebGL, and the working group abandoned this direction.
For this reason I think it's common for graphics applications and engines to generate shader permutations where the fragment shader uses a subset of the vertex shader's outputs.
litherum commentedon Aug 17, 2021
Metal does not allow attribute numbers > 30. However, it does allow the attribute numbers to be non-contiguous.
Metal does allow you to specify more vertex streams than are named in the vertex shader. Metal also allows the vertex shader to output more attributes than the fragment shader names.
Kangz commentedon Aug 17, 2021
That's allowed in the current spec.
D3D12 has strong constraints on this: FS can use a subset of the VS's outputs but it has to be contiguous starting from the first locations (so you can't just use location 0, 1 and 3 but leave 2 out). If we don't lift this constraint from WebGPU, then this will force shader modules to be compiled in
createPipelineat least in some cases. IMHO this is fine because it is already the case because of the pipeline layout and some other things.kainino0x commentedon Aug 17, 2021
@kenrussell isn't glGetAttribLocation for vertex inputs only? In any case, as Corentin said, we already allow that. (There's no
-1issue because attributes are always explicitly numbered, there is noglGetAttribLocationequivalent.)As for vertex-fragment matching, in handwritten WGSL code there's no concern that fragment inputs will end up becoming unused or optimized away - they're always explicitly listed in the entry point inputs (fwiw this is also true in OpenGL GLSL since there's only one entry point). However for GLSL->WGSL translation I'd worry that tooling will have a hard time holding onto the correct inputs to make them match exactly. Also - I don't know if this is possible, but - GLSL code with multiple entry points would have to decide fragment inputs based on static usage (at best) since there's no input<->entrypoint association.
kenrussell commentedon Aug 18, 2021
Yes, glGetAttribLocation's for vertex inputs. It's related to this topic because the vertex input became unused only because the fragment shader didn't use the vertex shader's output. Good to know that WebGPU allows this already.
Most likely the only GLSL that WebGPU should consider supporting for backward compatibility reasons is WebGL GLSL, and that only has a single entry point.
It seems to me that enforcing the strictest constraints to be most compatible with all of the shading languages' restrictions is the best approach for WebGPU. Developers will be able to adapt to them. If a design choice is made now that implicitly requires attribute renumbering and run-time shader compilation to be performed, that will have performance consequences for applications down the road. (I've been diagnosing such application startup time problems with a WebGL partner recently, and they're difficult to mitigate.)
kvark commentedon Aug 18, 2021
GLSL -> WGSL translation is already complicated, similar to how SPIRV -> WGSL is complicated, and it's not a primary design goal (but maybe a secondary?) for WGSL (or WebGPU) to make this path seamless.
What Naga's GLSL -> WGSL does today is using all of the "in" global variables as inputs to the entry point regardless of static usage.
I think this contradicts the story of HLSL, which arguably is the most popular shading language, and it requires the set of VS outputs and FS inputs to (mostly) match. I think HLSL example is also ideologically closer to WGSL since there are multiple entry points, and inputs/outputs are spelled out in the interface of entry point functions.
TL;DR: let's keep the current restriction in place until we know more about DXIL as a target.
litherum commentedon Aug 25, 2021
Here's a description of what the matching algorithm could look like: litherum@86ace00
litherum commentedon Aug 25, 2021
Tagging with wgsl since shader stage compatibility is a shading language thing.
[-]Data flow stream subsetting[/-][+]Shader stage input-output subsetting rules[/+]kvark commentedon Aug 25, 2021
Thank you for writing this down!
If I have the shaders, and I want to manually check their stage compatibility according to this algorithm, I'd need to have a depth=3 nested loop:
This isn't very intuitive, it's hard to reason about.
For comparison, the current matching algorithm is depth=2, but it has a property of being really intuitive: "the sets have to match exactly!", so I don't need to read the algorithm and step through it in order to check my stages.
Let's chat more about this during the office hours.
Kangz commentedon Aug 27, 2021
+1, thank you for writing it down. This is a bit more validation and semantic complexity than I would have hoped. TBH I don't think developers will find it easy to use this new flexibility that works only in specific cases. Like you have a shader and as soon as you start using
[[builtin(position)]]you have to rethink your compilation pipeline so that input/outputs match exactly.4 remaining items
kainino0x commentedon Jun 29, 2022
Notes from the minutes:
In #3073, variables are ordered by location, and
positionis put at the end (not how the spec is written exactly, but it's the effect). That's based on this:kainino0x commentedon Jun 29, 2022
I spoke further with @jenatali about this by email. He says the D3D12 runtime doesn't actually care about this rule - at the DXBC/DXIL level, each output/input has specific register numbers (there are 32 of them). The issue comes from the fact that the HLSL compilers are responsible for register allocation among the 32 registers.
Given this, we'd be encoding the current behavior of HLSL compilers into our spec, in a way. The register allocation in the compiler isn't even guaranteed to be stable - it just has to be consistent between vertex outputs and fragment inputs. If DXC changed its register allocation it could invalidate our rules.
I think we should not make this spec change (EDIT: instead make one to just require subsetting: #3122). Instead, Tint/Naga - if outputting HLSL - would be responsible for translating WGSL to HLSL in such a way that the HLSL compiler they use produces consistent register allocations for vertex and fragment so they match.
My implementation proposal for this is, when outputting HLSL, given either a list of vertex outputs or a list of fragment inputs where the max
locationisL:L+1HLSL declarations (0..L).(WebGPU itself of course continues to validate that the fragment inputs are a subset of the vertex outputs, and the declarations match if they exist in both.)
I think this should straightforwardly result in the HLSL compiler's register allocation producing the right results. One question about this is whether the HLSL compilers need there to be a static use of the variable for it to be included in register allocation. I would imagine not, though, because otherwise it seems impossible for normal HLSL users today to get the same register allocation between two shaders.
kainino0x commentedon Jun 29, 2022
Oh yeah, also have to think about builtins. I think the only builtin in both input and output is SV_Position. I believe position and front_facing/sample_index participate in register allocation, based on the meeting notes and Jiawei's investigation. So it may be necessary to pad out the user-defined inputs all the way to 30 and then put those builtins at the end. I don't know if that has a cost.
They could go at the beginning but it's possible that declaring sample_index (even if not statically used) will trigger sample-rate shading.
Kangz commentedon Jun 30, 2022
(comment about fixed typo)I think you inverted FS and VS, but also right now WebGPU checks that the VS outputs match the FS input exactly.
Thanks ton for gathering this info, it may give an opportunity to have the "subsetting" rule in WebGPU instead of the "prefix" one. The big question is about the performance cost of doing this, or whether the runtimes are going to be able to do cross-stage dead code elimination. @jenatali do you know if we can reasonably expect D3D12 (and 11) drivers to do this kind of optimization, or should we bench?
jenatali commentedon Jun 30, 2022
I would not expect D3D11 drivers to do cross-stage DCE. I would expect some D3D12 drivers would, and others would not.
There's a problem that would potentially arise if
L(in @kainino0x's example) was too large, such that havingL+1declarations would violate the limits of what D3D can support. I'm not sure if WebGPU defines a maximum location value that can be used? But perhaps that's the only spec change that's actually needed.Kangz commentedon Jun 30, 2022
L is bounded by
maxInterStageShaderVariablesso I think it's ok. IMHO the "subset" rule is so much nicer that even if not all drivers do DCE I think we should consider it if there's a viable path to it.kainino0x commentedon Jun 30, 2022
Proposed in #3122
Yes, we limit the location numbers to all be less than maxInterStageShaderVariables which on D3D12 is 30 (see #1962 (comment)). So that won't be a problem.
It sounds like the performance of my proposed polyfill could be pretty bad on some architectures (e.g. if you use ONLY location 29 for some reason), at least due to extra shared memory allocation, likely other extra bandwidth (e.g. when spilling), and maybe interpolation hardware usage. But this is something implementations will be able to fix over time by moving away from FXC, or simply by generating HLSL at pipeline-creation time (instead of shader-compilation time).
shrekshao commentedon Oct 11, 2022
I did some implementations following #2038 (comment)
And found some issues on D3D12 with FXC generating DXBC so we might need to choose an alternative implementation (I think a per pipeline transform)
The DXBC vertex output (and fragment input) signature looks like this:
Based on test results, inconsistence in these fields could lead to "D3D12 create graphics pipeline state failed with E_INVALIDARG (0x80070057)":
float3 a : TEXCOORD1;in VS output vsfloat3 a : TEXCOORD0in FS input)xyzvsxyis okay,xyvsxyzfails)NONEvsPOS)float4vsuint4)nointerpolation uint4 a : TEXCOORD0;in VS output, but missing in FS input, and the padding adds a placeholder sayfloat4 a : TEXCOORD0. The format doesn't match cuz we don't have the format information.Kangz commentedon Nov 1, 2022
Closing again (as no changes are needed) as agreed in the 2022-10-26 meeting.
shrekshao commentedon Nov 29, 2022
FYI, dawn implementation of per-pipeline WGSL transform is added. More details can be found at: https://bugs.chromium.org/p/dawn/issues/detail?id=1493