Fixed wrong code offset in text compute shader, moved functions out of main.c

main
noah metz 2024-10-17 11:06:09 -06:00
parent 717a072b3d
commit 7b916b0012
6 changed files with 288 additions and 248 deletions

@ -86,6 +86,20 @@ typedef struct FontDescriptorStruct {
VkDescriptorSet set;
} FontDescriptor;
typedef struct TextPointersMemoryStruct {
VmaAllocation pointers_memory;
VmaAllocation draw_memory;
VmaAllocation strings_memory;
VmaAllocation codes_memory;
VmaAllocation characters_memory;
VkBuffer pointers_buffer;
VkBuffer draw_buffer;
VkBuffer strings_buffer;
VkBuffer codes_buffer;
VkBuffer characters_buffer;
} TextPointersMemory;
typedef struct UILayerStruct {
VkBuffer colored_rects;
uint32_t colored_rect_count;
@ -95,11 +109,10 @@ typedef struct UILayerStruct {
VkDescriptorSet textured_rect_descriptor;
VkBuffer string_draw;
VkBuffer string_draw_clear;
uint32_t chars_size;
VkBuffer chars;
VkDeviceAddress string_pointers;
uint32_t string_count;
uint32_t chars_count;
FontDescriptor font;
} UILayer;
@ -127,8 +140,38 @@ typedef struct UIContextStruct {
ComputePipeline ui_compute_text;
} UIContext;
VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, vec2 window_scale, VkRenderPass render_pass, Queue transfer_queue, VkCommandPool transfer_pool, UIContext* context);
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 init_pipelines(
VkDevice device,
VmaAllocator allocator,
VkExtent2D swapchain_extent,
vec2 window_scale,
VkRenderPass render_pass,
Queue transfer_queue,
VkCommandPool transfer_pool,
UIContext* context);
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 create_text_pointers(
uint32_t max_strings,
uint32_t max_characters,
VkDevice device,
VmaAllocator allocator,
VkCommandPool transfer_pool,
Queue transfer_queue,
TextPointersMemory* memory,
VkDeviceAddress* address);
#endif

@ -85,7 +85,22 @@ typedef struct RenderContextStruct {
} RenderContext;
GLFWwindow* init_window();
VkResult init_vulkan(GLFWwindow* window, RenderContext* context);
VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_layers, uint32_t ui_layer_count);
VkResult init_vulkan(
GLFWwindow* window,
RenderContext* context);
VkResult draw_frame(
RenderContext* context,
UIContext* ui_context,
UILayer* ui_layers,
uint32_t ui_layer_count);
VkResult create_transfer_buffer(
VkDeviceSize size,
VmaAllocator allocator,
VkBuffer* buffer,
VmaAllocation* memory,
void** mapped);
#endif

@ -74,12 +74,12 @@ void main() {
uint buffer_pos = atomicAdd(push.pointers.draw.instance_count, string.len);
float x = 0;
for(uint i = 0; i < string.len; i++) {
Symbol symbol = font.symbol_list.symbols[push.pointers.codes.codes[buffer_pos + i]];
Symbol symbol = font.symbol_list.symbols[push.pointers.codes.codes[string.offset + i]];
push.pointers.characters.characters[buffer_pos + i].pos = string.pos + vec3(x, 0, 0);
x += string.size*symbol.advance/font.width;
push.pointers.characters.characters[buffer_pos + i].size = string.size;
push.pointers.characters.characters[buffer_pos + i].color = string.color;
push.pointers.characters.characters[buffer_pos + i].code = push.pointers.codes.codes[buffer_pos + i];
push.pointers.characters.characters[buffer_pos + i].code = push.pointers.codes.codes[string.offset + i];
}
// Write the characters to push.pointers.characters.characters[buffer_pos:buffer_pos+string.len)
}

@ -17,218 +17,6 @@ ColoredRect colored_rect(float width, float height, float r, float g, float b, f
return rect;
}
typedef struct TextPointersMemoryStruct {
VmaAllocation pointers_memory;
VmaAllocation draw_memory;
VmaAllocation strings_memory;
VmaAllocation codes_memory;
VmaAllocation characters_memory;
VmaAllocation draw_clear_memory;
VkBuffer pointers_buffer;
VkBuffer draw_buffer;
VkBuffer draw_clear_buffer;
VkBuffer strings_buffer;
VkBuffer codes_buffer;
VkBuffer characters_buffer;
} TextPointersMemory;
VkResult create_text_pointers(
uint32_t max_strings,
uint32_t max_characters,
VkDevice device,
VmaAllocator allocator,
VkCommandPool transfer_pool,
Queue transfer_queue,
TextPointersMemory* memory,
VkDeviceAddress* address) {
VkResult result;
VmaAllocationCreateInfo memory_info = {
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
VkBufferCreateInfo pointers_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(StringPointers),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &pointers_info, &memory_info, &memory->pointers_buffer, &memory->pointers_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo draw_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &draw_info, &memory_info, &memory->draw_buffer, &memory->draw_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo draw_clear_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &draw_clear_info, &memory_info, &memory->draw_clear_buffer, &memory->draw_clear_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo strings_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_strings*sizeof(String),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &strings_info, &memory_info, &memory->strings_buffer, &memory->strings_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo codes_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_characters*sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &codes_info, &memory_info, &memory->codes_buffer, &memory->codes_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo characters_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_characters*sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &characters_info, &memory_info, &memory->characters_buffer, &memory->characters_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferDeviceAddressInfo pointers_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->pointers_buffer,
};
VkBufferDeviceAddressInfo strings_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->strings_buffer,
};
VkBufferDeviceAddressInfo codes_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->codes_buffer,
};
VkBufferDeviceAddressInfo characters_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->characters_buffer,
};
VkBufferDeviceAddressInfo draw_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->draw_buffer,
};
VkBuffer temp_buffer;
VmaAllocation temp_memory;
VkBufferCreateInfo temp_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = sizeof(StringPointers) +
sizeof(DrawCommand),
};
VmaAllocationCreateInfo temp_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(allocator, &temp_buffer_info, &temp_memory_info, &temp_buffer, &temp_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
void* mapped;
result = vmaMapMemory(allocator, temp_memory, &mapped);
if(result != VK_SUCCESS) {
return result;
}
DrawCommand draw = {
.index_count = 6,
};
memcpy(mapped + 0, &draw, sizeof(DrawCommand));
StringPointers pointers = {
.strings = vkGetBufferDeviceAddress(device, &strings_address),
.codes = vkGetBufferDeviceAddress(device, &codes_address),
.characters = vkGetBufferDeviceAddress(device, &characters_address),
.draw = vkGetBufferDeviceAddress(device, &draw_address),
};
memcpy(mapped + sizeof(DrawCommand), &pointers, sizeof(StringPointers));
vmaUnmapMemory(allocator, temp_memory);
VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool);
VkBufferCopy copy_draw = {
.size = sizeof(DrawCommand),
.srcOffset = 0,
};
vkCmdCopyBuffer(command_buffer, temp_buffer, memory->draw_buffer, 1, &copy_draw);
vkCmdCopyBuffer(command_buffer, temp_buffer, memory->draw_clear_buffer, 1, &copy_draw);
VkBufferCopy copy_pointers = {
.size = sizeof(StringPointers),
.srcOffset = sizeof(DrawCommand),
};
vkCmdCopyBuffer(command_buffer, temp_buffer, memory->pointers_buffer, 1, &copy_pointers);
result = command_end_single(device, command_buffer, transfer_pool, transfer_queue);
if(result != VK_SUCCESS) {
return result;
}
vmaDestroyBuffer(allocator, temp_buffer, temp_memory);
*address = vkGetBufferDeviceAddress(device, &pointers_address);
return VK_SUCCESS;
}
VkResult create_transfer_buffer(VkDeviceSize size, VmaAllocator allocator, VkBuffer* buffer, VmaAllocation* memory, void** mapped) {
VkBufferCreateInfo buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = size,
};
VmaAllocationCreateInfo memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
VkResult result = vmaCreateBuffer(allocator, &buffer_info, &memory_info, buffer, memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
result = vmaMapMemory(allocator, *memory, mapped);
if(result != VK_SUCCESS) {
vmaDestroyBuffer(allocator, *buffer, *memory);
return result;
}
return VK_SUCCESS;
}
VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkResult result;
UIContext ui_context;
@ -283,7 +71,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
TextPointersMemory text_pointers;
VkDeviceAddress text_pointers_address;
result = create_text_pointers(10, 100, render_context->device, render_context->allocator, render_context->transfer_pool, render_context->transfer_queue, &text_pointers, &text_pointers_address);
result = create_text_pointers(2, 100, render_context->device, render_context->allocator, render_context->transfer_pool, render_context->transfer_queue, &text_pointers, &text_pointers_address);
if(result != VK_SUCCESS) {
return result;
}
@ -291,37 +79,48 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkBuffer temp_buffer;
VmaAllocation temp_memory;
void* mapped;
result = create_transfer_buffer(sizeof(String) + 100*sizeof(uint32_t), render_context->allocator, &temp_buffer, &temp_memory, (void**)&mapped);
if(result != VK_SUCCESS) {
return result;
}
String* mapped_string = (String*)mapped;
mapped_string->pos[0] = 200.0;
mapped_string->pos[1] = 200.0;
mapped_string->pos[2] = 0.5;
mapped_string->color[0] = 1.0;
mapped_string->color[1] = 1.0;
mapped_string->color[2] = 1.0;
mapped_string->color[3] = 1.0;
mapped_string->size = 50.0;
mapped_string->length = 100;
mapped_string->offset = 0;
uint32* mapped_codes = (uint32_t*)(mapped + sizeof(String));
result = create_transfer_buffer(2*sizeof(String) + 100*sizeof(uint32_t), render_context->allocator, &temp_buffer, &temp_memory, (void**)&mapped);
if(result != VK_SUCCESS) {
return result;
}
String* mapped_strings = (String*)mapped;
mapped_strings[0].pos[0] = 0;
mapped_strings[0].pos[1] = 200.0;
mapped_strings[0].pos[2] = 0.2;
mapped_strings[0].color[0] = 1.0;
mapped_strings[0].color[1] = 1.0;
mapped_strings[0].color[2] = 1.0;
mapped_strings[0].color[3] = 1.0;
mapped_strings[0].size = 50.0;
mapped_strings[0].length = 50;
mapped_strings[0].offset = 0;
mapped_strings[1].pos[0] = 0;
mapped_strings[1].pos[1] = 250.0;
mapped_strings[1].pos[2] = 0.1;
mapped_strings[1].color[0] = 1.0;
mapped_strings[1].color[1] = 1.0;
mapped_strings[1].color[2] = 1.0;
mapped_strings[1].color[3] = 1.0;
mapped_strings[1].size = 50.0;
mapped_strings[1].length = 50;
mapped_strings[1].offset = 50;
uint32* mapped_codes = (uint32_t*)(mapped + 2*sizeof(String));
for(uint32_t i = 0; i < 100; i++) {
mapped_codes[i] = i;
}
VkCommandBuffer command_buffer = command_begin_single(render_context->device, render_context->transfer_pool);
VkBufferCopy copy_string = {
.size = sizeof(String),
.size = 2*sizeof(String),
.srcOffset = 0,
};
vkCmdCopyBuffer(command_buffer, temp_buffer, text_pointers.strings_buffer, 1, &copy_string);
VkBufferCopy copy_codes = {
.size = 100*sizeof(uint32_t),
.srcOffset = sizeof(String),
.srcOffset = 2*sizeof(String),
};
vkCmdCopyBuffer(command_buffer, temp_buffer, text_pointers.codes_buffer, 1, &copy_codes);
result = command_end_single(render_context->device, command_buffer, render_context->transfer_pool, render_context->transfer_queue);
@ -338,12 +137,11 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
.colored_rect_count = 3,
.font = test_font,
.chars_size = 10*sizeof(Character),
.chars = text_pointers.characters_buffer,
.string_count = 1,
.string_count = 2,
.chars_count = 100,
.string_pointers = text_pointers_address,
.string_draw = text_pointers.draw_buffer,
.string_draw_clear = text_pointers.draw_clear_buffer,
};
while(glfwWindowShouldClose(window) == 0) {

@ -459,6 +459,164 @@ VkResult create_font_descriptor_pools(VkDevice device, uint32_t max_sets, VkDesc
return VK_SUCCESS;
}
VkResult create_text_pointers(
uint32_t max_strings,
uint32_t max_characters,
VkDevice device,
VmaAllocator allocator,
VkCommandPool transfer_pool,
Queue transfer_queue,
TextPointersMemory* memory,
VkDeviceAddress* address) {
VkResult result;
VmaAllocationCreateInfo memory_info = {
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
VkBufferCreateInfo pointers_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(StringPointers),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &pointers_info, &memory_info, &memory->pointers_buffer, &memory->pointers_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo draw_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &draw_info, &memory_info, &memory->draw_buffer, &memory->draw_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo strings_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_strings*sizeof(String),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &strings_info, &memory_info, &memory->strings_buffer, &memory->strings_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo codes_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_characters*sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &codes_info, &memory_info, &memory->codes_buffer, &memory->codes_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo characters_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = max_characters*sizeof(DrawCommand),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
result = vmaCreateBuffer(allocator, &characters_info, &memory_info, &memory->characters_buffer, &memory->characters_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferDeviceAddressInfo pointers_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->pointers_buffer,
};
VkBufferDeviceAddressInfo strings_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->strings_buffer,
};
VkBufferDeviceAddressInfo codes_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->codes_buffer,
};
VkBufferDeviceAddressInfo characters_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->characters_buffer,
};
VkBufferDeviceAddressInfo draw_address = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = memory->draw_buffer,
};
VkBuffer temp_buffer;
VmaAllocation temp_memory;
VkBufferCreateInfo temp_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = sizeof(StringPointers) +
sizeof(DrawCommand),
};
VmaAllocationCreateInfo temp_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(allocator, &temp_buffer_info, &temp_memory_info, &temp_buffer, &temp_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
void* mapped;
result = vmaMapMemory(allocator, temp_memory, &mapped);
if(result != VK_SUCCESS) {
return result;
}
DrawCommand draw = {
.index_count = 6,
};
memcpy(mapped + 0, &draw, sizeof(DrawCommand));
StringPointers pointers = {
.strings = vkGetBufferDeviceAddress(device, &strings_address),
.codes = vkGetBufferDeviceAddress(device, &codes_address),
.characters = vkGetBufferDeviceAddress(device, &characters_address),
.draw = vkGetBufferDeviceAddress(device, &draw_address),
};
memcpy(mapped + sizeof(DrawCommand), &pointers, sizeof(StringPointers));
vmaUnmapMemory(allocator, temp_memory);
VkCommandBuffer command_buffer = command_begin_single(device, transfer_pool);
VkBufferCopy copy_draw = {
.size = sizeof(DrawCommand),
.srcOffset = 0,
};
vkCmdCopyBuffer(command_buffer, temp_buffer, memory->draw_buffer, 1, &copy_draw);
VkBufferCopy copy_pointers = {
.size = sizeof(StringPointers),
.srcOffset = sizeof(DrawCommand),
};
vkCmdCopyBuffer(command_buffer, temp_buffer, memory->pointers_buffer, 1, &copy_pointers);
result = command_end_single(device, command_buffer, transfer_pool, transfer_queue);
if(result != VK_SUCCESS) {
return result;
}
vmaDestroyBuffer(allocator, temp_buffer, temp_memory);
*address = vkGetBufferDeviceAddress(device, &pointers_address);
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) {
FT_Face face;

@ -1047,10 +1047,11 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
vkCmdFillBuffer(command_buffer, ui_layers[i].string_draw, offsetof(DrawCommand, instance_count), sizeof(uint32_t), 0);
VkBufferMemoryBarrier clear_barrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.size = sizeof(DrawCommand),
.size = sizeof(uint32_t),
.offset = offsetof(DrawCommand, instance_count),
.buffer = ui_layers[i].string_draw,
.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.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);
@ -1059,7 +1060,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
VkBufferMemoryBarrier character_compute_barrier = {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.size = ui_layers[i].chars_size,
.size = ui_layers[i].chars_count,
.buffer = ui_layers[i].chars,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
@ -1154,3 +1155,28 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
return VK_SUCCESS;
}
VkResult create_transfer_buffer(VkDeviceSize size, VmaAllocator allocator, VkBuffer* buffer, VmaAllocation* memory, void** mapped) {
VkBufferCreateInfo buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = size,
};
VmaAllocationCreateInfo memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
VkResult result = vmaCreateBuffer(allocator, &buffer_info, &memory_info, buffer, memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
result = vmaMapMemory(allocator, *memory, mapped);
if(result != VK_SUCCESS) {
vmaDestroyBuffer(allocator, *buffer, *memory);
return result;
}
return VK_SUCCESS;
}