]> O.S.I.I.S - jp/vkvg.git/commitdiff
test vkClearAttachment, comments, pipelineCache use (should save and restore)
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 12 Jun 2018 09:44:38 +0000 (11:44 +0200)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 12 Jun 2018 09:44:38 +0000 (11:44 +0200)
include/vkvg.h
src/vkvg_context.c
src/vkvg_context_internal.c
src/vkvg_context_internal.h
src/vkvg_device.c
src/vkvg_device_internal.c
src/vkvg_device_internal.h
src/vkvg_surface.c

index 966a74c7e3bb017957b8fb51cfd0a7035967ebdf..359db621a7ee817c019487097a180f9f692714e4 100644 (file)
@@ -211,6 +211,7 @@ void vkvg_stroke_preserve   (VkvgContext ctx);
 void vkvg_fill                         (VkvgContext ctx);
 void vkvg_fill_preserve                (VkvgContext ctx);
 void vkvg_paint             (VkvgContext ctx);
+void vkvg_clear             (VkvgContext ctx);//use vkClearAttachment to speed up clearing surf
 void vkvg_reset_clip        (VkvgContext ctx);
 void vkvg_clip              (VkvgContext ctx);
 void vkvg_clip_preserve     (VkvgContext ctx);
index 7e41fc1b1509d980137d6bf92d5ebb7d7c8b820a..8c98b0f8ab04bf994c81e448957c5858c18678e5 100644 (file)
@@ -56,6 +56,9 @@ VkvgContext vkvg_create(VkvgSurface surf)
     };
     ctx->pushConsts = pc;
 
+    const VkClearRect cr = {{{0},{ctx->pSurf->width, ctx->pSurf->height}},0,1};
+    ctx->clearRect = cr;
+
     ctx->pPrev          = surf->dev->lastCtx;
     if (ctx->pPrev != NULL)
         ctx->pPrev->pNext = ctx;
@@ -322,12 +325,19 @@ void vkvg_rectangle (VkvgContext ctx, float x, float y, float w, float h){
 
     vkvg_close_path (ctx);
 }
+const VkClearAttachment clearStencil        = {VK_IMAGE_ASPECT_STENCIL_BIT, 1, {0}};
+const VkClearAttachment clearColorAttach    = {VK_IMAGE_ASPECT_COLOR_BIT,   0, {0}};
 
 void vkvg_reset_clip (VkvgContext ctx){
-    _flush_cmd_buff(ctx);
-    _clear_surface(ctx->pSurf, VK_IMAGE_ASPECT_STENCIL_BIT);
-    _init_cmd_buff(ctx);
+    _check_cmd_buff_state (ctx);
+    vkCmdClearAttachments(ctx->cmd, 1, &clearStencil, 1, &ctx->clearRect);
 }
+void vkvg_clear (VkvgContext ctx){
+    _check_cmd_buff_state (ctx);
+    VkClearAttachment ca[2] = {clearColorAttach, clearStencil};
+    vkCmdClearAttachments(ctx->cmd, 2, ca, 1, &ctx->clearRect);
+}
+
 void vkvg_clip (VkvgContext ctx){
     vkvg_clip_preserve(ctx);
     _clear_path(ctx);
index 2b6a29387fd1a93144e249f05213329c6165abaa..18ba7583655378e70171117c050a3a011c0271fe 100644 (file)
@@ -172,6 +172,8 @@ void _vao_add_rectangle (VkvgContext ctx, float x, float y, float width, float h
     ctx->vertCount+=4;
     _add_tri_indices_for_rect(ctx, firstIdx);
 }
+
+
 void _create_cmd_buff (VkvgContext ctx){
     ctx->cmd = vkh_cmd_buff_create(ctx->pSurf->dev, ctx->cmdPool,VK_COMMAND_BUFFER_LEVEL_PRIMARY);
 }
@@ -190,7 +192,9 @@ void _record_draw_cmd (VkvgContext ctx){
 
     ctx->curIndStart = ctx->indCount;
 }
+void _clear_attachment (VkvgContext ctx) {
 
+}
 inline void _submit_ctx_cmd(VkvgContext ctx){
     vkh_cmd_submit (ctx->pSurf->dev->gQueue, &ctx->cmd, ctx->flushFence);
 }
@@ -292,7 +296,7 @@ void _start_cmd_for_render_pass (VkvgContext ctx) {
 
         vkh_image_set_layout(ctx->cmd, ctx->pSurf->img, VK_IMAGE_ASPECT_COLOR_BIT,
                          VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
-                         VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
+                         VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
     }
 
     vkCmdBeginRenderPass (ctx->cmd, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
index 38a9cde33ebf7b8e15231fea5c721b34d94a8933..e0282d703fcbbb91b5acf6b54d0b3cebdde5cece 100644 (file)
@@ -78,13 +78,13 @@ typedef struct _vkvg_context_save_t{
 }vkvg_context_save_t;
 
 typedef struct _vkvg_context_t {
-    VkvgContext     pPrev;      //double linked list of contexts
-    VkvgContext     pNext;
-    uint32_t        references;
+    VkvgContext         pPrev;      //double linked list of contexts
+    VkvgContext         pNext;
+    uint32_t            references;
 
-    VkvgSurface                pSurf;
-    VkFence                    flushFence;
-    VkhImage        source;     //source of painting operation
+    VkvgSurface         pSurf;
+    VkFence             flushFence;
+    VkhImage            source;     //source of painting operation
 
     VkCommandPool              cmdPool;//local pools ensure thread safety
     VkCommandBuffer     cmd;    //single cmd buff for context operations
@@ -127,14 +127,16 @@ typedef struct _vkvg_context_t {
     vkvg_line_cap_t     lineCap;
     vkvg_line_join_t    lineJoin;
 
-    _vkvg_font_t  selectedFont;     //hold current face and size before cache addition
-    _vkvg_font_t* currentFont;      //font ready for lookup
-    vkvg_direction_t textDirection;
+    _vkvg_font_t        selectedFont;     //hold current face and size before cache addition
+    _vkvg_font_t*       currentFont;      //font pointing to cached fonts ready for lookup
+    vkvg_direction_t    textDirection;
 
-    push_constants  pushConsts;
-    VkvgPattern     pattern;
+    push_constants      pushConsts;
+    VkvgPattern         pattern;
 
     vkvg_context_save_t* pSavedCtxs;//last ctx saved ptr
+
+    VkClearRect         clearRect;
 }vkvg_context;
 
 bool _current_path_is_empty (VkvgContext ctx);
index 7cbc8d57c2e50ad542c88e4f71b296f56cd5a17b..072d4eb281aea1b4970d7647529b75c0bc004ae6 100644 (file)
@@ -55,7 +55,7 @@ VkvgDevice vkvg_device_create(VkPhysicalDevice phy, VkDevice vkdev, uint32_t qFa
     dev->cmd    = vkh_cmd_buff_create       (dev, dev->cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
     dev->fence  = vkh_fence_create_signaled (dev);
 
-    //_create_pipeline_cache      (dev);
+    _create_pipeline_cache      (dev);
     _init_fonts_cache           (dev);
     _setupRenderPass            (dev);
     _createDescriptorSetLayout  (dev);
@@ -85,11 +85,13 @@ void vkvg_device_destroy (VkvgDevice dev)
     vkDestroyPipeline               (dev->vkDev, dev->pipe_SUB,     NULL);
     vkDestroyPipeline               (dev->vkDev, dev->pipe_CLEAR,   NULL);
 
+#if DEBUG
     vkDestroyPipeline               (dev->vkDev, dev->pipelineWired, NULL);
     vkDestroyPipeline               (dev->vkDev, dev->pipelineLineList, NULL);
+#endif
 
     vkDestroyPipelineLayout         (dev->vkDev, dev->pipelineLayout, NULL);
-    //vkDestroyPipelineCache          (dev->vkDev, dev->pipelineCache, NULL);
+    vkDestroyPipelineCache          (dev->vkDev, dev->pipelineCache, NULL);
     vkDestroyRenderPass             (dev->vkDev, dev->renderPass, NULL);
 
     vkWaitForFences                 (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX);
index 63ff8e58184fc28a3539e7f316e63cd6e060fff5..ff4b23cbea26d3bacea0f8b93da184d545c66e2d 100644 (file)
@@ -237,40 +237,41 @@ void _setupPipelines(VkvgDevice dev)
     pipelineCreateInfo.layout = dev->pipelineLayout;
 
 
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipelinePolyFill));
+    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, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipelineClipping));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelineClipping));
 
     dsStateCreateInfo.back = dsStateCreateInfo.front = stencilOpState;
     blendAttachmentState.colorWriteMask=0xf;
     dynamicState.dynamicStateCount = 3;
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipe_OVER));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipe_OVER));
 
     blendAttachmentState.alphaBlendOp = blendAttachmentState.colorBlendOp = VK_BLEND_OP_SUBTRACT;
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipe_SUB));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipe_SUB));
 
     blendAttachmentState.blendEnable = VK_FALSE;
     //rasterizationState.polygonMode = VK_POLYGON_MODE_POINT;
     //shaderStages[1].pName = "op_CLEAR";
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipe_CLEAR));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipe_CLEAR));
 
+
+#if DEBUG
     rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
     inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST;
     shaderStages[1].pName = "main";
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipelineLineList));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelineLineList));
 
     shaderStages[1].module = modFragWired;
-    //pipelineCreateInfo.pStages = shaderStages;
-
     inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
     rasterizationState.polygonMode = VK_POLYGON_MODE_LINE;
-    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, VK_NULL_HANDLE, 1, &pipelineCreateInfo, NULL, &dev->pipelineWired));
+    VK_CHECK_RESULT(vkCreateGraphicsPipelines(dev->vkDev, dev->pipelineCache, 1, &pipelineCreateInfo, NULL, &dev->pipelineWired));
+    vkDestroyShaderModule(dev->vkDev, modFragWired, NULL);
+#endif
 
     vkDestroyShaderModule(dev->vkDev, modVert, NULL);
     vkDestroyShaderModule(dev->vkDev, modFrag, NULL);
-    vkDestroyShaderModule(dev->vkDev, modFragWired, NULL);
 }
 
 void _createDescriptorSetLayout (VkvgDevice dev) {
@@ -299,7 +300,7 @@ void _createDescriptorSetLayout (VkvgDevice dev) {
     VK_CHECK_RESULT(vkCreatePipelineLayout(dev->vkDev, &pipelineLayoutCreateInfo, NULL, &dev->pipelineLayout));
 }
 
-void _wait_device_fence (VkvgDevice dev) {
+void _wait_and_reset_device_fence (VkvgDevice dev) {
     vkWaitForFences (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX);
     vkResetFences (dev->vkDev, 1, &dev->fence);
 }
index 85131063569b2a5e5a4b149c917c63534a79fd4e..bf2fb06e0cf1275f63a237b0a2d7d0bac78bf598 100644 (file)
@@ -42,16 +42,20 @@ typedef struct _vkvg_device_t{
     uint32_t                references;
     VkCommandPool                      cmdPool;
     VkCommandBuffer         cmd;
+    //this fence is kept signaled when idle, wait and reset are called before each recording.
     VkFence                 fence;
 
-    VkPipeline                         pipe_OVER;
+    VkPipeline                         pipe_OVER;  //default operator
     VkPipeline                         pipe_SUB;
-    VkPipeline                         pipe_CLEAR;
+    VkPipeline                         pipe_CLEAR; //clear operator
 
-    VkPipeline                         pipelinePolyFill;
-    VkPipeline                         pipelineClipping;
+    VkPipeline                         pipelinePolyFill;   //
+    VkPipeline                         pipelineClipping;   //to update clip
+
+#if DEBUG
     VkPipeline                         pipelineWired;
     VkPipeline                         pipelineLineList;
+#endif
 
     VkPipelineCache                    pipelineCache;
     VkPipelineLayout           pipelineLayout;
@@ -73,5 +77,5 @@ void _setupPipelines            (VkvgDevice dev);
 void _createDescriptorSetLayout (VkvgDevice dev);
 void _flush_all_contexes        (VkvgDevice dev);
 void _init_all_contexes         (VkvgDevice dev);
-void _wait_device_fence         (VkvgDevice dev);
+void _wait_and_reset_device_fence         (VkvgDevice dev);
 #endif
index f8d89876d48250441a3ccaf4eff04efcc7396bbb..6e6baf51f35a40409f7c430698bc2bca7d4e39ee 100644 (file)
@@ -34,7 +34,7 @@ void _clear_surface (VkvgSurface surf, VkImageAspectFlags aspect)
     VkvgDevice      dev = surf->dev;
     VkCommandBuffer cmd = dev->cmd;
 
-    _wait_device_fence (dev);
+    _wait_and_reset_device_fence (dev);
 
     vkh_cmd_begin (cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
 
@@ -123,6 +123,7 @@ VkvgSurface vkvg_surface_create(VkvgDevice dev, uint32_t width, uint32_t height)
 
     return surf;
 }
+//TODO: it would be better to blit in original size and create ms final image with dest surf dims
 VkvgSurface vkvg_surface_create_from_bitmap (VkvgDevice dev, unsigned char* img, uint32_t width, uint32_t height) {
     VkvgSurface surf = (vkvg_surface*)calloc(1,sizeof(vkvg_surface));
 
@@ -151,13 +152,9 @@ VkvgSurface vkvg_surface_create_from_bitmap (VkvgDevice dev, unsigned char* img,
 
     memcpy (buff.allocInfo.pMappedData, img, imgSize);
 
-    /*unsigned char* mapImg = vkh_image_map (stagImg);
-    memcpy (mapImg, img, imgSize);
-    vkh_image_unmap (stagImg);*/
-
     VkCommandBuffer cmd = dev->cmd;
 
-    _wait_device_fence (dev);
+    _wait_and_reset_device_fence (dev);
 
     vkh_cmd_begin (cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
     vkh_image_set_layout (cmd, stagImg, VK_IMAGE_ASPECT_COLOR_BIT,
@@ -195,6 +192,7 @@ VkvgSurface vkvg_surface_create_from_bitmap (VkvgDevice dev, unsigned char* img,
     vkh_cmd_end     (cmd);
     vkh_cmd_submit  (dev->gQueue, &cmd, dev->fence);
 
+    //don't reset fence after completion as this is the last cmd. (signaled idle fence)
     vkWaitForFences (dev->vkDev, 1, &dev->fence, VK_TRUE, UINT64_MAX);
 
     vkvg_buffer_destroy (&buff);
@@ -290,7 +288,7 @@ void vkvg_surface_write_to_png (VkvgSurface surf, const char* path){
                                          VK_IMAGE_USAGE_TRANSFER_SRC_BIT|VK_IMAGE_USAGE_TRANSFER_DST_BIT);
 
     VkCommandBuffer cmd = dev->cmd;
-    _wait_device_fence (dev);
+    _wait_and_reset_device_fence (dev);
 
     vkh_cmd_begin (cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
     vkh_image_set_layout (cmd, stagImg, VK_IMAGE_ASPECT_COLOR_BIT,
@@ -312,7 +310,7 @@ void vkvg_surface_write_to_png (VkvgSurface surf, const char* path){
 
     vkh_cmd_end     (cmd);
     vkh_cmd_submit  (dev->gQueue, &cmd, dev->fence);
-    _wait_device_fence (dev);
+    _wait_and_reset_device_fence (dev);
 
     void* img = vkh_image_map (stagImg);