only create layer buffers if count is nonzero

main
noah metz 2024-10-21 13:11:59 -06:00
parent 1dbd924b8c
commit 6fa251773a
3 changed files with 110 additions and 60 deletions

@ -195,22 +195,32 @@ VkResult load_texture(
uint32_t* index, uint32_t* index,
TextureStorage* memory); TextureStorage* memory);
typedef struct UILayerInputStruct {
uint32_t strings;
uint32_t codes;
uint32_t drawables;
uint32_t font;
} UILayerInput;
VkResult create_container( VkResult create_container(
uint32_t id,
float x, float x,
float y, float y,
float width, float width,
float height, float height,
uint32_t layer_count,
UILayerInput* layers,
RenderContext* gpu, RenderContext* gpu,
UIContainerStorage* memory); UIContainerStorage* memory);
VkResult create_layer( VkResult create_layer(
uint32_t max_strings, uint32_t index,
uint32_t max_codes, uint32_t strings,
uint32_t max_drawables, uint32_t codes,
uint32_t font_index, uint32_t drawables,
uint32_t font,
RenderContext* gpu, RenderContext* gpu,
UIContainerStorage* container, UIContainerStorage* container);
UILayerStorage* memory);
void set_ui_rect( void set_ui_rect(
float x, float x,

@ -15,7 +15,6 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
UIContextStorage ui; UIContextStorage ui;
UIContainerStorage container; UIContainerStorage container;
UILayerStorage layers[2];
FontStorage font; FontStorage font;
uint32_t font_index; uint32_t font_index;
@ -42,35 +41,39 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
VK_RESULT(load_texture("test.png", render, &ui, &texture_index, &texture)); VK_RESULT(load_texture("test.png", render, &ui, &texture_index, &texture));
VK_RESULT(create_container(0, 0, 200, 200, render, &container)); UILayerInput layers[] = {
{
VK_RESULT(create_layer(10, 100, 10, font_index, render, &container, &layers[0])); .strings = 0,
VK_RESULT(create_layer(10, 100, 10, font_index, render, &container, &layers[1])); .codes = 0,
.drawables = 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)); },
{
uint32_t* mapped_count = (uint32_t*)(mapped); .strings = 1,
mapped_count[0] = 1; .codes = 100,
mapped_count[1] = 1; .drawables = 1,
UIDrawable* mapped_drawable = (UIDrawable*)(mapped + 2*sizeof(uint32_t)); .font = font_index,
},
};
VK_RESULT(create_container(0xDEADBEEF, 0.0, 0.0, 200.0, 200.0, sizeof(layers)/sizeof(UILayerInput), layers, render, &container));
VK_RESULT(create_transfer_buffer(render->allocator, 2*sizeof(UIDrawable) + sizeof(UIString) + 100*sizeof(uint32_t), &transfer, &transfer_memory, &mapped));
UIDrawable* mapped_drawable = (UIDrawable*)(mapped + 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_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(UIDrawable));
set_ui_string(0.0, 100.0, 32.0, 1.0, 1.0, 1.0, 1.0, 13, 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(UIDrawable) + sizeof(UIString));
VK_RESULT(set_ui_codes("Hello, World!", 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, container.layers[0].drawables, 0, 0, sizeof(UIDrawable));
command_copy_buffer(command_buffer, transfer, layers[1].layer, 0, offsetof(UILayer, num_drawables), sizeof(uint32_t)); command_copy_buffer(command_buffer, transfer, container.layers[1].drawables, sizeof(UIDrawable), 0, sizeof(UIDrawable));
command_copy_buffer(command_buffer, transfer, layers[1].layer, sizeof(uint32_t), offsetof(UILayer, dispatch_strings) + offsetof(DispatchCommand, x), sizeof(uint32_t));
command_copy_buffer(command_buffer, transfer, layers[0].drawables, 2*sizeof(uint32_t), 0, sizeof(UIDrawable));
command_copy_buffer(command_buffer, transfer, layers[1].drawables, 2*sizeof(uint32_t) + sizeof(UIDrawable), 0, sizeof(UIDrawable));
command_copy_buffer(command_buffer, transfer, layers[1].strings, 2*sizeof(uint32_t) + 2*sizeof(UIDrawable), 0, sizeof(UIString)); command_copy_buffer(command_buffer, transfer, container.layers[1].strings, 2*sizeof(UIDrawable), 0, sizeof(UIString));
command_copy_buffer(command_buffer, transfer, layers[1].codes, 2*sizeof(uint32_t) + 2*sizeof(UIDrawable) + sizeof(UIString), 0, 100*sizeof(uint32_t)); command_copy_buffer(command_buffer, transfer, container.layers[1].codes, 2*sizeof(UIDrawable) + sizeof(UIString), 0, 100*sizeof(uint32_t));
VK_RESULT(command_end_single(render->device, command_buffer, render->transfer_pool, render->transfer_queue)); VK_RESULT(command_end_single(render->device, command_buffer, render->transfer_pool, render->transfer_queue));
vkQueueWaitIdle(render->transfer_queue.handle); vkQueueWaitIdle(render->transfer_queue.handle);
destroy_transfer_buffer(render->allocator, transfer, transfer_memory); destroy_transfer_buffer(render->allocator, transfer, transfer_memory);
@ -78,7 +81,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
while(glfwWindowShouldClose(window) == 0) { while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents(); glfwPollEvents();
result = draw_frame(render, &ui, layers, sizeof(layers)/sizeof(UILayerStorage)); result = draw_frame(render, &ui, container.layers, container.layer_count);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result; return result;

@ -275,10 +275,13 @@ VkResult create_ui_pipeline(
} }
VkResult create_container( VkResult create_container(
uint32_t id,
float x, float x,
float y, float y,
float width, float width,
float height, float height,
uint32_t layer_count,
UILayerInput* layers,
RenderContext* gpu, RenderContext* gpu,
UIContainerStorage* memory) { UIContainerStorage* memory) {
VkResult result; VkResult result;
@ -302,61 +305,95 @@ VkResult create_container(
destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
memory->address = buffer_address(gpu->device, memory->container); memory->address = buffer_address(gpu->device, memory->container);
memory->id = id;
memory->layer_count = layer_count;
memory->layers = malloc(sizeof(UILayerStorage)*layer_count);
for(uint32_t i = 0; i < layer_count; i++) {
VK_RESULT(create_layer(i, layers[i].strings, layers[i].codes, layers[i].drawables, layers[i].font, gpu, memory));
}
return VK_SUCCESS; return VK_SUCCESS;
} }
VkResult create_layer( VkResult create_layer(
uint32_t max_strings, uint32_t index,
uint32_t max_codes, uint32_t strings,
uint32_t max_drawables, uint32_t codes,
uint32_t font_index, uint32_t drawables,
uint32_t font,
RenderContext* gpu, RenderContext* gpu,
UIContainerStorage* container, UIContainerStorage* container) {
UILayerStorage* memory) {
VkResult result; VkResult result;
VK_RESULT(create_storage_buffer(gpu->allocator, VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, sizeof(UILayer), &memory->layer, &memory->layer_memory)); VK_RESULT(create_storage_buffer(gpu->allocator, VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, sizeof(UILayer), &container->layers[index].layer, &container->layers[index].layer_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIString)*max_strings, &memory->strings, &memory->strings_memory)) if(strings > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIDrawable)*(max_drawables + max_codes), &memory->drawables, &memory->drawables_memory)); VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIString)*strings, &container->layers[index].strings, &container->layers[index].strings_memory))
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(uint32_t)*max_codes, &memory->codes, &memory->codes_memory)); }
if(codes + drawables > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIDrawable)*(drawables + codes), &container->layers[index].drawables, &container->layers[index].drawables_memory));
}
if(codes > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(uint32_t)*codes, &container->layers[index].codes, &container->layers[index].codes_memory));
}
VkBuffer transfer; VkBuffer transfer;
VmaAllocation transfer_memory; VmaAllocation transfer_memory;
void* mapped; void* mapped;
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UILayer), &transfer, &transfer_memory, &mapped)); VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UILayer), &transfer, &transfer_memory, &mapped));
memory->data.strings = buffer_address(gpu->device, memory->strings); if(strings > 0) {
memory->data.codes = buffer_address(gpu->device, memory->codes); container->layers[index].data.strings = buffer_address(gpu->device, container->layers[index].strings);
} else {
container->layers[index].data.strings = 0x00000000;
}
if(codes > 0) {
container->layers[index].data.codes = buffer_address(gpu->device, container->layers[index].codes);
} else {
container->layers[index].data.codes = 0x00000000;
}
if(codes + drawables > 0) {
container->layers[index].data.drawables = buffer_address(gpu->device, container->layers[index].drawables);
} else {
container->layers[index].data.drawables = 0x00000000;
}
memory->data.drawables = buffer_address(gpu->device, memory->drawables);
memory->data.draw.first_vertex = 0; container->layers[index].data.draw.first_vertex = 0;
memory->data.draw.vertex_count = 6; container->layers[index].data.draw.vertex_count = 6;
memory->data.draw.first_instance = 0; container->layers[index].data.draw.first_instance = 0;
memory->data.draw.instance_count = 0; container->layers[index].data.draw.instance_count = 0;
memory->data.dispatch_strings.x = 0; container->layers[index].data.dispatch_strings.x = strings;
memory->data.dispatch_strings.y = 1; container->layers[index].data.dispatch_strings.y = 1;
memory->data.dispatch_strings.z = 1; container->layers[index].data.dispatch_strings.z = 1;
memory->data.font_index = font_index; container->layers[index].data.font_index = font;
memory->data.max_drawables = max_drawables; container->layers[index].data.max_drawables = drawables + codes;
memory->data.max_strings = max_strings; container->layers[index].data.max_strings = strings;
memory->data.num_drawables = 0; container->layers[index].data.num_drawables = drawables;
memory->data.container = container->address; container->layers[index].data.container = container->address;
memcpy(mapped, &memory->data, sizeof(UILayer)); memcpy(mapped, &container->layers[index].data, sizeof(UILayer));
VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool); VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool);
command_copy_buffer(command_buffer, transfer, memory->layer, 0, 0, sizeof(UILayer)); command_copy_buffer(command_buffer, transfer, container->layers[index].layer, 0, 0, sizeof(UILayer));
vkCmdFillBuffer(command_buffer, memory->strings, 0, sizeof(UIString)*max_strings, 0x00000000); if(strings > 0) {
vkCmdFillBuffer(command_buffer, memory->drawables, 0, sizeof(UIDrawable)*max_drawables, 0x00000000); vkCmdFillBuffer(command_buffer, container->layers[index].strings, 0, sizeof(UIString)*strings, 0x00000000);
vkCmdFillBuffer(command_buffer, memory->codes, 0, sizeof(uint32_t)*max_codes, 0x00000000); }
if(codes + drawables > 0) {
vkCmdFillBuffer(command_buffer, container->layers[index].drawables, 0, sizeof(UIDrawable)*(drawables+codes), 0x00000000);
}
if(codes > 0) {
vkCmdFillBuffer(command_buffer, container->layers[index].codes, 0, sizeof(uint32_t)*codes, 0x00000000);
}
VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue)); VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue));
vkQueueWaitIdle(gpu->transfer_queue.handle); vkQueueWaitIdle(gpu->transfer_queue.handle);
destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
memory->address = buffer_address(gpu->device, memory->layer); container->layers[index].address = buffer_address(gpu->device, container->layers[index].layer);
return VK_SUCCESS; return VK_SUCCESS;
} }