• Advertisement
Sign in to follow this  

Shader with HLSL and Managed DirectX

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

Hello, I have in fact 2 simple questions concerning this very basic vertex/pixel shader called from Managed directX : First Question : I set my texture in C# for one mesh, everything is fine, no problem, but how this sampler variable 'TextureSampler' received the texture I have set in C# ? For me it seems to be a global variable that is not initialized but use with text2d ?!? Here is the code concerned : sampler TextureSampler; ... float4 Texture(PS_INPUT Input) : COLOR0{ return tex2D(TextureSampler, Input.Texcoord); }; ================================ Second Question : What's the difference between these 2 semantics : myType myVariable : POSITION myType myVariable : POSITION0 Shader :
//=============================================================================
float4x4 WorldViewProj;

//
// VERTEX SHADER
//

//From Application to Vertex Shader
struct VS_INPUT {
    float4 Position : POSITION0;
    float2 Texcoord : TEXCOORD0;
};

//From Vertex Shader to pixel processing
struct VS_OUTPUT {
    float4 Position : POSITION0;
    float2 Texcoord : TEXCOORD0;

    float4 color : COLOR0;
};

VS_OUTPUT Transform(VS_INPUT Input){
    VS_OUTPUT Output;

    // Computing the View position of the 3D Point
    Output.Position = mul(Input.Position, WorldViewProj);
    // Passed the texture value of the 3D point without modifications
    Output.Texcoord = Input.Texcoord;

    //Light the Pixel
    float4 mycolor = float4(1.0f, 1.0f,1.0f,0.0f);
    Output.color = mycolor;

    // Return Position+Texture value for each 3D point to the Pixel shader
    return Output;
}


//
// PIXEL SHADER
//

sampler TextureSampler;

struct PS_INPUT {
    float2 Texcoord : TEXCOORD0;
};

float4 Texture(PS_INPUT Input) : COLOR0{
    return tex2D(TextureSampler, Input.Texcoord);
};



// The Shader User interface ==================================
// Technique :
//
// TransformTexture : Will transform and apply texture.
//			 	
//========================================================
technique TransformTexture {
    pass P0 {
        VertexShader = compile vs_2_0 Transform();
        PixelShader  = compile ps_2_0 Texture();
    }
}

//=============================================================================


[Edited by - SeeMe on June 1, 2006 4:33:12 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hey bud,

Here is the source for my simple texture example:

float4x4 matWorld;
float4x4 matWorldViewProj;

texture asphalt;

sampler sampAsphalt = sampler_state
{
texture = (asphalt);
minfilter = LINEAR;
magfilter = LINEAR;
mipfilter = LINEAR;
};

struct VS_INPUT
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 Texture : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 Position : POSITION;
float3 Normal : TEXCOORD0;
float2 Texture : TEXCOORD1;
};

VS_OUTPUT vs_main( VS_INPUT input )
{
VS_OUTPUT output = (VS_OUTPUT)0;

output.Position = mul( input.Position, matWorldViewProj );
output.Normal = mul( input.Normal, matWorld );
output.Texture = input.Texture;

return output;
}

float4 ps_main( VS_OUTPUT input ) : COLOR
{
return tex2D( sampAsphalt, input.Texture );
}

technique texture1
{
pass P0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();
}
}



Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Tx you for your quick answer.

I complelty agree with your code where you clearly initialize your sampler with a texture.

The thing I don't understand is why, in my version it is also working perfectly (even If I'm not applying for the moment texture filters), and I really don't know where my sample is initialized to the texture.

Share this post


Link to post
Share on other sites
Heps,

If I have well understood :
In your shader call from C# with managed directX, I will have to use this type of initialization :

myShader.SetValue("asphalt", myTexture);

and remove my :

myDevice.SetTexture(0, myTexture);
.

An other thing, yes I know I have a milion of question :D

When : myDevice.SetTexture(0, myTexture); is done in C#, how can I find a reference of this texture from inside my shader ?

[Edited by - SeeMe on June 1, 2006 4:30:07 AM]

Share this post


Link to post
Share on other sites
I'm still trying to understang the "thing", the odd is that nobody seems to know about it (On this forum and on other one) !?!?

How possible, as there are plenty of exemples on the net using this type of sampler declaration ?

Do you know an other place where I could look ?

Tx you

Share this post


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

  • Advertisement