I have a texture2D packed within my constant buffer. how do i set the texture using ID3D11DeviceContext::UpdateSubresource or another method?


1 Answer 1


Constant buffers can not be used this way, constant buffers are holders for global constants and are not used for buffer\texture data types.

It is imposable to have a texture packed inside a constant buffer.

Constant buffers have a very small size limit, well not super small but I really don't think even if you could put a texture into it that it would be of much use.

Constant buffers are also optimized so that there can be a few in flight on the GPU at any time so having a texture packed inside of it would be very bad for performance as you would end up with many copy's of the texture flying around the GPU taking up memory.

You want to keep the constant buffer size down so as not to use up all the constant buffer renaming memory that the driver uses to update them while they are still in flight on the GPU.

Organizing constant buffers

Constant buffers reduce the bandwidth required to update shader constants by allowing shader constants to be grouped together and committed at the same time rather than making individual calls to commit each constant separately.

The best way to efficiently use constant buffers is to organize shader variables into constant buffers based on their frequency of update. This allows an application to minimize the bandwidth required for updating shader constants. For example, a shader might declare two constant buffers and organize the data in each based on their frequency of update: data that needs to be updated on a per-object basis (like a world matrix) is grouped into a constant buffer which could be updated for each object. This is separate from data that characterizes a scene and is therefore likely to be updated much less often (when the scene changes).

Some reading for you on constant buffers

  • \$\begingroup\$ What you can do is upload all your textures in a texture array and then use the constant buffer to select an index into the array, which is basically a "poor man's" replacement for D3D12 bindless resources. \$\endgroup\$ Dec 7, 2016 at 3:17
  • \$\begingroup\$ Hmmmm. I like that idea! \$\endgroup\$ Dec 7, 2016 at 6:18

You must log in to answer this question.

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