Add util function to align serialised buffers in chunks. Refs #133

* To align a buffer inside a chunk to a wider boundary like 32-bytes in
  this case, the chunk needs to be aligned and the buffer within it also
  needs to be aligned.
* The utility function accounts for the buffer serialised having a
  uint32 length in front of it, so it pads out until that will be at
  the desired boundary. This is a bit of a messy solution, but the
  easiest way to ensure the padding is there while being easily
  backwards compatible with old logs without the padding.
* D3D11 and GL serialise versions are bumped, D3D11 version is backwards
  compatible, GL breaks compatibility.
This commit is contained in:
baldurk
2015-04-24 11:07:02 +02:00
parent 24df051659
commit 584d439620
9 changed files with 65 additions and 3 deletions
+30
View File
@@ -940,6 +940,36 @@ void Serialiser::SkipBuffer()
ReadBytes(len);
}
void Serialiser::AlignNextBuffer(const size_t alignment)
{
// this is a super hack but it's the easiest way to align a buffer to a larger pow2 alignment
// than the default 16-bytes, while still able to be backwards compatible with old logs that
// weren't so aligned. We know that SerialiseBuffer will align to the nearest 16-byte boundary
// after serialising 4 bytes of length, so we pad up to exactly 4 bytes before the desired
// alignment, then after the 4 byte length there's nothing for the other padding to do.
//
// Note the chunk still needs to be aligned when the memory is allocated - this just ensures
// the offset from the start is also aligned
size_t len = 0;
if(m_Mode >= WRITING)
{
// add sizeof(uint32_t) since we'll be serialising out how much padding is here
uint64_t curoffs = GetOffset() + sizeof(uint32_t);
uint64_t alignedoffs = AlignUp(curoffs, (uint64_t)alignment);
len = size_t(alignedoffs - curoffs);
}
// avoid dynamically allocating
RDCASSERT(alignment <= 128);
byte padding[128] = {0};
byte *p = &padding[0];
SerialiseBuffer("", p, len);
}
void Serialiser::SerialiseBuffer(const char *name, byte *&buf, size_t &len)
{
uint32_t bufLen = (uint32_t)len;