]> O.S.I.I.S - jp/vkvg.git/commitdiff
try to make ctx thread safe with dedicated pools by context
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 29 Dec 2017 09:01:11 +0000 (10:01 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 29 Dec 2017 09:01:11 +0000 (10:01 +0100)
CMakeLists.txt
src/vkvg_context.c
src/vkvg_context_internal.c
src/vkvg_context_internal.h
src/vkvg_device.c
src/vkvg_device_internal.h
src/vkvg_surface.c
src/vkvg_surface_internal.h

index f2653596c8b42f269867483f843ec4642d6ab373..187e420fd18eb20c311d293c62cbbb0a72ed3e42 100644 (file)
@@ -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}
index 79333b873d589c9cfbf15894634b498970a1836f..49eda5986549a56611da7aad06e085ddab6c9b04 100644 (file)
@@ -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);
index 0104fb73dff5bdceca13ec8b125d60d301edeabb..8813c50aa589c3e4597ec604949edaa1cebe999a 100644 (file)
@@ -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));
index 0e32b9ae91cb2e3c9692ea7a7bd29f46281af92c..84430b76c121918d3511fa1481ab6f794a0479b4 100644 (file)
@@ -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;
index 5594095a0108777511f4dbbef5cc2ac3506a9b49..1331b2583c07512ae197b816218094ca08f3c736 100644 (file)
@@ -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));
-}
index 3b04f49f9fea7cba789dff0969ffcdc962d741ae..de11ab3e5accce39ff0f2c1c5745e8d8a3912fd2 100644 (file)
@@ -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;
index 17d7ea7044e3d884f19532227e8cd55616a0a2ca..da5afbeb74b49309e3e765f9e4c4782f90e7b29f 100644 (file)
@@ -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);
index b7c7f923b955ff156c6f3e43eba054f8f47b5107..60a37fc9849f4d231e978002177bfe011e9b124e 100644 (file)
@@ -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);