From 44d2f931df70213db3948618f54749f726b22389 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 18 Jan 2022 14:16:06 +0100 Subject: [PATCH] move font and source descriptor to threaded objs --- src/vkvg_context.c | 21 +--------------- src/vkvg_context_internal.c | 49 +++++-------------------------------- src/vkvg_context_internal.h | 10 ++------ src/vkvg_device.c | 8 ++++++ src/vkvg_device_internal.c | 30 +++++++++++++++++++++++ src/vkvg_device_internal.h | 5 ++++ src/vkvg_fonts.c | 2 +- src/vkvg_surface.c | 2 +- 8 files changed, 54 insertions(+), 73 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index a64345d..2de8908 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -103,11 +103,10 @@ VkvgContext vkvg_create(VkvgSurface surf) ctx->indexCache = (VKVG_IBO_INDEX_TYPE*)malloc(ctx->sizeIndices * sizeof(VKVG_IBO_INDEX_TYPE)); ctx->savedStencils = malloc(0); - ctx->selectedFontName = (char*)calloc(FONT_NAME_MAX_SIZE, sizeof(char)); ctx->selectedCharSize = 10 << 6; ctx->currentFont = NULL; - if (!ctx->points || !ctx->pathes || !ctx->vertexCache || !ctx->indexCache || !ctx->savedStencils || !ctx->selectedFontName) { + if (!ctx->points || !ctx->pathes || !ctx->vertexCache || !ctx->indexCache || !ctx->savedStencils ) { dev->status = VKVG_STATUS_NO_MEMORY; if (ctx->points) free(ctx->points); @@ -119,8 +118,6 @@ VkvgContext vkvg_create(VkvgSurface surf) free(ctx->indexCache); if (ctx->savedStencils) free(ctx->savedStencils); - if (ctx->selectedFontName) - free(ctx->selectedFontName); return NULL; } @@ -133,10 +130,6 @@ VkvgContext vkvg_create(VkvgSurface surf) ctx->th_objs = _get_or_create_threaded_objects(ctx->pSurf->dev, thrd_current()); _create_cmd_buff (ctx); - _createDescriptorPool (ctx); - _init_descriptor_sets (ctx); - _update_descriptor_set (ctx, ctx->pSurf->dev->fontCache->texture, ctx->dsFont); - _update_descriptor_set (ctx, surf->dev->emptyImg, ctx->dsSrc); _clear_path (ctx); @@ -153,11 +146,6 @@ VkvgContext vkvg_create(VkvgSurface surf) vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)ctx->cmdBuffers[1], "CTX Cmd Buff B"); vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_FENCE, (uint64_t)ctx->flushFence, "CTX Flush Fence"); - vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_POOL, (uint64_t)ctx->descriptorPool, "CTX Descriptor Pool"); - vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)ctx->dsSrc, "CTX DescSet SOURCE"); - vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)ctx->dsFont, "CTX DescSet FONT"); - vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)ctx->dsGrad, "CTX DescSet GRADIENT"); - vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_BUFFER, (uint64_t)ctx->indices.buffer, "CTX Index Buff"); vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_BUFFER, (uint64_t)ctx->vertices.buffer, "CTX Vertex Buff"); #endif @@ -236,11 +224,6 @@ void vkvg_destroy (VkvgContext ctx) vkFreeCommandBuffers(dev, ctx->cmdPool, 2, ctx->cmdBuffers); vkDestroyCommandPool(dev, ctx->cmdPool, NULL); - VkDescriptorSet dss[] = {ctx->dsFont,ctx->dsSrc}; - vkFreeDescriptorSets (dev, ctx->descriptorPool, 2, dss); - - vkDestroyDescriptorPool (dev, ctx->descriptorPool,NULL); - vkvg_buffer_destroy (&ctx->indices); vkvg_buffer_destroy (&ctx->vertices); @@ -250,7 +233,6 @@ void vkvg_destroy (VkvgContext ctx) //TODO:check this for source counter //vkh_image_destroy (ctx->source); - free(ctx->selectedFontName); free(ctx->pathes); free(ctx->points); if (ctx->dashCount > 0) @@ -1255,7 +1237,6 @@ void vkvg_save (VkvgContext ctx){ sav->curFillRule= ctx->curFillRule; sav->selectedCharSize = ctx->selectedCharSize; - sav->selectedFontName = (char*)calloc(FONT_NAME_MAX_SIZE,sizeof(char)); strcpy (sav->selectedFontName, ctx->selectedFontName); sav->currentFont = ctx->currentFont; diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index d96be1a..0c1bf69 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -601,7 +601,7 @@ void _start_cmd_for_render_pass (VkvgContext ctx) { CmdSetScissor(ctx->cmd, 0, 1, &ctx->bounds); - VkDescriptorSet dss[] = {ctx->dsFont, ctx->dsSrc,ctx->th_objs->dsGrad}; + VkDescriptorSet dss[] = {ctx->th_objs->dsFont, ctx->th_objs->dsSrc,ctx->th_objs->dsGrad}; CmdBindDescriptorSets(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelineLayout, 0, 3, dss, 0, NULL); @@ -647,7 +647,7 @@ void _update_cur_pattern (VkvgContext ctx, VkvgPattern pat) { if (!_wait_flush_fence (ctx)) return; if (lastPat->type == VKVG_PATTERN_TYPE_SURFACE)//unbind current source surface by replacing it with empty texture - _update_descriptor_set (ctx, ctx->pSurf->dev->emptyImg, ctx->dsSrc); + _update_descriptor_set (ctx->th_objs, ctx->pSurf->dev->emptyImg, ctx->th_objs->dsSrc); break; case VKVG_PATTERN_TYPE_SURFACE: { @@ -704,7 +704,7 @@ void _update_cur_pattern (VkvgContext ctx, VkvgPattern pat) { vkh_image_create_sampler (ctx->source, filter, filter, VK_SAMPLER_MIPMAP_MODE_NEAREST, addrMode); - _update_descriptor_set (ctx, ctx->source, ctx->dsSrc); + _update_descriptor_set (ctx->th_objs, ctx->source, ctx->th_objs->dsSrc); if (pat->hasMatrix) { @@ -721,7 +721,7 @@ void _update_cur_pattern (VkvgContext ctx, VkvgPattern pat) { return; if (lastPat && lastPat->type == VKVG_PATTERN_TYPE_SURFACE) - _update_descriptor_set (ctx, ctx->pSurf->dev->emptyImg, ctx->dsSrc); + _update_descriptor_set (ctx->th_objs, ctx->pSurf->dev->emptyImg, ctx->th_objs->dsSrc); vec4 bounds = {{(float)ctx->pSurf->width}, {(float)ctx->pSurf->height}, {0}, {0}};//store img bounds in unused source field ctx->pushConsts.source = bounds; @@ -770,20 +770,7 @@ void _update_cur_pattern (VkvgContext ctx, VkvgPattern pat) { if (lastPat) vkvg_pattern_destroy (lastPat); } -void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds){ - if (!_wait_flush_fence(ctx))//descriptorSet update invalidate cmd buffs - return; - VkDescriptorImageInfo descSrcTex = vkh_image_get_descriptor (img, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - VkWriteDescriptorSet writeDescriptorSet = { - .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .dstSet = ds, - .dstBinding = 0, - .descriptorCount = 1, - .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, - .pImageInfo = &descSrcTex - }; - vkUpdateDescriptorSets(ctx->pSurf->dev->vkDev, 1, &writeDescriptorSet, 0, NULL); -} + /* * Reset currently bound descriptor which image could be destroyed */ @@ -799,30 +786,7 @@ void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds){ VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsSrc)); }*/ -void _createDescriptorPool (VkvgContext ctx) { - VkvgDevice dev = ctx->pSurf->dev; - const VkDescriptorPoolSize descriptorPoolSize[] = { - {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2 }, - {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1 } - }; - VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, - .maxSets = 3, - .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, - .poolSizeCount = 2, - .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 = ctx->descriptorPool, - .descriptorSetCount = 1, - .pSetLayouts = &dev->dslFont - }; - VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsFont)); - descriptorSetAllocateInfo.pSetLayouts = &dev->dslSrc; - VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsSrc)); -} + //populate vertice buff for stroke bool _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool isCurve){ Vertex v = {{0},ctx->curColor, {0,0,-1}}; @@ -1189,7 +1153,6 @@ bool ptInTriangle(vec2 p, vec2 p0, vec2 p1, vec2 p2) { void _free_ctx_save (vkvg_context_save_t* sav){ if (sav->dashCount > 0) free (sav->dashes); - free(sav->selectedFontName); free (sav); } diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index c9e799b..675ba3e 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -114,7 +114,7 @@ typedef struct _vkvg_context_save_t{ vkvg_fill_rule_t curFillRule; long selectedCharSize; /* Font size*/ - char* selectedFontName; + char selectedFontName[FONT_NAME_MAX_SIZE]; _vkvg_font_identity_t selectedFont; //hold current face and size before cache addition _vkvg_font_identity_t* currentFont; //font ready for lookup vkvg_direction_t textDirection; @@ -139,9 +139,6 @@ typedef struct _vkvg_context_t { VkCommandBuffer cmd; //current recording buffer bool cmdStarted; //prevent flushing empty renderpass bool pushCstDirty;//prevent pushing to gpu if not requested - VkDescriptorPool descriptorPool;//one pool per thread - VkDescriptorSet dsFont; //fonts glyphs texture atlas descriptor (local for thread safety) - VkDescriptorSet dsSrc; //source ds VkRect2D bounds; @@ -204,7 +201,7 @@ typedef struct _vkvg_context_t { vkvg_fill_rule_t curFillRule; long selectedCharSize; /* Font size*/ - char* selectedFontName; + char selectedFontName[FONT_NAME_MAX_SIZE]; //_vkvg_font_t selectedFont; //hold current face and size before cache addition _vkvg_font_identity_t* currentFont; //font pointing to cached fonts identity _vkvg_font_t* currentFontSize; //font structure by size ready for lookup @@ -304,9 +301,6 @@ void _update_cur_pattern (VkvgContext ctx, VkvgPattern pat); void _set_mat_inv_and_vkCmdPush (VkvgContext ctx); void _start_cmd_for_render_pass (VkvgContext ctx); -void _createDescriptorPool (VkvgContext ctx); -void _init_descriptor_sets (VkvgContext ctx); -void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds); void _free_ctx_save (vkvg_context_save_t* sav); static inline float vec2_zcross (vec2 v1, vec2 v2){ diff --git a/src/vkvg_device.c b/src/vkvg_device.c index 37a1c94..769e9f9 100644 --- a/src/vkvg_device.c +++ b/src/vkvg_device.c @@ -204,6 +204,14 @@ void vkvg_device_destroy (VkvgDevice dev) if (dev->references > 0) return; + vkvg_device_thread_items_t* tmp = dev->threaded_objects; + while (tmp) { + vkvg_device_thread_items_t* next = tmp->next; + _delete_threaded_object(dev, tmp); + tmp = next; + } + + LOG(VKVG_LOG_INFO, "DESTROY Device\n"); vkh_image_destroy (dev->emptyImg); diff --git a/src/vkvg_device_internal.c b/src/vkvg_device_internal.c index e635ea5..a94c441 100644 --- a/src/vkvg_device_internal.c +++ b/src/vkvg_device_internal.c @@ -59,6 +59,9 @@ PFN_vkResetCommandBuffer ResetCommandBuffer; void _delete_threaded_object (VkvgDevice dev, vkvg_device_thread_items_t* throbjs) { vkvg_buffer_destroy (&throbjs->uboGrad); + /*VkDescriptorSet dss[] = {throbjs->dsFont, throbjs->dsSrc}; + vkFreeDescriptorSets (dev->vkDev, throbjs->descriptorPool, 2, dss);*/ + vkDestroyDescriptorPool (dev->vkDev, throbjs->descriptorPool,NULL); free (throbjs); @@ -83,6 +86,7 @@ vkvg_device_thread_items_t* _get_or_create_threaded_objects (VkvgDevice dev, thr } tmp = (vkvg_device_thread_items_t*)calloc(1, sizeof(vkvg_device_thread_items_t)); tmp->id = thrd_current(); + tmp->dev = dev->vkDev; const VkDescriptorPoolSize descriptorPoolSize[] = { {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2 }, @@ -100,6 +104,10 @@ vkvg_device_thread_items_t* _get_or_create_threaded_objects (VkvgDevice dev, thr .descriptorSetCount = 1, .pSetLayouts = &dev->dslGrad }; VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &tmp->dsGrad)); + descriptorSetAllocateInfo.pSetLayouts = &dev->dslFont; + VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &tmp->dsFont)); + descriptorSetAllocateInfo.pSetLayouts = &dev->dslSrc; + VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &tmp->dsSrc)); vkvg_buffer_create (dev, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, @@ -117,10 +125,32 @@ vkvg_device_thread_items_t* _get_or_create_threaded_objects (VkvgDevice dev, thr }; vkUpdateDescriptorSets(dev->vkDev, 1, &writeDescriptorSet, 0, NULL); + _update_descriptor_set (tmp, dev->fontCache->texture, tmp->dsFont); + _update_descriptor_set (tmp, dev->emptyImg, tmp->dsSrc); + +#if defined(DEBUG) && defined (VKVG_DBG_UTILS) + vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_POOL, (uint64_t)tmp->descriptorPool, "CTX Descriptor Pool"); + vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)tmp->dsSrc, "CTX DescSet SOURCE"); + vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)tmp->dsFont, "CTX DescSet FONT"); + vkh_device_set_object_name((VkhDevice)dev, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)tmp->dsGrad, "CTX DescSet GRADIENT"); +#endif + _add_threaded_objects(dev, tmp); return tmp; } +void _update_descriptor_set (vkvg_device_thread_items_t* ctx, VkhImage img, VkDescriptorSet ds){ + VkDescriptorImageInfo descSrcTex = vkh_image_get_descriptor (img, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + VkWriteDescriptorSet writeDescriptorSet = { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstSet = ds, + .dstBinding = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + .pImageInfo = &descSrcTex + }; + vkUpdateDescriptorSets(ctx->dev, 1, &writeDescriptorSet, 0, NULL); +} bool _try_get_phyinfo (VkhPhyInfo* phys, uint32_t phyCount, VkPhysicalDeviceType gpuType, VkhPhyInfo* phy) { for (uint32_t i=0; ilastCtx; while (next != NULL){ - _update_descriptor_set (next, cache->texture, next->dsFont); + _update_descriptor_set (next->th_objs, cache->texture, next->th_objs->dsFont); next = next->pPrev; } _wait_idle(dev); diff --git a/src/vkvg_surface.c b/src/vkvg_surface.c index ed9a19c..c7e18d1 100644 --- a/src/vkvg_surface.c +++ b/src/vkvg_surface.c @@ -161,7 +161,7 @@ VkvgSurface vkvg_surface_create_from_bitmap (VkvgDevice dev, unsigned char* img, ctx->pushConsts.fsq_patternType = (ctx->pushConsts.fsq_patternType & FULLSCREEN_BIT) + VKVG_PATTERN_TYPE_SURFACE; //_update_push_constants (ctx); - _update_descriptor_set (ctx, tmpImg, ctx->dsSrc); + _update_descriptor_set (ctx->th_objs, tmpImg, ctx->th_objs->dsSrc); _ensure_renderpass_is_started (ctx); vkvg_paint (ctx); -- 2.47.3