I am trying to optimise a palette mapping shader written in GLSL (running on iPhone). My implementation is certainly naive but I’m completely new to OpenGL… Here’s a summary of how it looks:
#define COLOR_BLACK vec4(0.0, 0.0, 0.0, 1.0)
#define COLOR_WHITE vec4(1.0, 1.0, 1.0, 1.0)
(... total of 16 colors)
highp vec4 color = texture2D(inputImageTexture, samplePos );
highp float distances[16];
distances[0] = distance(color, COLOR_BLACK);
distances[1] = distance(color, COLOR_WHITE);
(... total of 16 distance calculations)
(... find smallest colour distance)
mediump vec4 finalColor;
if (colorDistance == distances[0]) {
finalColor = COLOR_BLACK;
} else if (colorDistance == distances[1]) {
finalColor = COLOR_WHITE;
(... total of 16 comparisons)
gl_FragColor = finalColor;
This works fine but it’s rather slow. I’m sure I’m not making an effective use of OpenGL here 🙂
Any help appreciated !
Use a (small) 3D texture in nearest filtering mode as a lookup table.
Update due to comment
If 3D textures are not available you can use one larger sized 1D texture, again in nearest filtering mode, and address 3D texels the same way you’d address them in a flat array, i.e.