Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some simple XNA/HLSL questions

Tags:

c#

shader

xna

hlsl

I've been getting into HLSL programming lately and I'm very curious as to HOW some of the things I'm doing actually work.

For example, I've got this very simple shader here that shades any teal colored pixels to a red-ish color.

sampler2D mySampler;

float4 MyPixelShader(float2 texCoords : TEXCOORD0): COLOR
{
    float4 Color;
    Color = tex2D(mySampler, texCoords.xy);

    if(Color.r == 0 && Color.g == 1.0 && Color.b == 1.0)
    {
        Color.r = 1.0;
        Color.g = 0.5;
        Color.b = 0.5;
    }
    return Color;
}

technique Simple
{
    pass pass1
    {
        PixelShader = compile ps_2_0 MyPixelShader();
    }
}

I understand that the tex2D function grabs the pixel's color at the specified location, but what I don't understand is how mySampler even has any data. I'm not setting it or passing in a texture at all, yet it magically contains my texture's data.

Also, what is the difference between things like: COLOR and COLOR0 or TEXCOORD and TEXCOORD0

I can take a logical guess and say that COLOR0 is a registry in assembly that holds the currently used pixel color in the GPU. (that may be completely wrong, I'm just stating what I think it is)

And if so, does that mean specifying something like float2 texCoords : TEXCOORD0 will, by default, grab the current position the GPU is processing?

like image 633
inline Avatar asked Oct 01 '11 00:10

inline


1 Answers

mySampler is assgined to a sample register, the first is S0.

SpriteBatch uses the same register to draw textures so you have initilized it before for sure.

this register are in relation with GraphicDevice.Textures and GraphicDevice.SamplerStates arrays.

In fact, in your shader you can use this sentence:

 sampler TextureSampler : register(s0);

EDIT:

if you need to use a second texture in your shader you can make this:

 HLSL
      sampler MaskTexture : register(s1);

 C#:
      GraphicsDevice.Textures[1] = MyMaskTexture;
      GraphicsDevice.SamplerStates[1].AddresU = TextureAddresMode....

Color0 is not a registry and does not hold the current pixel color. It's referred to the vertex structure you are using.

When you define a vertex like a VertexPositionColor, the vertex contains a Position, and a Color, but if you want to define a custom vertex with two colors, you need a way to discriminate between the two colors... the channels.

The number suffix means the channel you are referring in the current vertex.

like image 145
Blau Avatar answered Nov 15 '22 03:11

Blau