11

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?

inline
  • 695
  • 1
  • 7
  • 17

1 Answers1

7

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.

Blau
  • 5,742
  • 1
  • 18
  • 27
  • Where exactly is it assigned to a sample in the register, though? I haven't specified which register it grabs the sample from so I guess it'll just grab s0 by default without any code specification? Also, you bring up another question: will `sampler` work just the same as `sampler2D`? – inline Oct 01 '11 at 15:10
  • whatever draw operation you do, use that registers, if you are using spritebatch or a basic effect before with that texture, it has being done intrinsically... – Blau Oct 01 '11 at 15:39