1 Star 0 Fork 0

愤怒的小白/test-vulkan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
xb_pipeline.h 10.42 KB
一键复制 编辑 原始数据 按行查看 历史
likuai2010 提交于 1年前 . init
#pragma once
#include "xb_device.h"
#include "xb_model.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
namespace xb {
static std::vector<char> readFile(const std::string& filename) {
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("failed to open file!");
}
size_t fileSize = (size_t) file.tellg();
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.close();
return buffer;
}
struct PipelineConfigInfo
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCreateInfo;
VkPipelineViewportStateCreateInfo viewportState;
VkPipelineRasterizationStateCreateInfo rasterizer;
VkPipelineMultisampleStateCreateInfo multisampling;
VkPipelineColorBlendAttachmentState colorBlendAttachment;
VkPipelineColorBlendStateCreateInfo colorBlending;
VkPipelineDynamicStateCreateInfo dynamicState;
VkPipelineDepthStencilStateCreateInfo depthStencilInfo;
VkPipelineLayout pipelineLayout = nullptr;
VkRenderPass renderPass = nullptr;
int subpass = 0;
VkViewport viewport;
VkRect2D scissor;
};
class XbPipeline
{
private:
/* data */
XbDevice xbDevice;
std::string vertPath;
std::string fragPath;
PipelineConfigInfo configInfo;
VkPipeline graphicsPipeline;
VkRenderPass renderPass;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
public:
XbPipeline(XbDevice& device);
~XbPipeline();
void bind(VkCommandBuffer commandBuffer){
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
}
void createGraphicsPipeline(
const PipelineConfigInfo configInfo
);
VkShaderModule createShaderModule(const std::vector<char>& code);
static PipelineConfigInfo defaultPipelineConfigInfo(uint32_t width, uint32_t height);
};
XbPipeline::XbPipeline(
XbDevice& device): xbDevice { device }
{
vertPath = "../shader/vert.spv";
fragPath = "../shader/frag.spv";
}
XbPipeline::~XbPipeline()
{
}
void XbPipeline::createGraphicsPipeline(
PipelineConfigInfo configInfo
)
{
this->configInfo = configInfo;
auto vertShaderCode = readFile(vertPath);
auto fragShaderCode = readFile(fragPath);
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo shaderStages[] = {vertShaderStageInfo, fragShaderStageInfo};
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
auto attributeDiscription = XbModel::Vertex::getAttributeDiscription();
auto bindingDescription = XbModel::Vertex::getBindDiscrioption();
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = static_cast<uint32_t>(bindingDescription.size());;
vertexInputInfo.pVertexBindingDescriptions = bindingDescription.data(); // Optional
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDiscription.size());
vertexInputInfo.pVertexAttributeDescriptions = attributeDiscription.data(); // Optional
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &configInfo.viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &configInfo.scissor;
std::vector<VkDynamicState> 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<uint32_t>(dynamicStates.size());
dynamicState.pDynamicStates = dynamicStates.data();
VkGraphicsPipelineCreateInfo pipelineInfo{};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &configInfo.inputAssemblyStateCreateInfo;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &configInfo.rasterizer;
pipelineInfo.pMultisampleState = &configInfo.multisampling;
pipelineInfo.pColorBlendState = &configInfo.colorBlending;
pipelineInfo.pDepthStencilState = &configInfo.depthStencilInfo;
pipelineInfo.pDynamicState = &dynamicState;
pipelineInfo.layout = configInfo.pipelineLayout;
pipelineInfo.renderPass = configInfo.renderPass;
pipelineInfo.subpass = configInfo.subpass;
pipelineInfo.basePipelineIndex = -1;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
if (vkCreateGraphicsPipelines(xbDevice.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
throw std::runtime_error("failed to create graphics pipeline!");
}
vkDestroyShaderModule(xbDevice.device, fragShaderModule, nullptr);
vkDestroyShaderModule(xbDevice.device, vertShaderModule, nullptr);
}
VkShaderModule XbPipeline::createShaderModule(const std::vector<char>& code) {
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(xbDevice.device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
throw std::runtime_error("failed to create shader module!");
}
return shaderModule;
}
PipelineConfigInfo XbPipeline::defaultPipelineConfigInfo(uint32_t width, uint32_t height)
{
PipelineConfigInfo configInfo{};
configInfo.inputAssemblyStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
configInfo.inputAssemblyStateCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
configInfo.inputAssemblyStateCreateInfo.primitiveRestartEnable = VK_FALSE;
configInfo.viewport.x = 0.0f;
configInfo.viewport.y = 0.0f;
configInfo.viewport.width = (float) width;
configInfo.viewport.height = (float) height;
configInfo.viewport.minDepth = 0.0f;
configInfo.viewport.maxDepth = 1.0f;
configInfo.scissor.offset = {0, 0};
configInfo.scissor.extent = {width, height};
configInfo.viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
configInfo.viewportState.viewportCount = 1;
configInfo.viewportState.pViewports = &configInfo.viewport;
configInfo.viewportState.scissorCount = 1;
configInfo.viewportState.pScissors = &configInfo.scissor;
configInfo.rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
configInfo.rasterizer.depthClampEnable = VK_FALSE;
configInfo.rasterizer.rasterizerDiscardEnable = VK_FALSE;
configInfo.rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
configInfo.rasterizer.lineWidth = 1.0f;
configInfo.rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
configInfo.rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
configInfo.rasterizer.depthBiasEnable = VK_FALSE;
configInfo.multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
configInfo.multisampling.sampleShadingEnable = VK_FALSE;
configInfo.multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
configInfo.colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
configInfo.colorBlendAttachment.blendEnable = VK_FALSE;
configInfo.colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
configInfo.colorBlending.logicOpEnable = VK_FALSE;
configInfo.colorBlending.logicOp = VK_LOGIC_OP_COPY;
configInfo.colorBlending.attachmentCount = 1;
configInfo.colorBlending.pAttachments = &configInfo.colorBlendAttachment;
configInfo.colorBlending.blendConstants[0] = 0.0f;
configInfo.colorBlending.blendConstants[1] = 0.0f;
configInfo.colorBlending.blendConstants[2] = 0.0f;
configInfo.colorBlending.blendConstants[3] = 0.0f;
configInfo.depthStencilInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
configInfo.depthStencilInfo.depthTestEnable = VK_TRUE;
configInfo.depthStencilInfo.depthWriteEnable = VK_TRUE;
configInfo.depthStencilInfo.depthCompareOp = VK_COMPARE_OP_LESS;
configInfo.depthStencilInfo.depthBoundsTestEnable = VK_FALSE;
std::vector<VkDynamicState> dynamicStates = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
configInfo.dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
configInfo.dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
configInfo.dynamicState.pDynamicStates = dynamicStates.data();
return configInfo;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fastCode2019/test-vulkan.git
git@gitee.com:fastCode2019/test-vulkan.git
fastCode2019
test-vulkan
test-vulkan
master

搜索帮助