Moved index to a return of load_* instead of a parameter

main
noah metz 2024-10-21 10:27:24 -06:00
parent e851d64af6
commit 7b23625859
3 changed files with 50 additions and 14 deletions

@ -160,6 +160,11 @@ typedef struct UIContextStorageStruct {
GraphicsPipeline pipeline; GraphicsPipeline pipeline;
ComputePipeline string_pipeline; ComputePipeline string_pipeline;
uint32_t max_fonts;
uint32_t max_textures;
uint8_t* font_slots;
uint8_t* texture_slots;
UIContext data; UIContext data;
} UIContextStorage; } UIContextStorage;
@ -179,23 +184,23 @@ VkResult load_font(
VkDevice device, VkDevice device,
VmaAllocator allocator, VmaAllocator allocator,
UIContextStorage* context, UIContextStorage* context,
uint32_t index,
VkCommandPool transfer_pool, VkCommandPool transfer_pool,
Queue transfer_queue, Queue transfer_queue,
FT_Library library, FT_Library library,
const char* ttf_file, const char* ttf_file,
uint32_t size, uint32_t size,
VkBool32 antialias, VkBool32 antialias,
uint32_t* index,
FontStorage* memory); FontStorage* memory);
VkResult load_texture( VkResult load_texture(
VkDevice device, VkDevice device,
VmaAllocator allocator, VmaAllocator allocator,
UIContextStorage* context, UIContextStorage* context,
uint32_t index,
VkCommandPool transfer_pool, VkCommandPool transfer_pool,
Queue transfer_queue, Queue transfer_queue,
const char* png_file, const char* png_file,
uint32_t* index,
TextureStorage* memory); TextureStorage* memory);
VkResult create_container( VkResult create_container(

@ -17,7 +17,9 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
UILayerStorage layers[2]; UILayerStorage layers[2];
FontStorage font; FontStorage font;
uint32_t font_index;
TextureStorage texture; TextureStorage texture;
uint32_t texture_index;
VkBuffer transfer; VkBuffer transfer;
VmaAllocation transfer_memory; VmaAllocation transfer_memory;
@ -41,14 +43,14 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
return VK_ERROR_UNKNOWN; 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_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, font_index, 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[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)); 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[0] = 1;
mapped_count[1] = 1; mapped_count[1] = 1;
UIDrawable* mapped_drawable = (UIDrawable*)(mapped + 2*sizeof(uint32_t)); 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]); 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));

@ -371,11 +371,23 @@ VkResult load_texture(
VkDevice device, VkDevice device,
VmaAllocator allocator, VmaAllocator allocator,
UIContextStorage* context, UIContextStorage* context,
uint32_t index,
VkCommandPool transfer_pool, VkCommandPool transfer_pool,
Queue transfer_queue, Queue transfer_queue,
const char* png_path, const char* png_path,
uint32_t* index,
TextureStorage* memory) { 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); spng_ctx* spng = spng_ctx_new(0);
if(spng == NULL) { if(spng == NULL) {
return VK_ERROR_UNKNOWN; return VK_ERROR_UNKNOWN;
@ -548,7 +560,7 @@ VkResult load_texture(
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = context->textures, .dstSet = context->textures,
.dstBinding = 0, .dstBinding = 0,
.dstArrayElement = index, .dstArrayElement = *index,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.descriptorCount = 1, .descriptorCount = 1,
.pImageInfo = &desc_image_info, .pImageInfo = &desc_image_info,
@ -557,7 +569,7 @@ VkResult load_texture(
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = context->samplers, .dstSet = context->samplers,
.dstBinding = 0, .dstBinding = 0,
.dstArrayElement = index, .dstArrayElement = *index,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.descriptorCount = 1, .descriptorCount = 1,
.pImageInfo = &desc_sampler_info, .pImageInfo = &desc_sampler_info,
@ -573,14 +585,26 @@ VkResult load_font(
VkDevice device, VkDevice device,
VmaAllocator allocator, VmaAllocator allocator,
UIContextStorage* context, UIContextStorage* context,
uint32_t index,
VkCommandPool transfer_pool, VkCommandPool transfer_pool,
Queue transfer_queue, Queue transfer_queue,
FT_Library library, FT_Library library,
const char* ttf_file, const char* ttf_file,
uint32_t size, uint32_t size,
VkBool32 antialias, VkBool32 antialias,
uint32_t* index,
FontStorage* memory) { 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; FT_Face face;
int error; int error;
@ -699,7 +723,7 @@ VkResult load_font(
free(symbols); free(symbols);
VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool); 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); command_copy_buffer(command_buffer, transfer, memory->symbols, image_size*info.num_symbols + sizeof(Font), 0, sizeof(SymbolInfo)*info.num_symbols);
VkImageMemoryBarrier first_barrier = { VkImageMemoryBarrier first_barrier = {
@ -791,7 +815,7 @@ VkResult load_font(
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = context->font_textures, .dstSet = context->font_textures,
.dstBinding = 0, .dstBinding = 0,
.dstArrayElement = index, .dstArrayElement = *index,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.descriptorCount = 1, .descriptorCount = 1,
.pImageInfo = &desc_texture_info, .pImageInfo = &desc_texture_info,
@ -800,7 +824,7 @@ VkResult load_font(
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = context->font_samplers, .dstSet = context->font_samplers,
.dstBinding = 0, .dstBinding = 0,
.dstArrayElement = index, .dstArrayElement = *index,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.descriptorCount = 1, .descriptorCount = 1,
.pImageInfo = &desc_sampler_info, .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)); 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; return VK_SUCCESS;
} }