openGL render to texture in iPhone fails when MSAA is enabled
Asked Answered
S

2

2

My render to texture iPhone code only works if I disable MSAA, otherwise all I get is a black texture. What could be the cause of the problem?

Here is my code:

glViewport(0,0, target->_Width, target->_Height);
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT Or GL_STENCIL_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, target->_Handle);          

// render stuff here

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, target->_Width, target->_Height, 0);
glBindTexture(GL_TEXTURE_2D, 0);
Sinclair answered 14/6, 2011 at 10:20 Comment(0)
S
3

Apparently, when you are using MSAA for your main framebuffer, you have to use it for any other FBOs you want to render to as well. Since GL_TEXTURE_2D_MULTISAMPLE is not available on OpenGL ES 2, the solution I have found is quite simply to apply the same modifications you need to go from regular rendering to MSAA rendering, to your render-to-texture code as well.

You need 3 additional buffers: a multi-sampled color renderbuffer, a multi-sampled depth renderbuffer, and a new FBO to attach them to. Bind the new FBO instead of the texture FBO before rendering. After rendering, resolve the new MSAA FBO into the texture FBO, the same way you do in your main rendering code using glResolveMultisampleFramebufferAPPLE().

Note that for some reason, texture rendering with enabled MSAA works without these modifications in the simulator. Maybe it uses GL_TEXTURE_2D_MULTISAMPLE automatically?

Symmetrical answered 12/9, 2011 at 9:16 Comment(0)
H
0

The cause of your problem is that you cannot render to a multisampled texture in OpenGL ES. Indeed, if I recall correctly, multisampled textures don't exist in OpenGL ES. Desktop OpenGL allows you to do it, but it introduces a whole new texture target (GL_TEXTURE_2D_MULTISAMPLE) in order to do it.

A buffer that offers multisampling is not the same thing as a regular texture; that's why Desktop OpenGL uses a special texture target, which has its own GLSL sampler type.

Halvah answered 14/6, 2011 at 11:53 Comment(2)
He isn't even doing render-to-texture (with FBOs), but the old style copy-to-texture.Dovetailed
Yes, I'm using glCopyTexImage2D, but the whole rendering happens inside a render to FBO (multisampled), I need to rewrite the rendering logic, I guessSinclair

© 2022 - 2024 — McMap. All rights reserved.