Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: Factors affecting the Efficiency of Shaders(GLSL)?

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    9

    Exclamation 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.
    http://www.opengl.org/sdk/docs/tutor...SL_Texture.zip
    main.cpp
    Code :
    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\n";
    	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!!\n";
    }
     
    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:
    Code :
    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:
    Code :
    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.
    Last edited by Balaji_mcr; 06-27-2013 at 03:27 AM. Reason: Code Correction

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    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.

  3. #3
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,126
    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 [code]...[/code] or [highlight=cpp]...[/highlight] blocks. Can use highlight=glsl for GLSL code.
    Last edited by Dark Photon; 06-27-2013 at 05:33 AM.

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    9
    thank you @ Dark Photon for your kind reply. i think the problem is with the frame rate. my application was running in 60 fps.

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    9
    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.

  7. #7
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    9
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •