简体   繁体   English

如何将 GL_TEXTURE_2D_ARRAY 绑定到 GL_COLOR_ATTACHMENT1 上的帧缓冲区?

[英]How to bind a GL_TEXTURE_2D_ARRAY to a framebuffer on GL_COLOR_ATTACHMENT1?

Edit: see @Rabbid76 answer, question was not truly related to GL_TEXTURE_2D_ARRAY, only to framebuffer color attachment activation!编辑:请参阅@Rabbid76 的回答,问题与 GL_TEXTURE_2D_ARRAY 并没有真正的关系,仅与帧缓冲区颜色附件激活有关!

I'm having trouble updating a shader that use to ouput into a single texture to multiple textures.我在更新用于将单个纹理输出到多个纹理的着色器时遇到问题。 Here's the simplified code, I'll put all I find relevant, feel free to ask for other parts of the code if they are important.这是简化的代码,我会把所有我觉得相关的,如果它们很重要,请随时询问代码的其他部分。

glGenTexture(1, tex);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA32F, x, y, 2);

glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0, 0);
glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, tex, 0, 1);

frag shader that writes:片段着色器写道:

#version 330

layout(location = 0) out vec4 col1;
layout(location = 1) out vec4 col2;

int main()
{
    col1 = vec4(1.0);
    col2 = vec4(2.0);
}

other shader that uses the result:使用结果的其他着色器:

#version 330 core

uniform sampler2DArray tex;

in vec2 Coord;

int main()
{
    vec4 val = texture(tex, vec3(Coord, 0));
    val += texture(tex, vec3(Coord, 1));
}

The problem is that col1 is well written in layout 0 (regardless of texture layer, glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texDefo, 0, 1); works as well), but I don't get to write in layout 1 ( GL_COLOR_ATTACHMENT1 ).问题是col1layout 0中写得很好(无论纹理层如何, glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texDefo, 0, 1);也可以),但我没有在layout 1中写( GL_COLOR_ATTACHMENT1 )。 Am I missing something here?我在这里错过了什么吗?

Extensive tests:广泛的测试:

From what I could narrow it down to, it really looks like layout(location = 1) doesn't work how I would have expected.从我可以缩小到的范围来看, layout(location = 1)看起来不像我预期的那样工作。 With the code provided I get使用提供的代码,我得到

  • val = col1 when layout(location = 0) out vec4 col1; val = col1 当layout(location = 0) out vec4 col1;
  • val = col2 when layout(location = 0) out vec4 col2; val = col2 当layout(location = 0) out vec4 col2; both regarless which texture layer I did bind the GL_COLOR_ATTACHMENTi , ie both layers seems to work in the GL_TEXTURE_2D_ARRAY .两者都不管我绑定了哪个纹理层GL_COLOR_ATTACHMENTi ,即两个层似乎都在GL_TEXTURE_2D_ARRAY中工作。

You need to specify the buffers to be drawn into with glDrawBuffers :您需要使用glDrawBuffers指定要绘制的缓冲区:

GLenum drawBuffers[]{ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, drawBuffers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM