   Problem with depth buffer, usual methods exhausted
  I'm having a problem with the depth buffer on my FBO. The problem comes and goes, and doesn't seem to be related to any particular code changes, its fairly arbitrary. I'll have the problem for a few hours and then it will go away again. This is the image I am getting, Its like my depth buffer is full of junk. I've tried all the usual settings; depth testing and depth writing are both on, its nothing to do with any opengl state that im explicitly setting, as demonstrated by the seemingly random occurance of the bug.This is my setup code for my FBO, it has 5 textures and a depth component.void RenderTarget::resize(size_t width, size_t height) {this->width = width; this->height = height;glBindFramebuffer(GL_FRAMEBUFFER, fbo);glBindTexture(GL_TEXTURE_2D, tex[kiColor]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex[kiColor], 0);GLenum err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}// same again for albedo, normal and position// depth texture which shaders are writing toglBindTexture(GL_TEXTURE_2D, tex[kiDepth]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, /*GL_RGB32F_ARB,*/ width, height, 0, GL_RGB, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, tex[kiDepth], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}// depth component which seems to have the problemglBindTexture(GL_TEXTURE_2D, tex[kiDepthBuffer]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex[kiDepthBuffer], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}// no problems of any kind are reported at any pointGLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);if (status != GL_FRAMEBUFFER_COMPLETE) {throw std::runtime_error("FBO incomplete.\n");}glBindTexture(GL_TEXTURE_2D, 0);unbind();}I get no runtime_error's thrown at any time.All other opengl software runs just fine on this machine with no visible artifacts.What could be causing this?Edit: More information: It startd happening this time when I made a complete copy of the project. I have made no code changes of any kind; I simply rebuilt the project and ran it. When I build and run the old project (which is identical, dont forget) from a different location on my hdd, I do not get the problem.Further edit: shrank large image of largeness.   
  A reboot fixed the problem. Now, both copies of the project render correctly. Its always tempting to blame your graphics card drivers for a bug like this, but considering that I've exhausted all avenues within my own code, and considering that the issue seems connected to something external to my own code, is it fair to blame nvidia on this occasion?THe middle ground of course is that I'm doing something wrong, which is exposing some kind of undefined or buggy behaviour of the graphics card; perhaps it manages to recover from incorrect states which I have set 99% of the time but fails at other times? Or perhaps something I am doing is just plain confusing it.   ;  Well, I suspect you haven't forgotten to clear the depth buffer when you start rendering into the fbo? Depth range is acceptable, too? Probably.I noticed you're using color attachment 0 and then color attachment 4. It's not like that _should_ cause anything remotely like this, but then again nothing should :PHave you tried using color attachment 0 and 1 without the gap? What happens if you use other texture formats? E.g. GL_RGBA8 or just GL_DEPTH_COMPONENT?Does any of this happen if you replace the depth texture with a buffer instead?Just guessing semi-aimlessly here. Perhaps some more info on how exactly you render will shed some light on this mystery?;Quote:Original post by IronFoxWell, I suspect you haven't forgotten to clear the depth buffer when you start rendering into the fbo? Depth range is acceptable, too? Probably.I noticed you're using color attachment 0 and then color attachment 4. It's not like that _should_ cause anything remotely like this, but then again nothing should :PHave you tried using color attachment 0 and 1 without the gap? What happens if you use other texture formats? E.g. GL_RGBA8 or just GL_DEPTH_COMPONENT?Does any of this happen if you replace the depth texture with a buffer instead?Just guessing semi-aimlessly here. Perhaps some more info on how exactly you render will shed some light on this mystery?That code has been truncated, there are 5 texture attachments (represented by the // "same again for albedo, normal and position" comment.Here is the full code, I decided to spare people the need to read the whole thing, void RenderTarget::resize(size_t width, size_t height) {this->width = width; this->height = height;glBindFramebuffer(GL_FRAMEBUFFER, fbo);glBindTexture(GL_TEXTURE_2D, tex[kiColor]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex[kiColor], 0);GLenum err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}glBindTexture(GL_TEXTURE_2D, tex[kiAlbedo]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, width, height, 0, GL_RGBA, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, tex[kiAlbedo], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}glBindTexture(GL_TEXTURE_2D, tex[kiNormal]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, width, height, 0, GL_RGB, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, tex[kiNormal], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}glBindTexture(GL_TEXTURE_2D, tex[kiPosition]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, width, height, 0, GL_RGB, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, tex[kiPosition], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}glBindTexture(GL_TEXTURE_2D, tex[kiDepth]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexImage2D(GL_TEXTURE_2D, 0, GL_R16F, /*GL_RGB32F_ARB,*/ width, height, 0, GL_RGB, GL_FLOAT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, tex[kiDepth], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}glBindTexture(GL_TEXTURE_2D, tex[kiDepthBuffer]);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex[kiDepthBuffer], 0);err = glGetError();if(err != GL_NO_ERROR){throw std::runtime_error("A GL error occurred");}GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);if (status != GL_FRAMEBUFFER_COMPLETE) {throw std::runtime_error("FBO incomplete.\n");}glBindTexture(GL_TEXTURE_2D, 0);unbind();};  Well, i guess that explains the gap :).It unfortunately doesn't exactly explain why you ran into those issues. I suspect changing some formats has not yielded acceptable results? Is the issue reproducible now that you rebooted or does it no longer occur?I've run into quite a couple rather strange phenomenons using OpenGL. Sometimes, however, it turned out that the place where an issue showed was not where it actually occurred. Some residual incorrect state caused by something done before. In such cases commenting out everything unnecessary and iteratively re-embedding it helped pinpoint the location (not necessarily fix the issue though).Can't be any shader either, can it? I've encountered the occasional actually correct shader, screwed up during compilation, generating very very odd results.It might also help to print out relevant parts of the OpenGL render status before you start rendering. Perhaps to the console or some log file. If your program is sufficiently short then that will probably not yield unexpected results but it did save my hide quite a few times in larger programs where actually valid things still lingered on in the OpenGL state. Most commonly multi-textures still bound, though i don't expect that to be an issue here. Then again, who knows. :)To round off aimless guessing: This isn't caused by some multi-threading-issue, is it? Just asking, been there, caused other effects in my case though.   ;  Booting up my machine this afternoon has caused the issue to start up again. I have an unknown window of opportunity to find the bug before it disappears once more.No, my renderer is not multithreaded.Im certain there must be a issue with my depth component in my render target - this is a render target used for g-buffers. This is my shader for the first pass, where I generate g-buffers:// input datauniform sampler2D tex0;uniform vec3 uSunDir;uniform vec3 uSunColour;varying vec3 world_position;varying vec3 vertex_normal;void main(){// texture calculationsvec4 texel = texture2D(tex0,gl_TexCoord[0].st);// single directional light from the sunvec3 lighting = uSunColour * dot(normalize(vertex_normal), normalize(-uSunDir));// first pass, with ambient light and directional light from the sungl_FragData[0] = vec4( lighting * texel.rgb * (gl_FrontMaterial.diffuse).rgb, (texel.a * gl_FrontMaterial.diffuse.a) );// albedo G_Buffergl_FragData[1] = vec4( texel.rgb, (texel.a * gl_FrontMaterial.diffuse.a) );  // world space normal g buffergl_FragData[2] = vec4( vertex_normal, 1.0 );  // world space position g buffergl_FragData[3] = vec4( world_position, 1.0 ); // screen space depth texture g buffergl_FragData[4] = vec4(gl_FragCoord.z);}This is the path on which I generate my g buffers:void Renderer::drawGBuffers(RenderManifest& manifest){glPushMatrix();set3DProjection(manifest.camera);int ambient_shader = this->resource_manager->shaders.findIndex("default");Resources::Shader * shad = resource_manager->shaders.get(ambient_shader);shad->apply();current_shader_program = shad->getProgramId();skybox.applyUniforms(current_shader_program);glEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0.5f);context->render_target->bind();glClearColor(skybox.backdrop_colour[0],skybox.backdrop_colour[1],skybox.backdrop_colour[2],0.f);glClearDepth(0.f);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);glDepthMask(GL_TRUE);glDepthFunc(GL_LESS);drawOpaqueList(manifest.list_3d);context->render_target->unbind();/*context->render_target->bind(1);drawTranslucentList(manifest.list_3d);context->render_target->unbind();*/glPopMatrix();}As you can see, I am clearly setting the only states which could possibly affect the depth buffer to correct values. There are no opengl state changes within drawOpaqueList which are not between glPushAttrib and glPopAttrib.;  I found the problem, mostly from looking at somebody elses FBO code. I was only generating 5 textures in the constructor of my RenderTarget class - the texture unit for the depth component was undefined. This causes undefined behaviour, and thus the seemingly random occurrence of the bug.RenderTarget::RenderTarget(std::size_t width, std::size_t height) : width(width), height(height) {if (!glGenFramebuffers) {throw std::runtime_error("glGenFramebuffers not supported.\n");}glGenFramebuffers(1, &fbo);glGenTextures(6, tex);resize(width, height);}What annoys me is that opengl failed to give me any warnings or errors. IF i were deflecting blame I would definately point it here. Here is a screenshot of what the scene SHOULD have looked like:Now free from jpeg compression artifacts