From e0846af2450064a4b1cd7c88a70d3b60635e891f Mon Sep 17 00:00:00 2001 From: Eugene Golushkov Date: Fri, 6 Dec 2024 17:18:03 +0100 Subject: [PATCH] [Vk] VulkanDeviceResource derived classes are sometimes created on other threads, so registration should be protected by mutex, recursive as notifyResourceDestroyed() can happen inside notifyDeviceLost() --- RenderSystems/Vulkan/include/OgreVulkanDeviceResource.h | 2 ++ RenderSystems/Vulkan/src/OgreVulkanDeviceResource.cpp | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/RenderSystems/Vulkan/include/OgreVulkanDeviceResource.h b/RenderSystems/Vulkan/include/OgreVulkanDeviceResource.h index 30516e3076..b88cbe594d 100644 --- a/RenderSystems/Vulkan/include/OgreVulkanDeviceResource.h +++ b/RenderSystems/Vulkan/include/OgreVulkanDeviceResource.h @@ -30,6 +30,7 @@ THE SOFTWARE. #include "OgreVulkanPrerequisites.h" +#include #include "ogrestd/vector.h" namespace Ogre @@ -73,6 +74,7 @@ namespace Ogre ~VulkanDeviceResourceManager(); // protected and non-virtual private: + std::recursive_mutex mResourcesMutex; vector::type mResources, mResourcesCopy; }; diff --git a/RenderSystems/Vulkan/src/OgreVulkanDeviceResource.cpp b/RenderSystems/Vulkan/src/OgreVulkanDeviceResource.cpp index e986ac5849..97cdaec12c 100644 --- a/RenderSystems/Vulkan/src/OgreVulkanDeviceResource.cpp +++ b/RenderSystems/Vulkan/src/OgreVulkanDeviceResource.cpp @@ -64,12 +64,16 @@ namespace Ogre void VulkanDeviceResourceManager::notifyResourceCreated( VulkanDeviceResource *deviceResource ) { + std::lock_guard guard(mResourcesMutex); + assert( std::find( mResources.begin(), mResources.end(), deviceResource ) == mResources.end() ); mResources.push_back( deviceResource ); } void VulkanDeviceResourceManager::notifyResourceDestroyed( VulkanDeviceResource *deviceResource ) { + std::lock_guard guard(mResourcesMutex); + vector::type::iterator it = std::find( mResources.begin(), mResources.end(), deviceResource ); assert( it != mResources.end() ); @@ -83,6 +87,8 @@ namespace Ogre void VulkanDeviceResourceManager::notifyDeviceLost() { + std::lock_guard guard(mResourcesMutex); + assert( mResourcesCopy.empty() ); // reentrancy is not expected nor supported mResourcesCopy = mResources; @@ -99,6 +105,8 @@ namespace Ogre void VulkanDeviceResourceManager::notifyDeviceRestored() { + std::lock_guard guard(mResourcesMutex); + assert( mResourcesCopy.empty() ); // reentrancy is not expected nor supported mResourcesCopy = mResources; for( unsigned pass = 0; pass < 2; ++pass )