2021-02-17 09:49:59 +00:00
|
|
|
// Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// VulkanHpp Samples : 14_InitPipeline
|
|
|
|
// Initialize graphics pipeline
|
|
|
|
|
|
|
|
#if defined( _MSC_VER )
|
|
|
|
// no need to ignore any warnings with MSVC
|
|
|
|
#elif defined( __clang__ )
|
|
|
|
# pragma clang diagnostic ignored "-Wmissing-braces"
|
|
|
|
#elif defined( __GNUC__ )
|
|
|
|
// no need to ignore any warnings with GCC
|
|
|
|
#else
|
|
|
|
// unknow compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../../samples/utils/geometries.hpp"
|
|
|
|
#include "../../samples/utils/math.hpp"
|
|
|
|
#include "../utils/shaders.hpp"
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
#include "SPIRV/GlslangToSpv.h"
|
2024-03-17 05:07:15 +00:00
|
|
|
#include "glslang/Public/ShaderLang.h"
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
static char const * AppName = "14_InitPipeline";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
|
|
|
|
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Context context;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::Instance instance = vk::raii::su::makeInstance( context, AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
2021-02-17 09:49:59 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::DebugUtilsMessengerEXT debugUtilsMessenger( instance, vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2021-02-17 09:49:59 +00:00
|
|
|
#endif
|
2022-03-22 11:26:28 +00:00
|
|
|
vk::raii::PhysicalDevice physicalDevice = vk::raii::PhysicalDevices( instance ).front();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::Format colorFormat = vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( surfaceData.surface ) ).format;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::RenderPass renderPass = vk::raii::su::makeRenderPass( device, colorFormat, vk::Format::eD16Unorm );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::DescriptorSetLayout descriptorSetLayout =
|
|
|
|
vk::raii::su::makeDescriptorSetLayout( device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::PipelineLayout pipelineLayout( device, { {}, *descriptorSetLayout } );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
glslang::InitializeProcess();
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::ShaderModule vertexShaderModule = vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
|
|
|
|
vk::raii::ShaderModule fragmentShaderModule = vk::raii::su::makeShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
|
2021-02-17 09:49:59 +00:00
|
|
|
glslang::FinalizeProcess();
|
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
|
|
|
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, vertexShaderModule, "main" ),
|
|
|
|
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, fragmentShaderModule, "main" )
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( coloredCubeData[0] ) );
|
|
|
|
std::array<vk::VertexInputAttributeDescription, 2> vertexInputAttributeDescriptions = {
|
|
|
|
vk::VertexInputAttributeDescription( 0, 0, vk::Format::eR32G32B32A32Sfloat, 0 ),
|
|
|
|
vk::VertexInputAttributeDescription( 1, 0, vk::Format::eR32G32B32A32Sfloat, 16 )
|
|
|
|
};
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo( {}, // flags
|
|
|
|
vertexInputBindingDescription, // vertexBindingDescriptions
|
|
|
|
vertexInputAttributeDescriptions // vertexAttributeDescriptions
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo( {}, vk::PrimitiveTopology::eTriangleList );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo( {}, 1, nullptr, 1, nullptr );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo( {}, // flags
|
|
|
|
false, // depthClampEnable
|
|
|
|
false, // rasterizerDiscardEnable
|
|
|
|
vk::PolygonMode::eFill, // polygonMode
|
|
|
|
vk::CullModeFlagBits::eBack, // cullMode
|
|
|
|
vk::FrontFace::eClockwise, // frontFace
|
|
|
|
false, // depthBiasEnable
|
|
|
|
0.0f, // depthBiasConstantFactor
|
|
|
|
0.0f, // depthBiasClamp
|
|
|
|
0.0f, // depthBiasSlopeFactor
|
|
|
|
1.0f // lineWidth
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo( {}, // flags
|
|
|
|
vk::SampleCountFlagBits::e1 // rasterizationSamples
|
|
|
|
// other values can be default
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::StencilOpState stencilOpState( vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::CompareOp::eAlways );
|
|
|
|
vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo( {}, // flags
|
|
|
|
true, // depthTestEnable
|
|
|
|
true, // depthWriteEnable
|
|
|
|
vk::CompareOp::eLessOrEqual, // depthCompareOp
|
|
|
|
false, // depthBoundTestEnable
|
|
|
|
false, // stencilTestEnable
|
|
|
|
stencilOpState, // front
|
|
|
|
stencilOpState // back
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ColorComponentFlags colorComponentFlags( vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB |
|
|
|
|
vk::ColorComponentFlagBits::eA );
|
|
|
|
vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState( false, // blendEnable
|
|
|
|
vk::BlendFactor::eZero, // srcColorBlendFactor
|
|
|
|
vk::BlendFactor::eZero, // dstColorBlendFactor
|
|
|
|
vk::BlendOp::eAdd, // colorBlendOp
|
|
|
|
vk::BlendFactor::eZero, // srcAlphaBlendFactor
|
|
|
|
vk::BlendFactor::eZero, // dstAlphaBlendFactor
|
|
|
|
vk::BlendOp::eAdd, // alphaBlendOp
|
|
|
|
colorComponentFlags // colorWriteMask
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo( {}, // flags
|
|
|
|
false, // logicOpEnable
|
|
|
|
vk::LogicOp::eNoOp, // logicOp
|
|
|
|
pipelineColorBlendAttachmentState, // attachments
|
|
|
|
{ { 1.0f, 1.0f, 1.0f, 1.0f } } // blendConstants
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
|
|
|
|
vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo( {}, dynamicStates );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo( {}, // flags
|
|
|
|
pipelineShaderStageCreateInfos, // stages
|
|
|
|
&pipelineVertexInputStateCreateInfo, // pVertexInputState
|
|
|
|
&pipelineInputAssemblyStateCreateInfo, // pInputAssemblyState
|
|
|
|
nullptr, // pTessellationState
|
|
|
|
&pipelineViewportStateCreateInfo, // pViewportState
|
|
|
|
&pipelineRasterizationStateCreateInfo, // pRasterizationState
|
|
|
|
&pipelineMultisampleStateCreateInfo, // pMultisampleState
|
|
|
|
&pipelineDepthStencilStateCreateInfo, // pDepthStencilState
|
|
|
|
&pipelineColorBlendStateCreateInfo, // pColorBlendState
|
|
|
|
&pipelineDynamicStateCreateInfo, // pDynamicState
|
2024-01-16 12:48:53 +00:00
|
|
|
pipelineLayout, // layout
|
|
|
|
renderPass // renderPass
|
2021-02-17 09:49:59 +00:00
|
|
|
);
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Pipeline pipeline( device, nullptr, graphicsPipelineCreateInfo );
|
|
|
|
switch ( pipeline.getConstructorSuccessCode() )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
case vk::Result::eSuccess: break;
|
|
|
|
case vk::Result::ePipelineCompileRequiredEXT:
|
|
|
|
// something meaningfull here
|
|
|
|
break;
|
|
|
|
default: assert( false ); // should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
|
|
|
catch ( vk::SystemError & err )
|
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
catch ( std::exception & err )
|
|
|
|
{
|
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
catch ( ... )
|
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|