Updated main program to create and register vertex structure with graphics pipeline

This commit is contained in:
2025-07-10 00:48:23 +10:00
parent fb2067e7f8
commit 42e6285977

View File

@@ -1,10 +1,31 @@
// TODO: Memory leak with application array, likely get_attribude_descriptions
// Don't know how though as they are deleted immediately after being used
#include "vulkan/basalt_command_buffer.h" #include "vulkan/basalt_command_buffer.h"
#include "vulkan/basalt_swapchain.h" #include "vulkan/basalt_swapchain.h"
#include <glm/glm.hpp>
struct Vertex
{
glm::vec2 pos;
glm::vec3 colour;
static VkVertexInputBindingDescription get_binding_description(void);
static VkResult get_attribute_descriptions(VkVertexInputAttributeDescription** out_attributes, u32* out_num_attributes);
};
static basalt::darray<Vertex> verts({
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
}, MEMORY_TAG_CLASS_ARRAY | MEMORY_TAG_ZONE_APPLICATION | MEMORY_TAG_ALIGN_32);
void framebuffer_resized(GLFWwindow* window, int width, int height); void framebuffer_resized(GLFWwindow* window, int width, int height);
void application() void application()
{ {
VkResult err = VK_SUCCESS;
basalt::Context ctx("Basic", basalt::Context ctx("Basic",
basalt::darray<const char*>({ basalt::darray<const char*>({
"VK_LAYER_KHRONOS_validation" "VK_LAYER_KHRONOS_validation"
@@ -66,6 +87,9 @@ void application()
}) })
.build(); .build();
VkVertexInputAttributeDescription* vertex_attribs = nullptr;
u32 num_vertex_attribs = 0;
VK_ASSERT(Vertex::get_attribute_descriptions(&vertex_attribs, &num_vertex_attribs), "Failed to get vertex input attribute description(s)\n\tAt %s:%d\n\tError %s\n\tVertex class %s\n", typeid(Vertex).name());
basalt::Pipeline pipeline = basalt::PipelineBuilder(dev, swapchain.swapchain_extent, VK_NULL_HANDLE) basalt::Pipeline pipeline = basalt::PipelineBuilder(dev, swapchain.swapchain_extent, VK_NULL_HANDLE)
.add_dynamic_state(VK_DYNAMIC_STATE_SCISSOR) .add_dynamic_state(VK_DYNAMIC_STATE_SCISSOR)
.add_dynamic_state(VK_DYNAMIC_STATE_VIEWPORT) .add_dynamic_state(VK_DYNAMIC_STATE_VIEWPORT)
@@ -82,7 +106,9 @@ void application()
}) })
.add_shader("shaders/out/simple.vert.spv", VK_SHADER_STAGE_VERTEX_BIT) .add_shader("shaders/out/simple.vert.spv", VK_SHADER_STAGE_VERTEX_BIT)
.add_shader("shaders/out/simple.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT) .add_shader("shaders/out/simple.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT)
.add_vertex_binding(Vertex::get_binding_description(), vertex_attribs, num_vertex_attribs)
.build(); .build();
basalt::mem::dealloc(vertex_attribs, sizeof(VkVertexInputAttributeDescription) * num_vertex_attribs, MEMORY_TAG_CLASS_ARRAY | MEMORY_TAG_ZONE_APPLICATION | MEMORY_TAG_ALIGN_ANY);
swapchain.create_framebuffers(pass.render_pass); swapchain.create_framebuffers(pass.render_pass);
@@ -98,7 +124,6 @@ void application()
BINFO("%s\n", mem_str); BINFO("%s\n", mem_str);
basalt::mem::dealloc(mem_str, mem_str_len, MEMORY_TAG_CLASS_STRING | MEMORY_TAG_ZONE_DEBUG | MEMORY_TAG_ALIGN_ANY); basalt::mem::dealloc(mem_str, mem_str_len, MEMORY_TAG_CLASS_STRING | MEMORY_TAG_ZONE_DEBUG | MEMORY_TAG_ALIGN_ANY);
VkResult err = VK_SUCCESS;
VkRect2D render_area = { .offset = {0,0}, .extent = swapchain.swapchain_extent }; VkRect2D render_area = { .offset = {0,0}, .extent = swapchain.swapchain_extent };
VkViewport viewport = { VkViewport viewport = {
.x = (float)render_area.offset.x, .x = (float)render_area.offset.x,
@@ -191,4 +216,31 @@ void framebuffer_resized(GLFWwindow* window, int width, int height)
if (swapchain == nullptr) if (swapchain == nullptr)
return; return;
swapchain->framebuffers_created = false; swapchain->framebuffers_created = false;
} }
VkVertexInputBindingDescription Vertex::get_binding_description(void)
{
VkVertexInputBindingDescription ret{};
ret.binding = 0;
ret.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
ret.stride = sizeof(Vertex);
return ret;
}
VkResult Vertex::get_attribute_descriptions(VkVertexInputAttributeDescription** out_attributes, u32* out_num_attributes)
{
*out_attributes = basalt::mem::allocT<VkVertexInputAttributeDescription>(2, MEMORY_TAG_CLASS_ARRAY | MEMORY_TAG_ZONE_APPLICATION | MEMORY_TAG_ALIGN_ANY);
if (out_attributes == nullptr)
return VK_ERROR_OUT_OF_HOST_MEMORY;
*out_num_attributes = 2;
VkVertexInputAttributeDescription* ptr = *out_attributes;
ptr[0].binding = 0;
ptr[0].location = 0;
ptr[0].format = VK_FORMAT_R32G32_SFLOAT;
ptr[0].offset = offsetof(Vertex, pos);
ptr[1].binding = 0;
ptr[1].location = 1;
ptr[1].format = VK_FORMAT_R32G32B32_SFLOAT;
ptr[1].offset = offsetof(Vertex, colour);
return VK_SUCCESS;
}