Sign in to follow this  

Applying a second texture dx9

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

Hi all, Having a little trouble getting access to a second texture in a pixel shader in DirectX 9. Ive checked that everything is loaded correctly and is functional. But it seems that any texture i add at stage 1 doesnt get passed through to the pixel shader. Can anyone see any blatant code I've over looked In setting the texture I have:
dxDevice->SetTexture( 0, Texture1);
dxDevice->SetTexture( 1, Texture2);
and to test this my pixel shader looks like this:

sampler2D Texture1; 
sampler2D Texture2; 


struct PS_Input
{
    float2 TexCoord0     : TEXCOORD0;
    float3 WorldPosition : TEXCOORD1; 
    float3 WorldNormal   : TEXCOORD2; 
};

struct PS_Output
{
	float4 Colour : COLOR0;
};


void main( in PS_Input i, out PS_Output o ) 
{
    o.Colour = tex2D( Texture2, i.TexCoord0 );	
}
This still results in Texture1 being applied regardless. Anyone any ideas whats going wrong?

Share this post


Link to post
Share on other sites
similar to semantics you can specify the GPU register for a variable to be in, so try

Texture tex0 : register(t0)
Texture tex1 : register(t1)

which specifies that tex0 is in texture register 0 and tex1 is in register 1 so that you aren't relying on the compiler guessing how you want them mapped.

Share this post


Link to post
Share on other sites
"ender_341":s suggestion will work.

The reason why you see the texture bound in the slot 0 (instead of slot 1) is that the shader compiler optimizes out texture variables if you don't use them. Since you don't refer to the first texture in your shader, the compiler doesn't allocate it at all and the second texture variable gets the slot 0. On the other hand, if you force the textures to given registers, the compiler doesn't move them.

You can verify this with fxc, it shows you all the resources that are actually used.

Share this post


Link to post
Share on other sites
I've never used a Texture object, i normally rely on the texture to be passed through the pipeline correctly and use a sampler2D object to return the colour at a specified position. The texture is loaded correctly as if i pass the texture through at stage 0 the texture applied to the model is the other texture.

--edit

Based on the idea that the first texture isnt be used so it gets ommited I altered my shader to look like this:


sampler2D Texture1;
sampler2D Texture2;


struct PS_Input
{
float2 TexCoord0 : TEXCOORD0;
float3 WorldPosition : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
};

struct PS_Output
{
float4 Colour : COLOR0;
};


void main( in PS_Input i, out PS_Output o )
{
float4 firstColour = tex2D( Texture1, i.TexCoord0 );
o.Colour = tex2D( Texture2, i.TexCoord0 );
}



even now though it is being referenced the shader still ignores the second texture. Any other suggestions?

[Edited by - terry_burns85 on March 28, 2010 3:20:17 PM]

Share this post


Link to post
Share on other sites
it's optimising out the second texture reference still because it's not actually being used for anything...

If you compile your shader with the command line compiler :-

fxc /Tps_2_0 /Emain.sh sh.fx

Then you get this which shows in the register section that it's only using one texture still. You either need to use it for something, or assign it to a specific texture register

[code]
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.27.952.3022
//
// fxc /Tps_2_0 /Emain sh.fx
//
//
// Parameters:
//
// sampler2D Texture2;
//
//
// Registers:
//
// Name Reg Size
// ------------ ----- ----
// Texture2 s0 1
//

ps_2_0
dcl t0.xy
dcl_2d s0
texld r0, t0, s0
mov oC0, r0

/ approximately 2 instruction slots used (1 texture, 1 arithmetic)
[code]

Share this post


Link to post
Share on other sites
You need to run something like this -

fxc /Tps_2_0 /Emain shader.psh
Where main is the entry point for your shader and shader.psh is the filename containing it. And ps_2_0 is the type of compilation. You use vs_2_0 or similar for vertex shaders.

You find fxc in the Utilities/bin/x86 directory of your d3d sdk

Share this post


Link to post
Share on other sites
Quote:
Original post by ender_341
Texture tex0 : register(t0)
Texture tex1 : register(t1)


Since you want to use Samplers, not Textures, instead of the above use the following:

sampler2D Texture1 : register(s0) 
sampler2D Texture2 : register(s1)


Share this post


Link to post
Share on other sites

This topic is 2814 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.

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