Introduce raii-compliant handle wrapper classes.

This commit is contained in:
asuessenbach
2021-02-17 10:49:59 +01:00
parent 8dc12ba963
commit 2cb1c19c7f
165 changed files with 32669 additions and 2892 deletions

View File

@@ -41,28 +41,28 @@ int main( int /*argc*/, char ** /*argv*/ )
{
try
{
vk::UniqueInstance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
#if !defined( NDEBUG )
vk::UniqueDebugUtilsMessengerEXT debugUtilsMessenger = vk::su::createDebugUtilsMessenger( instance );
vk::DebugUtilsMessengerEXT debugUtilsMessenger =
instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
#endif
vk::PhysicalDevice physicalDevice = instance->enumeratePhysicalDevices().front();
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 50, 50 ) );
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex =
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, *surfaceData.surface );
vk::UniqueDevice device =
vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
vk::Device device =
vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
vk::UniqueCommandPool commandPool = vk::su::createCommandPool( device, graphicsAndPresentQueueFamilyIndex.first );
vk::UniqueCommandBuffer commandBuffer = std::move( device
->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo(
commandPool.get(), vk::CommandBufferLevel::ePrimary, 1 ) )
.front() );
vk::CommandPool commandPool = vk::su::createCommandPool( device, graphicsAndPresentQueueFamilyIndex.first );
vk::CommandBuffer commandBuffer =
device.allocateCommandBuffers( vk::CommandBufferAllocateInfo( commandPool, vk::CommandBufferLevel::ePrimary, 1 ) )
.front();
vk::Queue graphicsQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::Queue presentQueue = device->getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
vk::Queue graphicsQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
vk::Queue presentQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
/* VULKAN_KEY_START */
@@ -72,66 +72,66 @@ int main( int /*argc*/, char ** /*argv*/ )
// See if we can use a linear tiled image for a texture, if not, we will need a staging buffer for the texture data
bool needsStaging = !( formatProperties.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage );
vk::UniqueImage image = device->createImageUnique(
vk::ImageCreateInfo( vk::ImageCreateFlags(),
vk::ImageType::e2D,
format,
vk::Extent3D( surfaceData.extent, 1 ),
1,
1,
vk::SampleCountFlagBits::e1,
needsStaging ? vk::ImageTiling::eOptimal : vk::ImageTiling::eLinear,
vk::ImageUsageFlagBits::eSampled |
( needsStaging ? vk::ImageUsageFlagBits::eTransferDst : vk::ImageUsageFlagBits() ),
vk::SharingMode::eExclusive,
{},
needsStaging ? vk::ImageLayout::eUndefined : vk::ImageLayout::ePreinitialized ) );
vk::ImageCreateInfo imageCreateInfo(
vk::ImageCreateFlags(),
vk::ImageType::e2D,
format,
vk::Extent3D( surfaceData.extent, 1 ),
1,
1,
vk::SampleCountFlagBits::e1,
needsStaging ? vk::ImageTiling::eOptimal : vk::ImageTiling::eLinear,
vk::ImageUsageFlagBits::eSampled |
( needsStaging ? vk::ImageUsageFlagBits::eTransferDst : vk::ImageUsageFlagBits() ),
vk::SharingMode::eExclusive,
{},
needsStaging ? vk::ImageLayout::eUndefined : vk::ImageLayout::ePreinitialized );
vk::Image image = device.createImage( imageCreateInfo );
vk::MemoryRequirements memoryRequirements = device->getImageMemoryRequirements( image.get() );
vk::MemoryRequirements memoryRequirements = device.getImageMemoryRequirements( image );
uint32_t memoryTypeIndex = vk::su::findMemoryType(
physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits,
needsStaging ? vk::MemoryPropertyFlags()
: ( vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ) );
: ( vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ) );
// allocate memory
vk::UniqueDeviceMemory imageMemory =
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
vk::DeviceMemory imageMemory =
device.allocateMemory( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
// bind memory
device->bindImageMemory( image.get(), imageMemory.get(), 0 );
device.bindImageMemory( image, imageMemory, 0 );
vk::UniqueBuffer textureBuffer;
vk::UniqueDeviceMemory textureBufferMemory;
vk::Buffer textureBuffer;
vk::DeviceMemory textureBufferMemory;
if ( needsStaging )
{
// Need a staging buffer to map and copy texture into
textureBuffer =
device->createBufferUnique( vk::BufferCreateInfo( vk::BufferCreateFlags(),
surfaceData.extent.width * surfaceData.extent.height * 4,
vk::BufferUsageFlagBits::eTransferSrc ) );
device.createBuffer( vk::BufferCreateInfo( vk::BufferCreateFlags(),
surfaceData.extent.width * surfaceData.extent.height * 4,
vk::BufferUsageFlagBits::eTransferSrc ) );
memoryRequirements = device->getBufferMemoryRequirements( textureBuffer.get() );
memoryRequirements = device.getBufferMemoryRequirements( textureBuffer );
memoryTypeIndex =
vk::su::findMemoryType( physicalDevice.getMemoryProperties(),
memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
// allocate memory
textureBufferMemory =
device->allocateMemoryUnique( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
textureBufferMemory = device.allocateMemory( vk::MemoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ) );
// bind memory
device->bindBufferMemory( textureBuffer.get(), textureBufferMemory.get(), 0 );
device.bindBufferMemory( textureBuffer, textureBufferMemory, 0 );
}
else
{
vk::SubresourceLayout subresourceLayout =
device->getImageSubresourceLayout( image.get(), vk::ImageSubresource( vk::ImageAspectFlagBits::eColor ) );
device.getImageSubresourceLayout( image, vk::ImageSubresource( vk::ImageAspectFlagBits::eColor ) );
}
void * data = device->mapMemory(
needsStaging ? textureBufferMemory.get() : imageMemory.get(), 0, memoryRequirements.size, vk::MemoryMapFlags() );
void * data = device.mapMemory(
needsStaging ? textureBufferMemory : imageMemory, 0, memoryRequirements.size, vk::MemoryMapFlags() );
// Checkerboard of 16x16 pixel squares
unsigned char * pImageMemory = static_cast<unsigned char *>( data );
@@ -148,68 +148,73 @@ int main( int /*argc*/, char ** /*argv*/ )
}
}
device->unmapMemory( needsStaging ? textureBufferMemory.get() : imageMemory.get() );
device.unmapMemory( needsStaging ? textureBufferMemory : imageMemory );
commandBuffer->begin( vk::CommandBufferBeginInfo() );
commandBuffer.begin( vk::CommandBufferBeginInfo() );
if ( needsStaging )
{
// Since we're going to blit to the texture image, set its layout to eTransferDstOptimal
vk::su::setImageLayout(
commandBuffer, image.get(), format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal );
commandBuffer, image, format, vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal );
vk::BufferImageCopy copyRegion( 0,
surfaceData.extent.width,
surfaceData.extent.height,
vk::ImageSubresourceLayers( vk::ImageAspectFlagBits::eColor, 0, 0, 1 ),
vk::Offset3D( 0, 0, 0 ),
vk::Extent3D( surfaceData.extent, 1 ) );
commandBuffer->copyBufferToImage(
textureBuffer.get(), image.get(), vk::ImageLayout::eTransferDstOptimal, copyRegion );
commandBuffer.copyBufferToImage( textureBuffer, image, vk::ImageLayout::eTransferDstOptimal, copyRegion );
// Set the layout for the texture image from eTransferDstOptimal to SHADER_READ_ONLY
vk::su::setImageLayout( commandBuffer,
image.get(),
format,
vk::ImageLayout::eTransferDstOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal );
vk::su::setImageLayout(
commandBuffer, image, format, vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal );
}
else
{
// If we can use the linear tiled image as a texture, just do it
vk::su::setImageLayout(
commandBuffer, image.get(), format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal );
commandBuffer, image, format, vk::ImageLayout::ePreinitialized, vk::ImageLayout::eShaderReadOnlyOptimal );
}
commandBuffer->end();
commandBuffer.end();
vk::su::submitAndWait( device, graphicsQueue, commandBuffer );
vk::UniqueSampler sampler =
device->createSamplerUnique( vk::SamplerCreateInfo( vk::SamplerCreateFlags(),
vk::Filter::eNearest,
vk::Filter::eNearest,
vk::SamplerMipmapMode::eNearest,
vk::SamplerAddressMode::eClampToEdge,
vk::SamplerAddressMode::eClampToEdge,
vk::SamplerAddressMode::eClampToEdge,
0.0f,
false,
1.0f,
false,
vk::CompareOp::eNever,
0.0f,
0.0f,
vk::BorderColor::eFloatOpaqueWhite ) );
vk::SamplerCreateInfo samplerCreateInfo( vk::SamplerCreateFlags(),
vk::Filter::eNearest,
vk::Filter::eNearest,
vk::SamplerMipmapMode::eNearest,
vk::SamplerAddressMode::eClampToEdge,
vk::SamplerAddressMode::eClampToEdge,
vk::SamplerAddressMode::eClampToEdge,
0.0f,
false,
1.0f,
false,
vk::CompareOp::eNever,
0.0f,
0.0f,
vk::BorderColor::eFloatOpaqueWhite );
vk::Sampler sampler = device.createSampler( samplerCreateInfo );
vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR, vk::ComponentSwizzle::eG, vk::ComponentSwizzle::eB, vk::ComponentSwizzle::eA );
vk::ImageViewCreateInfo imageViewCreateInfo(
vk::ImageViewCreateFlags(),
image.get(),
vk::ImageViewType::e2D,
format,
componentMapping,
vk::ImageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ) );
vk::UniqueImageView imageView = device->createImageViewUnique( imageViewCreateInfo );
vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
vk::ImageViewCreateInfo imageViewCreateInfo(
{}, image, vk::ImageViewType::e2D, format, componentMapping, imageSubresourceRange );
vk::ImageView imageView = device.createImageView( imageViewCreateInfo );
/* VULKAN_KEY_END */
device.destroyImageView( imageView );
device.destroySampler( sampler );
device.freeMemory( textureBufferMemory );
device.destroyBuffer( textureBuffer );
device.freeMemory( imageMemory );
device.destroyImage( image );
device.freeCommandBuffers( commandPool, commandBuffer );
device.destroyCommandPool( commandPool );
device.destroy();
instance.destroySurfaceKHR( surfaceData.surface );
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
instance.destroy();
}
catch ( vk::SystemError & err )
{