memory fixes
continuous-integration/drone/push Build is passing Details

pull/13/head
cosmonaut 2022-02-18 22:22:45 -08:00
parent 209b4ae6dd
commit bb948846c0
1 changed files with 65 additions and 49 deletions

View File

@ -1447,6 +1447,7 @@ typedef struct VulkanRenderer
/* Forward declarations */ /* Forward declarations */
static void VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer); static void VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer);
static void VULKAN_Wait(Refresh_Renderer *driverData);
static void VULKAN_Submit(Refresh_Renderer *driverData, uint32_t commandBufferCount, Refresh_CommandBuffer **pCommandBuffers); static void VULKAN_Submit(Refresh_Renderer *driverData, uint32_t commandBufferCount, Refresh_CommandBuffer **pCommandBuffers);
/* Error Handling */ /* Error Handling */
@ -3617,6 +3618,7 @@ static void VULKAN_INTERNAL_DeactivateUnusedImageDescriptorSets(
} }
} }
/* FIXME: get rid of descriptor set caching */
static void VULKAN_INTERNAL_ResetDescriptorSetData(VulkanRenderer* renderer) static void VULKAN_INTERNAL_ResetDescriptorSetData(VulkanRenderer* renderer)
{ {
uint32_t i, j; uint32_t i, j;
@ -3665,18 +3667,13 @@ static void VULKAN_INTERNAL_ResetDescriptorSetData(VulkanRenderer* renderer)
} }
} }
static void VULKAN_INTERNAL_PostWorkCleanup(VulkanRenderer* renderer)
{
/* Reset descriptor set data */
VULKAN_INTERNAL_ResetDescriptorSetData(renderer);
}
/* Swapchain */ /* Swapchain */
static uint8_t VULKAN_INTERNAL_QuerySwapChainSupport( static uint8_t VULKAN_INTERNAL_QuerySwapChainSupport(
VulkanRenderer *renderer, VulkanRenderer *renderer,
VkPhysicalDevice physicalDevice, VkPhysicalDevice physicalDevice,
VkSurfaceKHR surface, VkSurfaceKHR surface,
uint32_t graphicsFamilyIndex,
SwapChainSupportDetails *outputDetails SwapChainSupportDetails *outputDetails
) { ) {
VkResult result; VkResult result;
@ -3684,9 +3681,11 @@ static uint8_t VULKAN_INTERNAL_QuerySwapChainSupport(
uint32_t presentModeCount; uint32_t presentModeCount;
VkBool32 supportsPresent; VkBool32 supportsPresent;
if (graphicsFamilyIndex != UINT32_MAX)
{
renderer->vkGetPhysicalDeviceSurfaceSupportKHR( renderer->vkGetPhysicalDeviceSurfaceSupportKHR(
physicalDevice, physicalDevice,
renderer->queueFamilyIndices.graphicsFamily, graphicsFamilyIndex,
surface, surface,
&supportsPresent &supportsPresent
); );
@ -3696,6 +3695,7 @@ static uint8_t VULKAN_INTERNAL_QuerySwapChainSupport(
Refresh_LogWarn("This surface does not support presenting!"); Refresh_LogWarn("This surface does not support presenting!");
return 0; return 0;
} }
}
result = renderer->vkGetPhysicalDeviceSurfaceCapabilitiesKHR( result = renderer->vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
physicalDevice, physicalDevice,
@ -3899,6 +3899,7 @@ static CreateSwapchainResult VULKAN_INTERNAL_CreateSwapchain(
renderer, renderer,
renderer->physicalDevice, renderer->physicalDevice,
swapchainData->surface, swapchainData->surface,
renderer->queueFamilyIndices.graphicsFamily,
&swapchainSupportDetails &swapchainSupportDetails
)) { )) {
renderer->vkDestroySurfaceKHR( renderer->vkDestroySurfaceKHR(
@ -4335,23 +4336,13 @@ static void VULKAN_DestroyDevice(
Refresh_Device *device Refresh_Device *device
) { ) {
VulkanRenderer* renderer = (VulkanRenderer*) device->driverData; VulkanRenderer* renderer = (VulkanRenderer*) device->driverData;
VkResult waitResult;
CommandPoolHashArray commandPoolHashArray; CommandPoolHashArray commandPoolHashArray;
GraphicsPipelineLayoutHashArray graphicsPipelineLayoutHashArray; GraphicsPipelineLayoutHashArray graphicsPipelineLayoutHashArray;
ComputePipelineLayoutHashArray computePipelineLayoutHashArray; ComputePipelineLayoutHashArray computePipelineLayoutHashArray;
VulkanMemorySubAllocator *allocator; VulkanMemorySubAllocator *allocator;
uint32_t i, j, k; uint32_t i, j, k;
waitResult = renderer->vkDeviceWaitIdle(renderer->logicalDevice); VULKAN_Wait(device->driverData);
if (waitResult != VK_SUCCESS)
{
LogVulkanResultAsError("vkDeviceWaitIdle", waitResult);
}
/* We have to do this twice so the rotation happens correctly */
VULKAN_INTERNAL_PostWorkCleanup(renderer);
VULKAN_INTERNAL_PostWorkCleanup(renderer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer->vulkanBuffer); VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyVertexUniformBuffer->vulkanBuffer);
VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyFragmentUniformBuffer->vulkanBuffer); VULKAN_INTERNAL_DestroyBuffer(renderer, renderer->dummyFragmentUniformBuffer->vulkanBuffer);
@ -8617,6 +8608,29 @@ static void VULKAN_INTERNAL_CleanCommandBuffer(
SDL_UnlockMutex(renderer->acquireFenceLock); SDL_UnlockMutex(renderer->acquireFenceLock);
commandBuffer->inFlightFence = VK_NULL_HANDLE; commandBuffer->inFlightFence = VK_NULL_HANDLE;
/* Return this command buffer to its pool */
for (i = 0; i < renderer->submittedCommandBufferCount; i += 1)
{
if (renderer->submittedCommandBuffers[i] == commandBuffer)
{
renderer->submittedCommandBuffers[i] = renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount - 1];
renderer->submittedCommandBufferCount -= 1;
if (commandBuffer->commandPool->inactiveCommandBufferCount == commandBuffer->commandPool->inactiveCommandBufferCapacity)
{
commandBuffer->commandPool->inactiveCommandBufferCapacity += 1;
commandBuffer->commandPool->inactiveCommandBuffers = SDL_realloc(
commandBuffer->commandPool->inactiveCommandBuffers,
commandBuffer->commandPool->inactiveCommandBufferCapacity * sizeof(VulkanCommandBuffer*)
);
}
commandBuffer->commandPool->inactiveCommandBuffers[commandBuffer->commandPool->inactiveCommandBufferCount] = commandBuffer;
commandBuffer->commandPool->inactiveCommandBufferCount += 1;
}
}
} }
static void VULKAN_Wait( static void VULKAN_Wait(
@ -8669,27 +8683,6 @@ static void VULKAN_Submit(
SDL_LockMutex(renderer->submitLock); SDL_LockMutex(renderer->submitLock);
/* Check if we can perform any cleanups */
for (i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1)
{
/* If we set a timeout of 0, we can query the command buffer state */
vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->submittedCommandBuffers[i]->inFlightFence,
VK_TRUE,
0
);
if (vulkanResult == VK_SUCCESS)
{
VULKAN_INTERNAL_CleanCommandBuffer(
renderer,
renderer->submittedCommandBuffers[i]
);
}
}
commandBuffers = SDL_stack_alloc(VkCommandBuffer, commandBufferCount); commandBuffers = SDL_stack_alloc(VkCommandBuffer, commandBufferCount);
submitInfos = SDL_stack_alloc(VkSubmitInfo, commandBufferCount); submitInfos = SDL_stack_alloc(VkSubmitInfo, commandBufferCount);
@ -8744,6 +8737,7 @@ static void VULKAN_Submit(
} }
} }
/* Wait for any previous submissions on swapchains */
for (i = 0; i < commandBufferCount; i += 1) for (i = 0; i < commandBufferCount; i += 1)
{ {
currentCommandBuffer = (VulkanCommandBuffer*)pCommandBuffers[i]; currentCommandBuffer = (VulkanCommandBuffer*)pCommandBuffers[i];
@ -8754,7 +8748,7 @@ static void VULKAN_Submit(
if (swapchainData->inFlightFence != VK_NULL_HANDLE) if (swapchainData->inFlightFence != VK_NULL_HANDLE)
{ {
renderer->vkWaitForFences( vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice, renderer->logicalDevice,
1, 1,
&swapchainData->inFlightFence, &swapchainData->inFlightFence,
@ -8774,6 +8768,27 @@ static void VULKAN_Submit(
} }
} }
/* Check if we can perform any cleanups */
for (i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1)
{
/* If we set a timeout of 0, we can query the command buffer state */
vulkanResult = renderer->vkWaitForFences(
renderer->logicalDevice,
1,
&renderer->submittedCommandBuffers[i]->inFlightFence,
VK_TRUE,
0
);
if (vulkanResult == VK_SUCCESS)
{
VULKAN_INTERNAL_CleanCommandBuffer(
renderer,
renderer->submittedCommandBuffers[i]
);
}
}
/* Acquire a fence */ /* Acquire a fence */
fence = VULKAN_INTERNAL_AcquireFence(renderer); fence = VULKAN_INTERNAL_AcquireFence(renderer);
@ -9169,6 +9184,7 @@ static uint8_t VULKAN_INTERNAL_IsDeviceSuitable(
renderer, renderer,
physicalDevice, physicalDevice,
surface, surface,
UINT32_MAX,
&swapChainSupportDetails &swapChainSupportDetails
); );