Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shader Tuts

Started by dachande Aug 22, 2005 at 3:48 PM 14 replies 1.4k views
Original Post
dachande
dachande
Evenin' all, I've been searching for some good HLSL material for a while now, and I'm coming up with nothing... Very surprising how the Internet seems to have so little information on the subject!! The book shader X2 seems to be quite popular, but I'd like to learn more with some free tutorials before I go splashing out on books...! In particular, I'm looking at multitexturing. I have both a vertex and a pixel shader working fine... only they are as simple as they get... but my attempt at multitexturing is failing... Much to my dissapointment, the texture object and sampler object is VERY poorly documented! I haven't yet figured them our properly. Any help with my troubles would be much appreciated! Some advice, or leeds on where to get some info would be great. Thanks in advance, Dachande
Trip99
Trip99
NVidia FX Composer and ATIs RenderMonkey both come with lots of example shaders that might help but I agree there is limited info. on the net.
------------------------See my games programming site at: www.toymaker.info
remigius
remigius
I picked up the very basics from the book Managed DirectX 9 kickstart by Tom Miller. It has a few chapters introducing HLSL and helping you on your way. A bit limited perhaps, but it contains some good examples.

The NVidia SDK indeed provides a pile of HLSL shaders and the FX composer sounds like a great tool (haven't had time to check it out yet), so that's a good bet. From what I saw though, the NVidia sample shaders are a bit overwhelming and mostly require ps/vs 2.0 hardware.

On the MSDN you can find a helpful reference of HLSL intinsics, but beyond that there are few other pratical resources.

So, if anyone could recommend a good book about HLSL that'd be great. Preferably starting with the basics, discussing some more advanced effects and -mostly importantly- providing additional documentation of HLSL to supplement the sorely lacking docs on the net...
Armadon
Armadon
Hi there dachande,
How are you doing?

The Problem
Shader tutorials. Resources on shaders etc...

The Solution
I hope I'm not being condescending when I say that make sure you know your pipeline and how things are done in the fixed function pipeline before moving to shaders, such as how matrices are transformed and spaces. Spaces are probably one of the components that you need to know very well. Model Space, World Space, View Space, Clip Space...

To finish this off I have a link to a very interesting tutorial on shaders.
HLSL Introduction

I hope this helps buddy. Take good care and if you have any other questions, please don't hesitate to ask :)
remigius
remigius
I just re-read your topicstart and figured that this article about HLSL terrain rendering might help out. It covers terrain multitexturing based on height, by blending two seperate textures. This should provide you with some idea to get started. It requires sm2 though.

And I recalled I found some very nice free shaders the other day, which you can find on this page. These do focus on image post-processing, but they are generally well documented and shoud how to create some very nice effects.

Sorry I didn't post this earlier, but I kinda kicked into complain-mode when I saw I wasn't the only one not able to find proper tutorials and references for HLSL, especially since it was so late :)
Trip99
Trip99
Good link. On the book front I recommend Programming Vertex and Pixel Shaders by Wolfgang Engel.
------------------------See my games programming site at: www.toymaker.info
dachande
dachande
Thanks all for your helpful replies :)

I've been through all the links, I've used RenderMonkey and FXComposer, and downloaded all sorts of example code... but I still have found no clear explanation of using multiple textures (this is not my only hangup, but it is a big one!).

Sampler objects and texture objects are often declared, but not referenced again until a call to tex2d(sampler, coords) etc... There is no clear link between the sampler, and which texture it connects to - as far as I can see.

basically (pseudoish!):

sampler ASampler;
sampler BSampler;

float4 PSmain(float2 tex : TEXCOORD0, float2 tex2 : TEXCOORD1) : COLOR
{
float2 ACol = tex2d(ASampler, tex);
float2 BCol = tex2d(BSampler, tex2);
}

I see no link between samplers and textures! I'm totally open to the possibility that I'm being dumb... BUT I know that this stuff simply isn't documented to a satisfactory mannor in the docs!

Generally I'm not too bad at this stuff, but I get hung up on details that I don't understand :D

Can anyone shed some light?

Thanks,

Dachande
circlesoft
circlesoft
Quote:
Original post by dachande
I see no link between samplers and textures! I'm totally open to the possibility that I'm being dumb... BUT I know that this stuff simply isn't documented to a satisfactory mannor in the docs!


The Sampler construct is equivalent to IDirect3DDevice9::SetSamplerState() in the FFP. It controls all of the options like AddressU/V/V, Mip filter, min filter, and mag filter. Basically, it controls how your texture is read and what type of prosessing goes into it.

A Sampler is linked to a texture like this:

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


MSDN used to have an excerpt from the first chapter of ShaderX2 on it, but I can't seem to find it.
Tessellator
Tessellator
Hi mate,

To use a texture sampler, you have to declare it. Just make sure you set a relevant texture into your device via a pDevice->SetTexture(samplerNum, texture) and away you go. E.g:

sampler tex0;
sampler tex1;

// will become in asm:
dcl_2d s0
dcl_2d s1

One thing to look out for is that the HLSL compiler will not declare any samplers that are not used (even in if the DEBUG options are used) so if you do something like:

sampler tex0;
sampler tex1;
sampler tex2;

// Sampler tex1 is not referenced in the code
float4 diffuse = tex2D(tex0, IN.textureCoords);
float3 normal = tex2D(tex2, IN.textureCoords);

// The asm generated will be something like
dcl_2d s0
dcl_2d s1
texld r0, t5, s0 // tex2D(tex0, IN.textureCoords)
texld r1, t5, s1 // tex2D(tex2, IN.textureCoords) <- s1 is used rather than s2!

In this instance your normal var will contain whatever comes from texture sampler 2 rather than 1, which is not likely to be a normal :).

T
Spoonbender
Spoonbender
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.
dachande
dachande
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 = ;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

How does "texture = ;" 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
deffer
deffer
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 = ;
[...]
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
circlesoft
circlesoft
Quote:
Original post by dachande
How does "texture = ;" 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;};
Jnz86
Jnz86
Try this!
Giallanon
Giallanon
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;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.