Sign in to follow this  
speciesUnknown

OpenGL Problem with depth buffer, usual methods exhausted

Recommended Posts

speciesUnknown    527
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 to
		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");
		}

		// depth component which seems to have the problem
		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_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 point
		GLenum 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.

Share this post


Link to post
Share on other sites
speciesUnknown    527
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.

Share this post


Link to post
Share on other sites
IronFox    148
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 :P
Have 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?

Share this post


Link to post
Share on other sites
speciesUnknown    527
Quote:
Original post by IronFox
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 :P
Have 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();
}

Share this post


Link to post
Share on other sites
IronFox    148
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.

Share this post


Link to post
Share on other sites
speciesUnknown    527
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 data
uniform sampler2D tex0;

uniform vec3 uSunDir;
uniform vec3 uSunColour;

varying vec3 world_position;
varying vec3 vertex_normal;

void main()
{
// texture calculations
vec4 texel = texture2D(tex0,gl_TexCoord[0].st);

// single directional light from the sun
vec3 lighting = uSunColour * dot(normalize(vertex_normal), normalize(-uSunDir));

// first pass, with ambient light and directional light from the sun
gl_FragData[0] = vec4( lighting * texel.rgb * (gl_FrontMaterial.diffuse).rgb, (texel.a * gl_FrontMaterial.diffuse.a) );

// albedo G_Buffer
gl_FragData[1] = vec4( texel.rgb, (texel.a * gl_FrontMaterial.diffuse.a) );

// world space normal g buffer
gl_FragData[2] = vec4( vertex_normal, 1.0 );

// world space position g buffer
gl_FragData[3] = vec4( world_position, 1.0 );

// screen space depth texture g buffer
gl_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.

Share this post


Link to post
Share on other sites
speciesUnknown    527
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now