From 80c7de68ee8c7c8cb2848079ae43c3329fcee381 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Wed, 25 Apr 2018 18:02:18 +0200 Subject: [PATCH] matrix operations on context --- include/vkvg.h | 4 ++++ src/vkvg_context.c | 35 +++++++++++++++++++++++++---------- src/vkvg_context_internal.c | 7 +++++++ src/vkvg_context_internal.h | 1 + 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/include/vkvg.h b/include/vkvg.h index c750591..d1de356 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -181,6 +181,10 @@ void vkvg_restore (VkvgContext ctx); void vkvg_translate (VkvgContext ctx, float dx, float dy); void vkvg_scale (VkvgContext ctx, float sx, float sy); void vkvg_rotate (VkvgContext ctx, float radians); +void vkvg_transform (VkvgContext ctx, const vkvg_matrix_t* matrix); +void vkvg_set_matrix (VkvgContext ctx, const vkvg_matrix_t* matrix); +void vkvg_get_matrix (VkvgContext ctx, const vkvg_matrix_t* matrix); +void vkvg_identity_matrix (VkvgContext ctx); //pattern VkvgPattern vkvg_pattern_create (); diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 6410c32..6653777 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -163,6 +163,10 @@ void vkvg_new_sub_path (VkvgContext ctx){ void vkvg_new_path (VkvgContext ctx){ _clear_path(ctx); } +//path closing is done by setting the endpoint of the path to the same index +//as the start point. +//I'll test if closing by adding a new point with the same x,y as the start point +//would not make more sense. void vkvg_close_path (VkvgContext ctx){ if (_current_path_is_empty(ctx)) return; @@ -748,20 +752,31 @@ void vkvg_restore (VkvgContext ctx){ void vkvg_translate (VkvgContext ctx, float dx, float dy){ vkvg_matrix_translate (&ctx->pushConsts.mat, dx, dy); - ctx->pushConsts.matInv = ctx->pushConsts.mat; - vkvg_matrix_invert (&ctx->pushConsts.matInv); - _update_push_constants (ctx); + _set_mat_inv_and_vkCmdPush (ctx); } void vkvg_scale (VkvgContext ctx, float sx, float sy){ vkvg_matrix_scale (&ctx->pushConsts.mat, sx, sy); - ctx->pushConsts.matInv = ctx->pushConsts.mat; - vkvg_matrix_invert (&ctx->pushConsts.matInv); - _update_push_constants (ctx); + _set_mat_inv_and_vkCmdPush (ctx); } void vkvg_rotate (VkvgContext ctx, float radians){ vkvg_matrix_rotate (&ctx->pushConsts.mat, radians); - ctx->pushConsts.matInv = ctx->pushConsts.mat; - vkvg_matrix_invert (&ctx->pushConsts.matInv); - _update_push_constants (ctx); + _set_mat_inv_and_vkCmdPush (ctx); +} +void vkvg_transform (VkvgContext ctx, const vkvg_matrix_t* matrix) { + vkvg_matrix_t res; + vkvg_matrix_multiply (&res, &ctx->pushConsts.mat, matrix); + ctx->pushConsts.mat = res; + _set_mat_inv_and_vkCmdPush (ctx); +} +void vkvg_identity_matrix (VkvgContext ctx) { + vkvg_matrix_t im = VKVG_IDENTITY_MATRIX; + ctx->pushConsts.mat = im; + _set_mat_inv_and_vkCmdPush (ctx); +} +void vkvg_set_matrix (VkvgContext ctx, const vkvg_matrix_t* matrix){ + ctx->pushConsts.mat = (*matrix); + _set_mat_inv_and_vkCmdPush (ctx); +} +void vkvg_get_matrix (VkvgContext ctx, const vkvg_matrix_t* matrix){ + memcpy (matrix, &ctx->pushConsts.mat, sizeof(vkvg_matrix_t)); } - diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index fe2669e..5c3c3c5 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -248,6 +248,13 @@ void _init_cmd_buff (VkvgContext ctx){ _update_push_constants (ctx); } +//compute inverse mat used in shader when context matrix has changed +//then trigger push constants command +void _set_mat_inv_and_vkCmdPush (VkvgContext ctx) { + ctx->pushConsts.matInv = ctx->pushConsts.mat; + vkvg_matrix_invert (&ctx->pushConsts.matInv); + _update_push_constants (ctx); +} inline void _update_push_constants (VkvgContext ctx) { vkCmdPushConstants(ctx->cmd, ctx->pSurf->dev->pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(push_constants),&ctx->pushConsts); diff --git a/src/vkvg_context_internal.h b/src/vkvg_context_internal.h index 995dcf7..ba657f8 100644 --- a/src/vkvg_context_internal.h +++ b/src/vkvg_context_internal.h @@ -177,6 +177,7 @@ void _submit_wait_and_reset_cmd(VkvgContext ctx); void _submit_ctx_cmd (VkvgContext ctx); void _wait_and_reset_ctx_cmd(VkvgContext ctx); void _update_push_constants (VkvgContext ctx); +void _set_mat_inv_and_vkCmdPush (VkvgContext ctx); void _createDescriptorPool (VkvgContext ctx); void _init_descriptor_sets (VkvgContext ctx); -- 2.47.3