Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 274
user1207593 Avatar asked Feb 13 '12 19:02

user1207593


People also ask

How do you calculate texture memory?

An uncompressed 2D texture with mipmaps (full mipmaps chain) takes at least width*height*pixel_size*2 bytes of video memory. An uncompressed cubic texture with no mipmaps takes at least size*size*pixel_size*6 bytes of data.

How much memory does a 4k texture use?

Because each 4k texture takes about 90 Mb of graphics card memory. Suppose you have 60 textures, you will automatically have 5400 Mb of used GPU memory.

How much space does OpenGL take?

GL_RGBA8 takes up 32-bits per pixel, which is 4 bytes. Therefore, 512*512*4 = 1MB.

What is GPU texture memory?

Texture memory is a type of digital storage that makes texture data readily available to video rendering processors (also known as GPUs), typically 3D graphics hardware.


1 Answers

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?".

like image 103
ElementW Avatar answered Sep 23 '22 11:09

ElementW