From ab58c63a50edb9e64708d6eae6c3a9410a323274 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 21 Oct 2024 14:15:47 -0600 Subject: [PATCH] Moved font and texture storage variables to UIContext --- client/include/ui.h | 16 ++++---- client/src/main.c | 9 ++--- client/src/ui.c | 89 ++++++++++++++++++++++----------------------- 3 files changed, 56 insertions(+), 58 deletions(-) diff --git a/client/include/ui.h b/client/include/ui.h index 799b719..a5c42d4 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -57,6 +57,8 @@ typedef struct FontStorageStruct { uint32_t* charmap; uint32_t num_symbols; uint32_t index; + char* family; + char* style; } FontStorage; typedef struct TextureStorageStruct { @@ -64,7 +66,8 @@ typedef struct TextureStorageStruct { VkImage image; VkImageView view; VkSampler sampler; - uint32_t index; + + char* path; } TextureStorage; typedef struct UIStringStruct { @@ -172,8 +175,9 @@ typedef struct UIContextStorageStruct { uint32_t max_fonts; uint32_t max_textures; - char** font_slots; - char** texture_slots; + + FontStorage* fonts; + TextureStorage* texture_slots; uint32_t max_containers; UIContainerStorage* containers; @@ -195,15 +199,13 @@ VkResult load_font( FT_Library library, RenderContext* gpu, UIContextStorage* context, - uint32_t* index, - FontStorage* memory); + uint32_t* index); VkResult load_texture( const char* png_path, RenderContext* gpu, UIContextStorage* context, - uint32_t* index, - TextureStorage* memory); + uint32_t* index); typedef struct UILayerInputStruct { uint32_t num_strings; diff --git a/client/src/main.c b/client/src/main.c index 55c5821..32a4bbd 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -15,10 +15,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { UIContextStorage ui; - FontStorage font; uint32_t font_index; - - TextureStorage texture; uint32_t texture_index; VkResult result; @@ -34,9 +31,9 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { return VK_ERROR_UNKNOWN; } - VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, &ui, &font_index, &font)); + VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, &ui, &font_index)); - VK_RESULT(load_texture("test.png", render, &ui, &texture_index, &texture)); + VK_RESULT(load_texture("test.png", render, &ui, &texture_index)); UIDrawable image = { .pos = {0.0, 0.0}, @@ -80,7 +77,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { }, }; - map_string("Hello, World!", layers[1].codes, 0, font.charmap, font.num_symbols); + map_string("Hello, World!", layers[1].codes, 0, ui.fonts[0].charmap, ui.fonts[0].num_symbols); UIContainerInput container_info = { .id = 0xDEADBEEF, diff --git a/client/src/ui.c b/client/src/ui.c index f12dcf9..37c2159 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -422,13 +422,12 @@ VkResult load_texture( const char* png_path, RenderContext* gpu, UIContextStorage* context, - uint32_t* index, - TextureStorage* memory) { + uint32_t* index) { *index = 0xFFFFFFFF; for(uint32_t i = 0; i < context->max_textures; i++) { - if(context->texture_slots[i] == NULL) { - context->texture_slots[i] = malloc(strlen(png_path) + 1); - memcpy(context->texture_slots[i], png_path, strlen(png_path) + 1); + if(context->texture_slots[i].path == NULL) { + context->texture_slots[i].path = malloc(strlen(png_path) + 1); + memcpy(context->texture_slots[i].path, png_path, strlen(png_path) + 1); *index = i; break; } @@ -512,14 +511,14 @@ VkResult load_texture( VmaAllocation transfer_memory; void* mapped; - VK_RESULT(vmaCreateImage(gpu->allocator, &image_info, &memory_info, &memory->image, &memory->image_memory, NULL)); + VK_RESULT(vmaCreateImage(gpu->allocator, &image_info, &memory_info, &context->texture_slots[*index].image, &context->texture_slots[*index].image_memory, NULL)); VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(uint32_t)*ihdr.width*ihdr.height, &transfer, &transfer_memory, &mapped)); memcpy(mapped, image_buffer, sizeof(uint32_t)*ihdr.height*ihdr.width); VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool); VkImageMemoryBarrier first_barrier = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .image = memory->image, + .image = context->texture_slots[*index].image, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, @@ -545,11 +544,11 @@ VkResult load_texture( .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, }, }; - vkCmdCopyBufferToImage(command_buffer, transfer, memory->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); + vkCmdCopyBufferToImage(command_buffer, transfer, context->texture_slots[*index].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); VkImageMemoryBarrier second_barrier = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .image = memory->image, + .image = context->texture_slots[*index].image, .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, .subresourceRange = { @@ -572,7 +571,7 @@ VkResult load_texture( VkImageViewCreateInfo view_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .image = memory->image, + .image = context->texture_slots[*index].image, .viewType = VK_IMAGE_VIEW_TYPE_2D, .format = VK_FORMAT_R8G8B8A8_SRGB, .subresourceRange = { @@ -582,7 +581,7 @@ VkResult load_texture( }, }; - VK_RESULT(vkCreateImageView(gpu->device, &view_info, NULL, &memory->view)); + VK_RESULT(vkCreateImageView(gpu->device, &view_info, NULL, &context->texture_slots[*index].view)); VkSamplerCreateInfo sampler_info = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, @@ -593,15 +592,15 @@ VkResult load_texture( .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, }; - VK_RESULT(vkCreateSampler(gpu->device, &sampler_info, NULL, &memory->sampler)); + VK_RESULT(vkCreateSampler(gpu->device, &sampler_info, NULL, &context->texture_slots[*index].sampler)); VkDescriptorImageInfo desc_sampler_info = { - .sampler = memory->sampler, + .sampler = context->texture_slots[*index].sampler, }; VkDescriptorImageInfo desc_image_info = { .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - .imageView = memory->view, + .imageView = context->texture_slots[*index].view, }; VkWriteDescriptorSet desc_writes[] = { @@ -637,8 +636,7 @@ VkResult load_font( FT_Library library, RenderContext* gpu, UIContextStorage* context, - uint32_t* index, - FontStorage* memory){ + uint32_t* index){ FT_Face face; int error; @@ -649,12 +647,13 @@ VkResult load_font( *index = 0xFFFFFFFF; for(uint32_t i = 0; i < context->max_fonts; i++) { - if(context->font_slots[i] == 0) { - context->font_slots[i] = malloc(strlen(face->family_name) + strlen(face->style_name) + 2); - memcpy(context->font_slots[i], face->family_name, strlen(face->family_name)); - context->font_slots[i][strlen(face->family_name)] = ':'; - memcpy(context->font_slots[i] + strlen(face->family_name) + 1, face->style_name, strlen(face->style_name)); - context->font_slots[i][strlen(face->family_name) + strlen(face->style_name) + 1] = '\0'; + if(context->fonts[i].family == NULL) { + context->fonts[i].family = malloc(strlen(face->family_name)+1); + memcpy(&context->fonts[i].family, face->family_name, strlen(face->family_name)+1); + + context->fonts[i].style = malloc(strlen(face->style_name)+1); + memcpy(&context->fonts[i].style, face->style_name, strlen(face->style_name)+1); + *index = i; break; } @@ -703,16 +702,16 @@ VkResult load_font( info.width = max_width; info.height = max_height; info.num_symbols = symbol_count; - memory->num_symbols = symbol_count; + context->fonts[*index].num_symbols = symbol_count; uint32_t image_size = max_width*max_height*sizeof(uint32_t); uint32_t* images = malloc(image_size*symbol_count); memset(images, 0x00, image_size*symbol_count); - memory->charmap = malloc(sizeof(uint32_t)*symbol_count); - memcpy(memory->charmap, tmp_charmap, sizeof(uint32_t)*symbol_count); + context->fonts[*index].charmap = malloc(sizeof(uint32_t)*symbol_count); + memcpy(context->fonts[*index].charmap, tmp_charmap, sizeof(uint32_t)*symbol_count); free(tmp_charmap); 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, context->fonts[*index].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; @@ -735,7 +734,7 @@ VkResult load_font( } VkResult result; - VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(SymbolInfo)*info.num_symbols, &memory->symbols, &memory->symbol_memory)); + VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(SymbolInfo)*info.num_symbols, &context->fonts[*index].symbols, &context->fonts[*index].symbol_memory)); VkImageCreateInfo image_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, @@ -756,14 +755,14 @@ VkResult load_font( .usage = VMA_MEMORY_USAGE_GPU_ONLY, }; - VK_RESULT(vmaCreateImage(gpu->allocator, &image_info, &image_memory_info, &memory->image, &memory->image_memory, NULL)); + VK_RESULT(vmaCreateImage(gpu->allocator, &image_info, &image_memory_info, &context->fonts[*index].image, &context->fonts[*index].image_memory, NULL)); VkBuffer transfer; VmaAllocation transfer_memory; void* mapped; VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(Font) + image_size*info.num_symbols + sizeof(SymbolInfo)*info.num_symbols, &transfer, &transfer_memory, &mapped)); - info.symbol_list = buffer_address(gpu->device, memory->symbols); + info.symbol_list = buffer_address(gpu->device, context->fonts[*index].symbols); memcpy(mapped, images, image_size*info.num_symbols); memcpy(mapped + image_size*info.num_symbols, &info, sizeof(Font)); @@ -774,11 +773,11 @@ VkResult load_font( VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool); command_copy_buffer(command_buffer, transfer, context->font_infos, image_size*info.num_symbols, *index*sizeof(Font), sizeof(Font)); - command_copy_buffer(command_buffer, transfer, memory->symbols, image_size*info.num_symbols + sizeof(Font), 0, sizeof(SymbolInfo)*info.num_symbols); + command_copy_buffer(command_buffer, transfer, context->fonts[*index].symbols, image_size*info.num_symbols + sizeof(Font), 0, sizeof(SymbolInfo)*info.num_symbols); VkImageMemoryBarrier first_barrier = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .image = memory->image, + .image = context->fonts[*index].image, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, @@ -796,11 +795,11 @@ VkResult load_font( .imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .imageExtent = image_info.extent, }; - vkCmdCopyBufferToImage(command_buffer, transfer, memory->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); + vkCmdCopyBufferToImage(command_buffer, transfer, context->fonts[*index].image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); VkImageMemoryBarrier second_barrier = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - .image = memory->image, + .image = context->fonts[*index].image, .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, @@ -819,7 +818,7 @@ VkResult load_font( VkImageViewCreateInfo view_info = { .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - .image = memory->image, + .image = context->fonts[*index].image, .viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY, .format = VK_FORMAT_R8G8B8A8_SRGB, .subresourceRange = { @@ -830,7 +829,7 @@ VkResult load_font( .baseArrayLayer = 0, }, }; - VK_RESULT(vkCreateImageView(gpu->device, &view_info, NULL, &memory->view)) + VK_RESULT(vkCreateImageView(gpu->device, &view_info, NULL, &context->fonts[*index].view)) VkSamplerCreateInfo sampler_info = { .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, @@ -840,15 +839,15 @@ VkResult load_font( .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, }; - VK_RESULT(vkCreateSampler(gpu->device, &sampler_info, NULL, &memory->sampler)); + VK_RESULT(vkCreateSampler(gpu->device, &sampler_info, NULL, &context->fonts[*index].sampler)); VkDescriptorImageInfo desc_sampler_info = { - .sampler = memory->sampler, + .sampler = context->fonts[*index].sampler, }; VkDescriptorImageInfo desc_texture_info = { .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - .imageView = memory->view, + .imageView = context->fonts[*index].view, }; VkWriteDescriptorSet descriptor_writes[] = { @@ -1095,14 +1094,14 @@ VkResult create_ui_context( memory->max_textures = max_textures; memory->max_fonts = max_fonts; - memory->texture_slots = malloc(max_textures*sizeof(char*)); - memset(memory->texture_slots, 0, max_textures); - memory->font_slots = malloc(max_fonts*sizeof(char*)); - memset(memory->font_slots, 0, max_fonts); - - memory->containers = malloc(sizeof(UIContainerStorage)*max_containers); memory->max_containers = max_containers; - memset(memory->containers, 0, sizeof(UIContainerStorage)*max_containers); + + memory->texture_slots = malloc(max_textures*sizeof(TextureStorage)); + memset(memory->texture_slots, 0, max_textures*sizeof(TextureStorage)); + memory->fonts = malloc(max_fonts*sizeof(FontStorage)); + memset(memory->fonts, 0, max_fonts*sizeof(FontStorage)); + memory->containers = malloc(max_containers*sizeof(UIContainerStorage)); + memset(memory->containers, 0, max_containers*sizeof(UIContainerStorage)); return VK_SUCCESS; }