From 55204700ff0d92fec209cd9b34659aefcc7f814a Mon Sep 17 00:00:00 2001 From: Mikhail Gorobets Date: Mon, 15 Jan 2024 20:53:04 +0600 Subject: [PATCH] GraphicsTools: Added resource registry (close #452) --- Graphics/GraphicsTools/CMakeLists.txt | 1 + .../interface/GraphicsUtilities.h | 28 ++++ .../interface/ResourceRegistry.hpp | 146 ++++++++++++++++++ .../GraphicsTools/src/GraphicsUtilities.cpp | 114 ++++++++++++++ .../GraphicsTools/ResourceRegistryH_test.cpp | 27 ++++ 5 files changed, 316 insertions(+) create mode 100644 Graphics/GraphicsTools/interface/ResourceRegistry.hpp create mode 100644 Tests/IncludeTest/GraphicsTools/ResourceRegistryH_test.cpp diff --git a/Graphics/GraphicsTools/CMakeLists.txt b/Graphics/GraphicsTools/CMakeLists.txt index 56fc212c9d..347781f8ec 100644 --- a/Graphics/GraphicsTools/CMakeLists.txt +++ b/Graphics/GraphicsTools/CMakeLists.txt @@ -12,6 +12,7 @@ set(INTERFACE interface/DurationQueryHelper.hpp interface/GraphicsUtilities.h interface/MapHelper.hpp + interface/ResourceRegistry.hpp interface/ScopedDebugGroup.hpp interface/GPUCompletionAwaitQueue.hpp interface/ScopedQueryHelper.hpp diff --git a/Graphics/GraphicsTools/interface/GraphicsUtilities.h b/Graphics/GraphicsTools/interface/GraphicsUtilities.h index b7dcd60977..e4f1dcad9d 100644 --- a/Graphics/GraphicsTools/interface/GraphicsUtilities.h +++ b/Graphics/GraphicsTools/interface/GraphicsUtilities.h @@ -154,6 +154,34 @@ void DILIGENT_GLOBAL_FUNCTION(CreateSparseTextureMtl)(IRenderDevice* pDev IDeviceMemory* pMemory, ITexture** ppTexture); +#if DILIGENT_CPP_INTERFACE + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultSRV)(ITexture* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultRTV)(ITexture* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultDSV)(ITexture* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultUAV)(ITexture* pTexture); + +IBufferView* DILIGENT_GLOBAL_FUNCTION(GetDefaultSRV)(IBuffer* pBuffer); + +IBufferView* DILIGENT_GLOBAL_FUNCTION(GetDefaultUAV)(IBuffer* pBuffer); + +#endif + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultSRV)(IObject* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultRTV)(IObject* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultDSV)(IObject* pTexture); + +ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultUAV)(IObject* pTexture); + +IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultSRV)(IObject* pBuffer); + +IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultUAV)(IObject* pBuffer); + #include "../../../Primitives/interface/UndefRefMacro.h" DILIGENT_END_NAMESPACE // namespace Diligent diff --git a/Graphics/GraphicsTools/interface/ResourceRegistry.hpp b/Graphics/GraphicsTools/interface/ResourceRegistry.hpp new file mode 100644 index 0000000000..c8de74edbd --- /dev/null +++ b/Graphics/GraphicsTools/interface/ResourceRegistry.hpp @@ -0,0 +1,146 @@ +/* + * Copyright 2024 Diligent Graphics LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#include + +#include "../../../Common/interface/RefCntAutoPtr.hpp" +#include "../../GraphicsEngine/interface/Buffer.h" +#include "../../GraphicsEngine/interface/Texture.h" +#include "GraphicsUtilities.h" + +namespace Diligent +{ + +template +class ResourceRegistry +{ +public: + using ResourceIdType = Uint32; + + ResourceRegistry() = default; + + ~ResourceRegistry() = default; + + void Insert(ResourceIdType Id, IDeviceObject* pObject); + + bool IsInitialized(ResourceIdType Id) const; + + ITexture* GetTexture(ResourceIdType Id) const; + + IBuffer* GetBuffer(ResourceIdType Id) const; + + ITextureView* GetTextureSRV(ResourceIdType Id) const; + + ITextureView* GetTextureRTV(ResourceIdType Id) const; + + ITextureView* GetTextureDSV(ResourceIdType Id) const; + + ITextureView* GetTextureUAV(ResourceIdType Id) const; + + IBufferView* GetBufferSRV(ResourceIdType Id) const; + + IBufferView* GetBufferUAV(ResourceIdType Id) const; + +private: + std::array, ResourceCount> m_Resources{}; +}; + +template +void ResourceRegistry::Insert(ResourceIdType Id, IDeviceObject* pObject) +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + m_Resources[Id] = pObject; +} + +template +bool ResourceRegistry::IsInitialized(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return m_Resources[Id] != nullptr; +} + +template +ITexture* ResourceRegistry::GetTexture(ResourceIdType Id) const +{ + RefCntAutoPtr{m_Resources[Id], IID_Texture}; + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + DEV_CHECK_ERR(RefCntAutoPtr(m_Resources[Id], IID_Texture), "Resource is not a texture"); + return StaticCast(m_Resources[Id]); +} +template + +IBuffer* ResourceRegistry::GetBuffer(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + DEV_CHECK_ERR(RefCntAutoPtr(m_Resources[Id], IID_Buffer), "Resource is not a buffer"); + return StaticCast(m_Resources[Id]); +} + +template +ITextureView* ResourceRegistry::GetTextureSRV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetTextureDefaultSRV(m_Resources[Id]); +} + +template +ITextureView* ResourceRegistry::GetTextureRTV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetTextureDefaultRTV(m_Resources[Id]); +} + +template +ITextureView* ResourceRegistry::GetTextureDSV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetTextureDefaultDSV(m_Resources[Id]); +} + +template +ITextureView* ResourceRegistry::GetTextureUAV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetTextureDefaultUAV(m_Resources[Id]); +} + +template +IBufferView* ResourceRegistry::GetBufferSRV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetBufferDefaultSRV(m_Resources[Id]); +} + +template +IBufferView* ResourceRegistry::GetBufferUAV(ResourceIdType Id) const +{ + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); + return GetBufferDefaultUAV(m_Resources[Id]); +} + +} // namespace Diligent diff --git a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp index 1e44805cd1..30ad9cd7f9 100644 --- a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp +++ b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp @@ -33,6 +33,7 @@ #include "DebugUtilities.hpp" #include "GraphicsAccessories.hpp" #include "ColorConversion.h" +#include "RefCntAutoPtr.hpp" #define PI_F 3.1415926f @@ -463,6 +464,88 @@ void CreateSparseTextureMtl(IRenderDevice* pDevice, } #endif +static ITextureView* ExtractTextureView(ITexture* pTexture, TEXTURE_VIEW_TYPE ViewType) +{ + DEV_CHECK_ERR(pTexture != nullptr, "pTexture must not be null"); + const auto pView = pTexture->GetDefaultView(ViewType); + DEV_CHECK_ERR(pView != nullptr, "pView must not be null"); + return pView; +} + +static IBufferView* ExtractBufferView(IBuffer* pBuffer, BUFFER_VIEW_TYPE ViewType) +{ + DEV_CHECK_ERR(pBuffer != nullptr, "pTexture must not be null"); + const auto pView = pBuffer->GetDefaultView(ViewType); + DEV_CHECK_ERR(pView != nullptr, "pView must not be null"); + return pView; +} + +ITextureView* GetDefaultSRV(ITexture* pTexture) +{ + return ExtractTextureView(pTexture, TEXTURE_VIEW_SHADER_RESOURCE); +} + +ITextureView* GetDefaultRTV(ITexture* pTexture) +{ + return ExtractTextureView(pTexture, TEXTURE_VIEW_RENDER_TARGET); +} + +ITextureView* GetDefaultDSV(ITexture* pTexture) +{ + return ExtractTextureView(pTexture, TEXTURE_VIEW_DEPTH_STENCIL); +} + +ITextureView* GetDefaultUAV(ITexture* pTexture) +{ + return ExtractTextureView(pTexture, TEXTURE_VIEW_UNORDERED_ACCESS); +} + +IBufferView* GetDefaultSRV(IBuffer* pBuffer) +{ + return ExtractBufferView(pBuffer, BUFFER_VIEW_SHADER_RESOURCE); +} + +IBufferView* GetDefaultUAV(IBuffer* pBuffer) +{ + return ExtractBufferView(pBuffer, BUFFER_VIEW_UNORDERED_ACCESS); +} + +ITextureView* GetTextureDefaultSRV(IObject* pTexture) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pTexture, IID_Texture), "Resource is not a texture"); + return GetDefaultSRV(reinterpret_cast(pTexture)); +} + +ITextureView* GetTextureDefaultRTV(IObject* pTexture) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pTexture, IID_Texture), "Resource is not a texture"); + return GetDefaultRTV(reinterpret_cast(pTexture)); +} + +ITextureView* GetTextureDefaultDSV(IObject* pTexture) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pTexture, IID_Texture), "Resource is not a texture"); + return GetDefaultDSV(reinterpret_cast(pTexture)); +} + +ITextureView* GetTextureDefaultUAV(IObject* pTexture) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pTexture, IID_Texture), "Resource is not a texture"); + return GetDefaultUAV(reinterpret_cast(pTexture)); +} + +IBufferView* GetBufferDefaultSRV(IObject* pBuffer) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pBuffer, IID_Buffer), "Resource is not a buffer"); + return GetDefaultSRV(reinterpret_cast(pBuffer)); +} + +IBufferView* GetBufferDefaultUAV(IObject* pBuffer) +{ + DEV_CHECK_ERR(RefCntAutoPtr(pBuffer, IID_Buffer), "Resource is not a buffer"); + return GetDefaultUAV(reinterpret_cast(pBuffer)); +} + } // namespace Diligent @@ -503,4 +586,35 @@ extern "C" { Diligent::CreateSparseTextureMtl(pDevice, TexDesc, pMemory, ppTexture); } + + + Diligent::ITextureView* Diligent_GetTextureDefaultSRV(Diligent::IObject* pTexture) + { + return Diligent::GetTextureDefaultSRV(pTexture); + } + + Diligent::ITextureView* Diligent_GetTextureDefaultRTV(Diligent::IObject* pTexture) + { + return Diligent::GetTextureDefaultRTV(pTexture); + } + + Diligent::ITextureView* Diligent_GetTextureDefaultDSV(Diligent::IObject* pTexture) + { + return Diligent::GetTextureDefaultDSV(pTexture); + } + + Diligent::ITextureView* Diligent_GetTextureDefaultUAV(Diligent::IObject* pTexture) + { + return Diligent::GetTextureDefaultUAV(pTexture); + } + + Diligent::IBufferView* Diligent_GetBufferDefaultSRV(Diligent::IObject* pBuffer) + { + return Diligent::GetBufferDefaultSRV(pBuffer); + } + + Diligent::IBufferView* Diligent_GetBufferDefaultRTV(Diligent::IObject* pBuffer) + { + return Diligent::GetBufferDefaultUAV(pBuffer); + } } diff --git a/Tests/IncludeTest/GraphicsTools/ResourceRegistryH_test.cpp b/Tests/IncludeTest/GraphicsTools/ResourceRegistryH_test.cpp new file mode 100644 index 0000000000..776e4b63b1 --- /dev/null +++ b/Tests/IncludeTest/GraphicsTools/ResourceRegistryH_test.cpp @@ -0,0 +1,27 @@ +/* + * Copyright 2024 Diligent Graphics LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "DiligentCore/Graphics/GraphicsTools/interface/ResourceRegistry.hpp"