1

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!

SvinSimpe
  • 850
  • 1
  • 12
  • 28
  • Did you benchmark it? – m.s. Jul 18 '15 at 13:19
  • You can put `__declspec(align(16))` in front of the struct to force it on a 16-byte boundary (although this is a MSVC keyword, but I'm assuming you use windows since you're using directX) – rwols Jul 18 '15 at 13:19
  • 1
    If using C++11 I much prefer the `alignas` specifier http://en.cppreference.com/w/cpp/language/alignas – Tommy Andersen Jul 18 '15 at 13:40

1 Answers1

1

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.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81