diff --git a/client/include/ui.h b/client/include/ui.h index 416b006..c42798e 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -160,6 +160,11 @@ typedef struct UIContextStorageStruct { GraphicsPipeline pipeline; ComputePipeline string_pipeline; + uint32_t max_fonts; + uint32_t max_textures; + uint8_t* font_slots; + uint8_t* texture_slots; + UIContext data; } UIContextStorage; @@ -179,23 +184,23 @@ VkResult load_font( VkDevice device, VmaAllocator allocator, UIContextStorage* context, - uint32_t index, VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, + uint32_t* index, FontStorage* memory); VkResult load_texture( VkDevice device, VmaAllocator allocator, UIContextStorage* context, - uint32_t index, VkCommandPool transfer_pool, Queue transfer_queue, const char* png_file, + uint32_t* index, TextureStorage* memory); VkResult create_container( diff --git a/client/src/main.c b/client/src/main.c index 3429a86..2b7ffc9 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -17,7 +17,9 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { UILayerStorage layers[2]; FontStorage font; + uint32_t font_index; TextureStorage texture; + uint32_t texture_index; VkBuffer transfer; VmaAllocation transfer_memory; @@ -41,14 +43,14 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { return VK_ERROR_UNKNOWN; } - 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, render->transfer_pool, render->transfer_queue, library, "test.ttf", 16, VK_TRUE, &font_index, &font)); - VK_RESULT(load_texture(render->device, render->allocator, &ui, 0, render->transfer_pool, render->transfer_queue, "test.png", &texture)); + VK_RESULT(load_texture(render->device, render->allocator, &ui, render->transfer_pool, render->transfer_queue, "test.png", &texture_index, &texture)); 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])); + VK_RESULT(create_layer(10, 100, 10, font_index, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container, &layers[0])); + VK_RESULT(create_layer(10, 100, 10, font_index, render->device, render->allocator, render->transfer_pool, render->transfer_queue, &container, &layers[1])); VK_RESULT(create_transfer_buffer(render->allocator, 2*sizeof(uint32_t) + 2*sizeof(UIDrawable) + sizeof(UIString) + 100*sizeof(uint32_t), &transfer, &transfer_memory, &mapped)); @@ -56,7 +58,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) { mapped_count[0] = 1; mapped_count[1] = 1; UIDrawable* mapped_drawable = (UIDrawable*)(mapped + 2*sizeof(uint32_t)); - set_ui_image(0.0, 0.0, 100.0, 200.0, 1.0, 1.0, 1.0, 1.0, 0, &mapped_drawable[0]); + set_ui_image(0.0, 0.0, 100.0, 200.0, 1.0, 1.0, 1.0, 1.0, texture_index, &mapped_drawable[0]); 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)); diff --git a/client/src/ui.c b/client/src/ui.c index 0b6e64b..e4fbe1b 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -371,11 +371,23 @@ VkResult load_texture( VkDevice device, VmaAllocator allocator, UIContextStorage* context, - uint32_t index, VkCommandPool transfer_pool, Queue transfer_queue, const char* png_path, + uint32_t* index, TextureStorage* memory) { + *index = 0xFFFFFFFF; + for(uint32_t i = 0; i < context->max_textures; i++) { + if(context->texture_slots[i] == 0) { + context->texture_slots[i] = 1; + *index = i; + break; + } + } + if(*index == 0xFFFFFFFF) { + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } + spng_ctx* spng = spng_ctx_new(0); if(spng == NULL) { return VK_ERROR_UNKNOWN; @@ -548,7 +560,7 @@ VkResult load_texture( .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = context->textures, .dstBinding = 0, - .dstArrayElement = index, + .dstArrayElement = *index, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, .descriptorCount = 1, .pImageInfo = &desc_image_info, @@ -557,7 +569,7 @@ VkResult load_texture( .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = context->samplers, .dstBinding = 0, - .dstArrayElement = index, + .dstArrayElement = *index, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .descriptorCount = 1, .pImageInfo = &desc_sampler_info, @@ -573,14 +585,26 @@ VkResult load_font( VkDevice device, VmaAllocator allocator, UIContextStorage* context, - uint32_t index, VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, + uint32_t* index, FontStorage* memory) { + *index = 0xFFFFFFFF; + for(uint32_t i = 0; i < context->max_fonts; i++) { + if(context->font_slots[i] == 0) { + context->font_slots[i] = 1; + *index = i; + break; + } + } + if(*index == 0xFFFFFFFF) { + return VK_ERROR_OUT_OF_DEVICE_MEMORY; + } + FT_Face face; int error; @@ -699,7 +723,7 @@ VkResult load_font( free(symbols); VkCommandBuffer command_buffer = command_begin_single(device, 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, 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); VkImageMemoryBarrier first_barrier = { @@ -791,7 +815,7 @@ VkResult load_font( .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = context->font_textures, .dstBinding = 0, - .dstArrayElement = index, + .dstArrayElement = *index, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, .descriptorCount = 1, .pImageInfo = &desc_texture_info, @@ -800,7 +824,7 @@ VkResult load_font( .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = context->font_samplers, .dstBinding = 0, - .dstArrayElement = index, + .dstArrayElement = *index, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .descriptorCount = 1, .pImageInfo = &desc_sampler_info, @@ -1033,6 +1057,11 @@ VkResult create_ui_context( VK_RESULT(create_ui_pipeline(device, render_pass, memory->samplers_layout, memory->textures_layout, &memory->pipeline, &memory->string_pipeline)); + memory->max_textures = max_textures; + memory->max_fonts = max_fonts; + memory->texture_slots = malloc(max_textures); + memory->font_slots = malloc(max_fonts); + return VK_SUCCESS; }