Sign in to follow this  
CadeF

DirectX 9, sampling textures from HLSL without changing fvf/vertex structure

Recommended Posts

CadeF    421
I have a HLSL shader that gets the texture coordinates for texture #0. In HLSL, how can I sample the texture at #0 and the texture at #1 (assuming they both should be sampled in the same place) only with the texture coordinates for texture #0? I am using a vertex shader and a pixel shader.

Share this post


Link to post
Share on other sites
sirob    1181
In order to sample a single point in HLSL, you'd use a line similar to this:

float4 diffuseTexture = tex2D( TextureSampler, IN.texCoordDiffuse );

If you want to sample another point, you'd call the same line, but use a different sampler for it:

float4 diffuseTexture2 = tex2D( TextureSampler2, IN.texCoordDiffuse );

Note, if you wanted to use different texCoords, you'd modify the second parameter to the tex2D function.

Also, you now need to somehow blend the two colors together, depending on what you want to do. If you want to modulate (for example), you'd need to:

return diffuseTexture * diffuseTexture2;


Hope this helps.

Share this post


Link to post
Share on other sites
CadeF    421
Variable Decl
sampler2D sam1;
sampler2D sam2;

Pixel Shader
float4 SamColor;
float4 SamBump;
SamColor = tex2D( sam1, IN.texCoord);
SamBump = tex2D( sam2, IN.texCoord);
OUT.color = SamBump;
OUT.color.a = 1.0f;

It still samples from texture 0, not texture 1. Changing my variable decl to
sampler2D sam1 : TEXCOORD0;
sampler2D sam2 : TEXCOORD1;
And it still samples SamBump and SamColor from texture 0. SamColor should be texture 0, SamBump texture 1.

Share this post


Link to post
Share on other sites
sirob    1181
When defining a sampler, you'll need to define which texture it samples. Here's an example:


texture diffuseTexture : Diffuse;
texture diffuseTexture2 : Diffuse;
//------------------------------------
sampler TextureSampler = sampler_state
{
texture = <diffuseTexture>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

//------------------------------------
sampler TextureSampler2 = sampler_state
{
texture = <diffuseTexture2>;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};


Note, you'll need to set both diffuseTexture and diffuseTexture2 using the Effect->SetTexture() procedure.

Hope this helps :).

Share this post


Link to post
Share on other sites
CadeF    421
I got it to work using this code
texture texture0;
texture texture1;

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;
};

But, using this code
float4 SamColor;
float4 SamBump;
SamColor = tex2D( texSampler0, IN.tex0);
SamBump = tex2D( texSampler1, IN.tex1);
OUT.color = SamBump;
OUT.color.a = 1.0f;
It returns me texture 0, not texture 1. Any idea what is wrong?

My texcoords are transferred by the vertex shader like this
OUT.tex0 = IN.tex0;
OUT.tex1 = IN.tex0;
Where both pixel shader tex coords (0 and 1) are the UV of texture 0.

Edit: Works when PS 1.1-1.4 but not when compiled to PS 2.0...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this