0

inside my program I'm using glFramebufferTexture2D to set the target. But if I use it the output starts to flicker. If I use two frame buffers the output looks quite normal.

Has anybody an idea why that happens or what can be better inside the following source code? - that is an example and some not relevant code isn't inside.

// bind framebuffer for post process
::glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_SwapBuffer);

::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_SwapBufferTargets[SwapBufferTarget1]->m_NativeTextureHandle, 0);

unsigned int DrawAttachments = { GL_COLOR_ATTACHMENT0 };

::glDrawBuffers(1, &DrawAttachments);

...

// render gaussian blur
m_Shader->Use();

::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_BlurredImageFromPass1->m_NativeTextureHandle, 0);
_InputTexturePtr->ActivateTexture(GL_TEXTURE0);
RenderMesh();

::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _TargetTexturePtr->m_NativeTextureHandle, 0);
m_BlurredImageFromPass1->ActivateTexture(GL_TEXTURE0);
RenderMesh();

...

// copy swap buffer to system buffer
::glBindFramebuffer(GL_READ_FRAMEBUFFER, m_SwapBuffer);

::glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

::glBlitFramebuffer(0, 0, m_pConfig->m_Width, m_pConfig->m_Height, 0, 0, m_pConfig->m_Width, m_pConfig->m_Height, GL_COLOR_BUFFER_BIT, GL_NEAREST);

EDIT: I found the problem! It was inside my swap chain. I've rendered the original picture and after that a black one. So I get a flicker if frame rate drops.

Tobias
  • 427
  • 4
  • 19
  • Are you asking why does the double buffering reduce flicker? – Bartek Banachewicz Mar 05 '13 at 15:22
  • I've only one frame buffer and two render targets (as textures). Now I've got some flicker. Two frame buffers (every one gets his own render target) - switching framebuffer with glBindFramebuffer - I got no flicker. But on several websites I've read is written that switching framebuffer is slower as set up new rendertarget. – Tobias Mar 05 '13 at 15:29
  • 1
    "I've read it's slower" *sigh*. Don't prematurely optimize. Measure it first. Also, virtually every application uses double buffering. – Bartek Banachewicz Mar 05 '13 at 15:34

1 Answers1

1

This is probably better suited for a comment but is too large, so I will put it here. Your OpenGL semantics seem to be a little off in the following code segment:

::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_BlurredImageFromPass1->m_NativeTextureHandle, 0);
_InputTexturePtr->ActivateTexture(GL_TEXTURE0);
RenderMesh();

::glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _TargetTexturePtr->m_NativeTextureHandle, 0);
m_BlurredImageFromPass1->ActivateTexture(GL_TEXTURE0);
RenderMesh();

glActiveTexture (and thus your ActivateTexture wrapper) is purely for setting the active texture slot when binding a texture INPUT to a sampler in a shader program, and glFramebufferTexture2D is used in combination with glDrawBuffers to set the target OUTPUTS of your shader program. Thus, glActiveTexture and glFramebufferTexture2D should probably not be used on the same texture during the same draw operation. (Although I don't think this is what is causing your flicker) Additionally, I don't see where you bind/release your texture handles. It is generally good OpenGL practice to only bind objects when they are needed and release them immediately after. As OpenGL is a state machine, forgetting to release objects can really come and bite you in the ass on large projects.

Furthermore, when you bind a texture to a texture slot using glActiveTexture (or any glActiveTexture wrapper) always call glActiveTexture BEFORE you bind the texture handle.

Sir Digby Chicken Caesar
  • 3,063
  • 1
  • 23
  • 31
  • thx 4 ur answer. But I've tested the same code on a high end system and there are no issues. So in my opinion switching render targets isn't faster then switching whole framebuffer. – Tobias Mar 07 '13 at 08:14