2019-03-05 07:59:40 +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.
|
|
|
|
//
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
#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
|
|
|
|
|
2019-03-05 07:59:40 +00:00
|
|
|
#include "utils.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2019-05-09 13:25:40 +00:00
|
|
|
#include <iomanip>
|
2019-05-21 13:44:52 +00:00
|
|
|
#include <numeric>
|
2022-07-26 09:13:43 +00:00
|
|
|
#include <vulkan/vulkan.hpp>
|
|
|
|
#if defined( VULKAN_HPP_NO_TO_STRING )
|
|
|
|
# include <vulkan/vulkan_to_string.hpp>
|
|
|
|
#endif
|
2022-08-03 09:48:29 +00:00
|
|
|
#include <vulkan/vulkan_static_assertions.hpp>
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
2019-09-26 07:55:15 +00:00
|
|
|
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
|
|
|
|
#endif
|
2019-03-05 07:59:40 +00:00
|
|
|
|
|
|
|
namespace vk
|
|
|
|
{
|
|
|
|
namespace su
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DeviceMemory allocateDeviceMemory( vk::Device const & device,
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PhysicalDeviceMemoryProperties const & memoryProperties,
|
|
|
|
vk::MemoryRequirements const & memoryRequirements,
|
|
|
|
vk::MemoryPropertyFlags memoryPropertyFlags )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t memoryTypeIndex = findMemoryType( memoryProperties, memoryRequirements.memoryTypeBits, memoryPropertyFlags );
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
return device.allocateMemory( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
bool contains( std::vector<vk::ExtensionProperties> const & extensionProperties, std::string const & extensionName )
|
2019-10-28 14:36:21 +00:00
|
|
|
{
|
2021-05-25 14:14:56 +00:00
|
|
|
auto propertyIterator = std::find_if( extensionProperties.begin(),
|
|
|
|
extensionProperties.end(),
|
2022-02-28 09:11:04 +00:00
|
|
|
[&extensionName]( vk::ExtensionProperties const & ep ) { return extensionName == ep.extensionName; } );
|
2021-02-17 09:49:59 +00:00
|
|
|
return ( propertyIterator != extensionProperties.end() );
|
2019-10-28 14:36:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorPool createDescriptorPool( vk::Device const & device, std::vector<vk::DescriptorPoolSize> const & poolSizes )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( !poolSizes.empty() );
|
|
|
|
uint32_t maxSets =
|
2022-02-28 09:11:04 +00:00
|
|
|
std::accumulate( poolSizes.begin(), poolSizes.end(), 0, []( uint32_t sum, vk::DescriptorPoolSize const & dps ) { return sum + dps.descriptorCount; } );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( 0 < maxSets );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, maxSets, poolSizes );
|
2021-02-17 09:49:59 +00:00
|
|
|
return device.createDescriptorPool( descriptorPoolCreateInfo );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorSetLayout createDescriptorSetLayout( vk::Device const & device,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const & bindingData,
|
|
|
|
vk::DescriptorSetLayoutCreateFlags flags )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<vk::DescriptorSetLayoutBinding> bindings( bindingData.size() );
|
|
|
|
for ( size_t i = 0; i < bindingData.size(); i++ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
bindings[i] = vk::DescriptorSetLayoutBinding(
|
|
|
|
checked_cast<uint32_t>( i ), std::get<0>( bindingData[i] ), std::get<1>( bindingData[i] ), std::get<2>( bindingData[i] ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
return device.createDescriptorSetLayout( vk::DescriptorSetLayoutCreateInfo( flags, bindings ) );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Device createDevice( vk::PhysicalDevice const & physicalDevice,
|
|
|
|
uint32_t queueFamilyIndex,
|
|
|
|
std::vector<std::string> const & extensions,
|
|
|
|
vk::PhysicalDeviceFeatures const * physicalDeviceFeatures,
|
|
|
|
void const * pNext )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<char const *> enabledExtensions;
|
|
|
|
enabledExtensions.reserve( extensions.size() );
|
|
|
|
for ( auto const & ext : extensions )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
enabledExtensions.push_back( ext.data() );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
float queuePriority = 0.0f;
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo( {}, queueFamilyIndex, 1, &queuePriority );
|
2022-03-23 16:08:19 +00:00
|
|
|
vk::DeviceCreateInfo deviceCreateInfo( {}, deviceQueueCreateInfo, {}, enabledExtensions, physicalDeviceFeatures, pNext );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-03-09 08:13:32 +00:00
|
|
|
vk::Device device = physicalDevice.createDevice( deviceCreateInfo );
|
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
|
|
|
// initialize function pointers for instance
|
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init( device );
|
|
|
|
#endif
|
|
|
|
return device;
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<vk::Framebuffer> createFramebuffers( vk::Device const & device,
|
|
|
|
vk::RenderPass & renderPass,
|
|
|
|
std::vector<vk::ImageView> const & imageViews,
|
|
|
|
vk::ImageView const & depthImageView,
|
|
|
|
vk::Extent2D const & extent )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
vk::ImageView attachments[2];
|
2021-02-17 09:49:59 +00:00
|
|
|
attachments[1] = depthImageView;
|
|
|
|
|
|
|
|
vk::FramebufferCreateInfo framebufferCreateInfo(
|
|
|
|
vk::FramebufferCreateFlags(), renderPass, depthImageView ? 2 : 1, attachments, extent.width, extent.height, 1 );
|
|
|
|
std::vector<vk::Framebuffer> framebuffers;
|
2020-04-12 19:49:12 +00:00
|
|
|
framebuffers.reserve( imageViews.size() );
|
|
|
|
for ( auto const & view : imageViews )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
attachments[0] = view;
|
|
|
|
framebuffers.push_back( device.createFramebuffer( framebufferCreateInfo ) );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return framebuffers;
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::Pipeline createGraphicsPipeline( vk::Device const & device,
|
|
|
|
vk::PipelineCache const & pipelineCache,
|
|
|
|
std::pair<vk::ShaderModule, vk::SpecializationInfo const *> const & vertexShaderData,
|
|
|
|
std::pair<vk::ShaderModule, vk::SpecializationInfo const *> const & fragmentShaderData,
|
|
|
|
uint32_t vertexStride,
|
|
|
|
std::vector<std::pair<vk::Format, uint32_t>> const & vertexInputAttributeFormatOffset,
|
|
|
|
vk::FrontFace frontFace,
|
|
|
|
bool depthBuffered,
|
|
|
|
vk::PipelineLayout const & pipelineLayout,
|
|
|
|
vk::RenderPass const & renderPass )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineShaderStageCreateInfo(
|
|
|
|
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eVertex, vertexShaderData.first, "main", vertexShaderData.second ),
|
|
|
|
vk::PipelineShaderStageCreateInfo(
|
|
|
|
vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eFragment, fragmentShaderData.first, "main", fragmentShaderData.second )
|
2019-03-15 09:40:45 +00:00
|
|
|
};
|
|
|
|
|
2019-06-25 07:47:27 +00:00
|
|
|
std::vector<vk::VertexInputAttributeDescription> vertexInputAttributeDescriptions;
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo;
|
|
|
|
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, vertexStride );
|
|
|
|
|
|
|
|
if ( 0 < vertexStride )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vertexInputAttributeDescriptions.reserve( vertexInputAttributeFormatOffset.size() );
|
|
|
|
for ( uint32_t i = 0; i < vertexInputAttributeFormatOffset.size(); i++ )
|
2019-04-09 13:19:18 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
vertexInputAttributeDescriptions.emplace_back( i, 0, vertexInputAttributeFormatOffset[i].first, vertexInputAttributeFormatOffset[i].second );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
pipelineVertexInputStateCreateInfo.setVertexBindingDescriptions( vertexInputBindingDescription );
|
|
|
|
pipelineVertexInputStateCreateInfo.setVertexAttributeDescriptions( vertexInputAttributeDescriptions );
|
2019-04-09 13:19:18 +00:00
|
|
|
}
|
2019-03-15 09:40:45 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateCreateInfo( vk::PipelineInputAssemblyStateCreateFlags(),
|
|
|
|
vk::PrimitiveTopology::eTriangleList );
|
|
|
|
|
|
|
|
vk::PipelineViewportStateCreateInfo pipelineViewportStateCreateInfo( vk::PipelineViewportStateCreateFlags(), 1, nullptr, 1, nullptr );
|
|
|
|
|
|
|
|
vk::PipelineRasterizationStateCreateInfo pipelineRasterizationStateCreateInfo( vk::PipelineRasterizationStateCreateFlags(),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
vk::PolygonMode::eFill,
|
|
|
|
vk::CullModeFlagBits::eBack,
|
|
|
|
frontFace,
|
|
|
|
false,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
1.0f );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
vk::PipelineMultisampleStateCreateInfo pipelineMultisampleStateCreateInfo( {}, vk::SampleCountFlagBits::e1 );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::StencilOpState stencilOpState( vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::StencilOp::eKeep, vk::CompareOp::eAlways );
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineDepthStencilStateCreateInfo pipelineDepthStencilStateCreateInfo(
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineDepthStencilStateCreateFlags(), depthBuffered, depthBuffered, vk::CompareOp::eLessOrEqual, false, false, stencilOpState, stencilOpState );
|
|
|
|
|
|
|
|
vk::ColorComponentFlags colorComponentFlags( vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB |
|
|
|
|
vk::ColorComponentFlagBits::eA );
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PipelineColorBlendAttachmentState pipelineColorBlendAttachmentState( false,
|
|
|
|
vk::BlendFactor::eZero,
|
|
|
|
vk::BlendFactor::eZero,
|
|
|
|
vk::BlendOp::eAdd,
|
|
|
|
vk::BlendFactor::eZero,
|
|
|
|
vk::BlendFactor::eZero,
|
|
|
|
vk::BlendOp::eAdd,
|
|
|
|
colorComponentFlags );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(
|
|
|
|
vk::PipelineColorBlendStateCreateFlags(), false, vk::LogicOp::eNoOp, pipelineColorBlendAttachmentState, { { 1.0f, 1.0f, 1.0f, 1.0f } } );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
std::array<vk::DynamicState, 2> dynamicStates = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineDynamicStateCreateInfo pipelineDynamicStateCreateInfo( vk::PipelineDynamicStateCreateFlags(), dynamicStates );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo( vk::PipelineCreateFlags(),
|
|
|
|
pipelineShaderStageCreateInfos,
|
|
|
|
&pipelineVertexInputStateCreateInfo,
|
|
|
|
&pipelineInputAssemblyStateCreateInfo,
|
|
|
|
nullptr,
|
|
|
|
&pipelineViewportStateCreateInfo,
|
|
|
|
&pipelineRasterizationStateCreateInfo,
|
|
|
|
&pipelineMultisampleStateCreateInfo,
|
|
|
|
&pipelineDepthStencilStateCreateInfo,
|
|
|
|
&pipelineColorBlendStateCreateInfo,
|
|
|
|
&pipelineDynamicStateCreateInfo,
|
2021-02-17 09:49:59 +00:00
|
|
|
pipelineLayout,
|
|
|
|
renderPass );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
auto result = device.createGraphicsPipeline( pipelineCache, graphicsPipelineCreateInfo );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( result.result == vk::Result::eSuccess );
|
2021-02-17 09:49:59 +00:00
|
|
|
return result.value;
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<char const *> gatherExtensions( std::vector<std::string> const & extensions
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
,
|
|
|
|
std::vector<vk::ExtensionProperties> const & extensionProperties
|
|
|
|
#endif
|
|
|
|
)
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<char const *> enabledExtensions;
|
|
|
|
enabledExtensions.reserve( extensions.size() );
|
|
|
|
for ( auto const & ext : extensions )
|
|
|
|
{
|
2023-07-13 09:26:01 +00:00
|
|
|
assert( std::any_of(
|
|
|
|
extensionProperties.begin(), extensionProperties.end(), [ext]( vk::ExtensionProperties const & ep ) { return ext == ep.extensionName; } ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
enabledExtensions.push_back( ext.data() );
|
|
|
|
}
|
|
|
|
#if !defined( NDEBUG )
|
2023-07-13 09:26:01 +00:00
|
|
|
if ( std::none_of(
|
|
|
|
extensions.begin(), extensions.end(), []( std::string const & extension ) { return extension == VK_EXT_DEBUG_UTILS_EXTENSION_NAME; } ) &&
|
|
|
|
std::any_of( extensionProperties.begin(),
|
|
|
|
extensionProperties.end(),
|
|
|
|
[]( vk::ExtensionProperties const & ep ) { return ( strcmp( VK_EXT_DEBUG_UTILS_EXTENSION_NAME, ep.extensionName ) == 0 ); } ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
enabledExtensions.push_back( VK_EXT_DEBUG_UTILS_EXTENSION_NAME );
|
|
|
|
}
|
2019-09-26 07:55:15 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
return enabledExtensions;
|
|
|
|
}
|
2019-09-26 07:55:15 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
std::vector<char const *> gatherLayers( std::vector<std::string> const & layers
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-02-17 09:49:59 +00:00
|
|
|
,
|
|
|
|
std::vector<vk::LayerProperties> const & layerProperties
|
2019-12-12 10:40:21 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
)
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<char const *> enabledLayers;
|
|
|
|
enabledLayers.reserve( layers.size() );
|
|
|
|
for ( auto const & layer : layers )
|
2019-07-09 07:25:48 +00:00
|
|
|
{
|
2023-07-13 09:26:01 +00:00
|
|
|
assert( std::any_of( layerProperties.begin(), layerProperties.end(), [layer]( vk::LayerProperties const & lp ) { return layer == lp.layerName; } ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
enabledLayers.push_back( layer.data() );
|
2019-07-09 07:25:48 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2019-03-05 07:59:40 +00:00
|
|
|
// Enable standard validation layer to find as much errors as possible!
|
2023-07-13 09:26:01 +00:00
|
|
|
if ( std::none_of( layers.begin(), layers.end(), []( std::string const & layer ) { return layer == "VK_LAYER_KHRONOS_validation"; } ) &&
|
|
|
|
std::any_of( layerProperties.begin(),
|
|
|
|
layerProperties.end(),
|
|
|
|
[]( vk::LayerProperties const & lp ) { return ( strcmp( "VK_LAYER_KHRONOS_validation", lp.layerName ) == 0 ); } ) )
|
2019-07-09 07:25:48 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
enabledLayers.push_back( "VK_LAYER_KHRONOS_validation" );
|
2019-07-09 07:25:48 +00:00
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
return enabledLayers;
|
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
2023-09-07 13:20:10 +00:00
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init();
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ApplicationInfo applicationInfo( appName.c_str(), 1, engineName.c_str(), 1, apiVersion );
|
2021-05-25 14:14:56 +00:00
|
|
|
std::vector<char const *> enabledLayers = vk::su::gatherLayers( layers
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
,
|
|
|
|
vk::enumerateInstanceLayerProperties()
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
std::vector<char const *> enabledExtensions = vk::su::gatherExtensions( extensions
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
,
|
|
|
|
vk::enumerateInstanceExtensionProperties()
|
|
|
|
#endif
|
|
|
|
);
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
vk::Instance instance =
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::createInstance( makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions ).get<vk::InstanceCreateInfo>() );
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
|
2019-09-26 07:55:15 +00:00
|
|
|
// initialize function pointers for instance
|
2021-02-17 09:49:59 +00:00
|
|
|
VULKAN_HPP_DEFAULT_DISPATCHER.init( instance );
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::RenderPass createRenderPass(
|
|
|
|
vk::Device const & device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp, vk::ImageLayout colorFinalLayout )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2019-04-15 08:18:58 +00:00
|
|
|
std::vector<vk::AttachmentDescription> attachmentDescriptions;
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( colorFormat != vk::Format::eUndefined );
|
2021-02-17 09:49:59 +00:00
|
|
|
attachmentDescriptions.emplace_back( vk::AttachmentDescriptionFlags(),
|
|
|
|
colorFormat,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
loadOp,
|
|
|
|
vk::AttachmentStoreOp::eStore,
|
|
|
|
vk::AttachmentLoadOp::eDontCare,
|
|
|
|
vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
colorFinalLayout );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( depthFormat != vk::Format::eUndefined )
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
attachmentDescriptions.emplace_back( vk::AttachmentDescriptionFlags(),
|
|
|
|
depthFormat,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
loadOp,
|
|
|
|
vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::AttachmentLoadOp::eDontCare,
|
|
|
|
vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
vk::ImageLayout::eDepthStencilAttachmentOptimal );
|
2020-04-12 19:49:12 +00:00
|
|
|
}
|
|
|
|
vk::AttachmentReference colorAttachment( 0, vk::ImageLayout::eColorAttachmentOptimal );
|
|
|
|
vk::AttachmentReference depthAttachment( 1, vk::ImageLayout::eDepthStencilAttachmentOptimal );
|
|
|
|
vk::SubpassDescription subpassDescription( vk::SubpassDescriptionFlags(),
|
|
|
|
vk::PipelineBindPoint::eGraphics,
|
2023-07-13 09:26:01 +00:00
|
|
|
{},
|
2020-07-08 08:58:37 +00:00
|
|
|
colorAttachment,
|
2023-07-13 09:26:01 +00:00
|
|
|
{},
|
2022-02-28 09:11:04 +00:00
|
|
|
( depthFormat != vk::Format::eUndefined ) ? &depthAttachment : nullptr );
|
|
|
|
return device.createRenderPass( vk::RenderPassCreateInfo( vk::RenderPassCreateFlags(), attachmentDescriptions, subpassDescription ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
VKAPI_ATTR VkBool32 VKAPI_CALL debugUtilsMessengerCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
|
|
|
|
VkDebugUtilsMessengerCallbackDataEXT const * pCallbackData,
|
|
|
|
void * /*pUserData*/ )
|
2020-04-12 19:49:12 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
#if !defined( NDEBUG )
|
2024-01-16 12:48:53 +00:00
|
|
|
if ( static_cast<uint32_t>( pCallbackData->messageIdNumber ) == 0x822806fa )
|
2021-01-25 12:34:30 +00:00
|
|
|
{
|
2023-08-23 11:58:54 +00:00
|
|
|
// Validation Warning: vkCreateInstance(): to enable extension VK_EXT_debug_utils, but this extension is intended to support use by applications when
|
|
|
|
// debugging and it is strongly recommended that it be otherwise avoided.
|
2023-07-19 13:33:22 +00:00
|
|
|
return vk::False;
|
2021-01-25 12:34:30 +00:00
|
|
|
}
|
2024-01-16 12:48:53 +00:00
|
|
|
else if ( static_cast<uint32_t>( pCallbackData->messageIdNumber ) == 0xe8d1a9fe )
|
2021-01-25 12:34:30 +00:00
|
|
|
{
|
2023-08-23 11:58:54 +00:00
|
|
|
// Validation Performance Warning: Using debug builds of the validation layers *will* adversely affect performance.
|
2023-07-19 13:33:22 +00:00
|
|
|
return vk::False;
|
2021-01-25 12:34:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::cerr << vk::to_string( static_cast<vk::DebugUtilsMessageSeverityFlagBitsEXT>( messageSeverity ) ) << ": "
|
|
|
|
<< vk::to_string( static_cast<vk::DebugUtilsMessageTypeFlagsEXT>( messageTypes ) ) << ":\n";
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t" ) << "messageIDName = <" << pCallbackData->pMessageIdName << ">\n";
|
|
|
|
std::cerr << std::string( "\t" ) << "messageIdNumber = " << pCallbackData->messageIdNumber << "\n";
|
|
|
|
std::cerr << std::string( "\t" ) << "message = <" << pCallbackData->pMessage << ">\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 0 < pCallbackData->queueLabelCount )
|
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t" ) << "Queue Labels:\n";
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->queueLabelCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t\t" ) << "labelName = <" << pCallbackData->pQueueLabels[i].pLabelName << ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 0 < pCallbackData->cmdBufLabelCount )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t" ) << "CommandBuffer Labels:\n";
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->cmdBufLabelCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t\t" ) << "labelName = <" << pCallbackData->pCmdBufLabels[i].pLabelName << ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 0 < pCallbackData->objectCount )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t" ) << "Objects:\n";
|
2022-02-09 07:32:42 +00:00
|
|
|
for ( uint32_t i = 0; i < pCallbackData->objectCount; i++ )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t\t" ) << "Object " << i << "\n";
|
|
|
|
std::cerr << std::string( "\t\t\t" ) << "objectType = " << vk::to_string( static_cast<vk::ObjectType>( pCallbackData->pObjects[i].objectType ) )
|
|
|
|
<< "\n";
|
|
|
|
std::cerr << std::string( "\t\t\t" ) << "objectHandle = " << pCallbackData->pObjects[i].objectHandle << "\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( pCallbackData->pObjects[i].pObjectName )
|
2019-09-25 09:56:46 +00:00
|
|
|
{
|
2022-07-25 15:11:30 +00:00
|
|
|
std::cerr << std::string( "\t\t\t" ) << "objectName = <" << pCallbackData->pObjects[i].pObjectName << ">\n";
|
2019-09-25 09:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
2023-07-19 13:33:22 +00:00
|
|
|
return vk::False;
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t findGraphicsQueueFamilyIndex( std::vector<vk::QueueFamilyProperties> const & queueFamilyProperties )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
|
|
|
// get the first index into queueFamiliyProperties which supports graphics
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<vk::QueueFamilyProperties>::const_iterator graphicsQueueFamilyProperty =
|
|
|
|
std::find_if( queueFamilyProperties.begin(),
|
|
|
|
queueFamilyProperties.end(),
|
|
|
|
[]( vk::QueueFamilyProperties const & qfp ) { return qfp.queueFlags & vk::QueueFlagBits::eGraphics; } );
|
2021-02-17 09:49:59 +00:00
|
|
|
assert( graphicsQueueFamilyProperty != queueFamilyProperties.end() );
|
|
|
|
return static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
std::pair<uint32_t, uint32_t> findGraphicsAndPresentQueueFamilyIndex( vk::PhysicalDevice physicalDevice, vk::SurfaceKHR const & surface )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( queueFamilyProperties.size() < std::numeric_limits<uint32_t>::max() );
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t graphicsQueueFamilyIndex = findGraphicsQueueFamilyIndex( queueFamilyProperties );
|
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( graphicsQueueFamilyIndex, surface ) )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
return std::make_pair( graphicsQueueFamilyIndex,
|
|
|
|
graphicsQueueFamilyIndex ); // the first graphicsQueueFamilyIndex does also support presents
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
// the graphicsQueueFamilyIndex doesn't support present -> look for an other family index that supports both
|
|
|
|
// graphics and present
|
|
|
|
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
|
|
|
|
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
return std::make_pair( static_cast<uint32_t>( i ), static_cast<uint32_t>( i ) );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
// there's nothing like a single family index that supports both graphics and present -> look for an other family
|
|
|
|
// index that supports present
|
|
|
|
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
return std::make_pair( graphicsQueueFamilyIndex, static_cast<uint32_t>( i ) );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
throw std::runtime_error( "Could not find queues for both graphics or present -> terminating" );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t findMemoryType( vk::PhysicalDeviceMemoryProperties const & memoryProperties, uint32_t typeBits, vk::MemoryPropertyFlags requirementsMask )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t typeIndex = uint32_t( ~0 );
|
|
|
|
for ( uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++ )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
if ( ( typeBits & 1 ) && ( ( memoryProperties.memoryTypes[i].propertyFlags & requirementsMask ) == requirementsMask ) )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
|
|
|
typeIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
typeBits >>= 1;
|
|
|
|
}
|
2021-01-25 12:34:30 +00:00
|
|
|
assert( typeIndex != uint32_t( ~0 ) );
|
2019-03-15 09:40:45 +00:00
|
|
|
return typeIndex;
|
|
|
|
}
|
|
|
|
|
2019-03-05 07:59:40 +00:00
|
|
|
std::vector<std::string> getDeviceExtensions()
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
return { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> getInstanceExtensions()
|
|
|
|
{
|
|
|
|
std::vector<std::string> extensions;
|
2020-04-12 19:49:12 +00:00
|
|
|
extensions.push_back( VK_KHR_SURFACE_EXTENSION_NAME );
|
|
|
|
#if defined( VK_USE_PLATFORM_ANDROID_KHR )
|
|
|
|
extensions.push_back( VK_KHR_ANDROID_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_IOS_MVK )
|
|
|
|
extensions.push_back( VK_MVK_IOS_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_MACOS_MVK )
|
|
|
|
extensions.push_back( VK_MVK_MACOS_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_MIR_KHR )
|
|
|
|
extensions.push_back( VK_KHR_MIR_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_VI_NN )
|
|
|
|
extensions.push_back( VK_NN_VI_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_WAYLAND_KHR )
|
|
|
|
extensions.push_back( VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_WIN32_KHR )
|
|
|
|
extensions.push_back( VK_KHR_WIN32_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_XCB_KHR )
|
|
|
|
extensions.push_back( VK_KHR_XCB_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_XLIB_KHR )
|
|
|
|
extensions.push_back( VK_KHR_XLIB_SURFACE_EXTENSION_NAME );
|
|
|
|
#elif defined( VK_USE_PLATFORM_XLIB_XRANDR_EXT )
|
|
|
|
extensions.push_back( VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME );
|
2019-03-05 07:59:40 +00:00
|
|
|
#endif
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Format pickDepthFormat( vk::PhysicalDevice const & physicalDevice )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<vk::Format> candidates = { vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint };
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( vk::Format format : candidates )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::FormatProperties props = physicalDevice.getFormatProperties( format );
|
2019-06-25 07:47:27 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
throw std::runtime_error( "failed to find supported format!" );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::PresentModeKHR pickPresentMode( std::vector<vk::PresentModeKHR> const & presentModes )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
2019-06-25 07:47:27 +00:00
|
|
|
vk::PresentModeKHR pickedMode = vk::PresentModeKHR::eFifo;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( const auto & presentMode : presentModes )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( presentMode == vk::PresentModeKHR::eMailbox )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
pickedMode = presentMode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( presentMode == vk::PresentModeKHR::eImmediate )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
pickedMode = presentMode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pickedMode;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SurfaceFormatKHR pickSurfaceFormat( std::vector<vk::SurfaceFormatKHR> const & formats )
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( !formats.empty() );
|
2019-05-21 13:44:52 +00:00
|
|
|
vk::SurfaceFormatKHR pickedFormat = formats[0];
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( formats.size() == 1 )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( formats[0].format == vk::Format::eUndefined )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
pickedFormat.format = vk::Format::eB8G8R8A8Unorm;
|
|
|
|
pickedFormat.colorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// request several formats, the first found will be used
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::Format requestedFormats[] = { vk::Format::eB8G8R8A8Unorm, vk::Format::eR8G8B8A8Unorm, vk::Format::eB8G8R8Unorm, vk::Format::eR8G8B8Unorm };
|
2019-05-21 13:44:52 +00:00
|
|
|
vk::ColorSpaceKHR requestedColorSpace = vk::ColorSpaceKHR::eSrgbNonlinear;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( size_t i = 0; i < sizeof( requestedFormats ) / sizeof( requestedFormats[0] ); i++ )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
vk::Format requestedFormat = requestedFormats[i];
|
2021-05-25 14:14:56 +00:00
|
|
|
auto it = std::find_if( formats.begin(),
|
|
|
|
formats.end(),
|
2022-02-28 09:11:04 +00:00
|
|
|
[requestedFormat, requestedColorSpace]( vk::SurfaceFormatKHR const & f )
|
|
|
|
{ return ( f.format == requestedFormat ) && ( f.colorSpace == requestedColorSpace ); } );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( it != formats.end() )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
pickedFormat = *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( pickedFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear );
|
2019-05-21 13:44:52 +00:00
|
|
|
return pickedFormat;
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
void setImageLayout(
|
|
|
|
vk::CommandBuffer const & commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
vk::AccessFlags sourceAccessMask;
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( oldImageLayout )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ImageLayout::eTransferDstOptimal: sourceAccessMask = vk::AccessFlagBits::eTransferWrite; break;
|
|
|
|
case vk::ImageLayout::ePreinitialized: sourceAccessMask = vk::AccessFlagBits::eHostWrite; break;
|
|
|
|
case vk::ImageLayout::eGeneral: // sourceAccessMask is empty
|
|
|
|
case vk::ImageLayout::eUndefined: break;
|
|
|
|
default: assert( false ); break;
|
2019-05-21 13:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::PipelineStageFlags sourceStage;
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( oldImageLayout )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
case vk::ImageLayout::eGeneral:
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ImageLayout::ePreinitialized: sourceStage = vk::PipelineStageFlagBits::eHost; break;
|
|
|
|
case vk::ImageLayout::eTransferDstOptimal: sourceStage = vk::PipelineStageFlagBits::eTransfer; break;
|
|
|
|
case vk::ImageLayout::eUndefined: sourceStage = vk::PipelineStageFlagBits::eTopOfPipe; break;
|
|
|
|
default: assert( false ); break;
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::AccessFlags destinationAccessMask;
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( newImageLayout )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::ImageLayout::eColorAttachmentOptimal: destinationAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; break;
|
2019-05-21 13:44:52 +00:00
|
|
|
case vk::ImageLayout::eDepthStencilAttachmentOptimal:
|
2022-02-28 09:11:04 +00:00
|
|
|
destinationAccessMask = vk::AccessFlagBits::eDepthStencilAttachmentRead | vk::AccessFlagBits::eDepthStencilAttachmentWrite;
|
2019-05-21 13:44:52 +00:00
|
|
|
break;
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ImageLayout::eGeneral: // empty destinationAccessMask
|
|
|
|
case vk::ImageLayout::ePresentSrcKHR: break;
|
|
|
|
case vk::ImageLayout::eShaderReadOnlyOptimal: destinationAccessMask = vk::AccessFlagBits::eShaderRead; break;
|
|
|
|
case vk::ImageLayout::eTransferSrcOptimal: destinationAccessMask = vk::AccessFlagBits::eTransferRead; break;
|
|
|
|
case vk::ImageLayout::eTransferDstOptimal: destinationAccessMask = vk::AccessFlagBits::eTransferWrite; break;
|
|
|
|
default: assert( false ); break;
|
2019-05-21 13:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::PipelineStageFlags destinationStage;
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( newImageLayout )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::ImageLayout::eColorAttachmentOptimal: destinationStage = vk::PipelineStageFlagBits::eColorAttachmentOutput; break;
|
|
|
|
case vk::ImageLayout::eDepthStencilAttachmentOptimal: destinationStage = vk::PipelineStageFlagBits::eEarlyFragmentTests; break;
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ImageLayout::eGeneral: destinationStage = vk::PipelineStageFlagBits::eHost; break;
|
|
|
|
case vk::ImageLayout::ePresentSrcKHR: destinationStage = vk::PipelineStageFlagBits::eBottomOfPipe; break;
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::ImageLayout::eShaderReadOnlyOptimal: destinationStage = vk::PipelineStageFlagBits::eFragmentShader; break;
|
2019-05-21 13:44:52 +00:00
|
|
|
case vk::ImageLayout::eTransferDstOptimal:
|
2020-04-12 19:49:12 +00:00
|
|
|
case vk::ImageLayout::eTransferSrcOptimal: destinationStage = vk::PipelineStageFlagBits::eTransfer; break;
|
|
|
|
default: assert( false ); break;
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 13:44:52 +00:00
|
|
|
vk::ImageAspectFlags aspectMask;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( newImageLayout == vk::ImageLayout::eDepthStencilAttachmentOptimal )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
aspectMask = vk::ImageAspectFlagBits::eDepth;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( format == vk::Format::eD32SfloatS8Uint || format == vk::Format::eD24UnormS8Uint )
|
2019-05-21 13:44:52 +00:00
|
|
|
{
|
|
|
|
aspectMask |= vk::ImageAspectFlagBits::eStencil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aspectMask = vk::ImageAspectFlagBits::eColor;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageSubresourceRange imageSubresourceRange( aspectMask, 0, 1, 0, 1 );
|
|
|
|
vk::ImageMemoryBarrier imageMemoryBarrier( sourceAccessMask,
|
|
|
|
destinationAccessMask,
|
|
|
|
oldImageLayout,
|
|
|
|
newImageLayout,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
image,
|
|
|
|
imageSubresourceRange );
|
2021-02-17 09:49:59 +00:00
|
|
|
return commandBuffer.pipelineBarrier( sourceStage, destinationStage, {}, nullptr, nullptr, imageMemoryBarrier );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
void submitAndWait( vk::Device const & device, vk::Queue const & queue, vk::CommandBuffer const & commandBuffer )
|
2019-03-15 09:40:45 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Fence fence = device.createFence( vk::FenceCreateInfo() );
|
|
|
|
queue.submit( vk::SubmitInfo( 0, nullptr, nullptr, 1, &commandBuffer ), fence );
|
|
|
|
while ( vk::Result::eTimeout == device.waitForFences( fence, VK_TRUE, vk::su::FenceTimeout ) )
|
2019-03-15 09:40:45 +00:00
|
|
|
;
|
2021-02-17 09:49:59 +00:00
|
|
|
device.destroyFence( fence );
|
2019-03-15 09:40:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 07:43:45 +00:00
|
|
|
void updateDescriptorSets( vk::Device const & device,
|
|
|
|
vk::DescriptorSet const & descriptorSet,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, vk::Buffer const &, vk::DeviceSize, vk::BufferView const &>> const & bufferData,
|
|
|
|
vk::su::TextureData const & textureData,
|
|
|
|
uint32_t bindingOffset )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2019-06-25 07:47:27 +00:00
|
|
|
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
2020-04-12 19:49:12 +00:00
|
|
|
bufferInfos.reserve( bufferData.size() );
|
2019-06-25 07:47:27 +00:00
|
|
|
|
2019-03-26 11:24:36 +00:00
|
|
|
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
2020-04-12 19:49:12 +00:00
|
|
|
writeDescriptorSets.reserve( bufferData.size() + 1 );
|
2019-07-25 11:52:09 +00:00
|
|
|
uint32_t dstBinding = bindingOffset;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( auto const & bd : bufferData )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2022-05-02 07:43:45 +00:00
|
|
|
bufferInfos.emplace_back( std::get<1>( bd ), 0, std::get<2>( bd ) );
|
|
|
|
writeDescriptorSets.emplace_back( descriptorSet, dstBinding++, 0, 1, std::get<0>( bd ), nullptr, &bufferInfos.back(), &std::get<3>( bd ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2019-06-25 07:47:27 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorImageInfo imageInfo( textureData.sampler, textureData.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal );
|
|
|
|
writeDescriptorSets.emplace_back( descriptorSet, dstBinding, 0, vk::DescriptorType::eCombinedImageSampler, imageInfo, nullptr, nullptr );
|
2019-06-25 07:47:27 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
device.updateDescriptorSets( writeDescriptorSets, nullptr );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 07:43:45 +00:00
|
|
|
void updateDescriptorSets( vk::Device const & device,
|
|
|
|
vk::DescriptorSet const & descriptorSet,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, vk::Buffer const &, vk::DeviceSize, vk::BufferView const &>> const & bufferData,
|
|
|
|
std::vector<vk::su::TextureData> const & textureData,
|
|
|
|
uint32_t bindingOffset )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
2020-04-12 19:49:12 +00:00
|
|
|
bufferInfos.reserve( bufferData.size() );
|
2019-06-25 07:47:27 +00:00
|
|
|
|
|
|
|
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
2020-04-12 19:49:12 +00:00
|
|
|
writeDescriptorSets.reserve( bufferData.size() + ( textureData.empty() ? 0 : 1 ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
uint32_t dstBinding = bindingOffset;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( auto const & bd : bufferData )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2022-05-02 07:43:45 +00:00
|
|
|
bufferInfos.emplace_back( std::get<1>( bd ), 0, std::get<2>( bd ) );
|
|
|
|
writeDescriptorSets.emplace_back( descriptorSet, dstBinding++, 0, 1, std::get<0>( bd ), nullptr, &bufferInfos.back(), &std::get<3>( bd ) );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<vk::DescriptorImageInfo> imageInfos;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( !textureData.empty() )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
imageInfos.reserve( textureData.size() );
|
|
|
|
for ( auto const & td : textureData )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
imageInfos.emplace_back( td.sampler, td.imageData->imageView, vk::ImageLayout::eShaderReadOnlyOptimal );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
writeDescriptorSets.emplace_back( descriptorSet,
|
|
|
|
dstBinding,
|
|
|
|
0,
|
|
|
|
checked_cast<uint32_t>( imageInfos.size() ),
|
|
|
|
vk::DescriptorType::eCombinedImageSampler,
|
|
|
|
imageInfos.data(),
|
|
|
|
nullptr,
|
|
|
|
nullptr );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
device.updateDescriptorSets( writeDescriptorSets, nullptr );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
BufferData::BufferData( vk::PhysicalDevice const & physicalDevice,
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Device const & device,
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::DeviceSize size,
|
|
|
|
vk::BufferUsageFlags usage,
|
|
|
|
vk::MemoryPropertyFlags propertyFlags )
|
|
|
|
#if !defined( NDEBUG )
|
|
|
|
: m_size( size ), m_usage( usage ), m_propertyFlags( propertyFlags )
|
2019-06-25 07:47:27 +00:00
|
|
|
#endif
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
buffer = device.createBuffer( vk::BufferCreateInfo( vk::BufferCreateFlags(), size, usage ) );
|
2022-02-28 09:11:04 +00:00
|
|
|
deviceMemory = vk::su::allocateDeviceMemory( device, physicalDevice.getMemoryProperties(), device.getBufferMemoryRequirements( buffer ), propertyFlags );
|
2021-02-17 09:49:59 +00:00
|
|
|
device.bindBufferMemory( buffer, deviceMemory, 0 );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
DepthBufferData::DepthBufferData( vk::PhysicalDevice const & physicalDevice, vk::Device const & device, vk::Format format, vk::Extent2D const & extent )
|
2020-04-12 19:49:12 +00:00
|
|
|
: ImageData( physicalDevice,
|
|
|
|
device,
|
|
|
|
format,
|
|
|
|
extent,
|
|
|
|
vk::ImageTiling::eOptimal,
|
|
|
|
vk::ImageUsageFlagBits::eDepthStencilAttachment,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
vk::MemoryPropertyFlagBits::eDeviceLocal,
|
|
|
|
vk::ImageAspectFlagBits::eDepth )
|
2022-03-23 16:08:19 +00:00
|
|
|
{
|
|
|
|
}
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
ImageData::ImageData( vk::PhysicalDevice const & physicalDevice,
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Device const & device,
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Format format_,
|
|
|
|
vk::Extent2D const & extent,
|
|
|
|
vk::ImageTiling tiling,
|
|
|
|
vk::ImageUsageFlags usage,
|
|
|
|
vk::ImageLayout initialLayout,
|
|
|
|
vk::MemoryPropertyFlags memoryProperties,
|
|
|
|
vk::ImageAspectFlags aspectMask )
|
|
|
|
: format( format_ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageCreateInfo imageCreateInfo( vk::ImageCreateFlags(),
|
|
|
|
vk::ImageType::e2D,
|
|
|
|
format,
|
|
|
|
vk::Extent3D( extent, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
tiling,
|
|
|
|
usage | vk::ImageUsageFlagBits::eSampled,
|
|
|
|
vk::SharingMode::eExclusive,
|
2020-07-08 08:58:37 +00:00
|
|
|
{},
|
2020-04-12 19:49:12 +00:00
|
|
|
initialLayout );
|
2021-02-17 09:49:59 +00:00
|
|
|
image = device.createImage( imageCreateInfo );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
deviceMemory = vk::su::allocateDeviceMemory( device, physicalDevice.getMemoryProperties(), device.getImageMemoryRequirements( image ), memoryProperties );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
device.bindImageMemory( image, deviceMemory, 0 );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageViewCreateInfo imageViewCreateInfo( {}, image, vk::ImageViewType::e2D, format, {}, { aspectMask, 0, 1, 0, 1 } );
|
2021-02-17 09:49:59 +00:00
|
|
|
imageView = device.createImageView( imageViewCreateInfo );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
SurfaceData::SurfaceData( vk::Instance const & instance, std::string const & windowName, vk::Extent2D const & extent_ )
|
2020-04-12 19:49:12 +00:00
|
|
|
: extent( extent_ ), window( vk::su::createWindow( windowName, extent ) )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-01-28 09:16:10 +00:00
|
|
|
VkSurfaceKHR _surface;
|
2022-02-28 09:11:04 +00:00
|
|
|
VkResult err = glfwCreateWindowSurface( static_cast<VkInstance>( instance ), window.handle, nullptr, &_surface );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( err != VK_SUCCESS )
|
|
|
|
throw std::runtime_error( "Failed to create window!" );
|
2021-02-17 09:49:59 +00:00
|
|
|
surface = vk::SurfaceKHR( _surface );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
SwapChainData::SwapChainData( vk::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::Device const & device,
|
|
|
|
vk::SurfaceKHR const & surface,
|
|
|
|
vk::Extent2D const & extent,
|
|
|
|
vk::ImageUsageFlags usage,
|
|
|
|
vk::SwapchainKHR const & oldSwapChain,
|
|
|
|
uint32_t graphicsQueueFamilyIndex,
|
|
|
|
uint32_t presentQueueFamilyIndex )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SurfaceFormatKHR surfaceFormat = vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( surface ) );
|
|
|
|
colorFormat = surfaceFormat.format;
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( surface );
|
2021-12-07 09:39:23 +00:00
|
|
|
vk::Extent2D swapchainExtent;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max() )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
|
|
|
// If the surface size is undefined, the size is set to the size of the images requested.
|
2022-02-28 09:11:04 +00:00
|
|
|
swapchainExtent.width = clamp( extent.width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width );
|
|
|
|
swapchainExtent.height = clamp( extent.height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the surface size is defined, the swap chain size must match
|
|
|
|
swapchainExtent = surfaceCapabilities.currentExtent;
|
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::SurfaceTransformFlagBitsKHR preTransform = ( surfaceCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity )
|
2022-03-23 16:08:19 +00:00
|
|
|
? vk::SurfaceTransformFlagBitsKHR::eIdentity
|
|
|
|
: surfaceCapabilities.currentTransform;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::CompositeAlphaFlagBitsKHR compositeAlpha =
|
|
|
|
( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied ) ? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied
|
|
|
|
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied ) ? vk::CompositeAlphaFlagBitsKHR::ePostMultiplied
|
|
|
|
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::eInherit ) ? vk::CompositeAlphaFlagBitsKHR::eInherit
|
|
|
|
: vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
|
|
|
vk::PresentModeKHR presentMode = vk::su::pickPresentMode( physicalDevice.getSurfacePresentModesKHR( surface ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::SwapchainCreateInfoKHR swapChainCreateInfo( {},
|
|
|
|
surface,
|
2024-04-11 06:44:50 +00:00
|
|
|
vk::su::clampSurfaceImageCount( 3u, surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount ),
|
2020-04-12 19:49:12 +00:00
|
|
|
colorFormat,
|
|
|
|
surfaceFormat.colorSpace,
|
|
|
|
swapchainExtent,
|
|
|
|
1,
|
|
|
|
usage,
|
|
|
|
vk::SharingMode::eExclusive,
|
2020-07-08 08:58:37 +00:00
|
|
|
{},
|
2020-04-12 19:49:12 +00:00
|
|
|
preTransform,
|
|
|
|
compositeAlpha,
|
|
|
|
presentMode,
|
|
|
|
true,
|
2021-02-17 09:49:59 +00:00
|
|
|
oldSwapChain );
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2019-06-25 07:47:27 +00:00
|
|
|
uint32_t queueFamilyIndices[2] = { graphicsQueueFamilyIndex, presentQueueFamilyIndex };
|
2020-04-12 19:49:12 +00:00
|
|
|
// If the graphics and present queues are from different queue families, we either have to explicitly transfer
|
|
|
|
// ownership of images between the queues, or we have to create the swapchain with imageSharingMode as
|
|
|
|
// vk::SharingMode::eConcurrent
|
|
|
|
swapChainCreateInfo.imageSharingMode = vk::SharingMode::eConcurrent;
|
2019-03-26 11:24:36 +00:00
|
|
|
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
2020-04-12 19:49:12 +00:00
|
|
|
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
swapChain = device.createSwapchainKHR( swapChainCreateInfo );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
images = device.getSwapchainImagesKHR( swapChain );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
imageViews.reserve( images.size() );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ImageViewCreateInfo imageViewCreateInfo( {}, {}, vk::ImageViewType::e2D, colorFormat, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } );
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( auto image : images )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2021-11-11 10:49:07 +00:00
|
|
|
imageViewCreateInfo.image = image;
|
2021-02-17 09:49:59 +00:00
|
|
|
imageViews.push_back( device.createImageView( imageViewCreateInfo ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
CheckerboardImageGenerator::CheckerboardImageGenerator( std::array<uint8_t, 3> const & rgb0, std::array<uint8_t, 3> const & rgb1 )
|
2020-04-12 19:49:12 +00:00
|
|
|
: m_rgb0( rgb0 ), m_rgb1( rgb1 )
|
2022-03-23 16:08:19 +00:00
|
|
|
{
|
|
|
|
}
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
void CheckerboardImageGenerator::operator()( void * data, vk::Extent2D & extent ) const
|
2019-04-15 08:18:58 +00:00
|
|
|
{
|
|
|
|
// Checkerboard of 16x16 pixel squares
|
2020-04-12 19:49:12 +00:00
|
|
|
uint8_t * pImageMemory = static_cast<uint8_t *>( data );
|
|
|
|
for ( uint32_t row = 0; row < extent.height; row++ )
|
2019-04-15 08:18:58 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( uint32_t col = 0; col < extent.width; col++ )
|
2019-04-15 08:18:58 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::array<uint8_t, 3> const & rgb = ( ( ( row & 0x10 ) == 0 ) ^ ( ( col & 0x10 ) == 0 ) ) ? m_rgb1 : m_rgb0;
|
|
|
|
pImageMemory[0] = rgb[0];
|
|
|
|
pImageMemory[1] = rgb[1];
|
|
|
|
pImageMemory[2] = rgb[2];
|
|
|
|
pImageMemory[3] = 255;
|
2019-04-15 08:18:58 +00:00
|
|
|
pImageMemory += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
MonochromeImageGenerator::MonochromeImageGenerator( std::array<unsigned char, 3> const & rgb ) : m_rgb( rgb ) {}
|
2019-05-09 13:25:40 +00:00
|
|
|
|
2020-10-20 07:55:19 +00:00
|
|
|
void MonochromeImageGenerator::operator()( void * data, vk::Extent2D const & extent ) const
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
|
|
|
// fill in with the monochrome color
|
2020-04-12 19:49:12 +00:00
|
|
|
unsigned char * pImageMemory = static_cast<unsigned char *>( data );
|
|
|
|
for ( uint32_t row = 0; row < extent.height; row++ )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( uint32_t col = 0; col < extent.width; col++ )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
2019-06-25 07:47:27 +00:00
|
|
|
pImageMemory[0] = m_rgb[0];
|
|
|
|
pImageMemory[1] = m_rgb[1];
|
|
|
|
pImageMemory[2] = m_rgb[2];
|
2019-05-09 13:25:40 +00:00
|
|
|
pImageMemory[3] = 255;
|
|
|
|
pImageMemory += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
PixelsImageGenerator::PixelsImageGenerator( vk::Extent2D const & extent, size_t channels, unsigned char const * pixels )
|
2020-04-12 19:49:12 +00:00
|
|
|
: m_extent( extent ), m_channels( channels ), m_pixels( pixels )
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( m_channels == 4 );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 07:55:19 +00:00
|
|
|
void PixelsImageGenerator::operator()( void * data, vk::Extent2D const & extent ) const
|
2019-06-25 07:47:27 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( extent == m_extent );
|
2020-10-20 07:55:19 +00:00
|
|
|
memcpy( data, m_pixels, extent.width * extent.height * m_channels );
|
2019-06-25 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
TextureData::TextureData( vk::PhysicalDevice const & physicalDevice,
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Device const & device,
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::Extent2D const & extent_,
|
|
|
|
vk::ImageUsageFlags usageFlags,
|
|
|
|
vk::FormatFeatureFlags formatFeatureFlags,
|
|
|
|
bool anisotropyEnable,
|
|
|
|
bool forceStaging )
|
|
|
|
: format( vk::Format::eR8G8B8A8Unorm ), extent( extent_ )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2021-01-25 12:34:30 +00:00
|
|
|
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format );
|
2019-03-26 11:24:36 +00:00
|
|
|
|
2019-04-15 08:18:58 +00:00
|
|
|
formatFeatureFlags |= vk::FormatFeatureFlagBits::eSampledImage;
|
2022-02-28 09:11:04 +00:00
|
|
|
needsStaging = forceStaging || ( ( formatProperties.linearTilingFeatures & formatFeatureFlags ) != formatFeatureFlags );
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::ImageTiling imageTiling;
|
|
|
|
vk::ImageLayout initialLayout;
|
2019-04-15 08:18:58 +00:00
|
|
|
vk::MemoryPropertyFlags requirements;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( needsStaging )
|
2019-03-26 11:24:36 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( ( formatProperties.optimalTilingFeatures & formatFeatureFlags ) == formatFeatureFlags );
|
2022-02-28 09:11:04 +00:00
|
|
|
stagingBufferData =
|
|
|
|
std::unique_ptr<BufferData>( new BufferData( physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc ) );
|
|
|
|
imageTiling = vk::ImageTiling::eOptimal;
|
2019-03-26 11:24:36 +00:00
|
|
|
usageFlags |= vk::ImageUsageFlagBits::eTransferDst;
|
|
|
|
initialLayout = vk::ImageLayout::eUndefined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
imageTiling = vk::ImageTiling::eLinear;
|
2019-03-26 11:24:36 +00:00
|
|
|
initialLayout = vk::ImageLayout::ePreinitialized;
|
2020-04-12 19:49:12 +00:00
|
|
|
requirements = vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible;
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
imageData = std::unique_ptr<ImageData>( new ImageData( physicalDevice,
|
|
|
|
device,
|
|
|
|
format,
|
|
|
|
extent,
|
|
|
|
imageTiling,
|
|
|
|
usageFlags | vk::ImageUsageFlagBits::eSampled,
|
|
|
|
initialLayout,
|
|
|
|
requirements,
|
|
|
|
vk::ImageAspectFlagBits::eColor ) );
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
sampler = device.createSampler( vk::SamplerCreateInfo( vk::SamplerCreateFlags(),
|
|
|
|
vk::Filter::eLinear,
|
|
|
|
vk::Filter::eLinear,
|
|
|
|
vk::SamplerMipmapMode::eLinear,
|
|
|
|
vk::SamplerAddressMode::eRepeat,
|
|
|
|
vk::SamplerAddressMode::eRepeat,
|
|
|
|
vk::SamplerAddressMode::eRepeat,
|
|
|
|
0.0f,
|
|
|
|
anisotropyEnable,
|
|
|
|
16.0f,
|
|
|
|
false,
|
|
|
|
vk::CompareOp::eNever,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
vk::BorderColor::eFloatOpaqueBlack ) );
|
2019-03-26 11:24:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
UUID::UUID( uint8_t const data[VK_UUID_SIZE] )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
memcpy( m_data, data, VK_UUID_SIZE * sizeof( uint8_t ) );
|
2019-05-09 13:25:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
WindowData::WindowData( GLFWwindow * wnd, std::string const & name, vk::Extent2D const & extent ) : handle{ wnd }, name{ name }, extent{ extent } {}
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
WindowData::WindowData( WindowData && other ) : handle{}, name{}, extent{}
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::swap( handle, other.handle );
|
|
|
|
std::swap( name, other.name );
|
|
|
|
std::swap( extent, other.extent );
|
2020-01-28 09:16:10 +00:00
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-01-28 09:16:10 +00:00
|
|
|
WindowData::~WindowData() noexcept
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwDestroyWindow( handle );
|
2020-01-28 09:16:10 +00:00
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
WindowData createWindow( std::string const & windowName, vk::Extent2D const & extent )
|
2020-01-28 09:16:10 +00:00
|
|
|
{
|
|
|
|
struct glfwContext
|
2019-03-05 07:59:40 +00:00
|
|
|
{
|
2020-01-28 09:16:10 +00:00
|
|
|
glfwContext()
|
|
|
|
{
|
|
|
|
glfwInit();
|
2021-05-25 14:14:56 +00:00
|
|
|
glfwSetErrorCallback(
|
|
|
|
[]( int error, const char * msg )
|
|
|
|
{
|
|
|
|
std::cerr << "glfw: "
|
|
|
|
<< "(" << error << ") " << msg << std::endl;
|
|
|
|
} );
|
2020-01-28 09:16:10 +00:00
|
|
|
}
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-01-28 09:16:10 +00:00
|
|
|
~glfwContext()
|
|
|
|
{
|
|
|
|
glfwTerminate();
|
|
|
|
}
|
|
|
|
};
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-01-28 09:16:10 +00:00
|
|
|
static auto glfwCtx = glfwContext();
|
|
|
|
(void)glfwCtx;
|
2019-03-05 07:59:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwWindowHint( GLFW_CLIENT_API, GLFW_NO_API );
|
|
|
|
GLFWwindow * window = glfwCreateWindow( extent.width, extent.height, windowName.c_str(), nullptr, nullptr );
|
|
|
|
return WindowData( window, windowName, extent );
|
2019-03-05 07:59:40 +00:00
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
vk::DebugUtilsMessengerCreateInfoEXT makeDebugUtilsMessengerCreateInfoEXT()
|
|
|
|
{
|
|
|
|
return { {},
|
|
|
|
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError,
|
|
|
|
vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
|
|
|
|
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation,
|
|
|
|
&vk::su::debugUtilsMessengerCallback };
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined( NDEBUG )
|
|
|
|
vk::StructureChain<vk::InstanceCreateInfo>
|
|
|
|
#else
|
|
|
|
vk::StructureChain<vk::InstanceCreateInfo, vk::DebugUtilsMessengerCreateInfoEXT>
|
|
|
|
#endif
|
|
|
|
makeInstanceCreateInfoChain( vk::ApplicationInfo const & applicationInfo,
|
|
|
|
std::vector<char const *> const & layers,
|
|
|
|
std::vector<char const *> const & extensions )
|
|
|
|
{
|
|
|
|
#if defined( NDEBUG )
|
|
|
|
// in non-debug mode just use the InstanceCreateInfo for instance creation
|
2021-11-03 08:01:24 +00:00
|
|
|
vk::StructureChain<vk::InstanceCreateInfo> instanceCreateInfo( { {}, &applicationInfo, layers, extensions } );
|
2021-02-17 09:49:59 +00:00
|
|
|
#else
|
|
|
|
// in debug mode, addionally use the debugUtilsMessengerCallback in instance creation!
|
|
|
|
vk::DebugUtilsMessageSeverityFlagsEXT severityFlags( vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
|
|
|
|
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessageTypeFlagsEXT messageTypeFlags( vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation );
|
|
|
|
vk::StructureChain<vk::InstanceCreateInfo, vk::DebugUtilsMessengerCreateInfoEXT> instanceCreateInfo(
|
2022-02-28 09:11:04 +00:00
|
|
|
{ {}, &applicationInfo, layers, extensions }, { {}, severityFlags, messageTypeFlags, &vk::su::debugUtilsMessengerCallback } );
|
2021-02-17 09:49:59 +00:00
|
|
|
#endif
|
|
|
|
return instanceCreateInfo;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
} // namespace su
|
|
|
|
} // namespace vk
|
2019-05-09 13:25:40 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
std::ostream & operator<<( std::ostream & os, vk::su::UUID const & uuid )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
os << std::setfill( '0' ) << std::hex;
|
2021-04-15 08:49:54 +00:00
|
|
|
for ( uint32_t j = 0; j < VK_UUID_SIZE; ++j )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
os << std::setw( 2 ) << static_cast<uint32_t>( uuid.m_data[j] );
|
|
|
|
if ( j == 3 || j == 5 || j == 7 || j == 9 )
|
2019-05-09 13:25:40 +00:00
|
|
|
{
|
|
|
|
std::cout << '-';
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
os << std::setfill( ' ' ) << std::dec;
|
2019-05-09 13:25:40 +00:00
|
|
|
return os;
|
2019-11-26 10:28:57 +00:00
|
|
|
}
|