From 85fd25f59ae1db6224174de595c44e95482ee502 Mon Sep 17 00:00:00 2001 From: Warwick Date: Wed, 29 Jan 2025 18:24:16 +0000 Subject: [PATCH] Created a vertex input description --- shaders/shader.vert.glsl | 20 +++++---------- src/vulkan_wrapper.c | 55 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/shaders/shader.vert.glsl b/shaders/shader.vert.glsl index f5b2f8d..15ebac9 100644 --- a/shaders/shader.vert.glsl +++ b/shaders/shader.vert.glsl @@ -1,20 +1,12 @@ #version 450 +layout(location = 0) in vec2 inPosition; +layout(location = 1) in vec3 inColor; + layout(location = 0) out vec3 fragColor; -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[]( - vec3(1.0, 0.0, 0.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) -); - void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - fragColor = colors[gl_VertexIndex]; + gl_Position = vec4(inPosition, 0.0, 1.0); + fragColor = inColor; } + diff --git a/src/vulkan_wrapper.c b/src/vulkan_wrapper.c index 146f584..bdf0bb5 100644 --- a/src/vulkan_wrapper.c +++ b/src/vulkan_wrapper.c @@ -14,6 +14,9 @@ #define GLFW_INCLUDE_VULKAN #include +#include +#include + #ifdef NDEBUG const bool enableValidationLayers = false; #else @@ -648,6 +651,43 @@ VkShaderModule createShaderModule(VkDevice device, ShaderBuffer code) { return shaderModule; } +typedef struct { + vec2 pos; + vec3 colour; +} Vertex; + +const Vertex vertices[] = {{{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}}}; + +static VkVertexInputBindingDescription getBindingDescription() { + VkVertexInputBindingDescription bindingDescription = {0}; + + bindingDescription.binding = 0; + bindingDescription.stride = sizeof(Vertex); + bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + + return bindingDescription; +} + +// Currently this is for an array of 2 +static VkVertexInputAttributeDescription *populateAttributeDescriptions( + VkVertexInputAttributeDescription *attributeDescriptions) { + // location 0 + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[0].offset = offsetof(Vertex, pos); + + // location 1 + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, colour); + + return attributeDescriptions; +} + void createGraphicsPipeline(VulkanApp *app) { ShaderBuffer vertShaderCode = loadShaderIntoBuffer("shaders/shader.vert.spv"); ShaderBuffer fragShaderCode = loadShaderIntoBuffer("shaders/shader.frag.spv"); @@ -690,10 +730,17 @@ void createGraphicsPipeline(VulkanApp *app) { VkPipelineVertexInputStateCreateInfo vertexInputInfo = {0}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vertexInputInfo.vertexBindingDescriptionCount = 0; - vertexInputInfo.pVertexBindingDescriptions = NULL; // Optional - vertexInputInfo.vertexAttributeDescriptionCount = 0; - vertexInputInfo.pVertexAttributeDescriptions = NULL; // Optional + + VkVertexInputBindingDescription bindingDescription = getBindingDescription(); + #define attributeDescriptionsCount 2 + VkVertexInputAttributeDescription attributeDescriptions[attributeDescriptionsCount] = {0}; + populateAttributeDescriptions( + (VkVertexInputAttributeDescription *)&attributeDescriptions); + + vertexInputInfo.vertexBindingDescriptionCount = 1; + vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; + vertexInputInfo.vertexAttributeDescriptionCount = attributeDescriptionsCount; + vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions; VkPipelineInputAssemblyStateCreateInfo inputAssembly = {0}; inputAssembly.sType =