I have a system to render many objects, but I don’t know how to render more tan one object with same shader, let me explain:

I have a sphere and a cylinder, but both objects can be rendered by different shaders, example: Shader1: Shader to render object using 1 texture Shader2: Shader to render object using 2 textures

Both objects need to coexist in space, so if I want to render sphere with shader 1 and I use: shader1 resources:

context_->PSSetShaderResources(0, 1, &texture);
context_->PSSetSamplers(0, 1, &sampler);

shader2 resources:

context_->PSSetShaderResources(1, 1, &texture);
context_->PSSetSamplers(1, 1, &sampler);
context_->PSSetShaderResources(2, 1, &texture);
context_->PSSetSamplers(2, 1, &sampler);

in Shader1 the resources are references like this:

Texture2D colorTexture: register(t0);
SamplerState sampler: register(s0);

And in Shader2 the resources are references like this:

Texture2D colorTexture: register(t1);
SamplerState sampler: register(s1);
Texture2D colorTexture: register(t2);
SamplerState sampler: register(s2);

But what if I need to use shader1 resource’s in shader2???? How to manage those resources or do I need to replicate shader2 with shader1 registers?? this is the simplest example, this is part of a very much complex system with many many shaders and many textures, but I don’t know in what slot will be setted the resources, this can be absolutely generic, example, slot 5 will be used for texture 1 of shader2… It is possible to render many objects with minimal change of shaders, but the resources could be updated at any time.. I’m using directX11.


1 Answer 1


There are 2 approaches here. For the samplers, I would probably write a function that basically selects the sampler based upon the index you pass. But I think you will find, that you wont be switching samplers that much within your shader that much, if at all. You're more likely to switch to different shaders for different roles. Limit your samplers for starters until you have a firm grip, I think you will find you will create more shaders than samplers. Also, if you bind a sampler to a shader, then switch the shader, the original sampler is still bound. You dont need a different register per shader per sampler.

Now the 2nd part, in DX11 and DX12 (dx12 is more flexible), you can have texture arrays. Which you compile all your textures into one Shader Resource and reference it via an index. Effectively you can make your UV now UVW, where W is your texture index. This then means you can bind all your textures to one register, and reference this way. I went one step furher and use a constant buffer of lookups, that may return 1 or more texture samples based upon index (this becomes more a material type look up). But referencing the array directly should be enough for you to start.


You must log in to answer this question.

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