Fast paletted screen blit with OpenGL

不羁的心 提交于 2019-12-13 02:33:58

问题


A game uses software rendering to draw a full-screen paletted (8-bit) image in memory.

What's the fastest way to put that image on the screen, using OpenGL?

Things I've tried:

  • glDrawPixels with glPixelMap to specify the palette, and letting OpenGL do the palette mapping. Performance was horrendous (~5 FPS).
  • Doing palette mapping (indexed -> BGRX) in software, then drawing that using glDrawPixels. Performance was better, but CPU usage is still much higher than using 32-bit DirectDraw with the same software palette mapping.

Should I be using some kind of texture instead?


回答1:


  • glDrawPixels with glPixelMap to specify the palette, and letting OpenGL do the palette mapping. Performance was horrendous (~5 FPS).

That's not a surprise. glDrawPixels is not very fast to begin with and glPixelMap will do the index/palette → RGB conversion on the CPU in a surely not very optimized codepath.

  • Doing palette mapping (indexed -> BGRX) in software, then drawing that using glDrawPixels.

glDrawPixels is about one of the slowest functions in OpenGL there is. This has two main reasons: First it's a codepatch not very much optimized, second it writes directly into the target framebuffer, hence forcing the pipeline into synchronization every time it's called. Also on most GPU it isn't backed by any cache.

What I suggest is you place your indexed image into single channel texture, e.g. GL_R8 (for OpenGL-3 or later) or GL_LUMINANCE8, and your palette into a 1D RGB texture, so that the index used as texture coordinate does look up the color. Using a texture as a LUT is perfectly normal. With this combination you use a fragment shader for in-situ palette index to color conversion.

The fragment shader would look like this

#version 330

uniform sampler2D image;
uniform sampler1D palette;

in vec2 texcoord;

void main()
{
    float index = tex2D(image, texcoord).r * 255.f; // 255 for a 8 bit palette
    gl_FragColor = texelFetch(palette, index, 0);
}


来源:https://stackoverflow.com/questions/12524813/fast-paletted-screen-blit-with-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!