From 7e7f530f3fe6e654083d3af4a095534e82354a7a Mon Sep 17 00:00:00 2001 From: InkSoul Date: Sun, 10 Mar 2024 23:25:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=A1=B6=E7=82=B9=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E8=BE=93=E5=85=A5=E6=8F=8F=E8=BF=B0=E5=88=9B?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VulkanTutorial.cpp | 35 +++++++++++++++++++++++++++++++---- VulkanTutorial.h | 16 ++++++++++++++++ shaders/triangle.vert | 17 +++++------------ shaders/triangleVert.spv | Bin 1504 -> 1080 bytes 4 files changed, 52 insertions(+), 16 deletions(-) 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 a41dd2cd675e3469d59dc6e98d84c97a7d2d2693..1e4ce0c041e7ee15f8cf030b9c3e352942361e75 100644 GIT binary patch literal 1080 zcmYk4Uuzmc6vZd%Mx)Wx7-O5JsUeSr(1)fJN*h8_whw}qKwlTpZ5Y_BvTjP-r+l`2 zs=gF@ezQB`$o1ZP?!Pm4wpux>8?$9vrfp75ak{1=#0cAVIvm}O&R6;T{N2S{GCHQ} z63N*!JI=1@|9)yyoVH|N@=EerQq`{_{iK4LX_{~ph2wEJ`V!5S%V?GVPU0xBX&lXM z7SD3~GbXRGk!RCc@-W{_2$ZooGZ5F7X51ywB+kCaSswqAE409|i7i$(w~JJ0-Q{x! zhubv2wSP33nj!{g#jX>(ys@i%n$F^Nj13TbNRo9d&^@_3C(w@_*mOP8VeI5bHod!E zB#X@D)!dG3S|DGQ#XUG)d#rd9UdtGd!I3=pcnlki{e+5OlNSns(}{$G51&n+_rhmJ zPgT^Nss}x>4`ppj4*H}gv-Rwj>{|hG6#Q8!hzQ_Y39J0eqCuefYT43U4T(f?tz5k{l4=aFR6bao7r%s z|4V6Vz=$80HaPw9*{zTJo%%RFyJr4-y*WJDA%~j3LIUYeLgCz#!1tR_<{p-q)EXO< u7(FLq3fJcna*1(2?sTOd+~-KbPTAXMO~oglneZG+;7E+S;Qtfj50d|?qgO8g literal 1504 zcmYk5ZBNud6oqGZ>4G32BH#;R0mT<^MFnDF)EG3?WD_tXer-10N+zk@OW-Q!$6U%CCwBbK=EWe5;z^DZB6pD_lbols z;A~j*!x>3aHS;kY(h;+8ZeOy&`7ymK{vghmXU$b^!qFcb-s$KIjvgFG49B?z%J}l= z0x|SK?oo}|e?!JvmI~dbYWN26s-mvEFI!W+9#DzNtqAiXd>L!E#+MXRzb-s0PYs+e z%-(zt{$-81PdMuF^D>sh3l;pmr?cT(i!wCuPIE=E)*Gi;5`e=TcU3Vp71u3$SQh4c z=*ctVHF^4QGps0vb2F^gd3a_3^ZuGMTo*Rxm-^|JwKs&|=o@qV%^JI$<8L*zhH;ugQp(6Q*-1WD5ekY5ZsgJ+w=n7uQ6v$J(%~2hWvAR>d7DJE${}JnT6)B z7*GDS5O0dRCFX88w^vfr+kF{*y_K4t9?H(LSvj4QYY=-~<