From 14d048e9bfb1e3d61f1fe3d66de6a94d01949004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Mon, 21 Aug 2023 11:35:17 +0200 Subject: [PATCH] Reorder some object instantiations to get valid destruction sequence. (#1641) --- RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp | 8 ++++---- RAII_Samples/CopyBlitImage/CopyBlitImage.cpp | 8 ++++---- RAII_Samples/InitTexture/InitTexture.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp b/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp index 7c72020..4e752e3 100644 --- a/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp +++ b/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp @@ -73,6 +73,9 @@ int main( int /*argc*/, char ** /*argv*/ ) /* VULKAN_KEY_START */ + // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the vertex buffer first + vk::raii::DeviceMemory deviceMemory( nullptr ); + // create a vertex buffer for some vertex and color data vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); vk::raii::Buffer vertexBuffer( device, bufferCreateInfo ); @@ -83,7 +86,7 @@ int main( int /*argc*/, char ** /*argv*/ ) memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); - vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo ); + deviceMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo ); // copy the vertex and color data into that device memory uint8_t * pData = static_cast( deviceMemory.mapMemory( 0, memoryRequirements.size ) ); @@ -116,9 +119,6 @@ int main( int /*argc*/, char ** /*argv*/ ) commandBuffer.end(); vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer ); - // in order to prevent some validation layer warning, you need to explicitly free the buffer before the device memory - vertexBuffer.clear(); - /* VULKAN_KEY_END */ } catch ( vk::SystemError & err ) diff --git a/RAII_Samples/CopyBlitImage/CopyBlitImage.cpp b/RAII_Samples/CopyBlitImage/CopyBlitImage.cpp index 58f4984..25a08b1 100644 --- a/RAII_Samples/CopyBlitImage/CopyBlitImage.cpp +++ b/RAII_Samples/CopyBlitImage/CopyBlitImage.cpp @@ -84,6 +84,9 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal ); + // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the image first + vk::raii::DeviceMemory deviceMemory( nullptr ); + // Create an image, map it, and write some values to the image vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageType::e2D, @@ -101,7 +104,7 @@ int main( int /*argc*/, char ** /*argv*/ ) uint32_t memoryTypeIndex = vk::su::findMemoryType( memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); - vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo ); + deviceMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo ); blitSourceImage.bindMemory( *deviceMemory, 0 ); vk::raii::su::setImageLayout( commandBuffer, *blitSourceImage, swapChainData.colorFormat, vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral ); @@ -206,9 +209,6 @@ int main( int /*argc*/, char ** /*argv*/ ) } std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); - // in order to prevent some validation layer warning, you need to explicitly free the image before the device memory - blitSourceImage.clear(); - /* VULKAN_KEY_END */ } catch ( vk::SystemError & err ) diff --git a/RAII_Samples/InitTexture/InitTexture.cpp b/RAII_Samples/InitTexture/InitTexture.cpp index f599c33..b197b14 100644 --- a/RAII_Samples/InitTexture/InitTexture.cpp +++ b/RAII_Samples/InitTexture/InitTexture.cpp @@ -67,6 +67,9 @@ 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 ); + // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the image first + vk::raii::DeviceMemory imageMemory( nullptr ); + vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageType::e2D, format, @@ -89,7 +92,7 @@ int main( int /*argc*/, char ** /*argv*/ ) // allocate memory vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); - vk::raii::DeviceMemory imageMemory( device, memoryAllocateInfo ); + imageMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo ); // bind memory image.bindMemory( *imageMemory, 0 ); @@ -183,9 +186,6 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::ImageViewCreateInfo imageViewCreateInfo( {}, *image, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } ); vk::raii::ImageView imageView( device, imageViewCreateInfo ); - // in order to prevent some validation layer warning, you need to explicitly free the image before the device memory - image.clear(); - /* VULKAN_KEY_END */ } catch ( vk::SystemError & err )