Jump to content
  • Advertisement
Sign in to follow this  
kevroy314

Attach Extra Variable To Pixel Shader

This topic is 3062 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

I'm implementing a cellular automata using a pixel shader. I'm also very new to shaders and xna... I want to attach an extra variable used to aid computation to each pixel on the texture. I know you can just change the input/output struct to include an extra variable but I need to know how to access that variable for surrounding pixels. I don't know if that's possible (or if there's a better way to do the same thing). For instance if I have the following almost-shader-code I want to execute in a shader... float4 PixelShaderDigitalInkbots(VertexShaderOutput input) : COLOR0 { float4 currentColor = tex2D(Sampler,input.Tex0); float4 lurd; //used to wrap around the texture lurd.x = (input.Tex0.x > 0) ? input.Tex0.x - 1 : (isWrap) ? 800 - 1 : 800; lurd.y = (input.Tex0.y > 0) ? input.Tex0.y - 1 : (isWrap) ? 600 - 1 : 600; lurd.z = (input.Tex0.x < 800 - 1) ? input.Tex0.x + 1 : (isWrap) ? 0 : 800; lurd.w = (input.Tex0.y < 600 - 1) ? input.Tex0.y + 1 : (isWrap) ? 0 : 600; //sum the extra variable associated with each surrounding point float4 iCnt = getValue(Sampler,float2(lurd.x, lurd.y)) + getValue(Sampler,float2(lurd.x, input.Tex0.y)) + getValue(Sampler,float2(lurd.x, lurd.w)) + getValue(Sampler,float2(input.Tex0.x, lurd.y)) + getValue(Sampler,float2(input.Tex0.x, input.Tex0.y)) + getValue(Sampler,float2(input.Tex0.x, lurd.w)) + getValue(Sampler,float2(lurd.z, lurd.y)) + getValue(Sampler,float2(lurd.z, input.Tex0.y)) + getValue(Sampler,float2(lurd.z, lurd.w)); return Palette[(iCnt/9+2)%255)]; //pull a value from the color palette } Firstly obviously there is no function 'getValue', but that would be where I'd want to access that variable stored in association with each pixel. That's problem 1. Problem 2 is I don't think you can dynamically call an index from an array (Palette would store an array of 256 colors which would need to be accessed relative to the formula above). Does anyone know good solutions to either/both of these issues? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by kevroy314

Problem 2 is I don't think you can dynamically call an index from an array (Palette would store an array of 256 colors which would need to be accessed relative to the formula above).



This is true for ps_3_0 and below. Ultimately your code has to boil down to accessing a fixed register without using an index, which means you'll get something like this:

float4 val;
if(idx == 0)
val = c0;
else if(idx == 1)
val = c1;
...


The only good way to dynamically access data is to put it in a texture. Volume textures are pretty popular for encoding loop-up tables and things of that nature.

Also just FYI...if you want to post code here you can use the "code" or "source" tags. See the FAQ.

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!