From 780d06883a769e30e9689005699cd6af89b13db1 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Mon, 16 Mar 2026 00:28:54 -0700 Subject: [PATCH] Refactoring KRTexture2D::uploadTexture, replacing with getLodData. --- kraken/resources/texture/KRTexture2D.cpp | 18 ++- kraken/resources/texture/KRTexture2D.h | 2 +- kraken/resources/texture/KRTextureCube.cpp | 16 ++- kraken/resources/texture/KRTextureKTX.cpp | 5 +- kraken/resources/texture/KRTextureKTX.h | 2 +- kraken/resources/texture/KRTextureKTX2.cpp | 10 +- kraken/resources/texture/KRTextureKTX2.h | 2 +- kraken/resources/texture/KRTexturePNG.cpp | 8 +- kraken/resources/texture/KRTexturePNG.h | 2 +- kraken/resources/texture/KRTexturePVR.cpp | 5 +- kraken/resources/texture/KRTexturePVR.h | 2 +- kraken/resources/texture/KRTextureTGA.cpp | 129 ++++++--------------- kraken/resources/texture/KRTextureTGA.h | 2 +- 13 files changed, 79 insertions(+), 124 deletions(-) diff --git a/kraken/resources/texture/KRTexture2D.cpp b/kraken/resources/texture/KRTexture2D.cpp index d1f0bbb..a9faab4 100755 --- a/kraken/resources/texture/KRTexture2D.cpp +++ b/kraken/resources/texture/KRTexture2D.cpp @@ -32,6 +32,7 @@ #include "KREngine-common.h" #include "KRTexture2D.h" #include "KRTextureManager.h" +using namespace hydra; using namespace mimir; @@ -51,6 +52,16 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim) return true; } + Vector2i dimensions2d = getDimensions(); + Vector3i dimensions = { dimensions2d.x, dimensions2d.y, 1 }; + size_t bufferSize = getMemRequiredForSize(lod_max_dim); + void* buffer = malloc(bufferSize); + + if (!getLodData(buffer, lod_max_dim)) { + delete buffer; + return false; + } + bool success = true; int prev_lod_max_dim = m_new_lod_max_dim; m_new_lod_max_dim = 0; @@ -91,12 +102,11 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim) break; } - if (!uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim)) { - success = false; - break; - } + device.streamUpload((void*)buffer, bufferSize, dimensions, texture.image); } + delete buffer; + if (success) { m_new_lod_max_dim = prev_lod_max_dim; m_haveNewHandles = true; diff --git a/kraken/resources/texture/KRTexture2D.h b/kraken/resources/texture/KRTexture2D.h index 6832ec1..81b218d 100755 --- a/kraken/resources/texture/KRTexture2D.h +++ b/kraken/resources/texture/KRTexture2D.h @@ -48,7 +48,7 @@ public: virtual bool save(const std::string& path) override; virtual bool save(mimir::Block& data) override; - virtual bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) = 0; + virtual bool getLodData(void* buffer, int lod_max_dim) = 0; virtual hydra::Vector2i getDimensions() const = 0; protected: diff --git a/kraken/resources/texture/KRTextureCube.cpp b/kraken/resources/texture/KRTextureCube.cpp index 934f818..54c5c2f 100755 --- a/kraken/resources/texture/KRTextureCube.cpp +++ b/kraken/resources/texture/KRTextureCube.cpp @@ -93,6 +93,14 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim) return false; } + size_t bufferSizes[6] = {}; + void* buffers[6] = {}; + for (int i = 0; i < 6; i++) { + bufferSizes[i] = getMemRequiredForSize(lod_max_dim); + buffers[i] = malloc(bufferSizes[i]); + m_textures[i]->getLodData(buffers[i], lod_max_dim); + } + KRDeviceManager* deviceManager = getContext().getDeviceManager(); for (auto deviceItr = deviceManager->getDevices().begin(); deviceItr != deviceManager->getDevices().end(); deviceItr++) { @@ -117,7 +125,7 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim) std::string faceName = getName() + SUFFIXES[i]; if (m_textures[i]) { // TODO - Vulkan refactoring. We need to create a cube map texture rather than individual 2d textures. - m_textures[i]->uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim); + device.streamUpload(buffers[i], bufferSizes[i], Vector3i::Create(dimensions.x, dimensions.y, 1), texture.image); } } } @@ -128,6 +136,12 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim) m_new_lod_max_dim = prev_lod_max_dim; } + for (int i = 0; i < 6; i++) { + if (buffers[i]) { + delete buffers[i]; + } + } + return success; } diff --git a/kraken/resources/texture/KRTextureKTX.cpp b/kraken/resources/texture/KRTextureKTX.cpp index f3c5cf7..b696bd8 100755 --- a/kraken/resources/texture/KRTextureKTX.cpp +++ b/kraken/resources/texture/KRTextureKTX.cpp @@ -391,8 +391,9 @@ long KRTextureKTX::getMemRequiredForSize(int max_dim) return memoryRequired; } -bool KRTextureKTX::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha) +bool KRTextureKTX::getLodData(void* buffer, int lod_max_dim) { + unsigned char* converted_image = (unsigned char*)buffer; int target_dim = lod_max_dim; if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_lod_max_dim; @@ -414,12 +415,14 @@ bool KRTextureKTX::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d Block* block = *itr; if (width <= target_dim && height <= target_dim) { + /* if (width > current_lod_max_dim) { current_lod_max_dim = width; } if (height > current_lod_max_dim) { current_lod_max_dim = height; } + */ block->lock(); /* diff --git a/kraken/resources/texture/KRTextureKTX.h b/kraken/resources/texture/KRTextureKTX.h index 534c781..f615729 100755 --- a/kraken/resources/texture/KRTextureKTX.h +++ b/kraken/resources/texture/KRTextureKTX.h @@ -43,7 +43,7 @@ public: virtual ~KRTextureKTX(); virtual std::string getExtension() override; - bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) override; + bool getLodData(void* buffer, int lod_max_dim) override; virtual long getMemRequiredForSize(int max_dim) override; virtual hydra::Vector2i getDimensions() const override; diff --git a/kraken/resources/texture/KRTextureKTX2.cpp b/kraken/resources/texture/KRTextureKTX2.cpp index 7127f06..64124a6 100644 --- a/kraken/resources/texture/KRTextureKTX2.cpp +++ b/kraken/resources/texture/KRTextureKTX2.cpp @@ -112,8 +112,9 @@ long KRTextureKTX2::getMemRequiredForSize(int max_dim) return memoryRequired; } -bool KRTextureKTX2::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha) +bool KRTextureKTX2::getLodData(void* buffer, int lod_max_dim) { + unsigned char* converted_image = (unsigned char*)buffer; int target_dim = lod_max_dim; if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_lod_max_dim; @@ -134,13 +135,6 @@ bool KRTextureKTX2::uploadTexture(KRDevice& device, VkImage& image, int lod_max_ if (width <= target_dim && height <= target_dim) { - if (width > current_lod_max_dim) { - current_lod_max_dim = width; - } - if (height > current_lod_max_dim) { - current_lod_max_dim = height; - } - /* * TODO - Vulkan Refactoring GLDEBUG(glCompressedTexImage2D(target, destination_level, (unsigned int)m_header.glInternalFormat, width, height, 0, (int)block->getSize(), block->getStart())); diff --git a/kraken/resources/texture/KRTextureKTX2.h b/kraken/resources/texture/KRTextureKTX2.h index 2441e39..af60448 100644 --- a/kraken/resources/texture/KRTextureKTX2.h +++ b/kraken/resources/texture/KRTextureKTX2.h @@ -40,7 +40,7 @@ public: virtual ~KRTextureKTX2(); virtual std::string getExtension() override; - bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) override; + bool getLodData(void* buffer, int lod_max_dim) override; virtual long getMemRequiredForSize(int max_dim) override; virtual hydra::Vector2i getDimensions() const override; diff --git a/kraken/resources/texture/KRTexturePNG.cpp b/kraken/resources/texture/KRTexturePNG.cpp index 450efc5..f725b18 100644 --- a/kraken/resources/texture/KRTexturePNG.cpp +++ b/kraken/resources/texture/KRTexturePNG.cpp @@ -113,8 +113,9 @@ KRTexturePNG::~KRTexturePNG() } -bool KRTexturePNG::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha) +bool KRTexturePNG::getLodData(void* buffer, int lod_max_dim) { + unsigned char* converted_image = (unsigned char*)buffer; // TODO - Vulkan Refactoring - Perhaps it would be more efficient to reformat the color channels during the copy to the staging buffer. m_pData->lock(); PNG_HEADER* pHeader = (PNG_HEADER*)m_pData->getStart(); @@ -144,11 +145,6 @@ bool KRTexturePNG::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d } } - uint8_t* converted_image = (unsigned char*)malloc(m_imageSize); - Vector3i dimensions = { m_dimensions.x, m_dimensions.y, 1 }; - device.streamUpload((void*)converted_image, m_imageSize, dimensions, image); - current_lod_max_dim = m_max_lod_max_dim; - m_pData->unlock(); return true; } diff --git a/kraken/resources/texture/KRTexturePNG.h b/kraken/resources/texture/KRTexturePNG.h index ca3c663..1d04079 100644 --- a/kraken/resources/texture/KRTexturePNG.h +++ b/kraken/resources/texture/KRTexturePNG.h @@ -42,7 +42,7 @@ public: virtual ~KRTexturePNG(); virtual std::string getExtension() override; - bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) override; + bool getLodData(void* buffer, int lod_max_dim) override; #if !TARGET_OS_IPHONE && !defined(ANDROID) virtual KRTexture* compress(bool premultiply_alpha = false) override; diff --git a/kraken/resources/texture/KRTexturePVR.cpp b/kraken/resources/texture/KRTexturePVR.cpp index 78a0ccb..fdb86e5 100755 --- a/kraken/resources/texture/KRTexturePVR.cpp +++ b/kraken/resources/texture/KRTexturePVR.cpp @@ -201,7 +201,7 @@ long KRTexturePVR::getMemRequiredForSize(int max_dim) return memoryRequired; } -bool KRTexturePVR::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha) +bool KRTexturePVR::getLodData(void* buffer, int lod_max_dim) { int target_dim = lod_max_dim; if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_lod_max_dim; @@ -222,13 +222,14 @@ bool KRTexturePVR::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d for (std::list::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) { Block* block = *itr; if (width <= target_dim && height <= target_dim) { - + /* if (width > current_lod_max_dim) { current_lod_max_dim = width; } if (height > current_lod_max_dim) { current_lod_max_dim = height; } + */ block->lock(); /* diff --git a/kraken/resources/texture/KRTexturePVR.h b/kraken/resources/texture/KRTexturePVR.h index cdad4a2..a652b90 100755 --- a/kraken/resources/texture/KRTexturePVR.h +++ b/kraken/resources/texture/KRTexturePVR.h @@ -40,7 +40,7 @@ public: virtual ~KRTexturePVR(); virtual std::string getExtension() override; - bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) override; + bool getLodData(void* buffer, int lod_max_dim) override; virtual long getMemRequiredForSize(int max_dim) override; virtual hydra::Vector2i getDimensions() const override; diff --git a/kraken/resources/texture/KRTextureTGA.cpp b/kraken/resources/texture/KRTextureTGA.cpp index 16c690d..47be3f1 100755 --- a/kraken/resources/texture/KRTextureTGA.cpp +++ b/kraken/resources/texture/KRTextureTGA.cpp @@ -118,9 +118,9 @@ KRTextureTGA::~KRTextureTGA() } -bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha) +bool KRTextureTGA::getLodData(void* buffer, int lod_max_dim) { - // TODO - Vulkan Refactoring - Perhaps it would be more efficient to reformat the color channels during the copy to the staging buffer. + unsigned char* converted_image = (unsigned char*)buffer; m_pData->lock(); TGA_HEADER* pHeader = (TGA_HEADER*)m_pData->getStart(); @@ -138,7 +138,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d switch (pHeader->bitsperpixel) { case 24: { - unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4); + unsigned char* pSource = pData; unsigned char* pDest = converted_image; unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3; @@ -150,49 +150,22 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d pSource += 3; } assert(pSource <= m_pData->getEnd()); - device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); - free(converted_image); - - current_lod_max_dim = m_max_lod_max_dim; } break; case 32: { - if (premultiply_alpha) { - unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4); - unsigned char* pSource = pData; unsigned char* pDest = converted_image; unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3; while (pSource < pEnd) { - *pDest++ = (__uint32_t)pSource[2] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[1] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[0] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = pSource[3]; - pSource += 4; - } - assert(pSource <= m_pData->getEnd()); - device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); - free(converted_image); - } else { - unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4); - - unsigned char* pSource = pData; - unsigned char* pDest = converted_image; - unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3; - while (pSource < pEnd) { - *pDest++ = (__uint32_t)pSource[2]; - *pDest++ = (__uint32_t)pSource[1]; - *pDest++ = (__uint32_t)pSource[0]; - *pDest++ = pSource[3]; - pSource += 4; - } - assert(pSource <= m_pData->getEnd()); - device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); - free(converted_image); + *pDest++ = (__uint32_t)pSource[2]; + *pDest++ = (__uint32_t)pSource[1]; + *pDest++ = (__uint32_t)pSource[0]; + *pDest++ = pSource[3]; + pSource += 4; + + assert(pSource <= m_pData->getEnd()); } - - current_lod_max_dim = m_max_lod_max_dim; } break; default: @@ -204,73 +177,40 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d switch (pHeader->bitsperpixel) { case 32: { - unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4); unsigned char* pSource = pData; unsigned char* pDest = converted_image; unsigned char* pEnd = converted_image + pHeader->height * pHeader->width * 4; - if (premultiply_alpha) { - while (pDest < pEnd) { - int count = (*pSource & 0x7f) + 1; - if (*pSource & 0x80) { - // RLE Packet - pSource++; - while (count--) { - *pDest++ = (__uint32_t)pSource[2] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[1] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[0] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = pSource[3]; - } + + while (pDest < pEnd) { + int count = (*pSource & 0x7f) + 1; + if (*pSource & 0x80) { + // RLE Packet + pSource++; + while (count--) { + *pDest++ = pSource[2]; + *pDest++ = pSource[1]; + *pDest++ = pSource[0]; + *pDest++ = pSource[3]; + } + pSource += 4; + } else { + // RAW Packet + pSource++; + while (count--) { + *pDest++ = pSource[2]; + *pDest++ = pSource[1]; + *pDest++ = pSource[0]; + *pDest++ = pSource[3]; pSource += 4; - } else { - // RAW Packet - pSource++; - while (count--) { - *pDest++ = (__uint32_t)pSource[2] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[1] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = (__uint32_t)pSource[0] * (__uint32_t)pSource[3] / 0xff; - *pDest++ = pSource[3]; - pSource += 4; - } } } - assert(pSource <= m_pData->getEnd()); - assert(pDest == pEnd); - } else { - while (pDest < pEnd) { - int count = (*pSource & 0x7f) + 1; - if (*pSource & 0x80) { - // RLE Packet - pSource++; - while (count--) { - *pDest++ = pSource[2]; - *pDest++ = pSource[1]; - *pDest++ = pSource[0]; - *pDest++ = pSource[3]; - } - pSource += 4; - } else { - // RAW Packet - pSource++; - while (count--) { - *pDest++ = pSource[2]; - *pDest++ = pSource[1]; - *pDest++ = pSource[0]; - *pDest++ = pSource[3]; - pSource += 4; - } - } - } - assert(pSource <= m_pData->getEnd()); - assert(pDest == pEnd); } - device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); - free(converted_image); - current_lod_max_dim = m_max_lod_max_dim; + assert(pSource <= m_pData->getEnd()); + assert(pDest == pEnd); } break; case 24: { - unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4); unsigned char* pSource = pData; unsigned char* pDest = converted_image; unsigned char* pEnd = converted_image + pHeader->height * pHeader->width * 4; @@ -300,9 +240,6 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d } assert(pSource <= m_pData->getEnd()); assert(pDest == pEnd); - device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); - free(converted_image); - current_lod_max_dim = m_max_lod_max_dim; } break; default: diff --git a/kraken/resources/texture/KRTextureTGA.h b/kraken/resources/texture/KRTextureTGA.h index 9cea70b..0332f51 100755 --- a/kraken/resources/texture/KRTextureTGA.h +++ b/kraken/resources/texture/KRTextureTGA.h @@ -42,7 +42,7 @@ public: virtual ~KRTextureTGA(); virtual std::string getExtension() override; - bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool premultiply_alpha = false) override; + bool getLodData(void* buffer, int lod_max_dim) override; #if !TARGET_OS_IPHONE && !defined(ANDROID) virtual KRTexture* compress(bool premultiply_alpha = false) override;