Skip to content

Commit

Permalink
Fixed a segfault that occurs when using a shader with a uniform buffe…
Browse files Browse the repository at this point in the history
…r and the swapchain min image count is greater than 3
  • Loading branch information
PaoloMazzon committed Aug 25, 2024
1 parent 61819f2 commit 0995c50
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions VK2D/Opaque.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions VK2D/Renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions VK2D/Shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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++) {
Expand Down

0 comments on commit 0995c50

Please sign in to comment.