Diligent Engine  v.2.4.g
ShaderD3D11Impl.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 
32 
33 #include <mutex>
34 #include <unordered_map>
35 #include <atlbase.h>
36 #include <cstring>
37 
39 #include "ShaderBase.hpp"
40 #include "ShaderD3DBase.hpp"
41 #include "ShaderResourcesD3D11.hpp"
42 #include "HashUtils.hpp"
43 
44 namespace Diligent
45 {
46 
48 class ShaderD3D11Impl final : public ShaderBase<EngineD3D11ImplTraits>, public ShaderD3DBase
49 {
50 public:
52 
53  ShaderD3D11Impl(IReferenceCounters* pRefCounters,
54  class RenderDeviceD3D11Impl* pRenderDeviceD3D11,
55  const ShaderCreateInfo& ShaderCI);
57 
58  virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final;
59 
61  virtual Uint32 DILIGENT_CALL_TYPE GetResourceCount() const override final
62  {
63  return m_pShaderResources->GetTotalResources();
64  }
65 
67  virtual void DILIGENT_CALL_TYPE GetResourceDesc(Uint32 Index, ShaderResourceDesc& ResourceDesc) const override final
68  {
69  ResourceDesc = m_pShaderResources->GetHLSLShaderResourceDesc(Index);
70  }
71 
73  virtual void DILIGENT_CALL_TYPE GetHLSLResource(Uint32 Index, HLSLShaderResourceDesc& ResourceDesc) const override final
74  {
75  ResourceDesc = m_pShaderResources->GetHLSLShaderResourceDesc(Index);
76  }
77 
79  virtual ID3D11DeviceChild* DILIGENT_CALL_TYPE GetD3D11Shader() override final
80  {
81  // GetD3D11Shader(m_pShaderByteCode) should not throw as the shader must
82  // already have been created.
84  }
85 
86  ID3DBlob* GetBytecode() { return m_pShaderByteCode; }
87 
88  const std::shared_ptr<const ShaderResourcesD3D11>& GetShaderResources() const { return m_pShaderResources; }
89 
90  ID3D11DeviceChild* GetD3D11Shader(ID3DBlob* pBlob) noexcept(false);
91 
92 private:
93  struct BlobHashKey
94  {
95  const size_t Hash;
96  CComPtr<ID3DBlob> pBlob;
97 
98  explicit BlobHashKey(ID3DBlob* _pBlob) :
99  Hash{ComputeHash(_pBlob)},
100  pBlob{_pBlob}
101  {}
102 
103  struct Hasher
104  {
105  size_t operator()(const BlobHashKey& Key) const
106  {
107  return Key.Hash;
108  }
109  };
110 
111  bool operator==(const BlobHashKey& rhs) const
112  {
113  if (Hash != rhs.Hash)
114  return false;
115 
116  const auto Size0 = pBlob->GetBufferSize();
117  const auto Size1 = rhs.pBlob->GetBufferSize();
118  if (Size0 != Size1)
119  return false;
120 
121  return memcmp(pBlob->GetBufferPointer(), rhs.pBlob->GetBufferPointer(), Size0) == 0;
122  }
123 
124  private:
125  static size_t ComputeHash(ID3DBlob* pBlob)
126  {
127  const auto* pData = reinterpret_cast<const Uint32*>(pBlob->GetBufferPointer());
128  const auto Size = pBlob->GetBufferSize();
129  VERIFY(Size % 4 == 0, "Bytecode size is expected to be a multiple of 4");
130  size_t Hash = 0;
131  for (Uint32 i = 0; i < Size / 4; ++i)
132  {
133  HashCombine(Hash, pData[i]);
134  }
135  return Hash;
136  }
137  };
138 
139  std::mutex m_d3dShaderCacheMtx;
140  std::unordered_map<BlobHashKey, CComPtr<ID3D11DeviceChild>, BlobHashKey::Hasher> m_d3dShaderCache;
141 
142  // ShaderResources class instance must be referenced through the shared pointer, because
143  // it is referenced by ShaderResourceLayoutD3D11 class instances
144  std::shared_ptr<const ShaderResourcesD3D11> m_pShaderResources;
145 };
146 
147 } // namespace Diligent
Diligent::ShaderD3D11Impl::~ShaderD3D11Impl
~ShaderD3D11Impl()
Definition: ShaderD3D11Impl.cpp:110
Diligent::IReferenceCounters
Base interface for a reference counter object that stores the number of strong and weak references an...
Definition: ReferenceCounters.h:44
Diligent::ShaderD3D11Impl::GetHLSLResource
virtual void GetHLSLResource(Uint32 Index, HLSLShaderResourceDesc &ResourceDesc) const override final
Implementation of IShaderD3D::GetHLSLResource() method.
Definition: ShaderD3D11Impl.hpp:73
Diligent::ShaderD3D11Impl::GetResourceDesc
virtual void GetResourceDesc(Uint32 Index, ShaderResourceDesc &ResourceDesc) const override final
Implementation of IShader::GetResource() in Direct3D11 backend.
Definition: ShaderD3D11Impl.hpp:67
Diligent::IObject
Base interface for all dynamic objects in the engine.
Definition: Object.h:41
ShaderBase.hpp
Diligent::HLSLShaderResourceDesc
HLSL resource description.
Definition: ShaderD3D.h:46
Diligent::ShaderBase
Template class implementing base functionality of the shader object.
Definition: ShaderBase.hpp:49
Diligent::operator==
bool operator==(const Plane3D &p1, const Plane3D &p2)
Definition: AdvancedMath.hpp:442
Diligent::ShaderD3D11Impl::GetResourceCount
virtual Uint32 GetResourceCount() const override final
Implementation of IShader::GetResourceCount() in Direct3D11 backend.
Definition: ShaderD3D11Impl.hpp:61
Diligent::ShaderCreateInfo
Shader creation attributes.
Definition: Shader.h:241
Diligent::INTERFACE_ID
Unique interface identifier.
Definition: InterfaceID.h:37
Diligent::ShaderD3D11Impl::ShaderD3D11Impl
ShaderD3D11Impl(IReferenceCounters *pRefCounters, class RenderDeviceD3D11Impl *pRenderDeviceD3D11, const ShaderCreateInfo &ShaderCI)
Definition: ShaderD3D11Impl.cpp:87
Diligent::ShaderD3D11Impl::BlobHashKey::Hasher::operator()
size_t operator()(const BlobHashKey &Key) const
Definition: ShaderD3D11Impl.hpp:105
Diligent::ShaderD3D11Impl::GetBytecode
ID3DBlob * GetBytecode()
Definition: ShaderD3D11Impl.hpp:86
Diligent::ShaderD3D11Impl::QueryInterface
virtual void QueryInterface(const INTERFACE_ID &IID, IObject **ppInterface) override final
Definition: ShaderD3D11Impl.cpp:114
DILIGENT_CALL_TYPE
#define DILIGENT_CALL_TYPE
Definition: CommonDefinitions.h:45
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ComputeHash
std::size_t ComputeHash(const ArgsType &... Args)
Definition: HashUtils.hpp:57
Diligent::ShaderD3D11Impl::GetD3D11Shader
virtual ID3D11DeviceChild * GetD3D11Shader() override final
Implementation of IShaderD3D11::GetD3D11Shader() method.
Definition: ShaderD3D11Impl.hpp:79
Diligent::ShaderD3D11Impl
Shader implementation in Direct3D11 backend.
Definition: ShaderD3D11Impl.hpp:48
HashUtils.hpp
Diligent::ShaderResourceDesc
Shader resource description.
Definition: Shader.h:390
Diligent::ShaderD3DBase
Base implementation of a D3D shader.
Definition: ShaderD3DBase.hpp:40
Diligent::ShaderD3D11Impl::BlobHashKey::Hasher
Definition: ShaderD3D11Impl.hpp:103
EngineD3D11ImplTraits.hpp
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::RenderDeviceD3D11Impl
Render device implementation in Direct3D11 backend.
Definition: RenderDeviceD3D11Impl.hpp:40
ShaderResourcesD3D11.hpp
Diligent::ShaderD3D11Impl::GetShaderResources
const std::shared_ptr< const ShaderResourcesD3D11 > & GetShaderResources() const
Definition: ShaderD3D11Impl.hpp:88
ShaderD3DBase.hpp
Diligent::ShaderD3DBase::m_pShaderByteCode
CComPtr< ID3DBlob > m_pShaderByteCode
Definition: ShaderD3DBase.hpp:46
Diligent::HashCombine
void HashCombine(std::size_t &Seed, const T &Val)
Definition: HashUtils.hpp:44
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37