简体   繁体   English

为什么OpenGL渲染在Android上使用SDL2这么慢?

[英]Why is OpenGL rendering so slow with SDL2 on Android?

I am porting a C++ 2D game to Android with the help of the NDK and the SDL; 我正在NDK和SDL的帮助下将C ++ 2D游戏移植到Android; and I use OpenGL to render the sprites. 并且我使用OpenGL渲染精灵。 The performances are quite disappointing. 表演很令人失望。

After some investigations, I have found that the bottleneck is in the OpenGL calls during the rendering procedure. 经过一些调查,发现瓶颈在于渲染过程中的OpenGL调用。 Wait a second! 等一会儿! Before pasting the classic answer telling not change GL states, not to bind already bound textures and so on, please read the following. 在粘贴经典答案(告诉您不要更改GL状态,不绑定已经绑定的纹理等)之前,请阅读以下内容。

Actually, even if I display a single 364x353 rgba texture in a middle of a black screen, it happens that the rendering takes almost 20 ms. 实际上,即使我在黑屏的中间显示单个364x353 rgba纹理,也会发生渲染花费将近20 ms的情况。 The procedure can be summarized like this: 该过程可以总结如下:

Edit The code below used to call glFlush() . 编辑下面用于调用glFlush()的代码。 As thokra pointed out in the comments, this is not necessary. 正如thokra在评论中指出的那样,这是没有必要的。 Nevertheless, removing it did not improve the performances. 但是,删除它并不能提高性能。

glClear( GL_COLOR_BUFFER_BIT );

glBindTexture( the_texture );
glEnable(GL_BLEND);

glEnableClientState( GL_COLOR_ARRAY );
glColorPointer( 4, GL_FLOAT, 0, colors );

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 2, GL_FLOAT, 0, positions );

glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 2, GL_FLOAT, 0, texture_positions );

glDrawArrays( GL_TRIANGLE_FAN, 0, vertex_count );

glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glDisable(GL_BLEND);

//glFlush();
SDL_GL_SwapWindow( window );

The execution of the last two lines of this function (flush and swap) is very irregular in duration. 该函数的最后两行(刷新和交换)的执行持续时间非常不规律。 It can take from 4 to 12 ms! 可能需要4到12毫秒!

The best hints I could find on the web are this question on StackOverflow and this thread on Google Groups . 我在网上可以找到的最好的提示是StackOverflow上的这个问题Google网上论坛上的这个线程

The answers to the questions on StackOverflow are of no help. 有关StackOverflow的问题的答案没有帮助。 According to the author of the question: 根据问题的作者:

clearing vs not clearing frame with glClear doesn't affect fps for me. 使用glClear清除与不清除帧对我来说不影响fps。 Neither does enabling/disabling blending. 启用/禁用混合都不会。

and

[…] just want to see if using compressed textures improves fill rate. […]只想看看使用压缩纹理是否可以提高填充率。 It seems it doesn't […] 似乎没有[…]

The Google Groups thread is very similar to my problem but it ended when the author gave up: Google网上论坛线程与我的问题非常相似,但是当作者放弃时就结束了:

So basically, there's no way to draw a 480x854 image on screen with OpenGL at 60fps because of the texel reading bottleneck. 因此,基本上,由于纹理像素读取瓶颈,无法使用OpenGL以60fps的速度在屏幕上绘制480x854图像。

Seriously, no way? 说真的,没办法吗? Why the call to both glFlush() and SDL_GL_SwapWindow() takes so long? 为什么对glFlush()SDL_GL_SwapWindow()的调用都花这么长时间? And why is it so irregular? 为什么这么不规则? Can't I do anything about it? 我对此无能为力吗?

I did a quick search in SDL2's source code and found that SDL_GL_SwapWindow() uses the Java Native Interface to call a Java method which will do the work. 我在SDL2的源代码中进行了快速搜索, 发现 SDL_GL_SwapWindow()使用Java本机接口调用将完成工作的Java方法。 Can it be the cause of the irregularity? 可能是不合规定的原因吗? What can I do about this? 我该怎么办?

I don't know if you are still doing it but try to consult Google for this matter. 我不知道您是否仍在这样做,但请尝试咨询Google。 You will not get a rational answer from anywhere else on the web. 您将无法从网络上的任何其他地方获得合理的答案。 20ms is basically 50FPS. 20ms基本上是50FPS。 You mostly get like 250FPS doing those simple task you asked. 您通常会像250FPS一样执行您要求的简单任务。 What if, just what if, it is INTENTIONALLY LEFT OUT TO BE SLOW? 如果只是缓慢地慢下来怎么办?

We used to code in simple intercept calls and used whole VGA card. 我们曾经使用简单的拦截电话进行编码,并使用了整个VGA卡。 But nowadays even in smartphones it is ran in something like VIRTUAL MODE. 但是如今,即使在智能手机中,它也以虚拟模式运行。 You need to get those DIRECT ACCESS to video chip in assembly or c level. 您需要将这些直接访问以组装或c级的方式获得到视频芯片。 Ask and consult google about this. 询问并咨询Google。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM