DirectX11 Learning Notes

  • Tags : directx11
  • time :

1. Matrix of DX and HLSL

The matrix of DX (DirectX: XMMATRIX) follows Priority of row main element Storage, while HLSL's matrix defaults to Priority of column principal elements Storage. There are two ways to solve this problem.

1) Invert the matrix of DirectX.

DirectX::XMMATRIX m_matrix;

2) Using row in HLSL_ Major declaration matrix.

cbuffer Cbuffer0: register(b0)
row_major matrix mWorld;    // HLSL inside, no row_major if modified, the matrix is stored with column principal elements first 

I prefer Method 2.

2. The size of the constant buffer needs to be aligned with16 bytes

When creating a constant buffer, D3D11 needs to be filled in_ BUFFER_ DESC structure, where the ByteWidth property represents the size of the buffer, which should be an integer multiple of 16, otherwise the buffer creation will fail.

bufferDesc = {};
bufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(CBufferTrans);    // CBufferTrans it is a structure with dimensions aligned according to 16 bytes. if it is not an integer multiple of 16 bytes, buffer creation will fail 
hr = m_pD3dDevice->CreateBuffer(&bufferDesc, nullptr, &m_pCBufferTrans);