Jump to content
  • Advertisement
Sign in to follow this  
dachande

Shader Tuts

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

One thing I found helpful when I started learning about shaders a few months ago, was to use tutorials on both HLSL and Cg. As you've found out, there's not very much information on HLSL, but the two languages are so similar that a tutorial on Cg is just as good.

Share this post


Link to post
Share on other sites
Advertisement
Thanks again ;)

I've got it going now. I checked out the asm code my HLSL was producing, and I think I was prolly optimising out a texture or something ;)

Not sure about this still though:

sampler TextureSampler = sampler_state
{
texture = <diffuseTexture>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

How does "texture = <diffuseTexture>;" relate to the rest of the world? The samplers map to the texture stages in the order they are decalered, as far as I am aware... Any more info? :D

Thank you,

Dachande

Share this post


Link to post
Share on other sites
Well, the simplest way to declare textures is:
sampler tex0: register(s0);
sampler tex1: register(s1);


Then you can set proper textures in the application via call to pDevice->SetTexture(0, pSomeTex) and pDevice->SetTexture(1, pSomeOtherTex).
It will iherit all settings set with pDevice->SetSamplerState(0/1, ...).

As for the
sampler TextureSampler = sampler_state
{
texture = <someTextureVariable>;
[...]
MAGFILTER = LINEAR;
};


you have to declare in the shader:
Texture someTextureVariable;
and set it as normal shader global variable.
With ID3DXEffect interface it is a simple call to SetTexture.

I didn't use the second method, though, so don't kick me if it won't work [grin]
~def

Share this post


Link to post
Share on other sites
Quote:
Original post by dachande
How does "texture = <diffuseTexture>;" relate to the rest of the world? The samplers map to the texture stages in the order they are decalered, as far as I am aware... Any more info?e

"diffuseTexture" is the name of the texture that you want the sampler to read from. For example, if the name of your declared texture was "heightMap", the sampler declaration would be:

texture heightMap;

sampler TextureSampler = sampler_state
{
texture = <heightMap>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

Share this post


Link to post
Share on other sites
In this little example you can see how to declare 2 texture and "assign" them to different texture unit.
The final color is a simple multiplication of color from texture 1 and 2


Before running the shader you have to set 2 textures with the usual directX stuff. Something like:
pDevice->SetTexture (0,pTexture1);
pDevice->SetTexture (1,pTexture2);


In the shader I will call texDiffuse the texture in stage 0 and texBump the texture in stage 1



texture texDiffuse; // decalre texture 1
texture texBump; // decalre texture 2

sampler2D texSampler0 : TEXUNIT0 = sampler_state
{
Texture = (texDiffuse); // Here I'am saying that texDiffuse will be the texture in texture unit 0
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};

sampler2D texSampler1 : TEXUNIT1 = sampler_state
{
Texture = (texBump); // Here I'am saying that texBump will be the texture in texture unit 1
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};


PS_OUTPUT main (VS_OUTPUT IN)
{
PS_OUTPUT OUT;

// Get a color from texture unit 0
float4 colorFromTexture1 = tex2D (texSampler0, IN.Tex);

// Get a color from texture unit 1
float4 colorFromTexture2 = tex2D (texSampler1, IN.Tex);

// Multiply and return
OUT.Col = colorFromTexture1 * colorFromTexture2;
return OUT;
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!