Jump to content
  • Advertisement
Sign in to follow this  
Seroja

OpenGL Shadow Mapping problems

This topic is 3998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm working on a graphics project, and I wanted to add simple shadows to it. I tried to do shadow mapping like in Shadow Mapping Tutorial, but it didn't exactly work. I'm basically having 2 different problems: 1) This seems to be an ATi only problem. As soon as I enable shadowing (e.g. render to a depth map and use it), performance drops to about 3-4 FPS. Otherwise it's smooth (no FPS counter, but it's at least 30 FPS, since there are no lags). Funny thing is, the same happens when I use that tutorial's example - a trivial scene at 5 FPS. This all happened on my X800 GTO2. Then I tested it on nVidia's 6600GT, and it works just fine - tutorial's example at 100FPS, and mine quite smooth. I tried different texture filtering (nearest/linear/mipmap), different Z-buffer depths (16/24/32), different depth map depths (16/24/32/default), and nothing helped - same 3-4 FPS. Seems like copying to the depth map takes huge amount of time. Here's what I use now:
// Initialize depth texture (my texture loader, basically sets the passed params)
if (!glTex::UseTex(NULL, DepthSize, DepthSize, TexDepth, GL_DEPTH_COMPONENT, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE)) return false;
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_ALPHA);

// Update depth texture
glBindTexture(GL_TEXTURE_2D, TexDepth);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, DepthSize, DepthSize);

I should note that I'm not using pbuffers/FBO - only glCopyTexSubImage2D. I know that's inefficient, but FBOs are only available on new hardware, and pbuffers aren't that easy to work with. But this shouldn't be the problem since I'm also using cube mapping, and that is 6 copies. If cube mapping works fast, why shadow mapping doesn't? (Just in case, I updated the drivers - no change) Did anyone have this problem before? I've only seen similar drop in performance when I used NPOT textures with mipmaps on ATi (again, nVidia didn't seem to care). Could this be remedied by using pbuffers? If anyone has an ATi card, please try to download the demo of the tutorial and tell the FPS you get (it could be due to my computer/driver/card combo). 2) This problem is unrelated, and is actually about the algorithm. In the tutorial, only back faces are rendered to the depth map. I cannot do that since I have non-closed (flat) objects in my scene. So what I do is render whole scene, same way, and use polygon offset. Now when rendering, I tried to render bright first, shadowed second, but eventually used the opposite (with depth test of GL_LESS). This is because I have alpha blending in the scene (the grass), and it produced weird bright spots because bright didn't overwrite dark but combined with it. Here's how I render:
// Snap shadow map
SnapDepth(TexDepth);

// Go to reflective object position and snap cubemap
glLoadTransposeMatrixd((double*)RefObjMat);
SnapCubemap(TexCubemap);

// Draw objects (bright light)
glLoadTransposeMatrixd((double*)CameraViewMat);
glAlphaFunc(GL_GEQUAL, 0.01f);
glEnable(GL_ALPHA_TEST);
DrawObjects();
glDisable(GL_ALPHA_TEST);

// Draw objects (dim light)
glDepthFunc(GL_LESS);
glLoadTransposeMatrixd((double*)CameraViewMat);
DrawObjects(3);
glDepthFunc(GL_LEQUAL);

First shadowed, then bright almost works. The problem can be seen in the following screenshot: Image Hosted by ImageShack.us Image Hosted by ImageShack.us First image is camera view, second is light view. Object is a concave mirror. In camera view, you can see the weird bright strip between 2 shadowed parts. That is between shadow due to lighting model (opposite side to light) and shadow due to shadow mapping (self-shadowing). Reversing bright/dark render order didn't change anything regarding that strip. I guess that the reason for it is either imprecision (512*512 depth map, maybe 2K*2K could fix that, but that's too big), or polygon offset (moving shadow back a bit). I can't remove polygon offset because then whole scene would be Z-fighting. Any ideas how to fix it, leaving the mirror completely flat? (A solution is making it a shape with volume, but that's not what I want) P.S. shadow looks rather ugly, any way to improve that without using shaders? (OpenGL 1.4 only). I know nVidia has some kind of PCF built in and enabled through GL_LINEAR filter, but it doesn't seem to work on ATi. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Anyone?

Even if you can't help me to solve the problems, I'd appreciate if you downloaded the tutorial demo, and told me what FPS you get on what hardware. At least I'd know if it's an ATi issue or something else. Surely I'm not the only one with an ATi card here :-)

Share this post


Link to post
Share on other sites
The FPS drop on the ATI card could be caused by unsupported extensions. If something isn't supported by the hardware, OpenGL driver can silently switch to software rendering mode. Try using gDEBugger to figure out what's going on. Also, glGetError could be useful.

EDIT:
Try replacing glCopyTexSubImage2D with glCopyTexImage2D.

Share this post


Link to post
Share on other sites
Just tried tutorial's executable - I get about 240 FPS with my Mobility Radeon 9600 (64m vram).

Share this post


Link to post
Share on other sites
I tried the following:

1) glCopyTexSubImage2D
2) glCopyTexImage2D
3) glReadPixels + glTexImage2D

Performance is roughly the same - 3-4 FPS. Commenting these out effectively disables shadows, but performance goes up to a reasonable level again.

Software rendering seems like a good reason for this behaviour (though what exactly is there to emulate in software - array copying?)

I'll try gDEBugger, never used it, and see what happens.

Though if it works fine on ATi 9600, it does make it more likely that it's my computer making problems than the code. Or maybe some X??? series driver bug...

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja

Performance is roughly the same - 3-4 FPS. Commenting these out effectively disables shadows, but performance goes up to a reasonable level again.



Sounds like glCopy family of functions is implemented slowly in the X800 driver. Perhaps it's doing something stupid like:

glFinish();
memcpy(...);

One way you can make it run faster on the X800 is by using FBO (Frame Buffer Object). With an FBO there is no need to do glCopy, you can just render to texture directly.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush
Quote:
Original post by Seroja

Performance is roughly the same - 3-4 FPS. Commenting these out effectively disables shadows, but performance goes up to a reasonable level again.



Sounds like glCopy family of functions is implemented slowly in the X800 driver. Perhaps it's doing something stupid like:

glFinish();
memcpy(...);

One way you can make it run faster on the X800 is by using FBO (Frame Buffer Object). With an FBO there is no need to do glCopy, you can just render to texture directly.


I wish I could. It's an academic project, and we have only Intel's embedded chipsets (915G). It has OpenGL 1.4, so that's what I'm targeting. I'll try tomorrow the code on that chipset, but for now I've been working at home, keeping an eye on extensions I use.

FBOs didn't even make into a standard, and I haven't seen them supported on anyt embedded chipset (e.g. only nVidia and ATi seem to support them). They aren't even ARB, only EXT, and didn't make into any OpenGL standard (yet?).

If it won't work on Intels, I'll either fallback to planar shadows or go for pbuffers (both alternatives sound bad, especially since I might waste time on pbuffers only to find that performance didn't improve).

glCopy could indeed be implemented badly, but:
1) It's 1 copy of 512*512 depth texture per frame, why would it decrease preformance x3 or even x4?
2) I use same glCopy for cube mapping, which is 6 256*256 RGB(A) textures, and even increasing that to 6 512*512 textures didn't hurt performance as much.

I used gDEBugger, 200k calls per frame :S Code doesn't seem that long, and I used display lists (I have a skybox, 3 spheres, grass grid of about 6*6, + cubemapping and shadow mapping). OK, spheres are well-tesselated, but still I doubt I need VBOs for such low amount of geometry.
Anyway, I used removal of draw commands feature, brought that down to 5k, FPS went up by 1 -> which means enormous amount of GL calls isn't the problem. And previous version had 90k calls and worked fine.

Seems like either I have something weird on my computer, or I discovered a render path that the driver devs haven't thought of. Actually, I didn't find anywhere any shadow mapping with glCopy, except for that tutorial. Others use cube shadow maps / pbuffers / FBOs / etc. Maybe it's a subtle hint that I'm not using hardware the way it should be used...

Share this post


Link to post
Share on other sites
Quote:
Original post by Seroja

glCopy could indeed be implemented badly, but:
1) It's 1 copy of 512*512 depth texture per frame, why would it decrease preformance x3 or even x4?

If the OpenGL driver is doing something silly like using the CPU to copy data, that would kill performance because accessing VRAM from CPU is very slow on many graphic cards. Think 10MB/s instead of 10GB/s !!!

Quote:
Original post by Seroja
2) I use same glCopy for cube mapping, which is 6 256*256 RGB(A) textures, and even increasing that to 6 512*512 textures didn't hurt performance as much.

Maybe the OpenGL driver supports copying an RGBA texture using the GPU, but it falls back to 10MB/s for depth textures? In that case you can lie to the driver and say it's an RGBA texture so it does a fast copy and then change it back to DEPTH texture. You can use PBO (pixel buffer object) for that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!