Switching texture backing to opaque storage type

This commit is contained in:
Brad Davis 2017-02-17 13:01:46 -08:00
parent eafe0a04d5
commit 61ce66a039
3 changed files with 20 additions and 56 deletions

View file

@ -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) : Texture::Pixels::Pixels(const Element& format, Size size, const Byte* bytes) :
_format(format), _format(format),
_sysmem(size, bytes), _storage(new storage::MemoryStorage(size, bytes)) {
_isGPULoaded(false) { Texture::updateTextureCPUMemoryUsage(0, _storage->size());
Texture::updateTextureCPUMemoryUsage(0, _sysmem.getSize());
} }
Texture::Pixels::~Pixels() { Texture::Pixels::~Pixels() {
Texture::updateTextureCPUMemoryUsage(_sysmem.getSize(), 0); Texture::updateTextureCPUMemoryUsage(_storage->size(), 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);
} }
void Texture::Storage::assignTexture(Texture* texture) { void Texture::Storage::assignTexture(Texture* texture) {
@ -183,14 +159,6 @@ const Texture::PixelsPointer Texture::Storage::getMipFace(uint16 level, uint8 fa
return PixelsPointer(); 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 { bool Texture::Storage::isMipAvailable(uint16 level, uint8 face) const {
PixelsPointer mipFace = getMipFace(level, face); PixelsPointer mipFace = getMipFace(level, face);
return (mipFace && mipFace->getSize()); return (mipFace && mipFace->getSize());
@ -229,7 +197,8 @@ bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size s
auto faceBytes = bytes; auto faceBytes = bytes;
Size allocated = 0; Size allocated = 0;
for (auto& face : mip) { for (auto& face : mip) {
allocated += face->setData(format, sizePerFace, faceBytes); face.reset(new Pixels(format, size, bytes));
allocated += size;
faceBytes += sizePerFace; 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) { bool Texture::Storage::assignMipFaceData(uint16 level, const Element& format, Size size, const Byte* bytes, uint8 face) {
allocateMip(level); allocateMip(level);
auto mip = _mips[level]; auto& mip = _mips[level];
Size allocated = 0; Size allocated = 0;
if (face < mip.size()) { if (face < mip.size()) {
auto mipFace = mip[face]; mip[face].reset(new Pixels(format, size, bytes));
allocated += mipFace->setData(format, size, bytes); allocated += size;
bumpStamp(); bumpStamp();
} }

View file

@ -17,6 +17,8 @@
#include <QMetaType> #include <QMetaType>
#include <QUrl> #include <QUrl>
#include <shared/Storage.h>
#include "Forward.h" #include "Forward.h"
#include "Resource.h" #include "Resource.h"
@ -224,26 +226,26 @@ public:
bool operator!=(const Usage& usage) { return (_flags != usage._flags); } bool operator!=(const Usage& usage) { return (_flags != usage._flags); }
}; };
class Pixels { class Pixels {
public: public:
using StoragePointer = storage::StoragePointer;
Pixels() {} Pixels() {}
Pixels(const Pixels& pixels) = default; Pixels(const Pixels& pixels) = default;
Pixels(const Element& format, Size size, const Byte* bytes); Pixels(const Element& format, Size size, const Byte* bytes);
Pixels(const Element& format, StoragePointer& storage) : _format(format), _storage(storage.release()) {}
~Pixels(); ~Pixels();
const Byte* readData() const { return _sysmem.readData(); } const Byte* readData() const { return _storage->data(); }
Size getSize() const { return _sysmem.getSize(); } Size getSize() const { return _storage->size(); }
Size resize(Size pSize);
Size setData(const Element& format, Size size, const Byte* bytes );
const Element& getFormat() const { return _format; } const Element& getFormat() const { return _format; }
void notifyGPULoaded();
protected: protected:
Element _format; Element _format;
Sysmem _sysmem; StoragePointer _storage;
bool _isGPULoaded;
friend class Texture; friend class Texture;
}; };
@ -296,10 +298,6 @@ public:
const Texture* getTexture() const { return _texture; } const Texture* getTexture() const { return _texture; }
friend class 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; } const Sampler& getSampler() const { return _sampler; }
Stamp getSamplerStamp() const { return _samplerStamp; } 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 setExternalTexture(uint32 externalId, void* externalFence);
void setExternalRecycler(const ExternalRecycler& recycler); void setExternalRecycler(const ExternalRecycler& recycler);
ExternalRecycler getExternalRecycler() const; ExternalRecycler getExternalRecycler() const;

View file

@ -72,7 +72,7 @@ ktx::KTXUniquePointer Texture::serialize(const Texture& texture) {
for (uint32_t level = 0; level < header.numberOfMipmapLevels; level++) { for (uint32_t level = 0; level < header.numberOfMipmapLevels; level++) {
auto mip = texture.accessStoredMipFace(level); auto mip = texture.accessStoredMipFace(level);
if (mip) { if (mip) {
images.emplace_back(ktx::Image(mip->getSize(), 0, mip->readData())); images.emplace_back(ktx::Image((uint32_t)mip->getSize(), 0, mip->readData()));
} }
} }