Skip to content

Feature request: ignore shader writes to color attachments #2060

Closed

Description

@MatusT

I would like to propose an ability to discard shader writes to certain color attachments if it is doable.

Use case from my codebase:
I have a G-Buffer with Colors, Positions, Normals. I want to render opaque objects first and write associated g-buffer data. These data will be later used for (SS)AO (only opaque objects get AO computed). In the second pass I want to render transparent data into another color attachment BUT ignore writes of shader to positions and normals.

In this case I could use one shader with one pipeline with the desired functionality.

Activity

  1. kvark commented on Aug 24, 2021

    @kvark
    Contributor

    You wouldn't use the same pipeline. You'd definitely need a different pipeline, which would have smaller number of color targets in it. But an investigation is needed to figure out if you can use the same shader. The question is, basically, whether or not the native APIs support this.

  2. jobtalle commented on Aug 24, 2021

    @jobtalle

    You could also use a different pipeline and set writeMask in GPUColorTargetState to 0 for the targets you want to ignore, then you don't need to switch to a new pass. I'm not sure if or how this would impact performance.

  3. kvark commented on Aug 24, 2021

    @kvark
    Contributor

    Trying to find out the relevant Vulkan spec bits. Maybe it's supported, but the spec isn't clear about it:

    The pipeline creation rule:

    VUID-VkGraphicsPipelineCreateInfo-attachmentCount-00746
    If the pipeline is being created with fragment output interface state, and the subpass uses color attachments, the attachmentCount member of pColorBlendState must be equal to the colorAttachmentCount used to create subpass

    The fragment output interface matching rule:

    A fragment shader output variable identified with a Location decoration of i is directed to the color attachment indicated by pColorAttachments[i], after passing through the blending unit as described in Blending, if enabled. Locations are consumed as described in Location Assignment. The number of available fragment output locations is given by the maxFragmentOutputAttachments member of the VkPhysicalDeviceLimits structure.

    The exact meaning of this is that pColorAttachments[i] (of the subpass) is used for each shader output, so it can't be smaller. But maybe the spec isn't precise?

  4. magcius commented on Aug 25, 2021

    @magcius

    But an investigation is needed to figure out if you can use the same shader. The question is, basically, whether or not the native APIs support this.

    I don't see why -- presumably the browser's shader compiler could remove all writes to invalid attachments at pipeline compilation time? Or set the write mask to 0?

    It's worth noting that D3D12 allows shaders to write to "unbound RTVs" (null RTVs), even with non-zero write masks. It's simply treated the same as a zero write mask.

  5. litherum commented on Aug 25, 2021

    @litherum
    Contributor

    See also: #2038

  6. kvark commented on Aug 25, 2021

    @kvark
    Contributor

    @magcius

    I don't see why -- presumably the browser's shader compiler could remove all writes to invalid attachments at pipeline compilation time? Or set the write mask to 0?

    this would be possible if all the shaders are compiled at shader module pipeline creation time, which isn't the case.

  7. added this to the MVP milestone on Aug 27, 2021
  8. Kangz commented on Feb 1, 2022

    @Kangz
    Contributor

    It is a warning in Vulkan's validation layers but not clear if the spec allows it.

    Otherwise I made a small Dawn test that has an FS writing to two locations but uses it in a pipeline with only a single color target, and it passes (with debug layers) on all of D3D12/Metal/Vulkan. Turns out we didn't have validation for that in Dawn either! (I checked that Dawn doesn't have a transform that removes the outputs too).

    So I think we can just support ignoring FS outputs in WebGPU.

  9. Kangz commented on Feb 1, 2022

    @Kangz
    Contributor

    krOoze (intentionally not @ing them) pointed out that Vulkan always allows ignoring outputs of shader stages:

    15.1.3. Interface Matching:

    Shaders can declare and write to output variables that are not declared or read by the subsequent stage.

  10. added a commit that references this issue on Feb 3, 2022
    4db41e6
  11. Kangz commented on Feb 7, 2022

    @Kangz
    Contributor

    For completeness, I didn't find anything in the Metal docs that say that FS outputs are ignored, or not ignored.

    For D3D12 it seems the behavior should be described here but it only mentions outputs being a subset of the render target, not the opposite. @RafaelCintron @jenatalie, do you know if D3D12 allows the set of FS outputs to be larger than the set of the render targets? (the debug device is happy)

  12. added a commit that references this issue on Feb 9, 2022
    46869b8
  13. added a commit that references this issue on Feb 10, 2022
    b9b28d6
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions