best way to render to grayscale texture in OpenGL (es 2)

Started by
1 comment, last by ika_ 6 years, 3 months ago

I'm interested in rendering a grayscale output from a shader, to save into a texture for later use. I only want an 1 channel 8 bit texture rather than RGBA, to save memory etc.

I can think of a number of possible ways of doing this in OpenGL off the top of my head, just wondering what you guys think is the best / easiest / most compatible way, before I dive into coding? This has to work on old android OpenGL ES2 phones / tablets etc, so nothing too funky.

  1. Is there some way of rendering to a normal RGBA frame buffer, then using glCopyTexSubImage2D or similar to copy + translate the RGBA to a grayscale texture? This would seem the most obvious, and the docs kind of suggest it might work.
  2. Creating an 8 bit framebuffer. If this is possible / a good option?
  3. Rendering out RGBA, using glReadPixels, translating on the CPU to grayscale then reuploading as a fresh texture. Slow and horrible but this is a preprocess, and would be a good option is this is more guaranteed to work than other methods.
Advertisement

If you look at glTexImage2D's documentation, it notes that texture images can only be stored as "GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA". While other versions of OpenGL do support diffrent internal formats for image data, (The OpenGL 4 documentation specifies internal formats such as GL_R8, which I think is what you would be looking for), OpenGL ES2 does not seem to support this.

While compressed internal formats do not seem to be available in base OpenGL ES2, extensions do exist that allow this to be achieved, if you have them. Consider the following:

https://www.khronos.org/registry/OpenGL/extensions/OES/OES_required_internalformat.txt

https://www.khronos.org/registry/OpenGL/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt

Now, I couldn't find any extensions that offer an 8-bit internal format from briefly looking through the list of extensions, that were also compatible with OpenGL ES2. This extension also might be useful, however it is only compatible with OpenGL ES3 and does not give the option to store an 8-bit internal format:

https://www.khronos.org/registry/OpenGL/extensions/OES/OES_texture_compression_astc.txt

 

 

This topic is closed to new replies.

Advertisement