在onDrawFrame中调用GLES20会导致长时间延迟(GLES20 calls in onDrawFrame causes long delay)

我编写了一个游戏,当调用onDrawFrame()时,我首先更新游戏状态(游戏逻辑和绘图缓冲区),然后继续进行实际绘图。 在Moto G和Nexus 7上,一切都运行顺畅,每个onDrawFrame()调用只需要1-5ms。 但是,在三星Galaxy S3上,90%的时间onDrawFrame()调用需要30-50ms才能更新。

进一步研究这个问题我发现问题完全取决于我在下面附加的第一个渲染方法: - (编辑:块现在是glclear();删除了不必要的调用以获取句柄,请参阅注释)

public void render(float[] m, FloatBuffer vertex, FloatBuffer texture, ShortBuffer indices, int TextureNumber) {
        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertex);            
        GLES20.glEnableVertexAttribArray(mPositionHandle);            

        GLES20.glVertexAttribPointer(mTexCoordLoc, 2, GLES20.GL_FLOAT, false, 0, texture);            
        GLES20.glEnableVertexAttribArray(mTexCoordLoc);            

        GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);            

        int mSamplerLoc = GLES20.glGetUniformLocation(fhGraphicTools.sp_Image, "s_texture");            

        GLES20.glUniform1i(mSamplerLoc, TextureNumber);            

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.capacity(), GLES20.GL_UNSIGNED_SHORT, indices);            
        // Disable vertex arrays used
        GLES20.glDisableVertexAttribArray(mPositionHandle);            
        GLES20.glDisableVertexAttribArray(mTexCoordLoc);            
    }

在每个onDrawFrame()中调用上述方法5次,以在不同的纹理图集上绘制内容。 (前4个实际上是游戏的背景,即1个长矩形)。 记录了代码的每一行所花费的时间后,我发现S3上的延迟始终位于以下行之一:

int mPositionHandle = GLES20.glGetAttribLocation(fhGraphicTools.sp_Image, "vPosition");

要么

GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.capacity(), GLES20.GL_UNSIGNED_SHORT, indices);

滞后仅发生在render()的第一次调用中,因为后续调用每次大约需要1-2ms。 我已经尝试禁用第一个render()调用,但是之后第二个只需要1-2ms就可以成为延迟的来源,在同一行下。

有没有人知道我的代码中S3无法处理的错误? 似乎S3在每个onDrawFrame()的开头都有问题开始GL调用,但为什么这样的行为令我感到困惑。 可以采取哪些措施来减少“启动”延迟?

非常感谢您耐心阅读。

编辑代码以采取Selvin的建议。

I have written a game in which when onDrawFrame() is called I update the game state first (game logic and the buffers for drawing) and then proceed to do the actual drawings. On the Moto G and Nexus 7 everything works smoothly and each onDrawFrame() call takes only between 1-5ms. However, on the Samsung Galaxy S3, 90% of the time the onDrawFrame() call takes as long as 30-50ms to update.

Further investigating the issue I found the problem wholly lies on the first render method which I attach below:- (Edit: the block is now on glclear(); having removed the unnecessary calls to get the handles, please refer to the comments)

public void render(float[] m, FloatBuffer vertex, FloatBuffer texture, ShortBuffer indices, int TextureNumber) {
        GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertex);            
        GLES20.glEnableVertexAttribArray(mPositionHandle);            

        GLES20.glVertexAttribPointer(mTexCoordLoc, 2, GLES20.GL_FLOAT, false, 0, texture);            
        GLES20.glEnableVertexAttribArray(mTexCoordLoc);            

        GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);            

        int mSamplerLoc = GLES20.glGetUniformLocation(fhGraphicTools.sp_Image, "s_texture");            

        GLES20.glUniform1i(mSamplerLoc, TextureNumber);            

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.capacity(), GLES20.GL_UNSIGNED_SHORT, indices);            
        // Disable vertex arrays used
        GLES20.glDisableVertexAttribArray(mPositionHandle);            
        GLES20.glDisableVertexAttribArray(mTexCoordLoc);            
    }

The above method is called 5 times in each onDrawFrame() to draw things on different texture atlas. (the first 4 are actually the game's background which is 1 long rectangle). Having logged the time it takes in each line of the code I found that the lag I am having on the S3 always reside in one of the below lines:

int mPositionHandle = GLES20.glGetAttribLocation(fhGraphicTools.sp_Image, "vPosition");

or

GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.capacity(), GLES20.GL_UNSIGNED_SHORT, indices);

The lag only occurs in the first call of render() as the subsequent calls takes about 1-2ms each. I have tried disabling the first render() call but then the second which before only take 1-2ms now become the source of the lag, under the same lines.

Does anyone have an idea of what is wrong in my code which the S3 could not handle? It seem that the S3 have problem commencing GL calls at the beginning of each onDrawFrame(), but why is there such behavior is puzzling me. What can be done to decrease the "startup" delay?

Many thanks for your patience in reading.

Edited code to take Selvin's recommendation.


原文:https://stackoverflow.com/questions/25991167

在onDrawFrame中调用GLES20会导致长时间延迟(GLES20 calls in onDrawFrame causes long delay)_电脑培训

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值