Renamed UI structs

main
noah metz 2024-10-22 18:22:18 -06:00
parent 4cf1ed725d
commit c48dfce532
5 changed files with 202 additions and 179 deletions

@ -10,6 +10,6 @@ VkResult draw_frame(
VkResult record_draw_commands(
RenderContext* context,
UIContextStorage* ui_context);
UIContext* ui_context);
#endif

@ -38,22 +38,22 @@ typedef struct DispatchCommandStruct {
uint32_t z;
} DispatchCommand;
typedef struct FontStruct {
typedef struct GPUFontStruct {
VkDeviceAddress symbol_list;
uint32_t num_symbols;
uint32_t width;
uint32_t height;
} Font;
} GPUFont;
typedef struct SymbolInfoStruct {
typedef struct GPUSymbolStruct {
float top;
float left;
float width;
float height;
float advance;
} SymbolInfo;
} GPUSymbol;
typedef struct FontStorageStruct {
typedef struct FontStruct {
VmaAllocation symbol_memory;
VmaAllocation image_memory;
VkBuffer symbols;
@ -65,18 +65,18 @@ typedef struct FontStorageStruct {
uint32_t index;
char* family;
char* style;
} FontStorage;
} Font;
typedef struct TextureStorageStruct {
typedef struct TextureStruct {
VmaAllocation image_memory;
VkImage image;
VkImageView view;
VkSampler sampler;
char* path;
} TextureStorage;
} Texture;
typedef struct UIStringStruct {
typedef struct GPUStringStruct {
vec2 pos;
vec4 color;
float size;
@ -84,9 +84,9 @@ typedef struct UIStringStruct {
uint32_t length;
uint32_t font;
uint32_t id;
} UIString;
} GPUString;
typedef struct UIDrawableStruct {
typedef struct GPUDrawableStruct {
vec2 pos;
vec2 size;
vec4 color;
@ -94,9 +94,9 @@ typedef struct UIDrawableStruct {
uint32_t code;
uint32_t index;
uint32_t id;
} UIDrawable;
} GPUDrawable;
typedef struct UILayerStruct {
typedef struct GPULayerStruct {
VkDeviceAddress strings;
VkDeviceAddress codes;
@ -111,9 +111,9 @@ typedef struct UILayerStruct {
uint32_t num_drawables;
VkDeviceAddress container;
} UILayer;
} GPULayer;
typedef struct UIlayerStorageStruct {
typedef struct LayerStruct {
VkBuffer strings;
VkBuffer codes;
VkBuffer drawables;
@ -126,41 +126,61 @@ typedef struct UIlayerStorageStruct {
VkDeviceAddress address;
UIDrawable* drawables_buffer;
UIString* strings_buffer;
GPUDrawable* drawables_buffer;
GPUString* strings_buffer;
uint32_t* codes_buffer;
UILayer data;
} UILayerStorage;
GPULayer data;
} Layer;
typedef struct LayerInputStruct {
uint32_t num_strings;
uint32_t num_codes;
uint32_t num_drawables;
GPUString* strings;
uint32_t* codes;
GPUDrawable* drawables;
} LayerInput;
typedef struct UIContainerStruct {
typedef struct GPUContainerStruct {
vec2 offset;
vec2 size;
uint32_t anchor;
} UIContainer;
} GPUContainer;
typedef struct UIContainerStorageStruct {
typedef struct ContainerStruct {
VkBuffer container;
VmaAllocation container_memory;
VkDeviceAddress address;
UIContainer data;
GPUContainer data;
uint32_t id;
uint32_t layer_count;
Layer* layers;
} Container;
typedef struct ContainerInputStruct {
uint32_t id;
uint32_t anchor;
vec2 offset;
vec2 size;
uint32_t layer_count;
UILayerStorage* layers;
} UIContainerStorage;
LayerInput* layers;
} ContainerInput;
typedef struct UIContextStruct {
typedef struct GPUUIContextStruct {
VkDeviceAddress font_infos;
vec2 screen;
vec2 extent;
vec2 scale;
} UIContext;
} GPUUIContext;
typedef struct UIContextStorageStruct {
typedef struct UIContext {
VkDeviceAddress address;
VkBuffer context;
@ -185,21 +205,21 @@ typedef struct UIContextStorageStruct {
uint32_t max_fonts;
uint32_t max_textures;
FontStorage* fonts;
TextureStorage* texture_slots;
Font* fonts;
Texture* texture_slots;
uint32_t max_containers;
UIContainerStorage* containers;
Container* containers;
UIContext data;
} UIContextStorage;
GPUUIContext data;
} UIContext;
VkResult create_ui_context(
uint32_t max_fonts,
uint32_t max_textures,
uint32_t max_containers,
RenderContext* gpu,
UIContextStorage* memory);
UIContext* context);
VkResult load_font(
const char* ttf_file,
@ -207,45 +227,25 @@ VkResult load_font(
VkBool32 antialias,
FT_Library library,
RenderContext* gpu,
UIContextStorage* context,
UIContext* context,
uint32_t* index);
VkResult load_texture(
const char* png_path,
RenderContext* gpu,
UIContextStorage* context,
UIContext* context,
uint32_t* index);
typedef struct UILayerInputStruct {
uint32_t num_strings;
uint32_t num_codes;
uint32_t num_drawables;
UIString* strings;
uint32_t* codes;
UIDrawable* drawables;
} UILayerInput;
typedef struct UIContainerInputStruct {
uint32_t id;
uint32_t anchor;
vec2 offset;
vec2 size;
uint32_t layer_count;
UILayerInput* layers;
} UIContainerInput;
VkResult create_container(
UIContainerInput* container,
ContainerInput* container,
RenderContext* gpu,
UIContextStorage* context);
UIContext* context);
VkResult create_layer(
uint32_t index,
UILayerInput* input,
LayerInput* input,
RenderContext* gpu,
UIContainerStorage* container);
Container* container);
VkResult map_string(
const char * text,

@ -8,7 +8,7 @@
* - ui_context->containers[*].layers[*].address <- device address
* Basically, needs to be re-run whenever the number of layers/containers changes
*/
void record_ui_compute(VkCommandBuffer command_buffer, UIContextStorage* ui_context) {
void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context) {
VkDeviceAddress push[2] = {ui_context->address, 0};
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, ui_context->string_pipeline.pipeline);
@ -19,28 +19,28 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContextStorage* ui_cont
VkBufferMemoryBarrier draw_command_barrier_1 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.offset = offsetof(GPULayer, 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, 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));
command_copy_buffer(command_buffer, ui_context->containers[i].layers[j].layer, ui_context->containers[i].layers[j].layer, offsetof(GPULayer, num_drawables), offsetof(GPULayer, draw) + offsetof(DrawCommand, instance_count), sizeof(uint32_t));
VkBufferMemoryBarrier draw_command_barrier_2 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.offset = offsetof(GPULayer, draw),
.size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
};
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, ui_context->containers[i].layers[j].layer, offsetof(UILayer, dispatch_strings));
vkCmdDispatchIndirect(command_buffer, ui_context->containers[i].layers[j].layer, offsetof(GPULayer, dispatch_strings));
VkBufferMemoryBarrier draw_command_barrier_3 = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(UILayer, draw),
.offset = offsetof(GPULayer, draw),
.size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT,
@ -51,7 +51,7 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContextStorage* ui_cont
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.buffer = ui_context->containers[i].layers[j].drawables,
.offset = 0,
.size = sizeof(UIDrawable)*ui_context->containers[i].layers[j].data.max_drawables,
.size = sizeof(GPUDrawable)*ui_context->containers[i].layers[j].data.max_drawables,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
};
@ -68,7 +68,7 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContextStorage* ui_cont
* - ui_context->containers[*].layers[*].address <- device address
* Basically, needs to be re-run whenever the number of layers/containers changes
*/
void record_ui_draw(VkCommandBuffer command_buffer, UIContextStorage* ui_context) {
void record_ui_draw(VkCommandBuffer command_buffer, UIContext* ui_context) {
VkDeviceAddress push[2] = {ui_context->address, 0};
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->pipeline.pipeline);
@ -81,7 +81,7 @@ void record_ui_draw(VkCommandBuffer command_buffer, UIContextStorage* ui_context
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, ui_context->containers[i].layers[j].layer, offsetof(UILayer, draw), 1, 0);
vkCmdDrawIndirect(command_buffer, ui_context->containers[i].layers[j].layer, offsetof(GPULayer, draw), 1, 0);
}
}
}
@ -89,7 +89,7 @@ void record_ui_draw(VkCommandBuffer command_buffer, UIContextStorage* ui_context
VkResult record_draw_commands(
RenderContext* context,
UIContextStorage* ui_context) {
UIContext* ui_context) {
VkResult result;
for(uint32_t image_index = 0; image_index < context->swapchain_image_count; image_index++) {
VkCommandBuffer command_buffer = context->swapchain_command_buffers[image_index];

@ -10,30 +10,18 @@
#include "ft2build.h"
#include FT_FREETYPE_H
VkResult render_thread(GLFWwindow* window, RenderContext* render) {
FT_Library library;
UIContextStorage ui;
VkResult i_dont_know_yet(GLFWwindow* window, UIContext* ui, RenderContext* render) {
uint32_t font_index;
uint32_t background_index;
FT_Library library;
VkResult result;
VK_RESULT(create_ui_context(
10,
10,
10,
render,
&ui));
if(FT_Init_FreeType(&library) != FT_Err_Ok) {
return VK_ERROR_UNKNOWN;
}
VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, ui, &font_index));
VK_RESULT(load_font("test.ttf", 16, VK_TRUE, library, render, &ui, &font_index));
UIString context_strings[] = {
GPUString context_strings[] = {
{
.pos = {0, 16},
.size = 16,
@ -45,9 +33,9 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
};
uint32_t context_codes[100];
map_string("Example Text", context_codes, 0, ui.fonts[0].charmap, ui.fonts[0].num_symbols);
map_string("Example Text", context_codes, 0, ui->fonts[0].charmap, ui->fonts[0].num_symbols);
UILayerInput context_layers[] = {
LayerInput context_layers[] = {
{
.num_strings = 1,
.strings = context_strings,
@ -56,17 +44,17 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
},
};
UIContainerInput context_info = {
ContainerInput context_info = {
.id = 0x01,
.anchor = ANCHOR_TOP_LEFT,
.size = {WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT},
.layer_count = sizeof(context_layers)/sizeof(UILayerInput),
.layer_count = sizeof(context_layers)/sizeof(LayerInput),
.layers = context_layers,
};
vec2 map_size = {250, 200};
UIDrawable map_rects[] = {
GPUDrawable map_rects[] = {
{
.pos = {0.0, 0.0},
.size = {map_size[0], map_size[1]},
@ -75,24 +63,24 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
},
};
UILayerInput map_layers[] = {
LayerInput map_layers[] = {
{
.num_drawables = sizeof(map_rects)/sizeof(UIDrawable),
.num_drawables = sizeof(map_rects)/sizeof(GPUDrawable),
.drawables = map_rects,
},
};
UIContainerInput map_info = {
ContainerInput map_info = {
.id = 0x02,
.anchor = ANCHOR_TOP_RIGHT,
.size = {map_size[0], map_size[1]},
.layer_count = sizeof(map_layers)/sizeof(UILayerInput),
.layer_count = sizeof(map_layers)/sizeof(LayerInput),
.layers = map_layers,
};
vec2 inventory_size = {map_size[0], WINDOW_MIN_HEIGHT-map_size[1]};
UIDrawable inventory_rects[] = {
GPUDrawable inventory_rects[] = {
{
.pos = {0, 0},
.size = {inventory_size[0], inventory_size[1]},
@ -101,24 +89,24 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
},
};
UILayerInput inventory_layers[] = {
LayerInput inventory_layers[] = {
{
.num_drawables = sizeof(inventory_rects)/sizeof(UIDrawable),
.num_drawables = sizeof(inventory_rects)/sizeof(GPUDrawable),
.drawables = inventory_rects,
},
};
UIContainerInput inventory_info = {
ContainerInput inventory_info = {
.id = 0x03,
.anchor = ANCHOR_BOTTOM_RIGHT,
.size = {inventory_size[0], inventory_size[1]},
.layer_count = sizeof(inventory_layers)/sizeof(UILayerInput),
.layer_count = sizeof(inventory_layers)/sizeof(LayerInput),
.layers = inventory_layers,
};
vec2 chat_size = {WINDOW_MIN_WIDTH-inventory_size[0], 200};
UIDrawable chat_rects[] = {
GPUDrawable chat_rects[] = {
{
.pos = {0, 0},
.size = {chat_size[0], chat_size[1]},
@ -127,27 +115,34 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
},
};
UILayerInput chat_layers[] = {
LayerInput chat_layers[] = {
{
.num_drawables = sizeof(chat_rects)/sizeof(UIDrawable),
.num_drawables = sizeof(chat_rects)/sizeof(GPUDrawable),
.drawables = chat_rects,
},
};
UIContainerInput chat_info = {
ContainerInput chat_info = {
.id = 0x04,
.anchor = ANCHOR_BOTTOM_LEFT,
.size = {chat_size[0], chat_size[1]},
.layer_count = sizeof(chat_layers)/sizeof(UILayerInput),
.layer_count = sizeof(chat_layers)/sizeof(LayerInput),
.layers = chat_layers,
};
VK_RESULT(create_container(&context_info, render, &ui));
VK_RESULT(create_container(&map_info, render, &ui));
VK_RESULT(create_container(&inventory_info, render, &ui));
VK_RESULT(create_container(&chat_info, render, &ui));
VK_RESULT(create_container(&context_info, render, ui));
VK_RESULT(create_container(&map_info, render, ui));
VK_RESULT(create_container(&inventory_info, render, ui));
VK_RESULT(create_container(&chat_info, render, ui));
VK_RESULT(record_draw_commands(render, ui));
return VK_SUCCESS;
}
VkResult render_thread(GLFWwindow* window, RenderContext* render, UIContext* ui) {
VK_RESULT(record_draw_commands(render, &ui));
VkResult result;
double last_frame_time = glfwGetTime();
while(glfwWindowShouldClose(window) == 0) {
@ -165,6 +160,18 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render) {
return 0;
}
typedef enum ClientAuthStateEnum {
AUTH_SAVED,
AUTH_CREDENTIALS,
AUTH_ATTEMPT,
AUTH_ERROR,
AUTH_SUCCESS,
} ClientAuthState;
typedef struct ClientStateStruct {
} ClientState;
int logic_thread() {
return 0;
}
@ -227,11 +234,27 @@ int main() {
glfwSetCursorPosCallback(window, cursor_pos_callback);
RenderContext render = {};
if(init_vulkan(window, &render) != VK_SUCCESS) {
return 2;
}
UIContext ui = {};
VkResult result;
VK_RESULT(init_vulkan(window, &render));
VK_RESULT(create_ui_context(
10,
10,
10,
&render,
&ui));
//////////////////////////////////
/// Test Code
//////////////////////////////////
VK_RESULT(i_dont_know_yet(window, &ui, &render));
//////////////////////////////////
if(render_thread(window, &render) != VK_SUCCESS) {
if(render_thread(window, &render, &ui) != VK_SUCCESS) {
return 3;
}

@ -276,9 +276,9 @@ VkResult create_ui_pipeline(
}
VkResult create_container(
UIContainerInput* container,
ContainerInput* container,
RenderContext* gpu,
UIContextStorage* context) {
UIContext* context) {
uint32_t index = 0xFFFFFFFF;
for(uint32_t i = 0; i < context->max_containers; i++) {
if(context->containers[i].id == 0x00000000) {
@ -292,29 +292,29 @@ VkResult create_container(
VkResult result;
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIContainer), &context->containers[index].container, &context->containers[index].container_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUContainer), &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));
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(GPUContainer), &transfer, &transfer_memory, &mapped));
context->containers[index].data.offset[0] = container->offset[0];
context->containers[index].data.offset[1] = container->offset[1];
context->containers[index].data.size[0] = container->size[0];
context->containers[index].data.size[1] = container->size[1];
context->containers[index].data.anchor = container->anchor;
memcpy(mapped, &context->containers[index].data, sizeof(UIContainer));
memcpy(mapped, &context->containers[index].data, sizeof(GPUContainer));
VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool);
command_copy_buffer(command_buffer, transfer, context->containers[index].container, 0, 0, sizeof(UIContainer));
command_copy_buffer(command_buffer, transfer, context->containers[index].container, 0, 0, sizeof(GPUContainer));
VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue));
destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
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);
context->containers[index].layers = malloc(sizeof(Layer)*container->layer_count);
for(uint32_t i = 0; i < container->layer_count; i++) {
VK_RESULT(create_layer(i, &container->layers[i], gpu, &context->containers[index]));
}
@ -324,19 +324,19 @@ VkResult create_container(
VkResult create_layer(
uint32_t index,
UILayerInput* input,
LayerInput* input,
RenderContext* gpu,
UIContainerStorage* container) {
Container* container) {
VkResult result;
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, VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, sizeof(GPULayer), &container->layers[index].layer, &container->layers[index].layer_memory));
if(input->num_strings > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIString)*input->num_strings, &container->layers[index].strings, &container->layers[index].strings_memory));
container->layers[index].strings_buffer = malloc(sizeof(UIString)*input->num_strings);
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUString)*input->num_strings, &container->layers[index].strings, &container->layers[index].strings_memory));
container->layers[index].strings_buffer = malloc(sizeof(GPUString)*input->num_strings);
}
if(input->num_codes + input->num_drawables > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIDrawable)*(input->num_drawables + input->num_codes), &container->layers[index].drawables, &container->layers[index].drawables_memory));
container->layers[index].drawables_buffer = malloc(sizeof(UIDrawable)*input->num_drawables);
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUDrawable)*(input->num_drawables + input->num_codes), &container->layers[index].drawables, &container->layers[index].drawables_memory));
container->layers[index].drawables_buffer = malloc(sizeof(GPUDrawable)*input->num_drawables);
}
if(input->num_codes > 0) {
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(uint32_t)*input->num_codes, &container->layers[index].codes, &container->layers[index].codes_memory));
@ -346,7 +346,7 @@ VkResult create_layer(
VkBuffer transfer;
VmaAllocation transfer_memory;
void* mapped;
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UILayer) + sizeof(UIString) * input->num_strings + sizeof(UIDrawable)*input->num_drawables + sizeof(uint32_t)*input->num_codes, &transfer, &transfer_memory, &mapped));
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(GPULayer) + sizeof(GPUString) * input->num_strings + sizeof(GPUDrawable)*input->num_drawables + sizeof(uint32_t)*input->num_codes, &transfer, &transfer_memory, &mapped));
if(input->num_strings > 0) {
container->layers[index].data.strings = buffer_address(gpu->device, container->layers[index].strings);
@ -380,35 +380,35 @@ VkResult create_layer(
container->layers[index].data.max_strings = input->num_strings;
container->layers[index].data.num_drawables = input->num_drawables;
container->layers[index].data.container = container->address;
memcpy(mapped, &container->layers[index].data, sizeof(UILayer));
memcpy(mapped, &container->layers[index].data, sizeof(GPULayer));
VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool);
command_copy_buffer(command_buffer, transfer, container->layers[index].layer, 0, 0, sizeof(UILayer));
command_copy_buffer(command_buffer, transfer, container->layers[index].layer, 0, 0, sizeof(GPULayer));
if(input->num_strings > 0) {
UIString* strings = (UIString*)(mapped + sizeof(UILayer));
GPUString* strings = (GPUString*)(mapped + sizeof(GPULayer));
for(uint32_t i = 0; i < input->num_strings; i++) {
memcpy(&strings[i], &input->strings[i], sizeof(UIString));
memcpy(&container->layers[index].strings_buffer[i], &input->strings[i], sizeof(UIString));
memcpy(&strings[i], &input->strings[i], sizeof(GPUString));
memcpy(&container->layers[index].strings_buffer[i], &input->strings[i], sizeof(GPUString));
}
command_copy_buffer(command_buffer, transfer, container->layers[index].strings, sizeof(UILayer), 0, sizeof(UIString)*input->num_strings);
command_copy_buffer(command_buffer, transfer, container->layers[index].strings, sizeof(GPULayer), 0, sizeof(GPUString)*input->num_strings);
}
if(input->num_drawables > 0) {
UIDrawable* drawables = (UIDrawable*)(mapped + sizeof(UILayer) + sizeof(UIString)*input->num_strings);
GPUDrawable* drawables = (GPUDrawable*)(mapped + sizeof(GPULayer) + sizeof(GPUString)*input->num_strings);
for(uint32_t i = 0; i < input->num_drawables; i++) {
memcpy(&drawables[i], &input->drawables[i], sizeof(UIDrawable));
memcpy(&container->layers[index].drawables_buffer[i], &input->drawables[i], sizeof(UIDrawable));
memcpy(&drawables[i], &input->drawables[i], sizeof(GPUDrawable));
memcpy(&container->layers[index].drawables_buffer[i], &input->drawables[i], sizeof(GPUDrawable));
}
command_copy_buffer(command_buffer, transfer, container->layers[index].drawables, sizeof(UILayer) + sizeof(UIString)*input->num_strings, 0, sizeof(UIDrawable)*input->num_drawables);
command_copy_buffer(command_buffer, transfer, container->layers[index].drawables, sizeof(GPULayer) + sizeof(GPUString)*input->num_strings, 0, sizeof(GPUDrawable)*input->num_drawables);
}
if(input->num_codes > 0) {
uint32_t* codes = (uint32_t*)(mapped + sizeof(UILayer) + sizeof(UIString)*input->num_strings + sizeof(UIDrawable)*input->num_drawables);
uint32_t* codes = (uint32_t*)(mapped + sizeof(GPULayer) + sizeof(GPUString)*input->num_strings + sizeof(GPUDrawable)*input->num_drawables);
for(uint32_t i = 0; i < input->num_codes; i++) {
codes[i] = input->codes[i];
container->layers[index].codes_buffer[i] = input->codes[i];
}
command_copy_buffer(command_buffer, transfer, container->layers[index].codes, sizeof(UILayer) + sizeof(UIString)*input->num_strings + sizeof(UIDrawable)*input->num_drawables, 0, sizeof(uint32_t)*input->num_codes);
command_copy_buffer(command_buffer, transfer, container->layers[index].codes, sizeof(GPULayer) + sizeof(GPUString)*input->num_strings + sizeof(GPUDrawable)*input->num_drawables, 0, sizeof(uint32_t)*input->num_codes);
}
VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue));
vkQueueWaitIdle(gpu->transfer_queue.handle);
@ -422,7 +422,7 @@ VkResult create_layer(
VkResult load_texture(
const char* png_path,
RenderContext* gpu,
UIContextStorage* context,
UIContext* context,
uint32_t* index) {
*index = 0xFFFFFFFF;
for(uint32_t i = 0; i < context->max_textures; i++) {
@ -636,7 +636,7 @@ VkResult load_font(
VkBool32 antialias,
FT_Library library,
RenderContext* gpu,
UIContextStorage* context,
UIContext* context,
uint32_t* index){
FT_Face face;
@ -669,8 +669,8 @@ VkResult load_font(
}
uint32_t* tmp_charmap = malloc(sizeof(uint32_t)*face->num_glyphs);
SymbolInfo* symbols = malloc(sizeof(SymbolInfo)*face->num_glyphs);
Font info;
GPUSymbol* symbols = malloc(sizeof(GPUSymbol)*face->num_glyphs);
GPUFont info;
uint32_t glyph_index;
uint32_t max_height = 0;
@ -735,7 +735,7 @@ VkResult load_font(
}
VkResult result;
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(SymbolInfo)*info.num_symbols, &context->fonts[*index].symbols, &context->fonts[*index].symbol_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUSymbol)*info.num_symbols, &context->fonts[*index].symbols, &context->fonts[*index].symbol_memory));
VkImageCreateInfo image_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
@ -761,20 +761,20 @@ VkResult load_font(
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));
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(GPUFont) + image_size*info.num_symbols + sizeof(GPUSymbol)*info.num_symbols, &transfer, &transfer_memory, &mapped));
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));
memcpy(mapped + image_size*info.num_symbols + sizeof(Font), symbols, sizeof(SymbolInfo)*info.num_symbols);
memcpy(mapped + image_size*info.num_symbols, &info, sizeof(GPUFont));
memcpy(mapped + image_size*info.num_symbols + sizeof(GPUFont), symbols, sizeof(GPUSymbol)*info.num_symbols);
free(images);
free(symbols);
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, context->fonts[*index].symbols, image_size*info.num_symbols + sizeof(Font), 0, sizeof(SymbolInfo)*info.num_symbols);
command_copy_buffer(command_buffer, transfer, context->font_infos, image_size*info.num_symbols, *index*sizeof(GPUFont), sizeof(GPUFont));
command_copy_buffer(command_buffer, transfer, context->fonts[*index].symbols, image_size*info.num_symbols + sizeof(GPUFont), 0, sizeof(GPUSymbol)*info.num_symbols);
VkImageMemoryBarrier first_barrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
@ -1063,48 +1063,48 @@ VkResult create_ui_context(
uint32_t max_textures,
uint32_t max_containers,
RenderContext* gpu,
UIContextStorage* memory) {
UIContext* context) {
VkResult result;
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(UIContext), &memory->context, &memory->context_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(Font)*max_fonts, &memory->font_infos, &memory->font_infos_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUUIContext), &context->context, &context->context_memory));
VK_RESULT(create_storage_buffer(gpu->allocator, 0, sizeof(GPUFont)*max_fonts, &context->font_infos, &context->font_infos_memory));
VkBuffer transfer;
VmaAllocation transfer_memory;
VmaAllocation transfer_context;
UIContext* mapped;
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UIContext), &transfer, &transfer_memory, (void**)&mapped));
VK_RESULT(create_transfer_buffer(gpu->allocator, sizeof(UIContext), &transfer, &transfer_context, (void**)&mapped));
memory->data.font_infos = buffer_address(gpu->device, memory->font_infos);
memory->data.screen[0] = gpu->window_scale[0] / gpu->swapchain_extent.width;
memory->data.screen[1] = gpu->window_scale[1] / gpu->swapchain_extent.height;
memory->data.extent[0] = gpu->swapchain_extent.width / gpu->window_scale[0];
memory->data.extent[1] = gpu->swapchain_extent.height / gpu->window_scale[1];
memory->data.scale[0] = gpu->window_scale[0];
memory->data.scale[1] = gpu->window_scale[1];
memcpy(mapped, &memory->data, sizeof(UIContext));
context->data.font_infos = buffer_address(gpu->device, context->font_infos);
context->data.screen[0] = gpu->window_scale[0] / gpu->swapchain_extent.width;
context->data.screen[1] = gpu->window_scale[1] / gpu->swapchain_extent.height;
context->data.extent[0] = gpu->swapchain_extent.width / gpu->window_scale[0];
context->data.extent[1] = gpu->swapchain_extent.height / gpu->window_scale[1];
context->data.scale[0] = gpu->window_scale[0];
context->data.scale[1] = gpu->window_scale[1];
memcpy(mapped, &context->data, sizeof(GPUUIContext));
VkCommandBuffer command_buffer = command_begin_single(gpu->device, gpu->transfer_pool);
command_copy_buffer(command_buffer, transfer, memory->context, 0, 0, sizeof(UIContext));
command_copy_buffer(command_buffer, transfer, context->context, 0, 0, sizeof(GPUUIContext));
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->context);
destroy_transfer_buffer(gpu->allocator, transfer, transfer_context);
context->address = buffer_address(gpu->device, context->context);
VK_RESULT(create_ui_descriptor(gpu->device, max_fonts, max_textures, &memory->samplers_layout, &memory->textures_layout, &memory->samplers, &memory->textures, &memory->font_samplers, &memory->font_textures, &memory->fonts_pool, &memory->textures_pool));
VK_RESULT(create_ui_descriptor(gpu->device, max_fonts, max_textures, &context->samplers_layout, &context->textures_layout, &context->samplers, &context->textures, &context->font_samplers, &context->font_textures, &context->fonts_pool, &context->textures_pool));
VK_RESULT(create_ui_pipeline(gpu->device, gpu->render_pass, memory->samplers_layout, memory->textures_layout, &memory->pipeline, &memory->string_pipeline));
VK_RESULT(create_ui_pipeline(gpu->device, gpu->render_pass, context->samplers_layout, context->textures_layout, &context->pipeline, &context->string_pipeline));
memory->max_textures = max_textures;
memory->max_fonts = max_fonts;
memory->max_containers = max_containers;
context->max_textures = max_textures;
context->max_fonts = max_fonts;
context->max_containers = 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));
context->texture_slots = malloc(max_textures*sizeof(Texture));
memset(context->texture_slots, 0, max_textures*sizeof(Texture));
context->fonts = malloc(max_fonts*sizeof(Font));
memset(context->fonts, 0, max_fonts*sizeof(Font));
context->containers = malloc(max_containers*sizeof(Container));
memset(context->containers, 0, max_containers*sizeof(Container));
return VK_SUCCESS;
}