Sign in to follow this  
aevanthony

OpenGL Efficient Rendering

Recommended Posts

aevanthony    100
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
aevanthony    100
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
V-man    813
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
V-man    813
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
aevanthony    100
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
aevanthony    100
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;

glUseProgram(p); // ENABLE SHADER PROGRAM

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);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glViewport(0, 0, SD, SD);
glDrawBuffer(AttchPoint[TexRenderRead]);
glActiveTexture(GL_TEXTURE0 + TexRenderRead);
glPushMatrix();
glBegin(GL_QUADS);
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);
glEnd();
glPopMatrix();
glPopAttrib();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
TexRenderRead = (TexRenderRead + 1)%2;
TexRenderWrite = (TexRenderWrite + 1)%2;

glUseProgram(p2); // DISABLE SHADER PROGRAM

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glBegin(GL_QUADS);
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);
glEnd();
glPopMatrix();
glutSwapBuffers();
Sleep(1);



Share this post


Link to post
Share on other sites
aevanthony    100
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
V-man    813
If your driver offers http://www.opengl.org/registry/specs/EXT/wgl_swap_control.txt
then wglGetSwapIntervalEXT(0) disables and wglGetSwapIntervalEXT(1) enables it.

Share this post


Link to post
Share on other sites
aevanthony    100
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; http://glprogramming.com/paste.php?paste=192

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  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now