Effect - setting parameters

I’m moving my tile map drawing from SpriteBatch to VertexBuffer method. I’ve decided to write my own FX to refresh my memory(I have experience from XNA). First I encountered problems with setting parameters from code:

// CS class
this._dungeonShader.Parameters["tileDataTexture"].SetValue(this._tileDataTexture);
this._dungeonShader.Parameters["tilePaletteTexture"].SetValue(this._tileDataTexture);
…
// FX file
Texture2D tileDataTexture;
Texture2D tilePaletteTexture;

sampler s_tileData = sampler_state {
    Texture = <tileDataTexture >;
};

sampler s_tilePalette = sampler_state {
    Texture = <tilePaletteTexture>;
}; 

Result was very strange and both samplers were returning wrong values. Furthermore samplers result depended on the order of texture declaration and when I changed it to:

//Texture2D tileDataTexture;
//Texture2D tilePaletteTexture;
Texture2D tilePaletteTexture;
Texture2D tileDataTexture;

I’ve received different result. After changing my game code to:

/*this._dungeonShader.Parameters["tileDataTexture"].SetValue(this._tileDataTexture);
this._dungeonShader.Parameters["tilePaletteTexture"].SetValue(this._tileDataTexture);*/
this._graphicsDevice.Textures[0] = this._tileDataTexture;
this._graphicsDevice.Textures[1] = this._paletteTexture;

it worked as it should.

Now I’m trying to define SamplerState.PointClamp in FX file:

SamplerState Sampler {
    MipFilter = POINT;
    MinFilter = POINT;
    MagFilter = POINT;
    AddressU = Clamp;
    AddressV = Clamp;
    AddressW = Clamp;
    MaxMipLevel = 0;
};

Again without success and again when I used directly GraphicsDevice it worked:

this._graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;

This is my first HLSL experience after several years but I don’t remember that I had such a problems with XNA. Can someone tell me what am I doing wrong? Or maybe this is bug?

I don’t know about sampler state in effect files, I set them in code as well as you figured out already.

Sounds strange that the order of the textures/samplers would matter when you’re accessing them by name. I always register mine with indicies however using macros as in MonoGame.

I have same declarations for texture and sampling as seen here:
https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Effect/Resources/Macros.fxh

The macros could probably be extended further to allow specific sampler states for your needs.

1 Like

This reminds me of this issue. That’s the only cause I’ve seen of parameter order having an effect.

@Olivierko Thank you for macros it fixed problem with declaration order and it works as it should.

Anyone can help me with SamplerState? is it possible to set value for MipFilter,MinFilter etc. in FX file?

Texture2D ColorTexture;
sampler ColorSampler = sampler_state {
    texture = <ColorTexture>;
    AddressU = Wrap;
    AddressV = Wrap;
    MipFilter = Linear;
    MinFilter = Linear;
    MagFilter = Linear;
};