From 0995c507cda4f9dbd00838b9c73e0915c686735a Mon Sep 17 00:00:00 2001 From: Paolo Mazzon Date: Sun, 25 Aug 2024 11:15:58 -0700 Subject: [PATCH] Fixed a segfault that occurs when using a shader with a uniform buffer and the swapchain min image count is greater than 3 --- VK2D/Opaque.h | 1 + VK2D/Renderer.c | 14 ++++++++++---- VK2D/Shader.c | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/VK2D/Opaque.h b/VK2D/Opaque.h index 647a446..6c73407 100644 --- a/VK2D/Opaque.h +++ b/VK2D/Opaque.h @@ -143,6 +143,7 @@ struct VK2DShader_t { uint32_t uniformSize; ///< Uniform buffer size in bytes VK2DLogicalDevice dev; ///< Device this belongs to VK2DDescCon descCons[VK2D_MAX_FRAMES_IN_FLIGHT]; ///< Descriptor sets for the uniform buffers + int currentDescCon; ///< Points to the current desc con since for this frame since we have 1 per image in flight }; /// \brief Simple wrapper that groups image things together diff --git a/VK2D/Renderer.c b/VK2D/Renderer.c index 92d83a3..c31ee3e 100644 --- a/VK2D/Renderer.c +++ b/VK2D/Renderer.c @@ -399,9 +399,15 @@ void vk2dRendererStartFrame(const vec4 clearColour) { } // Reset shader desc cons - for (int i = 0; i < gRenderer->shaderListSize; i++) - if (gRenderer->customShaders[i] != NULL && gRenderer->customShaders[i]->uniformSize != 0) - vk2dDescConReset(gRenderer->customShaders[i]->descCons[gRenderer->scImageIndex]); + for (int i = 0; i < gRenderer->shaderListSize; i++) { + if (gRenderer->customShaders[i] != NULL && gRenderer->customShaders[i]->uniformSize != 0) { + gRenderer->customShaders[i]->currentDescCon += 1; + if (gRenderer->customShaders[i]->currentDescCon >= VK2D_MAX_FRAMES_IN_FLIGHT) { + gRenderer->customShaders[i]->currentDescCon = 0; + } + vk2dDescConReset(gRenderer->customShaders[i]->descCons[gRenderer->customShaders[i]->currentDescCon]); + } + } // Setup render pass VkRect2D rect = {0}; @@ -720,7 +726,7 @@ void vk2dRendererDrawShader(VK2DShader shader, void *data, VK2DTexture tex, floa // Create the data uniform uint32_t setCount = 3; if (shader->uniformSize != 0) { - sets[3] = vk2dDescConGetSet(shader->descCons[gRenderer->scImageIndex]); + sets[3] = vk2dDescConGetSet(shader->descCons[shader->currentDescCon]); VkBuffer buffer; VkDeviceSize offset; vk2dDescriptorBufferCopyData(gRenderer->descriptorBuffers[gRenderer->currentFrame], data, shader->uniformSize, &buffer, &offset); diff --git a/VK2D/Shader.c b/VK2D/Shader.c index 53af43b..c3be3c1 100644 --- a/VK2D/Shader.c +++ b/VK2D/Shader.c @@ -83,6 +83,7 @@ VK2DShader vk2dShaderFrom(uint8_t *vertexShaderBuffer, int vertexShaderBufferSiz out->spvFragSize = fragmentShaderBufferSize; out->uniformSize = uniformBufferSize; out->dev = dev; + out->currentDescCon = 0; if (uniformBufferSize != 0) { for (i = 0; i < VK2D_MAX_FRAMES_IN_FLIGHT && uniformBufferSize > 0; i++) { @@ -143,6 +144,7 @@ VK2DShader vk2dShaderLoad(const char *vertexShader, const char *fragmentShader, out->spvFragSize = fragFileSize; out->uniformSize = uniformBufferSize; out->dev = dev; + out->currentDescCon = 0; if (uniformBufferSize != 0) { for (i = 0; i < VK2D_MAX_FRAMES_IN_FLIGHT && uniformBufferSize > 0; i++) {