Reorder some object instantiations to get valid destruction sequence. (#1641)

This commit is contained in:
Andreas Süßenbach 2023-08-21 11:35:17 +02:00 committed by GitHub
parent d07d082af1
commit 14d048e9bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View File

@ -73,6 +73,9 @@ int main( int /*argc*/, char ** /*argv*/ )
/* VULKAN_KEY_START */ /* 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 // create a vertex buffer for some vertex and color data
vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer ); vk::BufferCreateInfo bufferCreateInfo( {}, sizeof( coloredCubeData ), vk::BufferUsageFlagBits::eVertexBuffer );
vk::raii::Buffer vertexBuffer( device, bufferCreateInfo ); vk::raii::Buffer vertexBuffer( device, bufferCreateInfo );
@ -83,7 +86,7 @@ int main( int /*argc*/, char ** /*argv*/ )
memoryRequirements.memoryTypeBits, memoryRequirements.memoryTypeBits,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); 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 // copy the vertex and color data into that device memory
uint8_t * pData = static_cast<uint8_t *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) ); uint8_t * pData = static_cast<uint8_t *>( deviceMemory.mapMemory( 0, memoryRequirements.size ) );
@ -116,9 +119,6 @@ int main( int /*argc*/, char ** /*argv*/ )
commandBuffer.end(); commandBuffer.end();
vk::raii::su::submitAndWait( device, graphicsQueue, commandBuffer ); 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 */ /* VULKAN_KEY_END */
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )

View File

@ -84,6 +84,9 @@ int main( int /*argc*/, char ** /*argv*/ )
vk::ImageLayout::eUndefined, vk::ImageLayout::eUndefined,
vk::ImageLayout::eTransferDstOptimal ); 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 // Create an image, map it, and write some values to the image
vk::ImageCreateInfo imageCreateInfo( {}, vk::ImageCreateInfo imageCreateInfo( {},
vk::ImageType::e2D, 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 ); uint32_t memoryTypeIndex = vk::su::findMemoryType( memoryProperties, memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible );
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
vk::raii::DeviceMemory deviceMemory( device, memoryAllocateInfo ); deviceMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo );
blitSourceImage.bindMemory( *deviceMemory, 0 ); blitSourceImage.bindMemory( *deviceMemory, 0 );
vk::raii::su::setImageLayout( commandBuffer, *blitSourceImage, swapChainData.colorFormat, vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral ); 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 ) ); 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 */ /* VULKAN_KEY_END */
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )

View File

@ -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 // 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 ); 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::ImageCreateInfo imageCreateInfo( {},
vk::ImageType::e2D, vk::ImageType::e2D,
format, format,
@ -89,7 +92,7 @@ int main( int /*argc*/, char ** /*argv*/ )
// allocate memory // allocate memory
vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex );
vk::raii::DeviceMemory imageMemory( device, memoryAllocateInfo ); imageMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo );
// bind memory // bind memory
image.bindMemory( *imageMemory, 0 ); 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::ImageViewCreateInfo imageViewCreateInfo( {}, *image, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } );
vk::raii::ImageView imageView( device, imageViewCreateInfo ); 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 */ /* VULKAN_KEY_END */
} }
catch ( vk::SystemError & err ) catch ( vk::SystemError & err )