2

When writing a fragment shader in GLSL for Vulkan I guess I've learned by example that the color of the fragment can be output like this:

layout(location = 0) out vec4 outColor;

void main() {
    outColor = /*...*/;
}

My question is where in the Vulkan or GLSL spec is this specified? ie That the out variable of location 0 should contain the output color and its type can be (must be?) vec4?

In the Vulkan spec it doesn't seem to be mentioned in 9.9 Fragment Shaders, and 26 Fragment Operations just links back to 9.9.

The GLSL spec has a two paragraph overview 2.5 Fragment Processor, doesn't go into specifics. In 7.1.5. Fragment Shader Special Variables it lists global variables, but no mention of the "out" variable. There is some discussion under Layout Qualifiers, but doesn't seem very specific.

Any idea?

3
  • 1
    It's not clear what you need "specified" here. For example, "That the out variable of location 0 should contain the output color" That's what it means when you put out in front of a global variable declaration in a fragment shader. Like, what else could it mean? What "specifics" are missing here? Commented Jan 23, 2021 at 6:11
  • 1
    @NicolBolas: So in GLSL section 4.4.2 it talks about in a fragment shader layout(location = 3) out vec4 color establishes "fragment color 3" as the first input to the blend equation? Surely there must be somewhere where it specifies the semantics of the output of a fragment shader? Commented Jan 23, 2021 at 6:57
  • 1
    @NicolBolas: And there is also an index property: layout(location = 3, index = 1) out vec4 factor; that effects the input index to the blend equation. Commented Jan 23, 2021 at 7:01

1 Answer 1

3

Fragment Output Interface chapter:

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

So basically you could define it differently from vec4. E.g. component by component. But the outputs that you omit will be garbage.

Any value that cannot be represented in the attachment’s format is undefined. For any other attachment format no conversion is performed. If the type of the values written by the fragment shader do not match the format of the corresponding color attachment, the resulting values are undefined for those components.

So if you mismatch the format, then output will be garbage.

If the framebuffer color attachment is VK_ATTACHMENT_UNUSED, no writes are performed through that attachment. Writes are not performed to framebuffer color attachments greater than or equal to the VkSubpassDescription::colorAttachmentCount or VkSubpassDescription2::colorAttachmentCount value.

So I think you could theoretically also declare more outputs than you have attachments.

GL_KHR_vulkan_glsl pseudoextension specifies how GLSL is translated to SPIR-V. But basically layout(location) translates to Location, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! Chapter 15 Shader Interfaces, of course. Thanks, this is exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.