# Problems with rendering to FBO

**URL:** <https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789>\
**Category:** PowerVR Insider\
**Created:** [August 2, 2010, 1:32pm UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789 "2010-08-02T13:32:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![3dgamer](https://avatars.discourse-cdn.com/v4/letter/3/b3f665/32.png) [@3dgamer](https://forums.imgtec.com/u/3dgamer)\
**Post date:** [August 2, 2010, 1:32pm UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/1 "2010-08-02T13:32:29Z")

</div>

Hi all,  
I’m having a little trouble with rendering to FBOs, with both PVRFrame and the actual device, a Sony Ericsson Satio. The first problem is that I’m unable to create a stencil-renderbuffer in PVRFrame (for glRenderbufferStorage with GL\_STENCIL\_INDEX8 it says "Error 500 (Invalid Enum): unknown or unsupported internal format), however on the device it works without any problems.   
  
Here’s my code:

Code:

  
  
bool InitBackBuffers(iString& errorInfo){  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //Create Stencil-Renderbuffer  
&nbsp;&nbsp;&nbsp; glGenRenderbuffers(1, &stencilRenderBuffer);  
&nbsp;&nbsp;&nbsp; glBindRenderbuffer(GL\_RENDERBUFFER,stencilRenderBuffer);  
&nbsp;&nbsp;&nbsp; //This line causes Problems in PVRFrame  
&nbsp;&nbsp;&nbsp; glRenderbufferStorage(GL\_RENDERBUFFER, GL\_STENCIL\_INDEX8, screenwidth,screenheight);  
  
&nbsp;&nbsp;&nbsp; //Create depth-texture  
&nbsp;&nbsp;&nbsp; //the functions are simply some object-oriented wrappers for the gl functions  
&nbsp;&nbsp;&nbsp; depthTexture=new CTexture2D();  
&nbsp;&nbsp;&nbsp; depthTexture-\>Bind();  
&nbsp;&nbsp;&nbsp; depthTexture-\>SetData(0,GL\_DEPTH\_COMPONENT,screenwidth,screenheight,GL\_UNSIGNED\_INT,0);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureWrappingS(GL\_CLAMP\_TO\_EDGE);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureWrappingT(GL\_CLAMP\_TO\_EDGE);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureMagFilter(GL\_NEAREST);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureMinFilter(GL\_NEAREST);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::BindTexture(GL\_TEXTURE\_2D,0);  
  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //BACKBUFFERCNT is one at the moment  
&nbsp;&nbsp;&nbsp; for(int i=0; i\<BACKBUFFERCNT; i++){  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //gernerate color rendertarget  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; renderTargets _= new CTexture2D();  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; renderTargets-\>SetData(0,GL\_RGBA,screenwidth,screenheight,GL\_UNSIGNED\_BYTE,NULL);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; renderTargets-\>Bind();  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureWrappingS(GL\_CLAMP\_TO\_EDGE);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureWrappingT(GL\_CLAMP\_TO\_EDGE);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureMagFilter(GL\_NEAREST);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderState::SetTextureMinFilter(GL\_NEAREST);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderState::BindTexture(GL\_TEXTURE\_2D,0);_  
  
_&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //generate framebuffer  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glGenFramebuffers(1,&framebuffers);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glBindFramebuffer(GL\_FRAMEBUFFER, framebuffers);&nbsp;&nbsp;&nbsp;_   
  
_&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glFramebufferTexture2D(GL\_FRAMEBUFFER,GL\_DEPTH\_ATTACHMENT, GL\_TEXTURE\_2D, depthTexture-\>GetGLName(),0);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //glFramebufferRenderbuffer(GL\_FRAMEBUFFER,GL\_STENCIL\_ATTACHMENT,GL\_RENDERBUFFER,stencilRenderBuffer);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glFramebufferTexture2D(GL\_FRAMEBUFFER, GL\_COLOR\_ATTACHMENT0,GL\_TEXTURE\_2D, renderTargets[0]-\>GetGLName(), 0);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GLenum status = glCheckFramebufferStatus(GL\_FRAMEBUFFER);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(status!=GL\_FRAMEBUFFER\_COMPLETE){  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(status==GL\_FRAMEBUFFER\_INCOMPLETE\_ATTACHMENT) errorInfo.Append("FBO incomplete.n");&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(status==GL\_FRAMEBUFFER\_INCOMPLETE\_MISSING\_ATTACHMENT) errorInfo.Append("FBO incomplete\_Missing.n");&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; glBindFramebuffer(GL\_FRAMEBUFFER, 0);  
&nbsp;&nbsp;&nbsp; return true;  
}_  
  
  
_void SetRenderTarget(GLint num){  
&nbsp;&nbsp;&nbsp; //only call gl functions if its necessary  
&nbsp;&nbsp;&nbsp; if(num==currFramebuffer) return;  
&nbsp;&nbsp;&nbsp; // -1 = default framebuffer  
&nbsp;&nbsp;&nbsp; if(num==-1) glBindFramebuffer(GL\_FRAMEBUFFER, 0);  
&nbsp;&nbsp;&nbsp; else glBindFramebuffer(GL\_FRAMEBUFFER, framebuffers[num]);  
&nbsp;&nbsp;&nbsp; currFramebuffer=num;  
}_  
  
  
  
_unsigned long framenum=0;_  
  
_void Render(CNode\*\* nodes, unsigned int count, CCamera& activeCam){_  
  
_&nbsp;&nbsp;&nbsp; framenum++;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetClearColor(0,0,1,1);  
&nbsp;&nbsp;&nbsp; for(int i=BACKBUFFERCNT-1; i\>=0; i--){  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetRenderTarget(i);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; glClear( GL\_COLOR\_BUFFER\_BIT|GL\_DEPTH\_BUFFER\_BIT|GL\_STENCIL\_BUFFER\_BIT);  
&nbsp;&nbsp;&nbsp; }_  
  
_&nbsp;&nbsp;&nbsp; //Render Some test objects  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; CRenderingEnvironment rendenv;  
&nbsp;&nbsp;&nbsp; rendenv.framenum=framenum;  
&nbsp;&nbsp;&nbsp; rendenv.ViewMat=activeCam.GetViewMatrix();&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; rendenv.ProjectionMat=activeCam.GetProjectionMatrix();  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; TVector\<Light\*\> lightlist;  
&nbsp;&nbsp;&nbsp; TVector\<RenderCol\> meshlist;  
&nbsp;&nbsp;&nbsp; TMat43 identityMatrix;  
&nbsp;&nbsp;&nbsp; GLfloat ambientLight[4]={0,0,0,0};  
&nbsp;&nbsp;&nbsp; for(int i=0; i\<count; i++)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IPAQRenderer::CollectRenderingInformation(rendenv,nodes,identityMatrix,meshlist,lightlist,ambientLight);&nbsp;&nbsp;&nbsp;_   
  
_&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; for(int i=0; i\<meshlist.Size(); i++){  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; meshlist.mesh-\>LoadShaderDataToGL(meshlist.submeshindex,rendenv,meshlist.modelmat);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; meshlist.mesh-\>LoadVertexDataToGL(rendenv,meshlist.modelmat);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; meshlist.mesh-\>LoadSubmeshDataToGL(meshlist.submeshindex,rendenv,meshlist.modelmat);  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; meshlist.mesh-\>RenderSubmesh(meshlist.submeshindex,rendenv,meshlist.modelmat);  
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //Object rendering finished  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; // A shader for rendering a textured fullscreenquad multiplied with a color  
&nbsp;&nbsp;&nbsp; CShaderProgram\* HUDShaderProgram = IPAQShaderManager::GetHudShader();  
&nbsp;&nbsp;&nbsp; IPAQRenderState::UseShaderProgram(\*HUDShaderProgram);&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //Render To system-provided Framebuffer;  
&nbsp; &nbsp;&nbsp;&nbsp; SetRenderTarget(-1);  
&nbsp;&nbsp;&nbsp; glClear( GL\_COLOR\_BUFFER\_BIT|GL\_DEPTH\_BUFFER\_BIT|GL\_STENCIL\_BUFFER\_BIT );  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetDepthTest(false);  
&nbsp;&nbsp;&nbsp; IPAQRenderState::SetBlendingEnabled(false);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; renderFullscreenQuad(renderTargets[0]);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; IPAQHudDrawer::DoDrawings();&nbsp;   
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; eglSwapBuffers(iEglDisplay, iEglSurface );  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;   
}_  
  

  
  
The second problem is with sampling from a depth texture. I do this by replacing the line  

Code:

renderFullscreenQuad(renderTargets[0]);

  
with  

Code:

renderFullscreenQuad(depthTexture);

  
Using PVRFrame I can see&nbsp; a nice grayscale image of the depth of scene, however on the device the screen is just totally black. And sometimes it changes it's color to white for a short time or another strange rendering artifact occurs: some tiles of the screen get swapped randomly, eg. I can see a part of the text rendered at the top left corner rotated about 90 degrees in the middle of the screen ^^.  
  
BTW: The engine code for PVRFrame and symbian is exactly the same, only a small "base-layer" for out- and input is different.  
  
Thank you for your help in advance!

---

<div class="post-metadata">

**Author:** ![slion](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@slion](https://forums.imgtec.com/u/slion)\
**Post date:** [August 9, 2010, 5:19am UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/2 "2010-08-09T05:19:32Z")

</div>

1/ Might be an issue with your graphic card, although I doubt it.  
2/ Maybe try using your depth texture only after the first swap buffer call, or make sure you call glFinish or glFlush before using it.

---

<div class="post-metadata">

**Author:** ![3dgamer](https://avatars.discourse-cdn.com/v4/letter/3/b3f665/32.png) [@3dgamer](https://forums.imgtec.com/u/3dgamer)\
**Post date:** [August 15, 2010, 7:14pm UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/3 "2010-08-15T19:14:35Z")

</div>

Hi,  
  
1. Don’t think it’s the problem, i have a rather new graphics card. My system:  
&nbsp; -windows 7 64 bit  
&nbsp; -XFX Geforce GTX275  
&nbsp; -Intel Quad Q9550  
  
2. Tried it, now the screen simply stays white, but the strange tile-swapping still occurs sometimes. The screen staying white might be caused by my shader, it’s normally only used for color textures, so all calculations including texture sampling are performed with lowp precision.

---

<div class="post-metadata">

**Author:** ![slion](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@slion](https://forums.imgtec.com/u/slion)\
**Post date:** [August 16, 2010, 6:34am UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/4 "2010-08-16T06:34:21Z")

</div>

They say in the doc everything ends up being highp anyway on the PC Emulation.

---

<div class="post-metadata">

**Author:** ![3dgamer](https://avatars.discourse-cdn.com/v4/letter/3/b3f665/32.png) [@3dgamer](https://forums.imgtec.com/u/3dgamer)\
**Post date:** [August 16, 2010, 7:33am UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/5 "2010-08-16T07:33:12Z")

</div>

Yep, I think that's why the depth-texturing problem does not occur on the PC.  
3DGamer2010-08-16 09:34:07

---

<div class="post-metadata">

**Author:** ![mkandula](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.imgtec.com/mkandula/32/123_2.png) [@mkandula](https://forums.imgtec.com/u/mkandula)\
**Post date:** [January 6, 2012, 10:57am UTC](https://forums.imgtec.com/t/problems-with-rendering-to-fbo/789/6 "2012-01-06T10:57:07Z")

</div>

was there any solution to the problem ? I am experiencing the same (1) !
