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

@ -5,9 +5,19 @@ layout(set = 0, binding = 0) uniform UIUniform {
mat4 screen;
} ubo;
layout(location = 0) in vec3 inPolygonPosition;
layout(location = 1) in vec2 inPolygonSize;
layout(location = 2) in vec4 inColor;
struct Rect {
vec4 pos;
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;
@ -21,7 +31,8 @@ const vec2 square[6] = {
};
void main() {
gl_Position = ubo.screen * vec4(vec3(square[gl_VertexIndex] * inPolygonSize, 0.0) + inPolygonPosition, 1.0);
fragColor = inColor;
Rect rect = pc.rects.r[gl_InstanceIndex];
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 {
vec3 pos;
vec4 pos;
vec4 color;
float size;
uint code;
};
struct String {
vec3 pos;
vec4 pos;
vec4 color;
float size;
uint offset;
@ -75,7 +75,7 @@ void main() {
float x = 0;
for(uint i = 0; i < string.len; 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;
push.pointers.characters.characters[buffer_pos + i].size = string.size;
push.pointers.characters.characters[buffer_pos + i].color = string.color;

@ -10,7 +10,7 @@ struct Symbol {
};
struct Character {
vec3 pos;
vec4 pos;
vec4 color;
float size;
uint code;
@ -68,7 +68,7 @@ void main() {
fragUV = vec2(fragU, fragV);
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;
}

@ -8,15 +8,6 @@
#include "ft2build.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 result;
UIContext ui_context;
@ -42,7 +33,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkBufferCreateInfo colored_rect_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 = 3*sizeof(ColoredRect),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
@ -63,9 +54,36 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
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[1] = colored_rect(100.0, 100.0, 0.0, 1.0, 0.0, 1.0, 0.0, 100.0, 0.5);
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[0] = 0.0;
colored_rects[0].pos[1] = 0.0;
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);
@ -132,8 +150,14 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
vmaUnmapMemory(render_context->allocator, 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 = {
.colored_rects = colored_rect_buffer,
.colored_rects = vkGetBufferDeviceAddress(render_context->device, &colored_rect_address_info),
.colored_rect_count = 3,
.font = test_font,

@ -1024,7 +1024,6 @@ VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_l
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;
VkRenderPassBeginInfo render_pass_begin = {
.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++) {
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);
}
}

@ -200,36 +200,9 @@ VkResult create_ui_colored_rect_pipeline(VkDevice device, VkRenderPass render_pa
},
};
VkVertexInputBindingDescription bindings[] = {
{
.binding = 0,
.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 = {
VkVertexInputBindingDescription bindings[] = {};
VkVertexInputAttributeDescription attributes[] = {};
VkPipelineVertexInputStateCreateInfo input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pVertexBindingDescriptions = bindings,
.vertexBindingDescriptionCount = sizeof(bindings)/sizeof(VkVertexInputBindingDescription),
@ -237,10 +210,17 @@ VkPipelineVertexInputStateCreateInfo input_info = {
.vertexAttributeDescriptionCount = sizeof(attributes)/sizeof(VkVertexInputAttributeDescription),
};
VkPushConstantRange push_constant = {
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.size = 8,
};
VkPipelineLayoutCreateInfo layout_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &ui_descriptor_layout,
.pPushConstantRanges = &push_constant,
.pushConstantRangeCount = 1,
};
VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {