From 67a5272ca6c57910cbaedd304e974e12ab50b52d Mon Sep 17 00:00:00 2001 From: ravi688 Date: Thu, 15 Aug 2024 17:02:14 +0530 Subject: [PATCH] [SGE] Cache color ranges and reapply whenever point size changes --- .../sge/internal/vulkan/vulkan_bitmap_text.h | 27 ++++++++------- source/sge/vulkan/vulkan_bitmap_text.c | 33 +++++++++++++++++-- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/include/sge/internal/vulkan/vulkan_bitmap_text.h b/include/sge/internal/vulkan/vulkan_bitmap_text.h index d0f08da2..8c073e87 100644 --- a/include/sge/internal/vulkan/vulkan_bitmap_text.h +++ b/include/sge/internal/vulkan/vulkan_bitmap_text.h @@ -86,6 +86,19 @@ typedef buffer_t /* element_type: u32 */ u32_buffer_t; typedef buf_ucount_t vulkan_bitmap_text_string_handle_t; typedef dictionary_t vulkan_bitmap_glyph_sub_buffer_handle_table_t; +/* the range also takes whitespaces into account */ +typedef struct char_attr_color_range_t +{ + /* inclusive */ + u32 begin; + /* exclusive */ + u32 end; + /* 32-bit x 4 color */ + color_t color; +} char_attr_color_range_t; + +typedef buffer_t /* element_type: char_attr_color_range_t */ char_attr_color_range_buffer_t; + typedef struct vulkan_bitmap_text_string_t { /* handle to the next free string if this string is in the free list or inuse string if this is in the inuse list */ @@ -100,6 +113,9 @@ typedef struct vulkan_bitmap_text_string_t u32_buffer_t index_mappings; /* string */ vulkan_bitmap_text_char_buffer_t chars; + /* populated when vulkan_bitmap_text_string_set_char_attr_color is called, + * and used for applying the color ranges again whenever set_point_size is called. */ + char_attr_color_range_buffer_t color_ranges; /* a rectangle in a 3D space * holds the position and the extents of the text */ struct { offset3d_t offset; extent2d_t extent; } rect; @@ -253,17 +269,6 @@ SGE_API void vulkan_bitmap_text_string_set_point_sizeH(vulkan_bitmap_text_t* tex SGE_API void vulkan_bitmap_text_string_set_transformH(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_handle_t handle, mat4_t transform); SGE_API void vulkan_bitmap_text_string_set_color(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_handle_t handle, color_t color); -/* the range also takes whitespaces into account */ -typedef struct char_attr_color_range_t -{ - /* inclusive */ - u32 begin; - /* exclusive */ - u32 end; - /* 32-bit x 4 color */ - color_t color; -} char_attr_color_range_t; - /* sets color attribute for each characters lying the range list passed to this function * ranges: is the list of ranges where each range consists of 'begin' and 'end' index * range_count: is the number of ranges */ diff --git a/source/sge/vulkan/vulkan_bitmap_text.c b/source/sge/vulkan/vulkan_bitmap_text.c index 83827d08..ab752880 100644 --- a/source/sge/vulkan/vulkan_bitmap_text.c +++ b/source/sge/vulkan/vulkan_bitmap_text.c @@ -199,6 +199,7 @@ SGE_API void vulkan_bitmap_text_destroy(vulkan_bitmap_text_t* text) buf_free(&text_string->chars); buf_free(&text_string->glyph_offsets); buf_free(&text_string->index_mappings); + buf_free(&text_string->color_ranges); } buf_clear(&text->text_strings, NULL); @@ -297,6 +298,7 @@ SGE_API vulkan_bitmap_text_string_handle_t vulkan_bitmap_text_string_create(vulk buf_push_null(&text_string->chars); text_string->glyph_offsets = memory_allocator_buf_new(text->renderer->allocator, f32); text_string->index_mappings = memory_allocator_buf_new(text->renderer->allocator, u32); + text_string->color_ranges = memory_allocator_buf_new(text->renderer->allocator, char_attr_color_range_t); text_string->color = vec4(1.0f, 1.0f, 1.0f, 1.0f); buf_push_pseudo(tst_buffer, 1); } @@ -364,6 +366,7 @@ SGE_API void vulkan_bitmap_text_string_destroyH(vulkan_bitmap_text_t* text, vulk buf_clear(&text_string->chars, NULL); buf_clear(&text_string->glyph_offsets, NULL); buf_clear(&text_string->index_mappings, NULL); + buf_clear(&text_string->color_ranges, NULL); text_string->rect.offset = offset3d(0.0f, 0.0f, 0.0f); text_string->rect.extent = extent2d(500, 300); text_string->transform = mat4_identity(); @@ -538,6 +541,10 @@ static void text_string_set(vulkan_bitmap_text_t* text, vulkan_bitmap_text_strin /* TODO: Remove this as we don't need this level of flexibility */ .rotn = { ((i%3) == 0) ? 1 : 0, ((i%3) == 1) ? 1 : 0, ((i%3) == 2) ? 1 : 0 }, .stid = U64_TO_U32(text_string->handle), + /* TODO: use exisiting color_ranges (if any) to assign color here, otherwise use the default text_string->color + * How? + * you have the index of this character into text_string->chars array + * use that index to find the color_range which contains it, and get the color of that color_range. */ .colr = text_string->color }; @@ -560,11 +567,20 @@ static void text_string_set(vulkan_bitmap_text_t* text, vulkan_bitmap_text_strin // vulkan_bitmap_glyph_atlas_texture_commit(text->texture, NULL); } +static void text_string_set_char_attr_color(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_t* text_string, const char_attr_color_range_t* ranges, u32 range_count); + static void text_string_set_point_size(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_t* text_string, u32 point_size) { text_string->point_size = point_size; if(buf_get_element_count(&text_string->chars) > 0) + { text_string_set(text, text_string, CAST_TO(const char*, buf_get_ptr(&text_string->chars))); + + // reapply the color ranges as the above call to text_string_set() uses the default color set via vulkan_bitmap_text_string_set_color() + u32 range_count = buf_get_element_count(&text_string->color_ranges); + if(range_count > 0) + text_string_set_char_attr_color(text, text_string, buf_get_ptr_at_typeof(&text_string->color_ranges, char_attr_color_range_t, 0), range_count); + } } SGE_API void vulkan_bitmap_text_set_glyph_layout_handler(vulkan_bitmap_text_t* text, vulkan_bitmap_text_glyph_layout_handler_t handler, void* user_data) @@ -651,6 +667,9 @@ SGE_API void vulkan_bitmap_text_string_setH(vulkan_bitmap_text_t* text, vulkan_ { vulkan_bitmap_text_string_t* text_string = get_text_stringH(text, handle); + /* we need to clear the color ranges buffer as new char array is supplied by the user and now it is now stale */ + buf_clear(&text_string->color_ranges, NULL); + /* clear the previous string characters */ buf_clear(&text_string->chars, NULL); /* write the new characters */ @@ -738,9 +757,8 @@ SGE_API void vulkan_bitmap_text_string_set_color(vulkan_bitmap_text_t* text, vul vulkan_instance_buffer_commit(&text->glyph_render_data_buffer, NULL); } -SGE_API void vulkan_bitmap_text_string_set_char_attr_color(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_handle_t handle, const char_attr_color_range_t* ranges, const u32 range_count) +static void text_string_set_char_attr_color(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_t* text_string, const char_attr_color_range_t* ranges, u32 range_count) { - vulkan_bitmap_text_string_t* text_string = get_text_stringH(text, handle); DEBUG_BLOCK ( AUTO char_count = buf_get_element_count(&text_string->chars); @@ -758,6 +776,17 @@ DEBUG_BLOCK vulkan_instance_buffer_commit(&text->glyph_render_data_buffer, NULL); } +SGE_API void vulkan_bitmap_text_string_set_char_attr_color(vulkan_bitmap_text_t* text, vulkan_bitmap_text_string_handle_t handle, const char_attr_color_range_t* ranges, const u32 range_count) +{ + vulkan_bitmap_text_string_t* text_string = get_text_stringH(text, handle); + + text_string_set_char_attr_color(text, text_string, ranges, range_count); + + /* TODO: this should be buf_clear_fast */ + buf_clear(&text_string->color_ranges, NULL); + buf_pushv(&text_string->color_ranges, CAST_TO(void*, ranges), range_count); +} + /* getters */ SGE_API u32 vulkan_bitmap_text_get_point_size(vulkan_bitmap_text_t* text) {