user1207593
user1207593

Reputation: 181

How to determine maximum texture memory on Android OpenGL ES

I'm writing a mixed 2D/3D game on Android and I'm not able to determine how much texture memory I can use. Is there any way to determine maximum texture memory in OpenGL ES?

I have my own texture cache and want to know the maximum threshold of texture memory. I'm making an approximate estimation using activityManager.getMemoryInfo(mi), but on some devices when I try to allocate texture (and many more are already in memory) the application crashes (EGL_BAD_ALLOC). When I set this threshold to a lower value, everything seems to be ok. Does anyone have any idea? Or eventually how to determine that the texture allocation was unsuccessful, instead of crashing.

Upvotes: 15

Views: 6577

Answers (1)

ElementW
ElementW

Reputation: 824

It seems that you are trying to know the amout of VRAM (video RAM) available. The question has already been partially answered.

However, the amount of VRAM does not determine how much textures will fit in it: these ones can be compressed, the amount of VRAM can change because of another app, and we don't know how much memory the video driver uses.

Method 1

From this topic, you can get the maximum amount of texture units; that means the max number that can be bound at the same time:

int[] maxSize = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);

And from this other one, you can retrieve the max texture size:

int[] maxNum = new int[1];
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxNum, 0);

So, logically, you can at least fit maxSize[0]²×maxNum[0] pixels in the VRAM without problems. Even if the VRAM runs out of space, the graphics driver will manage it for us (which may include an application crash in case of starvation).

Anyway, you're probably not going to adjust your textures' size depending on the VRAM amount or the available pixels; it's possible but not profitable, portable and is hard to implement properly. There's a way better way to do this:

Method 2

99% of the time, the amout of VRAM on the graphics chip is proportional to the screen size. Thus, use the screen's DPI (ldpi, mdpi, hdpi, xhdpi) to determine which version (size) of the texture to load (experiment with different (real) devices), so you're safe about the "will it fit for that device?".

Upvotes: 5

Related Questions