From: Jean-Philippe Bruyère Date: Mon, 2 Sep 2019 13:07:47 +0000 (+0200) Subject: using host cached vbo and ibo, memcpy before draw X-Git-Tag: v0.1-alpha~59 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=2c4b587a0cbca1fcd50c1edd5a91af9a54c06fba;p=jp%2Fvkvg.git using host cached vbo and ibo, memcpy before draw --- diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 92849c2..2cdd7cc 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -194,6 +194,9 @@ void vkvg_destroy (VkvgContext ctx) vkvg_buffer_destroy (&ctx->indices); vkvg_buffer_destroy (&ctx->vertices); + free(ctx->vertexCache); + free(ctx->indexCache); + //vkh_image_destroy (ctx->source); free(ctx->selectedFont.fontFile); @@ -679,7 +682,7 @@ void vkvg_stroke_preserve (VkvgContext ctx) iR = ctx->pathes[ptrPath]&PATH_ELT_MASK; _build_vb_step(ctx,v,hw,iL,i,iR, false); - uint32_t* inds = (uint32_t*)(ctx->indices.allocInfo.pMappedData + ((ctx->indCount-6) * sizeof(uint32_t))); + uint32_t* inds = &ctx->indexCache [ctx->indCount-6]; uint32_t ii = firstIdx; inds[1] = ii; inds[4] = ii; diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index c25528a..bd9c30d 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -140,6 +140,10 @@ void _create_gradient_buff (VkvgContext ctx){ sizeof(vkvg_gradient_t), &ctx->uboGrad); } void _create_vertices_buff (VkvgContext ctx){ + + ctx->vertexCache = (Vertex*)malloc(ctx->sizeVertices * sizeof(Vertex)); + ctx->indexCache = (uint32_t*)malloc(ctx->sizeVertices * sizeof(uint32_t)); + vkvg_buffer_create (ctx->pSurf->dev, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU, @@ -151,23 +155,21 @@ void _create_vertices_buff (VkvgContext ctx){ } const vec3 blankuv = {}; void _add_vertexf (VkvgContext ctx, float x, float y){ - Vertex* pVert = (Vertex*)(ctx->vertices.allocInfo.pMappedData + ctx->vertCount * sizeof(Vertex)); + Vertex* pVert = &ctx->vertexCache[ctx->vertCount]; pVert->pos.x = x; pVert->pos.y = y; pVert->uv = blankuv; ctx->vertCount++; } void _add_vertex(VkvgContext ctx, Vertex v){ - Vertex* pVert = (Vertex*)(ctx->vertices.allocInfo.pMappedData + ctx->vertCount * sizeof(Vertex)); - *pVert = v; + ctx->vertexCache[ctx->vertCount] = v; ctx->vertCount++; } void _set_vertex(VkvgContext ctx, uint32_t idx, Vertex v){ - Vertex* pVert = (Vertex*)(ctx->vertices.allocInfo.pMappedData + idx * sizeof(Vertex)); - *pVert = v; + ctx->vertexCache[idx] = v; } void _add_tri_indices_for_rect (VkvgContext ctx, uint32_t i){ - uint32_t* inds = (uint32_t*)(ctx->indices.allocInfo.pMappedData + (ctx->indCount * sizeof(uint32_t))); + uint32_t* inds = &ctx->indexCache[ctx->indCount]; inds[0] = i; inds[1] = i+2; inds[2] = i+1; @@ -177,7 +179,7 @@ void _add_tri_indices_for_rect (VkvgContext ctx, uint32_t i){ ctx->indCount+=6; } void _add_triangle_indices(VkvgContext ctx, uint32_t i0, uint32_t i1, uint32_t i2){ - uint32_t* inds = (uint32_t*)(ctx->indices.allocInfo.pMappedData + (ctx->indCount * sizeof(uint32_t))); + uint32_t* inds = &ctx->indexCache[ctx->indCount]; inds[0] = i0; inds[1] = i1; inds[2] = i2; @@ -192,7 +194,7 @@ void _vao_add_rectangle (VkvgContext ctx, float x, float y, float width, float h {{x+width,y+height},{0,0,-1}} }; uint32_t firstIdx = ctx->vertCount; - Vertex* pVert = (Vertex*)(ctx->vertices.allocInfo.pMappedData + ctx->vertCount * sizeof(Vertex)); + Vertex* pVert = &ctx->vertexCache[ctx->vertCount]; memcpy (pVert,v,4*sizeof(Vertex)); ctx->vertCount+=4; _add_tri_indices_for_rect(ctx, firstIdx); @@ -269,6 +271,10 @@ void _end_render_pass (VkvgContext ctx) { ctx->renderPassBeginInfo.renderPass = ctx->pSurf->dev->renderPass; } void _flush_cmd_buff (VkvgContext ctx){ + + memcpy(ctx->vertices.allocInfo.pMappedData, ctx->vertexCache, ctx->vertCount * sizeof (Vertex)); + memcpy(ctx->indices.allocInfo.pMappedData, ctx->indexCache, ctx->indCount * sizeof (uint32_t)); + if (!ctx->cmdStarted) return; _end_render_pass (ctx); @@ -529,7 +535,7 @@ void add_line(vkvg_context* ctx, vec2 p1, vec2 p2, vec4 col){ _add_vertex(ctx, v); v.pos = p2; _add_vertex(ctx, v); - uint32_t* inds = (uint32_t*)(ctx->indices.allocInfo.pMappedData + (ctx->indCount * sizeof(uint32_t))); + uint32_t* inds = &ctx->indexCache [ctx->indCount]; inds[0] = ctx->vertCount - 2; inds[1] = ctx->vertCount - 1; ctx->indCount+=2; diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index 27cba31..d844741 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -104,6 +104,10 @@ typedef struct _vkvg_context_t { size_t sizeVertices; //reserved size uint32_t vertCount; //effective vertices count + Vertex* vertexCache; + uint32_t* indexCache; + + //pathes, exists until stroke of fill vec2* points; //points array size_t sizePoints; //reserved size diff --git a/src/vkvg_device_internal.c b/src/vkvg_device_internal.c index d9b4000..98c7eba 100644 --- a/src/vkvg_device_internal.c +++ b/src/vkvg_device_internal.c @@ -370,6 +370,9 @@ void _createDescriptorSetLayout (VkvgDevice dev) { VK_CHECK_RESULT(vkCreatePipelineLayout(dev->vkDev, &pipelineLayoutCreateInfo, NULL, &dev->pipelineLayout)); } +void _wait_idle (VkvgDevice dev) { + vkDeviceWaitIdle (dev->vkDev); +} void _wait_and_reset_device_fence (VkvgDevice dev) { vkWaitForFences (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX); vkResetFences (dev->vkDev, 1, &dev->fence); diff --git a/src/vkvg_device_internal.h b/src/vkvg_device_internal.h index 49f4ffa..3d27f44 100644 --- a/src/vkvg_device_internal.h +++ b/src/vkvg_device_internal.h @@ -107,6 +107,7 @@ VkRenderPass _createRenderPassNoResolve(VkvgDevice dev, VkAttachmentLoadOp loadO void _setupPipelines (VkvgDevice dev); void _createDescriptorSetLayout (VkvgDevice dev); void _flush_all_contexes (VkvgDevice dev); +void _wait_idle (VkvgDevice dev); void _wait_and_reset_device_fence (VkvgDevice dev); void _submit_cmd (VkvgDevice dev, VkCommandBuffer* cmd, VkFence fence); #endif