2

What (or where documented, I only found some vague snippets on MSDN like " including any packing if necessary" and "...ByteWidth value of D3D11_BUFFER_DESC in multiples of 16") is Direct3D11's expected alignment/padding of the various ID3D11Buffer uses (e.g. vertex and constant buffers)?

For example when using D3D11_APPEND_ALIGNED_ELEMENT for an input layout for use with a vertex shader, what is the expectation for alignment? Originally I thought any vector types would want to be 16byte aligned, but this seems to not be the case. In-fact it seems there is not actually even an alignment expectation for the whole thing, at least beyond the 4bytes for the float type (not tried to force the compiler to miss-align a single primitive).

e.g.

D3D11_INPUT_ELEMENT_DESC inputDesc[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
//Seems to expect something like
struct Vertex
{
    float float float
    float float float float
    float float
};
//Rather than
__declspec(slign(16)) struct Vertex
{
    float float float padding
    float float float float
    float float padding padding
};
//HLSL
struct Input
{
    float4 pos : POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
};

In the case of constants, there is even a D3D11_INPUT_ELEMENT_DESC, yet there is still the question of ensuring the HLSL struct declaration and C++ one match up...

Fire Lancer
  • 29,364
  • 31
  • 116
  • 182

1 Answers1

2

You're approaching it from the wrong end. You should never ever try to hardcode input layouts in your C++ code - that is just a maintenance nightmare, because you will have to change it every time you change a shader, and when you have enough shaders, it becomes just not feasible to hardcode every single one.

Instead, look into D3DReflect API:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd607334%28v=vs.85%29.aspx

Basically, you can inspect the compiled shader and read the input layout directly off it instead of specifying it manually. You can also read the input names, and with that you can create vertex buffer dynamically with only those values that the shader needs.

Sunius
  • 2,789
  • 18
  • 30