From fee04df94391a1e4d8f471e15ef2daeb87410a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=BC=C3=9Fenbach?= Date: Wed, 23 Aug 2023 13:58:54 +0200 Subject: [PATCH] Resolve some more validation layer issues. (#1643) --- .../InputAttachment/InputAttachment.cpp | 5 ++- .../OcclusionQuery/OcclusionQuery.cpp | 5 ++- RAII_Samples/RayTracing/RayTracing.cpp | 40 +++++++++---------- .../SecondaryCommandBuffer.cpp | 6 +-- samples/InputAttachment/InputAttachment.cpp | 2 +- samples/OcclusionQuery/OcclusionQuery.cpp | 4 +- samples/RayTracing/RayTracing.cpp | 4 +- .../SecondaryCommandBuffer.cpp | 2 +- samples/utils/utils.cpp | 9 +++-- 9 files changed, 40 insertions(+), 37 deletions(-) diff --git a/RAII_Samples/InputAttachment/InputAttachment.cpp b/RAII_Samples/InputAttachment/InputAttachment.cpp index d84bf8f..8e01fbf 100644 --- a/RAII_Samples/InputAttachment/InputAttachment.cpp +++ b/RAII_Samples/InputAttachment/InputAttachment.cpp @@ -110,6 +110,9 @@ int main( int /*argc*/, char ** /*argv*/ ) // attachment which will be cleared to yellow, and then used by the shaders to color the drawn triangle. Final // result should be a yellow triangle + // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the vertex buffer first + vk::raii::DeviceMemory inputMemory( nullptr ); + // Create the image that will be used as the input attachment // The image for the color attachment is the presentable image already created as part of the SwapChainData vk::ImageCreateInfo imageCreateInfo( {}, @@ -126,7 +129,7 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::MemoryRequirements memoryRequirements = inputImage.getMemoryRequirements(); uint32_t memoryTypeIndex = vk::su::findMemoryType( physicalDevice.getMemoryProperties(), memoryRequirements.memoryTypeBits, {} ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); - vk::raii::DeviceMemory inputMemory( device, memoryAllocateInfo ); + inputMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo ); inputImage.bindMemory( *inputMemory, 0 ); // Set the image layout to TRANSFER_DST_OPTIMAL to be ready for clear diff --git a/RAII_Samples/OcclusionQuery/OcclusionQuery.cpp b/RAII_Samples/OcclusionQuery/OcclusionQuery.cpp index 44ff4d4..3e10ddc 100644 --- a/RAII_Samples/OcclusionQuery/OcclusionQuery.cpp +++ b/RAII_Samples/OcclusionQuery/OcclusionQuery.cpp @@ -121,6 +121,9 @@ int main( int /*argc*/, char ** /*argv*/ ) assert( result == vk::Result::eSuccess ); assert( imageIndex < swapChainData.images.size() ); + // in order to get a clean desctruction sequence, instantiate the DeviceMemory for the result buffer first + vk::raii::DeviceMemory queryResultMemory( nullptr ); + /* Allocate a uniform buffer that will take query results. */ vk::BufferCreateInfo bufferCreateInfo( {}, 4 * sizeof( uint64_t ), vk::BufferUsageFlagBits::eUniformBuffer | vk::BufferUsageFlagBits::eTransferDst ); vk::raii::Buffer queryResultBuffer( device, bufferCreateInfo ); @@ -130,7 +133,7 @@ int main( int /*argc*/, char ** /*argv*/ ) memoryRequirements.memoryTypeBits, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent ); vk::MemoryAllocateInfo memoryAllocateInfo( memoryRequirements.size, memoryTypeIndex ); - vk::raii::DeviceMemory queryResultMemory( device, memoryAllocateInfo ); + queryResultMemory = vk::raii::DeviceMemory( device, memoryAllocateInfo ); queryResultBuffer.bindMemory( *queryResultMemory, 0 ); diff --git a/RAII_Samples/RayTracing/RayTracing.cpp b/RAII_Samples/RayTracing/RayTracing.cpp index a472763..3705a15 100644 --- a/RAII_Samples/RayTracing/RayTracing.cpp +++ b/RAII_Samples/RayTracing/RayTracing.cpp @@ -75,10 +75,11 @@ static_assert( sizeof( GeometryInstanceData ) == 64, "GeometryInstanceData struc struct AccelerationStructureData { - vk::raii::AccelerationStructureNV acclerationStructure = nullptr; - vk::raii::su::BufferData scratchBufferData = nullptr; - vk::raii::su::BufferData resultBufferData = nullptr; - vk::raii::su::BufferData instanceBufferData = nullptr; + // Note: in order to have a clean destruction order, list the resultBufferData before the accelerationStructure that binds it + vk::raii::su::BufferData resultBufferData = nullptr; + vk::raii::AccelerationStructureNV accelerationStructure = nullptr; + vk::raii::su::BufferData scratchBufferData = nullptr; + vk::raii::su::BufferData instanceBufferData = nullptr; }; AccelerationStructureData createAccelerationStructureData( vk::raii::PhysicalDevice const & physicalDevice, @@ -95,19 +96,19 @@ AccelerationStructureData createAccelerationStructureData( vk::raii::PhysicalDev instances.empty() ? vk::AccelerationStructureTypeNV::eBottomLevel : vk::AccelerationStructureTypeNV::eTopLevel; vk::AccelerationStructureInfoNV accelerationStructureInfo( accelerationStructureType, {}, vk::su::checked_cast( instances.size() ), geometries ); vk::AccelerationStructureCreateInfoNV accelerationStructureCreateInfoNV( 0, accelerationStructureInfo ); - accelerationStructureData.acclerationStructure = vk::raii::AccelerationStructureNV( device, accelerationStructureCreateInfoNV ); + accelerationStructureData.accelerationStructure = vk::raii::AccelerationStructureNV( device, accelerationStructureCreateInfoNV ); vk::AccelerationStructureMemoryRequirementsInfoNV objectRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eObject, - *accelerationStructureData.acclerationStructure ); + *accelerationStructureData.accelerationStructure ); vk::DeviceSize resultSizeInBytes = device.getAccelerationStructureMemoryRequirementsNV( objectRequirements ).memoryRequirements.size; assert( 0 < resultSizeInBytes ); accelerationStructureData.resultBufferData = vk::raii::su::BufferData( physicalDevice, device, resultSizeInBytes, vk::BufferUsageFlagBits::eRayTracingNV, vk::MemoryPropertyFlagBits::eDeviceLocal ); vk::AccelerationStructureMemoryRequirementsInfoNV buildScratchRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eBuildScratch, - *accelerationStructureData.acclerationStructure ); + *accelerationStructureData.accelerationStructure ); vk::AccelerationStructureMemoryRequirementsInfoNV updateScratchRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eUpdateScratch, - *accelerationStructureData.acclerationStructure ); + *accelerationStructureData.accelerationStructure ); vk::DeviceSize scratchSizeInBytes = std::max( device.getAccelerationStructureMemoryRequirementsNV( buildScratchRequirements ).memoryRequirements.size, device.getAccelerationStructureMemoryRequirementsNV( updateScratchRequirements ).memoryRequirements.size ); assert( 0 < scratchSizeInBytes ); @@ -141,7 +142,7 @@ AccelerationStructureData createAccelerationStructureData( vk::raii::PhysicalDev } device.bindAccelerationStructureMemoryNV( - vk::BindAccelerationStructureMemoryInfoNV( *accelerationStructureData.acclerationStructure, *accelerationStructureData.resultBufferData.deviceMemory ) ); + vk::BindAccelerationStructureMemoryInfoNV( *accelerationStructureData.accelerationStructure, *accelerationStructureData.resultBufferData.deviceMemory ) ); vk::Buffer instanceData; if ( *accelerationStructureData.instanceBufferData.buffer ) @@ -153,7 +154,7 @@ AccelerationStructureData createAccelerationStructureData( vk::raii::PhysicalDev instanceData, 0, false, - *accelerationStructureData.acclerationStructure, + *accelerationStructureData.accelerationStructure, nullptr, *accelerationStructureData.scratchBufferData.buffer, 0 ); @@ -681,7 +682,6 @@ int main( int /*argc*/, char ** /*argv*/ ) { instanceExtensions.push_back( glfwExtensions[i] ); } - instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME ); #if VULKAN_HPP_ENABLE_DYNAMIC_LOADER_TOOL vk::raii::Context context; @@ -734,11 +734,7 @@ int main( int /*argc*/, char ** /*argv*/ ) auto supportedFeatures = physicalDevice.getFeatures2(); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, - { VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, - VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME, - VK_KHR_MAINTENANCE_3_EXTENSION_NAME, - VK_KHR_SWAPCHAIN_EXTENSION_NAME, - VK_NV_RAY_TRACING_EXTENSION_NAME }, + { VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_NV_RAY_TRACING_EXTENSION_NAME }, &supportedFeatures.get().features, &supportedFeatures.get() ); @@ -929,7 +925,7 @@ int main( int /*argc*/, char ** /*argv*/ ) topLevelAS = createAccelerationStructureData( physicalDevice, device, commandBuffer, - { std::make_pair( std::ref( bottomLevelAS.acclerationStructure ), std::ref( transform ) ) }, + { std::make_pair( std::ref( bottomLevelAS.accelerationStructure ), std::ref( transform ) ) }, std::vector() ); } ); @@ -943,11 +939,11 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::BufferMemoryBarrier bufferMemoryBarrier( {}, vk::AccessFlagBits::eShaderRead, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, *vertexBufferData.buffer, 0, VK_WHOLE_SIZE ); commandBuffer.pipelineBarrier( - vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr ); + vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eRayTracingShaderKHR, {}, nullptr, bufferMemoryBarrier, nullptr ); bufferMemoryBarrier.buffer = *indexBufferData.buffer; commandBuffer.pipelineBarrier( - vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr ); + vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eRayTracingShaderKHR, {}, nullptr, bufferMemoryBarrier, nullptr ); } ); std::vector bindings; @@ -982,7 +978,7 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::raii::DescriptorSets rayTracingDescriptorSets( device, descriptorSetAllocateInfo ); // Bind ray tracing specific descriptor sets into pNext of a vk::WriteDescriptorSet - vk::WriteDescriptorSetAccelerationStructureNV writeDescriptorSetAcceleration( 1, &*topLevelAS.acclerationStructure ); + vk::WriteDescriptorSetAccelerationStructureNV writeDescriptorSetAcceleration( 1, &*topLevelAS.accelerationStructure ); std::vector accelerationDescriptionSets; for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ ) { @@ -1280,7 +1276,7 @@ int main( int /*argc*/, char ** /*argv*/ ) rayTracingDescriptorSets.clear(); rayTracingDescriptorSetLayout = vk::raii::DescriptorSetLayout( nullptr ); rayTracingDescriptorPool = vk::raii::DescriptorPool( nullptr ); - topLevelAS.acclerationStructure = vk::raii::AccelerationStructureNV( nullptr ); + topLevelAS.accelerationStructure = vk::raii::AccelerationStructureNV( nullptr ); topLevelAS.instanceBufferData.buffer = vk::raii::Buffer( nullptr ); topLevelAS.instanceBufferData.deviceMemory = vk::raii::DeviceMemory( nullptr ); topLevelAS.resultBufferData.buffer = vk::raii::Buffer( nullptr ); @@ -1288,7 +1284,7 @@ int main( int /*argc*/, char ** /*argv*/ ) topLevelAS.scratchBufferData.buffer = vk::raii::Buffer( nullptr ); topLevelAS.scratchBufferData.deviceMemory = vk::raii::DeviceMemory( nullptr ); descriptorSet = vk::raii::DescriptorSet( nullptr ); - bottomLevelAS.acclerationStructure = vk::raii::AccelerationStructureNV( nullptr ); + bottomLevelAS.accelerationStructure = vk::raii::AccelerationStructureNV( nullptr ); bottomLevelAS.instanceBufferData.buffer = vk::raii::Buffer( nullptr ); bottomLevelAS.instanceBufferData.deviceMemory = vk::raii::DeviceMemory( nullptr ); bottomLevelAS.resultBufferData.buffer = vk::raii::Buffer( nullptr ); diff --git a/RAII_Samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp b/RAII_Samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp index 481610e..b9be71a 100644 --- a/RAII_Samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp +++ b/RAII_Samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp @@ -54,7 +54,7 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::raii::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface ); vk::raii::Device device = vk::raii::su::makeDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() ); - vk::raii::CommandPool commandPool = vk::raii::CommandPool( device, { {}, graphicsAndPresentQueueFamilyIndex.first } ); + vk::raii::CommandPool commandPool = vk::raii::CommandPool( device, { {}, graphicsAndPresentQueueFamilyIndex.first } ); vk::raii::CommandBuffer commandBuffer = vk::raii::su::makeCommandBuffer( device, commandPool ); vk::raii::Queue graphicsQueue( device, graphicsAndPresentQueueFamilyIndex.first, 0 ); @@ -104,7 +104,7 @@ int main( int /*argc*/, char ** /*argv*/ ) fragmentShaderModule, nullptr, sizeof( texturedCubeData[0] ), - { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, + { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32Sfloat, 16 } }, vk::FrontFace::eClockwise, true, pipelineLayout, @@ -192,7 +192,7 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::ImageSubresourceRange imageSubresourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 ); vk::ImageMemoryBarrier prePresentBarrier( vk::AccessFlagBits::eColorAttachmentWrite, - vk::AccessFlagBits::eMemoryRead, + {}, vk::ImageLayout::eColorAttachmentOptimal, vk::ImageLayout::ePresentSrcKHR, VK_QUEUE_FAMILY_IGNORED, diff --git a/samples/InputAttachment/InputAttachment.cpp b/samples/InputAttachment/InputAttachment.cpp index 0b13eeb..e1ee0f3 100644 --- a/samples/InputAttachment/InputAttachment.cpp +++ b/samples/InputAttachment/InputAttachment.cpp @@ -263,8 +263,8 @@ int main( int /*argc*/, char ** /*argv*/ ) device.destroyPipelineLayout( pipelineLayout ); device.destroyDescriptorSetLayout( descriptorSetLayout ); device.destroyImageView( inputAttachmentView ); + device.destroyImage( inputImage ); // destroy the inputImage before freeing the bound inputMemory ! device.freeMemory( inputMemory ); - device.destroyImage( inputImage ); swapChainData.clear( device ); device.freeCommandBuffers( commandPool, commandBuffer ); device.destroyCommandPool( commandPool ); diff --git a/samples/OcclusionQuery/OcclusionQuery.cpp b/samples/OcclusionQuery/OcclusionQuery.cpp index 884d19c..74a43bb 100644 --- a/samples/OcclusionQuery/OcclusionQuery.cpp +++ b/samples/OcclusionQuery/OcclusionQuery.cpp @@ -95,7 +95,7 @@ int main( int /*argc*/, char ** /*argv*/ ) std::make_pair( vertexShaderModule, nullptr ), std::make_pair( fragmentShaderModule, nullptr ), sizeof( coloredCubeData[0] ), - { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32B32A32Sfloat, 16 } }, + { { vk::Format::eR32G32B32A32Sfloat, 0 }, { vk::Format::eR32G32B32A32Sfloat, 16 } }, vk::FrontFace::eClockwise, true, pipelineLayout, @@ -199,8 +199,8 @@ int main( int /*argc*/, char ** /*argv*/ ) device.destroyFence( drawFence ); device.destroyQueryPool( queryPool ); + device.destroyBuffer( queryResultBuffer ); // destroy the queryResultBuffer before freeing the bound queryResultMemory ! device.freeMemory( queryResultMemory ); - device.destroyBuffer( queryResultBuffer ); device.destroySemaphore( imageAcquiredSemaphore ); /* VULKAN_KEY_END */ diff --git a/samples/RayTracing/RayTracing.cpp b/samples/RayTracing/RayTracing.cpp index 8f42af2..caac07e 100644 --- a/samples/RayTracing/RayTracing.cpp +++ b/samples/RayTracing/RayTracing.cpp @@ -942,11 +942,11 @@ int main( int /*argc*/, char ** /*argv*/ ) vk::BufferMemoryBarrier bufferMemoryBarrier( {}, vk::AccessFlagBits::eShaderRead, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, vertexBufferData.buffer, 0, VK_WHOLE_SIZE ); commandBuffer.pipelineBarrier( - vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr ); + vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eRayTracingShaderKHR, {}, nullptr, bufferMemoryBarrier, nullptr ); bufferMemoryBarrier.buffer = indexBufferData.buffer; commandBuffer.pipelineBarrier( - vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr ); + vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eRayTracingShaderKHR, {}, nullptr, bufferMemoryBarrier, nullptr ); } ); std::vector bindings; diff --git a/samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp b/samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp index 93771ea..d9e5d3b 100644 --- a/samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp +++ b/samples/SecondaryCommandBuffer/SecondaryCommandBuffer.cpp @@ -188,7 +188,7 @@ int main( int /*argc*/, char ** /*argv*/ ) commandBuffer.endRenderPass(); vk::ImageMemoryBarrier prePresentBarrier( vk::AccessFlagBits::eColorAttachmentWrite, - vk::AccessFlagBits::eMemoryRead, + {}, vk::ImageLayout::eColorAttachmentOptimal, vk::ImageLayout::ePresentSrcKHR, VK_QUEUE_FAMILY_IGNORED, diff --git a/samples/utils/utils.cpp b/samples/utils/utils.cpp index 49e88cd..c204aad 100644 --- a/samples/utils/utils.cpp +++ b/samples/utils/utils.cpp @@ -356,14 +356,15 @@ namespace vk void * /*pUserData*/ ) { #if !defined( NDEBUG ) - if ( pCallbackData->messageIdNumber == 648835635 ) + if ( static_cast(pCallbackData->messageIdNumber) == 0x822806fa ) { - // UNASSIGNED-khronos-Validation-debug-build-warning-message + // 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. return vk::False; } - if ( pCallbackData->messageIdNumber == 767975156 ) + else if ( static_cast(pCallbackData->messageIdNumber) == 0xe8d1a9fe ) { - // UNASSIGNED-BestPractices-vkCreateInstance-specialuse-extension + // Validation Performance Warning: Using debug builds of the validation layers *will* adversely affect performance. return vk::False; } #endif