]> O.S.I.I.S - jp/vkvg.git/commitdiff
update clipping with new poly filling
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 25 Apr 2018 19:14:07 +0000 (21:14 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Wed, 25 Apr 2018 19:14:07 +0000 (21:14 +0200)
src/vkvg_context.c
src/vkvg_context_internal.c
src/vkvg_context_internal.h
src/vkvg_device_internal.c
src/vkvg_device_internal.h

index 6653777b45d0ef8824b0b92d3f628864c2ee5c50..a4707da73a299ffa90052ae8138a381ff1dee71f 100644 (file)
@@ -297,19 +297,11 @@ void vkvg_rectangle (VkvgContext ctx, float x, float y, float w, float h){
 
     vkvg_close_path (ctx);
 }
-void vkvg_clip_preserve (VkvgContext ctx){
-    vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelineClipping);
-    vkvg_fill_preserve(ctx);
-    vkvg_flush(ctx);
-    //should test current operator to bind correct pipeline
-    ctx->stencilRef++;
-    vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipeline);
-    vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, ctx->stencilRef);
-}
+
 void vkvg_reset_clip (VkvgContext ctx){
     _flush_cmd_buff(ctx);
     _clear_stencil(ctx->pSurf);
-    ctx->stencilRef=0;
+    //ctx->stencilRef=0;
     _init_cmd_buff(ctx);
 }
 void vkvg_clip (VkvgContext ctx){
@@ -325,14 +317,7 @@ void vkvg_fill (VkvgContext ctx){
     vkvg_fill_preserve(ctx);
     _clear_path(ctx);
 }
-void vkvg_fill_preserve (VkvgContext ctx){
-    if (ctx->pathPtr == 0)      //nothing to fill
-        return;
-    _finish_path(ctx);
-
-    if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount)//flush if vk buff is full
-        vkvg_flush(ctx);
-
+void _poly_fill (VkvgContext ctx){
     vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelinePolyFill);
 
     uint32_t ptrPath = 0;;
@@ -358,11 +343,42 @@ void vkvg_fill_preserve (VkvgContext ctx){
 
         ptrPath+=2;
     }
+}
+void vkvg_clip_preserve (VkvgContext ctx){
+    if (ctx->pathPtr == 0)      //nothing to fill
+        return;
+    _finish_path(ctx);
+
+    if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount)//flush if vk buff is full
+        vkvg_flush(ctx);
+
+    _poly_fill (ctx);
+
+    vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipelineClipping);
+    vkCmdDrawIndexed (ctx->cmd,6,1,0,0,0);
+    //vkvg_flush(ctx);
+    //should test current operator to bind correct pipeline
+    //ctx->stencilRef++;
+    vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipeline);
+    vkCmdSetStencilCompareMask(ctx->cmd, VK_STENCIL_FRONT_AND_BACK, STENCIL_CLIP_BIT);
+    //vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, ctx->stencilRef);
+}
+void vkvg_fill_preserve (VkvgContext ctx){
+    if (ctx->pathPtr == 0)      //nothing to fill
+        return;
+    _finish_path(ctx);
+
+    if (ctx->pointCount * 4 > ctx->sizeIndices - ctx->indCount)//flush if vk buff is full
+        vkvg_flush(ctx);
+
+    _poly_fill (ctx);
 
     vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipeline);
-    vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, 0xff);
+    vkCmdSetStencilCompareMask(ctx->cmd, VK_STENCIL_FRONT_AND_BACK, STENCIL_FILL_BIT);
+    //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);
+    vkCmdSetStencilCompareMask(ctx->cmd, VK_STENCIL_FRONT_AND_BACK, STENCIL_CLIP_BIT);
+    //vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, 0x0);
 }
 void vkvg_stroke_preserve (VkvgContext ctx)
 {
@@ -505,6 +521,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);
+    //vkCmdSetStencilCompareMask(ctx->cmd, VK_STENCIL_FRONT_AND_BACK, 0x2);
     vkCmdDrawIndexed (ctx->cmd,6,1,0,0,0);
 }
 
@@ -601,10 +618,7 @@ void vkvg_set_line_join (VkvgContext ctx, vkvg_line_join_t join){
     ctx->lineJoint = join;
 }
 
-
-
 void vkvg_select_font_face (VkvgContext ctx, const char* name){
-
     _select_font_face (ctx, name);
 }
 void vkvg_set_font_size (VkvgContext ctx, uint32_t size){
@@ -651,7 +665,7 @@ void vkvg_save (VkvgContext ctx){
     VK_CHECK_RESULT(vkEndCommandBuffer(ctx->cmd));
     _submit_ctx_cmd(ctx);
 
-    sav->stencilRef = ctx->stencilRef;
+    //sav->stencilRef = ctx->stencilRef;
     sav->sizePoints = ctx->sizePoints;
     sav->pointCount = ctx->pointCount;
 
@@ -714,7 +728,7 @@ void vkvg_restore (VkvgContext ctx){
     VK_CHECK_RESULT(vkEndCommandBuffer(ctx->cmd));
     _submit_ctx_cmd(ctx);
 
-    ctx->stencilRef = sav->stencilRef;
+    //ctx->stencilRef = sav->stencilRef;
     ctx->sizePoints = sav->sizePoints;
     ctx->pointCount = sav->pointCount;
 
index 5c3c3c571fba65430f5dfeb4198e64beebfad5a0..ed1bb27bd86b8667e0886c2de73fe6e029e60677 100644 (file)
@@ -244,7 +244,8 @@ void _init_cmd_buff (VkvgContext ctx){
     vkCmdBindVertexBuffers(ctx->cmd, 0, 1, &ctx->vertices.buffer, offsets);
     vkCmdBindIndexBuffer(ctx->cmd, ctx->indices.buffer, 0, VK_INDEX_TYPE_UINT32);
     vkCmdBindPipeline(ctx->cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pSurf->dev->pipeline);
-    vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, ctx->stencilRef);
+    vkCmdSetStencilCompareMask(ctx->cmd, VK_STENCIL_FRONT_AND_BACK, 0);
+    //vkCmdSetStencilReference(ctx->cmd,VK_STENCIL_FRONT_AND_BACK, ctx->stencilRef);
 
     _update_push_constants  (ctx);
 }
index ba657f805b523d9f9bc3af95a7d24657bc6fe69a..7d02728f917dbeb55ef578494c4c3db41879792c 100644 (file)
@@ -41,12 +41,6 @@ typedef struct{
     vec3 uv;
 }Vertex;
 
-typedef struct _ear_clip_point{
-    vec2 pos;
-    uint32_t idx;
-    struct _ear_clip_point* next;
-}ear_clip_point;
-
 typedef struct {
     vec4     source;
     vec2     size;
@@ -95,7 +89,7 @@ typedef struct _vkvg_context_t {
 
     VkvgSurface                pSurf;
     VkFence                    flushFence;
-    uint32_t        stencilRef;
+    //uint32_t        stencilRef;
     VkhImage        source;
 
     VkCommandPool              cmdPool;
@@ -182,16 +176,13 @@ void _set_mat_inv_and_vkCmdPush (VkvgContext ctx);
 void _createDescriptorPool  (VkvgContext ctx);
 void _init_descriptor_sets  (VkvgContext ctx);
 void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds);
-void _update_gradient_desc_set (VkvgContext ctx);
+void _update_gradient_desc_set(VkvgContext ctx);
 void _reset_src_descriptor_set(VkvgContext ctx);
 void _free_ctx_save         (vkvg_context_save_t* sav);
 
 static inline float vec2_zcross (vec2 v1, vec2 v2){
     return v1.x*v2.y-v1.y*v2.x;
 }
-static inline float ecp_zcross (ear_clip_point* p0, ear_clip_point* p1, ear_clip_point* p2){
-    return vec2_zcross (vec2_sub (p1->pos, p0->pos), vec2_sub (p2->pos, p0->pos));
-}
 void _recursive_bezier(VkvgContext ctx,
                        float x1, float y1, float x2, float y2,
                        float x3, float y3, float x4, float y4,
index 67cafdf3f118e36a0616da1525f4655edb5e56bb..84021a902d9a2e7bd8fddc112c0c364cde05073d 100644 (file)
@@ -158,25 +158,29 @@ 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_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0xf,0xf,0};
+    /*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_REPLACE,VK_STENCIL_OP_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,STENCIL_FILL_BIT,STENCIL_ALL_BIT,0x2};
+    VkStencilOpState stencilOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,0xf,0xf,0xf};*/
+
+    VkStencilOpState polyFillOpState ={VK_STENCIL_OP_KEEP,VK_STENCIL_OP_INVERT,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,STENCIL_CLIP_BIT,STENCIL_FILL_BIT,0};
+    VkStencilOpState clipingOpState = {VK_STENCIL_OP_REPLACE,VK_STENCIL_OP_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_NOT_EQUAL,STENCIL_FILL_BIT,STENCIL_ALL_BIT,0x2};
+    VkStencilOpState stencilOpState = {VK_STENCIL_OP_KEEP,VK_STENCIL_OP_ZERO,VK_STENCIL_OP_KEEP,VK_COMPARE_OP_EQUAL,STENCIL_FILL_BIT,STENCIL_FILL_BIT,0x1};
 
     VkPipelineDepthStencilStateCreateInfo dsStateCreateInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
                 .depthTestEnable = VK_FALSE,
                 .depthWriteEnable = VK_FALSE,
                 .depthCompareOp = VK_COMPARE_OP_ALWAYS,
                 .stencilTestEnable = VK_TRUE,
-                .front = clipingOpState,
-                .back = clipingOpState };
+                .front = polyFillOpState,
+                .back = polyFillOpState };
 
     VkDynamicState dynamicStateEnables[] = {
         VK_DYNAMIC_STATE_VIEWPORT,
         VK_DYNAMIC_STATE_SCISSOR,
-        VK_DYNAMIC_STATE_STENCIL_REFERENCE,
+        VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
     };
     VkPipelineDynamicStateCreateInfo dynamicState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
-                .dynamicStateCount = 3,
+                .dynamicStateCount = 2,
                 .pDynamicStates = dynamicStateEnables };
 
     VkPipelineViewportStateCreateInfo viewportState = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
@@ -254,15 +258,17 @@ void _setupPipelines(VkvgDevice dev)
     pipelineCreateInfo.pDynamicState = &dynamicState;
     pipelineCreateInfo.layout = dev->pipelineLayout;
 
-    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));
 
+    inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
+    dsStateCreateInfo.back = dsStateCreateInfo.front = clipingOpState;
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelineClipping));
+
     //dsStateCreateInfo.back.writeMask = dsStateCreateInfo.front.writeMask = 0;
     dsStateCreateInfo.back = dsStateCreateInfo.front = stencilOpState;
     blendAttachmentState.colorWriteMask=0xf;
-    inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
+    dynamicState.dynamicStateCount = 3;
     VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipeline));
 
     blendAttachmentState.alphaBlendOp = blendAttachmentState.colorBlendOp = VK_BLEND_OP_SUBTRACT;
index 3a0b56d2c2a8aa271984a337284d69991915031a..d3f4505986e2f7317bc2febdf1f05d5ae813629f 100644 (file)
 #include "vkvg.h"
 #include "vkvg_fonts.h"
 
+#define STENCIL_FILL_BIT    0x1
+#define STENCIL_CLIP_BIT    0x2
+#define STENCIL_ALL_BIT     0x3
+
 typedef struct _vkvg_device_t{
     VkDevice                           vkDev;
     VkPhysicalDeviceMemoryProperties phyMemProps;