From a5df364c0066efd19fa848beca4df4b016ee32b7 Mon Sep 17 00:00:00 2001 From: InkSoul Date: Sun, 3 Mar 2024 23:41:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9B=BE=E5=BD=A2=E7=AE=A1?= =?UTF-8?q?=E7=BA=BF=E5=9B=BA=E5=AE=9A=E5=8A=9F=E8=83=BD=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VulkanTutorial.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++-- VulkanTutorial.h | 2 + 2 files changed, 130 insertions(+), 4 deletions(-) diff --git a/VulkanTutorial.cpp b/VulkanTutorial.cpp index 846a615..55b7fa8 100644 --- a/VulkanTutorial.cpp +++ b/VulkanTutorial.cpp @@ -98,7 +98,7 @@ void HelloTriangleApplication::initVulkan() { createInstance(); - showAvailableExtension(); + //showAvailableExtension(); setupDebugMessenger(); @@ -130,6 +130,10 @@ void HelloTriangleApplication::mainLoop(GLFWwindow* window){ void HelloTriangleApplication::cleanup(GLFWwindow* window) { + std::cout << "start to destroy resource" << std::endl; + + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + for (auto imageView : swapChainImageViews) { vkDestroyImageView(device, imageView, nullptr); @@ -175,7 +179,6 @@ std::vector HelloTriangleApplication::getRequiredExtensions() { void HelloTriangleApplication::showAvailableExtension() { // show available extensions - uint32_t availableExtensionCount = 0; vkEnumerateInstanceExtensionProperties(nullptr, &availableExtensionCount, nullptr); std::vector availableExtensions(availableExtensionCount); @@ -605,7 +608,8 @@ void HelloTriangleApplication::createGraphicPipeline() auto vertShaderCode = readFile("../../../shaders/triangleVert.spv"); auto fragShaderCode = readFile("../../..//shaders/triangleFrag.spv"); - std::cout << "vertShader size:" << vertShaderCode.size() << "\nfragShader size:" << fragShaderCode.size() << std::endl; + + //std::cout << "vertShader size:" << vertShaderCode.size() << "\nfragShader size:" << fragShaderCode.size() << std::endl; VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); @@ -626,8 +630,128 @@ void HelloTriangleApplication::createGraphicPipeline() VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo,fragShaderStageInfo }; + //顶点输入 + 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; - //管道创建完成后销毁着色器模块 + + //使用动态状态并保留视口和剪刀裁剪状态 + std::vector dynamicStates = { + VK_DYNAMIC_STATE_VIEWPORT, + VK_DYNAMIC_STATE_SCISSOR + }; + + VkPipelineDynamicStateCreateInfo dynamicState{}; + dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; + dynamicState.dynamicStateCount = static_cast(dynamicStates.size()); + dynamicState.pDynamicStates = dynamicStates.data(); + + //描述从顶点绘制哪种几何图形及是否启用primitive restart + VkPipelineInputAssemblyStateCreateInfo inputAsseblyState{}; + inputAsseblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + inputAsseblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + inputAsseblyState.primitiveRestartEnable = VK_FALSE; + + //视口描述将被渲染输出的帧缓冲区域 + VkViewport viewport{}; + viewport.x = 0.0f; + viewport.y = 0.0f; + viewport.width = (float)swapChainExtent.width; + viewport.height = (float)swapChainExtent.height; + viewport.minDepth = 0.0f;//指定深度值范围,通常为此范围 + viewport.maxDepth = 1.0f; + + // 剪刀矩阵,定义像素实际存储的区域 + VkRect2D scissor{}; + scissor.offset = { 0,0 }; + scissor.extent = swapChainExtent; + // 创建viewport + VkPipelineViewportStateCreateInfo viewportState{}; + viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; + viewportState.viewportCount = 1; + viewportState.scissorCount = 1; + + // 光栅化 + VkPipelineRasterizationStateCreateInfo rasterizer{}; + rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + rasterizer.depthClampEnable = VK_FALSE;// 不丢弃超出近平面和远平面的fragment + rasterizer.rasterizerDiscardEnable = VK_FALSE;// 启用则几何图形不会通过光栅化,禁用framebuffer的所有输出 + rasterizer.polygonMode = VK_POLYGON_MODE_FILL;// fragment填充多边形区域 + rasterizer.lineWidth = 1.0f; + + rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;// 背面剔除 + rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE; + + // 根据fragment的斜率偏置深度值 + rasterizer.depthBiasEnable = VK_FALSE; + rasterizer.depthBiasConstantFactor = 0.0f; + rasterizer.depthBiasClamp = 0.0f; + rasterizer.depthBiasSlopeFactor = 0.0f; + + // 多重采样(禁用) + VkPipelineMultisampleStateCreateInfo multisampling{}; + multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + multisampling.sampleShadingEnable = VK_FALSE; + multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + multisampling.minSampleShading = 1.0f; + multisampling.pSampleMask = nullptr; + multisampling.alphaToCoverageEnable = VK_FALSE; + multisampling.alphaToOneEnable = VK_FALSE; + + // 深度或模版缓冲区(暂留) + + // 颜色混合 color blending + //每个附加帧缓冲区的配置 + VkPipelineColorBlendAttachmentState colorBlendAttachment{}; + colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; + colorBlendAttachment.blendEnable = VK_FALSE; + colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; + colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; + colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; + colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; + colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; + + /* + alpha混合 + colorBlendAttachment.blendEnable = VK_TRUE; + colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; + colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD; + colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; + colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; + colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD; + */ + + //全局颜色混合配置,按位运算 + VkPipelineColorBlendStateCreateInfo colorBlendState{}; + colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + colorBlendState.logicOpEnable = VK_FALSE; + colorBlendState.logicOp = VK_LOGIC_OP_COPY; + colorBlendState.attachmentCount = 1; + colorBlendState.pAttachments = &colorBlendAttachment; + colorBlendState.blendConstants[0] = 0.0f; + colorBlendState.blendConstants[1] = 0.0f; + colorBlendState.blendConstants[2] = 0.0f; + colorBlendState.blendConstants[3] = 0.0f; + + VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; + pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pipelineLayoutInfo.setLayoutCount = 0; + pipelineLayoutInfo.pSetLayouts = nullptr; + pipelineLayoutInfo.pushConstantRangeCount = 0; + pipelineLayoutInfo.pPushConstantRanges = nullptr; + + if (vkCreatePipelineLayout(device,&pipelineLayoutInfo,nullptr,&pipelineLayout) != VK_SUCCESS) + { + throw std::runtime_error("failed to create pipeline Layout in createGraphicPipeline"); + } + + // 管道创建完成后销毁着色器模块 vkDestroyShaderModule(device, fragShaderModule, nullptr); vkDestroyShaderModule(device, vertShaderModule, nullptr); } diff --git a/VulkanTutorial.h b/VulkanTutorial.h index 71e205b..1189014 100644 --- a/VulkanTutorial.h +++ b/VulkanTutorial.h @@ -63,6 +63,8 @@ private: std::vector swapChainImageViews; + VkPipelineLayout pipelineLayout; + struct QueueFamilyIndices { std::optional graphicsFamily;