0

Say I have these two variables in my shader:

HLSL

texture ModelTexture;
sampler2D TextureSampler

And this is how I would set the texture:

C#

myEffect.Parameters["ModelTexture"].SetValue(woodTexture);

What do about the sampler? There's no EffectParameter.SetValue(SamplerState) overload but I want to change filters in runtime. How do I go about it?

I'm pretty sure the rest of the code is OK, because if I initialize TextureSampler directly in HLSL it properly draws textured objects.

Edit:

I've tried setting a SamplerState object in the GraphicsDevice.

C#

GraphicsDevice.SamplerStates[0] = new SamplerState { Filter = TextureFilter.Linear };

HLSL

sampler2D TextureSampler : register(s0);

Now texture assignment (see the 2nd snippet) throws NullReferenceException.

I've also tried setting the texture like above instead of via EffectParameter:

C#

GraphicsDevice.Textures[0] = woodTexture;

HLSL

texture ModelTexture : register(t0);

The program compiles and runs but the texture is not loaded (rendered object is black).

matt-pielat
  • 1,659
  • 3
  • 20
  • 33

2 Answers2

0

It's a long time that I haven't used XNA framework, but by a simple search in Google I get to this results:

What Is Sampler State?

Sampler state determines how texture data is sampled using texture addressing modes, filtering, and level of detail. Use the SamplerState class to create a sampler state object. Set the sampler state to the graphics device using the GraphicsDevice.SamplerStates Property property.

GraphicsDevice.SamplerStates Property

This method returns the last sampler state that was set for the GraphicsDevice, or the default GraphicsDevice sampler state, if not previously set. Programmable shaders reference textures using the sampler number, which is set as the index of a GraphicsDevice.Textures.

So you must create a SamplerState object and set it in one of the GraphicsDevice.SamplerStates slots.

MRB
  • 3,752
  • 4
  • 30
  • 44
  • @matt-pielat Set your Texture with `GraphicsDevice.Textures` and refer to that in your shader file with `sampler texture : register(s0);` where sampler index correspond to the index in the texture collection. See example from this [link](https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.textures.aspx) – MRB Nov 26 '16 at 19:02
  • Compiles & runs but the rendered model is black. – matt-pielat Nov 26 '16 at 19:21
0

Sampler states are set on the graphics device object or inside the shader itself.

Daniel Armstrong
  • 829
  • 9
  • 22