From 285a937e1bfa06299c4db8d2f6a6462a7276323d Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sun, 20 Oct 2024 22:39:12 -0600 Subject: [PATCH] Improvements to rendering --- client/include/ui.h | 11 +++++------ client/shader_src/string.comp | 2 +- client/shader_src/ui.vert | 12 +++--------- client/shader_src/ui_common.glsl | 11 +++++------ client/src/main.c | 6 +++--- client/src/ui.c | 22 ++++++++++------------ 6 files changed, 27 insertions(+), 37 deletions(-) diff --git a/client/include/ui.h b/client/include/ui.h index b95f1da..cdb72d4 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -40,11 +40,11 @@ typedef struct FontStruct { } Font; typedef struct SymbolInfoStruct { - int32_t top; - uint32_t left; - uint32_t width; - uint32_t height; - uint32_t advance; + float top; + float left; + float width; + float height; + float advance; } SymbolInfo; typedef struct FontStorageStruct { @@ -126,7 +126,6 @@ typedef struct UIContainerStorageStruct { typedef struct UIContextStruct { VkDeviceAddress font_infos; - vec2 screen; vec2 scale; } UIContext; diff --git a/client/shader_src/string.comp b/client/shader_src/string.comp index 73e2aa7..6e6ae16 100644 --- a/client/shader_src/string.comp +++ b/client/shader_src/string.comp @@ -21,7 +21,7 @@ void main() { uint code = pc.layer.codes.c[string.offset + i]; Symbol symbol = font.symbols.s[code]; pc.layer.drawables.d[buffer_pos + i].pos = string.pos + vec2(x, 0); - x += string.size*symbol.advance/font.width; + x += string.size*symbol.advance; pc.layer.drawables.d[buffer_pos + i].size = vec2(string.size, string.size); pc.layer.drawables.d[buffer_pos + i].color = string.color; pc.layer.drawables.d[buffer_pos + i].code = pc.layer.codes.c[string.offset + i]; diff --git a/client/shader_src/ui.vert b/client/shader_src/ui.vert index 5be859c..8268d49 100644 --- a/client/shader_src/ui.vert +++ b/client/shader_src/ui.vert @@ -24,7 +24,6 @@ const vec2 square[6] = { }; void main() { - vec2 scale = pc.context.scale / pc.context.screen; Drawable drawable = pc.layer.drawables.d[gl_InstanceIndex]; vec2 pos = square[gl_VertexIndex]; @@ -35,16 +34,11 @@ void main() { Font font = pc.context.fonts.f[pc.layer.font_index]; Symbol symbol = font.symbols.s[drawable.code]; - float scalex = float(symbol.width)/float(font.width); - float scaley = float(symbol.height)/float(font.height); - float offx = float(symbol.left)/float(font.width); - float offy = float(symbol.top)/float(font.height); - - fragUV = pos * vec2(scalex, scaley); - pos = pos * vec2(scalex, scaley) + vec2(offx, -offy); + fragUV = pos * vec2(symbol.width, symbol.height); + pos = pos * vec2(symbol.width, symbol.height) + vec2(symbol.left, -symbol.top); } - gl_Position = vec4((pos * drawable.size + drawable.pos + pc.layer.container.pos) * scale, 0.0, 1.0) - vec4(1.0, 1.0, 0.0, 0.0); + gl_Position = vec4((pos * drawable.size + drawable.pos + pc.layer.container.pos) * pc.context.scale, 0.0, 1.0) - vec4(1.0, 1.0, 0.0, 0.0); fragColor = drawable.color; code = drawable.code; diff --git a/client/shader_src/ui_common.glsl b/client/shader_src/ui_common.glsl index c6565eb..6b74da1 100644 --- a/client/shader_src/ui_common.glsl +++ b/client/shader_src/ui_common.glsl @@ -16,11 +16,11 @@ struct DispatchCommand { }; struct Symbol { - int top; - uint left; - uint width; - uint height; - uint advance; + float top; + float left; + float width; + float height; + float advance; }; layout(std430, buffer_reference) buffer SymbolList { @@ -90,6 +90,5 @@ layout(std430, buffer_reference) readonly buffer Layer { layout(std430, buffer_reference) buffer Context { FontList fonts; - vec2 screen; vec2 scale; }; diff --git a/client/src/main.c b/client/src/main.c index 38e2f47..ac4bdf8 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -41,7 +41,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { VK_RESULT(load_font(render->device, render->allocator, &ui, 0, render->transfer_pool, render->transfer_queue, library, "test.ttf", 16, VK_TRUE, &font)); - VK_RESULT(create_container(100, 100, 200, 200, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container)); + VK_RESULT(create_container(0, 0, 200, 200, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container)); VK_RESULT(create_layer(10, 100, 10, 0, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container, &layers[0])); VK_RESULT(create_layer(10, 100, 10, 0, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container, &layers[1])); @@ -56,9 +56,9 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { set_ui_rect(100.0, 0.0, 100.0, 200.0, 0.0, 1.0, 0.0, 1.0, &mapped_drawable[1]); UIString* mapped_string = (UIString*)(mapped + 2*sizeof(uint32_t) + 2*sizeof(UIDrawable)); - set_ui_string(0.0, 100.0, 16.0, 1.0, 1.0, 1.0, 1.0, 5, 0, &mapped_string[0]); + set_ui_string(0.0, 100.0, 32.0, 1.0, 1.0, 1.0, 1.0, 13, 0, &mapped_string[0]); uint32_t* mapped_codes = (uint32_t*)(mapped + 2*sizeof(uint32_t) + 2*sizeof(UIDrawable) + sizeof(UIString)); - VK_RESULT(set_ui_codes("Hello", mapped_codes, 0, font.charmap, font.num_symbols)); + VK_RESULT(set_ui_codes("Hello, World!", mapped_codes, 0, font.charmap, font.num_symbols)); VkCommandBuffer command_buffer = command_begin_single(render->device, render->transfer_pool); command_copy_buffer(command_buffer, transfer, layers[0].layer, 0, offsetof(UILayer, num_drawables), sizeof(uint32_t)); diff --git a/client/src/ui.c b/client/src/ui.c index f715067..450ece9 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -417,11 +417,6 @@ VkResult load_font( uint32_t width = face->glyph->bitmap.width; uint32_t height = face->glyph->bitmap.rows; tmp_charmap[i] = c; - symbols[i].width = width; - symbols[i].height = height; - symbols[i].left = face->glyph->bitmap_left; - symbols[i].top = face->glyph->bitmap_top; - symbols[i].advance = face->glyph->advance.x*16/face->units_per_EM; max_width = width > max_width ? width : max_width; max_height = height > max_height ? height : max_height; symbol_count += 1; @@ -442,6 +437,11 @@ VkResult load_font( for(uint32_t i = 0; i < symbol_count; i++) { glyph_index = FT_Get_Char_Index(face, memory->charmap[i]); FT_Load_Glyph(face, glyph_index, load_flags); + symbols[i].width = (float)face->glyph->bitmap.width/(float)max_width; + symbols[i].height = (float)face->glyph->bitmap.rows/(float)max_height; + symbols[i].advance = (float)face->glyph->advance.x*16.0/(float)face->units_per_EM/(float)max_width; + symbols[i].left = (float)face->glyph->bitmap_left/(float)max_width; + symbols[i].top = (float)face->glyph->bitmap_top/(float)max_height; for(uint32_t y = 0; y < face->glyph->bitmap.rows; y++) { for(uint32_t x = 0; x < face->glyph->bitmap.width; x++) { uint64_t level = face->glyph->bitmap.buffer[y*face->glyph->bitmap.width+x]; @@ -564,8 +564,8 @@ VkResult load_font( VkSamplerCreateInfo sampler_info = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, - .magFilter = VK_FILTER_NEAREST, - .minFilter = VK_FILTER_LINEAR, + .magFilter = VK_FILTER_CUBIC_IMG, + .minFilter = VK_FILTER_CUBIC_IMG, .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, @@ -754,10 +754,8 @@ VkResult create_ui_context( VK_RESULT(create_transfer_buffer(allocator, sizeof(UIContext), &transfer, &transfer_memory, (void**)&mapped)); memory->data.font_infos = buffer_address(device, memory->font_infos); - memory->data.screen[0] = swapchain_extent.width; - memory->data.screen[1] = swapchain_extent.height; - memory->data.scale[0] = window_scale[0]; - memory->data.scale[1] = window_scale[1]; + memory->data.scale[0] = window_scale[0] / swapchain_extent.width; + memory->data.scale[1] = window_scale[1] / swapchain_extent.height; memcpy(mapped, &memory->data, sizeof(UIContext)); VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool); @@ -835,7 +833,7 @@ VkResult set_ui_codes( if(mapped == 0xFFFFFFFF) { return VK_ERROR_UNKNOWN; } - buffer[i] = mapped; + buffer[i + offset] = mapped; i += 1; }