3

I am writing a 2D game in openGL and I ran into some performance problems while rendering a couple of textures covering the whole window.

What I do is actually create a texture with the size of the screen, render my scene onto that texture using FBO and then render the texture a couple of times with a different offsets to get a kind of "shadow" going. But when I do that I get a massive performance drop while using my integrated video card.

So all in all I render 7 quads onto the whole screen(background image, 5 "shadow images" with a black "tint" and the same texture with its true colors). I am using RGBA textures which are 1024x1024 in size and fit them in a 900x700 window. I am getting 200 FPS when I am not rendering the textures and 34 FPS when I do (in both scenarios I actually create the texture and render the scene onto it). I find this quite odd because I am only rendering 7 quads essentially. A strange thing is also that when I run a CPU profiler it doesn't suggest that this is the bottleneck(I know that opengl uses a pipeline architecture and this thing can happen but most of the times it doesn't).

When I use my external video card I get consistent 200 FPS when I do the tests above. But when I disable the scene rendering onto the texture and disable the texture rendering onto the screen I get ~1000 FPS. This happens only to my external video card - when I disable the FBO using the integrated one I get the same 200 FPS. This really confuses me.

Can anyone explain what's going on and if the above numbers sound right?

Integrated video card - Intel HD Graphics 4000

External video card - NVIDIA GeForce GTX 660M

P.S. I am writing my game in C# - so I use OpenTK if that is of any help.

Edit:

First of all thanks for all of the responses - they were all very helpful in a way, but unfortunately I think there is just a little bit more to it than just "simplify/optimize your code". Let me share some of my rendering code:

//fields defined when the program is initialized

Rectangle viewport;
//Texture with the size of the viewport
Texture fboTexture;
FBO fbo;

//called every frame
public void Render()
{
    //bind the texture to the fbo
    GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo.handle);
    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, fboTexture,
       TextureTarget.Texture2D, texture.TextureID, level: 0);

    //Begin rendering in Ortho 2D space
    GL.MatrixMode(MatrixMode.Projection);
    GL.PushMatrix();
    GL.LoadIdentity();
    GL.Ortho(viewport.Left, viewport.Right, viewport.Top, viewport.Bottom, -1.0, 1.0);
    GL.MatrixMode(MatrixMode.Modelview);
    GL.PushMatrix();
    GL.LoadIdentity();

    GL.PushAttrib(AttribMask.ViewportBit);
    GL.Viewport(viewport);

    //Render the scene - this is really simple I render some quads using shaders
    RenderScene();

    //Back to Perspective
    GL.PopAttrib(); // pop viewport
    GL.MatrixMode(MatrixMode.Projection);
    GL.PopMatrix();
    GL.MatrixMode(MatrixMode.Modelview);
    GL.PopMatrix();

    //Detach the texture
    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, fboTexture, 0,
                    0, level: 0);
    //Unbind the fbo
    GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

    GL.PushMatrix();
    GL.Color4(Color.Black.WithAlpha(128)); //Sets the color to (0,0,0,128) in a RGBA format

    for (int i = 0; i < 5; i++)
    {
        GL.Translate(-1, -1, 0);
        //Simple Draw method which binds the texture and draws a quad at (0;0) with
        //its size
        fboTexture.Draw();
    }
    GL.PopMatrix();
    GL.Color4(Color.White);
    fboTexture.Draw();
}

So I don't think there is actually anything wrong with the fbo and rendering onto the texture, because this is not causing the program to slow down on both of my cards. Previously I was initializing the fbo every frame and that might have been the reason for my Nvidia card to slow down, but now when I am pre-initializing everything I get the same FPS both with and without fbo.

I think the problem is not with the textures in general because if I disable textures and just render the untextured quads I get the same result. And still I think that my integrated card should run faster than 40 FPS when rendering only 7 quads on the screen, even if they cover the whole of it.

Can you give me some tips on how can I actually profile this and post back the result? That would be really useful.

Edit 2:

Ok I experimented a bit and managed to get much better performance. First I tried rendering the final quads with a shader - this didn't have any impact on performance as I expected.

Then I tried to run a profiler. But I far as I know SlimTune is just a CPU profiler and it didn't give me the results I wanted. Then I tried gDEBugger. It has an integration with visual studio which I later found out that it does not support .NET projects. I tried running the external version but it didn't seem to work (but maybe I just haven't played with it enough).

The thing that really did the trick was that rather than rendering the 7 quads directly to the screen I first render them on a texture, again using fbo, and then render the final texture once onto the screen. This got my fps from 40 to 120. Again this seem kind of curios to say the least. Why is rendering to a texture way faster than directly rendering to the screen? Nevertheless thanks for the help everyone - it seems that I have fixed my problem. I would really appreciate if someone come up with reasonable explanation of the situation.

6
  • 1
    You probably would think that 200 FPS for only 7 quads isn't enough as well. According to popular benchmarks like this one, your integrated card is actually almost three times slower than the discrete one. This aligns with your results (200 FPS vs 40 FPS) pretty well. To profile your code you should try a profiler like SlimTune. (I was going to recommend the NProf which I have used previously, but it is not supported anymore.) Oct 30, 2012 at 11:40
  • 1
    Since you scale the 1024x1024 image down, every pixel must at least read 4 texels. Also, each pixel must undergo 7 read-modify-write operations. So, in total, assuming 32-bit depth, you have 145 MiB of memory transfer per frame. Your integrated GPU has a theoretical bandwidth of 21 GiB/s, which it shares with the CPU. Thus, the theoretical limit (though admittedly without considering cache, but I have never seen a CPU/GPU getting close to its "theoretical" fantasy specifications either) is 147FPS if the CPU does not access any memory. Your numbers don't seem too unreasonable.
    – Damon
    Oct 30, 2012 at 12:14
  • By the way, is there a hindrance to fetching 7 samples in a shader and writing out one fragment in your FBO? This will both eliminate the read-modify-write operations and transform 80-90% of the memory accesses while fetching texture into L2 cache hits.
    – Damon
    Oct 30, 2012 at 12:18
  • Thanks for those numbers - it really seems that the problem is the performance of the GPU. I will do some tests , run a gpu profiler, try out the suggestion with the 7 samples in a shader and then I will post my results back.
    – Milcho
    Oct 30, 2012 at 13:39
  • Please don't use methods like GL.MatrixMode(MatrixMode.Projection);, the fixed function pipeline is used to much by too many people.
    – Felix K.
    Oct 31, 2012 at 8:21

3 Answers 3

6

Obviously this is a guess since I haven't seen or profiled your code, but I would guess that integrated cards are just struggling with your post-processing (drawing the texture several times to achieve your "shadow" effect).

I don't know your level of familiarity with these concepts, so sorry if I'm a bit verbose here.

About Post-Processing

Post-processing is the process of taking your completed scene, rendered to a texture, and applying effects to the image before displaying it on the screen. Typical uses of post-processing include:

  • Bloom - Simulate brightness more naturally by "bleeding" bright pixels into neighboring darker ones.

  • High Dynamic Range rendering - Bloom's big brother. The scene is rendered to a floating-point texture, allowing greater color ranges (as opposed to the usual 0 for black and 1 for full brightness). The final colors displayed on the screen are calculated using the average luminance of all the pixels on the screen. The effect of all of this is that the camera acts somewhat like the human eye - in a dark room, a bright light (say, through a window) looks extremely bright, but once you get outside, the camera adjusts and light only looks that bright if you stare directly at the sun.

  • Cel-shading - Colors are modified to give a cartoon-like appearance.

  • Motion blur

  • Depth of field - The in-game camera approximates a real one (or your eyes), where only objects at a certain distance are in-focus and the rest are blurry.

  • Deferred shading - A fairly advanced application of post-processing where lighting is calculated after the scene has been rendered. This costs a lot of video RAM (it usually uses several fullscreen textures) but allows a large number of lights to be added to the scene quickly.

In short, you can use post-processing for a lot of neat tricks. Unfortunately...

Post Processing Has a Cost

The cool thing about post-processing is that its cost is independent of the scene's geometric complexity - it will take the same amount of time whether you drew a million triangles or whether you drew a dozen. That's also its drawback, however. Even though you're only rendering a quad over and over to do post-processing, there is a cost for rendering each pixel. If you were to use a larger texture, the cost would be larger.

A dedicated graphics card obviously has far more computing resources to apply post-processing, whereas an integrated card usually has much fewer resources it can apply. It is for this reason that "low" graphics settings on video games often disable many post-processing effects. This wouldn't show up as a bottleneck on a CPU profiler because the delay happens on the graphics card. The CPU is waiting for the graphics card to finish before continuing your program (or, more accurately, the CPU is running another program while it waits for the graphics card to finish).

How Can You Speed Things Up?

  • Use fewer passes. If you halve the passes, you halve the time it takes to do post-processing. To that end,

  • Use shaders. Since I didn't see you mention them anywhere, I'm not sure if you're using shaders for your post-processing. Shaders essentially allow you to write a function in a C-like language (since you're in OpenGL, you can use either GLSL or Cg) which is run on every rendered pixel of an object. They can take any parameters you like, and are extremely useful for post-processing. You set the quad to be drawn using your shader, and then you can insert whatever algorithm you'd like to be run on every pixel of your scene.

3
  • Thanks for the informative answer. The reason I am rendering everything onto a texture is exactly for post-processing purposes. I will apply more advanced techniques in the future (including some of the ones you mentioned), but for now I need to get this problem sorted out. And do you think that 7 "fullscreen" quads should slow down the cars as much?
    – Milcho
    Oct 30, 2012 at 11:30
  • I can definitely imagine the post-processing being a large factor contributing to the slow down. However, the only way to be sure is to profile. You'll want to use something that can track both GPU performance and CPU performance. Though I haven't used it myself, gDEBugger is a common tool for profiling OpenGL programs.
    – Matt Kline
    Oct 30, 2012 at 14:30
  • Another situation say if you want bloom some effects without affect the other scene objects. You render all sfxs into a seperate render target. And you blur or do some tone mapping process of the render target. But sfxs normally use a small area of the render target, But you have to blend it back to the fullscreen backbuffer. Any suggestions to optimize this?
    – Joey.Z
    Aug 15, 2016 at 11:24
1

Seeing some code would be nice. If the only difference between the two is using an external GPU or not, the difference could be in memory management (ie how and when you're creating an FBO, etc.), since streaming data to the GPU can be slow. Try moving anything that creates any sort of OpenGL buffer or sends any sort of data to it to initialization. I can't really give any more detailed advice without seeing exactly what you're doing.

3
  • oh yeah and I've used OpenTK extensively, the only difference between it and plain OpenGL is that GL methods are slightly more expensive due to the way OpenTK uses delegates to bind methods. I love the way OpenTK organizes constants into enums too, makes everything so much quicker with autocomplete... Oct 30, 2012 at 0:28
  • It's not very easy to give the code since I've wrapped opengl into my own classes. Nevertheless I will do that tomorrow since it is quite late here. For now I can say that I barely do any initialization on each frame - everything is pre-initialized and I only reuse the textures and the FBO
    – Milcho
    Oct 30, 2012 at 0:48
  • Even a high-level overview of what you're doing would be helpful, better would be if you could follow the code and write down the GL method calls in order or something like that. Oct 30, 2012 at 1:02
1

It isn't just about number of quads you render, and I believe in your case it's got more to do with amout of triangle filling your video card has to do.

As was mentioned, the common way to do fullscreen post-processing is with shaders. If you want better performance on your integrated card and can't use shaders, then you should simplify your rendering routine.

Make sure you really need alpha blending. On some cards/drivers rendering textures with alpha channel can significantly reduce performance.

A somewhat low-quality way to reduce the amount of fullscreen filling would be to first perform all of your shadow draws on another, smaller texture (say, 256x256 instead of 1024x1024). Then you would draw a quad with that compound shadow texture onto your buffer. This way instead of 7 1024x1024 quads you would only need 6 256x256 and one 1024x1024. But you will lose in resolution.

Another technique, and I'm not sure it can be applied in your case, is to pre-render your complex background so you'll have to do less drawing in your rendering loop.

1
  • Thanks for the response. I tried disabling alpha blending - no luck. I am only using shaders for rendering onto the texture. And after that, as I posted in my edit, I just render it onto the screen with no shader processing because I don't need to for now. The low-quality post processing technique is quite neat I will use that in the future, but I don't thing that's the slowdown reason.
    – Milcho
    Oct 30, 2012 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.