Fixed shader buffer alignments

main
noah metz 2024-10-17 17:31:13 -06:00
parent 04e15db065
commit b67a2a924c
7 changed files with 75 additions and 65 deletions

@ -20,7 +20,7 @@ typedef struct GraphicsPipelineStruct {
} GraphicsPipeline; } GraphicsPipeline;
typedef struct ColoredRectStruct { typedef struct ColoredRectStruct {
vec3 pos; vec4 pos;
vec2 size; vec2 size;
vec4 color; vec4 color;
} ColoredRect; } ColoredRect;
@ -30,7 +30,7 @@ typedef struct UIUniformStruct {
} UIUniform; } UIUniform;
typedef struct StringStruct { typedef struct StringStruct {
vec3 pos; vec4 pos;
vec4 color; vec4 color;
float size; float size;
uint32_t offset; uint32_t offset;
@ -52,7 +52,7 @@ typedef struct StringPointersStruct {
} StringPointers; } StringPointers;
typedef struct CharacterStruct { typedef struct CharacterStruct {
vec3 pos; vec4 pos;
vec4 color; vec4 color;
float size; float size;
uint32_t code; uint32_t code;
@ -100,12 +100,8 @@ typedef struct TextPointersMemoryStruct {
} TextPointersMemory; } TextPointersMemory;
typedef struct UILayerStruct { typedef struct UILayerStruct {
VkBuffer colored_rects; VkDeviceAddress colored_rects;
uint32_t colored_rect_count; uint32_t colored_rect_count;
VkBuffer textured_rects;
uint32_t textured_rect_count;
VkDescriptorSet textured_rect_descriptor;
VkBuffer string_draw; VkBuffer string_draw;
VkBuffer chars; VkBuffer chars;

@ -5,9 +5,19 @@ layout(set = 0, binding = 0) uniform UIUniform {
mat4 screen; mat4 screen;
} ubo; } ubo;
layout(location = 0) in vec3 inPolygonPosition; struct Rect {
layout(location = 1) in vec2 inPolygonSize; vec4 pos;
layout(location = 2) in vec4 inColor; vec2 size;
vec4 color;
};
layout(std430, buffer_reference) readonly buffer RectList {
Rect r[];
};
layout(std430, push_constant) uniform PushConstant {
RectList rects;
} pc;
layout(location = 0) out vec4 fragColor; layout(location = 0) out vec4 fragColor;
@ -21,7 +31,8 @@ const vec2 square[6] = {
}; };
void main() { void main() {
gl_Position = ubo.screen * vec4(vec3(square[gl_VertexIndex] * inPolygonSize, 0.0) + inPolygonPosition, 1.0); Rect rect = pc.rects.r[gl_InstanceIndex];
fragColor = inColor; gl_Position = ubo.screen * vec4(vec3(square[gl_VertexIndex] * rect.size, 0.0) + rect.pos.xyz, 1.0);
fragColor = rect.color;
} }

@ -10,14 +10,14 @@ struct Symbol {
}; };
struct Character { struct Character {
vec3 pos; vec4 pos;
vec4 color; vec4 color;
float size; float size;
uint code; uint code;
}; };
struct String { struct String {
vec3 pos; vec4 pos;
vec4 color; vec4 color;
float size; float size;
uint offset; uint offset;
@ -75,7 +75,7 @@ void main() {
float x = 0; float x = 0;
for(uint i = 0; i < string.len; i++) { for(uint i = 0; i < string.len; i++) {
Symbol symbol = font.symbol_list.symbols[push.pointers.codes.codes[string.offset + 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); push.pointers.characters.characters[buffer_pos + i].pos = vec4(string.pos.xyz + vec3(x, 0, 0), 0);
x += string.size*symbol.advance/font.width; x += string.size*symbol.advance/font.width;
push.pointers.characters.characters[buffer_pos + i].size = string.size; 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].color = string.color;

@ -10,7 +10,7 @@ struct Symbol {
}; };
struct Character { struct Character {
vec3 pos; vec4 pos;
vec4 color; vec4 color;
float size; float size;
uint code; uint code;
@ -68,7 +68,7 @@ void main() {
fragUV = vec2(fragU, fragV); fragUV = vec2(fragU, fragV);
fragColor = character.color; fragColor = character.color;
gl_Position = ubo.screen * vec4(vec3(x, y, 0.0) + character.pos, 1.0); gl_Position = ubo.screen * vec4(vec3(x, y, 0.0) + character.pos.xyz, 1.0);
code = character.code; code = character.code;
} }

@ -8,15 +8,6 @@
#include "ft2build.h" #include "ft2build.h"
#include FT_FREETYPE_H #include FT_FREETYPE_H
ColoredRect colored_rect(float width, float height, float r, float g, float b, float a, float x, float y, float z) {
ColoredRect rect = {
.size = {width, height},
.pos = {x, y, z},
.color = {r, g, b, a},
};
return rect;
}
VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkResult result; VkResult result;
UIContext ui_context; UIContext ui_context;
@ -42,7 +33,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkBufferCreateInfo colored_rect_buffer_info = { VkBufferCreateInfo colored_rect_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_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 = 3*sizeof(ColoredRect), .size = 3*sizeof(ColoredRect),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE, .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
}; };
@ -63,9 +54,36 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
return result; return result;
} }
colored_rects[0] = colored_rect(100.0, 100.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5); colored_rects[0].pos[0] = 0.0;
colored_rects[1] = colored_rect(100.0, 100.0, 0.0, 1.0, 0.0, 1.0, 0.0, 100.0, 0.5); colored_rects[0].pos[1] = 0.0;
colored_rects[2] = colored_rect(100.0, 100.0, 0.0, 0.0, 1.0, 1.0, 100.0, 0.0, 0.5); colored_rects[0].pos[2] = 0.5;
colored_rects[0].size[0] = 100.0;
colored_rects[0].size[1] = 100.0;
colored_rects[0].color[0] = 1.0;
colored_rects[0].color[1] = 0.0;
colored_rects[0].color[2] = 0.0;
colored_rects[0].color[3] = 1.0;
colored_rects[1].pos[0] = 0.0;
colored_rects[1].pos[1] = 100.0;
colored_rects[1].pos[2] = 0.5;
colored_rects[1].size[0] = 100.0;
colored_rects[1].size[1] = 100.0;
colored_rects[1].color[0] = 0.0;
colored_rects[1].color[1] = 0.0;
colored_rects[1].color[2] = 1.0;
colored_rects[1].color[3] = 1.0;
colored_rects[2].pos[0] = 100.0;
colored_rects[2].pos[1] = 0.0;
colored_rects[2].pos[2] = 0.5;
colored_rects[2].size[0] = 100.0;
colored_rects[2].size[1] = 100.0;
colored_rects[2].color[0] = 0.0;
colored_rects[2].color[1] = 1.0;
colored_rects[2].color[2] = 0.0;
colored_rects[2].color[3] = 1.0;
vmaUnmapMemory(render_context->allocator, colored_rect_memory); vmaUnmapMemory(render_context->allocator, colored_rect_memory);
@ -132,8 +150,14 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
vmaUnmapMemory(render_context->allocator, temp_memory); vmaUnmapMemory(render_context->allocator, temp_memory);
vmaDestroyBuffer(render_context->allocator, temp_buffer, temp_memory); vmaDestroyBuffer(render_context->allocator, temp_buffer, temp_memory);
VkBufferDeviceAddressInfo colored_rect_address_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO,
.buffer = colored_rect_buffer,
};
UILayer test_layer = { UILayer test_layer = {
.colored_rects = colored_rect_buffer, .colored_rects = vkGetBufferDeviceAddress(render_context->device, &colored_rect_address_info),
.colored_rect_count = 3, .colored_rect_count = 3,
.font = test_font, .font = test_font,

@ -1024,7 +1024,6 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
vkCmdSetScissor(command_buffer, 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}}}; VkClearValue clear_values[2] = {{.color={{0.0f, 0.0f, 0.0f, 1.0f}}}, {.depthStencil={1.0f, 0.0f}}};
VkDeviceSize offset = 0;
VkRenderPassBeginInfo render_pass_begin = { VkRenderPassBeginInfo render_pass_begin = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
@ -1094,7 +1093,7 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
for(uint32_t i = 0; i < ui_layer_count; i++) { for(uint32_t i = 0; i < ui_layer_count; i++) {
if(ui_layers[i].colored_rect_count > 0) { if(ui_layers[i].colored_rect_count > 0) {
vkCmdBindVertexBuffers(command_buffer, 0, 1, &ui_layers[i].colored_rects, &offset); vkCmdPushConstants(command_buffer, ui_context->ui_pipeline_rect.layout, VK_SHADER_STAGE_VERTEX_BIT, 0, 8, &ui_layers[i].colored_rects);
vkCmdDraw(command_buffer, 6, ui_layers[i].colored_rect_count, 0, 0); vkCmdDraw(command_buffer, 6, ui_layers[i].colored_rect_count, 0, 0);
} }
} }

@ -200,36 +200,9 @@ VkResult create_ui_colored_rect_pipeline(VkDevice device, VkRenderPass render_pa
}, },
}; };
VkVertexInputBindingDescription bindings[] = { VkVertexInputBindingDescription bindings[] = {};
{ VkVertexInputAttributeDescription attributes[] = {};
.binding = 0, VkPipelineVertexInputStateCreateInfo input_info = {
.stride = sizeof(ColoredRect),
.inputRate = VK_VERTEX_INPUT_RATE_INSTANCE,
},
};
VkVertexInputAttributeDescription attributes[] = {
{
.binding = 0,
.location = 0,
.format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(ColoredRect, pos),
},
{
.binding = 0,
.location = 1,
.format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(ColoredRect, size),
},
{
.binding = 0,
.location = 2,
.format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = offsetof(ColoredRect, color),
},
};
VkPipelineVertexInputStateCreateInfo input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pVertexBindingDescriptions = bindings, .pVertexBindingDescriptions = bindings,
.vertexBindingDescriptionCount = sizeof(bindings)/sizeof(VkVertexInputBindingDescription), .vertexBindingDescriptionCount = sizeof(bindings)/sizeof(VkVertexInputBindingDescription),
@ -237,10 +210,17 @@ VkPipelineVertexInputStateCreateInfo input_info = {
.vertexAttributeDescriptionCount = sizeof(attributes)/sizeof(VkVertexInputAttributeDescription), .vertexAttributeDescriptionCount = sizeof(attributes)/sizeof(VkVertexInputAttributeDescription),
}; };
VkPushConstantRange push_constant = {
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.size = 8,
};
VkPipelineLayoutCreateInfo layout_info = { VkPipelineLayoutCreateInfo layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1, .setLayoutCount = 1,
.pSetLayouts = &ui_descriptor_layout, .pSetLayouts = &ui_descriptor_layout,
.pPushConstantRanges = &push_constant,
.pushConstantRangeCount = 1,
}; };
VkPipelineInputAssemblyStateCreateInfo input_assembly_info = { VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {