着色器2输出反馈

时间:2018-01-25 14:14:24

标签: opengl glsl fragment-shader

我想从我的着色器获取两个输出,然后将它们反馈到着色器以用作迭代器och累加器。这适用于默认的“频道”,但对于两个......所以,这里是着色器:

#version 420
uniform sampler2DRect inData0;
uniform sampler2DRect inData1;
out float outData0;
out float outData1;
void main(void)
{
   outData0 = 1 + texture(inData0, gl_FragCoord.xy).r;
   outData1 = -1 + texture(inData1, gl_FragCoord.xy).r;
}

#version 420
in vec2 position;
void main()
{
   gl_Position = vec4(position, 0, 1);
}
然后是标准的东西

    Int32 frameBufferId = GL.GenFramebuffer();
    GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferId);

    Int32 textureId0 = GL.GenTexture();
    GL.ActiveTexture(TextureUnit.Texture0);
    GL.BindTexture(TextureTarget.TextureRectangle, textureId0);
    GL.TexImage2D(TextureTarget.TextureRectangle, 0, PixelInternalFormat.R32f, 1024, 1024, 0, PixelFormat.Red, PixelType.Float, new Single[1024 * 1024]);
    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureRectangle, textureId0, 0);

    Int32 textureId1 = GL.GenTexture();
    GL.ActiveTexture(TextureUnit.Texture1);
    GL.BindTexture(TextureTarget.TextureRectangle, textureId1);
    GL.TexImage2D(TextureTarget.TextureRectangle, 0, PixelInternalFormat.R32f, 1024, 1024, 0, PixelFormat.Red, PixelType.Float, new Single[1024 * 1024]);
    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment1, TextureTarget.TextureRectangle, textureId1, 0);

    GL.TexParameter(TextureTarget.TextureRectangle, TextureParameterName.TextureMinFilter, (Int32)TextureMinFilter.Linear);

    Single[] arrayBufferData = new Single[4 * 2] { -1, -1, 1, -1, 1, 1, -1, 1 };

    Int32 arrayBufferId = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, arrayBufferId);
    GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(4 * 2 * sizeof(Single)), arrayBufferData, BufferUsageHint.StaticDraw);
    Int32 positionId = GL.GetAttribLocation(programId, "position");
    GL.EnableVertexAttribArray(positionId);
    GL.VertexAttribPointer(positionId, 2, VertexAttribPointerType.Float, false, 0, 0);
    GL.Viewport(0, 0, 1024, 1024);

所以,这是我设法到达某处的地方

    Single[] result = new Single[1024 * 1024];

    GL.**DrawBuffers**(2, new DrawBuffersEnum[] { DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1 });
    GL.DrawArrays(PrimitiveType.Quads, 0, 4);

    GL.**ReadBuffer**(ReadBufferMode.ColorAttachment0);
    GL.ReadPixels(0, 0, 1024, 1024, PixelFormat.Red, PixelType.Float, result);

    GL.**ReadBuffer**(ReadBufferMode.ColorAttachment1);
    GL.ReadPixels(0, 0, 1024, 1024, PixelFormat.Red, PixelType.Float, result);

这给了我两个输出正确。对于一个“通道”,我再次调用DrawArrays,它会将output0提供给input0,但在这里我希望output1也进入input1。这可行吗?这个R32f / Red用于测试 - 它们将被vec4取代。

编辑: 改变:

layout(binding = 0) uniform sampler2DRect inData0
layout(binding = 1) uniform sampler2DRect inData1
layout(location = 0) out float outData0;
layout(location = 1) out float outData1;

它有效。

1 个答案:

答案 0 :(得分:0)

绑定输入,然后将输出指向正确的位置。

layout(binding = 0) uniform sampler2DRect inData0
layout(binding = 1) uniform sampler2DRect inData1
layout(location = 0) out float outData0;
layout(location = 1) out float outData1;