How to get around the performance issues with Buffer.put() and Android OpenGL

心不动则不痛 提交于 2020-01-12 10:39:27

问题


It is a Java issue in general, though in this particular case I am using Vertex Arrays in Android for OpenGL. To use this basic GL system, you must use Java's Buffer system in native allocation mode. This is very slow on Java. And roughly 40-50% of my entire application's time is spent inside of buffer.put().

Is there any way to speed this up while staying in Java (ie, don't use the native sdk)?


回答1:


Avoid allocations in general. Use a pool of buffers and juggle them around as needed. You can have a few standard sizes and waste a few bytes at the end in exchange for performance. Also, when using OpenGL you typically don't need to rewrite your buffers every frame (unless you do extensive skinning or animation?). Normally, you have your pre-baked arrays, transform objects using matrices, and that's it.




回答2:


I've encountered a similar problem when integrating Java and JOGL - my solution was to manage the buffer resources in C, and use JNI to pass the pointer to the buffer to Java using the method

jobject NewDirectByteBuffer(JNIEnv * env, void * address, jlong capacity); 

found in jni.h. When you need to update the offset into the buffer, use reflection to manually modify the "address" field found in java.nio.Buffer. If adding more elements would cause you to exceed the capacity of the buffer in C, use an array list in C and have the direct buffer point to the array backing for the list.




回答3:


The only thing you can do really is to batch your jobs and hope the implementation in the phone is decent, since the penalty comes from locking and unlocking. If you batch your calls the driver should lock and unlock once. If you do it "spontaneously" you will lock and unlock all the time, which will give you a hefty performance hit. If the driver isn't clever enough to only map the buffers into ram once instead of for every call your best bet is to simply minimize the number of puts.



来源:https://stackoverflow.com/questions/3437259/how-to-get-around-the-performance-issues-with-buffer-put-and-android-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!