From a5b66f3e1c4a3e21de0d9362181d32c2c40d2bc3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Wed, 25 Apr 2018 21:14:07 +0200 Subject: [PATCH] update clipping with new poly filling --- src/vkvg_context.c | 64 ++++++++++++++++++++++--------------- src/vkvg_context_internal.c | 3 +- src/vkvg_context_internal.h | 13 ++------ src/vkvg_device_internal.c | 26 +++++++++------ src/vkvg_device_internal.h | 4 +++ 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 6653777..a4707da 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -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; diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 5c3c3c5..ed1bb27 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -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); } diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index ba657f8..7d02728 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -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, diff --git a/src/vkvg_device_internal.c b/src/vkvg_device_internal.c index 67cafdf..84021a9 100644 --- a/src/vkvg_device_internal.c +++ b/src/vkvg_device_internal.c @@ -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; diff --git a/src/vkvg_device_internal.h b/src/vkvg_device_internal.h index 3a0b56d..d3f4505 100644 --- a/src/vkvg_device_internal.h +++ b/src/vkvg_device_internal.h @@ -26,6 +26,10 @@ #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; -- 2.47.3