Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Palette shader problem  (Read 2142 times)

0 Members and 1 Guest are viewing this topic.

Schizm

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Palette shader problem
« on: April 24, 2012, 12:27:45 am »
Well, it's not really related to SFML, but I see that's common thing in General forum :)

I'm writing some game engine (in fact, reverse-engineering one old game).
I need to be able to load an image (it's in game's internal format, uses palette and per-pixel alpha) and then swap it's palette at rendering time; First I used to load full copy of the image for each needed palette, but that seems to consume too much memory (over 70mb per sprite, nearly 200 different possible sprites/palettes, all on the same game level). So then I left old solution as fallback if shaders are not supported and started writing a shader that will apply a palette to my image, so memory usage can be reduced to ~10mb or even ~5mb per sprite (~100 images).

Now, look at this screenshot:

As you probably can notice, the colors are a bit off their real index, but I'm not sure why it happened.

This is how it's supposed to look like (with shaders disabled):


The image loading code is all ok (see second screenshot). Problem should be in the shader code, but I'm not really sure where is it.

Fragment shader code:
#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect rec1;
uniform sampler1D palette;

void main(void)
{
        vec4 oldcol = texture2DRect(rec1, gl_TexCoord[0].st);
        float index = (oldcol.r + oldcol.g + oldcol.b) / 3;
        vec4 newcol = texture1D(palette, index);

        gl_FragColor.rgb = newcol.rgb * gl_Color.rgb;
        gl_FragColor.a = oldcol.a * gl_Color.a;
}

rec1 = GL_TEXTURE_RECTANGLE_ARB texture containing palette index in (r,g,b) and per-pixel alpha.
palette = GL_TEXTURE_1D texture, 256 texels wide, containing palette colors.

I'm really stuck here, please help :(
« Last Edit: April 24, 2012, 01:00:53 am by Schizm »

Schizm

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Palette shader problem
« Reply #1 on: April 24, 2012, 01:23:49 am »
Not sure, why I did this (never thought it will help), but I've changed 1D-texture's width to 255 instead of 256. Now colors are correct, but FPS then drops from 120 to 6-8. How to reproduce same effect, but in more OpenGL-friendly way? (index = (index / 256.0) * 255.0) does not work as expected, producing only more wrong colors.

Texture initialization code:
        glGenTextures(1, &this->m_PaletteTex);
        glBindTexture(GL_TEXTURE_1D, this->m_PaletteTex);

        // here I tried changing 256 to 255, which fixed color offsets but introduced massive lag
        glTexImage1D(GL_TEXTURE_1D, 0, 4, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, this->m_Palette);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP);

        glBindTexture(GL_TEXTURE_1D, 0);
 
« Last Edit: April 24, 2012, 01:30:34 am by Schizm »

Schizm

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Palette shader problem
« Reply #2 on: April 26, 2012, 12:23:45 pm »
Time to bump.

 

anything