Basic HLSL texture question

Started by
5 comments, last by Foxbane 6 years, 8 months ago

Is the slot pointed to by a Texture2D/SamplerState in a pixel shader assigned at compile time and immutable?

So on the HLSL side I'd have:

Texture2D tex0 : register(t0);

Texture2D tex1 : register(t1);

SamplerState smp0 : register(s0);

SamplerState smp1 : register(s1);

And then on the host side:

context->PSSetShaderResource( 0, 1, textureA )

context->PSSetShaderResource( 1, 1, textureB )

context->PSSetSamplers( 0, 1, samplerA );

context->PSSetSamplers( 1, 1, samplerB );

When the program is run tex0 would refer to textureA, tex1 to textureB and so on with the samplers.

What if I wanted to switch them around on the HLSL side.  So in OpenGL I could do glUniform1i( tex0, 1 ) and glUniform( tex1, 0 ).  This would make tex0 refer to textureB and tex1 refer to textureA.  Can the same be done with DX11?  If so what is the call to modify tex0/tex0 - and the smp0/smp1?

Advertisement

The first parameter to PSSetShaderResource() and PSSetSamplers() is the start slot (if the number of resources to bind is one, the only slot) to bind to the pipeline. In your case, if you wanted tex0 to refer to textureB you would call "context->PSSetShaderResource(0, 1, textureB);".

And, to be more specific, AFAIK you can't "switch them around" on the HLSL side. When you write "Texture2D tex0 : register(t0);" you specify that "tex0" will refer to the texture register with index 0. Then, it is up to you to bind the correct SRVs to that register on the C++ side. You can then change whatever you want to bind to that register, by calling XSSetShaderResource() with different start slots.

2 hours ago, GuyWithBeard said:

And, to be more specific, AFAIK you can't "switch them around" on the HLSL side.

And that would be the answer!  Bummer.  Thanks.

I'm not sure if you interpreted what he was saying correctly, if you wanted to swap your textures, you can do that via the API calls:


// Your original
context->PSSetSamplers( 0, 1, samplerA );
context->PSSetSamplers( 1, 1, samplerB );
// just swap'em
context->PSSetSamplers( 0, 1, samplerB );
context->PSSetSamplers( 1, 1, samplerA );

 

Yeah, sorry if I was being unclear. You can definitely swap them around, just not on the HLSL side. You have to do it on the host side.

Yup - I understood it.  Thx.

This topic is closed to new replies.

Advertisement