View Full Version : Factors affecting the Efficiency of Shaders(GLSL)?

06-26-2013, 08:30 AM
My application simply loads an image as a Texture & rotate it in Z-axis.This is my code.
----Beginning of code----
Reference Source code.

virtual void OnInit()

shader = SM.loadfromFile("vertexshader.txt","fragmentshader.txt"); // load (and compile, link) from file
if (shader==0)
std::cout << "Error Loading, compiling or linking shader\n";
ProgramObject = shader->GetProgramObject();
locAngle = glGetUniformLocation(ProgramObject, "uAngle");

// Create a Texture from file.
pTexture = cwc::TextureFactory::CreateTextureFromFile("Penguins.jpg");

if (!pTexture)
std::cout << "***WARNING: Failed loading texture!!\n";

virtual void OnRender(void)

if (pTexture)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST); // Disable Filtering!

if (shader)
glUniform1f(locAngle, angle);

if (shader) shader->setUniform1i("myTexture", 0);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-glXpos, -glYpos, glZpos);//Left Bottom
glTexCoord2f(1.0f, 0.0f); glVertex3f( glXpos, -glYpos, glZpos);//Right Bottom
glTexCoord2f(1.0f, 1.0f); glVertex3f( glXpos, glYpos, glZpos);//Right Top
glTexCoord2f(0.0f, 1.0f); glVertex3f(-glXpos, glYpos, glZpos);//Left Top

if (shader) shader->end();

Vertex Shader Source:

uniform float uAngle;
varying vec2 vTexCoord;
attribute vec4 position;

void main(void)
mat3 rotation = mat3
vec3( cos(uAngle), sin(uAngle), 0.0),
vec3(-sin(uAngle), cos(uAngle), 0.0),
vec3( 0.0, 0.0, 1.0)
vec3 projected_position = rotation * position.xyz;
vTexCoord = gl_MultiTexCoord0.xy;
gl_Position = gl_ModelViewProjectionMatrix * vec4(projected_position, 1.0);

Fragment Shader Source:

uniform sampler2D myTexture;
varying vec2 vTexCoord;
void main (void)
vec4 color = texture2D(myTexture, vTexCoord);
gl_FragColor = color;
Now my question is when i run this application in my PC it runs well(Spec:nvidia Geforce 210 GPU ,Processor:Intel 3 GHz)
But it consumes 50-55% of processor usage in my PC whereas it consumes only 2-8% of processor usage in some other PC's.
I've also tried to do the same Image Rotation using glRotate() & through custom Rotation matrix methods. it consumes only 0-2% of processor usage only.
How glRotate() consumes only 1 of processor usage?
What causes this increased Processor usage?
What are the factors which affect this GLSL performance?
Experts please help me on this. Thank you.

06-26-2013, 08:43 AM
First of all, why would a shader being executed on the GPU drive your CPU usage through the roof? What does your rendering loop look like? And please format your code properly. Please read the forum guidelines. Thank you.

Dark Photon
06-26-2013, 04:06 PM
What thokra said. Your code isn't doing much of anything, though immediate mode (glBegin..glEnd) is pretty well the most inefficient way to submit batches to the GPU.

I suspect your "CPU" usage problem probably has more to do with something like you have sync-to-vblank OFF on your box whereas it's ON on the others (put a glFinish() after swapbuffers and then time from this point to this point on the next frame -- see if you get some insanely small time or something more like 16.6ms (aka 60Hz -- typical LCD scan-out rate). You can force this in the driver control panel among other places.

Alternatively, if you are running sync-to-vblank on your box, could be the driver is doing something very inefficient like a busy wait waiting for VSYNC. Look at your driver docs to see if/how you can tune this.

Or, could be you don't have the OpenGL drivers for your GPU installed properly on your box and you're falling back to software OpenGL rendering (all CPU; no GPU).

And please format your code properly. Please read the forum guidelines.

In particular: Forum Guidelines (http://www.opengl.org/discussion_boards/showthread.php/176139-Forum-Posting-Guidelines?p=1230021#post1230021). Mark your code blocks with
... or ... blocks. Can use highlight=glsl for GLSL code.

06-27-2013, 12:34 AM
Thank you thokra for your reply. Since i'm new to this GLSL i cant understand the Hardware/Software dependency of GLSL. It would be very useful if u specify any links regarding this pipeline of this shaders.

06-28-2013, 01:39 AM
thank you @ Dark Photon for your kind reply. i think the problem is with the frame rate. my application was running in 60 fps.

06-28-2013, 04:01 AM
i cant understand the Hardware/Software dependency of GLSL

I suggest you don't use the term dependency here. Programmable shading is part of a bigger pipepline usually referred to as the rendering pipeline (http://www.opengl.org/wiki/Rendering_Pipeline_Overview). The simple fact in regards to your specific problem is that shaders are being executed exclusively on the GPU - therefore, no matter how complex a shader is, if your not doing much work on the CPU, i.e. in your application which triggers shaders execution by calling a drawing command, your CPU usage should by way below 50%.

It would be very useful if u specify any links regarding this pipeline of this shaders.

The most important link is the one above. In general I suggest you use the wiki and this here board to learn stuff and ask questions. Also, check out fellow board member Alfonse's tutorial here (http://www.arcsynthesis.org/gltut/).

06-28-2013, 08:38 AM
you are right thokra. Actually i noticed that all the Tutorial Applications are running at 60 fps by default. So when i tried to control the frame rate by making the application sleep for 50ms in
OnIdle()ievirtual void OnIdle()
}. Then the CPU usage is reduced to 12%. But i still cant understand why it is taking 50% of cpu load to render a simple model.

Thanks for your advice,Since i'm new to this OpenGL, i'll gradually improve myself in framing questions & understanding the problem.

07-01-2013, 01:08 AM
Thank you all. I've noticed that by default this OpenGL Apps are running at 60 fps & this call back to this OnIdle() function takes more CPU usage. So i've unregistered this call back in glut header file & not my application is taking less cpu usage.

07-01-2013, 02:56 AM
But i still cant understand why it is taking 50% of cpu load to render a simple model.

It doesn't. The number of draw calls isn't the problem as you effectively do almost nothing when drawing a quad. The frequency at which you submit draw calls is, however. Say your frame is done in a little under 1ms - and even that is far too much - you will execute OnRender() approx. 1000 times per second in an infinite loop, i.e. your CPU is constantly doing stuff with your process (your application). As you've already observed, letting your main thread sleep for a while reduces the CPU load substantially. The same thing happens when you VSYNC: SwapBuffers() simply blocks, i.e. your thread can only continue when the functions returns which will also lead to a tremendous reduction in CPU load.

07-01-2013, 03:44 AM
Thank you for your reply i understood the problem.