diff --git a/VulkanTutorial.cpp b/VulkanTutorial.cpp index 78a3c61..f4b102a 100644 --- a/VulkanTutorial.cpp +++ b/VulkanTutorial.cpp @@ -954,12 +954,16 @@ void HelloTriangleApplication::createGraphicPipeline() VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo,fragShaderStageInfo }; //顶点输入 + + auto bindingDescription = Vertex::getBindingDescription(); + auto attributeDescriptions = Vertex::getAttributeDescriptions(); + VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vertexInputInfo.vertexBindingDescriptionCount = 0; - vertexInputInfo.pVertexBindingDescriptions = nullptr; - vertexInputInfo.vertexAttributeDescriptionCount = 0; - vertexInputInfo.pVertexAttributeDescriptions = nullptr; + vertexInputInfo.vertexBindingDescriptionCount = 1; + vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; + vertexInputInfo.vertexAttributeDescriptionCount = static_cast(attributeDescriptions.size()); + vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); //使用动态状态并保留视口和剪刀裁剪状态 @@ -1177,4 +1181,27 @@ int main() return EXIT_SUCCESS; } +VkVertexInputBindingDescription HelloTriangleApplication::Vertex::getBindingDescription() +{ + VkVertexInputBindingDescription bindingDescription{}; + bindingDescription.binding = 0; + bindingDescription.stride = sizeof(Vertex); + bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + return bindingDescription; +} + +std::array HelloTriangleApplication::Vertex::getAttributeDescriptions() +{ + std::array attributeDescriptions{}; + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + attributeDescriptions[0].offset = offsetof(Vertex, pos); + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, color); + + return attributeDescriptions; +} diff --git a/VulkanTutorial.h b/VulkanTutorial.h index cebb729..36718b6 100644 --- a/VulkanTutorial.h +++ b/VulkanTutorial.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include #include #include +#include #include @@ -101,6 +103,20 @@ private: bool framebufferResized = false; + struct Vertex + { + glm::vec2 pos; + glm::vec3 color; + static VkVertexInputBindingDescription getBindingDescription(); + static std::array getAttributeDescriptions(); + }; + + const std::vector 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}} + }; + void initWindow(int Width, int Height); void createInstance(); diff --git a/shaders/triangle.vert b/shaders/triangle.vert index e352619..6b72343 100644 --- a/shaders/triangle.vert +++ b/shaders/triangle.vert @@ -1,20 +1,13 @@ #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.0f, -0.5f), - vec2(0.5f, 0.5f), - vec2(-0.5f, 0.5f) -); -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; } \ No newline at end of file diff --git a/shaders/triangleVert.spv b/shaders/triangleVert.spv index a41dd2c..1e4ce0c 100644 Binary files a/shaders/triangleVert.spv and b/shaders/triangleVert.spv differ