2019-03-15 09:40:45 +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
|
|
|
|
|
|
|
|
#include "../utils/geometries.hpp"
|
|
|
|
#include "../utils/math.hpp"
|
|
|
|
#include "../utils/shaders.hpp"
|
|
|
|
#include "../utils/utils.hpp"
|
|
|
|
#include "SPIRV/GlslangToSpv.h"
|
2020-04-12 19:49:12 +00:00
|
|
|
#include "vulkan/vulkan.hpp"
|
|
|
|
|
2019-03-15 09:40:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static char const * AppName = "14_InitPipeline";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
|
2019-03-15 09:40:45 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 500, 500 ) );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
|
|
|
|
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.surface );
|
|
|
|
vk::UniqueDevice device =
|
|
|
|
vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueRenderPass renderPass = vk::su::createRenderPass(
|
|
|
|
device,
|
|
|
|
vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( surfaceData.surface.get() ) ).format,
|
|
|
|
vk::Format::eD16Unorm );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueDescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(
|
|
|
|
device, { { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex } } );
|
|
|
|
vk::UniquePipelineLayout pipelineLayout = device->createPipelineLayoutUnique(
|
2020-07-08 08:58:37 +00:00
|
|
|
vk::PipelineLayoutCreateInfo( vk::PipelineLayoutCreateFlags(), *descriptorSetLayout ) );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
|
|
|
glslang::InitializeProcess();
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::UniqueShaderModule vertexShaderModule =
|
|
|
|
vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText_PC_C );
|
|
|
|
vk::UniqueShaderModule fragmentShaderModule =
|
|
|
|
vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText_C_C );
|
2019-03-15 09:40:45 +00:00
|
|
|
glslang::FinalizeProcess();
|
|
|
|
|
|
|
|
/* VULKAN_KEY_START */
|
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineShaderStageCreateInfo(
|
|
|
|
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eVertex, vertexShaderModule.get(), "main" ),
|
|
|
|
vk::PipelineShaderStageCreateInfo(
|
|
|
|
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eFragment, fragmentShaderModule.get(), "main" )
|
2019-03-15 09:40:45 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, sizeof( coloredCubeData[0] ) );
|
|
|
|
std::array<vk::VertexInputAttributeDescription, 2> vertexInputAttributeDescriptions = {
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::VertexInputAttributeDescription( 0, 0, vk::Format::eR32G32B32A32Sfloat, 0 ),
|
|
|
|
vk::VertexInputAttributeDescription( 1, 0, vk::Format::eR32G32B32A32Sfloat, 16 )
|
2019-03-15 09:40:45 +00:00
|
|
|
};
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo(
|
2019-03-15 09:40:45 +00:00
|
|
|
vk::PipelineVertexInputStateCreateFlags(), // flags
|
2020-07-08 08:58:37 +00:00
|
|
|
vertexInputBindingDescription, // vertexBindingDescriptions
|
|
|
|
vertexInputAttributeDescriptions // vertexAttributeDescriptions
|
2019-03-15 09:40:45 +00:00
|
|
|
);
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo(
|
|
|
|
vk::PipelineInputAssemblyStateCreateFlags(), vk::PrimitiveTopology::eTriangleList );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo(
|
|
|
|
vk::PipelineViewportStateCreateFlags(), 1, nullptr, 1, nullptr );
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo(
|
2019-03-15 09:40:45 +00:00
|
|
|
vk::PipelineRasterizationStateCreateFlags(), // 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
|
|
|
|
);
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo(
|
2019-12-16 13:51:29 +00:00
|
|
|
vk::PipelineMultisampleStateCreateFlags(), // flags
|
|
|
|
vk::SampleCountFlagBits::e1 // rasterizationSamples
|
|
|
|
// other values can be default
|
|
|
|
);
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::StencilOpState stencilOpState(
|
|
|
|
vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::CompareOp::eAlways );
|
|
|
|
vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(
|
|
|
|
vk::PipelineDepthStencilStateCreateFlags(), // flags
|
|
|
|
true, // depthTestEnable
|
|
|
|
true, // depthWriteEnable
|
|
|
|
vk::CompareOp::eLessOrEqual, // depthCompareOp
|
|
|
|
false, // depthBoundTestEnable
|
|
|
|
false, // stencilTestEnable
|
|
|
|
stencilOpState, // front
|
|
|
|
stencilOpState // back
|
2019-03-15 09:40:45 +00:00
|
|
|
);
|
|
|
|
|
2020-04-12 19:49:12 +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
|
2019-03-15 09:40:45 +00:00
|
|
|
);
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(
|
|
|
|
vk::PipelineColorBlendStateCreateFlags(), // flags
|
|
|
|
false, // logicOpEnable
|
|
|
|
vk::LogicOp::eNoOp, // logicOp
|
2020-07-08 08:58:37 +00:00
|
|
|
pipelineColorBlendAttachmentState, // attachments
|
2020-04-12 19:49:12 +00:00
|
|
|
{ { 1.0f, 1.0f, 1.0f, 1.0f } } // blendConstants
|
2019-03-15 09:40:45 +00:00
|
|
|
);
|
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
|
|
|
|
vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo( vk::PipelineDynamicStateCreateFlags(),
|
|
|
|
dynamicStates );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo(
|
|
|
|
vk::PipelineCreateFlags(), // flags
|
2020-07-08 08:58:37 +00:00
|
|
|
pipelineShaderStageCreateInfos, // stages
|
2020-04-12 19:49:12 +00:00
|
|
|
&pipelineVertexInputStateCreateInfo, // pVertexInputState
|
|
|
|
&pipelineInputAssemblyStateCreateInfo, // pInputAssemblyState
|
|
|
|
nullptr, // pTessellationState
|
|
|
|
&pipelineViewportStateCreateInfo, // pViewportState
|
|
|
|
&pipelineRasterizationStateCreateInfo, // pRasterizationState
|
|
|
|
&pipelineMultisampleStateCreateInfo, // pMultisampleState
|
|
|
|
&pipelineDepthStencilStateCreateInfo, // pDepthStencilState
|
|
|
|
&pipelineColorBlendStateCreateInfo, // pColorBlendState
|
|
|
|
&pipelineDynamicStateCreateInfo, // pDynamicState
|
|
|
|
pipelineLayout.get(), // layout
|
|
|
|
renderPass.get() // renderPass
|
2019-03-15 09:40:45 +00:00
|
|
|
);
|
|
|
|
|
2020-07-20 11:48:03 +00:00
|
|
|
vk::ResultValue<vk::UniquePipeline> pipeline =
|
|
|
|
device->createGraphicsPipelineUnique( nullptr, graphicsPipelineCreateInfo );
|
|
|
|
switch ( pipeline.result )
|
|
|
|
{
|
|
|
|
case vk::Result::eSuccess: break;
|
|
|
|
case vk::Result::ePipelineCompileRequiredEXT:
|
|
|
|
// something meaningfull here
|
|
|
|
break;
|
|
|
|
default: assert( false ); // should never happen
|
|
|
|
}
|
2019-03-15 09:40:45 +00:00
|
|
|
|
|
|
|
/* VULKAN_KEY_END */
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|