|
| 1 | +/* |
| 2 | + * Copyright 2024 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#pragma once |
| 28 | + |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include "../../../Common/interface/RefCntAutoPtr.hpp" |
| 32 | +#include "../../GraphicsEngine/interface/Buffer.h" |
| 33 | +#include "../../GraphicsEngine/interface/Texture.h" |
| 34 | +#include "GraphicsUtilities.h" |
| 35 | + |
| 36 | +namespace Diligent |
| 37 | +{ |
| 38 | + |
| 39 | +/// Helper class that facilitates resource management. |
| 40 | +class ResourceRegistry |
| 41 | +{ |
| 42 | +public: |
| 43 | + using ResourceIdType = Uint32; |
| 44 | + |
| 45 | + ResourceRegistry() = default; |
| 46 | + ~ResourceRegistry() = default; |
| 47 | + |
| 48 | + explicit ResourceRegistry(size_t ResourceCount) : |
| 49 | + m_Resources(ResourceCount) |
| 50 | + {} |
| 51 | + |
| 52 | + void SetSize(size_t ResourceCount) |
| 53 | + { |
| 54 | + m_Resources.resize(ResourceCount); |
| 55 | + } |
| 56 | + |
| 57 | + void Insert(ResourceIdType Id, IDeviceObject* pObject) |
| 58 | + { |
| 59 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 60 | + m_Resources[Id] = pObject; |
| 61 | + } |
| 62 | + |
| 63 | + bool IsInitialized(ResourceIdType Id) const |
| 64 | + { |
| 65 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 66 | + return m_Resources[Id] != nullptr; |
| 67 | + } |
| 68 | + |
| 69 | + ITexture* GetTexture(ResourceIdType Id) const |
| 70 | + { |
| 71 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 72 | + DEV_CHECK_ERR(RefCntAutoPtr<ITexture>(m_Resources[Id], IID_Texture), "Resource is not a texture"); |
| 73 | + return StaticCast<ITexture*>(m_Resources[Id]); |
| 74 | + } |
| 75 | + |
| 76 | + IBuffer* GetBuffer(ResourceIdType Id) const |
| 77 | + { |
| 78 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 79 | + DEV_CHECK_ERR(RefCntAutoPtr<IBuffer>(m_Resources[Id], IID_Buffer), "Resource is not a buffer"); |
| 80 | + return StaticCast<IBuffer*>(m_Resources[Id]); |
| 81 | + } |
| 82 | + |
| 83 | + ITextureView* GetTextureSRV(ResourceIdType Id) const |
| 84 | + { |
| 85 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 86 | + return GetTextureDefaultSRV(m_Resources[Id]); |
| 87 | + } |
| 88 | + |
| 89 | + ITextureView* GetTextureRTV(ResourceIdType Id) const |
| 90 | + { |
| 91 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 92 | + return GetTextureDefaultRTV(m_Resources[Id]); |
| 93 | + } |
| 94 | + |
| 95 | + ITextureView* GetTextureDSV(ResourceIdType Id) const |
| 96 | + { |
| 97 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 98 | + return GetTextureDefaultDSV(m_Resources[Id]); |
| 99 | + } |
| 100 | + |
| 101 | + ITextureView* GetTextureUAV(ResourceIdType Id) const |
| 102 | + { |
| 103 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 104 | + return GetTextureDefaultUAV(m_Resources[Id]); |
| 105 | + } |
| 106 | + |
| 107 | + IBufferView* GetBufferSRV(ResourceIdType Id) const |
| 108 | + { |
| 109 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 110 | + return GetBufferDefaultSRV(m_Resources[Id]); |
| 111 | + } |
| 112 | + |
| 113 | + IBufferView* GetBufferUAV(ResourceIdType Id) const |
| 114 | + { |
| 115 | + DEV_CHECK_ERR(Id < m_Resources.size(), "Resource index is out of range"); |
| 116 | + return GetBufferDefaultUAV(m_Resources[Id]); |
| 117 | + } |
| 118 | + |
| 119 | +private: |
| 120 | + std::vector<RefCntAutoPtr<IDeviceObject>> m_Resources; |
| 121 | +}; |
| 122 | + |
| 123 | +} // namespace Diligent |
0 commit comments