Skip to content

Commit c460b56

Browse files
MikhailGorobetsTheMostDiligent
authored andcommitted
GraphicsTools: Added resource registry (close #452)
1 parent 916a166 commit c460b56

5 files changed

Lines changed: 313 additions & 2 deletions

File tree

Graphics/GraphicsTools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(INTERFACE
1212
interface/DurationQueryHelper.hpp
1313
interface/GraphicsUtilities.h
1414
interface/MapHelper.hpp
15+
interface/ResourceRegistry.hpp
1516
interface/ScopedDebugGroup.hpp
1617
interface/GPUCompletionAwaitQueue.hpp
1718
interface/ScopedQueryHelper.hpp

Graphics/GraphicsTools/interface/GraphicsUtilities.h

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -154,6 +154,58 @@ void DILIGENT_GLOBAL_FUNCTION(CreateSparseTextureMtl)(IRenderDevice* pDev
154154
IDeviceMemory* pMemory,
155155
ITexture** ppTexture);
156156

157+
#if DILIGENT_CPP_INTERFACE
158+
159+
/// Returns default shader resource view of a texture.
160+
/// If the texture is null, returns null.
161+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultSRV)(ITexture* pTexture);
162+
163+
/// Returns default render target view of a texture.
164+
/// If the texture is null, returns null.
165+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultRTV)(ITexture* pTexture);
166+
167+
/// Returns default depth-stencil view of a texture.
168+
/// If the texture is null, returns null.
169+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultDSV)(ITexture* pTexture);
170+
171+
/// Returns default unordered access view of a texture.
172+
/// If the texture is null, returns null.
173+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetDefaultUAV)(ITexture* pTexture);
174+
175+
/// Returns default shader resource view of a buffer.
176+
/// If the buffer is null, returns null.
177+
IBufferView* DILIGENT_GLOBAL_FUNCTION(GetDefaultSRV)(IBuffer* pBuffer);
178+
179+
/// Returns default unordered access view of a buffer.
180+
/// If the buffer is null, returns null.
181+
IBufferView* DILIGENT_GLOBAL_FUNCTION(GetDefaultUAV)(IBuffer* pBuffer);
182+
183+
#endif
184+
185+
/// Returns default shader resource view of a texture.
186+
/// If the texture is null, returns null.
187+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultSRV)(IObject* pTexture);
188+
189+
/// Returns default render target view of a texture.
190+
/// If the texture is null, returns null.
191+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultRTV)(IObject* pTexture);
192+
193+
/// Returns default depth-stencil view of a texture.
194+
/// If the texture is null, returns null.
195+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultDSV)(IObject* pTexture);
196+
197+
/// Returns default unordered access view of a texture.
198+
/// If the texture is null, returns null.
199+
ITextureView* DILIGENT_GLOBAL_FUNCTION(GetTextureDefaultUAV)(IObject* pTexture);
200+
201+
/// Returns default shader resource view of a buffer.
202+
/// If the buffer is null, returns null.
203+
IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultSRV)(IObject* pBuffer);
204+
205+
/// Returns default unordered access view of a buffer.
206+
/// If the buffer is null, returns null.
207+
IBufferView* DILIGENT_GLOBAL_FUNCTION(GetBufferDefaultUAV)(IObject* pBuffer);
208+
157209
#include "../../../Primitives/interface/UndefRefMacro.h"
158210

159211
DILIGENT_END_NAMESPACE // namespace Diligent
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Graphics/GraphicsTools/src/GraphicsUtilities.cpp

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,6 +33,7 @@
3333
#include "DebugUtilities.hpp"
3434
#include "GraphicsAccessories.hpp"
3535
#include "ColorConversion.h"
36+
#include "RefCntAutoPtr.hpp"
3637

3738
#define PI_F 3.1415926f
3839

@@ -463,6 +464,82 @@ void CreateSparseTextureMtl(IRenderDevice* pDevice,
463464
}
464465
#endif
465466

467+
inline ITextureView* ExtractTextureView(ITexture* pTexture, TEXTURE_VIEW_TYPE ViewType)
468+
{
469+
return pTexture != nullptr ? pTexture->GetDefaultView(ViewType) : nullptr;
470+
}
471+
472+
inline IBufferView* ExtractBufferView(IBuffer* pBuffer, BUFFER_VIEW_TYPE ViewType)
473+
{
474+
return pBuffer != nullptr ? pBuffer->GetDefaultView(ViewType) : nullptr;
475+
}
476+
477+
ITextureView* GetDefaultSRV(ITexture* pTexture)
478+
{
479+
return ExtractTextureView(pTexture, TEXTURE_VIEW_SHADER_RESOURCE);
480+
}
481+
482+
ITextureView* GetDefaultRTV(ITexture* pTexture)
483+
{
484+
return ExtractTextureView(pTexture, TEXTURE_VIEW_RENDER_TARGET);
485+
}
486+
487+
ITextureView* GetDefaultDSV(ITexture* pTexture)
488+
{
489+
return ExtractTextureView(pTexture, TEXTURE_VIEW_DEPTH_STENCIL);
490+
}
491+
492+
ITextureView* GetDefaultUAV(ITexture* pTexture)
493+
{
494+
return ExtractTextureView(pTexture, TEXTURE_VIEW_UNORDERED_ACCESS);
495+
}
496+
497+
IBufferView* GetDefaultSRV(IBuffer* pBuffer)
498+
{
499+
return ExtractBufferView(pBuffer, BUFFER_VIEW_SHADER_RESOURCE);
500+
}
501+
502+
IBufferView* GetDefaultUAV(IBuffer* pBuffer)
503+
{
504+
return ExtractBufferView(pBuffer, BUFFER_VIEW_UNORDERED_ACCESS);
505+
}
506+
507+
ITextureView* GetTextureDefaultSRV(IObject* pTexture)
508+
{
509+
DEV_CHECK_ERR(pTexture == nullptr || RefCntAutoPtr<ITexture>(pTexture, IID_Texture), "Resource is not a texture");
510+
return GetDefaultSRV(static_cast<ITexture*>(pTexture));
511+
}
512+
513+
ITextureView* GetTextureDefaultRTV(IObject* pTexture)
514+
{
515+
DEV_CHECK_ERR(pTexture == nullptr || RefCntAutoPtr<ITexture>(pTexture, IID_Texture), "Resource is not a texture");
516+
return GetDefaultRTV(static_cast<ITexture*>(pTexture));
517+
}
518+
519+
ITextureView* GetTextureDefaultDSV(IObject* pTexture)
520+
{
521+
DEV_CHECK_ERR(pTexture == nullptr || RefCntAutoPtr<ITexture>(pTexture, IID_Texture), "Resource is not a texture");
522+
return GetDefaultDSV(static_cast<ITexture*>(pTexture));
523+
}
524+
525+
ITextureView* GetTextureDefaultUAV(IObject* pTexture)
526+
{
527+
DEV_CHECK_ERR(pTexture == nullptr || RefCntAutoPtr<ITexture>(pTexture, IID_Texture), "Resource is not a texture");
528+
return GetDefaultUAV(static_cast<ITexture*>(pTexture));
529+
}
530+
531+
IBufferView* GetBufferDefaultSRV(IObject* pBuffer)
532+
{
533+
DEV_CHECK_ERR(pBuffer == nullptr || RefCntAutoPtr<IBuffer>(pBuffer, IID_Buffer), "Resource is not a buffer");
534+
return GetDefaultSRV(static_cast<IBuffer*>(pBuffer));
535+
}
536+
537+
IBufferView* GetBufferDefaultUAV(IObject* pBuffer)
538+
{
539+
DEV_CHECK_ERR(pBuffer == nullptr || RefCntAutoPtr<IBuffer>(pBuffer, IID_Buffer), "Resource is not a buffer");
540+
return GetDefaultUAV(static_cast<IBuffer*>(pBuffer));
541+
}
542+
466543
} // namespace Diligent
467544

468545

@@ -503,4 +580,35 @@ extern "C"
503580
{
504581
Diligent::CreateSparseTextureMtl(pDevice, TexDesc, pMemory, ppTexture);
505582
}
583+
584+
585+
Diligent::ITextureView* Diligent_GetTextureDefaultSRV(Diligent::IObject* pTexture)
586+
{
587+
return Diligent::GetTextureDefaultSRV(pTexture);
588+
}
589+
590+
Diligent::ITextureView* Diligent_GetTextureDefaultRTV(Diligent::IObject* pTexture)
591+
{
592+
return Diligent::GetTextureDefaultRTV(pTexture);
593+
}
594+
595+
Diligent::ITextureView* Diligent_GetTextureDefaultDSV(Diligent::IObject* pTexture)
596+
{
597+
return Diligent::GetTextureDefaultDSV(pTexture);
598+
}
599+
600+
Diligent::ITextureView* Diligent_GetTextureDefaultUAV(Diligent::IObject* pTexture)
601+
{
602+
return Diligent::GetTextureDefaultUAV(pTexture);
603+
}
604+
605+
Diligent::IBufferView* Diligent_GetBufferDefaultSRV(Diligent::IObject* pBuffer)
606+
{
607+
return Diligent::GetBufferDefaultSRV(pBuffer);
608+
}
609+
610+
Diligent::IBufferView* Diligent_GetBufferDefaultRTV(Diligent::IObject* pBuffer)
611+
{
612+
return Diligent::GetBufferDefaultUAV(pBuffer);
613+
}
506614
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include "DiligentCore/Graphics/GraphicsTools/interface/ResourceRegistry.hpp"

0 commit comments

Comments
 (0)