]> O.S.I.I.S - jp/vkvg.git/commitdiff
fill poly with stencil tests
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 24 Apr 2018 20:41:49 +0000 (22:41 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 24 Apr 2018 20:41:49 +0000 (22:41 +0200)
src/vkvg_context.c
src/vkvg_context_internal.c
src/vkvg_device.c
src/vkvg_device_internal.c
src/vkvg_device_internal.h

index cd59d27a5322fce86d3fc4b0116feab6ed282aa7..1476c89736e7fddfcea5595a2867f6766aef3649 100644 (file)
@@ -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) {
index 3af9c4ecb8ad6ad76f767d549b1884b2b838c4d2..b2a0f557d7690d551c2baba4c10d0004b8bfc8ae 100644 (file)
@@ -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 };
index f7de9b6696b37321ab8bfb9e5dcfcaac01caf784..5734fd1cc542bd393b34c6a6021596197d8d47e7 100644 (file)
@@ -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);
index 399fae8c1ec3cd893a73597adbe648d50e83ee94..67cafdf3f118e36a0616da1525f4655edb5e56bb 100644 (file)
@@ -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;
index 768e03864371f642ee4bdec2bbfe0274068383aa..3a0b56d2c2a8aa271984a337284d69991915031a 100644 (file)
@@ -39,6 +39,7 @@ typedef struct _vkvg_device_t{
     VkFence                 fence;
 
     VkPipeline                         pipeline;
+    VkPipeline                         pipelinePolyFill;
     VkPipeline                         pipelineClipping;
     VkPipeline                         pipeline_OP_SUB;
     VkPipeline                         pipelineWired;