summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-11-05 15:31:12 -0500
committerJonathan Bradley <jcb@pikum.xyz>2025-11-06 10:25:41 -0500
commit9d78bb3f6149b32ae85c278a13cbb4a34f8b01a5 (patch)
tree0d88a1b8a3892be26544a91709949d8b8b017b53
parent3f788084e9a7efec681f6770516942084fc35235 (diff)
pke: font track actual fibi count
-rw-r--r--src/font.cpp31
1 files changed, 17 insertions, 14 deletions
diff --git a/src/font.cpp b/src/font.cpp
index 496c052..33b35b8 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -149,14 +149,14 @@ float FontType_Inner_LookAheadLineCount(const FontType *const ft, const FontRend
return ret;
}
-bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInstanceBufferItem *ptr_dst) {
+bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInstanceBufferItem *ptr_dst, uint32_t *count) {
assert(ft != nullptr);
assert(fr != nullptr);
assert(ptr_dst != nullptr);
FontGlyphChar *fgc;
FontInstanceBufferItem *buf_item;
bool new_word = true;
- uint32_t i;
+ uint32_t i, ii;
float font_glyph_spacing;
float cursor_head, line_index, line_height, line_length, line_offset;
glm::vec2 glyph_size;
@@ -180,6 +180,7 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
// Suggestion: require min distance to be 0?
// Also note that I don't think we're doing any calculations to make sure the
// left-most pixel is exact to the Extent (screen size).
+ // pixel-perfect font rendering might require its own shader.
font_glyph_spacing = ft->spacing.em_size * fr->settings.char_scale;
cursor_head = 0;
@@ -199,7 +200,7 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
line_offset += (fr->settings.surface_area_size.y - text_height) / 4.0;
}
- for (i = 0; i < fr->n_glyphs; ++i) {
+ for (i = 0, ii = 0; i < fr->n_glyphs; ++i) {
translate = glm::vec3(0);
scale = glm::vec3(1);
fgc = &ft->glyphs[fr->glyph_indices[i]];
@@ -292,7 +293,7 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
cursor_head += fgc->advance * font_glyph_spacing;
- buf_item = &ptr_dst[i];
+ buf_item = &ptr_dst[ii++];
buf_item->pos_scale = glm::translate(glm::mat4(1), translate);
buf_item->pos_scale = glm::scale(buf_item->pos_scale, scale);
buf_item->color_foreground = fr->settings.color_foreground;
@@ -317,6 +318,9 @@ bool FontType_Inner_CalcTransforms(const FontType *ft, FontRender *fr, FontInsta
(fr->settings.char_scale / ft->msdf_settings.minimum_scale)
* ft->msdf_settings.px_range;
}
+ if (count != nullptr) {
+ (*count) += ii;
+ };
return true;
}
@@ -372,7 +376,7 @@ void FontType_Teardown() {
void FontType_Tick(double delta) {
(void)delta;
VkResult vkResult;
- size_t index;
+ uint32_t index;
FontType *ft;
for (FontTypeIndex_T i = 0; i < (FontTypeIndex_T)ftd.n_ft; ++i) {
@@ -402,8 +406,8 @@ void FontType_Tick(double delta) {
continue;
}
- ft->bindings.instance_counter = index;
if (index == 0) {
+ ft->bindings.instance_counter = 0;
continue;
}
ft->gr.should_update_instance_buffer = false;
@@ -416,15 +420,14 @@ void FontType_Tick(double delta) {
if (fr->isMarkedForRemoval == true) {
return;
}
- pk_arr_resize(&fibis, index + fr->n_glyphs);
- if (FontType_Inner_CalcTransforms(ft, fr, &fibis[index])) {
- index += fr->n_glyphs;
- }
+ FontType_Inner_CalcTransforms(ft, fr, &fibis[index], &index);
};
pk_bkt_arr_iterate(&ft->renders, iter_fn.invoke, &iter_fn);
ft->bindings.instance_counter = index;
// check recreate buffer
+ // check against index because it's the real count
+ // fibis.next includes whitespace & unhandled characters
if (ft->bindings.instance_buffer_max_count < index) {
if (ft->bindings.bd_instance.buffer != VK_NULL_HANDLE) {
vkDestroyBuffer(vkDevice, ft->bindings.bd_instance.buffer, vkAllocator);
@@ -435,7 +438,7 @@ void FontType_Tick(double delta) {
}
pkvk_buffer_create_data create_data{};
- create_data.buffer_byte_length[0] = fibis.stride * fibis.next;
+ create_data.buffer_byte_length[0] = fibis.stride * index;
create_data.n_buffers = 1;
create_data.index_instance = 0;
create_data.index_index = -1;
@@ -449,7 +452,7 @@ void FontType_Tick(double delta) {
}
PKVK_TmpBufferDetails tmpBufferDetails{};
- PKVK_BeginBuffer(graphicsFamilyIndex, fibis.stride * fibis.next, tmpBufferDetails);
+ PKVK_BeginBuffer(graphicsFamilyIndex, (VkDeviceSize)fibis.stride * (VkDeviceSize)index, tmpBufferDetails);
assert(tmpBufferDetails.buffer != VK_NULL_HANDLE);
{
VkCommandBufferBeginInfo vkCommandBufferBeginInfo;
@@ -461,12 +464,12 @@ void FontType_Tick(double delta) {
vkResult = vkBeginCommandBuffer(tmpBufferDetails.cmdBuffer, &vkCommandBufferBeginInfo);
assert(vkResult == VK_SUCCESS);
- memcpy(tmpBufferDetails.deviceData, fibis.data, fibis.stride * fibis.next);
+ memcpy(tmpBufferDetails.deviceData, fibis.data, (size_t)fibis.stride * (size_t)index);
VkBufferCopy vk_buffer_copy{};
vk_buffer_copy.srcOffset = 0;
vk_buffer_copy.dstOffset = 0;
- vk_buffer_copy.size = fibis.stride * fibis.next;
+ vk_buffer_copy.size = (VkDeviceSize)fibis.stride * (VkDeviceSize)index;
vkCmdCopyBuffer(tmpBufferDetails.cmdBuffer, tmpBufferDetails.buffer, ft->bindings.bd_instance.buffer, 1, &vk_buffer_copy);
vkResult = vkEndCommandBuffer(tmpBufferDetails.cmdBuffer);