Moved containers into UIContextStorage

main
noah metz 2024-10-21 14:05:27 -06:00
parent 688fccf251
commit 1702f8ff85
5 changed files with 93 additions and 73 deletions

@ -6,8 +6,6 @@
VkResult draw_frame(
RenderContext* context,
UIContextStorage* ui_context,
UIContainerStorage* containers,
uint32_t container_count);
UIContextStorage* ui_context);
#endif

@ -175,12 +175,16 @@ typedef struct UIContextStorageStruct {
char** font_slots;
char** texture_slots;
uint32_t max_containers;
UIContainerStorage* containers;
UIContext data;
} UIContextStorage;
VkResult create_ui_context(
uint32_t max_fonts,
uint32_t max_textures,
uint32_t max_containers,
RenderContext* gpu,
UIContextStorage* memory);
@ -225,7 +229,7 @@ typedef struct UIContainerInputStruct {
VkResult create_container(
UIContainerInput* container,
RenderContext* gpu,
UIContainerStorage* memory);
UIContextStorage* context);
VkResult create_layer(
uint32_t index,

@ -2,9 +2,7 @@
VkResult draw_frame(
RenderContext* context,
UIContextStorage* ui_context,
UIContainerStorage* containers,
uint32_t container_count) {
UIContextStorage* ui_context) {
VkResult result;
result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX);
@ -66,22 +64,23 @@ VkResult draw_frame(
// Compute Pass
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, ui_context->string_pipeline.pipeline);
for(uint32_t i = 0; i < container_count; i++) {
for(uint32_t j = 0; j < containers[i].layer_count; j++) {
push[1] = containers[i].layers[j].address;
for(uint32_t i = 0; i < ui_context->max_containers; i++) {
if(ui_context->containers[i].id != 0x00000000) {
for(uint32_t j = 0; j < ui_context->containers[i].layer_count; j++) {
push[1] = ui_context->containers[i].layers[j].address;
VkBufferMemoryBarrier draw_command_barrier_1 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = containers[i].layers[j].layer,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT,
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
};
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &draw_command_barrier_1, 0, NULL);
command_copy_buffer(command_buffer, containers[i].layers[j].layer, containers[i].layers[j].layer, offsetof(UILayer, num_drawables), offsetof(UILayer, draw) + offsetof(DrawCommand, instance_count), sizeof(uint32_t));
command_copy_buffer(command_buffer, ui_context->containers[i].layers[j].layer, ui_context->containers[i].layers[j].layer, offsetof(UILayer, num_drawables), offsetof(UILayer, draw) + offsetof(DrawCommand, instance_count), sizeof(uint32_t));
VkBufferMemoryBarrier draw_command_barrier_2 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = containers[i].layers[j].layer,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
@ -89,10 +88,10 @@ VkResult draw_frame(
};
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 1, &draw_command_barrier_2, 0, NULL);
vkCmdPushConstants(command_buffer, ui_context->string_pipeline.layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, 16, push);
vkCmdDispatchIndirect(command_buffer, containers[i].layers[j].layer, offsetof(UILayer, dispatch_strings));
vkCmdDispatchIndirect(command_buffer, ui_context->containers[i].layers[j].layer, offsetof(UILayer, dispatch_strings));
VkBufferMemoryBarrier draw_command_barrier_3 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = containers[i].layers[j].layer,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
@ -102,15 +101,16 @@ VkResult draw_frame(
VkBufferMemoryBarrier drawables_barrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = containers[i].layers[j].drawables,
.buffer = ui_context->containers[i].layers[j].drawables,
.offset = 0,
.size = sizeof(UIDrawable)*containers[i].layers[j].data.max_drawables,
.size = sizeof(UIDrawable)*ui_context->containers[i].layers[j].data.max_drawables,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
};
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT, 0, 0, NULL, 1, &drawables_barrier, 0, NULL);
}
}
}
// Render Pass
@ -123,11 +123,13 @@ VkResult draw_frame(
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->pipeline.layout, 1, 1, &ui_context->font_textures, 0, NULL);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->pipeline.layout, 2, 1, &ui_context->samplers, 0, NULL);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->pipeline.layout, 3, 1, &ui_context->textures, 0, NULL);
for(uint32_t i = 0; i < container_count; i++) {
for(uint32_t j = 0; j < containers[i].layer_count; j++) {
push[1] = containers[i].layers[j].address;
for(uint32_t i = 0; i < ui_context->max_containers; i++) {
if(ui_context->containers[i].id != 0x00000000) {
for(uint32_t j = 0; j < ui_context->containers[i].layer_count; j++) {
push[1] = ui_context->containers[i].layers[j].address;
vkCmdPushConstants(command_buffer, ui_context->pipeline.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, 16, push);
vkCmdDrawIndirect(command_buffer, containers[i].layers[j].layer, offsetof(UILayer, draw), 1, 0);
vkCmdDrawIndirect(command_buffer, ui_context->containers[i].layers[j].layer, offsetof(UILayer, draw), 1, 0);
}
}
}

@ -14,7 +14,6 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
FT_Library library;
UIContextStorage ui;
UIContainerStorage container;
FontStorage font;
uint32_t font_index;
@ -25,6 +24,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
VkResult result;
VK_RESULT(create_ui_context(
10,
10,
10,
render,
@ -90,12 +90,12 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
.layers = layers,
};
VK_RESULT(create_container(&container_info, render, &container));
VK_RESULT(create_container(&container_info, render, &ui));
while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents();
result = draw_frame(render, &ui, &container, 1);
result = draw_frame(render, &ui);
if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result;

@ -277,33 +277,44 @@ VkResult create_ui_pipeline(
VkResult create_container(
UIContainerInput* container,
RenderContext* gpu,
UIContainerStorage* memory) {
UIContextStorage* context) {
uint32_t index = 0xFFFFFFFF;
for(uint32_t i = 0; i < context->max_containers; i++) {
if(context->containers[i].id == 0x00000000) {
index = i;
break;
}
}
if(index == 0xFFFFFFFF) {
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
VkResult result;
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIContainer), &memory->container, &memory->container_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIContainer), &context->containers[index].container, &context->containers[index].container_memory));
VkBuffer transfer;
VmaAllocation transfer_memory;
void* mapped;
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UIContainer), &transfer, &transfer_memory, &mapped));
memory->data.pos[0] = container->pos[0];
memory->data.pos[1] = container->pos[1];
memory->data.size[0] = container->size[0];
memory->data.size[1] = container->size[1];
memcpy(mapped, &memory->data, sizeof(UIContainer));
context->containers[index].data.pos[0] = container->pos[0];
context->containers[index].data.pos[1] = container->pos[1];
context->containers[index].data.size[0] = container->size[0];
context->containers[index].data.size[1] = container->size[1];
memcpy(mapped, &context->containers[index].data, sizeof(UIContainer));
VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool);
command_copy_buffer(command_buffer, transfer, memory->container, 0, 0, sizeof(UIContainer));
command_copy_buffer(command_buffer, transfer, context->containers[index].container, 0, 0, sizeof(UIContainer));
VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue));
destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
memory->address = buffer_address(gpu->device, memory->container);
memory->id = container->id;
memory->layer_count = container->layer_count;
memory->layers = malloc(sizeof(UILayerStorage)*container->layer_count);
context->containers[index].address = buffer_address(gpu->device, context->containers[index].container);
context->containers[index].id = container->id;
context->containers[index].layer_count = container->layer_count;
context->containers[index].layers = malloc(sizeof(UILayerStorage)*container->layer_count);
for(uint32_t i = 0; i < container->layer_count; i++) {
VK_RESULT(create_layer(i, &container->layers[i], gpu, memory));
VK_RESULT(create_layer(i, &container->layers[i], gpu, &context->containers[index]));
}
return VK_SUCCESS;
@ -1050,6 +1061,7 @@ VkResult create_ui_descriptor(
VkResult create_ui_context(
uint32_t max_fonts,
uint32_t max_textures,
uint32_t max_containers,
RenderContext* gpu,
UIContextStorage* memory) {
@ -1083,11 +1095,15 @@ VkResult create_ui_context(
memory->max_textures = max_textures;
memory->max_fonts = max_fonts;
memory->texture_slots = malloc(max_textures);
memory->texture_slots = malloc(max_textures*sizeof(char*));
memset(memory->texture_slots, 0, max_textures);
memory->font_slots = malloc(max_fonts);
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);
return VK_SUCCESS;
}