I am trying to implement a faster way of dynamically uploading textures to the GPU by using the EGLImage extension.
I first create a GraphicBuffer and bind it to an OpenGL texture:
buffers[index] = new GraphicBuffer(width, height, HAL_PIXEL_FORMAT_RGBA_8888, GraphicBuffer::USAGE_SW_WRITE_OFTEN | GraphicBuffer::USAGE_HW_TEXTURE); EGLint eglImageAttributes[] = {EGL_IMAGE_PRESERVED_KHR, EGL_FALSE, EGL_NONE}; EGLClientBuffer nativeBuffer = buffers[index]->getNativeBuffer(); eglImages[index] = _eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, nativeBuffer, eglImageAttributes); _glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImages[index]);
I lock the buffer and copy pixels to it:
buffers[index]->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &pixels); /* Copy pixels to buffer... */ buffers[index]->unlock();
This works the first frame but after I have drawn the texture in OpenGL I am unable to modify the data in the GraphicBuffer (so the same texture is always drawn). How can I modify the OpenGL texture by simply modifying the GraphicBuffer? I am not sure if I am simply doing this wrong or if there is some kind of lock on the buffer after OpenGL has used it.
Hi.. It should be possible to use GraphicBuffer to continually update a texture. Just a few ideas...
1) Try using these flags when creating the GraphicBuffer:
android::GraphicBuffer::USAGE_HW_TEXTURE
android::GraphicBuffer::USAGE_HW_2D
GRALLOC_USAGE_SW_READ_OFTEN
GRALLOC_USAGE_SW_WRITE_OFTEN);
2) Try setting the EGL_IMAGE_PRESERVED_KHR attribute to true
3) After updating the texture, make sure you set the active texture, and bind..
glActiveTexture(GL_TEXTURE0));
glEnable(GL_TEXTURE_2D));
glBindTexture(GL_TEXTURE_2D, texID);