29 #include "TextureBaseD3D11.h" 30 #include "BufferD3D11Impl.h" 31 #include "SamplerD3D11Impl.h" 67 pBuff = std::move(_pBuff);
95 ID3D11Resource *pd3d11Resource;
96 CachedResource() : pTexture(nullptr), pBuffer(nullptr), pd3d11Resource(nullptr)
103 pTexture = pTexView ? ValidatedCast<TextureBaseD3D11>(pTexView->TextureViewD3D11Impl::GetTexture()) :
nullptr;
104 pView.Attach(pTexView.Detach());
105 pd3d11Resource = pTexture ? pTexture->TextureBaseD3D11::GetD3D11Texture() :
nullptr;
112 pBuffer = pBufView ? ValidatedCast<BufferD3D11Impl>(pBufView->BufferViewD3D11Impl::GetBuffer()) :
nullptr;
113 pView.Attach(pBufView.Detach());
114 pd3d11Resource = pBuffer ? pBuffer->BufferD3D11Impl::GetD3D11Buffer() :
nullptr;
118 void Initialize(Int32 CBCount, Int32 SRVCount, Int32 SamplerCount, Int32 UAVCount,
class IMemoryAllocator &MemAllocator);
119 void Destroy(
class IMemoryAllocator &MemAllocator);
122 __forceinline
void SetCB(Uint32 Slot, RefCntAutoPtr<BufferD3D11Impl> &&pBuffD3D11Impl)
124 auto *pd3d11Buff = pBuffD3D11Impl ? pBuffD3D11Impl->BufferD3D11Impl::GetD3D11Buffer() :
nullptr;
125 SetD3D11ResourceInternal<CachedCB>(Slot, GetCBCount(), &ShaderResourceCacheD3D11::GetCBArrays, std::move(pBuffD3D11Impl), pd3d11Buff);
128 __forceinline
void SetTexSRV(Uint32 Slot, RefCntAutoPtr<TextureViewD3D11Impl>&& pTexView)
130 auto pd3d11SRV = pTexView ?
static_cast<ID3D11ShaderResourceView*
>(pTexView->TextureViewD3D11Impl::GetD3D11View()) :
nullptr;
131 SetD3D11ResourceInternal<CachedResource>(Slot, GetSRVCount(), &ShaderResourceCacheD3D11::GetSRVArrays, std::move(pTexView), pd3d11SRV);
134 __forceinline
void SetBufSRV(Uint32 Slot, RefCntAutoPtr<BufferViewD3D11Impl> &&pBuffView)
136 auto pd3d11SRV = pBuffView ?
static_cast<ID3D11ShaderResourceView*
>(pBuffView->BufferViewD3D11Impl::GetD3D11View()) :
nullptr;
137 SetD3D11ResourceInternal<CachedResource>(Slot, GetSRVCount(), &ShaderResourceCacheD3D11::GetSRVArrays, std::move(pBuffView), pd3d11SRV);
140 __forceinline
void SetTexUAV(Uint32 Slot, RefCntAutoPtr<TextureViewD3D11Impl>&& pTexView)
142 auto pd3d11UAV = pTexView ?
static_cast<ID3D11UnorderedAccessView*
>(pTexView->TextureViewD3D11Impl::GetD3D11View()) :
nullptr;
143 SetD3D11ResourceInternal<CachedResource>(Slot, GetUAVCount(), &ShaderResourceCacheD3D11::GetUAVArrays, std::move(pTexView), pd3d11UAV);
146 __forceinline
void SetBufUAV(Uint32 Slot, RefCntAutoPtr<BufferViewD3D11Impl> &&pBuffView)
148 auto pd3d11UAV = pBuffView ?
static_cast<ID3D11UnorderedAccessView*
>(pBuffView->BufferViewD3D11Impl::GetD3D11View()) :
nullptr;
149 SetD3D11ResourceInternal<CachedResource>(Slot, GetUAVCount(), &ShaderResourceCacheD3D11::GetUAVArrays, std::move(pBuffView), pd3d11UAV);
152 __forceinline
void SetSampler(Uint32 Slot, SamplerD3D11Impl *pSampler)
154 auto *pd3d11Sampler = pSampler ? pSampler->SamplerD3D11Impl::GetD3D11SamplerState() :
nullptr;
155 SetD3D11ResourceInternal<CachedSampler>(Slot, GetSamplerCount(), &ShaderResourceCacheD3D11::GetSamplerArrays, pSampler, pd3d11Sampler);
160 __forceinline CachedCB& GetCB(Uint32 Slot)
162 VERIFY(Slot < GetCBCount(),
"CB slot is out of range" );
163 ShaderResourceCacheD3D11::CachedCB *CBs;
164 ID3D11Buffer **pd3d11CBs;
165 GetCBArrays(CBs, pd3d11CBs);
169 __forceinline CachedResource& GetSRV(Uint32 Slot)
171 VERIFY(Slot < GetSRVCount(),
"SRV slot is out of range" );
172 ShaderResourceCacheD3D11::CachedResource *SRVResources;
173 ID3D11ShaderResourceView **pd3d11SRVs;
174 GetSRVArrays(SRVResources, pd3d11SRVs);
175 return SRVResources[Slot];
178 __forceinline CachedResource& GetUAV(Uint32 Slot)
180 VERIFY(Slot < GetUAVCount(),
"UAV slot is out of range" );
181 ShaderResourceCacheD3D11::CachedResource *UAVResources;
182 ID3D11UnorderedAccessView **pd3d11UAVs;
183 GetUAVArrays(UAVResources, pd3d11UAVs);
184 return UAVResources[Slot];
187 __forceinline CachedSampler& GetSampler(Uint32 Slot)
189 VERIFY(Slot < GetSamplerCount(),
"Sampler slot is out of range" );
190 ShaderResourceCacheD3D11::CachedSampler *Samplers;
191 ID3D11SamplerState **pd3d11Samplers;
192 GetSamplerArrays(Samplers, pd3d11Samplers);
193 return Samplers[Slot];
197 __forceinline
bool IsCBBound(Uint32 Slot)
const 199 CachedCB* CBs =
nullptr;
200 ID3D11Buffer** d3d11CBs =
nullptr;
201 const_cast<ShaderResourceCacheD3D11*
>(
this)->GetCBArrays(CBs, d3d11CBs);
202 if( Slot < GetCBCount() && d3d11CBs[Slot] != nullptr )
204 VERIFY(CBs[Slot].pBuff !=
nullptr,
"No relevant buffer resource");
210 __forceinline
bool IsSRVBound(Uint32 Slot,
bool dbgIsTextureView)
const 212 CachedResource* SRVResources =
nullptr;
213 ID3D11ShaderResourceView** d3d11SRVs =
nullptr;
214 const_cast<ShaderResourceCacheD3D11*
>(
this)->GetSRVArrays(SRVResources, d3d11SRVs);
215 if( Slot < GetSRVCount() && d3d11SRVs[Slot] != nullptr )
217 VERIFY( ( dbgIsTextureView && SRVResources[Slot].pTexture !=
nullptr) ||
218 (!dbgIsTextureView && SRVResources[Slot].pBuffer !=
nullptr),
"No relevant resource");
224 __forceinline
bool IsUAVBound(Uint32 Slot,
bool dbgIsTextureView)
const 226 CachedResource* UAVResources =
nullptr;
227 ID3D11UnorderedAccessView** d3d11UAVs =
nullptr;
228 const_cast<ShaderResourceCacheD3D11*
>(
this)->GetUAVArrays(UAVResources, d3d11UAVs);
229 if( Slot < GetUAVCount() && d3d11UAVs[Slot] != nullptr )
231 VERIFY( ( dbgIsTextureView && UAVResources[Slot].pTexture !=
nullptr) ||
232 (!dbgIsTextureView && UAVResources[Slot].pBuffer !=
nullptr),
"No relevant resource");
238 __forceinline
bool IsSamplerBound(Uint32 Slot)
const 240 CachedSampler* Samplers =
nullptr;
241 ID3D11SamplerState** d3d11Samplers =
nullptr;
242 const_cast<ShaderResourceCacheD3D11*
>(
this)->GetSamplerArrays(Samplers, d3d11Samplers);
243 if( Slot < GetSamplerCount() && d3d11Samplers[Slot] != nullptr )
245 VERIFY(Samplers[Slot].pSampler !=
nullptr,
"No relevant sampler");
251 void dbgVerifyCacheConsistency();
254 static __forceinline Uint32 PackResourceCounts(Uint32 CBCount, Uint32 SRVCount, Uint32 SamplerCount, Uint32 UAVCount)
256 VERIFY_EXPR(CBCount <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT );
257 VERIFY_EXPR(SRVCount <= D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT);
258 VERIFY_EXPR(SamplerCount <= D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT);
259 VERIFY_EXPR(UAVCount <= D3D11_PS_CS_UAV_REGISTER_COUNT);
260 return (CBCount & 0xFF) | ((SRVCount & 0xFF) << 8) | ((SamplerCount & 0xFF) << 16) | ((UAVCount & 0xFF) << 24);
263 static __forceinline Uint32 UnpackCBCount (Uint32 PackedCounts) {
return (PackedCounts >> 0) & 0xFF; }
264 static __forceinline Uint32 UnpackSRVCount (Uint32 PackedCounts) {
return (PackedCounts >> 8) & 0xFF; }
265 static __forceinline Uint32 UnpackSamplerCount (Uint32 PackedCounts) {
return (PackedCounts >> 16) & 0xFF; }
266 static __forceinline Uint32 UnpackUAVCount (Uint32 PackedCounts) {
return (PackedCounts >> 24) & 0xFF; }
268 __forceinline Uint32 GetCBCount()
const{
return UnpackCBCount (m_ResourceCounts);}
269 __forceinline Uint32 GetSRVCount()
const{
return UnpackSRVCount (m_ResourceCounts);}
270 __forceinline Uint32 GetSamplerCount()
const{
return UnpackSamplerCount(m_ResourceCounts);}
271 __forceinline Uint32 GetUAVCount()
const{
return UnpackUAVCount (m_ResourceCounts);}
272 __forceinline Uint32 GetPackedCounts()
const{
return m_ResourceCounts;}
274 __forceinline
void GetResourceArrays(CachedCB* &CBs, ID3D11Buffer** &pd3d11CBs,
275 CachedResource* &SRVResources, ID3D11ShaderResourceView** &d3d11SRVs,
276 CachedSampler* &Samplers, ID3D11SamplerState** &pd3d11Samplers,
277 CachedResource* &UAVResources, ID3D11UnorderedAccessView** &pd3d11UAVs)
279 VERIFY_EXPR(IsInitialized());
280 CBs =
reinterpret_cast<CachedCB*
>( m_pResourceData );
281 pd3d11CBs =
reinterpret_cast<ID3D11Buffer**
>( CBs + GetCBCount() );
282 SRVResources =
reinterpret_cast<CachedResource*
>( pd3d11CBs + GetCBCount() );
283 d3d11SRVs =
reinterpret_cast<ID3D11ShaderResourceView**
>( SRVResources + GetSRVCount() );
284 Samplers =
reinterpret_cast<CachedSampler*
>( d3d11SRVs + GetSRVCount() );
285 pd3d11Samplers =
reinterpret_cast<ID3D11SamplerState**
>( Samplers + GetSamplerCount() );
286 UAVResources =
reinterpret_cast<CachedResource*
>( pd3d11Samplers + GetSamplerCount() );
287 pd3d11UAVs =
reinterpret_cast<ID3D11UnorderedAccessView**
>( UAVResources + GetUAVCount() );
290 __forceinline
void GetCBArrays(CachedCB* &CBs, ID3D11Buffer** &pd3d11CBs)
292 CBs =
reinterpret_cast<CachedCB*
>( m_pResourceData );
293 pd3d11CBs =
reinterpret_cast<ID3D11Buffer**
>( CBs + GetCBCount() );
296 __forceinline
void GetSRVArrays(CachedResource* &SRVResources, ID3D11ShaderResourceView** &d3d11SRVs)
298 SRVResources =
reinterpret_cast<CachedResource*
>( m_pResourceData + (
sizeof(CachedCB) +
sizeof(ID3D11Buffer*)) * GetCBCount() );
299 d3d11SRVs =
reinterpret_cast<ID3D11ShaderResourceView**
>( SRVResources + GetSRVCount() );
302 __forceinline
void GetSamplerArrays(CachedSampler* &Samplers, ID3D11SamplerState** &pd3d11Samplers)
304 Samplers =
reinterpret_cast<CachedSampler*
>( m_pResourceData +
305 (
sizeof(CachedCB) +
sizeof(ID3D11Buffer*)) * GetCBCount() +
306 (
sizeof(CachedResource) +
sizeof(ID3D11ShaderResourceView*)) * GetSRVCount() );
307 pd3d11Samplers =
reinterpret_cast<ID3D11SamplerState**
>( Samplers + GetSamplerCount() );
310 __forceinline
void GetUAVArrays(CachedResource* &UAVResources, ID3D11UnorderedAccessView** &pd3d11UAVs)
312 UAVResources =
reinterpret_cast<CachedResource*
>( m_pResourceData +
313 (
sizeof(CachedCB) +
sizeof(ID3D11Buffer*)) * GetCBCount() +
314 (
sizeof(CachedResource) +
sizeof(ID3D11ShaderResourceView*)) * GetSRVCount() +
315 (
sizeof(CachedSampler) +
sizeof(ID3D11SamplerState*)) * GetSamplerCount() );
316 pd3d11UAVs =
reinterpret_cast<ID3D11UnorderedAccessView**
>( UAVResources + GetUAVCount() );
319 __forceinline
bool IsInitialized()
const{
return m_ResourceCounts != InvalidResourceCounts;}
323 template<
typename TCachedResourceType,
typename TGetResourceArraysFunc,
typename TSrcResourceType,
typename TD3D11ResourceType>
324 __forceinline
void SetD3D11ResourceInternal(Uint32 Slot, Uint32 Size,
325 TGetResourceArraysFunc GetArrays,
326 TSrcResourceType &&pResource,
327 TD3D11ResourceType *pd3d11Resource)
329 VERIFY(Slot < Size,
"Resource cache is not big enough" );
330 VERIFY(pResource !=
nullptr && pd3d11Resource !=
nullptr || pResource ==
nullptr && pd3d11Resource ==
nullptr,
331 "Resource and D3D11 resource must be set/unset atomically");
332 TCachedResourceType* Resources;
333 TD3D11ResourceType** d3d11ResArr;
334 (this->*GetArrays)(Resources, d3d11ResArr);
335 Resources[Slot].Set(std::forward<TSrcResourceType>(pResource));
336 d3d11ResArr[Slot] = pd3d11Resource;
339 static constexpr Uint32 InvalidResourceCounts = 0xFFFFFFFF;
345 Uint32 m_ResourceCounts = InvalidResourceCounts;
349 Uint8 *m_pResourceData =
nullptr;
352 IMemoryAllocator *m_pdbgMemoryAllocator =
nullptr;
Base implementation of the Diligent::ITextureD3D11 interface.
Definition: TextureBaseD3D11.h:50
The class implements a cache that holds resources bound to a specific shader stage.
Definition: ShaderResourceCacheD3D11.h:46
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
RefCntAutoPtr< class SamplerD3D11Impl > pSampler
Strong reference to the sampler.
Definition: ShaderResourceCacheD3D11.h:75
Describes resources associated with the cached SRV or UAV.
Definition: ShaderResourceCacheD3D11.h:83
Describes resources associated with the cached constant buffer.
Definition: ShaderResourceCacheD3D11.h:61
Implementation of the Diligent::ISamplerD3D11 interface.
Definition: SamplerD3D11Impl.h:38
RefCntAutoPtr< IDeviceObject > pView
Wee keep strong reference to the view instead of the reference to the texture or buffer because this ...
Definition: ShaderResourceCacheD3D11.h:90
Template class that implements reference counting.
Definition: RefCntAutoPtr.h:71
RefCntAutoPtr< BufferD3D11Impl > pBuff
Strong reference to the buffer.
Definition: ShaderResourceCacheD3D11.h:64
Describes resources associated with the cached sampler.
Definition: ShaderResourceCacheD3D11.h:72
Implementation of the Diligent::IBufferD3D11 interface.
Definition: BufferD3D11Impl.h:51