Advertisement
whillothewhisp

pipeline creation

Jan 5th, 2022 (edited)
1,810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.73 KB | None | 0 0
  1.    VkPipeline pipeline;
  2.  
  3.     //vert/frag stages
  4.     VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
  5.     vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  6.     vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
  7.     vertShaderStageInfo.module = create_shader_module_from_file_path("Shaders/objectpicking_vert.spv");
  8.     vertShaderStageInfo.pName = "main";
  9.  
  10.     VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
  11.     fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  12.     fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  13.     fragShaderStageInfo.module = create_shader_module_from_file_path("Shaders/objectpicking_frag.spv");
  14.     fragShaderStageInfo.pName = "main";
  15.     std::vector< VkPipelineShaderStageCreateInfo> shaderStages;
  16.     shaderStages.push_back(vertShaderStageInfo);
  17.     shaderStages.push_back(fragShaderStageInfo);
  18.  
  19.     VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
  20.     vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  21.  
  22.     auto bindingDescription = Vertex::get_binding_description();
  23.     auto attributeDescriptions = Vertex::get_attribute_descriptions();
  24.  
  25.     vertexInputInfo.vertexBindingDescriptionCount = 1;
  26.     vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
  27.     vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
  28.     vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
  29.  
  30.     VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
  31.     inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  32.     inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  33.     inputAssembly.primitiveRestartEnable = VK_FALSE;
  34.  
  35.     VkViewport viewport{};
  36.     viewport.x = 0;
  37.     viewport.y = 0;
  38.     viewport.width = (float)swapChainExtent.width;
  39.     viewport.height = (float)swapChainExtent.height;
  40.     viewport.minDepth = 0.0f;
  41.     viewport.maxDepth = 1.0f;
  42.  
  43.     //We're going to have a dynamic scissor so this doesnt really matter
  44.     VkRect2D scissor{};
  45.     scissor.offset =  { 0,0 };
  46.     scissor.extent = swapChainExtent;
  47.  
  48.     VkPipelineViewportStateCreateInfo viewportState{};
  49.     viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  50.     viewportState.viewportCount = 1;
  51.     viewportState.pViewports = &viewport;
  52.     viewportState.scissorCount = 1;
  53.     viewportState.pScissors = &scissor;
  54.  
  55.     VkPipelineRasterizationStateCreateInfo rasterizer{};
  56.     rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  57.     rasterizer.depthClampEnable = VK_FALSE;
  58.     rasterizer.rasterizerDiscardEnable = VK_FALSE;
  59.     rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
  60.     rasterizer.lineWidth = 1.0f;
  61.     rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
  62.     rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  63.     rasterizer.depthBiasEnable = VK_FALSE;
  64.    
  65.     //Only one samples since this is offscreen
  66.     VkPipelineMultisampleStateCreateInfo multisampling{};
  67.     multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  68.     multisampling.sampleShadingEnable = VK_FALSE;
  69.     multisampling.rasterizationSamples =  VK_SAMPLE_COUNT_1_BIT;
  70.  
  71.     VkPipelineDepthStencilStateCreateInfo depthStencil{};
  72.     depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  73.     depthStencil.depthTestEnable = VK_TRUE;
  74.     depthStencil.depthWriteEnable =  VK_TRUE;
  75.     depthStencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
  76.     depthStencil.depthBoundsTestEnable = VK_FALSE;
  77.     depthStencil.stencilTestEnable = VK_FALSE;
  78.    
  79.     //blending is set to false since we don't need it here
  80.     VkPipelineColorBlendAttachmentState colorBlendAttachment{};
  81.     colorBlendAttachment.colorWriteMask =  VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT |                VK_COLOR_COMPONENT_A_BIT;
  82.     colorBlendAttachment.blendEnable = VK_FALSE;
  83.     colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  84.     colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  85.     colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
  86.     colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
  87.     colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  88.     colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
  89.  
  90.     VkPipelineColorBlendStateCreateInfo colorBlending{};
  91.     colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  92.     colorBlending.logicOpEnable = VK_FALSE;
  93.     colorBlending.logicOp = VK_LOGIC_OP_COPY;
  94.     colorBlending.attachmentCount = 1;
  95.     colorBlending.pAttachments = &colorBlendAttachment;
  96.     colorBlending.blendConstants[0] = 1.0f;
  97.     colorBlending.blendConstants[1] = 0.0f;
  98.     colorBlending.blendConstants[2] = 0.0f;
  99.     colorBlending.blendConstants[3] = 0.0f;
  100.    
  101.     //we only need a dynamic state for the scissor, this means whatever we set to the scissor here will be ignored, and we'll need to       use vkCmdSetScissor() every frame we use the pipeline
  102.     std::array<VkDynamicState, 1> states = { VK_DYNAMIC_STATE_SCISSOR};
  103.  
  104.     VkPipelineDynamicStateCreateInfo dynamic_state{};
  105.     dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  106.     dynamic_state.dynamicStateCount = states.size();
  107.     dynamic_state.pDynamicStates = states.data();
  108.    
  109.     VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
  110.     pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  111.     pipelineLayoutInfo.setLayoutCount = 1;
  112.     pipelineLayoutInfo.pSetLayouts = &descriptor_set_layouts[pipelines[object_picking_pipeline].descriptor_set_index];
  113.    
  114.     if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelines[object_picking_pipeline].pipeline_layout) !=                   VK_SUCCESS)
  115.     {
  116.         throw std::runtime_error("failed to create pipeline layout!");
  117.     }
  118.  
  119.     VkGraphicsPipelineCreateInfo pipelineInfo{};
  120.     pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  121.     pipelineInfo.stageCount = shaderStages.size();
  122.     pipelineInfo.pStages = shaderStages.data();
  123.     pipelineInfo.pVertexInputState = &vertexInputInfo;
  124.     pipelineInfo.pInputAssemblyState = &inputAssembly;
  125.     pipelineInfo.pViewportState = &viewportState;
  126.     pipelineInfo.pRasterizationState = &rasterizer;
  127.     pipelineInfo.pMultisampleState = &multisampling;
  128.     pipelineInfo.pDepthStencilState = &depthStencil;
  129.     pipelineInfo.pColorBlendState = &colorBlending;
  130.     pipelineInfo.layout = pipelines[object_picking_pipeline].pipeline_layout;
  131.     pipelineInfo.renderPass = object_picking_pass;
  132.     pipelineInfo.subpass = 0;
  133.     pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
  134.     pipelineInfo.pDynamicState = &dynamic_state;
  135.  
  136.     if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline) != VK_SUCCESS) {
  137.         throw std::runtime_error("failed to create graphics pipeline!");
  138.     }
  139.  
  140.     vkDestroyShaderModule(device, params.fragShaderModule, nullptr);
  141.     vkDestroyShaderModule(device, params.vertShaderModule, nullptr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement