From 6490c52245ee004e43000854b7e05a0dcfe33743 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 24 May 2017 13:02:54 -0700 Subject: [PATCH] Do not allow divide by zero on assigning mip data --- libraries/gpu/src/gpu/Texture.cpp | 36 +++++++++++++++---------------- libraries/gpu/src/gpu/Texture.h | 2 +- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 4dd40eb861..4b836512c4 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -123,42 +123,40 @@ bool MemoryStorage::isMipAvailable(uint16 level, uint8 face) const { return (mipFace && mipFace->getSize()); } -bool MemoryStorage::allocateMip(uint16 level) { - bool changed = false; +void MemoryStorage::allocateMip(uint16 level) { + auto faceCount = Texture::NUM_FACES_PER_TYPE[getType()]; if (level >= _mips.size()) { - _mips.resize(level+1, std::vector(Texture::NUM_FACES_PER_TYPE[getType()])); - changed = true; + _mips.resize(level + 1, std::vector(faceCount)); } auto& mip = _mips[level]; - for (auto& face : mip) { - if (!face) { - changed = true; - } + if (mip.size() != faceCount) { + mip.resize(faceCount); } - bumpStamp(); - - return changed; } void MemoryStorage::assignMipData(uint16 level, const storage::StoragePointer& storagePointer) { - allocateMip(level); auto& mip = _mips[level]; + auto faceCount = Texture::NUM_FACES_PER_TYPE[getType()]; + // here we grabbed an array of faces // The bytes assigned here are supposed to contain all the faces bytes of the mip. // For tex1D, 2D, 3D there is only one face // For Cube, we expect the 6 faces in the order X+, X-, Y+, Y-, Z+, Z- - auto sizePerFace = storagePointer->size() / mip.size(); - size_t offset = 0; - for (auto& face : mip) { - face = storagePointer->createView(sizePerFace, offset); - offset += sizePerFace; - } + auto sizePerFace = storagePointer->size() / faceCount; - bumpStamp(); + if (sizePerFace > 0) { + size_t offset = 0; + for (auto& face : mip) { + face = storagePointer->createView(sizePerFace, offset); + offset += sizePerFace; + } + + bumpStamp(); + } } diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 98a4add3c8..211dc7b8ce 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -304,7 +304,7 @@ public: bool isMipAvailable(uint16 level, uint8 face = 0) const override; protected: - bool allocateMip(uint16 level); + void allocateMip(uint16 level); std::vector> _mips; // an array of mips, each mip is an array of faces };