Skip to content

Allow render pipelines to have color targets not written to by fragment outputs #1900

Closed

Description

@austinEng

Currently, validating GPUFragmentState(descriptor) says:

For each colorState layout descriptor in the list descriptor.targets:

  • descriptor.module must contain an output variable that:
    • is statically used by descriptor.entryPoint, and
    • has a type that is compatible with colorState.format.

This is overly restrictive because it means that the fragment shader must write to every color attachment.
It makes the following example use case difficult:

  1. Begin a render pass with two color attachments
  2. Bind a pipeline that writes to the first color attachment and draw, without modifying the second attachment
  3. Bind a pipeline that writes to the second color attachment and draw, without modifying the first attachment
  4. End the render pass

Another use case is a render pipeline that does not write to any attachments (may write to storage or to the depth attachment).

Technically, this can be worked around by setting the GPUColorTargetState.writeMask to 0 for all attachments the developer does not want to modify. However, it will also require them to modify their shaders to include these additional masked-out fragment outputs.

It would be good if we could avoid requiring the developer to disable writes and add dummy outputs to the shader. Experimentally, all of the target APIs Metal/D3D12/Vulkan allow you to have a fragment shader that does not write to all (or any) of the color attachments.

For Vulkan, the pipeline may write something undefined to the attachment [1], but this can be mitigated by the implementation by disabling blending for that color target and setting the write mask to 0. I didn't find documentation for Metal / D3D12, but disabling writes seems to work there as well. No shader modification is required.

[1] Vulkan spec 14.3 Fragment Output Interface

The input values to blending or color attachment writes are undefined for components which do not correspond to a fragment shader output.

Activity

  1. kvark commented on Jul 6, 2021

    @kvark
    Contributor

    Thank you for filing this! This is related to #1250

    First of all, what we have today is sufficient for users to get this behavior. They can set 0 to the write masks, while their shaders would need to take into account that these outputs exist. I don't think it's a big issue, since the shaders need to know the output locations anyway, so if a shader outputs location 0 and 2, it's not like omitting 1 makes it significantly more generic.

    Secondly, we can achieve the desired effect without changing the API. We can just say something along the lines of:

    fragment shader outputs are only required to written if the color mask is not 0

  2. austinEng commented on Jul 7, 2021

    @austinEng
    ContributorAuthor

    fragment shader outputs are only required to written if the color mask is not 0

    sounds good to me! main concern here is that not having this would impact shader reusability where authors may just need to needlessly add unused output locations in order to be used in the same render pass as at other shaders that write to those locations.

  3. vorg commented on Jul 17, 2021

    @vorg

    It might be worth noticing that WebGL2 (and WebGL 1 via WEBGL_draw_buffers extensions) requires all attached color attachments to be written to. That would mean for people coming from WebGL the current WebGPU behaviour is not new.

    https://www.khronos.org/registry/webgl/specs/latest/2.0/#5.4

    If any draw buffer with an attachment does not have a defined fragment shader output, draws generate INVALID_OPERATION, unless all 4 channels of colorMask are set to false.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions