Refactored KRTextureCube::createGPUTexture to use Vulkan.

Removed KRTexture::m_iNewHandle, as all references have been removed with Vulkan refactoring.
This commit is contained in:
2022-07-16 00:56:16 -07:00
parent 83ea50384f
commit 90dd95ae76
3 changed files with 59 additions and 19 deletions

View File

@@ -40,7 +40,6 @@ KRTexture::KRTexture(KRContext &context, std::string name) : KRResource(context,
m_current_lod_max_dim = 0; m_current_lod_max_dim = 0;
m_new_lod_max_dim = 0; m_new_lod_max_dim = 0;
m_iHandle = 0; m_iHandle = 0;
m_iNewHandle = 0;
m_textureMemUsed = 0; m_textureMemUsed = 0;
m_newTextureMemUsed = 0; m_newTextureMemUsed = 0;
m_last_frame_used = 0; m_last_frame_used = 0;

View File

@@ -101,9 +101,8 @@ protected:
std::vector<TextureHandle> m_newHandles; std::vector<TextureHandle> m_newHandles;
std::atomic_bool m_haveNewHandles; std::atomic_bool m_haveNewHandles;
// TODO - Remove m_iHandle and m_iNewHandle once Vulkan refactoring complete // TODO - Remove m_iHandle once Vulkan refactoring complete
GLuint m_iHandle; GLuint m_iHandle;
GLuint m_iNewHandle;
std::atomic_flag m_handle_lock; std::atomic_flag m_handle_lock;
int m_current_lod_max_dim; int m_current_lod_max_dim;

View File

@@ -58,19 +58,56 @@ KRTextureCube::~KRTextureCube()
bool KRTextureCube::createGPUTexture(int lod_max_dim) bool KRTextureCube::createGPUTexture(int lod_max_dim)
{ {
assert(m_iNewHandle == m_iHandle); // Only allow one resize per frame assert(!m_haveNewHandles); // Only allow one resize per frame
bool success = true; bool success = true;
m_iNewHandle = 0; int prev_lod_max_dim = m_new_lod_max_dim;
GLDEBUG(glGenTextures(1, &m_iNewHandle));
assert(m_iNewHandle != 0);
m_new_lod_max_dim = 0; m_new_lod_max_dim = 0;
GLDEBUG(glBindTexture(GL_TEXTURE_CUBE_MAP, m_iNewHandle));
bool bMipMaps = false; bool bMipMaps = false;
Vector2i dimensions = Vector2i::Zero();
for (int i = 0; i < 6; i++) {
if (!m_textures[i]) {
success = false;
} else {
KRTexture2D& tex = *m_textures[i];
Vector2i texDimensions = tex.getDimensions();
if (dimensions.x == 0) {
dimensions = texDimensions;
} else if (dimensions != texDimensions) {
success = false;
}
if (tex.hasMipmaps()) {
bMipMaps = true;
}
}
}
if (!success) {
// Not all face images were loaded, or they have
// mismatched dimensions
// TODO - Perhaps we should have multiple error result codes.
return false;
}
KRDeviceManager* deviceManager = getContext().getDeviceManager();
for (auto deviceItr = deviceManager->getDevices().begin(); deviceItr != deviceManager->getDevices().end(); deviceItr++) {
KRDevice& device = *(*deviceItr).second;
KrDeviceHandle deviceHandle = (*deviceItr).first;
VmaAllocator allocator = device.getAllocator();
KRTexture::TextureHandle& texture = m_newHandles.emplace_back();
texture.device = deviceHandle;
texture.allocation = VK_NULL_HANDLE;
texture.image = VK_NULL_HANDLE;
if (!device.createImage(dimensions, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &texture.image, &texture.allocation)) {
success = false;
break;
}
}
for(int i=0; i<6; i++) { for(int i=0; i<6; i++) {
std::string faceName = getName() + SUFFIXES[i]; std::string faceName = getName() + SUFFIXES[i];
if(m_textures[i]) { if(m_textures[i]) {
@@ -78,16 +115,21 @@ bool KRTextureCube::createGPUTexture(int lod_max_dim)
m_textures[i]->uploadTexture(TARGETS[i], lod_max_dim, m_new_lod_max_dim); m_textures[i]->uploadTexture(TARGETS[i], lod_max_dim, m_new_lod_max_dim);
} }
} }
if(bMipMaps) { if (!success) {
GLDEBUG(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); for (TextureHandle t : m_newHandles) {
} else { std::unique_ptr<KRDevice>& device = deviceManager->getDevice(t.device);
GLDEBUG(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); VmaAllocator allocator = device->getAllocator();
// GLDEBUG(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)); vmaDestroyImage(allocator, t.image, t.allocation);
// GLDEBUG(glGenerateMipmap(GL_TEXTURE_CUBE_MAP)); }
m_newHandles.clear();
m_new_lod_max_dim = prev_lod_max_dim;
}
if (success) {
m_haveNewHandles = true;
} }
return success; return success;
} }