# HLSL & image filtering

## Recommended Posts

genesys    133
Can someone explain me how to perform imagefiltering with HLSL? I couldn't get the informations from the manual... to apply a texture I use for example float4 textureoutput = tex2D(texturesampler, uvcoordinates); but where and how do i specify the imagefiltering? Thx!

##### Share on other sites
dave    2187
If i remember correctly you set the filtering in the sampler declaration in the fx file.

ace

##### Share on other sites
genesys    133
no FX file!! pure HLSL...

I'm using the irrlicht engine, which isn't able to load .fx files but it can read HLSL code and use it

##### Share on other sites
Cypher19    768
Quote:
 Original post by genesysno FX file!! pure HLSL...I'm using the irrlicht engine, which isn't able to load .fx files but it can read HLSL code and use it

Well, you must be declaring texturesampler SOMEWHERE if you're using HLSL.

##### Share on other sites
Quote:
 Original post by genesysno FX file!! pure HLSL...I'm using the irrlicht engine, which isn't able to load .fx files but it can read HLSL code and use it

I'm not familiar with that particular engine, but in normal D3D, you can call SetSamplerState to specify different filtering modes along with other properties.

neneboricua

##### Share on other sites
genesys    133
im declaring the sampler in the HLSL code . . .

sampler tex0;

struct PS_INPUT
{
float2 uv1 : TEXCOORD0;
};

float4 ps_main(PS_INPUT Input) : COLOR
{
float4 diffuse = tex2D(tex0,Input.uv1);
return diffuse;
}

here i declared the sampler at the beginning . . . how must i alter the code to activate filtering?

##### Share on other sites
sirob    1181
You should declare samplers like so:
texture diffuseTexture : Diffuse;sampler TextureSampler = sampler_state {    texture = <diffuseTexture>;    AddressU  = CLAMP;            AddressV  = CLAMP;    AddressW  = CLAMP;    MIPFILTER = LINEAR;    MINFILTER = LINEAR;    MAGFILTER = LINEAR;};

 Too bad there isn't code recognition for hlsl :(. [/edit]

##### Share on other sites
Quote:
 Original post by sirobYou should declare samplers like so:*** Source Snippet Removed *** Too bad there isn't code recognition for hlsl :(. [/edit]

Remember that this only works if you're using .fx files. The OP stated that he was just using HLSL and was not using the Effect interface.

In that situation, you can't set the filtering properties from shader code. You have to do it from the application using SetSamplerState. That's basically what the ID3DXEffect interface is doing behind the scenes for you anyway.

neneboricua

##### Share on other sites
genesys    133
hum . . . ok - but i can set the renderstates only by object (or rather by material which is applied to the object) . . . so - what do i have to do if i'm using multiple textures in one shader for one object - but i want one texture to be filtered and another to be unfiltered?

##### Share on other sites
jollyjeffers    1570
Quote:
 Original post by genesyshum . . . ok - but i can set the renderstates only by object (or rather by material which is applied to the object) . . . so - what do i have to do if i'm using multiple textures in one shader for one object - but i want one texture to be filtered and another to be unfiltered?

As neneboricua19 said, you have to use the IDirect3DDevice9::SetSamplerState() function before calling the draw call with your shader to configure the sort of sampling (linear/anisotropic/point etc..)

If you notice from the SDK documentation, the first parameter is a texture index (0 based). By changing this (as appropriate for your shader) you can have one texture linear-filtered and another point-filtered (for example). You could even have different addressing/wrapping modes for each stage if you desire...

pDev->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );pDev->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );pDev->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC );pDev->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC );pDev->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_POINT );pDev->SetSamplerState( 2, D3DSAMP_MINFILTER, D3DTEXF_POINT );pDev->SetTexture( 0, pSome_Linear_Filtered_Texture_Here );pDev->SetTexture( 1, pAn_Anisotropic_Texture );pDev->SetTexture( 2, a_HDR_point_filtered_texture );

sampler tex0 : register( s0 );sampler tex1 : register( s1 );sampler tex2 : register( s2 );//...float4 a = tex2D( tex0, uv ); // Read from a linear-filtered texturefloat4 b = tex2D( tex1, uv ); // Read from a anisotropic filtered texturefloat4 c = tex2D( tex2, uv ); // Read from a point-filtered texture//...

hth
Jack