From 907c27715cb40e90271aaf3a2b3732b5c6866a9e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Sat, 14 Apr 2018 05:57:40 +0200 Subject: [PATCH] reset descriptor of bound source when no longer used, update readme --- README.md | 32 ++++++++++++++++++++++++-------- shaders/triangle.vert | 2 +- src/vkvg_context.c | 13 +++++++++++-- src/vkvg_context_internal.c | 18 +++++++++++++++++- src/vkvg_context_internal.h | 1 + src/vkvg_surface.c | 5 +++++ tests/test1.c | 16 ++++++++++++---- vkh | 2 +- 8 files changed, 72 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 294c053..6e08739 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@


- vkvg + vkvg -
-
+
+
Vulkan Vector Graphics -
+

- +

@@ -18,9 +18,9 @@ **vkvg** is a multiplatform **c** library for drawing 2D vector graphics with [Vulkan](https://www.khronos.org/vulkan/). -[Cairo](https://www.cairographics.org/) is missing a Vulkan backend, so I decided to start one myself trying to keep my api as close to Cairo as possible. Maybe vkvg could serve as a starting point for Cairo maintainers to start their Vulkan backend. +[Cairo](https://www.cairographics.org/) was missing a Vulkan backend, so I decided to start one myself trying to keep my api as close to Cairo as possible. Maybe vkvg could serve as a starting point for Cairo maintainers to start their Vulkan backend. -### Current status: +### Current status: vkvg is in early development stage, and no guarantee is given on the possible roadmap: @@ -28,9 +28,25 @@ vkvg is in early development stage, and no guarantee is given on the possible ro - Basic painting operation. - Font system with caching operational. - Context should be thread safe, tests required. +- Image loading with [stb lib](https://github.com/nothings/stb) - Nice logo. +### Requirements: + +- [Vulkan](https://www.khronos.org/vulkan/) +- [FontConfig](https://www.freedesktop.org/wiki/Software/fontconfig/) +- [Freetype](https://www.freetype.org/) +- PkgConfig (currently used only to find harbfbuzz) +- [Harfbuzz](https://www.freedesktop.org/wiki/Software/HarfBuzz/) +- GLSLC: spirv compiler, included in [LunarG SDK](https://www.lunarg.com/vulkan-sdk/) (building only) +- [xxd](https://linux.die.net/man/1/xxd): generate headers with precompiled shaders (building only) +- [GLFW](http://www.glfw.org/) (only for running demo app) +- CMake + ### Roadmap -- Offscreen pattern building. +- Improve triangulation algorithm. +- Offscreen pattern building. +- Matrix transformations. +- SVG rendering. diff --git a/shaders/triangle.vert b/shaders/triangle.vert index c36ca95..4e583c4 100644 --- a/shaders/triangle.vert +++ b/shaders/triangle.vert @@ -31,5 +31,5 @@ void main() }else outSrcRect = pushConsts.source; - gl_Position = vec4(inPos.xy*pushConsts.scale+pushConsts.translate,0.0, 1.0); + gl_Position = vec4(inPos.xy * pushConsts.scale - vec2(1), 0.0, 1.0); } diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 9569b73..856bc43 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -23,7 +23,7 @@ VkvgContext vkvg_create(VkvgSurface surf) push_constants pc = { {}, {2.0f/(float)ctx->pSurf->width,2.0f/(float)ctx->pSurf->height}, - {-1.f,-1.f}, + {0.f,0.f}, VKVG_SRC_SOLID }; ctx->pushConsts = pc; @@ -50,6 +50,8 @@ VkvgContext vkvg_create(VkvgSurface surf) _init_cmd_buff (ctx); _clear_path (ctx); + + return ctx; } void vkvg_flush (VkvgContext ctx){ @@ -429,6 +431,12 @@ void vkvg_paint (VkvgContext ctx){ void vkvg_set_rgba (VkvgContext ctx, float r, float g, float b, float a) { + if (ctx->pushConsts.srcType == VKVG_SRC_PATTERN){ + _flush_cmd_buff (ctx); + _reset_src_descriptor_set (ctx); + _init_cmd_buff (ctx); + } + vec4 c = {r,g,b,a}; ctx->pushConsts.source = c; ctx->pushConsts.srcType = VKVG_SRC_SOLID; @@ -464,7 +472,8 @@ void vkvg_set_source_surface(VkvgContext ctx, VkvgSurface surf, float x, float y ctx->source = surf->img; - vkh_image_create_sampler(ctx->source,VK_FILTER_NEAREST, VK_FILTER_NEAREST, + if (vkh_image_get_sampler(ctx->source) == VK_NULL_HANDLE) + vkh_image_create_sampler(ctx->source,VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST,VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER); if (vkh_image_get_layout (ctx->source) != VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL){ diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 94ac6d0..3d7f317 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -168,9 +168,9 @@ void _init_cmd_buff (VkvgContext ctx){ .renderPass = ctx->pSurf->dev->renderPass, .framebuffer = ctx->pSurf->fb, .renderArea.extent = {ctx->pSurf->width,ctx->pSurf->height}, - .clearValueCount = 4, .pClearValues = clearValues}; + vkh_cmd_begin (ctx->cmd,VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); vkCmdBeginRenderPass (ctx->cmd, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport = {0,0,ctx->pSurf->width,ctx->pSurf->height,0,1}; @@ -231,6 +231,22 @@ void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds){ }; vkUpdateDescriptorSets(ctx->pSurf->dev->vkDev, 1, &writeDescriptorSet, 0, NULL); } + +/* + * Reset currently bound descriptor which image could be destroyed + */ +void _reset_src_descriptor_set (VkvgContext ctx){ + VkvgDevice dev = ctx->pSurf->dev; + //VkDescriptorSet dss[] = {ctx->dsSrc}; + vkFreeDescriptorSets (dev->vkDev, ctx->descriptorPool, 1, &ctx->dsSrc); + + VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = { .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .descriptorPool = ctx->descriptorPool, + .descriptorSetCount = 1, + .pSetLayouts = &dev->dslSrc }; + VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsSrc)); +} + void _createDescriptorPool (VkvgContext ctx) { VkvgDevice dev = ctx->pSurf->dev; VkDescriptorPoolSize descriptorPoolSize = {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2 }; diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index 5399dfc..fd20637 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -140,6 +140,7 @@ uint32_t _get_last_point_of_closed_path (VkvgContext ctx, uint32_t ptrPath); void _createDescriptorPool (VkvgContext ctx); void _init_descriptor_sets (VkvgContext ctx); void _update_descriptor_set (VkvgContext ctx, VkhImage img, VkDescriptorSet ds); +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){ diff --git a/src/vkvg_surface.c b/src/vkvg_surface.c index 2fe0a9a..0e8955c 100644 --- a/src/vkvg_surface.c +++ b/src/vkvg_surface.c @@ -146,6 +146,11 @@ VkvgSurface vkvg_surface_create_from_image (VkvgDevice dev, const char* filePath //create tmp context with rendering pipeline to create the multisample img VkvgContext ctx = vkvg_create (surf); + + VkClearAttachment ca = {VK_IMAGE_ASPECT_COLOR_BIT,0, { 0.0f, 0.0f, 0.0f, 0.0f }}; + VkClearRect cr = {{{0,0},{surf->width,surf->height}},0,1}; + vkCmdClearAttachments(ctx->cmd, 1, &ca, 1, &cr); + vec4 srcRect = {0,0,surf->width,surf->height}; ctx->pushConsts.source = srcRect; ctx->pushConsts.srcType = VKVG_SRC_PATTERN; diff --git a/tests/test1.c b/tests/test1.c index 4dbf7f1..732d481 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -649,7 +649,15 @@ void test_img_surface (VkvgContext ctx) { //VkvgSurface imgSurf = vkvg_surface_create_from_image(device, "/mnt/data/images/path2674.png"); //VkvgSurface imgSurf = vkvg_surface_create_from_image(device, "/mnt/data/images/horse-black-head-shape-of-a-chess-piece_318-52446.jpg"); - vkvg_set_source_surface(ctx, imgSurf, 300, 300); + vkvg_set_source_surface(ctx, imgSurf, 200, 200); + vkvg_paint(ctx); + vkvg_set_source_surface(ctx, imgSurf, 400, 400); + vkvg_paint(ctx); + vkvg_flush(ctx); + vkvg_surface_destroy(imgSurf); + + imgSurf = vkvg_surface_create_from_image(device, "/mnt/data/images/path2674.png"); + vkvg_set_source_surface(ctx, imgSurf, 0, 0); vkvg_paint(ctx); vkvg_flush(ctx); vkvg_surface_destroy(imgSurf); @@ -674,9 +682,7 @@ int main(int argc, char *argv[]) { VkvgSurface surf2 = vkvg_surface_create (device,1024,800);; VkvgContext ctx = vkvg_create(surf2); - //test_img_surface(ctx); - - vkvg_set_rgba(ctx,0.02,0.02,0.1,1); + vkvg_set_rgba(ctx,0.02,0.02,0.3,1.0); //vkvg_paint(ctx); vkvg_rectangle (ctx,0,0,1024,800); vkvg_fill (ctx); @@ -687,6 +693,8 @@ int main(int argc, char *argv[]) { vkvg_test_curves(ctx); test_text(ctx); + //test_img_surface(ctx); + vkvg_destroy(ctx); ctx = vkvg_create(surf); diff --git a/vkh b/vkh index 11a3c03..27fad24 160000 --- a/vkh +++ b/vkh @@ -1 +1 @@ -Subproject commit 11a3c034e013251ae11e39d1bce38119b376db47 +Subproject commit 27fad24142f4cfbbc2e0069bf0446f2f9fafa345 -- 2.47.3