Sign in to follow this  

OpenGL Efficient Rendering

Recommended Posts

Hmm, This application I tested with has a 128x128 texture, and the shader I'm using just does a glFragColor = texture2d...etc, a simple color assignment. Now when I scale the window up very large, my entire system suffers performance problems, mostly... actually more specifically, audio playback gets very bad and CPU shoots up. So I was wondering... what does OpenGL in place of when no shader programs are created? What's the default behavior? Maybe even... would it be a good idea to disable the shader program entirely when OpenGL is rendering a texture to the window, and just have it active while the FBO is rendering to the texture?

Share this post

Link to post
Share on other sites
I created a new Shader program, assigned it NO shader. I used glUseProgram(textureProgram); before the FBO rendered to a texture, I used; glUseProgram(DoNothing) after.

On my current PC, CPU usage while scaling my applications window during run time. With a 256x256 texture rendered to a window scaled up to 1680x1050; CPU from ~30% to ~8%

Mmmm... that's like what, 60-75% less cpu usage? Much better, can't wait to see how this runs on a slower gpu/system.

That'll go a long way to broaden the range of GPU's that will smoothly run this program I'm sure. Mmmmm.

:) I'll have to test it all out more and all that mess, but so far happy.

Share this post

Link to post
Share on other sites
Are you rendering with an infinite loop? In that case, with a single core CPU, it should use 100% CPU if vsync is off. If on, it will be like 1 to 5%.
With dual core, instead of 100%, it would be somewhere around 50%.
With dual core and if you have vsync on, it would be 1 to 5%.

Share this post

Link to post
Share on other sites
The other way is to render when the OS sends a notification because you window has been exposed or needs a refresh for some reason. On Windows, it sends a WM_PAINT message.
If you are using GLUT, then get rid of glutIdleFunc. Your render functions gets called when GLUT received a notification from the OS.

Share this post

Link to post
Share on other sites
When removing that idle function nothing redraws to the screen.

Just to add, I made sure vsync was forced... on both machines, but based on some reading in to Vista there's problems with vsync overall. In any case, I haven't seen anything near 50% usage on either machine. Well, except on this FX5200... but that's before and after glUseProgram.

When I swap the programs and set glUseProgram to use the program with no shader attached on this machine with a FX5200 it doesn't seem to take effect. The visual display doesn't render as if the texture is scaled, it still looks like the fragment shader is executing per pixel... using a program where no shader is attached!! Ironically the card I don't even need for did as I expected... (x1950pro) /ultra-sigh.

That's perplexing, I can't seem to figure out why that is, every thing's the same as the machine I ran this on last night on my ATI box.

Is it possible this FX card is ignoring glUseProgram calls I'm using per frame? Any other ideas? Can you think of something else?

I mean the only thing I did was akin to;

1. glUseProgram(p) // fun stuff
2. FBO render to texture. // receives fun stuff
3. glUseProgram(p2) // do no more fun stuff
4. Render texture to window. // texture rendered without extra fun stuff

*Kicks a bucket*

Share this post

Link to post
Share on other sites
The CPU rises only really* when I scale up, *only on this machine (of the two I tested). It's as if the glUseProgram(p2) doesn't do what it should. (Use a program which has no shader associated with it, to prevent a fragment shader from doing anything to every pixel. Hence more efficient, the goal is to force only* the texture to be processed with my fragment shader.)

This is the significant portion of the rendering loop.

static float da = 1.0;


glBindTexture(GL_TEXTURE_2D, FBOTex[TexRenderWrite]);
GLint TexLoc = glGetUniformLocation(p, "tex"); glUniform1i(TexLoc, 0);
vLoc = glGetUniformLocation(p, "aL"); glUniform1f(vLoc, u0);
vLoc = glGetUniformLocation(p, "aR"); glUniform1f(vLoc, u1);

glViewport(0, 0, SD, SD);
glActiveTexture(GL_TEXTURE0 + TexRenderRead);
glTexCoord2f(0.0, 0.0); glVertex3f(-da, -da, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-da, da, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( da, da, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( da, -da, 0.0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
TexRenderRead = (TexRenderRead + 1)%2;
TexRenderWrite = (TexRenderWrite + 1)%2;


glTexCoord2f(0.0, 0.0); glVertex3f(-da, -da, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-da, da, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f( da, da, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f( da, -da, 0.0);

Share this post

Link to post
Share on other sites
I fixed it, after researching I came across the fact that I should have done this instead to disable my fragment shader for rendering to the window;

Reference the above code and do this instead;

glUseProgram(p2) -> glUseProgram(0)

I can scale my window without a dramatic performance hit! it's a lot better. I need to do better still, it still uses too much(maybe) CPU. ~35% when scaled to over 800x600... maybe it's the best I can get though, even WinAmp has a comparable CPU usage on this machine, so maybe this is what I've been looking for.

Vista apparently has problems with vsync. Is there a way through OpenGL I can force vsync?

[Edited by - aevanthony on April 24, 2008 3:51:09 PM]

Share this post

Link to post
Share on other sites
If your driver offers
then wglGetSwapIntervalEXT(0) disables and wglGetSwapIntervalEXT(1) enables it.

Share this post

Link to post
Share on other sites
Nice, thanks for the patience! :D

Just so it's here for a reference, I found a nice snippet of code on glprogramming .com to work with that;

Thanks again! I think I can be happy with what I have finally and move toward blending multiple textures and such now. Mmmm, the reward of progress is addictive.

[Edited by - aevanthony on April 24, 2008 3:25:24 PM]

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  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By test opty
      Hi all,
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
  • Popular Now