shelll
shelll

Reputation: 3383

android opengl es 1.1 texture compression on the fly

i have to use texture compression as my app is currently using up to 100MB of ram for textures.

i am creating textures from Views, so it is not possible for them to be created in a compressed format. how can i compress them with ETC1/ATC/PVRTC on the fly and send them to gpu? i tried:

GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, ETC1.ETC1_RGB8_OES, bitmap, 0);

i also tried other compression formats supported by my phone, but the texture is always white. the input bitmap is an RGB_565 and mip-maps are disabled.

is it possible to send a bitmap as a texture to opengl es 1.1 so it gets automatically compressed on android, like it is possible on a pc?

Upvotes: 3

Views: 2493

Answers (2)

shelll
shelll

Reputation: 3383

with help from Arne Bergene Fossaa i get to this solution:

int size = m_TexBitmap.getRowBytes() * m_TexBitmap.getHeight();
ByteBuffer bb = ByteBuffer.allocateDirect(size); // size is good
bb.order(ByteOrder.nativeOrder());
m_TexBitmap.copyPixelsToBuffer(bb);
bb.position(0);

ETC1Texture etc1tex;
// RGB_565 is 2 bytes per pixel
//ETC1Texture etc1tex = ETC1Util.compressTexture(bb, m_TexWidth, m_TexHeight, 2, 2*m_TexWidth);

final int encodedImageSize = ETC1.getEncodedDataSize(m_TexWidth, m_TexHeight);
ByteBuffer compressedImage = ByteBuffer.allocateDirect(encodedImageSize).order(ByteOrder.nativeOrder());
// RGB_565 is 2 bytes per pixel
ETC1.encodeImage(bb, m_TexWidth, m_TexHeight, 2, 2*m_TexWidth, compressedImage);
etc1tex = new ETC1Texture(m_TexWidth, m_TexHeight, compressedImage);

//ETC1Util.loadTexture(GL10.GL_TEXTURE_2D, 0, 0, GL10.GL_RGB, GL10.GL_UNSIGNED_SHORT_5_6_5, etc1tex);
gl.glCompressedTexImage2D(GL10.GL_TEXTURE_2D, 0, ETC1.ETC1_RGB8_OES, m_TexWidth, m_TexHeight, 0, etc1tex.getData().capacity(), etc1tex.getData());

bb = null;
compressedImage = null;
etc1tex = null;

i know about the ETC1Util.compressTexture and ETC1Util.loadTexture, but they were giving corrupted textures. good thing is that i went from 100MB down to 26MB with native memory consumption. but this solution is slow as hell. and even though it is done on a separate thread with min priority, the rendering thread is totally blocked. is there a more efficent way? or do i have to create these ETC1 textures on the first run on a new device and save them to SD card for later reuse?

Upvotes: 4

Arne Bergene Fossaa
Arne Bergene Fossaa

Reputation: 652

You cannot do that through OpenGL ES - only ETC decompression is supported. ETC compression is not really trivial to do fast and good - you might have a look at http://devtools.ericsson.com/etc and implement etcpack in your program, though.

Upvotes: 1

Related Questions