0

I am trying to output the result of a fragment shader to two different outputs (frame buffer objects).

As far as the shader is concerned, I learned I can specify the different targets as :

layout (location = x) out vec4 color;
layout (location = y) out vec4 miscData;

Now the question being : how do I specify these locations C++ side ? So that my two FBO's match the shader's output indexes ? I have an example of it for the vertex shader in Nicol Bolas' book, but didn't read far enough to the fragment shader parts - so I don't know if he covers this subject or not.

Just as another trivia question : I've read the texture indexes are at least 48 (TEXTURE0, TEXTURE1, etc...) and that you don't exceed this even in the most extreme graphic apps. How come ? Is it because you generally don't use more that 48 textures at the same time in a fragment shader ? I must be missing something very obvious because, obviously, modern games use hunderds if not thousands of different textures.

Finally, does texture indexes (TEXTURE0, TEXTURE1, ...) have anything to do with layout locations ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
PinkTurtle
  • 6,942
  • 3
  • 25
  • 44
  • 1
    You _can_ _not_ write to multiple FBOs/framebuffers at once. You can write to multiple color _attachments_ of a single FBO at once. – derhass May 17 '15 at 22:10

1 Answers1

1

You should have multiple Color attachments in your Framebuffer then set the draw buffers, something like this:

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID0, 0);

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texID1, 0);

GLuint drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, drawBuffers);
Mehdi
  • 582
  • 4
  • 14
  • I'm actually trying to write to 2 different textures/FBOs from inside the fragment shader. – PinkTurtle May 17 '15 at 21:39
  • 2
    This is how you could achieve it !! you need to have two textures attached to your frame buffer set the draw buffers bind the frame buffer and then write to it from the frag layout (location = 0) out vec4 color; layout (location = 1) out vec4 miscData; – Mehdi May 17 '15 at 21:44
  • I think I get it now from derhass comment :) Answer accepted. – PinkTurtle May 17 '15 at 22:17
  • Can you write to both a color attachment and a depth texture too ? – PinkTurtle May 17 '15 at 22:35
  • absolutely you can. you need one more glFramebufferTexture2D but if you are not going to read from the depth, then you better use a render buffer instead. – Mehdi May 18 '15 at 11:19
  • I need to write to a color+depth texture (shader pass 1) and then read from them at shader pass 2. Problem is the standard `GL_UNSIGNED_BYTE` texture format isnt precise enough - I need max precision as far as glsl is concerned - for my depth buffer Im going to read from. I am not sure what a renderbuffer is yet =) why can't you read from it ? Should I write to a `GL_DEPTH24_STENCIL8` render buffer ? Can I then access the data at pass 2 ? Thanks (and sorry for long comment...) – PinkTurtle May 18 '15 at 13:45
  • 1
    for percision when creating the texture you can use GL_FLOAT and GL_DEPTH_COMPONENT and let the driver chose the size for you and attache it like so: glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexID, 0); check the code coz im writing it up of my head. if you need the stencil buffer then use GL_DEPTH_STENCIL, you can look for deffered rendering where you heavly use frame buffers, good luck. – Mehdi May 18 '15 at 18:06
  • I am confused because https://www.khronos.org/opengles/sdk/docs/man/xhtml/glFramebufferTexture2D.xml says the *attachment* parameter must be either `GL_COLOR_ATTACHMENT0`, `GL_DEPTH_ATTACHMENT`, or `GL_STENCIL_ATTACHMENT`. What about `COLOR_ATTACHMENTi` > 0 ? I've had no success at trying to bind textures to `GL_COLOR_ATTACHMENT1` or `GL_COLOR_ATTACHMENT2`. I posted another question illustrating it here : http://stackoverflow.com/questions/30484629/color-attachments-how-to-render-to-multiple – PinkTurtle May 28 '15 at 09:31