Diligent Engine  v.2.4.g
ShaderResourceCacheGL.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include <array>
31 #include <vector>
32 
33 #include "BufferGLImpl.hpp"
34 #include "TextureBaseGL.hpp"
35 #include "SamplerGLImpl.hpp"
37 
38 namespace Diligent
39 {
40 
42 // All resources are stored in the continuous memory using the following layout:
43 //
44 // | Cached UBs | Cached Textures | Cached Images | Cached Storage Blocks |
45 // |----------------------------------------------------|--------------------------|---------------------------|
46 // | 0 | 1 | ... | UBCount-1 | 0 | 1 | ...| SmpCount-1 | 0 | 1 | ... | ImgCount-1 | 0 | 1 | ... | SBOCount-1 |
47 // -----------------------------------------------------------------------------------------------------------
48 //
50 {
51 public:
52  explicit ShaderResourceCacheGL(ResourceCacheContentType ContentType) noexcept :
53  m_ContentType{ContentType}
54  {}
55 
57 
58  // clang-format off
63  // clang-format on
64 
66  struct CachedUB
67  {
70  };
71 
74  {
81 
82  TextureBaseGL* pTexture = nullptr;
83  union
84  {
85  BufferGLImpl* pBuffer = nullptr; // When pTexture == nullptr
86  SamplerGLImpl* pSampler; // When pTexture != nullptr
87  };
88  CachedResourceView() noexcept {}
89 
91  {
92  // Do not null out pSampler as it could've been initialized by PipelineResourceSignatureGLImpl::InitSRBResourceCache!
93  // pSampler = nullptr;
94 
95  // Avoid unnecessary virtual call
96  pTexture = pTexView ? pTexView->GetTexture<TextureBaseGL>() : nullptr;
97  if (pTexView && SetSampler)
98  {
99  pSampler = ValidatedCast<SamplerGLImpl>(pTexView->GetSampler());
100  }
101 
102  pView = std::move(pTexView);
103  }
104 
106  {
107  pTexture = nullptr;
108  // Avoid unnecessary virtual call
109  pBuffer = pBufView ? pBufView->GetBuffer<BufferGLImpl>() : nullptr;
110  pView = std::move(pBufView);
111  }
112  };
113 
114  struct CachedSSBO
115  {
118  };
119 
120  using TResourceCount = std::array<Uint16, 4>; // same as PipelineResourceSignatureGLImpl::TBindings.
121  static size_t GetRequriedMemorySize(const TResourceCount& ResCount);
122 
123  void Initialize(const TResourceCount& Count, IMemoryAllocator& MemAllocator);
124 
126  {
127  GetUB(CacheOffset).pBuffer = std::move(pBuff);
128  }
129 
130  void SetTexture(Uint32 CacheOffset, RefCntAutoPtr<TextureViewGLImpl>&& pTexView, bool SetSampler)
131  {
132  GetTexture(CacheOffset).Set(std::move(pTexView), SetSampler);
133  }
134 
135  void SetSampler(Uint32 CacheOffset, ISampler* pSampler)
136  {
137  GetTexture(CacheOffset).pSampler = ValidatedCast<SamplerGLImpl>(pSampler);
138  }
139 
141  {
142  GetTexture(CacheOffset).Set(std::move(pBuffView));
143  }
144 
146  {
147  GetImage(CacheOffset).Set(std::move(pTexView), false);
148  }
149 
150  void SetBufImage(Uint32 CacheOffset, RefCntAutoPtr<BufferViewGLImpl>&& pBuffView)
151  {
152  GetImage(CacheOffset).Set(std::move(pBuffView));
153  }
154 
155  void SetSSBO(Uint32 CacheOffset, RefCntAutoPtr<BufferViewGLImpl>&& pBuffView)
156  {
157  GetSSBO(CacheOffset).pBufferView = std::move(pBuffView);
158  }
159 
160  bool IsUBBound(Uint32 CacheOffset) const
161  {
162  if (CacheOffset >= GetUBCount())
163  return false;
164 
165  const auto& UB = GetConstUB(CacheOffset);
166  return UB.pBuffer;
167  }
168 
169  bool IsTextureBound(Uint32 CacheOffset, bool dbgIsTextureView) const
170  {
171  if (CacheOffset >= GetTextureCount())
172  return false;
173 
174  const auto& Texture = GetConstTexture(CacheOffset);
175  VERIFY_EXPR(dbgIsTextureView || Texture.pTexture == nullptr);
176  return Texture.pView;
177  }
178 
179  bool IsImageBound(Uint32 CacheOffset, bool dbgIsTextureView) const
180  {
181  if (CacheOffset >= GetImageCount())
182  return false;
183 
184  const auto& Image = GetConstImage(CacheOffset);
185  VERIFY_EXPR(dbgIsTextureView || Image.pTexture == nullptr);
186  return Image.pView;
187  }
188 
189  bool IsSSBOBound(Uint32 CacheOffset) const
190  {
191  if (CacheOffset >= GetSSBOCount())
192  return false;
193 
194  const auto& SSBO = GetConstSSBO(CacheOffset);
195  return SSBO.pBufferView;
196  }
197 
198  // clang-format off
199  Uint32 GetUBCount() const { return (m_TexturesOffset - m_UBsOffset) / sizeof(CachedUB); }
200  Uint32 GetTextureCount() const { return (m_ImagesOffset - m_TexturesOffset) / sizeof(CachedResourceView); }
201  Uint32 GetImageCount() const { return (m_SSBOsOffset - m_ImagesOffset) / sizeof(CachedResourceView); }
202  Uint32 GetSSBOCount() const { return (m_MemoryEndOffset - m_SSBOsOffset) / sizeof(CachedSSBO); }
203  // clang-format on
204 
205  const CachedUB& GetConstUB(Uint32 CacheOffset) const
206  {
207  VERIFY(CacheOffset < GetUBCount(), "Uniform buffer index (", CacheOffset, ") is out of range");
208  return reinterpret_cast<CachedUB*>(m_pResourceData + m_UBsOffset)[CacheOffset];
209  }
210 
211  const CachedResourceView& GetConstTexture(Uint32 CacheOffset) const
212  {
213  VERIFY(CacheOffset < GetTextureCount(), "Texture index (", CacheOffset, ") is out of range");
214  return reinterpret_cast<CachedResourceView*>(m_pResourceData + m_TexturesOffset)[CacheOffset];
215  }
216 
217  const CachedResourceView& GetConstImage(Uint32 CacheOffset) const
218  {
219  VERIFY(CacheOffset < GetImageCount(), "Image buffer index (", CacheOffset, ") is out of range");
220  return reinterpret_cast<CachedResourceView*>(m_pResourceData + m_ImagesOffset)[CacheOffset];
221  }
222 
223  const CachedSSBO& GetConstSSBO(Uint32 CacheOffset) const
224  {
225  VERIFY(CacheOffset < GetSSBOCount(), "Shader storage block index (", CacheOffset, ") is out of range");
226  return reinterpret_cast<CachedSSBO*>(m_pResourceData + m_SSBOsOffset)[CacheOffset];
227  }
228 
229  bool IsInitialized() const
230  {
231  return m_MemoryEndOffset != InvalidResourceOffset;
232  }
233 
234  ResourceCacheContentType GetContentType() const { return m_ContentType; }
235 
236 #ifdef DILIGENT_DEVELOPMENT
237  void SetStaticResourcesInitialized()
238  {
239  m_bStaticResourcesInitialized = true;
240  }
241  bool StaticResourcesInitialized() const { return m_bStaticResourcesInitialized; }
242 #endif
243 
244  void BindResources(GLContextState& GLState,
245  const std::array<Uint16, 4>& BaseBindings,
246  std::vector<TextureBaseGL*>& WritableTextures,
247  std::vector<BufferGLImpl*>& WritableBuffers) const;
248 
249 private:
250  CachedUB& GetUB(Uint32 CacheOffset)
251  {
252  return const_cast<CachedUB&>(const_cast<const ShaderResourceCacheGL*>(this)->GetConstUB(CacheOffset));
253  }
254 
255  CachedResourceView& GetTexture(Uint32 CacheOffset)
256  {
257  return const_cast<CachedResourceView&>(const_cast<const ShaderResourceCacheGL*>(this)->GetConstTexture(CacheOffset));
258  }
259 
260  CachedResourceView& GetImage(Uint32 CacheOffset)
261  {
262  return const_cast<CachedResourceView&>(const_cast<const ShaderResourceCacheGL*>(this)->GetConstImage(CacheOffset));
263  }
264 
265  CachedSSBO& GetSSBO(Uint32 CacheOffset)
266  {
267  return const_cast<CachedSSBO&>(const_cast<const ShaderResourceCacheGL*>(this)->GetConstSSBO(CacheOffset));
268  }
269 
270 private:
271  static constexpr const Uint16 InvalidResourceOffset = 0xFFFF;
272  static constexpr const Uint16 m_UBsOffset = 0;
273 
274  Uint16 m_TexturesOffset = InvalidResourceOffset;
275  Uint16 m_ImagesOffset = InvalidResourceOffset;
276  Uint16 m_SSBOsOffset = InvalidResourceOffset;
277  Uint16 m_MemoryEndOffset = InvalidResourceOffset;
278 
279  Uint8* m_pResourceData = nullptr;
280  IMemoryAllocator* m_pAllocator = nullptr;
281 
282  // Indicates what types of resources are stored in the cache
283  const ResourceCacheContentType m_ContentType;
284 
285 #ifdef DILIGENT_DEVELOPMENT
286  bool m_bStaticResourcesInitialized = false;
287 #endif
288 };
289 
290 } // namespace Diligent
Diligent::ResourceCacheContentType
ResourceCacheContentType
The type of the content that is stored in the shader resource cache.
Definition: ShaderResourceCacheCommon.hpp:39
ShaderResourceCacheCommon.hpp
Diligent::ShaderResourceCacheGL::IsUBBound
bool IsUBBound(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:160
Diligent::ShaderResourceCacheGL::GetConstUB
const CachedUB & GetConstUB(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:205
Diligent::ISampler
Texture sampler interface.
Definition: Sampler.h:192
SamplerGLImpl.hpp
Diligent::ShaderResourceCacheGL::GetUBCount
Uint32 GetUBCount() const
Definition: ShaderResourceCacheGL.hpp:199
Diligent::ShaderResourceCacheGL::CachedResourceView::pView
RefCntAutoPtr< IDeviceObject > pView
We keep strong reference to the view instead of the reference to the texture or buffer because this i...
Definition: ShaderResourceCacheGL.hpp:80
Diligent::ShaderResourceCacheGL::CachedResourceView::pTexture
TextureBaseGL * pTexture
Definition: ShaderResourceCacheGL.hpp:82
Diligent::ShaderResourceCacheGL::SetSSBO
void SetSSBO(Uint32 CacheOffset, RefCntAutoPtr< BufferViewGLImpl > &&pBuffView)
Definition: ShaderResourceCacheGL.hpp:155
Diligent::ShaderResourceCacheGL
The class implements a cache that holds resources bound to a specific GL program.
Definition: ShaderResourceCacheGL.hpp:49
Diligent::ShaderResourceCacheGL::~ShaderResourceCacheGL
~ShaderResourceCacheGL()
Definition: ShaderResourceCacheGL.cpp:94
Diligent::ShaderResourceCacheGL::SetTexImage
void SetTexImage(Uint32 CacheOffset, RefCntAutoPtr< TextureViewGLImpl > &&pTexView)
Definition: ShaderResourceCacheGL.hpp:145
Diligent::ShaderResourceCacheGL::GetTextureCount
Uint32 GetTextureCount() const
Definition: ShaderResourceCacheGL.hpp:200
Diligent::ShaderResourceCacheGL::SetSampler
void SetSampler(Uint32 CacheOffset, ISampler *pSampler)
Definition: ShaderResourceCacheGL.hpp:135
Diligent::ShaderResourceCacheGL::Initialize
void Initialize(const TResourceCount &Count, IMemoryAllocator &MemAllocator)
Definition: ShaderResourceCacheGL.cpp:52
Diligent::ShaderResourceCacheGL::GetContentType
ResourceCacheContentType GetContentType() const
Definition: ShaderResourceCacheGL.hpp:234
Diligent::ShaderResourceCacheGL::CachedResourceView::Set
void Set(RefCntAutoPtr< BufferViewGLImpl > &&pBufView)
Definition: ShaderResourceCacheGL.hpp:105
Diligent::ShaderResourceCacheGL::SetBufImage
void SetBufImage(Uint32 CacheOffset, RefCntAutoPtr< BufferViewGLImpl > &&pBuffView)
Definition: ShaderResourceCacheGL.hpp:150
Diligent::ShaderResourceCacheGL::IsImageBound
bool IsImageBound(Uint32 CacheOffset, bool dbgIsTextureView) const
Definition: ShaderResourceCacheGL.hpp:179
Diligent::ShaderResourceCacheGL::CachedUB::pBuffer
RefCntAutoPtr< BufferGLImpl > pBuffer
Strong reference to the buffer.
Definition: ShaderResourceCacheGL.hpp:69
Diligent::ShaderResourceCacheGL::CachedSSBO
Definition: ShaderResourceCacheGL.hpp:114
TextureBaseGL.hpp
Diligent::ShaderResourceCacheGL::CachedResourceView
Describes a resource bound to a sampler or an image slot.
Definition: ShaderResourceCacheGL.hpp:73
Diligent::ShaderResourceCacheGL::CachedResourceView::CachedResourceView
CachedResourceView() noexcept
Definition: ShaderResourceCacheGL.hpp:88
Diligent::ShaderResourceCacheGL::IsTextureBound
bool IsTextureBound(Uint32 CacheOffset, bool dbgIsTextureView) const
Definition: ShaderResourceCacheGL.hpp:169
Diligent::ShaderResourceCacheGL::operator=
ShaderResourceCacheGL & operator=(const ShaderResourceCacheGL &)=delete
Diligent::ShaderResourceCacheGL::GetConstTexture
const CachedResourceView & GetConstTexture(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:211
Diligent::RefCntAutoPtr
Template class that implements reference counting.
Definition: RefCntAutoPtr.hpp:73
Diligent::TextureBaseGL
Base implementation of a texture object in OpenGL backend.
Definition: TextureBaseGL.hpp:41
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ShaderResourceCacheGL::BindResources
void BindResources(GLContextState &GLState, const std::array< Uint16, 4 > &BaseBindings, std::vector< TextureBaseGL * > &WritableTextures, std::vector< BufferGLImpl * > &WritableBuffers) const
Definition: ShaderResourceCacheGL.cpp:121
Diligent::BufferGLImpl
Buffer object implementation in OpenGL backend.
Definition: BufferGLImpl.hpp:41
Diligent::ShaderResourceCacheGL::CachedResourceView::pSampler
SamplerGLImpl * pSampler
Definition: ShaderResourceCacheGL.hpp:86
Diligent::ShaderResourceCacheGL::CachedUB
Describes a resource bound to a uniform buffer or a shader storage block slot.
Definition: ShaderResourceCacheGL.hpp:66
Diligent::ShaderResourceCacheGL::GetConstImage
const CachedResourceView & GetConstImage(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:217
Diligent::ShaderResourceCacheGL::SetUniformBuffer
void SetUniformBuffer(Uint32 CacheOffset, RefCntAutoPtr< BufferGLImpl > &&pBuff)
Definition: ShaderResourceCacheGL.hpp:125
Diligent::IMemoryAllocator
Base interface for a raw memory allocator.
Definition: MemoryAllocator.h:41
BufferGLImpl.hpp
Diligent::ShaderResourceCacheGL::ShaderResourceCacheGL
ShaderResourceCacheGL(ResourceCacheContentType ContentType) noexcept
Definition: ShaderResourceCacheGL.hpp:52
Diligent::ShaderResourceCacheGL::CachedSSBO::pBufferView
RefCntAutoPtr< BufferViewGLImpl > pBufferView
Strong reference to the buffer.
Definition: ShaderResourceCacheGL.hpp:117
Diligent::Uint16
uint16_t Uint16
16-bit unsigned integer
Definition: BasicTypes.h:52
Diligent::ShaderResourceCacheGL::SetTexture
void SetTexture(Uint32 CacheOffset, RefCntAutoPtr< TextureViewGLImpl > &&pTexView, bool SetSampler)
Definition: ShaderResourceCacheGL.hpp:130
Diligent::ShaderResourceCacheGL::GetRequriedMemorySize
static size_t GetRequriedMemorySize(const TResourceCount &ResCount)
Definition: ShaderResourceCacheGL.cpp:37
Diligent::ShaderResourceCacheGL::IsSSBOBound
bool IsSSBOBound(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:189
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
Diligent::ShaderResourceCacheGL::GetSSBOCount
Uint32 GetSSBOCount() const
Definition: ShaderResourceCacheGL.hpp:202
Diligent::ShaderResourceCacheGL::IsInitialized
bool IsInitialized() const
Definition: ShaderResourceCacheGL.hpp:229
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::ShaderResourceCacheGL::GetImageCount
Uint32 GetImageCount() const
Definition: ShaderResourceCacheGL.hpp:201
Diligent::ShaderResourceCacheGL::SetTexelBuffer
void SetTexelBuffer(Uint32 CacheOffset, RefCntAutoPtr< BufferViewGLImpl > &&pBuffView)
Definition: ShaderResourceCacheGL.hpp:140
Diligent::ShaderResourceCacheGL::GetConstSSBO
const CachedSSBO & GetConstSSBO(Uint32 CacheOffset) const
Definition: ShaderResourceCacheGL.hpp:223
Diligent::ShaderResourceCacheGL::TResourceCount
std::array< Uint16, 4 > TResourceCount
Definition: ShaderResourceCacheGL.hpp:120
Diligent::SamplerGLImpl
Sampler implementation in OpenGL backend.
Definition: SamplerGLImpl.hpp:38
Diligent::ShaderResourceCacheGL::CachedResourceView::pBuffer
BufferGLImpl * pBuffer
Definition: ShaderResourceCacheGL.hpp:85
Diligent::ShaderResourceCacheGL::CachedResourceView::Set
void Set(RefCntAutoPtr< TextureViewGLImpl > &&pTexView, bool SetSampler)
Definition: ShaderResourceCacheGL.hpp:90
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37