From 77e4fbddfb6603a1a56b4c4fb482313fa26495ab Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 5 Sep 2019 17:34:12 +0200 Subject: [PATCH] use vertexOffset in drawCommand to be able to flush just before emiting a new draw command if vbo or ibo is too small --- src/vkvg_context.c | 9 +++++---- src/vkvg_context_internal.c | 15 +++++++-------- src/vkvg_context_internal.h | 1 + src/vkvg_fonts.c | 2 +- tests/stroke.c | 21 +++++++++++++++------ 5 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 02e47e8..edb9c19 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -68,6 +68,7 @@ VkvgContext vkvg_create(VkvgSurface surf) ctx->vertCount = 0; ctx->indCount = 0; ctx->curIndStart = 0; + ctx->curVertOffset = 0; VkRect2D scissor = {{0,0},{ctx->pSurf->width,ctx->pSurf->height}}; ctx->bounds = scissor; @@ -568,7 +569,7 @@ void vkvg_stroke_preserve (VkvgContext ctx) while (ptrPath < ctx->pathPtr){ uint ptrCurve = 0; - VKVG_IBO_INDEX_TYPE firstIdx = (VKVG_IBO_INDEX_TYPE)ctx->vertCount; + VKVG_IBO_INDEX_TYPE firstIdx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset); i = ctx->pathes[ptrPath]&PATH_ELT_MASK; LOG(LOG_INFO_PATH, "\tPATH: start = %d; ", ctx->pathes[ptrPath]&PATH_ELT_MASK, ctx->pathes[ptrPath+1]&PATH_ELT_MASK); @@ -607,7 +608,7 @@ void vkvg_stroke_preserve (VkvgContext ctx) _add_vertexf(ctx, cosf(a) * hw + p0.x, sinf(a) * hw + p0.y); a+=step; } - uint32_t p0Idx = ctx->vertCount; + VKVG_IBO_INDEX_TYPE p0Idx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset); for (uint p = firstIdx; p < p0Idx; p++) _add_triangle_indices(ctx, p0Idx+1, p, p+1); firstIdx = p0Idx; @@ -663,7 +664,7 @@ void vkvg_stroke_preserve (VkvgContext ctx) _add_vertex(ctx, v); if (ctx->lineCap == VKVG_LINE_CAP_ROUND){ - firstIdx = ctx->vertCount; + firstIdx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset); float step = M_PIF / hw; float a = acosf(n.x)+ M_PIF_2; if (n.y < 0) @@ -675,7 +676,7 @@ void vkvg_stroke_preserve (VkvgContext ctx) a-=step; } - uint32_t p0Idx = ctx->vertCount-1; + VKVG_IBO_INDEX_TYPE p0Idx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset - 1); for (uint p = firstIdx-1 ; p < p0Idx; p++) _add_triangle_indices(ctx, p+1, p, firstIdx-2); } diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 48e9bf8..4be4550 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -254,9 +254,9 @@ void _create_cmd_buff (VkvgContext ctx){ void _record_draw_cmd (VkvgContext ctx){ if (ctx->indCount == ctx->curIndStart) return; - LOG(LOG_INFO, "RECORD DRAW CMD: ctx = %lu; vertices = %d; indices = %d\n", (ulong)ctx, ctx->vertCount - *((uint32_t*)(ctx->indices.allocInfo.pMappedData + (ctx->curIndStart * sizeof(uint32_t)))), ctx->indCount - ctx->curIndStart); + LOG(LOG_INFO, "RECORD DRAW CMD: ctx = %lu; vertices = %d; indices = %d\n", (ulong)ctx, ctx->vertCount - ctx->indexCache[ctx->curIndStart], ctx->indCount - ctx->curIndStart); _check_cmd_buff_state(ctx); - CmdDrawIndexed(ctx->cmd, ctx->indCount - ctx->curIndStart, 1, ctx->curIndStart, 0, 1); + CmdDrawIndexed(ctx->cmd, ctx->indCount - ctx->curIndStart, 1, ctx->curIndStart, (int32_t)ctx->curVertOffset, 0); #ifdef VKVG_WIRED_DEBUG CmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelineWired); @@ -265,6 +265,7 @@ void _record_draw_cmd (VkvgContext ctx){ #endif ctx->curIndStart = ctx->indCount; + ctx->curVertOffset = ctx->vertCount; } void _clear_attachment (VkvgContext ctx) { @@ -327,9 +328,7 @@ void _end_render_pass (VkvgContext ctx) { memcpy(ctx->vertices.allocInfo.pMappedData, ctx->vertexCache, ctx->vertCount * sizeof (Vertex)); memcpy(ctx->indices.allocInfo.pMappedData, ctx->indexCache, ctx->indCount * sizeof (VKVG_IBO_INDEX_TYPE)); - ctx->vertCount = 0; - ctx->indCount = 0; - ctx->curIndStart = 0; + ctx->vertCount = ctx->indCount = ctx->curIndStart = ctx->curVertOffset = 0; } void _flush_cmd_buff (VkvgContext ctx){ if (!ctx->cmdStarted) @@ -615,7 +614,7 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ bisec = vec2_perp(bisec); bisec = vec2_mult(bisec,lh); - uint32_t idx = ctx->vertCount; + uint32_t idx = ctx->vertCount - ctx->curVertOffset; if (ctx->lineJoin == VKVG_LINE_JOIN_MITER || isCurve){ v.pos = vec2_add(ctx->points[i], bisec); @@ -668,7 +667,7 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ a+=step; } } - uint32_t p0Idx = ctx->vertCount; + uint32_t p0Idx = ctx->vertCount - ctx->curVertOffset; _add_triangle_indices(ctx, idx, idx+2, idx+1); if (cross<0){ for (uint p = idx+2; p < p0Idx; p++) @@ -949,7 +948,7 @@ void _fill_ec (VkvgContext ctx){ uint32_t firstPtIdx = ctx->pathes[ptrPath]&PATH_ELT_MASK; uint32_t lastPtIdx = ctx->pathes[ptrPath+1]&PATH_ELT_MASK; uint32_t pathPointCount = lastPtIdx - firstPtIdx + 1; - uint32_t firstVertIdx = ctx->vertCount; + uint32_t firstVertIdx = ctx->vertCount-ctx->curVertOffset; ear_clip_point ecps[pathPointCount]; uint32_t ecps_count = pathPointCount; diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index b378caf..622cbe8 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -101,6 +101,7 @@ typedef struct _vkvg_context_t { uint32_t indCount; //current indice count uint32_t curIndStart; //last index recorded in cmd buff + uint32_t curVertOffset; //vertex offset in draw indexed command vkvg_buff vertices; //vertex buffer with persistent mapped memory size_t sizeVertices; //reserved size diff --git a/src/vkvg_fonts.c b/src/vkvg_fonts.c index c670f0f..adb05cb 100644 --- a/src/vkvg_fonts.c +++ b/src/vkvg_fonts.c @@ -477,7 +477,7 @@ void _show_text_run (VkvgContext ctx, VkvgText tr) { pen.y - cr->bmpDiff.y + (tr->glyph_pos[i].y_offset >> 6)}; v.pos = p0; - uint32_t firstIdx = ctx->vertCount; + uint32_t firstIdx = ctx->vertCount - ctx->curVertOffset; v.uv.x = cr->bounds.x; v.uv.y = cr->bounds.y; diff --git a/tests/stroke.c b/tests/stroke.c index 3f6d1d5..028e9bc 100644 --- a/tests/stroke.c +++ b/tests/stroke.c @@ -3,22 +3,31 @@ void test(){ VkvgContext ctx = vkvg_create(surf); - vkvg_set_line_width(ctx, 2); + vkvg_set_line_width(ctx, 1); vkvg_set_source_rgba(ctx,1,0,0,1); vkvg_move_to(ctx,200.5,200.5); vkvg_line_to(ctx,400.5,200.5); vkvg_line_to(ctx,400.5,400.5); vkvg_line_to(ctx,200.5,400.5); vkvg_close_path(ctx); - vkvg_stroke_preserve(ctx); - vkvg_set_source_rgba(ctx,0,0.2,0.35,1); - vkvg_fill(ctx); - vkvg_set_source_rgba(ctx,0.5,1,0,1); + vkvg_stroke(ctx); + + vkvg_set_source_rgba(ctx,0,1,0,1); vkvg_move_to(ctx,300.5,300.5); vkvg_line_to(ctx,500.5,300.5); vkvg_line_to(ctx,500.5,500.5); vkvg_line_to(ctx,300.5,500.5); - vkvg_close_path(ctx); + vkvg_stroke(ctx); + + //vkvg_set_source_rgba(ctx,0,0.2,0.35,1); + //vkvg_fill(ctx); + + vkvg_set_source_rgba(ctx,0.5,1,0,1); + vkvg_move_to(ctx,320.5,320.5); + vkvg_line_to(ctx,520.5,320.5); + vkvg_line_to(ctx,520.5,520.5); + vkvg_line_to(ctx,320.5,520.5); + //vkvg_close_path(ctx); vkvg_stroke(ctx); vkvg_set_line_width(ctx, 40); vkvg_set_source_rgba(ctx,0.5,0.6,1,1.0); -- 2.47.3