Diligent Engine API Reference
ShaderResourceLayoutD3D11.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
28 
29 // Set this define to 1 to use unordered_map to store shader variables.
30 // Note that sizeof(m_VariableHash)==128 (release mode, MS compiler, x64).
31 #define USE_VARIABLE_HASH_MAP 0
32 
33 
34 #include "ShaderResources.h"
35 #include "ShaderBase.h"
36 #include "ShaderResourceCacheD3D11.h"
37 #include "EngineD3D11Defines.h"
38 #include "STDAllocator.h"
39 #include "ShaderVariableD3DBase.h"
40 #include "ShaderResourcesD3D11.h"
41 
42 namespace Diligent
43 {
44 
45 class IMemoryAllocator;
46 
50 {
51 public:
52  ShaderResourceLayoutD3D11(IObject &Owner, IMemoryAllocator& ResLayoutDataAllocator);
54 
55  // No copies or moves
57  ShaderResourceLayoutD3D11& operator = (const ShaderResourceLayoutD3D11&) = delete;
60 
61  void Initialize(const std::shared_ptr<const ShaderResourcesD3D11>& pSrcResources,
62  const SHADER_VARIABLE_TYPE *VarTypes,
63  Uint32 NumVarTypes,
64  ShaderResourceCacheD3D11& ResourceCache,
65  IMemoryAllocator& ResCacheDataAllocator,
66  IMemoryAllocator& ResLayoutDataAllocator);
67 
68  void CopyResources(ShaderResourceCacheD3D11 &DstCache);
69 
70  using ShaderVariableD3D11Base = ShaderVariableD3DBase<ShaderResourceLayoutD3D11>;
71 
72  struct ConstBuffBindInfo : ShaderVariableD3D11Base
73  {
74  ConstBuffBindInfo( const D3DShaderResourceAttribs& ResourceAttribs,
75  ShaderResourceLayoutD3D11 &ParentResLayout ) :
76  ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs)
77  {}
78  // Non-virtual function
79  __forceinline void BindResource(IDeviceObject *pObject, Uint32 ArrayIndex, const ShaderResourceLayoutD3D11 *dbgResLayout);
80  virtual void Set(IDeviceObject *pObject)override final{ BindResource(pObject, 0, nullptr); }
81 
82  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
83  {
84  for(Uint32 elem=0; elem < NumElements; ++elem)
85  BindResource(ppObjects[elem], FirstElement+elem, nullptr);
86  }
87 
88  __forceinline bool IsBound(Uint32 ArrayIndex);
89  };
90 
91  struct TexAndSamplerBindInfo : ShaderVariableD3D11Base
92  {
93  TexAndSamplerBindInfo( const D3DShaderResourceAttribs& _TextureAttribs,
94  const D3DShaderResourceAttribs& _SamplerAttribs,
95  ShaderResourceLayoutD3D11 &ParentResLayout) :
96  ShaderVariableD3D11Base(ParentResLayout, _TextureAttribs),
97  SamplerAttribs(_SamplerAttribs)
98  {}
99 
100  // Non-virtual function
101  __forceinline void BindResource(IDeviceObject *pObject, Uint32 ArrayIndex, const ShaderResourceLayoutD3D11 *dbgResLayout);
102  virtual void Set(IDeviceObject *pObject)override final{ BindResource(pObject, 0, nullptr); }
103 
104  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
105  {
106  for(Uint32 elem=0; elem < NumElements; ++elem)
107  BindResource(ppObjects[elem], FirstElement+elem, nullptr);
108  }
109 
110  __forceinline bool IsBound(Uint32 ArrayIndex);
111 
112  static const D3DShaderResourceAttribs InvalidSamplerAttribs;
113 
114  const D3DShaderResourceAttribs &SamplerAttribs;
115  };
116 
117  struct TexUAVBindInfo : ShaderVariableD3D11Base
118  {
119  TexUAVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs,
120  ShaderResourceLayoutD3D11 &ParentResLayout ) :
121  ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs)
122  {}
123 
124  // Provide non-virtual function
125  __forceinline void BindResource(IDeviceObject *pObject, Uint32 ArrayIndex, const ShaderResourceLayoutD3D11 *dbgResLayout);
126  virtual void Set(IDeviceObject *pObject)override final{ BindResource(pObject, 0, nullptr); }
127 
128  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
129  {
130  for(Uint32 elem=0; elem < NumElements; ++elem)
131  BindResource(ppObjects[elem], FirstElement+elem, nullptr);
132  }
133 
134  __forceinline bool IsBound(Uint32 ArrayIndex);
135  };
136 
137  struct BuffUAVBindInfo : ShaderVariableD3D11Base
138  {
139  BuffUAVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs,
140  ShaderResourceLayoutD3D11 &ParentResLayout ) :
141  ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs)
142  {}
143 
144  // Non-virtual function
145  __forceinline void BindResource(IDeviceObject *pObject, Uint32 ArrayIndex, const ShaderResourceLayoutD3D11 *dbgResLayout);
146  virtual void Set(IDeviceObject *pObject)override final{ BindResource(pObject, 0, nullptr); }
147 
148  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
149  {
150  for(Uint32 elem=0; elem < NumElements; ++elem)
151  BindResource(ppObjects[elem], FirstElement+elem, nullptr);
152  }
153 
154  __forceinline bool IsBound(Uint32 ArrayIndex);
155  };
156 
157  struct BuffSRVBindInfo : ShaderVariableD3D11Base
158  {
159  BuffSRVBindInfo( const D3DShaderResourceAttribs& ResourceAttribs,
160  ShaderResourceLayoutD3D11 &ParentResLayout ) :
161  ShaderVariableD3D11Base(ParentResLayout, ResourceAttribs)
162  {}
163 
164  // Non-virtual function
165  __forceinline void BindResource(IDeviceObject *pObject, Uint32 ArrayIndex, const ShaderResourceLayoutD3D11 *dbgResLayout);
166  virtual void Set(IDeviceObject *pObject)override final{ BindResource(pObject, 0, nullptr); }
167 
168  virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final
169  {
170  for(Uint32 elem=0; elem < NumElements; ++elem)
171  BindResource(ppObjects[elem], FirstElement+elem, nullptr);
172  }
173 
174  __forceinline bool IsBound(Uint32 ArrayIndex);
175  };
176 
177  // dbgResourceCache is only used for sanity check and as a remainder that the resource cache must be alive
178  // while Layout is alive
179  void BindResources( IResourceMapping* pResourceMapping, Uint32 Flags, const ShaderResourceCacheD3D11 &dbgResourceCache );
180 
181 #ifdef VERIFY_SHADER_BINDINGS
182  void dbgVerifyBindings()const;
183 #endif
184 
185  IShaderVariable* GetShaderVariable( const Char* Name );
186  __forceinline SHADER_TYPE GetShaderType()const{return m_pResources->GetShaderType();}
187 
188  IObject& GetOwner(){return m_Owner;}
189 private:
190 
191  void InitVariablesHashMap();
192 
193  const Char* GetShaderName()const;
194 
195  // No need to use shared pointer, as the resource cache is either part of the same
196  // ShaderD3D11Impl object, or ShaderResourceBindingD3D11Impl object
197  ShaderResourceCacheD3D11 *m_pResourceCache = nullptr;
198 
199  std::unique_ptr<void, STDDeleterRawMem<void> > m_ResourceBuffer;
200 
201  // Offsets in bytes
202  Uint16 m_TexAndSamplersOffset = 0;
203  Uint16 m_TexUAVsOffset = 0;
204  Uint16 m_BuffUAVsOffset = 0;
205  Uint16 m_BuffSRVsOffset = 0;
206 
207  Uint8 m_NumCBs = 0; // Max == 14
208  Uint8 m_NumTexSRVs = 0; // Max == 128
209  Uint8 m_NumTexUAVs = 0; // Max == 8
210  Uint8 m_NumBufUAVs = 0; // Max == 8
211  Uint8 m_NumBufSRVs = 0; // Max == 128
212 
213  ConstBuffBindInfo& GetCB(Uint32 cb)
214  {
215  VERIFY_EXPR(cb<m_NumCBs);
216  return reinterpret_cast<ConstBuffBindInfo*>(m_ResourceBuffer.get())[cb];
217  }
218  const ConstBuffBindInfo& GetCB(Uint32 cb)const
219  {
220  VERIFY_EXPR(cb<m_NumCBs);
221  return reinterpret_cast<ConstBuffBindInfo*>(m_ResourceBuffer.get())[cb];
222  }
223 
224  TexAndSamplerBindInfo& GetTexSRV(Uint32 t)
225  {
226  VERIFY_EXPR(t<m_NumTexSRVs);
227  return reinterpret_cast<TexAndSamplerBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_TexAndSamplersOffset )[t];
228  }
229  const TexAndSamplerBindInfo& GetTexSRV(Uint32 t)const
230  {
231  VERIFY_EXPR(t<m_NumTexSRVs);
232  return reinterpret_cast<TexAndSamplerBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_TexAndSamplersOffset )[t];
233  }
234 
235  TexUAVBindInfo& GetTexUAV(Uint32 u)
236  {
237  VERIFY_EXPR(u < m_NumTexUAVs);
238  return reinterpret_cast<TexUAVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_TexUAVsOffset)[u];
239  }
240  const TexUAVBindInfo& GetTexUAV(Uint32 u)const
241  {
242  VERIFY_EXPR(u < m_NumTexUAVs);
243  return reinterpret_cast<TexUAVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_TexUAVsOffset)[u];
244  }
245 
246  BuffUAVBindInfo& GetBufUAV(Uint32 u)
247  {
248  VERIFY_EXPR(u < m_NumBufUAVs);
249  return reinterpret_cast<BuffUAVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_BuffUAVsOffset)[u];
250  }
251  const BuffUAVBindInfo& GetBufUAV(Uint32 u)const
252  {
253  VERIFY_EXPR(u < m_NumBufUAVs);
254  return reinterpret_cast<BuffUAVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_BuffUAVsOffset)[u];
255  }
256 
257  BuffSRVBindInfo& GetBufSRV(Uint32 s)
258  {
259  VERIFY_EXPR(s < m_NumBufSRVs);
260  return reinterpret_cast<BuffSRVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_BuffSRVsOffset)[s];
261  }
262  const BuffSRVBindInfo& GetBufSRV(Uint32 s)const
263  {
264  VERIFY_EXPR(s < m_NumBufSRVs);
265  return reinterpret_cast<BuffSRVBindInfo*>( reinterpret_cast<Uint8*>(m_ResourceBuffer.get()) + m_BuffSRVsOffset)[s];
266  }
267 
268  template<typename THandleCB,
269  typename THandleTexSRV,
270  typename THandleTexUAV,
271  typename THandleBufSRV,
272  typename THandleBufUAV>
273  void HandleResources(THandleCB HandleCB,
274  THandleTexSRV HandleTexSRV,
275  THandleTexUAV HandleTexUAV,
276  THandleBufSRV HandleBufSRV,
277  THandleBufUAV HandleBufUAV)
278  {
279  for (Uint32 cb = 0; cb < m_NumCBs; ++cb)
280  HandleCB(GetCB(cb));
281  for (Uint32 t = 0; t < m_NumTexSRVs; ++t)
282  HandleTexSRV(GetTexSRV(t));
283  for (Uint32 u = 0; u < m_NumTexUAVs; ++u)
284  HandleTexUAV(GetTexUAV(u));
285  for (Uint32 s = 0; s < m_NumBufSRVs; ++s)
286  HandleBufSRV(GetBufSRV(s));
287  for (Uint32 u = 0; u < m_NumBufUAVs; ++u)
288  HandleBufUAV(GetBufUAV(u));
289  }
290 
291 #if USE_VARIABLE_HASH_MAP
292  // Hash map to look up shader variables by name.
293  // Note that sizeof(m_VariableHash)==128 (release mode, MS compiler, x64).
294  typedef std::pair<HashMapStringKey, IShaderVariable*> VariableHashData;
295  std::unordered_map<HashMapStringKey, IShaderVariable*, std::hash<HashMapStringKey>, std::equal_to<HashMapStringKey>, STDAllocatorRawMem<VariableHashData> > m_VariableHash;
296 #endif
297 
298  std::shared_ptr<const ShaderResourcesD3D11> m_pResources;
299  IObject &m_Owner;
300 };
301 
302 }
Base interface for all objects created by the render device Diligent::IRenderDevice.
Definition: DeviceObject.h:40
Shader resource variable.
Definition: Shader.h:265
The class implements a cache that holds resources bound to a specific shader stage.
Definition: ShaderResourceCacheD3D11.h:46
SHADER_TYPE
Describes the shader type.
Definition: Shader.h:46
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Resouce mapping.
Definition: ResourceMapping.h:80
SHADER_VARIABLE_TYPE
Describes shader variable type that is used by ShaderVariableDesc.
Definition: Shader.h:100
Diligent::ShaderResourceLayoutD3D11 class http://diligentgraphics.com/diligent-engine/architecture/d3...
Definition: ShaderResourceLayoutD3D11.h:49