Skip to content

texture_2d_depth for loading depth values might be unnecessary? #2094

Closed

Description

@magcius

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:

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

  1. magcius commented on Sep 9, 2021

    @magcius
    Author

    See also: #1198 , 82500965

  2. kvark commented on Sep 9, 2021

    @kvark
    Contributor

    Thank you for the investigation work!
    I'd split this question in 2:

    1. should we be able to have a regular GPUTextureSampleType == "float" binding for depth textures in the bind group layout?
    2. if not, should we consider "texture_2d" in WGSL to be compatible with the GPUTextureSampleType == "depth" binding in the layout? In this case, the shader generator can see the BGL and put depth2d in the resulting MSL. So we can do this even without assuming anything extra about Metal valid usage.
  3. magcius commented on Sep 9, 2021

    @magcius
    Author

    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"?

  4. kvark commented on Sep 9, 2021

    @kvark
    Contributor

    No other requirements. It's only a separate type on MSL.
    As a small side bonus to either of these choices, texture_depth_multisampled_2d will be terminated :)

  5. added this to the V1.0 milestone on Sep 10, 2021
  6. Kangz commented on Sep 10, 2021

    @Kangz
    Contributor

    I think we still need GPUTextureSampleType == "depth" for comparison sampling, as that can only be done with depth2d. 1) would be nice. This way you only need to care about the "depth" sample type for comparison sampling. FYI @litherum

  7. magcius commented on Sep 12, 2021

    @magcius
    Author

    Yeah, I am definitely OK with keeping "depth" for comparison sampling.

  8. kainino0x commented on Sep 13, 2021

    @kainino0x
    Contributor

    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)

  9. magcius commented on Sep 13, 2021

    @magcius
    Author

    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.

  10. kvark commented on Sep 13, 2021

    @kvark
    Contributor

    @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.

  11. kdashg commented on Sep 15, 2021

    @kdashg
    Contributor
    WGSL meeting minutes 2021-09-14
    • DM: Previously we decided all depth textures had to be bound as depth bindings. This is inconvenient. Not how it’s done anywhere except Metal. Metal doesn’t have the binding types that we have. Observed by @magcius that the porting paths on Metal, in the cases when comparison is not use, will map to regular texture, and not using depth-texture. Seems to work for everyone for now.
    • AB: But that’s not documented as the valid behaviour.
    • DM: Want to layout the options
        1. Ask clarification from Metal: is the desired pattern supportable?
        1. Allow the shader to see regular texture 2D, provided the binding is the regular depth binding. (Does not require input from Metal. Solution is in the shader compiler only.)
    • JG: On Metal the compilation needs the pipeline layout, to say which samplers are dept samplers. We probably want to wait for MM for both.
    • RM: I’ll ask the Metal team, try to get feedback.
  12. magicus commented on Sep 15, 2021

    @magicus
  13. kainino0x commented on Sep 15, 2021

    @kainino0x
  14. Kangz commented on Oct 22, 2021

    @Kangz
    Contributor

    @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?

  15. litherum commented on Oct 25, 2021

    @litherum
    Contributor

    Looking at the docs, there are 2 results:

    1. Non-depth textures have write() functions, but depth textures do not
    2. gather() for non-depth textures takes an enum class component {x, y, z, w} argument, but gather() for depth textures does not. It seems unlikely that specifying any value other than component::x on depth textures will work portably.
  16. 30 remaining items

  17. litherum commented on Jun 27, 2022

    @litherum
  18. kainino0x commented on Jun 29, 2022

    @kainino0x
    Contributor

    Went ahead and filed the new proposal as #3115.

  19. modified the milestones: post-V1, V1.0 on Jun 29, 2022
  20. litherum commented on Jul 8, 2022

    @litherum
    Contributor

    I'm not sure this argument has been made before, but option 2 has a problem. Consider a shader like this:

    fn foo(tex: texture2d) {
        sample(tex, ...);
    }
    fn bar() {
        foo(texture1);
        foo(texture2);
    }
    

    Imagine that texture1 has been bound with a depth bind point, and texture2 has been bound with a texture_2d bind point. The shader itself typechecks successfully, but if we want to use Metal's internal depth types for the texture2, we'll end up having to duplicate the body of foo. 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.)

  21. magcius commented on Jul 8, 2022

    @magcius
    Author

    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.

  22. kainino0x commented on Jul 8, 2022

    @kainino0x
    Contributor

    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.

  23. litherum commented on Jul 17, 2022

    @litherum
    Contributor

    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

    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:

    fn foo(textureA : texture2d, textureB : texture2d, textureC : texture2d) {
        sample(textureA, ...);
        sample(textureB, ...);
        sample(textureC, ...);
    }
    
    fn bar() {
        foo(depthTexture, someTexture, someOtherTexture);
        foo(someTexture, depthTexture, someOtherTexture);
        foo(someTexture, someOtherTexture, depthTexture);
        foo(depthTexture, depthTexture, someTexture);
        ... etc etc etc ...
    }
    

    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:

    fn a(myTexture: texture2d) {
        sample(myTexture, ...);
    }
    fn b(myTexture: texture2d) {
        ...
        a(myTexture);
        ...
    }
    fn c(myTexture: texture2d) {
        ...
        b(myTexture);
        ...
    }
    fn d(myTexture: texture2d) {
        ...
        c(myTexture);
        c(myOtherTexture);
        ...
    }
    

    This describes how the blowup is global, rather than local - a() needs to be inlined/specialized, so that makes b() have to be inlined/specialized, so that makes c() have to be inlined/specialized. The bulk of the shader can be inside b() and c(), which means we can end up essentially multiplying the compiled size of the entire shader, just because someone added a single function call c(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.

    freedom to choose between rewriting the shader, and relying on de facto behavior

    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?

    if we add texture operations in the future that aren't safe on depth textures, we can reject those at pipeline creation time

    GPUTextureSampleType.depth could 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.

  24. kainino0x commented on Jul 20, 2022

    @kainino0x
    Contributor

    GPUTextureSampleType.depth could 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.

    This is a key observation. Given this I'm pretty sure I'm convinced it makes sense to do option 3.

  25. kainino0x commented on Jul 20, 2022

    @kainino0x
    Contributor

    Meeting: Re-resolved on option 3.

  26. added
    copyediting
    on Jul 20, 2022
  27. added a commit that references this issue on Aug 12, 2022
    17f6d90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    copyediting

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions