Jump to content

  • Log In with Google      Sign In   
  • Create Account

Tim Coolman

Member Since 27 Mar 2012
Offline Last Active May 24 2013 12:14 PM

#4998113 Understanding Buffer, Texture, and Sampler "registers"

Posted by Tim Coolman on 06 November 2012 - 11:36 AM

I am trying to better understand the limitations implied by the register keyword for HLSL buffers, textures, and samplers. I will explain my understanding of it, then pose a couple questions. Any corrections, verification, or clarification on this topic is much appreciated.

Let's take constant buffers for example. The first parameter of VSSetConstantBuffers is Start Slot, which I believe corresponds to the register defined for a cbuffer in HLSL. So, if you have:

[source lang="cpp"]cbuffer MyConstantBuffer : register(b2){ ...};[/source]

Then in order to supply an ID3D11Buffer resource for that cbuffer, then you would call:

[source lang="cpp"]myContext->VSSetConstantBuffer(2, 1, & myBufferResource);[/source]

Where "2" is the slot/register number indicated by "b2" in HLSL. In the MSDN documentation for VSSetConstantBuffer, the Start Slot parameter can be from 0 to D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - 1. That would be 0 to 13 on my machine. So I assume that means I am limited to 14 cbuffers in my HLSL code, with registers ranging from "b0" to "b13". As I understand it, these registers can't be reused for multiple, "incompatible" constant buffers.

But is it also correct to assume that the scope of this limitation is per compiled shader? Currently I have all of my vertex and pixel shaders in a single file. So when the shaders are compiled, they are sharing the same configuration of constant buffers, textures, and samplers. With this setup I'm limited to 14 total constant buffers between all shaders. But if I separated out all my shaders to different files with their own constant buffer declarations, I could have 14 constant buffers PER shader since they would be completely separated from each other at compile time. Is this correct?

Then when I call VSSetConstantBuffer, I will provide the Start Slot number specific to the shader I'm about to use.

I hope this has made sense. Let me know if I am misunderstanding this at all. At first I thought that D3D11 programs were actually limited to 14 total constant buffers, but I found this hard to believe for large scale projects.