Factors affecting the Efficiency of Shaders(GLSL)?

My application simply loads an image as a Texture & rotate it in Z-axis.This is my code.
----Beginning of code----
Reference Source code.
www.opengl.org/sdk/docs/tutorials/ClockworkCoders/downloads/GLSL_Texture.zip
main.cpp


virtual void OnInit()
{
	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);

	shader = SM.loadfromFile("vertexshader.txt","fragmentshader.txt"); // load (and compile, link) from file
	if (shader==0) 
	std::cout << "Error Loading, compiling or linking shader
";
	else
	  {         
		 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!!
";
}

virtual void OnRender(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  if (pTexture) 
  {
	 pTexture->bind(0);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);	
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Disable Filtering!
  }

  if (shader) 
  {
	 shader->begin();
	 glUniform1f(locAngle, angle);         
  }
 
  if (shader) shader->setUniform1i("myTexture", 0);

	  
	  glBegin(GL_QUADS);
		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		
	  glEnd();

	  if (shader) shader->end();
	  glutSwapBuffers();	
}

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

----End----
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.

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.

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. Mark your code blocks with [noparse]

...

or

...

[/noparse] blocks. Can use highlight=glsl for GLSL code.

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.

thank you @ Dark Photon for your kind reply. i think the problem is with the frame rate. my application was running in 60 fps.

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. 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.

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()
{
_sleep(50);
}. 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.

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.

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.

Thank you for your reply i understood the problem.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.