Jump to content
  • Advertisement
Sign in to follow this  
derrace

HLSL Pixel Shader that does palette swap

This topic is 2060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]".

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}
Edited by derrace

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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? Edited by derrace

Share this post


Link to post
Share on other sites
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. Edited by derrace

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!