Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

glsl - Structure of output of fragment shader?

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?

question from:https://stackoverflow.com/questions/65855985/structure-of-output-of-fragment-shader

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...