From 61ce66a03956c8a68a73ce11f99bdea6b6af8667 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 17 Feb 2017 13:01:46 -0800 Subject: [PATCH] Switching texture backing to opaque storage type --- libraries/gpu/src/gpu/Texture.cpp | 47 +++++---------------------- libraries/gpu/src/gpu/Texture.h | 27 +++++++-------- libraries/gpu/src/gpu/Texture_ktx.cpp | 2 +- 3 files changed, 20 insertions(+), 56 deletions(-) diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index b2cb41e9bb..833647bbda 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -122,36 +122,12 @@ uint8 Texture::NUM_FACES_PER_TYPE[NUM_TYPES] = { 1, 1, 1, 6 }; Texture::Pixels::Pixels(const Element& format, Size size, const Byte* bytes) : _format(format), - _sysmem(size, bytes), - _isGPULoaded(false) { - Texture::updateTextureCPUMemoryUsage(0, _sysmem.getSize()); + _storage(new storage::MemoryStorage(size, bytes)) { + Texture::updateTextureCPUMemoryUsage(0, _storage->size()); } Texture::Pixels::~Pixels() { - Texture::updateTextureCPUMemoryUsage(_sysmem.getSize(), 0); -} - -Texture::Size Texture::Pixels::resize(Size pSize) { - auto prevSize = _sysmem.getSize(); - auto newSize = _sysmem.resize(pSize); - Texture::updateTextureCPUMemoryUsage(prevSize, newSize); - return newSize; -} - -Texture::Size Texture::Pixels::setData(const Element& format, Size size, const Byte* bytes ) { - _format = format; - auto prevSize = _sysmem.getSize(); - auto newSize = _sysmem.setData(size, bytes); - Texture::updateTextureCPUMemoryUsage(prevSize, newSize); - _isGPULoaded = false; - return newSize; -} - -void Texture::Pixels::notifyGPULoaded() { - _isGPULoaded = true; - auto prevSize = _sysmem.getSize(); - auto newSize = _sysmem.resize(0); - Texture::updateTextureCPUMemoryUsage(prevSize, newSize); + Texture::updateTextureCPUMemoryUsage(_storage->size(), 0); } void Texture::Storage::assignTexture(Texture* texture) { @@ -183,14 +159,6 @@ const Texture::PixelsPointer Texture::Storage::getMipFace(uint16 level, uint8 fa return PixelsPointer(); } -void Texture::Storage::notifyMipFaceGPULoaded(uint16 level, uint8 face) const { - PixelsPointer mipFace = getMipFace(level, face); - // Free the mips - if (mipFace) { - mipFace->notifyGPULoaded(); - } -} - bool Texture::Storage::isMipAvailable(uint16 level, uint8 face) const { PixelsPointer mipFace = getMipFace(level, face); return (mipFace && mipFace->getSize()); @@ -229,7 +197,8 @@ bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size s auto faceBytes = bytes; Size allocated = 0; for (auto& face : mip) { - allocated += face->setData(format, sizePerFace, faceBytes); + face.reset(new Pixels(format, size, bytes)); + allocated += size; faceBytes += sizePerFace; } @@ -242,11 +211,11 @@ bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size s bool Texture::Storage::assignMipFaceData(uint16 level, const Element& format, Size size, const Byte* bytes, uint8 face) { allocateMip(level); - auto mip = _mips[level]; + auto& mip = _mips[level]; Size allocated = 0; if (face < mip.size()) { - auto mipFace = mip[face]; - allocated += mipFace->setData(format, size, bytes); + mip[face].reset(new Pixels(format, size, bytes)); + allocated += size; bumpStamp(); } diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index cc351de787..0a5afe78c3 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -17,6 +17,8 @@ #include #include +#include + #include "Forward.h" #include "Resource.h" @@ -224,26 +226,26 @@ public: bool operator!=(const Usage& usage) { return (_flags != usage._flags); } }; + class Pixels { public: + using StoragePointer = storage::StoragePointer; + Pixels() {} Pixels(const Pixels& pixels) = default; Pixels(const Element& format, Size size, const Byte* bytes); + Pixels(const Element& format, StoragePointer& storage) : _format(format), _storage(storage.release()) {} ~Pixels(); - const Byte* readData() const { return _sysmem.readData(); } - Size getSize() const { return _sysmem.getSize(); } - Size resize(Size pSize); - Size setData(const Element& format, Size size, const Byte* bytes ); + const Byte* readData() const { return _storage->data(); } + Size getSize() const { return _storage->size(); } const Element& getFormat() const { return _format; } - - void notifyGPULoaded(); - + + protected: Element _format; - Sysmem _sysmem; - bool _isGPULoaded; + StoragePointer _storage; friend class Texture; }; @@ -296,10 +298,6 @@ public: const Texture* getTexture() const { return _texture; } friend class Texture; - - // THis should be only called by the Texture from the Backend to notify the storage that the specified mip face pixels - // have been uploaded to the GPU memory. IT is possible for the storage to free the system memory then - virtual void notifyMipFaceGPULoaded(uint16 level, uint8 face) const; }; @@ -481,9 +479,6 @@ public: const Sampler& getSampler() const { return _sampler; } Stamp getSamplerStamp() const { return _samplerStamp; } - // Only callable by the Backend - void notifyMipFaceGPULoaded(uint16 level, uint8 face = 0) const { return _storage->notifyMipFaceGPULoaded(level, face); } - void setExternalTexture(uint32 externalId, void* externalFence); void setExternalRecycler(const ExternalRecycler& recycler); ExternalRecycler getExternalRecycler() const; diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index d63bffb74f..404aca77a4 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -72,7 +72,7 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture) { for (uint32_t level = 0; level < header.numberOfMipmapLevels; level++) { auto mip = texture.accessStoredMipFace(level); if (mip) { - images.emplace_back(ktx::Image(mip->getSize(), 0, mip->readData())); + images.emplace_back(ktx::Image((uint32_t)mip->getSize(), 0, mip->readData())); } }