From 4a13f3c832c7ed9e96e16be0dc30f06bafb66ae7 Mon Sep 17 00:00:00 2001 From: kearwood Date: Tue, 19 Jul 2022 00:13:42 -0700 Subject: [PATCH] Added VkImage parameter to KRTexture::uploadTexture and all overrides. Converted KRTextureTGA from OpenGL to Vulkan --- kraken/KRTexture2D.cpp | 20 +++++------------ kraken/KRTexture2D.h | 4 +++- kraken/KRTextureCube.cpp | 16 ++++---------- kraken/KRTextureKTX.cpp | 2 +- kraken/KRTextureKTX.h | 2 +- kraken/KRTexturePVR.cpp | 2 +- kraken/KRTexturePVR.h | 2 +- kraken/KRTextureTGA.cpp | 48 +++++++++++----------------------------- kraken/KRTextureTGA.h | 2 +- 9 files changed, 31 insertions(+), 67 deletions(-) diff --git a/kraken/KRTexture2D.cpp b/kraken/KRTexture2D.cpp index 7b8ed64..eb70173 100755 --- a/kraken/KRTexture2D.cpp +++ b/kraken/KRTexture2D.cpp @@ -65,26 +65,18 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim) { success = false; break; } - } - if (success) { - if (!uploadTexture(lod_max_dim, m_new_lod_max_dim)) { - m_new_lod_max_dim = prev_lod_max_dim; + if (!uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim)) { success = false; + break; } } - if (!success) { - for (TextureHandle t : m_newHandles) { - std::unique_ptr& device = deviceManager->getDevice(t.device); - VmaAllocator allocator = device->getAllocator(); - vmaDestroyImage(allocator, t.image, t.allocation); - } - m_newHandles.clear(); - } - if (success) { - m_haveNewHandles = true; + m_new_lod_max_dim = prev_lod_max_dim; + m_haveNewHandles = true; + } else { + destroyNewHandles(); } return success; diff --git a/kraken/KRTexture2D.h b/kraken/KRTexture2D.h index 58e38bf..a57aec5 100755 --- a/kraken/KRTexture2D.h +++ b/kraken/KRTexture2D.h @@ -38,6 +38,8 @@ using std::list; #include "KRTexture.h" +class KRDevice; + class KRTexture2D : public KRTexture { public: KRTexture2D(KRContext &context, KRDataBlock *data, std::string name); @@ -45,7 +47,7 @@ public: virtual bool save(const std::string& path); virtual bool save(KRDataBlock &data); - virtual bool uploadTexture(int lod_max_dim, int ¤t_lod_max_dim, bool compress = false, bool premultiply_alpha = false) = 0; + virtual bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int ¤t_lod_max_dim, bool compress = false, bool premultiply_alpha = false) = 0; virtual void bind(GLuint texture_unit); virtual Vector2i getDimensions() const = 0; diff --git a/kraken/KRTextureCube.cpp b/kraken/KRTextureCube.cpp index f732f8b..48673bf 100755 --- a/kraken/KRTextureCube.cpp +++ b/kraken/KRTextureCube.cpp @@ -113,23 +113,15 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim) /* TODO - Vulkan refactoring... incorporate TARGETS[i], */ - m_textures[i]->uploadTexture(device, lod_max_dim, m_new_lod_max_dim); + m_textures[i]->uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim); } } } - - if (!success) { - for (TextureHandle t : m_newHandles) { - std::unique_ptr& device = deviceManager->getDevice(t.device); - VmaAllocator allocator = device->getAllocator(); - vmaDestroyImage(allocator, t.image, t.allocation); - } - m_newHandles.clear(); - m_new_lod_max_dim = prev_lod_max_dim; - } - if (success) { m_haveNewHandles = true; + } else { + destroyHandles(); + m_new_lod_max_dim = prev_lod_max_dim; } return success; diff --git a/kraken/KRTextureKTX.cpp b/kraken/KRTextureKTX.cpp index 9ab3e93..e490d93 100755 --- a/kraken/KRTextureKTX.cpp +++ b/kraken/KRTextureKTX.cpp @@ -158,7 +158,7 @@ long KRTextureKTX::getMemRequiredForSize(int max_dim) return memoryRequired; } -bool KRTextureKTX::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) +bool KRTextureKTX::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) { int target_dim = lod_max_dim; if(target_dim < (int)m_min_lod_max_dim) target_dim = m_min_lod_max_dim; diff --git a/kraken/KRTextureKTX.h b/kraken/KRTextureKTX.h index cf56b54..c7dd6a8 100755 --- a/kraken/KRTextureKTX.h +++ b/kraken/KRTextureKTX.h @@ -41,7 +41,7 @@ public: virtual ~KRTextureKTX(); virtual std::string getExtension(); - bool uploadTexture(KRDevice& device, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; + bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; virtual long getMemRequiredForSize(int max_dim); virtual Vector2i getDimensions() const override; diff --git a/kraken/KRTexturePVR.cpp b/kraken/KRTexturePVR.cpp index fec71b5..c477305 100755 --- a/kraken/KRTexturePVR.cpp +++ b/kraken/KRTexturePVR.cpp @@ -178,7 +178,7 @@ long KRTexturePVR::getMemRequiredForSize(int max_dim) return memoryRequired; } -bool KRTexturePVR::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) +bool KRTexturePVR::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) { int target_dim = lod_max_dim; if(target_dim < (int)m_min_lod_max_dim) target_dim = m_min_lod_max_dim; diff --git a/kraken/KRTexturePVR.h b/kraken/KRTexturePVR.h index fde3858..f781781 100755 --- a/kraken/KRTexturePVR.h +++ b/kraken/KRTexturePVR.h @@ -40,7 +40,7 @@ public: virtual ~KRTexturePVR(); virtual std::string getExtension(); - bool uploadTexture(KRDevice& device, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; + bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; virtual long getMemRequiredForSize(int max_dim); virtual Vector2i getDimensions() const override; diff --git a/kraken/KRTextureTGA.cpp b/kraken/KRTextureTGA.cpp index a2b7c8f..fd91dd1 100755 --- a/kraken/KRTextureTGA.cpp +++ b/kraken/KRTextureTGA.cpp @@ -114,24 +114,28 @@ KRTextureTGA::~KRTextureTGA() } -bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) +bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int ¤t_lod_max_dim, bool compress, bool premultiply_alpha) { + // TODO - Vulkan Refactoring - Perhaps it would be more efficient to reformat the color channels during the copy to the staging buffer. + m_pData->lock(); TGA_HEADER *pHeader = (TGA_HEADER *)m_pData->getStart(); unsigned char *pData = (unsigned char *)pHeader + (long)pHeader->idlength + (long)pHeader->colourmaplength * (long)pHeader->colourmaptype + sizeof(TGA_HEADER); + /* + * TODO - Vulkan refactoring to support compressing textures on load GLenum internal_format = GL_RGBA; - -#if !TARGET_OS_IPHONE && !defined(ANDROID) if(compress) { internal_format = pHeader->bitsperpixel == 24 ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; } -#endif + */ if(pHeader->colourmaptype != 0) { m_pData->unlock(); return false; // Mapped colors not supported } + + Vector2i dimensions = { pHeader->width, pHeader->height }; switch(pHeader->imagetype) { case 2: // rgb @@ -139,11 +143,6 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t case 24: { unsigned char *converted_image = (unsigned char *)malloc(pHeader->width * pHeader->height * 4); -//#ifdef __APPLE__ -// vImage_Buffer source_image = { pData, pHeader->height, pHeader->width, pHeader->width*3 }; -// vImage_Buffer dest_image = { converted_image, pHeader->height, pHeader->width, pHeader->width*4 }; -// vImageConvert_RGB888toRGBA8888(&source_image, NULL, 0xff, &dest_image, false, kvImageDoNotTile); -//#else unsigned char *pSource = pData; unsigned char *pDest = converted_image; unsigned char *pEnd = pData + pHeader->height * pHeader->width * 3; @@ -155,12 +154,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t pSource += 3; } assert(pSource <= m_pData->getEnd()); -//#endif - /* - * TODO - Vulkan Refactoring - GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image)); - GLDEBUG(glFinish()); - */ + device.streamUpload((void *)converted_image, pDest - converted_image, dimensions, image); free(converted_image); current_lod_max_dim = m_max_lod_max_dim; @@ -182,11 +176,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t pSource += 4; } assert(pSource <= m_pData->getEnd()); - /* - * TODO - Vulkan Refactoring - GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image)); - GLDEBUG(glFinish()); - */ + 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); @@ -202,11 +192,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t pSource += 4; } assert(pSource <= m_pData->getEnd()); - /* - * TODO - Vulkan Refactoring - GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)pData)); - GLDEBUG(glFinish()); - */ + device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); free(converted_image); } @@ -281,11 +267,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t assert(pSource <= m_pData->getEnd()); assert(pDest == pEnd); } - /* - * TODO - Vulkan Refactoring - GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image)); - GLDEBUG(glFinish()); - */ + device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); free(converted_image); current_lod_max_dim = m_max_lod_max_dim; } @@ -322,11 +304,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int ¤t } assert(pSource <= m_pData->getEnd()); assert(pDest == pEnd); - /* - * TODO - Vulkan Refactoring - GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image)); - GLDEBUG(glFinish()); - */ + device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image); free(converted_image); current_lod_max_dim = m_max_lod_max_dim; } diff --git a/kraken/KRTextureTGA.h b/kraken/KRTextureTGA.h index 10ce468..d692984 100755 --- a/kraken/KRTextureTGA.h +++ b/kraken/KRTextureTGA.h @@ -40,7 +40,7 @@ public: virtual ~KRTextureTGA(); virtual std::string getExtension(); - bool uploadTexture(KRDevice& device, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; + bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int& current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) override; #if !TARGET_OS_IPHONE && !defined(ANDROID) virtual KRTexture *compress(bool premultiply_alpha = false);