From b3a953084161b4c43b4c1964d49ef17df8266405 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 24 Apr 2018 22:41:49 +0200 Subject: [PATCH] fill poly with stencil tests --- src/vkvg_context.c | 61 +++++++++++-------------------------- src/vkvg_context_internal.c | 9 +++--- src/vkvg_device.c | 1 + src/vkvg_device_internal.c | 9 ++++-- src/vkvg_device_internal.h | 1 + 5 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index cd59d27..1476c89 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -75,6 +75,10 @@ VkvgContext vkvg_create(VkvgSurface surf) _init_descriptor_sets (ctx); _update_descriptor_set (ctx, ctx->pSurf->dev->fontCache->cacheTex, ctx->dsFont); _update_gradient_desc_set(ctx); + + //add full screen quad at the beginning, recurently used + _vkvg_fill_rectangle (ctx,0,0,ctx->pSurf->width,ctx->pSurf->height); + _init_cmd_buff (ctx); _clear_path (ctx); @@ -339,6 +343,8 @@ void vkvg_fill_preserve (VkvgContext ctx){ if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount)//flush if vk buff is full vkvg_flush(ctx); + vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelinePolyFill); + uint32_t ptrPath = 0;; Vertex v = {}; v.uv.z = -1; @@ -353,54 +359,21 @@ void vkvg_fill_preserve (VkvgContext ctx){ uint32_t pathPointCount = lastPtIdx - ctx->pathes[ptrPath] + 1; uint32_t firstVertIdx = ctx->vertCount; - ear_clip_point ecps[pathPointCount]; - uint32_t ecps_count = pathPointCount; - uint32_t i = 0; - //init points link list - while (i < pathPointCount-1){ - v.pos = ctx->points[i+firstPtIdx]; - ear_clip_point ecp = {v.pos, i+firstVertIdx, &ecps[i+1]}; - ecps[i] = ecp; - _add_vertex(ctx, v); - i++; + for (int i = 0; i < pathPointCount; i++) { + v.pos = ctx->points[i+firstPtIdx]; + _add_vertex(ctx, v); } - v.pos = ctx->points[i+firstPtIdx]; - ear_clip_point ecp = {v.pos, i+firstVertIdx, ecps}; - ecps[i] = ecp; - _add_vertex(ctx, v); - - ear_clip_point* ecp_current = ecps; - - while (ecps_count > 3) { - ear_clip_point* v0 = ecp_current->next, - *v1 = ecp_current, *v2 = ecp_current->next->next; - if (ecp_zcross (v0, v2, v1)<0){ - ecp_current = ecp_current->next; - continue; - } - ear_clip_point* vP = v2->next; - bool isEar = true; - while (vP!=v1){ - if (ptInTriangle (vP->pos, v0->pos, v2->pos, v1->pos)){ - isEar = false; - break; - } - vP = vP->next; - } - if (isEar){ - _add_triangle_indices (ctx, v0->idx, v1->idx, v2->idx); - v1->next = v2; - ecps_count --; - }else - ecp_current = ecp_current->next; - } - if (ecps_count == 3) - _add_triangle_indices(ctx, ecp_current->next->idx, ecp_current->idx, ecp_current->next->next->idx); + + vkCmdDraw (ctx->cmd,pathPointCount,1,firstVertIdx,0); ptrPath+=2; } - _record_draw_cmd(ctx); + + vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipeline); + vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, 0xff); + vkCmdDrawIndexed (ctx->cmd,6,1,0,0,0); + vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, 0x0); } void vkvg_fill (VkvgContext ctx){ vkvg_fill_preserve(ctx); @@ -545,7 +518,7 @@ void _vkvg_fill_rectangle (VkvgContext ctx, float x, float y, float width, float } void vkvg_paint (VkvgContext ctx){ _vkvg_fill_rectangle (ctx, 0, 0, ctx->pSurf->width, ctx->pSurf->height); - _record_draw_cmd (ctx); + _record_draw_cmd(ctx); } inline void vkvg_set_source_rgb (VkvgContext ctx, float r, float g, float b) { diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 3af9c4e..b2a0f55 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -185,10 +185,6 @@ void _explicit_ms_resolve (VkvgContext ctx){ } void _flush_cmd_buff (VkvgContext ctx){ - if (ctx->indCount == 0){ - vkResetCommandBuffer(ctx->cmd,0); - return; - } _record_draw_cmd (ctx); vkCmdEndRenderPass (ctx->cmd); //_explicit_ms_resolve (ctx); @@ -197,7 +193,10 @@ void _flush_cmd_buff (VkvgContext ctx){ _submit_wait_and_reset_cmd(ctx); } void _init_cmd_buff (VkvgContext ctx){ - ctx->vertCount = ctx->indCount = ctx->curIndStart = 0; + //full surf quad triangles is at the beginning + ctx->vertCount = 4; + ctx->indCount = 6; + ctx->curIndStart = 6; //VkClearValue clearValues[2]; //clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; //clearValues[1].depthStencil = { 1.0f, 0 }; diff --git a/src/vkvg_device.c b/src/vkvg_device.c index f7de9b6..5734fd1 100644 --- a/src/vkvg_device.c +++ b/src/vkvg_device.c @@ -58,6 +58,7 @@ void vkvg_device_destroy (VkvgDevice dev) vkDestroyDescriptorSetLayout (dev->vkDev, dev->dslFont,NULL); vkDestroyDescriptorSetLayout (dev->vkDev, dev->dslSrc, NULL); + vkDestroyPipeline (dev->vkDev, dev->pipelinePolyFill, NULL); vkDestroyPipeline (dev->vkDev, dev->pipeline, NULL); vkDestroyPipeline (dev->vkDev, dev->pipelineClipping, NULL); vkDestroyPipeline (dev->vkDev, dev->pipeline_OP_SUB, NULL); diff --git a/src/vkvg_device_internal.c b/src/vkvg_device_internal.c index 399fae8..67cafdf 100644 --- a/src/vkvg_device_internal.c +++ b/src/vkvg_device_internal.c @@ -132,7 +132,7 @@ void _setupPipelines(VkvgDevice dev) .renderPass = dev->renderPass }; VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, - .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST }; + .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN }; VkPipelineRasterizationStateCreateInfo rasterizationState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, .polygonMode = VK_POLYGON_MODE_FILL, @@ -158,8 +158,9 @@ void _setupPipelines(VkvgDevice dev) .pAttachments = &blendAttachmentState }; /*failOp,passOp,depthFailOp,compareOp, compareMask, writeMask, reference;*/ + VkStencilOpState polyFillOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_INVERT,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0x0,0xf,0}; VkStencilOpState clipingOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_INCREMENT_AND_CLAMP,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0x0,0xf,0}; - VkStencilOpState stencilOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_REPLACE,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0xf,0x0,0}; + VkStencilOpState stencilOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0xf,0xf,0}; VkPipelineDepthStencilStateCreateInfo dsStateCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, .depthTestEnable = VK_FALSE, @@ -255,9 +256,13 @@ void _setupPipelines(VkvgDevice dev) VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelineClipping)); + dsStateCreateInfo.back = dsStateCreateInfo.front = polyFillOpState; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelinePolyFill)); + //dsStateCreateInfo.back.writeMask = dsStateCreateInfo.front.writeMask = 0; dsStateCreateInfo.back = dsStateCreateInfo.front = stencilOpState; blendAttachmentState.colorWriteMask=0xf; + inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipeline)); blendAttachmentState.alphaBlendOp = blendAttachmentState.colorBlendOp = VK_BLEND_OP_SUBTRACT; diff --git a/src/vkvg_device_internal.h b/src/vkvg_device_internal.h index 768e038..3a0b56d 100644 --- a/src/vkvg_device_internal.h +++ b/src/vkvg_device_internal.h @@ -39,6 +39,7 @@ typedef struct _vkvg_device_t{ VkFence fence; VkPipeline pipeline; + VkPipeline pipelinePolyFill; VkPipeline pipelineClipping; VkPipeline pipeline_OP_SUB; VkPipeline pipelineWired; -- 2.47.3