Jump to content
  • Advertisement
Sign in to follow this  

Choose texture slot to sample by number

This topic is 1278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The way my engine works is that first I bind all the textures needed to render a frame at once (PSSetShaderResources()), then I specify which texture slot to sample per-instance by variables in an instance buffer. It works great, the problem is that it is coded in such an ugly way, just look at this HLSL:


Texture2D Texture0 : register(ps, t0);

Texture2D Texture1 : register(ps, t1);

Texture2D Texture2 : register(ps, t2);

Texture2D Texture3 : register(ps, t3);

Texture2D Texture4 : register(ps, t4);

Texture2D Texture5 : register(ps, t5);

Texture2D Texture6 : register(ps, t6);

Texture2D Texture7 : register(ps, t7);


float4 texture_sample(float id, float2 texcoord)


[call] switch (id)


case 0: { return Texture0.Sample(ObjSamplerState, texcoord); } break;

case 1: { return Texture1.Sample(ObjSamplerState, texcoord); } break;

case 2: { return Texture2.Sample(ObjSamplerState, texcoord); } break;

case 3: { return Texture3.Sample(ObjSamplerState, texcoord); } break;

case 4: { return Texture4.Sample(ObjSamplerState, texcoord); } break;

case 5: { return Texture5.Sample(ObjSamplerState, texcoord); } break;

case 6: { return Texture6.Sample(ObjSamplerState, texcoord); } break;

case 7: { return Texture7.Sample(ObjSamplerState, texcoord); } break;

default: { return Texture0.Sample(ObjSamplerState, texcoord); } break;




Is there any way to directly sample a texture in a slot, by number? Something like this: id.Sample(ObjSamplerState, texcoord); with id being the number of the texture slot as registered. Id rather not use texture arrays because all those textures can have different and even changing dimensions, so it would be more complicated.

Edited by Blasp

Share this post

Link to post
Share on other sites
Texture arrays are the only way to dynamically index textures in D3D11. D3D12 will support dynamically indexing into arrays of separate texture SRV's, which is also supported by Nvidia's bindless texture extensions for GL (I imagine that Vulcan will support it as well). However, you should keep in mind that current hardware that supports this functionality will suffer from reduced performance for cases where the index is divergent among warps/wavefronts. Edited by MJP

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!