1
\$\begingroup\$

In Direct3D 9, after assigning a few textures to samplers using SetTexture, what is the correct way to access/create the samplers in the relevant pixel shader? Previously I was creating samplers like this:

texture tex : register(r0);
sampler2D sam = sampler_state {
    texture = <tex>;
    minfilter = none;
    magfilter = none;
    mipfilter = none;
};

Which seemed to work fine, however, now that I'm binding multiple textures this doesn't work consistently. However, the following appears to work:

sampler2D sam : register(s0);
sampler2D sam1 : register(s1);

The documentation for SetTexture says:

Programmable shaders reference textures using the sampler number.

But I'm not sure whether or not this means that the sampler registers should be used to sample textures set with SetTexture, or whether the sampler requires any further initialisation aside from the call to SetTexture.

\$\endgroup\$
2
  • \$\begingroup\$ Why are you using legacy Direct3D 9 instead of something more modern like DirectX 11? Unless you are specifically targeting Windows XP, there's no need to use it. Debugging support on modern versions of Windows is quite limited, and modern tools & libraries are built for DirectX 11. While Direct3D 10 supported limited hardware, DirectX 11 supports all hardware with WDDM drivers. To keep using Direct3D 9 means sticking with the 7+ year old legacy DirectX SDK and deprecated D3DX9 utility library. See MSDN. \$\endgroup\$ Feb 27, 2017 at 18:27
  • \$\begingroup\$ Note that I don't recommend using DirectX 12 unless you are already an expert in using DirectX 11. DirectX 12 really should have been called "Direct3D Extreme Pro" or maybe "ReallyDirect3D". \$\endgroup\$ Feb 27, 2017 at 18:31

1 Answer 1

0
\$\begingroup\$

In Direct3D 9 HLSL all you need to declare is the sampler; you do not need to declare the texture at all.

So:

sampler sam0: register(s0); // s0 = sampler register 0

To bind the texture use:

device->SetTexture (0, tex); // 0 is the sampler register number

And to sample it use:

tex2D (sam0, coords); // sample using the sampler declared above

Setting a sampler state at declaration time (via the "= sampler_state {...}" syntax) is actually not part of HLSL at all; it's part of the Effects framework instead, which is a software library written by Microsoft and which abstracts some of the details of dealing with shaders.

You can add this code to regular HLSL but it will have no effect.

What you should do instead is use SetSamplerState calls to setup the sampler from the C/C++ side.

Direct3D 10+ is a lot more flexible in that it fully decouples samplers from textures, but Direct3D 9 doesn't have that flexibility.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks. Do you know where in the documentation the relationship between SetTexture, SetSamplerState and HLSL are described? \$\endgroup\$ Mar 1, 2017 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .