From 1b021713595acad55831aae663d7a1729701f140 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Fri, 29 Dec 2017 10:01:11 +0100 Subject: [PATCH] try to make ctx thread safe with dedicated pools by context --- CMakeLists.txt | 4 ++-- src/vkvg_context.c | 13 ++++++++++--- src/vkvg_context_internal.c | 15 ++++++++++++--- src/vkvg_context_internal.h | 12 ++++++++---- src/vkvg_device.c | 25 ++++++++++++------------- src/vkvg_device_internal.h | 3 +++ src/vkvg_surface.c | 27 ++++++++++++--------------- src/vkvg_surface_internal.h | 2 -- 8 files changed, 59 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f265359..187e420 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ FOREACH(SHADER ${SHADERS}) SET(SHADER_OUTPUTS ${SHADER_OUTPUTS} ${shader-output}) ENDFOREACH() -ADD_CUSTOM_TARGET(CompileShaders ALL DEPENDS ${SHADER_OUTPUTS}) +#ADD_CUSTOM_TARGET(CompileShaders ALL DEPENDS ${SHADER_OUTPUTS}) ADD_CUSTOM_TARGET(BuildShaderHeader ALL DEPENDS ${SHADER_OUTPUTS}) # COMMENT "Clear ${SHADERS_H}" @@ -93,7 +93,7 @@ FILE(GLOB VKVG_SRC src/*.c vkh/*.c) ADD_LIBRARY(${PROJECT_NAME} SHARED ${VKVG_SRC} ${SHADERS}) add_dependencies(${PROJECT_NAME} BuildShaderHeader) -add_dependencies(BuildShaderHeader CompileShaders) +#add_dependencies(BuildShaderHeader CompileShaders) SET_TARGET_PROPERTIES(vkvg PROPERTIES VERSION ${PROJECT_VERSION} diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 79333b8..49eda59 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -9,6 +9,7 @@ static uint32_t dlpCount = 0; VkvgContext vkvg_create(VkvgSurface surf) { + VkvgDevice dev = surf->dev; VkvgContext ctx = (vkvg_context*)calloc(1, sizeof(vkvg_context)); ctx->sizePoints = VKVG_PTS_SIZE; @@ -34,13 +35,16 @@ VkvgContext vkvg_create(VkvgSurface surf) ctx->selectedFont.fontFile = (char*)calloc(FONT_FILE_NAME_MAX_SIZE,sizeof(char)); - ctx->flushFence = vkh_fence_create(ctx->pSurf->dev->vkDev); + ctx->flushFence = vkh_fence_create(dev->vkDev); ctx->points = (vec2*) malloc (VKVG_VBO_SIZE*sizeof(vec2)); ctx->pathes = (uint32_t*) malloc (VKVG_PATHES_SIZE*sizeof(uint32_t)); + ctx->cmdPool = vkh_cmd_pool_create (dev->vkDev, dev->qFam, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT); + _create_vertices_buff (ctx); _create_cmd_buff (ctx); + _createDescriptorPool (ctx); _init_descriptor_sets (ctx); _update_font_descriptor_set (ctx); _init_cmd_buff (ctx); @@ -89,10 +93,13 @@ void vkvg_destroy (VkvgContext ctx) VkDevice dev = ctx->pSurf->dev->vkDev; vkDestroyFence (dev, ctx->flushFence,NULL); - vkFreeCommandBuffers(dev, ctx->pSurf->dev->cmdPool, 1, &ctx->cmd); + vkFreeCommandBuffers(dev, ctx->cmdPool, 1, &ctx->cmd); + vkDestroyCommandPool(dev, ctx->cmdPool, NULL); VkDescriptorSet dss[] = {ctx->dsFont,ctx->dsSrc}; - vkFreeDescriptorSets(dev, ctx->pSurf->dev->descriptorPool,2,dss); + vkFreeDescriptorSets (dev, ctx->descriptorPool,2,dss); + + vkDestroyDescriptorPool (dev, ctx->descriptorPool,NULL); vkvg_buffer_destroy (&ctx->indices); vkvg_buffer_destroy (&ctx->vertices); diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 0104fb7..8813c50 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -68,7 +68,7 @@ void _add_triangle_indices(VkvgContext ctx, uint32_t i0, uint32_t i1, uint32_t i ctx->indCount+=3; } void _create_cmd_buff (VkvgContext ctx){ - ctx->cmd = vkh_cmd_buff_create(ctx->pSurf->dev->vkDev, ctx->pSurf->dev->cmdPool,VK_COMMAND_BUFFER_LEVEL_PRIMARY); + ctx->cmd = vkh_cmd_buff_create(ctx->pSurf->dev->vkDev, ctx->cmdPool,VK_COMMAND_BUFFER_LEVEL_PRIMARY); } void _record_draw_cmd (VkvgContext ctx){ if (ctx->indCount == ctx->curIndStart) @@ -230,11 +230,20 @@ void _update_font_descriptor_set (VkvgContext ctx){ }; vkUpdateDescriptorSets(dev->vkDev, 1, &writeDescriptorSet, 0, NULL); } - +void _createDescriptorPool (VkvgContext ctx) { + VkvgDevice dev = ctx->pSurf->dev; + VkDescriptorPoolSize descriptorPoolSize = {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4 }; + VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .maxSets = 4, + .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, + .poolSizeCount = 1, + .pPoolSizes = &descriptorPoolSize }; + VK_CHECK_RESULT(vkCreateDescriptorPool(dev->vkDev, &descriptorPoolCreateInfo, NULL, &ctx->descriptorPool)); +} void _init_descriptor_sets (VkvgContext ctx){ VkvgDevice dev = ctx->pSurf->dev; VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, - .descriptorPool = dev->descriptorPool, + .descriptorPool = ctx->descriptorPool, .descriptorSetCount = 1, .pSetLayouts = &dev->dslFont }; VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsFont)); diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index 0e32b9a..84430b7 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -66,12 +66,15 @@ typedef struct _vkvg_context_t { VkvgContext pNext; VkvgSurface pSurf; - VkCommandBuffer cmd; VkFence flushFence; uint32_t stencilRef; VkhImage source; - VkDescriptorSet dsFont; - VkDescriptorSet dsSrc; + + VkCommandPool cmdPool; + VkCommandBuffer cmd; + VkDescriptorPool descriptorPool; + VkDescriptorSet dsFont; + VkDescriptorSet dsSrc; //vk buffers, holds data until flush vkvg_buff indices; @@ -133,9 +136,10 @@ void _clear_path (VkvgContext ctx); bool _path_is_closed (VkvgContext ctx, uint32_t ptrPath); uint32_t _get_last_point_of_closed_path (VkvgContext ctx, uint32_t ptrPath); +void _createDescriptorPool (VkvgContext ctx); void _init_descriptor_sets (VkvgContext ctx); void _update_source_descriptor_set (VkvgContext ctx); -void _update_font_descriptor_set (VkvgContext ctx); +void _update_font_descriptor_set (VkvgContext ctx); static inline float vec2_zcross (vec2 v1, vec2 v2){ return v1.x*v2.y-v1.y*v2.x; diff --git a/src/vkvg_device.c b/src/vkvg_device.c index 5594095..1331b25 100644 --- a/src/vkvg_device.c +++ b/src/vkvg_device.c @@ -6,7 +6,6 @@ void _create_pipeline_cache (VkvgDevice dev); void _setupRenderPass (VkvgDevice dev); void _setupPipelines (VkvgDevice dev); void _createDescriptorSetLayout (VkvgDevice dev); -void _createDescriptorSet (VkvgDevice dev); VkvgDevice vkvg_device_create(VkDevice vkdev, VkQueue queue, uint32_t qFam, VkPhysicalDeviceMemoryProperties memprops) { @@ -19,7 +18,12 @@ VkvgDevice vkvg_device_create(VkDevice vkdev, VkQueue queue, uint32_t qFam, VkPh dev->phyMemProps = memprops; dev->queue = queue; - dev->cmdPool = vkh_cmd_pool_create (dev->vkDev, qFam, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT); + dev->qFam = qFam; + dev->lastCtx = NULL; + + dev->cmdPool = vkh_cmd_pool_create (dev->vkDev, qFam, VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT); + dev->cmd = vkh_cmd_buff_create (dev->vkDev, dev->cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY); + dev->fence = vkh_fence_create_signaled (dev->vkDev); _create_pipeline_cache(dev); @@ -28,7 +32,6 @@ VkvgDevice vkvg_device_create(VkDevice vkdev, VkQueue queue, uint32_t qFam, VkPh _setupRenderPass (dev); _createDescriptorSetLayout (dev); - _createDescriptorSet (dev); _setupPipelines (dev); return dev; @@ -38,7 +41,6 @@ void vkvg_device_destroy(VkvgDevice dev) { vkDestroyDescriptorSetLayout (dev->vkDev, dev->dslFont,NULL); vkDestroyDescriptorSetLayout (dev->vkDev, dev->dslSrc, NULL); - vkDestroyDescriptorPool (dev->vkDev, dev->descriptorPool,NULL); vkDestroyPipeline (dev->vkDev, dev->pipeline, NULL); vkDestroyPipeline (dev->vkDev, dev->pipelineClipping, NULL); @@ -49,7 +51,13 @@ void vkvg_device_destroy(VkvgDevice dev) vkDestroyPipelineLayout (dev->vkDev, dev->pipelineLayout, NULL); vkDestroyPipelineCache (dev->vkDev, dev->pipelineCache, NULL); vkDestroyRenderPass (dev->vkDev, dev->renderPass, NULL); + + vkWaitForFences (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX); + + vkDestroyFence (dev->vkDev, dev->fence,NULL); + vkFreeCommandBuffers (dev->vkDev, dev->cmdPool, 1, &dev->cmd); vkDestroyCommandPool (dev->vkDev, dev->cmdPool, NULL); + _destroy_font_cache(dev); free(dev); } @@ -325,13 +333,4 @@ void _createDescriptorSetLayout (VkvgDevice dev) { VK_CHECK_RESULT(vkCreatePipelineLayout(dev->vkDev, &pipelineLayoutCreateInfo, NULL, &dev->pipelineLayout)); } -void _createDescriptorSet (VkvgDevice dev) { - VkDescriptorPoolSize descriptorPoolSize = {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4 }; - VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, - .maxSets = 4, - .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, - .poolSizeCount = 1, - .pPoolSizes = &descriptorPoolSize }; - VK_CHECK_RESULT(vkCreateDescriptorPool(dev->vkDev, &descriptorPoolCreateInfo, NULL, &dev->descriptorPool)); -} diff --git a/src/vkvg_device_internal.h b/src/vkvg_device_internal.h index 3b04f49..de11ab3 100644 --- a/src/vkvg_device_internal.h +++ b/src/vkvg_device_internal.h @@ -11,7 +11,10 @@ typedef struct _vkvg_device_t{ VkRenderPass renderPass; VkQueue queue; + uint32_t qFam; VkCommandPool cmdPool; + VkCommandBuffer cmd; + VkFence fence; VkPipeline pipeline; VkPipeline pipelineClipping; diff --git a/src/vkvg_surface.c b/src/vkvg_surface.c index 17d7ea7..da5afbe 100644 --- a/src/vkvg_surface.c +++ b/src/vkvg_surface.c @@ -3,26 +3,28 @@ void _clear_stencil (VkvgSurface surf) { - vkh_cmd_begin (surf->cmd,VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); + VkvgDevice dev = surf->dev; + VkCommandBuffer cmd = dev->cmd; + + vkWaitForFences (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX); + vkResetFences (dev->vkDev, 1, &dev->fence); + + vkh_cmd_begin (cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); VkClearDepthStencilValue clr = {1.0f,0}; VkImageSubresourceRange range = {VK_IMAGE_ASPECT_STENCIL_BIT,0,1,0,1}; - vkh_image_set_layout (surf->cmd, surf->stencilMS, VK_IMAGE_ASPECT_STENCIL_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, + vkh_image_set_layout (cmd, surf->stencilMS, VK_IMAGE_ASPECT_STENCIL_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); - vkCmdClearDepthStencilImage (surf->cmd, surf->stencilMS->image, + vkCmdClearDepthStencilImage (cmd, surf->stencilMS->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,&clr,1,&range); - vkh_image_set_layout (surf->cmd, surf->stencilMS, VK_IMAGE_ASPECT_STENCIL_BIT, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + vkh_image_set_layout (cmd, surf->stencilMS, VK_IMAGE_ASPECT_STENCIL_BIT, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); - vkh_cmd_end (surf->cmd); + vkh_cmd_end (cmd); - //vkh_cmd_submit_with_semaphores (surf->dev->queue,&surf->cmd,VK_NULL_HANDLE,surf->semaphore,VK_NULL_HANDLE); - VkFence fence = vkh_fence_create(surf->dev->vkDev); - vkh_cmd_submit (surf->dev->queue,&surf->cmd,fence); - vkWaitForFences(surf->dev->vkDev,1,&fence,VK_TRUE,UINT64_MAX); - vkDestroyFence(surf->dev->vkDev,fence,NULL); + vkh_cmd_submit (dev->queue, &cmd, dev->fence); } VkvgSurface vkvg_surface_create(VkvgDevice dev, int32_t width, uint32_t height){ @@ -56,9 +58,6 @@ VkvgSurface vkvg_surface_create(VkvgDevice dev, int32_t width, uint32_t height){ .layers = 1 }; VK_CHECK_RESULT(vkCreateFramebuffer(surf->dev->vkDev, &frameBufferCreateInfo, NULL, &surf->fb)); - surf->semaphore = vkh_semaphore_create(dev->vkDev); - surf->cmd = vkh_cmd_buff_create(surf->dev->vkDev, surf->dev->cmdPool,VK_COMMAND_BUFFER_LEVEL_PRIMARY); - _clear_stencil(surf); return surf; @@ -66,8 +65,6 @@ VkvgSurface vkvg_surface_create(VkvgDevice dev, int32_t width, uint32_t height){ void vkvg_surface_destroy(VkvgSurface surf) { - vkDestroySemaphore(surf->dev->vkDev,surf->semaphore,NULL); - vkFreeCommandBuffers(surf->dev->vkDev,surf->dev->cmdPool,1,&surf->cmd); vkDestroyFramebuffer(surf->dev->vkDev, surf->fb, NULL); vkh_image_destroy(surf->img); vkh_image_destroy(surf->imgMS); diff --git a/src/vkvg_surface_internal.h b/src/vkvg_surface_internal.h index b7c7f92..60a37fc 100644 --- a/src/vkvg_surface_internal.h +++ b/src/vkvg_surface_internal.h @@ -13,8 +13,6 @@ typedef struct _vkvg_surface_t { VkhImage img; VkhImage imgMS; VkhImage stencilMS; - VkSemaphore semaphore; - VkCommandBuffer cmd; }vkvg_surface; void _clear_stencil (VkvgSurface surf); -- 2.47.3