Vulkan-Hpp
14_InitPipeline.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // VulkanHpp Samples : 14_InitPipeline
16 // Initialize graphics pipeline
17 
18 #if defined( _MSC_VER )
19 // no need to ignore any warnings with MSVC
20 #elif defined( __clang__ )
21 # pragma clang diagnostic ignored "-Wmissing-braces"
22 #elif defined( __GNUC__ )
23 // no need to ignore any warnings with GCC
24 #else
25 // unknow compiler... just ignore the warnings for yourselves ;)
26 #endif
27 
28 #include "../utils/geometries.hpp"
29 #include "../utils/math.hpp"
30 #include "../utils/shaders.hpp"
31 #include "../utils/utils.hpp"
32 #include "SPIRV/GlslangToSpv.h"
33 
34 #include <iostream>
35 
36 static char const * AppName = "14_InitPipeline";
37 static char const * EngineName = "Vulkan.hpp";
38 
39 int main( int /*argc*/, char ** /*argv*/ )
40 {
41  try
42  {
43  vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
44 #if !defined( NDEBUG )
46 #endif
47 
48  vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
49 
50  vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
51 
52  std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
53  vk::Device device = vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
54 
55  vk::RenderPass renderPass =
57 
58  vk::DescriptorSetLayout descriptorSetLayout =
61 
62  glslang::InitializeProcess();
65  glslang::FinalizeProcess();
66 
67  /* VULKAN_KEY_START */
68 
69  std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
72  };
73 
74  vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( coloredCubeData[0] ) );
75  std::array<vk::VertexInputAttributeDescription, 2> vertexInputAttributeDescriptions = {
78  };
79  vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo( vk::PipelineVertexInputStateCreateFlags(), // flags
80  vertexInputBindingDescription, // vertexBindingDescriptions
81  vertexInputAttributeDescriptions // vertexAttributeDescriptions
82  );
83 
86 
87  vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo( vk::PipelineViewportStateCreateFlags(), 1, nullptr, 1, nullptr );
88 
89  vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo( vk::PipelineRasterizationStateCreateFlags(), // flags
90  false, // depthClampEnable
91  false, // rasterizerDiscardEnable
92  vk::PolygonMode::eFill, // polygonMode
93  vk::CullModeFlagBits::eBack, // cullMode
94  vk::FrontFace::eClockwise, // frontFace
95  false, // depthBiasEnable
96  0.0f, // depthBiasConstantFactor
97  0.0f, // depthBiasClamp
98  0.0f, // depthBiasSlopeFactor
99  1.0f // lineWidth
100  );
101 
102  vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo( vk::PipelineMultisampleStateCreateFlags(), // flags
103  vk::SampleCountFlagBits::e1 // rasterizationSamples
104  // other values can be default
105  );
106 
108  vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo( vk::PipelineDepthStencilStateCreateFlags(), // flags
109  true, // depthTestEnable
110  true, // depthWriteEnable
111  vk::CompareOp::eLessOrEqual, // depthCompareOp
112  false, // depthBoundTestEnable
113  false, // stencilTestEnable
114  stencilOpState, // front
115  stencilOpState // back
116  );
117 
120  vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState( false, // blendEnable
121  vk::BlendFactor::eZero, // srcColorBlendFactor
122  vk::BlendFactor::eZero, // dstColorBlendFactor
123  vk::BlendOp::eAdd, // colorBlendOp
124  vk::BlendFactor::eZero, // srcAlphaBlendFactor
125  vk::BlendFactor::eZero, // dstAlphaBlendFactor
126  vk::BlendOp::eAdd, // alphaBlendOp
127  colorComponentFlags // colorWriteMask
128  );
129  vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo( vk::PipelineColorBlendStateCreateFlags(), // flags
130  false, // logicOpEnable
131  vk::LogicOp::eNoOp, // logicOp
132  pipelineColorBlendAttachmentState, // attachments
133  { { 1.0f, 1.0f, 1.0f, 1.0f } } // blendConstants
134  );
135 
136  std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
137  vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo( vk::PipelineDynamicStateCreateFlags(), dynamicStates );
138 
139  vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo( vk::PipelineCreateFlags(), // flags
140  pipelineShaderStageCreateInfos, // stages
141  &pipelineVertexInputStateCreateInfo, // pVertexInputState
142  &pipelineInputAssemblyStateCreateInfo, // pInputAssemblyState
143  nullptr, // pTessellationState
144  &pipelineViewportStateCreateInfo, // pViewportState
145  &pipelineRasterizationStateCreateInfo, // pRasterizationState
146  &pipelineMultisampleStateCreateInfo, // pMultisampleState
147  &pipelineDepthStencilStateCreateInfo, // pDepthStencilState
148  &pipelineColorBlendStateCreateInfo, // pColorBlendState
149  &pipelineDynamicStateCreateInfo, // pDynamicState
150  pipelineLayout, // layout
151  renderPass // renderPass
152  );
153 
154  vk::Result result;
155  vk::Pipeline pipeline;
156  std::tie( result, pipeline ) = device.createGraphicsPipeline( nullptr, graphicsPipelineCreateInfo );
157  switch ( result )
158  {
159  case vk::Result::eSuccess: break;
161  // something meaningfull here
162  break;
163  default: assert( false ); // should never happen
164  }
165 
166  device.destroyPipeline( pipeline );
167 
168  /* VULKAN_KEY_END */
169 
170  device.destroyShaderModule( fragmentShaderModule );
171  device.destroyShaderModule( vertexShaderModule );
172  device.destroyPipelineLayout( pipelineLayout );
173  device.destroyDescriptorSetLayout( descriptorSetLayout );
174  device.destroyRenderPass( renderPass );
175  device.destroy();
176  instance.destroySurfaceKHR( surfaceData.surface );
177 #if !defined( NDEBUG )
178  instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
179 #endif
180  instance.destroy();
181  }
182  catch ( vk::SystemError & err )
183  {
184  std::cout << "vk::SystemError: " << err.what() << std::endl;
185  exit( -1 );
186  }
187  catch ( std::exception & err )
188  {
189  std::cout << "std::exception: " << err.what() << std::endl;
190  exit( -1 );
191  }
192  catch ( ... )
193  {
194  std::cout << "unknown error\n";
195  exit( -1 );
196  }
197  return 0;
198 }
int main(int, char **)
void cout(vk::SurfaceCapabilitiesKHR const &surfaceCapabilities)
VULKAN_HPP_NODISCARD Result createPipelineLayout(const vk::PipelineLayoutCreateInfo *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::PipelineLayout *pPipelineLayout, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyShaderModule(vk::ShaderModule shaderModule, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD ResultValue< vk::Pipeline > createGraphicsPipeline(vk::PipelineCache pipelineCache, const vk::GraphicsPipelineCreateInfo &createInfo, Optional< const vk::AllocationCallbacks > allocator VULKAN_HPP_DEFAULT_ARGUMENT_NULLPTR_ASSIGNMENT, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const
void destroyPipelineLayout(vk::PipelineLayout pipelineLayout, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyPipeline(vk::Pipeline pipeline, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyDescriptorSetLayout(vk::DescriptorSetLayout descriptorSetLayout, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyRenderPass(vk::RenderPass renderPass, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result createDebugUtilsMessengerEXT(const vk::DebugUtilsMessengerCreateInfoEXT *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::DebugUtilsMessengerEXT *pMessenger, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result enumeratePhysicalDevices(uint32_t *pPhysicalDeviceCount, vk::PhysicalDevice *pPhysicalDevices, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroySurfaceKHR(vk::SurfaceKHR surface, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyDebugUtilsMessengerEXT(vk::DebugUtilsMessengerEXT messenger, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result getSurfaceFormatsKHR(vk::SurfaceKHR surface, uint32_t *pSurfaceFormatCount, vk::SurfaceFormatKHR *pSurfaceFormats, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
virtual const char * what() const VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6206
vk::DebugUtilsMessengerCreateInfoEXT makeDebugUtilsMessengerCreateInfoEXT()
Definition: utils.cpp:1025
vk::ShaderModule createShaderModule(vk::Device const &device, vk::ShaderStageFlagBits shaderStage, std::string const &shaderText)
Definition: shaders.cpp:88
vk::RenderPass createRenderPass(vk::Device const &device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp, vk::ImageLayout colorFinalLayout)
Definition: utils.cpp:314
vk::Instance createInstance(std::string const &appName, std::string const &engineName, std::vector< std::string > const &layers, std::vector< std::string > const &extensions, uint32_t apiVersion)
Definition: utils.cpp:279
vk::Device createDevice(vk::PhysicalDevice const &physicalDevice, uint32_t queueFamilyIndex, std::vector< std::string > const &extensions, vk::PhysicalDeviceFeatures const *physicalDeviceFeatures, void const *pNext)
Definition: utils.cpp:86
vk::DescriptorSetLayout createDescriptorSetLayout(vk::Device const &device, std::vector< std::tuple< vk::DescriptorType, uint32_t, vk::ShaderStageFlags >> const &bindingData, vk::DescriptorSetLayoutCreateFlags flags)
Definition: utils.cpp:73
std::pair< uint32_t, uint32_t > findGraphicsAndPresentQueueFamilyIndex(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR const &surface)
Definition: utils.cpp:420
std::vector< std::string > getInstanceExtensions()
Definition: utils.cpp:477
vk::SurfaceFormatKHR pickSurfaceFormat(std::vector< vk::SurfaceFormatKHR > const &formats)
Definition: utils.cpp:539
std::vector< std::string > getDeviceExtensions()
Definition: utils.cpp:472
@ ePipelineCompileRequiredEXT
@ eR32G32B32A32Sfloat
const std::string vertexShaderText_PC_C
Definition: shaders.hpp:31
const std::string fragmentShaderText_C_C
Definition: shaders.hpp:79
vk::SurfaceKHR surface
Definition: utils.hpp:205