Improvements to rendering

main
noah metz 2024-10-20 22:39:12 -06:00
parent 8c793d36c3
commit 285a937e1b
6 changed files with 27 additions and 37 deletions

@ -40,11 +40,11 @@ typedef struct FontStruct {
} Font; } Font;
typedef struct SymbolInfoStruct { typedef struct SymbolInfoStruct {
int32_t top; float top;
uint32_t left; float left;
uint32_t width; float width;
uint32_t height; float height;
uint32_t advance; float advance;
} SymbolInfo; } SymbolInfo;
typedef struct FontStorageStruct { typedef struct FontStorageStruct {
@ -126,7 +126,6 @@ typedef struct UIContainerStorageStruct {
typedef struct UIContextStruct { typedef struct UIContextStruct {
VkDeviceAddress font_infos; VkDeviceAddress font_infos;
vec2 screen;
vec2 scale; vec2 scale;
} UIContext; } UIContext;

@ -21,7 +21,7 @@ void main() {
uint code = pc.layer.codes.c[string.offset + i]; uint code = pc.layer.codes.c[string.offset + i];
Symbol symbol = font.symbols.s[code]; Symbol symbol = font.symbols.s[code];
pc.layer.drawables.d[buffer_pos + i].pos = string.pos + vec2(x, 0); 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].size = vec2(string.size, string.size);
pc.layer.drawables.d[buffer_pos + i].color = string.color; 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]; pc.layer.drawables.d[buffer_pos + i].code = pc.layer.codes.c[string.offset + i];

@ -24,7 +24,6 @@ const vec2 square[6] = {
}; };
void main() { void main() {
vec2 scale = pc.context.scale / pc.context.screen;
Drawable drawable = pc.layer.drawables.d[gl_InstanceIndex]; Drawable drawable = pc.layer.drawables.d[gl_InstanceIndex];
vec2 pos = square[gl_VertexIndex]; vec2 pos = square[gl_VertexIndex];
@ -35,16 +34,11 @@ void main() {
Font font = pc.context.fonts.f[pc.layer.font_index]; Font font = pc.context.fonts.f[pc.layer.font_index];
Symbol symbol = font.symbols.s[drawable.code]; Symbol symbol = font.symbols.s[drawable.code];
float scalex = float(symbol.width)/float(font.width); fragUV = pos * vec2(symbol.width, symbol.height);
float scaley = float(symbol.height)/float(font.height); pos = pos * vec2(symbol.width, symbol.height) + vec2(symbol.left, -symbol.top);
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);
} }
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; fragColor = drawable.color;
code = drawable.code; code = drawable.code;

@ -16,11 +16,11 @@ struct DispatchCommand {
}; };
struct Symbol { struct Symbol {
int top; float top;
uint left; float left;
uint width; float width;
uint height; float height;
uint advance; float advance;
}; };
layout(std430, buffer_reference) buffer SymbolList { layout(std430, buffer_reference) buffer SymbolList {
@ -90,6 +90,5 @@ layout(std430, buffer_reference) readonly buffer Layer {
layout(std430, buffer_reference) buffer Context { layout(std430, buffer_reference) buffer Context {
FontList fonts; FontList fonts;
vec2 screen;
vec2 scale; vec2 scale;
}; };

@ -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(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[0]));
VK_RESULT(create_layer(10, 100, 10, 0, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container, &layers[1])); 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]); 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)); 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)); 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); 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)); command_copy_buffer(command_buffer, transfer, layers[0].layer, 0, offsetof(UILayer, num_drawables), sizeof(uint32_t));

@ -417,11 +417,6 @@ VkResult load_font(
uint32_t width = face->glyph->bitmap.width; uint32_t width = face->glyph->bitmap.width;
uint32_t height = face->glyph->bitmap.rows; uint32_t height = face->glyph->bitmap.rows;
tmp_charmap[i] = c; 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_width = width > max_width ? width : max_width;
max_height = height > max_height ? height : max_height; max_height = height > max_height ? height : max_height;
symbol_count += 1; symbol_count += 1;
@ -442,6 +437,11 @@ VkResult load_font(
for(uint32_t i = 0; i < symbol_count; i++) { for(uint32_t i = 0; i < symbol_count; i++) {
glyph_index = FT_Get_Char_Index(face, memory->charmap[i]); glyph_index = FT_Get_Char_Index(face, memory->charmap[i]);
FT_Load_Glyph(face, glyph_index, load_flags); 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 y = 0; y < face->glyph->bitmap.rows; y++) {
for(uint32_t x = 0; x < face->glyph->bitmap.width; x++) { for(uint32_t x = 0; x < face->glyph->bitmap.width; x++) {
uint64_t level = face->glyph->bitmap.buffer[y*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 = { VkSamplerCreateInfo sampler_info = {
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_NEAREST, .magFilter = VK_FILTER_CUBIC_IMG,
.minFilter = VK_FILTER_LINEAR, .minFilter = VK_FILTER_CUBIC_IMG,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.addressModeW = 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)); 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.font_infos = buffer_address(device, memory->font_infos);
memory->data.screen[0] = swapchain_extent.width; memory->data.scale[0] = window_scale[0] / swapchain_extent.width;
memory->data.screen[1] = swapchain_extent.height; memory->data.scale[1] = window_scale[1] / swapchain_extent.height;
memory->data.scale[0] = window_scale[0];
memory->data.scale[1] = window_scale[1];
memcpy(mapped, &memory->data, sizeof(UIContext)); memcpy(mapped, &memory->data, sizeof(UIContext));
VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool); VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool);
@ -835,7 +833,7 @@ VkResult set_ui_codes(
if(mapped == 0xFFFFFFFF) { if(mapped == 0xFFFFFFFF) {
return VK_ERROR_UNKNOWN; return VK_ERROR_UNKNOWN;
} }
buffer[i] = mapped; buffer[i + offset] = mapped;
i += 1; i += 1;
} }