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;
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<KRDevice>& 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;

View File

@@ -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 &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 Vector2i getDimensions() const = 0;

View File

@@ -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<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) {
m_haveNewHandles = true;
} else {
destroyHandles();
m_new_lod_max_dim = prev_lod_max_dim;
}
return success;

View File

@@ -158,7 +158,7 @@ long KRTextureKTX::getMemRequiredForSize(int max_dim)
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;
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 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;

View File

@@ -178,7 +178,7 @@ long KRTexturePVR::getMemRequiredForSize(int max_dim)
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;
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 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;

View File

@@ -114,24 +114,28 @@ 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();
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 &current
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 &current
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 &current
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 &current
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 &current
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 &current
}
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;
}

View File

@@ -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);