Seperated font into descriptor and memory

noah metz 2024-10-18 10:14:12 -06:00
parent 22d3b62b12
commit 82979bde71
4 changed files with 34 additions and 51 deletions

@ -76,7 +76,7 @@ typedef struct SymbolInfoStruct {
uint32_t advance;
} SymbolInfo;
typedef struct FontDescriptorStruct {
typedef struct FontStorageStruct {
VmaAllocation symbol_memory;
VmaAllocation uniform_memory;
VmaAllocation image_memory;
@ -85,8 +85,7 @@ typedef struct FontDescriptorStruct {
VkImage image;
VkImageView view;
VkSampler sampler;
VkDescriptorSet set;
} FontDescriptor;
} FontStorage;
typedef struct TextPointersMemoryStruct {
VmaAllocation pointers_memory;
@ -106,12 +105,16 @@ typedef struct UILayerStruct {
VkDeviceAddress colored_rects;
uint32_t colored_rect_count;
VkDeviceAddress textured_rects;
uint32_t textured_rect_count;
VkDescriptorSet texture_array;
VkBuffer string_draw;
VkBuffer chars;
VkDeviceAddress string_pointers;
uint32_t string_count;
uint32_t chars_count;
FontDescriptor font;
VkDescriptorSet font;
} UILayer;
typedef struct UIContextStruct {
@ -129,28 +132,6 @@ typedef struct UIContextStruct {
ComputePipeline ui_compute_text;
} UIContext;
typedef struct UIFontStruct {
} UIFont;
typedef struct UITextStruct {
} UIText;
typedef struct UICharacterStruct {
} UICharacter;
typedef struct UIElementStruct {
uint32_t type;
uint32_t offset;
} UIElement;
typedef struct UIContainerStruct {
VkDeviceAddress elements;
} UIContainer;
typedef struct UIStruct {
} UI;
VkResult init_pipelines(
VkDevice device,
VmaAllocator allocator,
@ -171,7 +152,8 @@ VkResult load_font(
uint32_t size,
VkBool32 antialias,
uint32_t** charmap,
FontDescriptor* descriptor);
FontStorage* memory,
VkDescriptorSet* font);
VkResult create_text_pointers(
uint32_t max_strings,

@ -24,9 +24,10 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
return VK_ERROR_UNKNOWN;
}
FontDescriptor test_font;
FontStorage font_memory;
VkDescriptorSet font;
uint32_t* charmap;
result = load_font(render_context->device, render_context->allocator, ui_context.font_layout, ui_context.font_pool, render_context->transfer_pool, render_context->transfer_queue, library, "test.ttf", 16, VK_TRUE, &charmap, &test_font);
result = load_font(render_context->device, render_context->allocator, ui_context.font_layout, ui_context.font_pool, render_context->transfer_pool, render_context->transfer_queue, library, "test.ttf", 16, VK_TRUE, &charmap, &font_memory, &font);
if(result != VK_SUCCESS) {
return result;
}
@ -160,7 +161,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
.colored_rects = vkGetBufferDeviceAddress(render_context->device, &colored_rect_address_info),
.colored_rect_count = 3,
.font = test_font,
.font = font,
.chars = text_pointers.characters_buffer,
.string_count = 2,
.chars_count = 100,

@ -1057,7 +1057,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
};
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_DEPENDENCY_BY_REGION_BIT, 0, NULL, 1, &clear_barrier, 0, NULL);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, ui_context->ui_compute_text.layout, 0, 1, &ui_layers[i].font.set, 0, NULL);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, ui_context->ui_compute_text.layout, 0, 1, &ui_layers[i].font, 0, NULL);
vkCmdPushConstants(command_buffer, ui_context->ui_pipeline_text.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, &ui_layers[i].string_pointers);
vkCmdDispatch(command_buffer, ui_layers[i].string_count, 1, 1);
@ -1104,7 +1104,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
for(uint32_t i = 0; i < ui_layer_count; i++) {
if(ui_layers[i].string_count > 0) {
// Bind Font Descriptor
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 1, 1, &ui_layers[i].font.set, 0, NULL);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 1, 1, &ui_layers[i].font, 0, NULL);
// Push pointers
vkCmdPushConstants(command_buffer, ui_context->ui_pipeline_text.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_COMPUTE_BIT, 0, 8, &ui_layers[i].string_pointers);
vkCmdDrawIndirect(command_buffer, ui_layers[i].string_draw, 0, 1, sizeof(DrawCommand));

@ -556,7 +556,7 @@ VkResult create_text_pointers(
return VK_SUCCESS;
}
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, uint32_t** charmap, FontDescriptor* descriptor) {
VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayout layout, VkDescriptorPool pool,VkCommandPool transfer_pool, Queue transfer_queue, FT_Library library, const char* ttf_file, uint32_t size, VkBool32 antialias, uint32_t** charmap, FontStorage* memory, VkDescriptorSet* font) {
FT_Face face;
int error;
@ -643,7 +643,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.descriptorPool = pool,
};
result = vkAllocateDescriptorSets(device, &set_allocate_info, &descriptor->set);
result = vkAllocateDescriptorSets(device, &set_allocate_info, font);
if(result != VK_SUCCESS) {
return result;
}
@ -657,7 +657,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
VmaAllocationCreateInfo symbol_memory_info = {
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
result = vmaCreateBuffer(allocator, &symbol_buffer_info, &symbol_memory_info, &descriptor->symbols, &descriptor->symbol_memory, NULL);
result = vmaCreateBuffer(allocator, &symbol_buffer_info, &symbol_memory_info, &memory->symbols, &memory->symbol_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
@ -673,7 +673,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
result = vmaCreateBuffer(allocator, &uniform_buffer_info, &uniform_memory_info, &descriptor->uniform, &descriptor->uniform_memory, NULL);
result = vmaCreateBuffer(allocator, &uniform_buffer_info, &uniform_memory_info, &memory->uniform, &memory->uniform_memory, NULL);
VkImageCreateInfo image_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
@ -694,7 +694,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
result = vmaCreateImage(allocator, &image_info, &image_memory_info, &descriptor->image, &descriptor->image_memory, NULL);
result = vmaCreateImage(allocator, &image_info, &image_memory_info, &memory->image, &memory->image_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
@ -725,7 +725,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
memcpy(mapped_staging + image_size*uniform.num_symbols + sizeof(FontUniform), symbols, sizeof(SymbolInfo)*uniform.num_symbols);
VkBufferDeviceAddressInfo address_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = descriptor->symbols,
.buffer = memory->symbols,
};
uniform.symbol_list = vkGetBufferDeviceAddress(device, &address_info);
memcpy(mapped_staging + image_size*uniform.num_symbols, &uniform, sizeof(FontUniform));
@ -741,18 +741,18 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.srcOffset = image_size*uniform.num_symbols,
.dstOffset = 0,
};
vkCmdCopyBuffer(command_buffer, staging_buffer, descriptor->uniform, 1, &uniform_copy_info);
vkCmdCopyBuffer(command_buffer, staging_buffer, memory->uniform, 1, &uniform_copy_info);
VkBufferCopy symbol_copy_info = {
.size = sizeof(SymbolInfo)*uniform.num_symbols,
.srcOffset = image_size*uniform.num_symbols + sizeof(FontUniform),
.dstOffset = 0,
};
vkCmdCopyBuffer(command_buffer, staging_buffer, descriptor->symbols, 1, &symbol_copy_info);
vkCmdCopyBuffer(command_buffer, staging_buffer, memory->symbols, 1, &symbol_copy_info);
VkImageMemoryBarrier first_barrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = descriptor->image,
.image = memory->image,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
@ -770,11 +770,11 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.imageExtent = image_info.extent,
};
vkCmdCopyBufferToImage(command_buffer, staging_buffer, descriptor->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy);
vkCmdCopyBufferToImage(command_buffer, staging_buffer, memory->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy);
VkImageMemoryBarrier second_barrier = {
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
.image = descriptor->image,
.image = memory->image,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
@ -795,7 +795,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
VkImageViewCreateInfo view_info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = descriptor->image,
.image = memory->image,
.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
.format = VK_FORMAT_R8G8B8A8_SRGB,
.subresourceRange = {
@ -806,7 +806,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.baseArrayLayer = 0,
},
};
result = vkCreateImageView(device, &view_info, NULL, &descriptor->view);
result = vkCreateImageView(device, &view_info, NULL, &memory->view);
if(result != VK_SUCCESS) {
return result;
}
@ -819,7 +819,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
};
result = vkCreateSampler(device, &sampler_info, NULL, &descriptor->sampler);
result = vkCreateSampler(device, &sampler_info, NULL, &memory->sampler);
if(result != VK_SUCCESS) {
return result;
}
@ -827,19 +827,19 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
VkDescriptorBufferInfo desc_uniform_info = {
.offset = 0,
.range = sizeof(FontUniform),
.buffer = descriptor->uniform,
.buffer = memory->uniform,
};
VkDescriptorImageInfo desc_image_info = {
.sampler = descriptor->sampler,
.sampler = memory->sampler,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.imageView = descriptor->view,
.imageView = memory->view,
};
VkWriteDescriptorSet descriptor_writes[] = {
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descriptor->set,
.dstSet = *font,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
@ -848,7 +848,7 @@ VkResult load_font(VkDevice device, VmaAllocator allocator, VkDescriptorSetLayou
},
{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = descriptor->set,
.dstSet = *font,
.dstBinding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1,