Refactoring KRTexture2D::uploadTexture, replacing with getLodData.
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, macos-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
2026-03-16 00:28:54 -07:00
parent 3914e917a1
commit 780d06883a
13 changed files with 79 additions and 124 deletions

View File

@@ -32,6 +32,7 @@
#include "KREngine-common.h" #include "KREngine-common.h"
#include "KRTexture2D.h" #include "KRTexture2D.h"
#include "KRTextureManager.h" #include "KRTextureManager.h"
using namespace hydra;
using namespace mimir; using namespace mimir;
@@ -51,6 +52,16 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim)
return true; 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; bool success = true;
int prev_lod_max_dim = m_new_lod_max_dim; int prev_lod_max_dim = m_new_lod_max_dim;
m_new_lod_max_dim = 0; m_new_lod_max_dim = 0;
@@ -91,12 +102,11 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim)
break; break;
} }
if (!uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim)) { device.streamUpload((void*)buffer, bufferSize, dimensions, texture.image);
success = false;
break;
}
} }
delete buffer;
if (success) { if (success) {
m_new_lod_max_dim = prev_lod_max_dim; m_new_lod_max_dim = prev_lod_max_dim;
m_haveNewHandles = true; m_haveNewHandles = true;

View File

@@ -48,7 +48,7 @@ public:
virtual bool save(const std::string& path) override; virtual bool save(const std::string& path) override;
virtual bool save(mimir::Block& data) 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; virtual hydra::Vector2i getDimensions() const = 0;
protected: protected:

View File

@@ -93,6 +93,14 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim)
return false; 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(); KRDeviceManager* deviceManager = getContext().getDeviceManager();
for (auto deviceItr = deviceManager->getDevices().begin(); deviceItr != deviceManager->getDevices().end(); deviceItr++) { 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]; std::string faceName = getName() + SUFFIXES[i];
if (m_textures[i]) { if (m_textures[i]) {
// TODO - Vulkan refactoring. We need to create a cube map texture rather than individual 2d textures. // 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; m_new_lod_max_dim = prev_lod_max_dim;
} }
for (int i = 0; i < 6; i++) {
if (buffers[i]) {
delete buffers[i];
}
}
return success; return success;
} }

View File

@@ -391,8 +391,9 @@ long KRTextureKTX::getMemRequiredForSize(int max_dim)
return memoryRequired; 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; int target_dim = lod_max_dim;
if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_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; Block* block = *itr;
if (width <= target_dim && height <= target_dim) { if (width <= target_dim && height <= target_dim) {
/*
if (width > current_lod_max_dim) { if (width > current_lod_max_dim) {
current_lod_max_dim = width; current_lod_max_dim = width;
} }
if (height > current_lod_max_dim) { if (height > current_lod_max_dim) {
current_lod_max_dim = height; current_lod_max_dim = height;
} }
*/
block->lock(); block->lock();
/* /*

View File

@@ -43,7 +43,7 @@ public:
virtual ~KRTextureKTX(); virtual ~KRTextureKTX();
virtual std::string getExtension() override; 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 long getMemRequiredForSize(int max_dim) override;
virtual hydra::Vector2i getDimensions() const override; virtual hydra::Vector2i getDimensions() const override;

View File

@@ -112,8 +112,9 @@ long KRTextureKTX2::getMemRequiredForSize(int max_dim)
return memoryRequired; 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; int target_dim = lod_max_dim;
if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_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 <= 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 * TODO - Vulkan Refactoring
GLDEBUG(glCompressedTexImage2D(target, destination_level, (unsigned int)m_header.glInternalFormat, width, height, 0, (int)block->getSize(), block->getStart())); GLDEBUG(glCompressedTexImage2D(target, destination_level, (unsigned int)m_header.glInternalFormat, width, height, 0, (int)block->getSize(), block->getStart()));

View File

@@ -40,7 +40,7 @@ public:
virtual ~KRTextureKTX2(); virtual ~KRTextureKTX2();
virtual std::string getExtension() override; 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 long getMemRequiredForSize(int max_dim) override;
virtual hydra::Vector2i getDimensions() const override; virtual hydra::Vector2i getDimensions() const override;

View File

@@ -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. // TODO - Vulkan Refactoring - Perhaps it would be more efficient to reformat the color channels during the copy to the staging buffer.
m_pData->lock(); m_pData->lock();
PNG_HEADER* pHeader = (PNG_HEADER*)m_pData->getStart(); 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; return true;
} }

View File

@@ -42,7 +42,7 @@ public:
virtual ~KRTexturePNG(); virtual ~KRTexturePNG();
virtual std::string getExtension() override; 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) #if !TARGET_OS_IPHONE && !defined(ANDROID)
virtual KRTexture* compress(bool premultiply_alpha = false) override; virtual KRTexture* compress(bool premultiply_alpha = false) override;

View File

@@ -201,7 +201,7 @@ long KRTexturePVR::getMemRequiredForSize(int max_dim)
return memoryRequired; 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; int target_dim = lod_max_dim;
if (target_dim < (int)m_min_lod_max_dim) target_dim = m_min_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<Block*>::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) { for (std::list<Block*>::iterator itr = m_blocks.begin(); itr != m_blocks.end(); itr++) {
Block* block = *itr; Block* block = *itr;
if (width <= target_dim && height <= target_dim) { if (width <= target_dim && height <= target_dim) {
/*
if (width > current_lod_max_dim) { if (width > current_lod_max_dim) {
current_lod_max_dim = width; current_lod_max_dim = width;
} }
if (height > current_lod_max_dim) { if (height > current_lod_max_dim) {
current_lod_max_dim = height; current_lod_max_dim = height;
} }
*/
block->lock(); block->lock();
/* /*

View File

@@ -40,7 +40,7 @@ public:
virtual ~KRTexturePVR(); virtual ~KRTexturePVR();
virtual std::string getExtension() override; 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 long getMemRequiredForSize(int max_dim) override;
virtual hydra::Vector2i getDimensions() const override; virtual hydra::Vector2i getDimensions() const override;

View File

@@ -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(); m_pData->lock();
TGA_HEADER* pHeader = (TGA_HEADER*)m_pData->getStart(); 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) { switch (pHeader->bitsperpixel) {
case 24: case 24:
{ {
unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4);
unsigned char* pSource = pData; unsigned char* pSource = pData;
unsigned char* pDest = converted_image; unsigned char* pDest = converted_image;
unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3; 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; pSource += 3;
} }
assert(pSource <= m_pData->getEnd()); 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; break;
case 32: case 32:
{ {
if (premultiply_alpha) {
unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4);
unsigned char* pSource = pData; unsigned char* pSource = pData;
unsigned char* pDest = converted_image; unsigned char* pDest = converted_image;
unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3; unsigned char* pEnd = pData + pHeader->height * pHeader->width * 3;
while (pSource < pEnd) { while (pSource < pEnd) {
*pDest++ = (__uint32_t)pSource[2] * (__uint32_t)pSource[3] / 0xff; *pDest++ = (__uint32_t)pSource[2];
*pDest++ = (__uint32_t)pSource[1] * (__uint32_t)pSource[3] / 0xff; *pDest++ = (__uint32_t)pSource[1];
*pDest++ = (__uint32_t)pSource[0] * (__uint32_t)pSource[3] / 0xff; *pDest++ = (__uint32_t)pSource[0];
*pDest++ = pSource[3]; *pDest++ = pSource[3];
pSource += 4; pSource += 4;
}
assert(pSource <= m_pData->getEnd()); 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);
} }
current_lod_max_dim = m_max_lod_max_dim;
} }
break; break;
default: default:
@@ -204,73 +177,40 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_d
switch (pHeader->bitsperpixel) { switch (pHeader->bitsperpixel) {
case 32: case 32:
{ {
unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4);
unsigned char* pSource = pData; unsigned char* pSource = pData;
unsigned char* pDest = converted_image; unsigned char* pDest = converted_image;
unsigned char* pEnd = converted_image + pHeader->height * pHeader->width * 4; unsigned char* pEnd = converted_image + pHeader->height * pHeader->width * 4;
if (premultiply_alpha) {
while (pDest < pEnd) { while (pDest < pEnd) {
int count = (*pSource & 0x7f) + 1; int count = (*pSource & 0x7f) + 1;
if (*pSource & 0x80) { if (*pSource & 0x80) {
// RLE Packet // RLE Packet
pSource++; pSource++;
while (count--) { while (count--) {
*pDest++ = (__uint32_t)pSource[2] * (__uint32_t)pSource[3] / 0xff; *pDest++ = pSource[2];
*pDest++ = (__uint32_t)pSource[1] * (__uint32_t)pSource[3] / 0xff; *pDest++ = pSource[1];
*pDest++ = (__uint32_t)pSource[0] * (__uint32_t)pSource[3] / 0xff; *pDest++ = pSource[0];
*pDest++ = pSource[3]; *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; 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); assert(pSource <= m_pData->getEnd());
free(converted_image); assert(pDest == pEnd);
current_lod_max_dim = m_max_lod_max_dim;
} }
break; break;
case 24: case 24:
{ {
unsigned char* converted_image = (unsigned char*)malloc(pHeader->width * pHeader->height * 4);
unsigned char* pSource = pData; unsigned char* pSource = pData;
unsigned char* pDest = converted_image; unsigned char* pDest = converted_image;
unsigned char* pEnd = converted_image + pHeader->height * pHeader->width * 4; 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(pSource <= m_pData->getEnd());
assert(pDest == pEnd); 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; break;
default: default:

View File

@@ -42,7 +42,7 @@ public:
virtual ~KRTextureTGA(); virtual ~KRTextureTGA();
virtual std::string getExtension() override; 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) #if !TARGET_OS_IPHONE && !defined(ANDROID)
virtual KRTexture* compress(bool premultiply_alpha = false) override; virtual KRTexture* compress(bool premultiply_alpha = false) override;