Repository navigation
Description
We currently have a special requirement that all depth textures must use a special texture_2d_depth type in the WGSL shader. While it's common to have special shadow sampler types for doing comparison tests, it's not common for shaders just loading depth values to require it. This is somewhat of an annoying requirement for engines, though not an impossible one; I'd like to see if it is strictly necessary.
This requirement seems to originate from the Metal Shading Language Specification, which says:
You must declare textures with depth formats as one of the following texture data types:
depth2d<T, access a = access::sample>
depth2d_array<T, access a = access::sample>
depthcube<T, access a = access::sample>
depthcube_array<T, access a = access::sample>
depth2d_ms<T, access a = access::read>
depth2d_ms_array<T, access a = access::read>
However, I looked at the common shader tools for Metal, and found:
- glsl-optimizer only outputs depth2d for comparison samplers: https://github.com/aras-p/glsl-optimizer/blob/d78c3d2f249aa870368ad320905bc954c47704f6/src/glsl/ir_print_metal_visitor.cpp#L471-L474
- SPIRV-Cross only outputs depth2d if comparison operations were used on the samplers: https://github.com/KhronosGroup/SPIRV-Cross/blob/c2500e504d2b823d73d2f129e4f4f050e9618ecb/spirv_msl.cpp#L13498-L13540
These two tools cover a substantial amount of games shipping on Unity and Unreal. So, it seems that these operations are "de facto" spec'd to work.
I think this could be evidence that we could drop the texture_2d_depth requirement for simply loading depth textures, though it could still be required for doing comparison sampling.
The second issue is that loading from a depth texture seems to have undefined values for the second, third and fourth components in loads, as seen in the Metal Shading Language Specification:
For a texture with the MTLPixelFormatR8Unorm pixel format, the default values for unspecified normalized components are G = 0.0, B = 0.0, and A = 1.0. For a texture with depth or stencil pixel format (such as MTLPixelFormatDepth24Unorm_Stencil8 or MTLPixelFormatStencil8), the default value for an unspecified component is undefined.
(This is a bizarre contradiction in the spec. depth2d's special load function only returns one component, there are no "unspecified components", so how could they even be undefined? This gives me some confidence that the texture loads are actually spec'd.)
On new enough devices, we could use texture swizzles, but that doesn't work for all platforms we want to support. However, given that I expect loading the G/B/A channels from a depth texture to be relatively rare in content, I think it could be solved by compiling an extra shader at pipeline creation time on these devices only if they access G/B/A of a depth texture load. But others can respond with the difficulty of such an approach.
Activity
magcius commentedon Sep 9, 2021
See also: #1198 , 82500965
kvark commentedon Sep 9, 2021
Thank you for the investigation work!
I'd split this question in 2:
GPUTextureSampleType == "float"binding for depth textures in the bind group layout?GPUTextureSampleType == "depth"binding in the layout? In this case, the shader generator can see the BGL and putdepth2din the resulting MSL. So we can do this even without assuming anything extra about Metal valid usage.magcius commentedon Sep 9, 2021
Good question. I would clearly prefer if
GPUTextureSampleType == "depth"could go away as well, but a simple step 1 might just be just removing texture_2d_depth from WGSL.Are there any other requirements that mandate
GPUTextureSampleType == "depth"?kvark commentedon Sep 9, 2021
No other requirements. It's only a separate type on MSL.
As a small side bonus to either of these choices,
texture_depth_multisampled_2dwill be terminated :)Kangz commentedon Sep 10, 2021
I think we still need
GPUTextureSampleType == "depth"for comparison sampling, as that can only be done withdepth2d. 1) would be nice. This way you only need to care about the"depth"sample type for comparison sampling. FYI @litherummagcius commentedon Sep 12, 2021
Yeah, I am definitely OK with keeping "depth" for comparison sampling.
kainino0x commentedon Sep 13, 2021
re: OP / question 1: @litherum would it be possible to get input from the Metal team on why this restriction exists? (that depth-format textures must be bound as
depth*in MSL even if they're sampled as color - especially given the contradiction of the "second issue" above - and the lack of this restriction in other APIs)magcius commentedon Sep 13, 2021
FYI, from more porting experience: because you already are forced to specify viewDimension in the pipeline layout, I don't think it's too onerous to require specifying depth at the same time.
I'd be OK with moving forward with removing texture_2d_depth and just using the pipeline layout to write the correct texture type in the shader, which sounds like it wouldn't require correspondence with the Metal team. But it would be a nice future to allow float as well.
kvark commentedon Sep 13, 2021
@magcius I agree, having this outcome would be totally fine. The reason @kainino0x tries to dig the Metal behavior is that - if it turns out to be valid, then we wouldn't need to do any of the extra implementation work to glue the BGL that disagrees with the shader. So if we can get this answer soon-ish, it would help for sure.
kdashg commentedon Sep 15, 2021
WGSL meeting minutes 2021-09-14
magicus commentedon Sep 15, 2021
kainino0x commentedon Sep 15, 2021
Kangz commentedon Oct 22, 2021
@litherum did you have any chance to make the list of builtins that would need to maybe be emulated in the future for depth textures?
litherum commentedon Oct 25, 2021
Looking at the docs, there are 2 results:
write()functions, but depth textures do notgather()for non-depth textures takes anenum class component {x, y, z, w}argument, butgather()for depth textures does not. It seems unlikely that specifying any value other thancomponent::xon depth textures will work portably.30 remaining items
litherum commentedon Jun 27, 2022
kainino0x commentedon Jun 29, 2022
Went ahead and filed the new proposal as #3115.
litherum commentedon Jul 8, 2022
I'm not sure this argument has been made before, but option 2 has a problem. Consider a shader like this:
Imagine that
texture1has been bound with adepthbind point, andtexture2has been bound with atexture_2dbind point. The shader itself typechecks successfully, but if we want to use Metal's internal depth types for thetexture2, we'll end up having to duplicate the body offoo. In the worse case, we'll have to duplicate functions 2^n times, where n is the number of textures bound to the pipeline.After some more discussion, we're now comfortable with option 3 instead of option 2, provided the spec indicates that operations which can observe/return the y/z/w channels will observe/return uninitialized and nonportable data. (Sorry for this to come in so late.)
magcius commentedon Jul 8, 2022
Yes, you'll have to either inline or duplicate functions, however, the number of functions you'll need is the number of times called with different texture type arguments, not the number bound to the pipeline. You already require the pipeline layout at shader compilation time, which will tell you the texture type for a given binding.
kainino0x commentedon Jul 8, 2022
Option 2 gives implementations the freedom to choose between rewriting the shader, and relying on de facto behavior, by adding validation that makes the first possible. It also means that, if we add texture operations in the future that aren't safe on depth textures, we can reject those at pipeline creation time (because we know whether they're used with depth textures or not). So far, feedback has been that the extra validation is acceptable from a developer standpoint.
For these reasons, I think we should keep the previous resolution of option 2.
litherum commentedon Jul 17, 2022
Right - that makes this worse, not better. In general, function calls are expected to be more numerous than bindings.
Here's an example program which describes one thing I'm worried about:
Above, we end up having to make a lot of copies of
foo()(either by inlining or specializing). Every parameter might need to use the depth type or the non-depth type. I believe @dneto0 has historically expressed a desire to avoid this kind of blowup.Here's another example of something else I'm worried about:
This describes how the blowup is global, rather than local -
a()needs to be inlined/specialized, so that makesb()have to be inlined/specialized, so that makesc()have to be inlined/specialized. The bulk of the shader can be insideb()andc(), which means we can end up essentially multiplying the compiled size of the entire shader, just because someone added a single function callc(myOtherTexture)somewhere.The conclusion that I'm coming to is that, if the information about what is bound to a depth texture is present in the bind group layout, but isn't present in the shader source, it's too easy for an author to accidentally create a binary size footgun.
This decision seems false to me. If compilers are going to be relying on the de-facto behavior in any situation, then option 2 doesn't seem to have much value. If the de-facto behavior can be trusted, then option 3 is strictly better.
The value of option 2 is "the compiler will intentionally blow up the binary size of your program to generate texture operations that follow the letter of the law."
I'm making a judgement call here: blowing up the binary size of the program is more harmful than relying on the de-facto behavior. And, if no compilers are actually going to be blowing up the binary size of the program, then it seems unfortunate to require authors to supply information which won't end up being used.
Do Tint or Naga currently plan on blowing up the binary size of the program based on
GPUTextureSampleType.depth?GPUTextureSampleType.depthcould be re-added at the point that we add these new operations. Enforcing this is no worse than the existing texture analysis we have to do today.kainino0x commentedon Jul 20, 2022
This is a key observation. Given this I'm pretty sure I'm convinced it makes sense to do option 3.
kainino0x commentedon Jul 20, 2022
Meeting: Re-resolved on option 3.
Allow using depth textures with texture_2d (gpuweb#3038)