From a74dd637868e534f6c49d35cdfb73fc97fed8e37 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Wed, 2 Jul 2025 09:12:33 -0400 Subject: [PATCH] reuse command buffers flag --- .../apis/metal/loader/metal_command_loader.m | 1 + .../command_buffer/metal_command_buffer.h | 4 ++++ .../command_buffer/metal_command_buffer.m | 20 ++++++++++++++----- .../src/framebuffers/metal_framebuffer.m | 5 ++--- .../vulkan/loader/vulkan_command_loader.c | 1 + .../command_buffer/vulkan_command_buffer.c | 4 ++++ .../command_buffer/vulkan_command_buffer.h | 1 + .../command_buffer/gryphn_command_buffer.c | 4 ++++ .../command_buffer/gryphn_command_buffer.h | 1 + .../command_pool/gryphn_command_pool.c | 1 + .../command_pool/gryphn_command_pool.h | 7 +++++++ .../loader/src/gryphn_command_functions.h | 1 + .../function_loader/CMakeLists.txt | 1 + .../function_loader/loader/function_loader.c | 7 ++++--- .../{loader => src}/command_functions.c | 3 +++ .../{loader => src}/command_functions.h | 1 + .../{loader => src}/device_functions.c | 0 .../{loader => src}/device_functions.h | 0 .../{loader => src}/instance_functions.c | 0 .../{loader => src}/instance_functions.h | 0 .../{loader => src}/loader_utils.h | 0 21 files changed, 51 insertions(+), 11 deletions(-) rename projects/validation_layers/function_loader/{loader => src}/command_functions.c (95%) rename projects/validation_layers/function_loader/{loader => src}/command_functions.h (95%) rename projects/validation_layers/function_loader/{loader => src}/device_functions.c (100%) rename projects/validation_layers/function_loader/{loader => src}/device_functions.h (100%) rename projects/validation_layers/function_loader/{loader => src}/instance_functions.c (100%) rename projects/validation_layers/function_loader/{loader => src}/instance_functions.h (100%) rename projects/validation_layers/function_loader/{loader => src}/loader_utils.h (100%) diff --git a/projects/apis/metal/loader/metal_command_loader.m b/projects/apis/metal/loader/metal_command_loader.m index 2ab3df4..eca1735 100644 --- a/projects/apis/metal/loader/metal_command_loader.m +++ b/projects/apis/metal/loader/metal_command_loader.m @@ -8,6 +8,7 @@ gnCommandFunctions loadMetalCommandFunctions() { ._gnBeginCommandBuffer = beginMetalCommandBuffer, ._gnResetCommandBuffer = resetMetalCommandBuffer, ._gnEndCommandBuffer = endMetalCommandBuffer, + ._gnDestroyCommandBuffer = destroyMetalCommandBuffer, ._gnCommandBeginRenderPass = metelBeginRenderPass, ._gnCommandEndRenderPass = endMetalRenderPass, diff --git a/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.h b/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.h index 5a1747a..e35ed88 100644 --- a/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.h +++ b/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.h @@ -5,7 +5,9 @@ #import typedef struct gnPlatformCommandBuffer_t { + gnBool isIndirectCommandBuffer; id commandBuffer; + id encoder; gnGraphicsPipeline boundGraphcisPipeline; gnBufferHandle indexBuffer; @@ -13,5 +15,7 @@ typedef struct gnPlatformCommandBuffer_t { gnReturnCode allocateMetalCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPool pool); void resetMetalCommandBuffer(gnCommandBuffer commandBuffer); +void resetMetalCommandBuffer_validated(gnCommandBuffer commandBuffer); gnReturnCode beginMetalCommandBuffer(gnCommandBuffer commandBuffer); gnReturnCode endMetalCommandBuffer(gnCommandBuffer commandBuffer); +void destroyMetalCommandBuffer(gnCommandBuffer commandBuffer); diff --git a/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.m b/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.m index eaf0d03..4fcf198 100644 --- a/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.m +++ b/projects/apis/metal/src/commands/command_buffer/metal_command_buffer.m @@ -1,15 +1,24 @@ #include "metal_command_buffer.h" #include "commands/command_pool/metal_command_pool.h" +#include "debugger/gryphn_debugger.h" +#include "instance/gryphn_instance.h" #import gnReturnCode allocateMetalCommandBuffers(gnCommandBufferHandle* commandBuffers, uint32_t count, gnCommandPool pool) { for (int i = 0; i < count; i++) { - commandBuffers[i]->commandBuffer = malloc(sizeof(gnPlatformCommandBuffer)); } + commandBuffers[i]->commandBuffer = malloc(sizeof(gnPlatformCommandBuffer)); + commandBuffers[i]->commandBuffer->commandBuffer = [pool->commandPool->commandQueue commandBuffer]; + + // write a command log at some point + if ((pool->info.flags & GN_REUSE_COMMAND_BUFFERS) == GN_REUSE_COMMAND_BUFFERS) + commandBuffers[i]->commandBuffer->isIndirectCommandBuffer = gnTrue; + } return GN_SUCCESS; } void resetMetalCommandBuffer(gnCommandBuffer commandBuffer) { - commandBuffer->commandBuffer->commandBuffer = [commandBuffer->commandPool->commandPool->commandQueue commandBuffer]; + if (commandBuffer->commandBuffer->isIndirectCommandBuffer) + commandBuffer->commandBuffer->commandBuffer = [commandBuffer->commandPool->commandPool->commandQueue commandBuffer]; } gnReturnCode beginMetalCommandBuffer(gnCommandBuffer commandBuffer) { commandBuffer->commandBuffer->boundGraphcisPipeline = NULL; @@ -17,7 +26,8 @@ gnReturnCode beginMetalCommandBuffer(gnCommandBuffer commandBuffer) { return GN_SUCCESS; } -gnReturnCode endMetalCommandBuffer(gnCommandBuffer commandBuffer) { - // [commandBuffer->commandBuffer->commandBuffer commit]; - return GN_SUCCESS; +gnReturnCode endMetalCommandBuffer(gnCommandBuffer commandBuffer) { return GN_SUCCESS; } + +void destroyMetalCommandBuffer(gnCommandBuffer commandBuffer) { + [commandBuffer->commandBuffer->commandBuffer release]; } diff --git a/projects/apis/metal/src/framebuffers/metal_framebuffer.m b/projects/apis/metal/src/framebuffers/metal_framebuffer.m index 2d465ee..a70e719 100644 --- a/projects/apis/metal/src/framebuffers/metal_framebuffer.m +++ b/projects/apis/metal/src/framebuffers/metal_framebuffer.m @@ -53,9 +53,8 @@ gnReturnCode createMetalFramebuffer(gnFramebuffer framebuffer, gnOutputDevice de wasDepthStencil = gnTrue; } if (isStencilFormat(info.renderPassDescriptor->info.attachmentInfos[i].format)) { - gnDebuggerSetErrorMessage(device->instance->debugger, (gnMessageData){ - .message = gnCreateString("Stencil attachments are not currently supported in metal (get on this)") - }); + MTLRenderPassStencilAttachmentDescriptor* stencilAttachment = framebuffer->framebuffer->framebuffer.stencilAttachment; + stencilAttachment.texture = info.attachments[i]->texture->texture; wasDepthStencil = gnTrue; } diff --git a/projects/apis/vulkan/loader/vulkan_command_loader.c b/projects/apis/vulkan/loader/vulkan_command_loader.c index 57d8ca3..de7eca7 100644 --- a/projects/apis/vulkan/loader/vulkan_command_loader.c +++ b/projects/apis/vulkan/loader/vulkan_command_loader.c @@ -8,6 +8,7 @@ gnCommandFunctions loadVulkanCommandFunctions() { ._gnBeginCommandBuffer = beginCommandBuffer, ._gnResetCommandBuffer = resetCommandBuffer, ._gnEndCommandBuffer = endCommandBuffer, + ._gnDestroyCommandBuffer = destroyVulkanCommandBuffer, ._gnCommandBeginRenderPass = beginRenderPass, ._gnCommandEndRenderPass = endRenderPass, diff --git a/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.c b/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.c index 5d98ef2..bda2ef0 100644 --- a/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.c +++ b/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.c @@ -78,3 +78,7 @@ void VkEndTransferOperation(VkCommandBuffer transferBuffer, VkCommandPool pool, vkQueueWaitIdle(syncQueue); vkFreeCommandBuffers(device, pool, 1, &transferBuffer); } + +void destroyVulkanCommandBuffer(gnCommandBufferHandle commandBuffer) { + vkFreeCommandBuffers(commandBuffer->commandPool->device->outputDevice->device, commandBuffer->commandPool->commandPool->commandPool, 1, &commandBuffer->commandBuffer->buffer); +} diff --git a/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.h b/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.h index 9030411..2a29cf7 100644 --- a/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.h +++ b/projects/apis/vulkan/src/commands/command_buffer/vulkan_command_buffer.h @@ -19,3 +19,4 @@ gnReturnCode allocateCommandBuffers(gnCommandBufferHandle* commandBuffers, uint3 gnReturnCode beginCommandBuffer(gnCommandBufferHandle commandBuffer); void resetCommandBuffer(gnCommandBufferHandle commandBuffer); gnReturnCode endCommandBuffer(gnCommandBufferHandle commandBuffer); +void destroyVulkanCommandBuffer(gnCommandBufferHandle commandBuffer); diff --git a/projects/core/src/command/command_buffer/gryphn_command_buffer.c b/projects/core/src/command/command_buffer/gryphn_command_buffer.c index 8bbd35e..66eae7f 100644 --- a/projects/core/src/command/command_buffer/gryphn_command_buffer.c +++ b/projects/core/src/command/command_buffer/gryphn_command_buffer.c @@ -30,3 +30,7 @@ gnReturnCode gnBeginCommandBuffer(gnCommandBufferHandle commandBuffer) { gnReturnCode gnEndCommandBuffer(gnCommandBufferHandle commandBuffer) { return commandBuffer->commandPool->instance->callingLayer->commandFunctions._gnEndCommandBuffer(commandBuffer); } + +void gnDestroyCommandBuffer(gnCommandBufferHandle commandBuffer) { + commandBuffer->commandPool->instance->callingLayer->commandFunctions._gnDestroyCommandBuffer(commandBuffer); +} diff --git a/projects/core/src/command/command_buffer/gryphn_command_buffer.h b/projects/core/src/command/command_buffer/gryphn_command_buffer.h index b341f87..e86efd8 100644 --- a/projects/core/src/command/command_buffer/gryphn_command_buffer.h +++ b/projects/core/src/command/command_buffer/gryphn_command_buffer.h @@ -27,3 +27,4 @@ gnReturnCode gnCommandPoolAllocateCommandBuffersFromList(gnCommandBufferArrayLis void gnResetCommandBuffer(gnCommandBufferHandle commandBuffer); gnReturnCode gnBeginCommandBuffer(gnCommandBufferHandle commandBuffer); gnReturnCode gnEndCommandBuffer(gnCommandBufferHandle commandBuffer); +void gnDestroyCommandBuffer(gnCommandBufferHandle commandBuffer); diff --git a/projects/core/src/command/command_pool/gryphn_command_pool.c b/projects/core/src/command/command_pool/gryphn_command_pool.c index b4b8759..4e011d1 100644 --- a/projects/core/src/command/command_pool/gryphn_command_pool.c +++ b/projects/core/src/command/command_pool/gryphn_command_pool.c @@ -8,6 +8,7 @@ gnReturnCode gnCreateCommandPool(gnCommandPoolHandle* commandPool, gnOutputDevic (*commandPool)->instance = device->instance; (*commandPool)->device = device; + (*commandPool)->info = info; return device->instance->callingLayer->deviceFunctions._gnCreateCommandPool((*commandPool), device, info); } diff --git a/projects/core/src/command/command_pool/gryphn_command_pool.h b/projects/core/src/command/command_pool/gryphn_command_pool.h index 751c4d2..a40edd3 100644 --- a/projects/core/src/command/command_pool/gryphn_command_pool.h +++ b/projects/core/src/command/command_pool/gryphn_command_pool.h @@ -3,8 +3,14 @@ #include #include "gryphn_handles.h" +typedef enum gnCommandPoolFlags { + GN_NO_FLAGS = 0, + GN_REUSE_COMMAND_BUFFERS = 1 +} gnCommandPoolFlags; + typedef struct gnCommandPoolInfo { uint32_t queueIndex; + gnCommandPoolFlags flags; } gnCommandPoolInfo; #ifdef GN_REVEAL_IMPL @@ -12,6 +18,7 @@ struct gnCommandPool_t { struct gnPlatformCommandPool_t* commandPool; gnDevice device; gnInstance instance; + gnCommandPoolInfo info; }; #endif diff --git a/projects/loader/src/gryphn_command_functions.h b/projects/loader/src/gryphn_command_functions.h index ed6d5e4..03e586b 100644 --- a/projects/loader/src/gryphn_command_functions.h +++ b/projects/loader/src/gryphn_command_functions.h @@ -15,6 +15,7 @@ typedef struct gnCommandFunctions_t { gnReturnCode (*_gnBeginCommandBuffer)(gnCommandBufferHandle commandBuffer); void (*_gnResetCommandBuffer)(gnCommandBufferHandle commandBuffer); gnReturnCode (*_gnEndCommandBuffer)(gnCommandBufferHandle commandBuffer); + void (*_gnDestroyCommandBuffer)(gnCommandBufferHandle commandBuffer); void (*_gnCommandBeginRenderPass)(gnCommandBufferHandle buffer, gnRenderPassInfo passInfo); void (*_gnCommandEndRenderPass)(gnCommandBufferHandle buffer); diff --git a/projects/validation_layers/function_loader/CMakeLists.txt b/projects/validation_layers/function_loader/CMakeLists.txt index 2009596..f0a7a50 100644 --- a/projects/validation_layers/function_loader/CMakeLists.txt +++ b/projects/validation_layers/function_loader/CMakeLists.txt @@ -8,3 +8,4 @@ target_include_directories(GryphnFunctionValidator PUBLIC ${CMAKE_CURRENT_SOURCE target_include_directories(GryphnFunctionValidator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../) target_include_directories(GryphnFunctionValidator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../core/src/) target_include_directories(GryphnFunctionValidator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../platform/) +target_include_directories(GryphnFunctionValidator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/) diff --git a/projects/validation_layers/function_loader/loader/function_loader.c b/projects/validation_layers/function_loader/loader/function_loader.c index dcda9ef..3d0f199 100644 --- a/projects/validation_layers/function_loader/loader/function_loader.c +++ b/projects/validation_layers/function_loader/loader/function_loader.c @@ -1,7 +1,7 @@ #include "function_loader.h" -#include "instance_functions.h" -#include "device_functions.h" -#include "command_functions.h" +#include "src/instance_functions.h" +#include "src/device_functions.h" +#include "src/command_functions.h" gnInstanceFunctions loadFunctionLoaderInstanceFunctions() { return (gnInstanceFunctions){ @@ -94,6 +94,7 @@ gnCommandFunctions loadFunctionLoaderCommandFunctions() { ._gnBeginCommandBuffer = checkBeginCommandBuffer, ._gnResetCommandBuffer = checkResetCommandBuffer, ._gnEndCommandBuffer = checkEndCommandBuffer, + ._gnDestroyCommandBuffer = checkDestroyCommandBuffer, ._gnCommandBeginRenderPass = checkCommandBeginRenderPass, ._gnCommandEndRenderPass = checkCommandEndRenderPass, diff --git a/projects/validation_layers/function_loader/loader/command_functions.c b/projects/validation_layers/function_loader/src/command_functions.c similarity index 95% rename from projects/validation_layers/function_loader/loader/command_functions.c rename to projects/validation_layers/function_loader/src/command_functions.c index cbf27e7..b2af583 100644 --- a/projects/validation_layers/function_loader/loader/command_functions.c +++ b/projects/validation_layers/function_loader/src/command_functions.c @@ -21,6 +21,9 @@ void checkResetCommandBuffer(gnCommandBufferHandle commandBuffer) { gnReturnCode checkEndCommandBuffer(gnCommandBufferHandle commandBuffer) { CHECK_FUNCTION_WITH_RETURN_CODE_COMMAND(commandBuffer->instance, _gnEndCommandBuffer, commandBuffer); } +void checkDestroyCommandBuffer(gnCommandBufferHandle buffer) { + CHECK_VOID_FUNCTION_COMMAND(buffer->instance, _gnDestroyCommandBuffer, buffer); +} void checkCommandBeginRenderPass(gnCommandBufferHandle buffer, gnRenderPassInfo passInfo) { CHECK_VOID_FUNCTION_COMMAND(buffer->instance, _gnCommandBeginRenderPass, buffer, passInfo); diff --git a/projects/validation_layers/function_loader/loader/command_functions.h b/projects/validation_layers/function_loader/src/command_functions.h similarity index 95% rename from projects/validation_layers/function_loader/loader/command_functions.h rename to projects/validation_layers/function_loader/src/command_functions.h index fdc110c..8350339 100644 --- a/projects/validation_layers/function_loader/loader/command_functions.h +++ b/projects/validation_layers/function_loader/src/command_functions.h @@ -4,6 +4,7 @@ gnReturnCode checkCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* comma gnReturnCode checkBeginCommandBuffer(gnCommandBufferHandle commandBuffer); void checkResetCommandBuffer(gnCommandBufferHandle commandBuffer); gnReturnCode checkEndCommandBuffer(gnCommandBufferHandle commandBuffer); +void checkDestroyCommandBuffer(gnCommandBufferHandle commandBuffer); void checkCommandBeginRenderPass(gnCommandBufferHandle buffer, gnRenderPassInfo passInfo); void checkCommandEndRenderPass(gnCommandBufferHandle buffer); diff --git a/projects/validation_layers/function_loader/loader/device_functions.c b/projects/validation_layers/function_loader/src/device_functions.c similarity index 100% rename from projects/validation_layers/function_loader/loader/device_functions.c rename to projects/validation_layers/function_loader/src/device_functions.c diff --git a/projects/validation_layers/function_loader/loader/device_functions.h b/projects/validation_layers/function_loader/src/device_functions.h similarity index 100% rename from projects/validation_layers/function_loader/loader/device_functions.h rename to projects/validation_layers/function_loader/src/device_functions.h diff --git a/projects/validation_layers/function_loader/loader/instance_functions.c b/projects/validation_layers/function_loader/src/instance_functions.c similarity index 100% rename from projects/validation_layers/function_loader/loader/instance_functions.c rename to projects/validation_layers/function_loader/src/instance_functions.c diff --git a/projects/validation_layers/function_loader/loader/instance_functions.h b/projects/validation_layers/function_loader/src/instance_functions.h similarity index 100% rename from projects/validation_layers/function_loader/loader/instance_functions.h rename to projects/validation_layers/function_loader/src/instance_functions.h diff --git a/projects/validation_layers/function_loader/loader/loader_utils.h b/projects/validation_layers/function_loader/src/loader_utils.h similarity index 100% rename from projects/validation_layers/function_loader/loader/loader_utils.h rename to projects/validation_layers/function_loader/src/loader_utils.h