Android opengl es 2.0 着色器中的时间相关操作随着时间的推移变得越来越慢
Android opengl es 2.0 timely dependant operation in shader becomes slower over time
我已经为我的 android 应用程序编写了一些着色器代码。它有一些时间相关的动画,在 webgl 版本上工作得很好,着色器代码在下面,但可以找到完整版本 here
vec3 bip(vec2 uv, vec2 center)
{
vec2 diff = center-uv; //difference between center and start coordinate
float r = length(diff); //vector length
float scale = mod(u_ElapsedTime,2.); //it is equal 1 every 2 seconds and trigerring function
float circle = smoothstep(scale, scale+cirleWidth, r)
* smoothstep(scale+cirleWidth,scale, r)*4.;
return vec3(circle);
}
Return 函数在 Fragcolor 中用作颜色的基础。
u_ElapsedTime 通过 uniform 发送到着色器:
glUniform1f(uElapsedTime,elapsedTime);
从 "onDrawFrame" 发送到着色器的时间数据:
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
elapsedTime = (SystemClock.currentThreadTimeMillis()-startTime)/100f;
//Log.d("KOS","time " + elapsedTime);
scannerProgram.useProgram(); //initialize shader
scannerProgram.setUniforms(resolution,elapsedTime,rotate); //send uniforms to shader
scannerSurface.bindData(scannerProgram); //get attribute location
scannerSurface.draw(); //draw vertices with given attributes
}
所以一切看起来都很好。然而,经过一段时间后,看起来有一些滞后,帧数比开始时少。最后,对于该功能,每个循环可能只有一到两帧。同时,似乎 opengl 本身没有一些滞后,因为例如我可以旋转图片并且看不到任何滞后。
延迟的原因可能是什么?
更新:
绑定数据代码:
public void bindData(ScannerShaderProgram scannerProgram) {
//getting location of each attribute for shader program
vertexArray.setVertexAttribPointer(
0,
scannerProgram.getPositionAttributeLocation(),
POSITION_COMPONENT_COUNT,
0
);
在我看来像是精度问题。尝试从您的着色器中获取此行:
float scale = mod(u_ElapsedTime,2.);
并改为在 CPU 上执行。例如
elapsedTime = ((SystemClock.currentThreadTimeMillis()-startTime)%200)/100f;
我已经为我的 android 应用程序编写了一些着色器代码。它有一些时间相关的动画,在 webgl 版本上工作得很好,着色器代码在下面,但可以找到完整版本 here
vec3 bip(vec2 uv, vec2 center)
{
vec2 diff = center-uv; //difference between center and start coordinate
float r = length(diff); //vector length
float scale = mod(u_ElapsedTime,2.); //it is equal 1 every 2 seconds and trigerring function
float circle = smoothstep(scale, scale+cirleWidth, r)
* smoothstep(scale+cirleWidth,scale, r)*4.;
return vec3(circle);
}
Return 函数在 Fragcolor 中用作颜色的基础。
u_ElapsedTime 通过 uniform 发送到着色器:
glUniform1f(uElapsedTime,elapsedTime);
从 "onDrawFrame" 发送到着色器的时间数据:
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
elapsedTime = (SystemClock.currentThreadTimeMillis()-startTime)/100f;
//Log.d("KOS","time " + elapsedTime);
scannerProgram.useProgram(); //initialize shader
scannerProgram.setUniforms(resolution,elapsedTime,rotate); //send uniforms to shader
scannerSurface.bindData(scannerProgram); //get attribute location
scannerSurface.draw(); //draw vertices with given attributes
}
所以一切看起来都很好。然而,经过一段时间后,看起来有一些滞后,帧数比开始时少。最后,对于该功能,每个循环可能只有一到两帧。同时,似乎 opengl 本身没有一些滞后,因为例如我可以旋转图片并且看不到任何滞后。
延迟的原因可能是什么?
更新: 绑定数据代码:
public void bindData(ScannerShaderProgram scannerProgram) {
//getting location of each attribute for shader program
vertexArray.setVertexAttribPointer(
0,
scannerProgram.getPositionAttributeLocation(),
POSITION_COMPONENT_COUNT,
0
);
在我看来像是精度问题。尝试从您的着色器中获取此行:
float scale = mod(u_ElapsedTime,2.);
并改为在 CPU 上执行。例如
elapsedTime = ((SystemClock.currentThreadTimeMillis()-startTime)%200)/100f;