Diligent Engine  v.2.4.g
RenderPassCache.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 <unordered_map>
34 #include <mutex>
35 
36 #include "GraphicsTypes.h"
37 #include "Constants.h"
38 #include "HashUtils.hpp"
40 #include "RefCntAutoPtr.hpp"
41 
42 namespace Diligent
43 {
44 
45 class RenderDeviceVkImpl;
46 class RenderPassVkImpl;
47 
49 {
50 public:
51  RenderPassCache(RenderDeviceVkImpl& DeviceVk) noexcept;
52 
53  // clang-format off
54  RenderPassCache (const RenderPassCache&) = delete;
55  RenderPassCache (RenderPassCache&&) = delete;
58  // clang-format on
59 
61 
62  // This structure is used as the key to find framebuffer
64  {
65  // clang-format off
68  SampleCount {0},
70  {}
71  // clang-format on
72 
73  RenderPassCacheKey(Uint32 _NumRenderTargets,
74  Uint32 _SampleCount,
75  const TEXTURE_FORMAT _RTVFormats[],
76  TEXTURE_FORMAT _DSVFormat) :
77  // clang-format off
78  NumRenderTargets{static_cast<decltype(NumRenderTargets)>(_NumRenderTargets)},
79  SampleCount {static_cast<decltype(SampleCount)> (_SampleCount) },
80  DSVFormat {_DSVFormat }
81  // clang-format on
82  {
83  VERIFY_EXPR(_NumRenderTargets <= std::numeric_limits<decltype(NumRenderTargets)>::max());
84  VERIFY_EXPR(_SampleCount <= std::numeric_limits<decltype(SampleCount)>::max());
85  for (Uint32 rt = 0; rt < NumRenderTargets; ++rt)
86  RTVFormats[rt] = _RTVFormats[rt];
87  }
88  // Default memeber initialization is intentionally omitted
92  TEXTURE_FORMAT RTVFormats[MAX_RENDER_TARGETS];
93 
94  bool operator==(const RenderPassCacheKey& rhs) const
95  {
96  // clang-format off
97  if (GetHash() != rhs.GetHash() ||
99  SampleCount != rhs.SampleCount ||
100  DSVFormat != rhs.DSVFormat)
101  {
102  return false;
103  }
104  // clang-format on
105 
106  for (Uint32 rt = 0; rt < NumRenderTargets; ++rt)
107  if (RTVFormats[rt] != rhs.RTVFormats[rt])
108  return false;
109 
110  return true;
111  }
112 
113  size_t GetHash() const
114  {
115  if (Hash == 0)
116  {
118  for (Uint32 rt = 0; rt < NumRenderTargets; ++rt)
119  HashCombine(Hash, RTVFormats[rt]);
120  }
121  return Hash;
122  }
123 
124  private:
125  mutable size_t Hash = 0;
126  };
127 
128  RenderPassVkImpl* GetRenderPass(const RenderPassCacheKey& Key);
129 
130  void Destroy();
131 
132 private:
133  struct RenderPassCacheKeyHash
134  {
135  std::size_t operator()(const RenderPassCacheKey& Key) const
136  {
137  return Key.GetHash();
138  }
139  };
140 
141  RenderDeviceVkImpl& m_DeviceVkImpl;
142 
143  std::mutex m_Mutex;
144  std::unordered_map<RenderPassCacheKey, RefCntAutoPtr<RenderPassVkImpl>, RenderPassCacheKeyHash> m_Cache;
145 };
146 
147 } // namespace Diligent
Diligent::RenderPassCache::RenderPassCacheKey::RTVFormats
TEXTURE_FORMAT RTVFormats[MAX_RENDER_TARGETS]
Definition: RenderPassCache.hpp:92
Diligent::RenderPassCache::~RenderPassCache
~RenderPassCache()
Definition: RenderPassCache.cpp:46
Diligent::RenderDeviceVkImpl
Render device implementation in Vulkan backend.
Definition: RenderDeviceVkImpl.hpp:58
Diligent::RenderPassCache::RenderPassCache
RenderPassCache(RenderDeviceVkImpl &DeviceVk) noexcept
Definition: RenderPassCache.cpp:41
Diligent::RenderPassCache::RenderPassCacheKey
Definition: RenderPassCache.hpp:63
Diligent::RenderPassCache::RenderPassCacheKey::RenderPassCacheKey
RenderPassCacheKey()
Definition: RenderPassCache.hpp:66
Diligent::RenderPassCache::GetRenderPass
RenderPassVkImpl * GetRenderPass(const RenderPassCacheKey &Key)
Definition: RenderPassCache.cpp:65
Diligent::max
Vector3< T > max(const Vector3< T > &a, const Vector3< T > &b)
Definition: BasicMath.hpp:1660
Constants.h
Diligent::RenderPassCache::RenderPassCacheKey::SampleCount
Uint8 SampleCount
Definition: RenderPassCache.hpp:90
Diligent::TEX_FORMAT_UNKNOWN
@ TEX_FORMAT_UNKNOWN
Unknown format.
Definition: GraphicsTypes.h:331
Diligent::RenderPassCache::Destroy
void Destroy()
Definition: RenderPassCache.cpp:54
Diligent::RenderPassCache::RenderPassCacheKey::operator==
bool operator==(const RenderPassCacheKey &rhs) const
Definition: RenderPassCache.hpp:94
Diligent::RenderPassCache::RenderPassCacheKey::RenderPassCacheKey
RenderPassCacheKey(Uint32 _NumRenderTargets, Uint32 _SampleCount, const TEXTURE_FORMAT _RTVFormats[], TEXTURE_FORMAT _DSVFormat)
Definition: RenderPassCache.hpp:73
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::RenderPassCache::operator=
RenderPassCache & operator=(const RenderPassCache &)=delete
Diligent::RenderPassCache::RenderPassCacheKey::NumRenderTargets
Uint8 NumRenderTargets
Definition: RenderPassCache.hpp:89
Diligent::RenderPassCache::RenderPassCacheKey::GetHash
size_t GetHash() const
Definition: RenderPassCache.hpp:113
Diligent::TEXTURE_FORMAT
TEXTURE_FORMAT
Texture formats.
Definition: GraphicsTypes.h:328
HashUtils.hpp
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
Diligent::RenderPassVkImpl
Render pass implementation in Vulkan backend.
Definition: RenderPassVkImpl.hpp:41
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
RefCntAutoPtr.hpp
GraphicsTypes.h
VulkanObjectWrappers.hpp
Diligent::RenderPassCache
Definition: RenderPassCache.hpp:48
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
Diligent::RenderPassCache::RenderPassCacheKey::DSVFormat
TEXTURE_FORMAT DSVFormat
Definition: RenderPassCache.hpp:91