From fbb0a24c4f45a42db917a1491f849eccb33d145b Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 23 Feb 2017 02:08:46 -0800 Subject: [PATCH] FIxing the bug preventing to deserialize normals, clean up the ktx usage of the storage::Storage class and cleaning up somewhat the Qt pixel formats used --- .../src/gpu/gl45/GL45BackendTexture.cpp | 2 +- libraries/gpu/src/gpu/Format.cpp | 1 + libraries/gpu/src/gpu/Format.h | 2 + libraries/ktx/src/ktx/KTX.cpp | 4 +- libraries/ktx/src/ktx/KTX.h | 7 ++-- libraries/ktx/src/ktx/Reader.cpp | 4 +- libraries/ktx/src/ktx/Writer.cpp | 2 +- .../src/model-networking/TextureCache.cpp | 4 ++ libraries/model/src/model/TextureMap.cpp | 41 +++++++++++++++---- tests/ktx/src/main.cpp | 2 +- 10 files changed, 50 insertions(+), 19 deletions(-) diff --git a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp index f6c40259ea..d5ad0204bf 100644 --- a/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp +++ b/libraries/gpu-gl/src/gpu/gl45/GL45BackendTexture.cpp @@ -146,7 +146,7 @@ void GL45Texture::copyMipFaceFromTexture(uint16_t sourceMip, uint16_t targetMip, GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(_gpuObject.getTexelFormat(), _gpuObject.getStoredMipFormat()); copyMipFaceLinesFromTexture(targetMip, face, size, 0, texelFormat.format, texelFormat.type, mipData->readData()); } else { - qCDebug(gpugllogging) << "Missing mipData level=" << sourceMip << " face=" << (int)face << " for texture " << _gpuObject.source().c_str(); + qCDebug(gpugllogging) << "Missing mipData level=" << sourceMip << " face=" << (int)face << " for texture " << _gpuObject.source().c_str(); } } diff --git a/libraries/gpu/src/gpu/Format.cpp b/libraries/gpu/src/gpu/Format.cpp index 5b61a3a5a4..de202911e3 100644 --- a/libraries/gpu/src/gpu/Format.cpp +++ b/libraries/gpu/src/gpu/Format.cpp @@ -11,6 +11,7 @@ using namespace gpu; const Element Element::COLOR_R_8 { SCALAR, NUINT8, RED }; +const Element Element::COLOR_SR_8 { SCALAR, NUINT8, SRED }; const Element Element::COLOR_RGBA_32{ VEC4, NUINT8, RGBA }; const Element Element::COLOR_SRGBA_32{ VEC4, NUINT8, SRGBA }; diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 4610597a56..493a2de3c2 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -150,6 +150,7 @@ enum Semantic { STENCIL, // Stencil only buffer DEPTH_STENCIL, // Depth Stencil buffer + SRED, SRGB, SRGBA, SBGRA, @@ -229,6 +230,7 @@ public: } static const Element COLOR_R_8; + static const Element COLOR_SR_8; static const Element COLOR_RGBA_32; static const Element COLOR_SRGBA_32; static const Element COLOR_BGRA_32; diff --git a/libraries/ktx/src/ktx/KTX.cpp b/libraries/ktx/src/ktx/KTX.cpp index b03f855783..cc9c1069b1 100644 --- a/libraries/ktx/src/ktx/KTX.cpp +++ b/libraries/ktx/src/ktx/KTX.cpp @@ -77,8 +77,8 @@ KTX::KTX() { KTX::~KTX() { } -void KTX::resetStorage(Storage* storage) { - _storage.reset(storage); +void KTX::resetStorage(StoragePointer& storage) { + _storage = storage; } const Header* KTX::getHeader() const { diff --git a/libraries/ktx/src/ktx/KTX.h b/libraries/ktx/src/ktx/KTX.h index 0e0bb3831e..7aef33704e 100644 --- a/libraries/ktx/src/ktx/KTX.h +++ b/libraries/ktx/src/ktx/KTX.h @@ -345,8 +345,7 @@ namespace ktx { void setUncompressed(GLType type, uint32_t typeSize, GLFormat format, GLInternalFormat_Uncompressed internalFormat, GLBaseInternalFormat baseInternalFormat) { glType = (uint32_t) type; - // FIXME this should correspond to the size of glType - glTypeSize = 1; + glTypeSize = typeSize; glFormat = (uint32_t) format; glInternalFormat = (uint32_t) internalFormat; glBaseInternalFormat = (uint32_t) baseInternalFormat; @@ -421,7 +420,7 @@ namespace ktx { using Images = std::vector; class KTX { - void resetStorage(Storage* src); + void resetStorage(StoragePointer& src); KTX(); public: @@ -449,7 +448,7 @@ namespace ktx { static Images writeImages(Byte* destBytes, size_t destByteSize, const Images& images); // Parse a block of memory and create a KTX object from it - static std::unique_ptr create(std::unique_ptr& src); + static std::unique_ptr create(StoragePointer& src); static bool checkHeaderFromStorage(size_t srcSize, const Byte* srcBytes); static Images parseImages(const Header& header, size_t srcSize, const Byte* srcBytes); diff --git a/libraries/ktx/src/ktx/Reader.cpp b/libraries/ktx/src/ktx/Reader.cpp index e9e0f2760c..d74b45c01c 100644 --- a/libraries/ktx/src/ktx/Reader.cpp +++ b/libraries/ktx/src/ktx/Reader.cpp @@ -163,7 +163,7 @@ namespace ktx { return images; } - std::unique_ptr KTX::create(std::unique_ptr& src) { + std::unique_ptr KTX::create(StoragePointer& src) { if (!src) { return nullptr; } @@ -173,7 +173,7 @@ namespace ktx { } std::unique_ptr result(new KTX()); - result->resetStorage(src.release()); + result->resetStorage(src); // read metadata // result->_keyValues = getKeyValues(result->getHeader()->bytesOfKeyValueData, result->getKeyValueData()); diff --git a/libraries/ktx/src/ktx/Writer.cpp b/libraries/ktx/src/ktx/Writer.cpp index 747e1e7bac..901571f804 100644 --- a/libraries/ktx/src/ktx/Writer.cpp +++ b/libraries/ktx/src/ktx/Writer.cpp @@ -27,7 +27,7 @@ namespace ktx { }; std::unique_ptr KTX::create(const Header& header, const Images& images, const KeyValues& keyValues) { - std::unique_ptr storagePointer; + StoragePointer storagePointer; { auto storageSize = ktx::KTX::evalStorageSize(header, images); auto memoryStorage = new storage::MemoryStorage(storageSize); diff --git a/libraries/model-networking/src/model-networking/TextureCache.cpp b/libraries/model-networking/src/model-networking/TextureCache.cpp index 1f21e9e78d..f4bb3707e8 100644 --- a/libraries/model-networking/src/model-networking/TextureCache.cpp +++ b/libraries/model-networking/src/model-networking/TextureCache.cpp @@ -232,6 +232,10 @@ NetworkTexture::TextureLoaderFunc getTextureLoaderForType(NetworkTexture::Type t return model::TextureUsage::createMetallicTextureFromImage; break; } + case Type::OCCLUSION_TEXTURE: { + return model::TextureUsage::create2DTextureFromImage; + break; + } case Type::STRICT_TEXTURE: { return model::TextureUsage::createStrict2DTextureFromImage; break; diff --git a/libraries/model/src/model/TextureMap.cpp b/libraries/model/src/model/TextureMap.cpp index d2cdeb0a0b..c2bca9d1be 100755 --- a/libraries/model/src/model/TextureMap.cpp +++ b/libraries/model/src/model/TextureMap.cpp @@ -85,7 +85,7 @@ QImage processSourceImage(const QImage& srcImage, bool cubemap) { return srcImage; } -gpu::Texture* cacheTexture(const std::string& name, gpu::Texture* srcTexture, bool write = true, bool read = false) { // FIXME: set read to false for a working state +gpu::Texture* cacheTexture(const std::string& name, gpu::Texture* srcTexture, bool write = true, bool read = true) { if (!srcTexture) { return nullptr; } @@ -132,8 +132,32 @@ gpu::Texture* cacheTexture(const std::string& name, gpu::Texture* srcTexture, bo } if (read && QFileInfo(cacheFilename.c_str()).exists()) { - auto ktxFile = ktx::KTX::create(std::unique_ptr(new storage::FileStorage(cacheFilename.c_str()))); - returnedTexture->setKtxBacking(ktxFile); + { + FILE* file = fopen(cacheFilename.c_str(), "rb"); + if (file != nullptr) { + // obtain file size: + fseek (file , 0 , SEEK_END); + auto size = ftell(file); + rewind(file); + + auto storage = std::make_shared(size); + fread(storage->data(), 1, storage->size(), file); + fclose (file); + + //then create a new texture out of the ktx + auto theNewTexure = Texture::unserialize(srcTexture->getUsage(), srcTexture->getUsageType(), + ktx::KTX::create(std::static_pointer_cast(storage)), srcTexture->getSampler()); + + if (theNewTexure) { + returnedTexture = theNewTexure; + delete srcTexture; + } + } + } + + // auto ktxFile = ktx::KTX::create(std::unique_ptr(new storage::FileStorage(cacheFilename.c_str()))); + // returnedTexture->setKtxBacking(ktxFile); + } } @@ -360,15 +384,16 @@ gpu::Texture* TextureUsage::createNormalTextureFromNormalImage(const QImage& src PROFILE_RANGE(resource_parse, "createNormalTextureFromNormalImage"); QImage image = processSourceImage(srcImage, false); - // Make sure the normal map source image is RGBA32 - if (image.format() != QImage::Format_RGBA8888) { - image = image.convertToFormat(QImage::Format_RGBA8888); + // Make sure the normal map source image is ARGB32 + if (image.format() != QImage::Format_ARGB32) { + image = image.convertToFormat(QImage::Format_ARGB32); } + gpu::Texture* theTexture = nullptr; if ((image.width() > 0) && (image.height() > 0)) { - gpu::Element formatMip = gpu::Element::COLOR_RGBA_32; + gpu::Element formatMip = gpu::Element::COLOR_BGRA_32; gpu::Element formatGPU = gpu::Element::COLOR_RGBA_32; theTexture = (gpu::Texture::create2D(formatGPU, image.width(), image.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); @@ -378,7 +403,7 @@ gpu::Texture* TextureUsage::createNormalTextureFromNormalImage(const QImage& src generateMips(theTexture, image, true); theTexture->setSource(srcImageName); - theTexture = cacheTexture(theTexture->source(), theTexture, true, false); + theTexture = cacheTexture(theTexture->source(), theTexture, true, true); } return theTexture; diff --git a/tests/ktx/src/main.cpp b/tests/ktx/src/main.cpp index 2dbf2f60d7..34280cb263 100644 --- a/tests/ktx/src/main.cpp +++ b/tests/ktx/src/main.cpp @@ -113,7 +113,7 @@ int main(int argc, char** argv) { outFile.close(); } - auto ktxFile = ktx::KTX::create(std::unique_ptr(new storage::FileStorage(TEST_IMAGE_KTX))); + auto ktxFile = ktx::KTX::create(std::shared_ptr(new storage::FileStorage(TEST_IMAGE_KTX))); { const auto& memStorage = ktxMemory->getStorage(); const auto& fileStorage = ktxFile->getStorage();