2021-02-17 09:49:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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
|
|
|
|
// unknown compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../../samples/utils/utils.hpp"
|
|
|
|
|
|
|
|
#include <numeric>
|
2022-09-21 07:28:38 +00:00
|
|
|
#include <vulkan/vulkan_raii.hpp>
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
namespace vk
|
|
|
|
{
|
|
|
|
namespace raii
|
|
|
|
{
|
|
|
|
namespace su
|
|
|
|
{
|
|
|
|
vk::raii::DeviceMemory allocateDeviceMemory( vk::raii::Device const & device,
|
|
|
|
vk::PhysicalDeviceMemoryProperties const & memoryProperties,
|
|
|
|
vk::MemoryRequirements const & memoryRequirements,
|
|
|
|
vk::MemoryPropertyFlags memoryPropertyFlags )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t memoryTypeIndex = vk::su::findMemoryType( memoryProperties, memoryRequirements.memoryTypeBits, memoryPropertyFlags );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
|
|
|
|
return vk::raii::DeviceMemory( device, memoryAllocateInfo );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2022-02-28 09:11:04 +00:00
|
|
|
void copyToDevice( vk::raii::DeviceMemory const & deviceMemory, T const * pData, size_t count, vk::DeviceSize stride = sizeof( T ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
assert( sizeof( T ) <= stride );
|
|
|
|
uint8_t * deviceData = static_cast<uint8_t *>( deviceMemory.mapMemory( 0, count * stride ) );
|
|
|
|
if ( stride == sizeof( T ) )
|
|
|
|
{
|
|
|
|
memcpy( deviceData, pData, count * sizeof( T ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
memcpy( deviceData, &pData[i], sizeof( T ) );
|
|
|
|
deviceData += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deviceMemory.unmapMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void copyToDevice( vk::raii::DeviceMemory const & deviceMemory, T const & data )
|
|
|
|
{
|
|
|
|
copyToDevice<T>( deviceMemory, &data, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class... Args>
|
|
|
|
std::unique_ptr<T> make_unique( Args &&... args )
|
|
|
|
{
|
|
|
|
#if ( 14 <= VULKAN_HPP_CPP_VERSION )
|
|
|
|
return std::make_unique<T>( std::forward<Args>( args )... );
|
|
|
|
#else
|
|
|
|
return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
2023-08-14 14:09:11 +00:00
|
|
|
void oneTimeSubmit( vk::raii::Device const & device, vk::raii::CommandPool const & commandPool, vk::raii::Queue const & queue, Func const & func )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2023-08-14 14:09:11 +00:00
|
|
|
vk::raii::CommandBuffer commandBuffer = std::move( vk::raii::CommandBuffers( device, { *commandPool, vk::CommandBufferLevel::ePrimary, 1 } ).front() );
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit ) );
|
|
|
|
func( commandBuffer );
|
|
|
|
commandBuffer.end();
|
|
|
|
vk::SubmitInfo submitInfo( nullptr, nullptr, *commandBuffer );
|
|
|
|
queue.submit( submitInfo, nullptr );
|
|
|
|
queue.waitIdle();
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
void setImageLayout(
|
|
|
|
vk::raii::CommandBuffer const & commandBuffer, vk::Image image, vk::Format format, vk::ImageLayout oldImageLayout, vk::ImageLayout newImageLayout )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
vk::AccessFlags sourceAccessMask;
|
|
|
|
switch ( oldImageLayout )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::PipelineStageFlags sourceStage;
|
|
|
|
switch ( oldImageLayout )
|
|
|
|
{
|
|
|
|
case vk::ImageLayout::eGeneral:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::AccessFlags destinationAccessMask;
|
|
|
|
switch ( newImageLayout )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::ImageLayout::eColorAttachmentOptimal: destinationAccessMask = vk::AccessFlagBits::eColorAttachmentWrite; break;
|
2021-02-17 09:49:59 +00:00
|
|
|
case vk::ImageLayout::eDepthStencilAttachmentOptimal:
|
2022-02-28 09:11:04 +00:00
|
|
|
destinationAccessMask = vk::AccessFlagBits::eDepthStencilAttachmentRead | vk::AccessFlagBits::eDepthStencilAttachmentWrite;
|
2021-02-17 09:49:59 +00:00
|
|
|
break;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::PipelineStageFlags destinationStage;
|
|
|
|
switch ( newImageLayout )
|
|
|
|
{
|
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;
|
2021-02-17 09:49:59 +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;
|
2021-02-17 09:49:59 +00:00
|
|
|
case vk::ImageLayout::eTransferDstOptimal:
|
|
|
|
case vk::ImageLayout::eTransferSrcOptimal: destinationStage = vk::PipelineStageFlagBits::eTransfer; break;
|
|
|
|
default: assert( false ); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::ImageAspectFlags aspectMask;
|
|
|
|
if ( newImageLayout == vk::ImageLayout::eDepthStencilAttachmentOptimal )
|
|
|
|
{
|
|
|
|
aspectMask = vk::ImageAspectFlagBits::eDepth;
|
|
|
|
if ( format == vk::Format::eD32SfloatS8Uint || format == vk::Format::eD24UnormS8Uint )
|
|
|
|
{
|
|
|
|
aspectMask |= vk::ImageAspectFlagBits::eStencil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aspectMask = vk::ImageAspectFlagBits::eColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
return commandBuffer.pipelineBarrier( sourceStage, destinationStage, {}, nullptr, nullptr, imageMemoryBarrier );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BufferData
|
|
|
|
{
|
|
|
|
BufferData( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::DeviceSize size,
|
|
|
|
vk::BufferUsageFlags usage,
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::MemoryPropertyFlags propertyFlags = vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent )
|
2021-06-21 09:32:43 +00:00
|
|
|
: buffer( device, vk::BufferCreateInfo( {}, size, usage ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-06-21 09:32:43 +00:00
|
|
|
, m_size( size )
|
|
|
|
, m_usage( usage )
|
|
|
|
, m_propertyFlags( propertyFlags )
|
2021-02-17 09:49:59 +00:00
|
|
|
#endif
|
|
|
|
{
|
2023-08-08 11:40:56 +00:00
|
|
|
deviceMemory = vk::raii::su::allocateDeviceMemory( device, physicalDevice.getMemoryProperties(), buffer.getMemoryRequirements(), propertyFlags );
|
2024-01-16 12:48:53 +00:00
|
|
|
buffer.bindMemory( deviceMemory, 0 );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
BufferData( std::nullptr_t ) {}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
template <typename DataType>
|
|
|
|
void upload( DataType const & data ) const
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
assert( ( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostCoherent ) && ( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
assert( sizeof( DataType ) <= m_size );
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
void * dataPtr = deviceMemory.mapMemory( 0, sizeof( DataType ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
memcpy( dataPtr, &data, sizeof( DataType ) );
|
2021-06-21 09:32:43 +00:00
|
|
|
deviceMemory.unmapMemory();
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
void upload( std::vector<DataType> const & data, size_t stride = 0 ) const
|
|
|
|
{
|
|
|
|
assert( m_propertyFlags & vk::MemoryPropertyFlagBits::eHostVisible );
|
|
|
|
|
|
|
|
size_t elementSize = stride ? stride : sizeof( DataType );
|
|
|
|
assert( sizeof( DataType ) <= elementSize );
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
copyToDevice( deviceMemory, data.data(), data.size(), elementSize );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
void upload( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::raii::CommandPool const & commandPool,
|
|
|
|
vk::raii::Queue const & queue,
|
|
|
|
std::vector<DataType> const & data,
|
|
|
|
size_t stride ) const
|
|
|
|
{
|
|
|
|
assert( m_usage & vk::BufferUsageFlagBits::eTransferDst );
|
|
|
|
assert( m_propertyFlags & vk::MemoryPropertyFlagBits::eDeviceLocal );
|
|
|
|
|
|
|
|
size_t elementSize = stride ? stride : sizeof( DataType );
|
|
|
|
assert( sizeof( DataType ) <= elementSize );
|
|
|
|
|
|
|
|
size_t dataSize = data.size() * elementSize;
|
|
|
|
assert( dataSize <= m_size );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::su::BufferData stagingBuffer( physicalDevice, device, dataSize, vk::BufferUsageFlagBits::eTransferSrc );
|
2021-06-21 09:32:43 +00:00
|
|
|
copyToDevice( stagingBuffer.deviceMemory, data.data(), data.size(), elementSize );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::su::oneTimeSubmit( device,
|
|
|
|
commandPool,
|
|
|
|
queue,
|
|
|
|
[&]( vk::raii::CommandBuffer const & commandBuffer )
|
|
|
|
{ commandBuffer.copyBuffer( *stagingBuffer.buffer, *this->buffer, vk::BufferCopy( 0, 0, dataSize ) ); } );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-08 11:40:56 +00:00
|
|
|
// the DeviceMemory should be destroyed before the Buffer it is bound to; to get that order with the standard destructor
|
|
|
|
// of the BufferData, the order of DeviceMemory and Buffer here matters
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::raii::DeviceMemory deviceMemory = nullptr;
|
2023-08-08 11:40:56 +00:00
|
|
|
vk::raii::Buffer buffer = nullptr;
|
2021-02-17 09:49:59 +00:00
|
|
|
#if !defined( NDEBUG )
|
|
|
|
private:
|
|
|
|
vk::DeviceSize m_size;
|
|
|
|
vk::BufferUsageFlags m_usage;
|
|
|
|
vk::MemoryPropertyFlags m_propertyFlags;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ImageData
|
|
|
|
{
|
|
|
|
ImageData( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::Format format_,
|
|
|
|
vk::Extent2D const & extent,
|
|
|
|
vk::ImageTiling tiling,
|
|
|
|
vk::ImageUsageFlags usage,
|
|
|
|
vk::ImageLayout initialLayout,
|
|
|
|
vk::MemoryPropertyFlags memoryProperties,
|
|
|
|
vk::ImageAspectFlags aspectMask )
|
|
|
|
: format( format_ )
|
2021-06-21 09:32:43 +00:00
|
|
|
, image( device,
|
|
|
|
{ vk::ImageCreateFlags(),
|
|
|
|
vk::ImageType::e2D,
|
|
|
|
format,
|
|
|
|
vk::Extent3D( extent, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
tiling,
|
|
|
|
usage | vk::ImageUsageFlagBits::eSampled,
|
|
|
|
vk::SharingMode::eExclusive,
|
|
|
|
{},
|
|
|
|
initialLayout } )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2023-08-08 11:40:56 +00:00
|
|
|
deviceMemory = vk::raii::su::allocateDeviceMemory( device, physicalDevice.getMemoryProperties(), image.getMemoryRequirements(), memoryProperties );
|
2024-01-16 12:48:53 +00:00
|
|
|
image.bindMemory( deviceMemory, 0 );
|
|
|
|
imageView = vk::raii::ImageView( device, vk::ImageViewCreateInfo( {}, image, vk::ImageViewType::e2D, format, {}, { aspectMask, 0, 1, 0, 1 } ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
ImageData( std::nullptr_t ) {}
|
|
|
|
|
2023-08-08 11:40:56 +00:00
|
|
|
// the DeviceMemory should be destroyed before the Image it is bound to; to get that order with the standard destructor
|
|
|
|
// of the ImageData, the order of DeviceMemory and Image here matters
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::Format format;
|
|
|
|
vk::raii::DeviceMemory deviceMemory = nullptr;
|
2023-08-08 11:40:56 +00:00
|
|
|
vk::raii::Image image = nullptr;
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::raii::ImageView imageView = nullptr;
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DepthBufferData : public ImageData
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
DepthBufferData( vk::raii::PhysicalDevice const & physicalDevice, vk::raii::Device const & device, vk::Format format, vk::Extent2D const & extent )
|
2021-02-17 09:49:59 +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
|
|
|
{
|
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SurfaceData
|
|
|
|
{
|
|
|
|
SurfaceData( vk::raii::Instance const & instance, std::string const & windowName, vk::Extent2D const & extent_ )
|
|
|
|
: extent( extent_ ), window( vk::su::createWindow( windowName, extent ) )
|
|
|
|
{
|
|
|
|
VkSurfaceKHR _surface;
|
2022-02-28 09:11:04 +00:00
|
|
|
VkResult err = glfwCreateWindowSurface( static_cast<VkInstance>( *instance ), window.handle, nullptr, &_surface );
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( err != VK_SUCCESS )
|
|
|
|
throw std::runtime_error( "Failed to create window!" );
|
2021-10-11 15:49:13 +00:00
|
|
|
surface = vk::raii::SurfaceKHR( instance, _surface );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::Extent2D extent;
|
|
|
|
vk::su::WindowData window;
|
|
|
|
vk::raii::SurfaceKHR surface = nullptr;
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SwapChainData
|
|
|
|
{
|
2021-06-21 09:32:43 +00:00
|
|
|
SwapChainData( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::raii::SurfaceKHR const & surface,
|
|
|
|
vk::Extent2D const & extent,
|
|
|
|
vk::ImageUsageFlags usage,
|
|
|
|
vk::raii::SwapchainKHR const * pOldSwapchain,
|
|
|
|
uint32_t graphicsQueueFamilyIndex,
|
|
|
|
uint32_t presentQueueFamilyIndex )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::SurfaceFormatKHR surfaceFormat = vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( surface ) );
|
2022-02-28 09:11:04 +00:00
|
|
|
colorFormat = surfaceFormat.format;
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::SurfaceCapabilitiesKHR surfaceCapabilities = physicalDevice.getSurfaceCapabilitiesKHR( surface );
|
2021-12-07 09:39:23 +00:00
|
|
|
vk::Extent2D swapchainExtent;
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( surfaceCapabilities.currentExtent.width == std::numeric_limits<uint32_t>::max() )
|
|
|
|
{
|
|
|
|
// 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 = vk::su::clamp( extent.width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width );
|
|
|
|
swapchainExtent.height = vk::su::clamp( extent.height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height );
|
2021-02-17 09:49:59 +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 =
|
2023-08-08 11:40:56 +00:00
|
|
|
( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePreMultiplied ) ? vk::CompositeAlphaFlagBitsKHR::ePreMultiplied
|
2022-02-28 09:11:04 +00:00
|
|
|
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::ePostMultiplied ) ? vk::CompositeAlphaFlagBitsKHR::ePostMultiplied
|
2023-08-08 11:40:56 +00:00
|
|
|
: ( surfaceCapabilities.supportedCompositeAlpha & vk::CompositeAlphaFlagBitsKHR::eInherit ) ? vk::CompositeAlphaFlagBitsKHR::eInherit
|
|
|
|
: vk::CompositeAlphaFlagBitsKHR::eOpaque;
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::PresentModeKHR presentMode = vk::su::pickPresentMode( physicalDevice.getSurfacePresentModesKHR( surface ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::SwapchainCreateInfoKHR swapChainCreateInfo( {},
|
2024-01-16 12:48:53 +00:00
|
|
|
surface,
|
2023-08-07 10:00:11 +00:00
|
|
|
vk::su::clamp( 3u, surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount ),
|
2021-02-17 09:49:59 +00:00
|
|
|
colorFormat,
|
|
|
|
surfaceFormat.colorSpace,
|
|
|
|
swapchainExtent,
|
|
|
|
1,
|
|
|
|
usage,
|
|
|
|
vk::SharingMode::eExclusive,
|
|
|
|
{},
|
|
|
|
preTransform,
|
|
|
|
compositeAlpha,
|
|
|
|
presentMode,
|
|
|
|
true,
|
2021-06-21 09:32:43 +00:00
|
|
|
pOldSwapchain ? **pOldSwapchain : nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
if ( graphicsQueueFamilyIndex != presentQueueFamilyIndex )
|
|
|
|
{
|
|
|
|
uint32_t queueFamilyIndices[2] = { graphicsQueueFamilyIndex, presentQueueFamilyIndex };
|
|
|
|
// 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;
|
|
|
|
swapChainCreateInfo.queueFamilyIndexCount = 2;
|
|
|
|
swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices;
|
|
|
|
}
|
2021-10-11 15:49:13 +00:00
|
|
|
swapChain = vk::raii::SwapchainKHR( device, swapChainCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
images = swapChain.getImages();
|
2021-02-17 09:49:59 +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 } );
|
2021-02-17 09:49:59 +00:00
|
|
|
for ( auto image : images )
|
|
|
|
{
|
2022-09-21 07:28:38 +00:00
|
|
|
imageViewCreateInfo.image = image;
|
2021-02-17 09:49:59 +00:00
|
|
|
imageViews.emplace_back( device, imageViewCreateInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::Format colorFormat;
|
|
|
|
vk::raii::SwapchainKHR swapChain = nullptr;
|
2022-09-21 07:28:38 +00:00
|
|
|
std::vector<vk::Image> images;
|
2021-10-11 15:49:13 +00:00
|
|
|
std::vector<vk::raii::ImageView> imageViews;
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TextureData
|
|
|
|
{
|
|
|
|
TextureData( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::Extent2D const & extent_ = { 256, 256 },
|
|
|
|
vk::ImageUsageFlags usageFlags = {},
|
|
|
|
vk::FormatFeatureFlags formatFeatureFlags = {},
|
|
|
|
bool anisotropyEnable = false,
|
|
|
|
bool forceStaging = false )
|
2021-06-21 09:32:43 +00:00
|
|
|
: format( vk::Format::eR8G8B8A8Unorm )
|
|
|
|
, extent( extent_ )
|
|
|
|
, sampler( device,
|
|
|
|
{ {},
|
|
|
|
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 } )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
vk::FormatProperties formatProperties = physicalDevice.getFormatProperties( format );
|
|
|
|
|
|
|
|
formatFeatureFlags |= vk::FormatFeatureFlagBits::eSampledImage;
|
2022-02-28 09:11:04 +00:00
|
|
|
needsStaging = forceStaging || ( ( formatProperties.linearTilingFeatures & formatFeatureFlags ) != formatFeatureFlags );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::ImageTiling imageTiling;
|
|
|
|
vk::ImageLayout initialLayout;
|
|
|
|
vk::MemoryPropertyFlags requirements;
|
|
|
|
if ( needsStaging )
|
|
|
|
{
|
|
|
|
assert( ( formatProperties.optimalTilingFeatures & formatFeatureFlags ) == formatFeatureFlags );
|
2022-02-28 09:11:04 +00:00
|
|
|
stagingBufferData = BufferData( physicalDevice, device, extent.width * extent.height * 4, vk::BufferUsageFlagBits::eTransferSrc );
|
|
|
|
imageTiling = vk::ImageTiling::eOptimal;
|
2021-02-17 09:49:59 +00:00
|
|
|
usageFlags |= vk::ImageUsageFlagBits::eTransferDst;
|
|
|
|
initialLayout = vk::ImageLayout::eUndefined;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
imageTiling = vk::ImageTiling::eLinear;
|
|
|
|
initialLayout = vk::ImageLayout::ePreinitialized;
|
|
|
|
requirements = vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible;
|
|
|
|
}
|
2021-10-11 15:49:13 +00:00
|
|
|
imageData = ImageData( physicalDevice,
|
|
|
|
device,
|
|
|
|
format,
|
|
|
|
extent,
|
|
|
|
imageTiling,
|
|
|
|
usageFlags | vk::ImageUsageFlagBits::eSampled,
|
|
|
|
initialLayout,
|
|
|
|
requirements,
|
|
|
|
vk::ImageAspectFlagBits::eColor );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ImageGenerator>
|
|
|
|
void setImage( vk::raii::CommandBuffer const & commandBuffer, ImageGenerator const & imageGenerator )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
void * data = needsStaging ? stagingBufferData.deviceMemory.mapMemory( 0, stagingBufferData.buffer.getMemoryRequirements().size )
|
|
|
|
: imageData.deviceMemory.mapMemory( 0, imageData.image.getMemoryRequirements().size );
|
2021-02-17 09:49:59 +00:00
|
|
|
imageGenerator( data, extent );
|
2021-10-11 15:49:13 +00:00
|
|
|
needsStaging ? stagingBufferData.deviceMemory.unmapMemory() : imageData.deviceMemory.unmapMemory();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
if ( needsStaging )
|
|
|
|
{
|
|
|
|
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::raii::su::setImageLayout( commandBuffer, imageData.image, imageData.format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::BufferImageCopy copyRegion( 0,
|
|
|
|
extent.width,
|
|
|
|
extent.height,
|
|
|
|
vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ),
|
|
|
|
vk::Offset3D( 0, 0, 0 ),
|
|
|
|
vk::Extent3D( extent, 1 ) );
|
2024-01-16 12:48:53 +00:00
|
|
|
commandBuffer.copyBufferToImage( stagingBufferData.buffer, imageData.image, vk::ImageLayout::eTransferDstOptimal, copyRegion );
|
2021-10-11 15:49:13 +00:00
|
|
|
// Set the layout for the texture image from eTransferDstOptimal to eShaderReadOnlyOptimal
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::su::setImageLayout(
|
2024-01-16 12:48:53 +00:00
|
|
|
commandBuffer, imageData.image, imageData.format, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we can use the linear tiled image as a texture, just do it
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::su::setImageLayout(
|
2024-01-16 12:48:53 +00:00
|
|
|
commandBuffer, imageData.image, imageData.format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:49:13 +00:00
|
|
|
vk::Format format;
|
|
|
|
vk::Extent2D extent;
|
|
|
|
bool needsStaging;
|
|
|
|
BufferData stagingBufferData = nullptr;
|
|
|
|
ImageData imageData = nullptr;
|
|
|
|
vk::raii::Sampler sampler;
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
std::pair<uint32_t, uint32_t> findGraphicsAndPresentQueueFamilyIndex( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::raii::SurfaceKHR const & surface )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
|
|
|
|
assert( queueFamilyProperties.size() < std::numeric_limits<uint32_t>::max() );
|
|
|
|
|
|
|
|
uint32_t graphicsQueueFamilyIndex = vk::su::findGraphicsQueueFamilyIndex( queueFamilyProperties );
|
2024-01-16 12:48:53 +00:00
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( graphicsQueueFamilyIndex, surface ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
return std::make_pair( graphicsQueueFamilyIndex,
|
|
|
|
graphicsQueueFamilyIndex ); // the first graphicsQueueFamilyIndex does also support presents
|
2021-02-17 09:49:59 +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++ )
|
|
|
|
{
|
|
|
|
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
|
2024-01-16 12:48:53 +00:00
|
|
|
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
return std::make_pair( static_cast<uint32_t>( i ), static_cast<uint32_t>( i ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++ )
|
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), surface ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
return std::make_pair( graphicsQueueFamilyIndex, static_cast<uint32_t>( i ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error( "Could not find queues for both graphics or present -> terminating" );
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::CommandBuffer makeCommandBuffer( vk::raii::Device const & device, vk::raii::CommandPool const & commandPool )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::CommandBufferAllocateInfo commandBufferAllocateInfo( commandPool, vk::CommandBufferLevel::ePrimary, 1 );
|
2021-06-21 09:32:43 +00:00
|
|
|
return std::move( vk::raii::CommandBuffers( device, commandBufferAllocateInfo ).front() );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 12:28:39 +00:00
|
|
|
void fullPipelineBarrier( vk::raii::CommandBuffer const & commandBuffer )
|
|
|
|
{
|
|
|
|
vk::MemoryBarrier memoryBarrier( vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite,
|
|
|
|
vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite );
|
|
|
|
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, memoryBarrier, nullptr, nullptr );
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::DescriptorPool makeDescriptorPool( vk::raii::Device const & device, std::vector<vk::DescriptorPoolSize> const & poolSizes )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
assert( !poolSizes.empty() );
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t maxSets = std::accumulate(
|
|
|
|
poolSizes.begin(), poolSizes.end(), 0, []( uint32_t sum, vk::DescriptorPoolSize const & dps ) { return sum + dps.descriptorCount; } );
|
2021-02-17 09:49:59 +00:00
|
|
|
assert( 0 < maxSets );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo( vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, maxSets, poolSizes );
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::DescriptorPool( device, descriptorPoolCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::DescriptorSetLayout makeDescriptorSetLayout( vk::raii::Device const & device,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, uint32_t, vk::ShaderStageFlags>> const & bindingData,
|
|
|
|
vk::DescriptorSetLayoutCreateFlags flags = {} )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::DescriptorSetLayoutBinding> bindings( bindingData.size() );
|
|
|
|
for ( size_t i = 0; i < bindingData.size(); i++ )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
bindings[i] = vk::DescriptorSetLayoutBinding(
|
|
|
|
vk::su::checked_cast<uint32_t>( i ), std::get<0>( bindingData[i] ), std::get<1>( bindingData[i] ), std::get<2>( bindingData[i] ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo( flags, bindings );
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::DescriptorSetLayout( device, descriptorSetLayoutCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Device makeDevice( vk::raii::PhysicalDevice const & physicalDevice,
|
|
|
|
uint32_t queueFamilyIndex,
|
|
|
|
std::vector<std::string> const & extensions = {},
|
|
|
|
vk::PhysicalDeviceFeatures const * physicalDeviceFeatures = nullptr,
|
|
|
|
void const * pNext = nullptr )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<char const *> enabledExtensions;
|
|
|
|
enabledExtensions.reserve( extensions.size() );
|
|
|
|
for ( auto const & ext : extensions )
|
|
|
|
{
|
|
|
|
enabledExtensions.push_back( ext.data() );
|
|
|
|
}
|
|
|
|
|
|
|
|
float queuePriority = 0.0f;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DeviceQueueCreateInfo deviceQueueCreateInfo( vk::DeviceQueueCreateFlags(), queueFamilyIndex, 1, &queuePriority );
|
2022-03-23 16:08:19 +00:00
|
|
|
vk::DeviceCreateInfo deviceCreateInfo( vk::DeviceCreateFlags(), deviceQueueCreateInfo, {}, enabledExtensions, physicalDeviceFeatures, pNext );
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::Device( physicalDevice, deviceCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
std::vector<vk::raii::Framebuffer> makeFramebuffers( vk::raii::Device const & device,
|
|
|
|
vk::raii::RenderPass & renderPass,
|
|
|
|
std::vector<vk::raii::ImageView> const & imageViews,
|
|
|
|
vk::raii::ImageView const * pDepthImageView,
|
|
|
|
vk::Extent2D const & extent )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
vk::ImageView attachments[2];
|
2024-01-16 12:48:53 +00:00
|
|
|
attachments[1] = pDepthImageView ? *pDepthImageView : vk::ImageView();
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::FramebufferCreateInfo framebufferCreateInfo(
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::FramebufferCreateFlags(), renderPass, pDepthImageView ? 2 : 1, attachments, extent.width, extent.height, 1 );
|
2021-06-21 09:32:43 +00:00
|
|
|
std::vector<vk::raii::Framebuffer> framebuffers;
|
2021-02-17 09:49:59 +00:00
|
|
|
framebuffers.reserve( imageViews.size() );
|
|
|
|
for ( auto const & imageView : imageViews )
|
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
attachments[0] = imageView;
|
2021-06-21 09:32:43 +00:00
|
|
|
framebuffers.push_back( vk::raii::Framebuffer( device, framebufferCreateInfo ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return framebuffers;
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::raii::Pipeline makeGraphicsPipeline( vk::raii::Device const & device,
|
|
|
|
vk::raii::PipelineCache const & pipelineCache,
|
|
|
|
vk::raii::ShaderModule const & vertexShaderModule,
|
|
|
|
vk::SpecializationInfo const * vertexShaderSpecializationInfo,
|
|
|
|
vk::raii::ShaderModule const & fragmentShaderModule,
|
|
|
|
vk::SpecializationInfo const * fragmentShaderSpecializationInfo,
|
|
|
|
uint32_t vertexStride,
|
|
|
|
std::vector<std::pair<vk::Format, uint32_t>> const & vertexInputAttributeFormatOffset,
|
|
|
|
vk::FrontFace frontFace,
|
|
|
|
bool depthBuffered,
|
|
|
|
vk::raii::PipelineLayout const & pipelineLayout,
|
|
|
|
vk::raii::RenderPass const & renderPass )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::array<vk::PipelineShaderStageCreateInfo, 2> pipelineShaderStageCreateInfos = {
|
2024-01-16 12:48:53 +00:00
|
|
|
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eVertex, vertexShaderModule, "main", vertexShaderSpecializationInfo ),
|
|
|
|
vk::PipelineShaderStageCreateInfo( {}, vk::ShaderStageFlagBits::eFragment, fragmentShaderModule, "main", fragmentShaderSpecializationInfo )
|
2021-02-17 09:49:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<vk::VertexInputAttributeDescription> vertexInputAttributeDescriptions;
|
|
|
|
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo;
|
|
|
|
vk::VertexInputBindingDescription vertexInputBindingDescription( 0, vertexStride );
|
|
|
|
|
|
|
|
if ( 0 < vertexStride )
|
|
|
|
{
|
|
|
|
vertexInputAttributeDescriptions.reserve( vertexInputAttributeFormatOffset.size() );
|
|
|
|
for ( uint32_t i = 0; i < vertexInputAttributeFormatOffset.size(); i++ )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
vertexInputAttributeDescriptions.emplace_back( i, 0, vertexInputAttributeFormatOffset[i].first, vertexInputAttributeFormatOffset[i].second );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
pipelineVertexInputStateCreateInfo.setVertexBindingDescriptions( vertexInputBindingDescription );
|
|
|
|
pipelineVertexInputStateCreateInfo.setVertexAttributeDescriptions( vertexInputAttributeDescriptions );
|
|
|
|
}
|
|
|
|
|
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 );
|
2021-02-17 09:49:59 +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 );
|
2021-02-17 09:49:59 +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 );
|
2021-02-17 09:49:59 +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 );
|
|
|
|
vk::PipelineColorBlendStateCreateInfo pipelineColorBlendStateCreateInfo(
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineColorBlendStateCreateFlags(), false, vk::LogicOp::eNoOp, pipelineColorBlendAttachmentState, { { 1.0f, 1.0f, 1.0f, 1.0f } } );
|
2021-02-17 09:49:59 +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 );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo( vk::PipelineCreateFlags(),
|
|
|
|
pipelineShaderStageCreateInfos,
|
|
|
|
&pipelineVertexInputStateCreateInfo,
|
|
|
|
&pipelineInputAssemblyStateCreateInfo,
|
|
|
|
nullptr,
|
|
|
|
&pipelineViewportStateCreateInfo,
|
|
|
|
&pipelineRasterizationStateCreateInfo,
|
|
|
|
&pipelineMultisampleStateCreateInfo,
|
|
|
|
&pipelineDepthStencilStateCreateInfo,
|
|
|
|
&pipelineColorBlendStateCreateInfo,
|
|
|
|
&pipelineDynamicStateCreateInfo,
|
2024-01-16 12:48:53 +00:00
|
|
|
pipelineLayout,
|
|
|
|
renderPass );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::Pipeline( device, pipelineCache, graphicsPipelineCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Image makeImage( vk::raii::Device const & device )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
vk::ImageCreateInfo imageCreateInfo( {},
|
|
|
|
vk::ImageType::e2D,
|
|
|
|
vk::Format::eB8G8R8A8Unorm,
|
|
|
|
vk::Extent3D( 640, 640, 1 ),
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
vk::ImageTiling::eLinear,
|
|
|
|
vk::ImageUsageFlagBits::eTransferSrc );
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::Image( device, imageCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::Instance makeInstance( vk::raii::Context const & context,
|
|
|
|
std::string const & appName,
|
|
|
|
std::string const & engineName,
|
|
|
|
std::vector<std::string> const & layers = {},
|
|
|
|
std::vector<std::string> const & extensions = {},
|
|
|
|
uint32_t apiVersion = VK_API_VERSION_1_0 )
|
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 )
|
|
|
|
,
|
|
|
|
context.enumerateInstanceLayerProperties()
|
|
|
|
#endif
|
|
|
|
);
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<char const *> enabledExtensions = vk::su::gatherExtensions( extensions
|
2021-05-25 14:14:56 +00:00
|
|
|
#if !defined( NDEBUG )
|
2022-02-28 09:11:04 +00:00
|
|
|
,
|
|
|
|
context.enumerateInstanceExtensionProperties()
|
2021-05-25 14:14:56 +00:00
|
|
|
#endif
|
2022-02-28 09:11:04 +00:00
|
|
|
);
|
2021-02-17 09:49:59 +00:00
|
|
|
#if defined( NDEBUG )
|
|
|
|
vk::StructureChain<vk::InstanceCreateInfo>
|
|
|
|
#else
|
|
|
|
vk::StructureChain<vk::InstanceCreateInfo, vk::DebugUtilsMessengerCreateInfoEXT>
|
|
|
|
#endif
|
2022-02-28 09:11:04 +00:00
|
|
|
instanceCreateInfoChain = vk::su::makeInstanceCreateInfoChain( applicationInfo, enabledLayers, enabledExtensions );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::Instance( context, instanceCreateInfoChain.get<vk::InstanceCreateInfo>() );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 09:32:43 +00:00
|
|
|
vk::raii::RenderPass makeRenderPass( vk::raii::Device const & device,
|
|
|
|
vk::Format colorFormat,
|
|
|
|
vk::Format depthFormat,
|
|
|
|
vk::AttachmentLoadOp loadOp = vk::AttachmentLoadOp::eClear,
|
|
|
|
vk::ImageLayout colorFinalLayout = vk::ImageLayout::ePresentSrcKHR )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::AttachmentDescription> attachmentDescriptions;
|
|
|
|
assert( colorFormat != vk::Format::eUndefined );
|
|
|
|
attachmentDescriptions.emplace_back( vk::AttachmentDescriptionFlags(),
|
|
|
|
colorFormat,
|
|
|
|
vk::SampleCountFlagBits::e1,
|
|
|
|
loadOp,
|
|
|
|
vk::AttachmentStoreOp::eStore,
|
|
|
|
vk::AttachmentLoadOp::eDontCare,
|
|
|
|
vk::AttachmentStoreOp::eDontCare,
|
|
|
|
vk::ImageLayout::eUndefined,
|
|
|
|
colorFinalLayout );
|
|
|
|
if ( depthFormat != vk::Format::eUndefined )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
vk::AttachmentReference colorAttachment( 0, vk::ImageLayout::eColorAttachmentOptimal );
|
|
|
|
vk::AttachmentReference depthAttachment( 1, vk::ImageLayout::eDepthStencilAttachmentOptimal );
|
|
|
|
vk::SubpassDescription subpassDescription( vk::SubpassDescriptionFlags(),
|
|
|
|
vk::PipelineBindPoint::eGraphics,
|
2023-08-08 11:40:56 +00:00
|
|
|
{},
|
2021-02-17 09:49:59 +00:00
|
|
|
colorAttachment,
|
2023-08-08 11:40:56 +00:00
|
|
|
{},
|
2022-02-28 09:11:04 +00:00
|
|
|
( depthFormat != vk::Format::eUndefined ) ? &depthAttachment : nullptr );
|
|
|
|
vk::RenderPassCreateInfo renderPassCreateInfo( vk::RenderPassCreateFlags(), attachmentDescriptions, subpassDescription );
|
2021-06-21 09:32:43 +00:00
|
|
|
return vk::raii::RenderPass( device, renderPassCreateInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vk::Format pickDepthFormat( vk::raii::PhysicalDevice const & physicalDevice )
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<vk::Format> candidates = { vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint };
|
2021-02-17 09:49:59 +00:00
|
|
|
for ( vk::Format format : candidates )
|
|
|
|
{
|
|
|
|
vk::FormatProperties props = physicalDevice.getFormatProperties( format );
|
|
|
|
|
|
|
|
if ( props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eDepthStencilAttachment )
|
|
|
|
{
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw std::runtime_error( "failed to find supported format!" );
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
void submitAndWait( vk::raii::Device const & device, vk::raii::Queue const & queue, vk::raii::CommandBuffer const & commandBuffer )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
vk::raii::Fence fence( device, vk::FenceCreateInfo() );
|
2024-01-16 12:48:53 +00:00
|
|
|
queue.submit( vk::SubmitInfo( nullptr, nullptr, *commandBuffer ), fence );
|
|
|
|
while ( vk::Result::eTimeout == device.waitForFences( { fence }, VK_TRUE, vk::su::FenceTimeout ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:43:45 +00:00
|
|
|
void updateDescriptorSets(
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::raii::DescriptorSet const & descriptorSet,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, vk::raii::Buffer const &, vk::DeviceSize, vk::raii::BufferView const *>> const & bufferData,
|
|
|
|
vk::raii::su::TextureData const & textureData,
|
|
|
|
uint32_t bindingOffset = 0 )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
|
|
|
bufferInfos.reserve( bufferData.size() );
|
|
|
|
|
|
|
|
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
|
|
|
writeDescriptorSets.reserve( bufferData.size() + 1 );
|
|
|
|
uint32_t dstBinding = bindingOffset;
|
2022-05-02 07:43:45 +00:00
|
|
|
for ( auto const & bd : bufferData )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
bufferInfos.emplace_back( std::get<1>( bd ), 0, std::get<2>( bd ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::BufferView bufferView;
|
2022-05-02 07:43:45 +00:00
|
|
|
if ( std::get<3>( bd ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
bufferView = *std::get<3>( bd );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
writeDescriptorSets.emplace_back(
|
2024-01-16 12:48:53 +00:00
|
|
|
descriptorSet, dstBinding++, 0, 1, std::get<0>( bd ), nullptr, &bufferInfos.back(), std::get<3>( bd ) ? &bufferView : nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 12:48:53 +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 );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
device.updateDescriptorSets( writeDescriptorSets, nullptr );
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:43:45 +00:00
|
|
|
void updateDescriptorSets(
|
|
|
|
vk::raii::Device const & device,
|
|
|
|
vk::raii::DescriptorSet const & descriptorSet,
|
|
|
|
std::vector<std::tuple<vk::DescriptorType, vk::raii::Buffer const &, vk::DeviceSize, vk::raii::BufferView const *>> const & bufferData,
|
|
|
|
std::vector<vk::raii::su::TextureData> const & textureData,
|
|
|
|
uint32_t bindingOffset = 0 )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
|
|
|
std::vector<vk::DescriptorBufferInfo> bufferInfos;
|
|
|
|
bufferInfos.reserve( bufferData.size() );
|
|
|
|
|
|
|
|
std::vector<vk::WriteDescriptorSet> writeDescriptorSets;
|
|
|
|
writeDescriptorSets.reserve( bufferData.size() + ( textureData.empty() ? 0 : 1 ) );
|
|
|
|
uint32_t dstBinding = bindingOffset;
|
2022-05-02 07:43:45 +00:00
|
|
|
for ( auto const & bd : bufferData )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
bufferInfos.emplace_back( std::get<1>( bd ), 0, std::get<2>( bd ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::BufferView bufferView;
|
2022-05-02 07:43:45 +00:00
|
|
|
if ( std::get<3>( bd ) )
|
2021-02-17 09:49:59 +00:00
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
bufferView = *std::get<3>( bd );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
writeDescriptorSets.emplace_back(
|
2024-01-16 12:48:53 +00:00
|
|
|
descriptorSet, dstBinding++, 0, 1, std::get<0>( bd ), nullptr, &bufferInfos.back(), std::get<3>( bd ) ? &bufferView : nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<vk::DescriptorImageInfo> imageInfos;
|
|
|
|
if ( !textureData.empty() )
|
|
|
|
{
|
|
|
|
imageInfos.reserve( textureData.size() );
|
|
|
|
for ( auto const & thd : textureData )
|
|
|
|
{
|
2024-01-16 12:48:53 +00:00
|
|
|
imageInfos.emplace_back( thd.sampler, thd.imageData.imageView, vk::ImageLayout::eShaderReadOnlyOptimal );
|
2021-02-17 09:49:59 +00:00
|
|
|
}
|
2024-01-16 12:48:53 +00:00
|
|
|
writeDescriptorSets.emplace_back( descriptorSet,
|
2021-02-17 09:49:59 +00:00
|
|
|
dstBinding,
|
|
|
|
0,
|
|
|
|
vk::su::checked_cast<uint32_t>( imageInfos.size() ),
|
|
|
|
vk::DescriptorType::eCombinedImageSampler,
|
|
|
|
imageInfos.data(),
|
|
|
|
nullptr,
|
|
|
|
nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
device.updateDescriptorSets( writeDescriptorSets, nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace su
|
|
|
|
} // namespace raii
|
|
|
|
} // namespace vk
|