I'm for the moment learning the shaders, but keep on "blocking" on one ot two thing that I don't understand.
My first question :
I'm rewritting my Managed DirectX code with shader basicaly to render a mesh without any other thing that just render it.
I still have in my C# this type of code :
...
myDevice.SetTexture(0, myTexture);
Render calls
...
In the shader (where it seems that I don't have to pass the texture), I have under the pixel shader section this type of code :
...
sampler TextureSampler;
float4 Texture(PS_INPUT Input) : COLOR0{
return tex2D(TextureSampler, Input.Texcoord);
};
...
and the most strange thing : It's working, and using the "myTexture" set with the Device from C#.
When does HLSL initialized the TextureSampler variable with my texture and how
For the complete shader code you could take a look here :
http://www.gamedev.net/community/forums/topic.asp topic_id=395808
My second question :
In general how do you pass the textures to the Shaders What's the best practice
And the last one for the road :
Is there a difference between these 2 structures :
struct VS_INPUT {
float4 Position : POSITION0;
}
struct VS_INPUT {
float4 Position : POSITION;
}
Tx you very much for your help !

Basic shader questions used with Managed DirectX
RFU
Q1 - are you SURE its using the shader. Change the shader to
return 1.0-tex2D(TextureSampler, Input.Texcoord);
and check that it is using it. Sounds like its still using the fixed function.
Q2. See samples on MDXInfo http://www.mdxinfo.com/tutorials.php view=HLSL%20tutorials
tusAugusto
Xzion
Hello !
I have change the return value from my pixel shader and indead it has "inversed" the color of the mesh so it is cleraly used.
Here is a copy of the Pixel shader after your change ://
// PIXEL SHADER
//
sampler TextureSampler2;
struct PS_INPUT {
float2 Texcoord : TEXCOORD0;
};
float4 Texture(PS_INPUT Input) : COLOR0{
return 1.0f-tex2D(TextureSampler2, Input.Texcoord);
};
Matt Anderson
Hello !
Yes this sampler initialization was strange to me. What I still can't understand, is that this type of sampler declaration without initiliazation is used in a lot of beginner Shader exemples, without any explanations ...
And most of all nobody seems to care/know how it was working (Have posted this question on sevaral forums).
Until one guy from the new MDXinfo forum answer me more than clearly ! (It's the only answer received in 3 days ...).
You can see his answer just here : http://www.mdxforum.com/viewtopic.php t=25
In fact there is nothing "lucky" in that. The compiler will use the register "s0" by default if nothing is specified. And that's exactly what Manadeg directX does when setting the texture (myDevice.SetTexture(0, myTexture), the 0 here means "in the S0 register of the GPU !".
Kind regards,
Leung
My guess was fairly close :-)
Actually there is a some luck involved.
This happens to work becuase the fixed function pipeline on modern cards is actually running prewritten shader code. So you are lucky that on your card the fixed function pipeline is implemented using a shader and therefore the fixed function texture is set in the same way as it would be on a shader.
Not all graphics cards implement fixed function with shaders (though I suspect its just older ones) so you will probably find that this code doesn't work the same on all graphics cards. WIthout a lot of testing its not a behaviour I would want to rely on.