影响着色器效率的因素(GLSL)? [英] Factors affecting the Efficiency of Shaders(GLSL)?

查看:239
本文介绍了影响着色器效率的因素(GLSL)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序只是将图像加载为纹理&在Z轴旋转。这是我的代码。

----代码开头----

参考源代码。

http://www.opengl.org/sdk/docs/tutor...SL_Texture.zip

main.cpp

代码:

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





----结束----

现在我的问题是什么时候我在我的电脑上运行这个应用程序它运行良好(规格:nvidia Geforce 210 GPU,处理器:英特尔3 GHz)

但它在我的电脑中消耗50-55%的处理器使用率,而它只消耗2其他一些PC的处理器使用率为-8%。

我也尝试使用glRotate()&通过自定义旋转矩阵方法。它仅消耗0-2%的处理器使用率。

glRotate()如何只消耗1个处理器使用?

是什么原因导致处理器使用率增加?

影响GLSL性能的因素有哪些?

专家请帮我解决这个问题。谢谢。



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

推荐答案

您好解决方案非常简单。默认情况下,freeglut以60 fps呈现,并且不断调用OnIdle()次数。(当Message Queue为空时调用OnIdle())。



这就是应用程序占用更多CPU的原因。因此,如果你不想使用这个空闲函数,我们可以从Header文件中的glut主循环中的回调列表中删除该函数。



通过这种方式我们可以减少CPU使用率。(如果需要)。
Hi the solution is very simple. By default freeglut is rendering at 60 fps & it is constantly calling the OnIdle() number of times.(OnIdle() is called when Message Queue is Empty).

That's why the application takes more CPU usage. so if u don't want to use this Idle function we can remove that function from call back list from the glut main loop in Header file.

By this way we can reduce the CPU usage.(if needed).


这篇关于影响着色器效率的因素(GLSL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆