working on Vulkahn compute pipelines

This commit is contained in:
2024-11-18 17:23:14 +01:00
parent d8b14d1a7e
commit d0eb9e0876
18 changed files with 278 additions and 42 deletions

View File

@@ -20,11 +20,10 @@
if((res) != VK_SUCCESS) \
{ \
if(backend != PULSE_NULL_HANDLE && PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend)) \
PulseLogErrorFmt(backend, "(Vulkan) Call to Vulkan function failed due to %s", VulkanVerbaliseResult(res)); \
PulseLogErrorFmt(backend, "(Vulkan) call to Vulkan function failed due to %s", VulkanVerbaliseResult(res)); \
PulseSetInternalError(error); \
return retval; \
}
#define CHECK_VK(backend, res, error) CHECK_VK_RETVAL(backend, res, error, )
typedef struct VulkanGlobal

View File

@@ -78,9 +78,8 @@ PulseCommandList VulkanRequestCommandList(PulseDevice device, PulseCommandListUs
}
cmd->compute_pipelines_bound_size = 0;
cmd->state = PULSE_COMMAND_LIST_STATE_EMPTY;
cmd->state = PULSE_COMMAND_LIST_STATE_RECORDING;
cmd->is_available = false;
cmd->is_compute_pipeline_bound = false;
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*);
VulkanCommandList* vulkan_cmd = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd, VulkanCommandList*);
@@ -103,7 +102,6 @@ PulseCommandList VulkanRequestCommandList(PulseDevice device, PulseCommandListUs
bool VulkanSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFence fence)
{
PULSE_UNUSED(device);
PULSE_CHECK_HANDLE_RETVAL(cmd, false);
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*);
@@ -121,8 +119,9 @@ bool VulkanSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFenc
VkFence vulkan_fence;
if(fence != PULSE_NULL_HANDLE)
{
vulkan_fence = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VkFence);
vulkan_device->vkResetFences(vulkan_device->device, 1, &vulkan_fence);
vulkan_fence = VULKAN_RETRIEVE_DRIVER_DATA_AS(fence, VkFence);
CHECK_VK_RETVAL(device->backend, vulkan_device->vkResetFences(vulkan_device->device, 1, &vulkan_fence), PULSE_ERROR_DEVICE_ALLOCATION_FAILED, false);
fence->cmd = cmd;
}
VulkanQueue* vulkan_queue;
@@ -140,6 +139,7 @@ bool VulkanSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFenc
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &vulkan_cmd->cmd;
res = vulkan_device->vkQueueSubmit(vulkan_queue->queue, 1, &submit_info, vulkan_fence);
cmd->state = PULSE_COMMAND_LIST_STATE_SENT;
switch(res)
{
case VK_SUCCESS: return true;

View File

@@ -2,16 +2,92 @@
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include "Vulkan.h"
#include "VulkanDevice.h"
#include "VulkanCommandList.h"
#include "VulkanComputePipeline.h"
PulseComputePipeline VulkanCreateComputePipeline(PulseDevice device, const PulseComputePipelineCreateInfo* info)
{
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*);
PulseComputePipelineHandler* pipeline = (PulseComputePipelineHandler*)calloc(1, sizeof(PulseComputePipelineHandler));
PULSE_CHECK_ALLOCATION_RETVAL(pipeline, PULSE_NULL_HANDLE);
VulkanComputePipeline* vulkan_pipeline = (VulkanComputePipeline*)calloc(1, sizeof(VulkanComputePipeline));
PULSE_CHECK_ALLOCATION_RETVAL(vulkan_pipeline, PULSE_NULL_HANDLE);
pipeline->driver_data = vulkan_pipeline;
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
{
if(info->code == PULSE_NULLPTR)
PulseLogError(device->backend, "invalid code pointer passed to PulseComputePipelineCreateInfo");
if(info->entrypoint == PULSE_NULLPTR)
PulseLogError(device->backend, "invalid entrypoint pointer passed to PulseComputePipelineCreateInfo");
if(info->format == PULSE_SHADER_FORMAT_SPIRV_BIT && (device->backend->supported_shader_formats & PULSE_SHADER_FORMAT_SPIRV_BIT) == 0)
PulseLogError(device->backend, "invalid shader format passed to PulseComputePipelineCreateInfo");
}
VkShaderModuleCreateInfo shader_module_create_info = {};
shader_module_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shader_module_create_info.codeSize = info->code_size;
shader_module_create_info.pCode = (const uint32_t*)info->code;
CHECK_VK_RETVAL(device->backend, vulkan_device->vkCreateShaderModule(vulkan_device->device, &shader_module_create_info, PULSE_NULLPTR, &vulkan_pipeline->module), PULSE_ERROR_INITIALIZATION_FAILED, PULSE_NULL_HANDLE);
VkPipelineShaderStageCreateInfo shader_stage_info = {};
shader_stage_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shader_stage_info.stage = VK_SHADER_STAGE_COMPUTE_BIT;
shader_stage_info.module = vulkan_pipeline->module;
shader_stage_info.pName = info->entrypoint;
VkPipelineLayoutCreateInfo pipeline_layout_info = {};
pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipeline_layout_info.setLayoutCount = 0;
pipeline_layout_info.pSetLayouts = PULSE_NULLPTR; // will change
CHECK_VK_RETVAL(device->backend, vulkan_device->vkCreatePipelineLayout(vulkan_device->device, &pipeline_layout_info, PULSE_NULLPTR, &vulkan_pipeline->layout), PULSE_ERROR_INITIALIZATION_FAILED, PULSE_NULL_HANDLE);
VkComputePipelineCreateInfo pipeline_info = {};
pipeline_info.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
pipeline_info.layout = vulkan_pipeline->layout;
pipeline_info.stage = shader_stage_info;
CHECK_VK_RETVAL(device->backend, vulkan_device->vkCreateComputePipelines(vulkan_device->device, VK_NULL_HANDLE, 1, &pipeline_info, PULSE_NULLPTR, &vulkan_pipeline->pipeline), PULSE_ERROR_INITIALIZATION_FAILED, PULSE_NULL_HANDLE);
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(device->backend))
PulseLogInfoFmt(device->backend, "(Vulkan) created new compute pipeline %p", pipeline);
return pipeline;
}
void VulkanBindComputePipeline(PulseComputePass pass, PulseComputePipeline pipeline)
void VulkanDispatchComputePipeline(PulseComputePipeline pipeline, PulseCommandList cmd, uint32_t groupcount_x, uint32_t groupcount_y, uint32_t groupcount_z)
{
VulkanComputePipeline* vulkan_pipeline = VULKAN_RETRIEVE_DRIVER_DATA_AS(pipeline, VulkanComputePipeline*);
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd->device, VulkanDevice*);
VulkanCommandList* vulkan_cmd = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd, VulkanCommandList*);
vulkan_device->vkCmdBindPipeline(vulkan_cmd->cmd, VK_PIPELINE_BIND_POINT_COMPUTE, vulkan_pipeline->pipeline);
vulkan_device->vkCmdDispatch(vulkan_cmd->cmd, groupcount_x, groupcount_y, groupcount_z);
}
void VulkanDestroyComputePipeline(PulseDevice device, PulseComputePipeline pipeline)
{
if(pipeline == PULSE_NULL_HANDLE)
{
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
PulseLogWarning(device->backend, "compute pipeline is NULL, this may be a bug in your application");
return;
}
VulkanComputePipeline* vulkan_pipeline = VULKAN_RETRIEVE_DRIVER_DATA_AS(pipeline, VulkanComputePipeline*);
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*);
vulkan_device->vkDeviceWaitIdle(vulkan_device->device);
vulkan_device->vkDestroyShaderModule(vulkan_device->device, vulkan_pipeline->module, PULSE_NULLPTR);
vulkan_device->vkDestroyPipelineLayout(vulkan_device->device, vulkan_pipeline->layout, PULSE_NULLPTR);
vulkan_device->vkDestroyPipeline(vulkan_device->device, vulkan_pipeline->pipeline, PULSE_NULLPTR);
free(vulkan_pipeline);
free(pipeline);
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(device->backend))
PulseLogInfoFmt(device->backend, "(Vulkan) destroyed compute pipeline %p", pipeline);
}

View File

@@ -13,10 +13,13 @@
typedef struct VulkanComputePipeline
{
VkShaderModule module;
VkPipelineLayout layout;
VkPipeline pipeline;
} VulkanComputePipeline;
PulseComputePipeline VulkanCreateComputePipeline(PulseDevice device, const PulseComputePipelineCreateInfo* info);
void VulkanBindComputePipeline(PulseComputePipeline pipeline);
void VulkanDispatchComputePipeline(PulseComputePipeline pipeline, PulseCommandList cmd, uint32_t groupcount_x, uint32_t groupcount_y, uint32_t groupcount_z);
void VulkanDestroyComputePipeline(PulseDevice device, PulseComputePipeline pipeline);
#endif // PULSE_VULKAN_COMPUTE_PIPELINE_H_

View File

@@ -227,7 +227,7 @@ PulseDevice VulkanCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
PULSE_LOAD_DRIVER_DEVICE(Vulkan);
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(backend))
PulseLogInfoFmt(backend, "(Vulkan) Created device from %s", device->properties.deviceName);
PulseLogInfoFmt(backend, "(Vulkan) created device from %s", device->properties.deviceName);
return pulse_device;
}
@@ -241,7 +241,7 @@ void VulkanDestroyDevice(PulseDevice device)
vmaDestroyAllocator(vulkan_device->allocator);
vulkan_device->vkDestroyDevice(vulkan_device->device, PULSE_NULLPTR);
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(device->backend))
PulseLogInfoFmt(device->backend, "(Vulkan) Destroyed device created from %s", vulkan_device->properties.deviceName);
PulseLogInfoFmt(device->backend, "(Vulkan) destroyed device created from %s", vulkan_device->properties.deviceName);
free(vulkan_device->cmd_pools);
free(vulkan_device);
free(device);

View File

@@ -21,6 +21,7 @@ PulseFence VulkanCreateFence(PulseDevice device)
PulseFenceHandler* fence = (PulseFenceHandler*)malloc(sizeof(PulseFenceHandler));
PULSE_CHECK_ALLOCATION_RETVAL(fence, PULSE_NULL_HANDLE);
fence->cmd = PULSE_NULL_HANDLE;
fence->driver_data = vulkan_fence;
return fence;
}
@@ -60,7 +61,10 @@ bool VulkanIsFenceReady(PulseDevice device, PulseFence fence)
VkResult res = vulkan_device->vkGetFenceStatus(vulkan_device->device, vulkan_fence);
switch(res)
{
case VK_SUCCESS: return true;
case VK_SUCCESS:
if(fence->cmd != PULSE_NULL_HANDLE)
fence->cmd->state = PULSE_COMMAND_LIST_STATE_READY;
return true;
case VK_NOT_READY: return false;
case VK_ERROR_DEVICE_LOST: PulseSetInternalError(PULSE_ERROR_DEVICE_LOST); return false;
@@ -80,12 +84,19 @@ bool VulkanWaitForFences(PulseDevice device, const PulseFence* fences, uint32_t
VkFence* vulkan_fences = (VkFence*)calloc(fences_count, sizeof(VkFence));
PULSE_CHECK_ALLOCATION_RETVAL(vulkan_fences, false);
for(uint32_t i = 0; i < fences_count; i++)
{
if(fences[i]->cmd == PULSE_NULL_HANDLE && PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(device->backend))
PulseLogError(device->backend, "cannot wait on a fence that has no command list attached to it");
vulkan_fences[i] = VULKAN_RETRIEVE_DRIVER_DATA_AS(((PulseFence)fences[i]), VkFence);
}
VkResult result = vulkan_device->vkWaitForFences(vulkan_device->device, fences_count, vulkan_fences, wait_for_all, UINT64_MAX);
free(vulkan_fences);
switch(result)
{
case VK_SUCCESS: break;
case VK_SUCCESS:
for(uint32_t i = 0; i < fences_count; i++)
fences[i]->cmd->state = PULSE_COMMAND_LIST_STATE_READY;
break;
case VK_TIMEOUT: break;
case VK_ERROR_DEVICE_LOST: PulseSetInternalError(PULSE_ERROR_DEVICE_LOST); return false;