سؤال

I have read that some graphics cards benefit if you align your vertex data to be 32 bytes.

This usually involves adding padding:

typedef struct {
  float x, y, z;
  int padding[5];
} Vertex;

But I have been wondering, does this also mean you should allocate the data to be aligned to 32-bytes (malloc aligns to 1-byte)? Meaning the pointer to the data would divide evenly into 32? Does it matter?

(I am uploading this data to a VBO)

Thanks

هل كانت مفيدة؟

المحلول

Typically, the copy operation from the client memory to the VBO can be faster if the source memory is aligned (the destination typically will be). It somewhat depends on how you do the upload to the VBO.

That said, the upload will be the only thing that gets boosted by the alignment. Once the memory is in the VBO, it's the alignment of the VBO server memory (that you don't control) that matters (GL implementations know this, and they do align the VBO memory).

Oh, and 32 bytes with 20 bytes padding is just not going to be faster than 16 with 4 bytes of padding. What matters is that you have a power-of-two size, so that a single complete vertex fetch does not straddle cachelines.

Last, malloc does not align to 1 byte. It aligns at least to the minimum alignment requirement of basic types, which on most platforms is 8.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top