Skip to content

investigation: texture load channel values for channels not in the texel format #1115

Closed

Description

@dneto0

An r8unorm image has a red channel only. But the texture load function always returns a vec4<f32>. What are the values for the green, blue, and alpha channels?

This issue is forked from #1112 (comment)

As near as I can tell, no platform API describes the values from the extra channels in any detail. So "unspecified value" is the baseline.

With extra work in shader code, we can sanitize the result, as long as we know which components need to be sanitized. At the very least we need to know which channels are unspecified.
(For example, if blue and alpha are undefined, we can do a shuffle with a zero-vector, or alternately a type constructor that took a swizzle of the known values.)

Activity

  1. added
    wgsl
    on Sep 29, 2020
  2. kvark commented on Sep 29, 2020

    @kvark
    Contributor

    My expectation was that the texture unit would handle this in a defined way, e.g. filling up with "0, 0, 0, 1" for floating-point textures. If it's truly undefined, we are lucky to have this way of working around by providing the format data externally, and doing the shader modifications.

  3. Kangz commented on Sep 29, 2020

    @Kangz
    Contributor

    I wasn't able to find anything in the specifications for Vulkan, D3D11 and OpenGL ES either. However Vulkan and D3D12 allow controlling the texture view "swizzling" with values of 0 and 1 for channels so we could force things to be (0, 0, 0, 1). Metal has that too, but it's only available on more recent version of iOS and macOS.

    I updated Dawn's TextureFormatTests and it seem the default value for non-existent components if (0, 0, 0, 1) on all APIs, for all formats.

  4. kvark commented on Sep 29, 2020

    @kvark
    Contributor

    @Kangz good point about the swizzle! It's certainly a better solution than patching the shader. However, D3D12 doesn't have swizzling for UAVs, so some shader patching will still be needed.

  5. dneto0 commented on Sep 29, 2020

    @dneto0
    ContributorAuthor

    Ah, I found that Vulkan is very prescriptive on this point unspecified colour channels go to 0, and alpha goes to 1.

    https://www.khronos.org/registry/vulkan/specs/1.0/html/chap16.html#textures-conversion-to-rgba

  6. dneto0 commented on Sep 29, 2020

    @dneto0
    ContributorAuthor

    I'll update #1112 to say that green, blue default to 0, and alpha to 1.

  7. kvark commented on Sep 29, 2020

    @kvark
    Contributor

    Would be great to get some confirmation from D3D12 (@damyanp, @RafaelCintron ) and Metal (@litherum ) about what the rules are.
    Note that the Swizzle feature in Metal is macOS 10.15+ and iOS 13.0+ , which is higher the baseline we want to support.

  8. damyanp commented on Oct 5, 2020

    @damyanp

    From D3D spec section "Defaults for Missing Components":

    The default value for missing components in an Element format is "0" for any component except A, which gets "1". The way "1" appears in the Shader depends on the Element format, in that it takes the specified data interpretation of the first typed component that is actually present in the format (starting from the left in RGBA order). If this interpretation is UNORM or FLOAT, then 1.0f is used for missing components. If the interpretation is SINT or UINT, then 0x1 is used.

    For example, when the format DXGI_FORMAT_R24_UNORM_X8_TYPELESS is read into a Shader, the values for G and B are 0, and A is 1.0f. For DXGI_FORMAT_R16G16_UINT, the B gets 0 and A gets 0x00000001. DXGI_FORMAT_R16_SINT provides 0 for G and B, and 0x00000001 for A.

  9. added a commit that references this issue on Oct 26, 2020
  10. added a commit that references this issue on Nov 9, 2020
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

    wgsl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions