How to retrieve a sampler state in SM5/D3D12

Started by
6 comments, last by 21st Century Moose 7 years, 10 months ago

Hi, I need to use the tex2Dgrad() and I just used it in D3D9 + SM3 when we can easily write the following code in effect file:

sampler HeightSampler =
sampler_state
{
Texture =(HeightTexture) ;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

then we can call tex2Dgrad in this way:

tex2Dgrad(HeightSampler, MovedTex, dx, dy);

How can we get such a sampler state object in SM5 then?

Advertisement
That syntax within the {} section, initializing the sampler state members, actually isnt HLSL code. It's Microsoft FX code.

If you're using the old FX compiler, it gets used.
If you use the plain HLSL compiler, it gets ignored, even in DX9!

IMHO, you shouldn't use the FX system, even on DX9, which means you need to initialize sampler states yourself via the API.

To be clearer about what you're doing in D3D9, the Effects framework is just a software wrapper. Effects will pre-process this, take out the sampler states, replace them with a bunch of device->SetSamplerState calls, and optionally save and restore the previous state via stateblocks. Effects in D3D11 will do similar but will create and set sampler state objects. But the important thing to understand is that in each case Effects is nothing more than a software wrapper around the underlying API, and - in the absence of Effects - you can achieve the very same end result by just calling the underlying API directly yourself.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Since you mention D3D12...

If you're getting into the habit of declaring your root signatures in HLSL then you can declare Static Samplers inline with the Root Signature. This is becoming quite common since actually a great many of the samplers that you want to use are known at shader-writing time and don't need to be configurable from code (it can also be a small perf win). Of course if you want some level of "Option Screen" configurability for things like Anisotropy Level then this won't help.

Static Samplers

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

Of course if you want some level of "Option Screen" configurability for things like Anisotropy Level then this won't help.

Just have a different rootsig file per option permutation :D

Thanks you guys so much for the help.

I finally found a alternative function in SM4 or later: SampleGrad(), so that I can call it with a existing texture and sampler in HLSL source:

g_txDiffuse.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

However I still wonder if we can use the old tex2Dgrad() in this case, as it requires a sampler argument that already bound to an texture, is there any way to initialize that kind of "sampler" in D3D12, I guess we should do that totally in CPU/C++ code side?

D3D9/SM3 had samplers/textures as a single object.
D3D10/SM4 split them into two objects now, which must be configured in C++.

D3D9/SM3 had samplers/textures as a single object.
D3D10/SM4 split them into two objects now, which must be configured in C++.

...and what this means is that in D3D10+ you can create a single sampler state and reuse it for multiple textures (because all the sampler state does is define how the texture is sampled, it has nothing to do with the actual texture itself).

So:

g_txDiffuse.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

g_txNormal.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

g_txSpecular.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

g_txLightmap.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

g_txWibble.SampleGrad( g_samLinear, Input.Tex, ddx, ddy);

Will sample 5 different textures using the same sampler state.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement