I wonder how to use a shader with subroutines and multiple render targets. I have a fragment shader which contains a few subroutines. (I posted only relevant pieces of the code).
#version 420
in vec3 data1;
in vec3 data2;
layout (location = 0) out vec4 fragColor;
layout (location = 1) out vec3 target1;
layout (location = 2) out vec3 target2;
subroutine void RenderPassType();
subroutine uniform RenderPassType RenderPass;
subroutine (RenderPassType)
void first()
{
}
subroutine (RenderPassType)
void second()
{
target1 = data1;
target2 = data2;
}
subroutine (RenderPassType)
void third()
{
//some computations
fragColor = result
}
I created two fbo’s which contain rendering targets (textures).
First:
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTex, 0);
Second:
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex1, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex2, 0);
A data for glDrawBuffers:
GLenum drawBuffers1[] = {GL_NONE, GL_NONE, GL_NONE,);
GLenum drawBuffers2[] = {GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1);
Rendering:
//bind the first fbo
//bind the first subroutine
glDrawBuffers(3, drawBuffers1);
...
//unbind the first fbo
//bind the second fbo
//bind the second subroutine
glDrawBuffers(3, drawBuffers2);
...
//unbind the second fbo
...
The above code works, but I wonder if it is the only right way to use subroutines together
with multiple render targets. Is something which I can do better (more efficient) ?
Do I have to use (location = 0) for the default framebuffer output ?
When I first bind the second fbo and subroutine and next the first fbo and subroutine glDrawBuffers
clears all textures. What can I do about it ?