Got characters rendering from two buffer addresses

main
noah metz 2024-10-14 22:10:14 -06:00
parent eab26cf72c
commit 6988abcd86
5 changed files with 57 additions and 70 deletions

@ -37,7 +37,7 @@ typedef struct TextStruct {
typedef struct CharStruct {
vec3 pos;
vec4 color;
vec2 size;
float size;
uint32_t code;
} Char;
@ -80,6 +80,7 @@ typedef struct UILayerStruct {
uint32_t textured_rect_count;
VkDescriptorSet textured_rect_descriptor;
VkDeviceAddress texts_address;
VkBuffer texts;
uint32_t text_count;
FontDescriptor font;
@ -102,7 +103,7 @@ typedef struct UIContextStruct {
VkDescriptorPool font_pool;
VkDescriptorSetLayout font_layout;
struct RectBuffer ui_rect;
GraphicsPipeline ui_pipeline_rect;
GraphicsPipeline ui_pipeline_text;

@ -8,6 +8,9 @@ struct Symbol {
};
struct Character {
vec3 pos;
vec4 color;
float size;
uint code;
};
@ -29,30 +32,29 @@ layout(set = 1, binding = 0) uniform FontUniform {
uint height;
uint space_width;
SymbolList symbol_list;
CharacterList characters;
} font;
layout(std430, push_constant) uniform Push {
CharacterList characters;
} push;
layout(location = 0) in vec2 inVertexPosition;
layout(location = 1) in vec3 inPosition;
layout(location = 2) in vec2 inSize;
layout(location = 3) in vec4 inColor;
layout(location = 4) in uint code;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec2 fragUV;
void main() {
Symbol symbol = font.symbol_list.symbols[code];
Character character = font.characters.characters[0];
Character character = push.characters.characters[gl_InstanceIndex];
Symbol symbol = font.symbol_list.symbols[character.code];
float fragU = (inVertexPosition.x*symbol.width + symbol.x) / font.width;
float fragV = inVertexPosition.y + symbol.top / font.height;
float x = inVertexPosition.x * inSize.x * symbol.width / font.height;
float y = (inVertexPosition.y + float(symbol.top)/float(font.height)) * inSize.y;
float x = inVertexPosition.x * character.size * symbol.width / font.height;
float y = (inVertexPosition.y + float(symbol.top)/float(font.height)) * character.size;
fragUV = vec2(fragU, fragV);
fragColor = inColor;
gl_Position = ubo.screen * vec4(vec3(x, y, 0.0) + inPosition, 1.0);
fragColor = character.color;
gl_Position = ubo.screen * vec4(vec3(x, y, 0.0) + character.pos, 1.0);
}

@ -156,7 +156,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkBufferCreateInfo text_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
.size = 2*sizeof(Char),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
@ -183,8 +183,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
text[0].color[3] = 1.0f;
text[0].pos[0] = 200.0f;
text[0].pos[1] = 200.0f;
text[0].size[0] = 200.0f;
text[0].size[1] = 200.0f;
text[0].size = 200.0f;
text[0].code = 1;
text[1].color[0] = 1.0f;
@ -193,12 +192,16 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
text[1].color[3] = 1.0f;
text[1].pos[0] = 400.0f;
text[1].pos[1] = 200.0f;
text[1].size[0] = 200.0f;
text[1].size[1] = 200.0f;
text[1].size = 200.0f;
text[1].code = 14;
vmaUnmapMemory(render_context->allocator, text_memory);
VkBufferDeviceAddressInfo test_address_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = text_buffer,
};
UILayer test_layer = {
.colored_rects = colored_rect_buffer,
.colored_rect_count = 3,
@ -206,6 +209,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
.font = test_font_descriptor,
.text_count = 2,
.texts = text_buffer,
.texts_address = vkGetBufferDeviceAddress(render_context->device, &test_address_info),
};
while(glfwWindowShouldClose(window) == 0) {

@ -319,11 +319,6 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
.stride = sizeof(vec2),
.inputRate = VK_VERTEX_INPUT_RATE_VERTEX,
},
{
.binding = 1,
.stride = sizeof(Char),
.inputRate = VK_VERTEX_INPUT_RATE_INSTANCE,
}
};
VkVertexInputAttributeDescription attributes[] = {
{
@ -332,30 +327,6 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = 0,
},
{
.binding = 1,
.location = 1,
.format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(Char, pos),
},
{
.binding = 1,
.location = 2,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(Char, size),
},
{
.binding = 1,
.location = 3,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = offsetof(Char, color),
},
{
.binding = 1,
.location = 4,
.format = VK_FORMAT_R32_UINT,
.offset = offsetof(Char, code),
},
};
VkPipelineVertexInputStateCreateInfo input_info = {
@ -367,10 +338,18 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
};
VkDescriptorSetLayout all_layouts[] = {ui_descriptor_layout, font_layout};
VkPushConstantRange push_constant = {
.size = 8,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
};
VkPipelineLayoutCreateInfo layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = sizeof(all_layouts)/sizeof(VkDescriptorSetLayout),
.pSetLayouts = all_layouts,
.pPushConstantRanges = &push_constant,
.pushConstantRangeCount = 1,
};
VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {
@ -387,7 +366,7 @@ VkResult create_ui_text_pipeline(VkDevice device, VkRenderPass render_pass, VkDe
return VK_SUCCESS;
}
VkResult create_text_descriptor_pools(VkDevice device, uint32_t max_sets, VkDescriptorPool* font_pool, VkDescriptorSetLayout* font_layout) {
VkResult create_font_descriptor_pools(VkDevice device, uint32_t max_sets, VkDescriptorPool* font_pool, VkDescriptorSetLayout* font_layout) {
VkResult result;
VkDescriptorPoolSize font_pool_sizes[] = {
{
@ -891,7 +870,7 @@ VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swap
return result;
}
result = create_text_descriptor_pools(device, 10, &context->font_pool, &context->font_layout);
result = create_font_descriptor_pools(device, 10, &context->font_pool, &context->font_layout);
if(result != VK_SUCCESS) {
return result;
}

@ -969,13 +969,14 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
}
uint32_t image_index;
VkCommandBuffer command_buffer = context->swapchain_command_buffers[context->current_frame];
result = vkAcquireNextImageKHR(context->device, context->swapchain, UINT64_MAX, context->image_available_semaphores[context->current_frame], VK_NULL_HANDLE, &image_index);
if(result != VK_SUCCESS) {
return result;
}
result = vkResetCommandBuffer(context->swapchain_command_buffers[context->current_frame], 0);
result = vkResetCommandBuffer(command_buffer, 0);
if(result != VK_SUCCESS) {
return result;
}
@ -983,7 +984,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
VkCommandBufferBeginInfo begin_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
};
result = vkBeginCommandBuffer(context->swapchain_command_buffers[context->current_frame], &begin_info);
result = vkBeginCommandBuffer(command_buffer, &begin_info);
if(result != VK_SUCCESS) {
return result;
}
@ -994,12 +995,12 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
.maxDepth = 1.0f,
.minDepth = 0.0f,
};
vkCmdSetViewport(context->swapchain_command_buffers[context->current_frame], 0, 1, &viewport);
vkCmdSetViewport(command_buffer, 0, 1, &viewport);
VkRect2D scissor = {
.extent = context->swapchain_extent,
};
vkCmdSetScissor(context->swapchain_command_buffers[context->current_frame], 0, 1, &scissor);
vkCmdSetScissor(command_buffer, 0, 1, &scissor);
VkClearValue clear_values[2] = {{.color={{0.0f, 0.0f, 0.0f, 1.0f}}}, {.depthStencil={1.0f, 0.0f}}};
VkDeviceSize offset = 0;
@ -1014,8 +1015,8 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
.clearValueCount = 2,
.pClearValues = clear_values,
};
vkCmdBeginRenderPass(context->swapchain_command_buffers[context->current_frame], &world_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]);
vkCmdBeginRenderPass(command_buffer, &world_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(command_buffer);
// UI Render Pass
VkRenderPassBeginInfo ui_render_pass_begin = {
@ -1027,36 +1028,36 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
.clearValueCount = 2,
.pClearValues = clear_values,
};
vkCmdBeginRenderPass(context->swapchain_command_buffers[context->current_frame], &ui_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBeginRenderPass(command_buffer, &ui_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
// Draw UI colored rects ////////////////////////////////
vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.pipeline);
vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.layout, 0, 1, &ui_context->ui_descriptor_set, 0, NULL);
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_context->ui_rect.vertex, &offset);
vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], ui_context->ui_rect.index, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.layout, 0, 1, &ui_context->ui_descriptor_set, 0, NULL);
vkCmdBindVertexBuffers(command_buffer, 0, 1, &ui_context->ui_rect.vertex, &offset);
vkCmdBindIndexBuffer(command_buffer, ui_context->ui_rect.index, 0, VK_INDEX_TYPE_UINT32);
for(uint32_t i = 0; i < ui_layer_count; i++) {
if(ui_layers[i].colored_rect_count > 0) {
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_layers[i].colored_rects, &offset);
vkCmdDrawIndexed(context->swapchain_command_buffers[context->current_frame], 6, ui_layers[i].colored_rect_count, 0, 0, 0);
vkCmdBindVertexBuffers(command_buffer, 1, 1, &ui_layers[i].colored_rects, &offset);
vkCmdDrawIndexed(command_buffer, 6, ui_layers[i].colored_rect_count, 0, 0, 0);
}
}
/////////////////////////////////////////////////////////
// Draw UI text /////////////////////////////////////////
vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.pipeline);
vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 0, 1, &ui_context->ui_descriptor_set, 0, NULL);
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 0, 1, &ui_context->ui_descriptor_set, 0, NULL);
for(uint32_t i = 0; i < ui_layer_count; i++) {
if(ui_layers[i].text_count > 0) {
vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 1, 1, &ui_layers[i].font.set, 0, NULL);
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_layers[i].texts, &offset);
vkCmdDrawIndexed(context->swapchain_command_buffers[context->current_frame], 6, ui_layers[i].text_count, 0, 0, 0);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_text.layout, 1, 1, &ui_layers[i].font.set, 0, NULL);
vkCmdPushConstants(command_buffer, ui_context->ui_pipeline_text.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, 8, &ui_layers[i].texts_address);
vkCmdDrawIndexed(command_buffer, 6, ui_layers[i].text_count, 0, 0, 0);
}
}
/////////////////////////////////////////////////////////
vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]);
vkCmdEndRenderPass(command_buffer);
result = vkEndCommandBuffer(context->swapchain_command_buffers[context->current_frame]);
result = vkEndCommandBuffer(command_buffer);
if(result != VK_SUCCESS) {
return result;
}
@ -1068,7 +1069,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
.pWaitSemaphores = &context->image_available_semaphores[context->current_frame],
.pWaitDstStageMask = wait_stages,
.commandBufferCount = 1,
.pCommandBuffers = &context->swapchain_command_buffers[context->current_frame],
.pCommandBuffers = &command_buffer,
.signalSemaphoreCount = 1,
.pSignalSemaphores = &context->render_finished_semaphores[context->current_frame],
};