HLSL Pixel Shader that does palette swap

Started by
18 comments, last by derrace 11 years, 4 months ago
Hi there,

I have implemented a simple pixel shader which can replace a particular colour in a sprite with another colour.

It looks something like this:



sampler input : register(s0);
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
float4 colour = tex2D(input, coords);
if(colour.r == sourceColours[0].r &&
colour.g == sourceColours[0].g &&
colour.b == sourceColours[0].b) return targetColours[0];

return colour;
}


What I would like to do is have the function take in 2 textures, a default table, and a lookup table (both same dimensions).
Grab the current pixel, and find the location XY (coords) of the matching RGB in the default table, and then substitute it with the colour found in the lookup table at XY.

I have figured how to pass the Textures from C# into the function, but I am not sure how to find the coords in the default table by matching the colour.

Could someone kindly assist?

Thanks in advance.
Advertisement
Hmm, think I got it,

It involves using a for loop to cycle through the a variant of the above code but I am getting this silly error now =(

Shader uses texture addressing operations in a dependency chain that is too complex for the target shader model (ps_2_0) to handle.
Use a 3D texture instead. That way instead of putting a for loop in your shader ( slow ), you take the color value, and use that value to look up a 3d texture that has the colours you want in it.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
If you're going to do this, it would be a whole lot simpler if the original sprite contained pallete-indices instead of colours.

You could pre-process your sprites once on the CPU to avoid having to execute that ridiculously huge loop in your pixel shader.

Then you'd just have to do "[font=courier new,courier,monospace]fetch sprite colour (which is an index), fetch colour from pallete at index[/font]", instead of "[font=courier new,courier,monospace]fetch sprite colour, search entire source pallets for colour to determine it's index, fetch colour from new pallete at index[/font]".
Hey all, thanks for the replies.


Use a 3D texture instead. That way instead of putting a for loop in your shader ( slow ), you take the color value, and use that value to look up a 3d texture that has the colours you want in it.


Hmm, that would require me to manually insert values into the 3rd dimension, if I have more more than 1 set of colours I want to change depending on the situation, I would need to store more than 1 look up value?



If you're going to do this, it would be a whole lot simpler if the original sprite contained pallete-indices instead of colours.

You could pre-process your sprites once on the CPU to avoid having to execute that ridiculously huge loop in your pixel shader.

Then you'd just have to do "[font=courier new,courier,monospace]fetch sprite colour (which is an index), fetch colour from pallete at index[/font]", instead of "[font=courier new,courier,monospace]fetch sprite colour, search entire source pallets for colour to determine it's index, fetch colour from new pallete at index[/font]".


Similar to what you suggested, I am thinking along the line of using one of the 3 RGB channels as my unique index, and storing my colour table values in an array of 255 elements.
Ok, I have created a 4x1 png and inserted 4 different coloured pixels. I have changed index from 0 to 3 (or any value for that fact) but it always display the colour of the first pixel. If i change index with the var coords in the tex1D method, and move my sprite, it cycles through the colours when I move, but displays the colour of the first pixel when stationary. Do you have any idea why this is happening?

here's the snippet of my function:


float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{

float4 colour = tex2D(input, coords);

int index = 0; //doesn't matter what value i put, it always return the first pixel of my palette
float4 lookUp = tex1D(Palette2Sampler,index);
//float4 lookUp = tex1D(Palette2Sampler,coord);
// when I run the above and move the sprite, it cycles through the colours, but always
// displays the colour of the first pixel when stationary.

return lookup;
}
Texture coordinates range between (0, 1), not (0, n). Divide your index by n, where n is the number of texels (4 in your case).

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.

And make sure you're using point sampling.

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.
That's not just DX9*, that's how texture coordinates work in all APIs.
[font=courier new,courier,monospace]centreOfTexel = (texelIndex+0.5)/widthInTexels[/font]

*[size=2]The "DX9 half pixel offset issue" is actually to do with how it addresses screen pixels during rasterization.

Texture coordinates range between (0, 1), not (0, n). Divide your index by n, where n is the number of texels (4 in your case).

Also, if this is DX9, you need to apply the half texel offset so that you're sampling from the middle of the texel (sampling from the edge will be "unstable"). So add (0.5 / 4) to the index.

And make sure you're using point sampling.



Ah, 0 to 1. Got it. In my script, I don't think I need to do any division as it would probably have done it for me. When setting up my colourtable, I would just multiply it by 255 since I am using the bands of the pixel as my index.

Got my algo for pixel shader for palette swap sorta working.. but I am having some issues with it.

http://imgur.com/3pPnO

Bands aren't unique, so I am taking the average of RGB as my index. I don't get a whole number, but I made each lookup area 3x1 (i need to increase it to 3). I am not sure if it's the decimals that is causing the issue. I will need to debug further. Also, I have two default colours with the same value, but expecting different palette swaps.. How can this be achieved?


for now just assume my colour table is 255 x 10. I will hardcode the dividers as well as the y index for now.

[source lang="java"]
float4 colour = tex2D(input, coords);
float2 index = float2((colour.r + colour.g+colour.b)/3.0 , 4/10.0 + 0.5 / 10.0);
float4 lookUp = tex2D(PaletteSampler,index);
if(colour.a != 0) {
colour.rgb = lookUp.rgb;
}

return colour;
[/source]

sorry, what do you mean by point sampling? Isn't the above point sampling?
hmm, so I have added a check and it does look like a lot of pixels aren't being replaced from the lookup table. It almost appears like I got my indices wrong =\

I think I need a better way of indexing. Does anyone have any suggestion?

To recap, currently I am taking the average of the RGB as the neither of the bands are unique =\. My palette (see screenshot attached) has potentially 15 pixels.

http://imgur.com/ujO6E

Replacing my default sprites with a greyscale one is kinda tedious, and I am hoping there's a better way.

This topic is closed to new replies.

Advertisement