Sign in to follow this  
Blasp

Choose texture slot to sample by number

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this