Added VkImage parameter to KRTexture::uploadTexture and all overrides.

Converted KRTextureTGA from OpenGL to Vulkan
This commit is contained in:
2022-07-19 00:13:42 -07:00
parent 2a75ee68a0
commit 4a13f3c832
9 changed files with 31 additions and 67 deletions

View File

@@ -65,26 +65,18 @@ bool KRTexture2D::createGPUTexture(int lod_max_dim) {
success = false; success = false;
break; break;
} }
}
if (success) { if (!uploadTexture(device, texture.image, lod_max_dim, m_new_lod_max_dim)) {
if (!uploadTexture(lod_max_dim, m_new_lod_max_dim)) {
m_new_lod_max_dim = prev_lod_max_dim;
success = false; success = false;
break;
} }
} }
if (!success) {
for (TextureHandle t : m_newHandles) {
std::unique_ptr<KRDevice>& device = deviceManager->getDevice(t.device);
VmaAllocator allocator = device->getAllocator();
vmaDestroyImage(allocator, t.image, t.allocation);
}
m_newHandles.clear();
}
if (success) { if (success) {
m_new_lod_max_dim = prev_lod_max_dim;
m_haveNewHandles = true; m_haveNewHandles = true;
} else {
destroyNewHandles();
} }
return success; return success;

View File

@@ -38,6 +38,8 @@ using std::list;
#include "KRTexture.h" #include "KRTexture.h"
class KRDevice;
class KRTexture2D : public KRTexture { class KRTexture2D : public KRTexture {
public: public:
KRTexture2D(KRContext &context, KRDataBlock *data, std::string name); KRTexture2D(KRContext &context, KRDataBlock *data, std::string name);
@@ -45,7 +47,7 @@ public:
virtual bool save(const std::string& path); virtual bool save(const std::string& path);
virtual bool save(KRDataBlock &data); virtual bool save(KRDataBlock &data);
virtual bool uploadTexture(int lod_max_dim, int &current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) = 0; virtual bool uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int &current_lod_max_dim, bool compress = false, bool premultiply_alpha = false) = 0;
virtual void bind(GLuint texture_unit); virtual void bind(GLuint texture_unit);
virtual Vector2i getDimensions() const = 0; virtual Vector2i getDimensions() const = 0;

View File

@@ -113,23 +113,15 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim)
/* TODO - Vulkan refactoring... /* TODO - Vulkan refactoring...
incorporate TARGETS[i], 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<KRDevice>& 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) { if (success) {
m_haveNewHandles = true; m_haveNewHandles = true;
} else {
destroyHandles();
m_new_lod_max_dim = prev_lod_max_dim;
} }
return success; return success;

View File

@@ -158,7 +158,7 @@ long KRTextureKTX::getMemRequiredForSize(int max_dim)
return memoryRequired; return memoryRequired;
} }
bool KRTextureKTX::uploadTexture(KRDevice& device, int lod_max_dim, int &current_lod_max_dim, bool compress, bool premultiply_alpha) bool KRTextureKTX::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int &current_lod_max_dim, bool compress, bool premultiply_alpha)
{ {
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;

View File

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

View File

@@ -178,7 +178,7 @@ long KRTexturePVR::getMemRequiredForSize(int max_dim)
return memoryRequired; return memoryRequired;
} }
bool KRTexturePVR::uploadTexture(KRDevice& device, int lod_max_dim, int &current_lod_max_dim, bool compress, bool premultiply_alpha) bool KRTexturePVR::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int &current_lod_max_dim, bool compress, bool premultiply_alpha)
{ {
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;

View File

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

View File

@@ -114,36 +114,35 @@ KRTextureTGA::~KRTextureTGA()
} }
bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int &current_lod_max_dim, bool compress, bool premultiply_alpha) bool KRTextureTGA::uploadTexture(KRDevice& device, VkImage& image, int lod_max_dim, int &current_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(); m_pData->lock();
TGA_HEADER *pHeader = (TGA_HEADER *)m_pData->getStart(); 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); 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; GLenum internal_format = GL_RGBA;
#if !TARGET_OS_IPHONE && !defined(ANDROID)
if(compress) { if(compress) {
internal_format = pHeader->bitsperpixel == 24 ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; internal_format = pHeader->bitsperpixel == 24 ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
} }
#endif */
if(pHeader->colourmaptype != 0) { if(pHeader->colourmaptype != 0) {
m_pData->unlock(); m_pData->unlock();
return false; // Mapped colors not supported return false; // Mapped colors not supported
} }
Vector2i dimensions = { pHeader->width, pHeader->height };
switch(pHeader->imagetype) { switch(pHeader->imagetype) {
case 2: // rgb case 2: // rgb
switch(pHeader->bitsperpixel) { switch(pHeader->bitsperpixel) {
case 24: case 24:
{ {
unsigned char *converted_image = (unsigned char *)malloc(pHeader->width * pHeader->height * 4); 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 *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;
@@ -155,12 +154,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int &current
pSource += 3; pSource += 3;
} }
assert(pSource <= m_pData->getEnd()); assert(pSource <= m_pData->getEnd());
//#endif device.streamUpload((void *)converted_image, pDest - converted_image, dimensions, image);
/*
* TODO - Vulkan Refactoring
GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image));
GLDEBUG(glFinish());
*/
free(converted_image); free(converted_image);
current_lod_max_dim = m_max_lod_max_dim; current_lod_max_dim = m_max_lod_max_dim;
@@ -182,11 +176,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int &current
pSource += 4; pSource += 4;
} }
assert(pSource <= m_pData->getEnd()); assert(pSource <= m_pData->getEnd());
/* device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image);
* TODO - Vulkan Refactoring
GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image));
GLDEBUG(glFinish());
*/
free(converted_image); free(converted_image);
} else { } else {
unsigned char *converted_image = (unsigned char *)malloc(pHeader->width * pHeader->height * 4); 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 &current
pSource += 4; pSource += 4;
} }
assert(pSource <= m_pData->getEnd()); assert(pSource <= m_pData->getEnd());
/* device.streamUpload((void*)converted_image, pDest - converted_image, dimensions, image);
* TODO - Vulkan Refactoring
GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)pData));
GLDEBUG(glFinish());
*/
free(converted_image); free(converted_image);
} }
@@ -281,11 +267,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int &current
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);
* TODO - Vulkan Refactoring
GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image));
GLDEBUG(glFinish());
*/
free(converted_image); free(converted_image);
current_lod_max_dim = m_max_lod_max_dim; current_lod_max_dim = m_max_lod_max_dim;
} }
@@ -322,11 +304,7 @@ bool KRTextureTGA::uploadTexture(KRDevice& device, int lod_max_dim, int &current
} }
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);
* TODO - Vulkan Refactoring
GLDEBUG(glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image));
GLDEBUG(glFinish());
*/
free(converted_image); free(converted_image);
current_lod_max_dim = m_max_lod_max_dim; current_lod_max_dim = m_max_lod_max_dim;
} }

View File

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