Jump to content
  • Advertisement
Sign in to follow this  
SiliconMunky

HLSL Pixel Shader Texture Lookup

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

I've been trying to do texture splatting using a pixel shader in HLSL, but I'm having trouble trying to figure out how to do a texture lookup in texture that isn't in stage 0. Maybe I'm just a bit confused about the texture object, and the use of tex2D. Any info would be greatly appreciated. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try initializing your sampler like

Sampler2D diffuseColor : register(s1);

if you want to use a texture in sampler 1

(use register(s0) for sampler 0 and so on).

You can use the texture "objects" (the ones in HLSL) and associate them with the sampler. You have to set the texture in direct3d to the texture in HLSL before rendering. Don't forget to call CommitChanges() if you are using ID3DXEffect.

Try the first way I described first...

Share this post


Link to post
Share on other sites
Hi there SiliconMunky,
How are you doing?

The Problem
Texture lookups on different texture stages.

The Solution
What you want to do is the following:
1) Set each textue that you want to blend to a different stage such as
IDirect3DDevice9::SetTexture(0, myBase);
IDirect3DDevice9::SetTexture(1, mySecondTexture);

2) Then in HLSL you will have 2 samplers, 1 for the base and the other for the second texture such as

sampler2D texSampler0 : TEXUNIT0 = sampler_state
{
Texture = (texture0);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D texSampler1 : TEXUNIT1 = sampler_state
{
Texture = (texture1);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};



PS:
if you are using the ID3DXEffect framework you... (texture0) and (texture1) refers to the names of the texture constants and can be set using :
effect.SetValue("texture0", textureBase);
effect.SetValue("texture1", textureSecond;


3) Then in your shader you will extract the color using tex2D.
tex2D:
There are two overloaded tex2D texture lookup functions:
2-D texture lookup
2-D texture lookup with partial derivatives


//calculate the color for the base texture
float4 colorBase = tex2D(texSampler0, IN.tex0);
//calculate the color for the second texture texture
float4 colorSecond = tex2D(texSampler1, IN.tex1);

And then you can do whatever you want with them since you have the color information for both :)


I really hope this helps buddy and if it's a little unclear or any mistakes, please do not hesitate to reply here and if i'm around I will do my best to answer.

Share this post


Link to post
Share on other sites
Thanks for the quality reply Armadon

One thing I'm curious about is that if I'm not using the effect framework, then what is the purpose of the line

Texture = (texture1);

?

Is that line required?
My next question is about the texture coordinates
I have 5 textures and 2 sets of texture coordinates. The first texture coordinate is for the alphamap that sits in stage 0, and the second texture coordinate is for the 4 terrain textures that sit in stages 1 to 4.

I was hoping you could look at my shader code and point out what I am doing wrong.

Thanks for the help.


sampler2D Texture0 : TEXUNIT0 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D Texture1 : TEXUNIT1 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D Texture2 : TEXUNIT2 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D Texture3 : TEXUNIT3 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler2D Texture4 : TEXUNIT4 = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};

float4 ps_main( float4 inDiffuse: COLOR0, float2 inTx1: TEXCOORD0, float2 inTx2: TEXCOORD1 ) : COLOR0
{
float4 alphamap = tex2D(Texture0, inTx1);
float4 outColor = tex2D(Texture1, inTx2);
float4 red = tex2D(Texture2, inTx2);
float4 green = tex2D(Texture3, inTx2);
float4 blue = tex2D(Texture4, inTx2);

outColor = (alphamap.r * red) + ((1 - alphamap.r) * outColor);
outColor = (alphamap.g * green) + ((1 - alphamap.g) * outColor);
outColor = (alphamap.b * blue) + ((1 - alphamap.b) * outColor);
outColor = outColor * inDiffuse;

return outColor;
}

Share this post


Link to post
Share on other sites
well, I replaced TEXUNIT0 with register(s0) as the AP was saying and it appears to be working correctly.

Maybe someone could explain the role of "register(s2)" in


sampler2D Texture2 : register(s2) = sampler_state
{
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};


Thanks again all.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!