Skip to content

Loosen pipeline fragment output validation - #1918

Merged
kvark merged 1 commit into
gpuweb:mainfrom
austinEng:fragment-state-validation
Jul 20, 2021
Merged

kvark merged 1 commit into
gpuweb:mainfrom
austinEng:fragment-state-validation

Conversation

@austinEng

@austinEng austinEng commented Jul 7, 2021 •

Copy link
Copy Markdown
Contributor

This loosens validation so that fragment shaders do not need
to include an output location for color targets that cannot be written
to (have a writeMask of 0). This allows better shader reuse so that
the shader fragment outputs do not need to match the pipeline
color targets exactly if the pipeline does not intend to write
to those targets.

Resolves #1900


Preview | Diff

This loosens validation so that fragment shaders do not need
to include an output location for color targets that cannot be written
to (have a writeMask of 0). This allows better shader reuse so that
the shader fragment outputs do not need to match the pipeline
color targets exactly if the pipeline does not intend to write
to those targets.
@austinEng
austinEng requested a review from kvark July 7, 2021 21:58
@austinEng austinEng changed the title Loosen pipeline fragment outputs validation Loosen pipeline fragment output validation Jul 7, 2021

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is generally good. It involves a bit more work on the implementation side to plumb the outputs to the shader. To help justifying this work, could you post a practical example that would benefit from this change?

@austinEng

Copy link
Copy Markdown
Contributor Author

Not actually more work. The spec prior to this PR required plumbing the outputs from the shader in order to validate they were present, with the correct type.

@kvark

kvark commented Jul 8, 2021

Copy link
Copy Markdown
Contributor

The current spec requires that an implementation reflects the shader's use of outputs.
The new spec would require that an implementation actually produces shader code/patches to write the outputs that are not used. This is more complicated and invasive.

@austinEng

austinEng commented Jul 8, 2021 •

Copy link
Copy Markdown
Contributor Author

Stated in the linked issue, "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" so no shader patches are needed. I didn't find docs that say it is disallowed, and experimentally, the backend validation layers do not say that this is invalid. The Vulkan explicitly calls out that this is allowed, but does say that an undefined value may be written to the output - which is why the PR is validating that the writeMask is 0.

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, yes, thanks for correction! So the implementation will, instead of patching the shader, cut out the output from the pipeline description entirely. That makes it much easier to implement.

@kainino0x

Copy link
Copy Markdown
Contributor

IIRC this was what we agreed upon in the meeting? So we can merge it?

@kvark

kvark commented Jul 20, 2021

Copy link
Copy Markdown
Contributor

Just re-reading the Vulkan part, so they consider this to be "undefined value" and not cover with any valid usage rules. Seems good to go.

@kvark
kvark merged commit f0e9389 into gpuweb:main Jul 20, 2021
github-actions Bot added a commit that referenced this pull request Jul 20, 2021
SHA: f0e9389
Reason: push, by @kvark

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot added a commit that referenced this pull request Jul 20, 2021
SHA: f0e9389
Reason: push, by @kvark

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot added a commit that referenced this pull request Jul 20, 2021
SHA: f0e9389
Reason: push, by @kvark

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@austinEng
austinEng deleted the fragment-state-validation branch July 20, 2021 19:35
austinEng added a commit to austinEng/webgpu-cts that referenced this pull request Jul 22, 2021
Tests that use fragment shaders that don't write to the output
must declare the color target with writeMask 0.

Spec PR: gpuweb/gpuweb#1918
Bug: crbug.com/dawn/962
austinEng added a commit to austinEng/webgpu-cts that referenced this pull request Jul 22, 2021
Tests that use fragment shaders that don't write to the output
must declare the color target with writeMask 0.

Spec PR: gpuweb/gpuweb#1918
Bug: crbug.com/dawn/962
kainino0x pushed a commit to gpuweb/cts that referenced this pull request Jul 22, 2021
Tests that use fragment shaders that don't write to the output
must declare the color target with writeMask 0.

Spec PR: gpuweb/gpuweb#1918
Bug: crbug.com/dawn/962
@shrekshao

Copy link
Copy Markdown
Contributor

Recently when I'm working on the implementation for this, it seems on Metal it doesn't allow pipeline shader output type != color attachment type with writeMask == 0

WGSL:

struct Output {
        @location(0) color0 : vec4<f32>,
        // location(1) is left empty
        @location(2) color2 : vec4<i32>,
        @location(3) color3 : vec4<u32>,
}

@stage(fragment)
fn main() -> Output {
        var output : Output;
        output.color0 = vec4<f32>(1.0, 0.0, 0.0, 1.0);
        output.color2 = vec4<i32>(12, 34, 56, 78);
        output.color3 = vec4<u32>(255u, 127u, 63u, 1u);
        return output;
}

Color target format:

0: RGBA8Unorm
1: Undefined
2: RGBA8Unorm
3: R32Uint

WriteMask of 1,2,3 are 0.

And I got this on Metal:

Device lost during test: Error creating pipeline state output of type int4 is not compatible with a MTLPixelFormatRGBA8Unorm color attachement.

I set writeMask to 0 and blendingEnabled to false for MTLRenderPipelineColorAttachmentDescriptor. (Let me know if setting some other state could turn it to valid)

Since we don't a shader patch, maybe we'd like to adjust the validation rule to exclude the case where shader output type != color attachment/target type? I think for real use case, devs can just set the unused one to undefined (sparse attachment). It doesn't make much sense to write some int32 to a RGBA8Unorm with writeMask set to 0 though?

@austinEng

Copy link
Copy Markdown
Contributor Author

This makes sense to me. I think the original intent of the PR was to make it valid for the fragment stage have extra outputs, not outputs that are a different format from the pipeline color targets. Could you make a PR for this validation?

@Kangz

Kangz commented Apr 29, 2022

Copy link
Copy Markdown
Contributor

@shrekshao I don't understand the problem

    - For each non-`null` |colorState| layout descriptor in the list |descriptor|.{{GPUFragmentState/targets}}:
        - ...
        - If |descriptor|.{{GPUProgrammableStage/entryPoint}} has a [=pipeline output=] value
            with [=location=] attribute equal to the index of the |colorState|
            in the |descriptor|.{{GPUFragmentState/targets}} list:
            - The [=pipeline output=] type must be compatible with |colorState|.{{GPUColorTargetState/format}}.

            Otherwise:
            - |colorState|.{{GPUColorTargetState/writeMask}} must be 0.

Here the loop will go through indices 0 2 and 3. (assuming undefined is that the color state 1 is not present in the descriptor. In Dawn's API that's translated with an Undefined format, but it's an implementation detail of Dawn/Chromium).

For index 0: the entryPoint has a pipeline output. The type is f32 which is compatible with "rgba8unrom"
For index 2: the entryPoint has a pipeline output. The type is i32 which is not compatible with "rgba8unorm" -> ERROR.

What am I missing?

@shrekshao

shrekshao commented Apr 29, 2022 •

Copy link
Copy Markdown
Contributor

@Kangz

For index 2: the entryPoint has a pipeline output. The type is i32 which is not compatible with "rgba8unorm" -> ERROR.

It is not compatible.

But the writeMask of index 2 is set 0

WriteMask of 1,2,3 are 0.

so it satisfies the "Otherwise" branch so it should be valid.

We want to rule out these cases (shader output type don't match with attachment format) that don't seem to have realistic meaning, by only allow one incompatible case: "|colorState|.{{GPUColorTargetState/format}} can be null instead.", "if |colorState|.{{GPUColorTargetState/writeMask}} is 0:"

@Kangz

Kangz commented Apr 29, 2022

Copy link
Copy Markdown
Contributor

They way I read the spec is:

if (descriptor.entryPoint has a pipeline output value with location attribute equal to the index of the colorState in the descriptor.targets list) {
    (validate that) the pipeline output type must be compatible with colorState.format.
} else {
    (validate that) colorState.writeMask must be 0.
}

Basically if entries match, they must be compatible. If there is no output in the shader, but an entry in the descriptor, then the writeMask must be 0.

This matches the commit message of this PR:

This loosens validation so that fragment shaders do not need to include an output location for color targets that cannot be written to (have a writeMask of 0).

@shrekshao

Copy link
Copy Markdown
Contributor

You are right.
For the example in #1918 (comment)

if (descriptor.entryPoint has a pipeline output value with location attribute equal to the index of the colorState in the descriptor.targets list) {
     // index 0, 2, 3 goes into this branch
    (validate that) the pipeline output type must be compatible with colorState.format.
} else {
    // index 1 goes into this branch
    (validate that) colorState.writeMask must be 0.
}

The current spec looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants