pad MemoryStorage size to multiple of 4 rather than using a vector of uint32_t -- this gets the same alignment without needs casts

This commit is contained in:
Seth Alves 2017-05-11 07:11:40 -07:00
parent 712386f256
commit dfb9fecf17
2 changed files with 7 additions and 7 deletions

View file

@ -39,8 +39,9 @@ StoragePointer Storage::toFileStorage(const QString& filename) const {
}
MemoryStorage::MemoryStorage(size_t size, const uint8_t* data) : _size(size) {
_data.resize((size + 3) / 4); // alloc smallest number of 4-byte chunks that will cover size bytes
// alloc smallest number of 4-byte chunks that will cover size bytes. The buffer is padded out to a multiple
// of 4 to force an alignment that glTextureSubImage2D can later use.
_data.resize((size + 3) & ~0x3);
if (data) {
memcpy(_data.data(), data, size);
}

View file

@ -41,16 +41,15 @@ namespace storage {
class MemoryStorage : public Storage {
public:
MemoryStorage(size_t size, const uint8_t* data = nullptr);
const uint8_t* data() const override { return reinterpret_cast<const uint8_t*>(_data.data()); }
uint8_t* data() { return reinterpret_cast<uint8_t*>(_data.data()); }
uint8_t* mutableData() override { return reinterpret_cast<uint8_t*>(_data.data()); }
const uint8_t* data() const override { return _data.data(); }
uint8_t* data() { return _data.data(); }
uint8_t* mutableData() override { return _data.data(); }
size_t _size { 0 };
size_t size() const override { return _size; }
operator bool() const override { return true; }
private:
// the vector is of uint32_t rather than uint8_t to force alignment
std::vector<uint32_t> _data;
std::vector<uint8_t> _data;
};
class FileStorage : public Storage {