c++optimizationdirectx-11vertex-attributes

Padding vertex structure manually to achieve allignment


When passing vertex data to your shaders, is it wise to apply padding to your vertex structure to achieve allignment (16byte) or is that something that the hardware is performing anyway?

For example are these two vertex structures equally effective?

struct Vertex44    // <<----- NO PADDING
{
    XMFLOAT3    position;
    XMFLOAT3    normal;
    XMFLOAT2    texCoord;
    XMFLOAT3    tangent;
};

struct Vertex48   // <<----- WITH PADDING
{
    XMFLOAT3    position;
    XMFLOAT3    normal;
    XMFLOAT2    texCoord;
    XMFLOAT3    tangent;
    float       padding;
};

Thank you!


Solution

  • For vertex formats, you do get a performance improvement on most hardware if you keep the total size of the stride to exactly 32-bytes or exactly 64-bytes. This only affects the Input Assembler layout engines. Once data is in the GPU pipeline the driver & runtime keep pretty much everything aligned as needed for the hardware.