Diligent Engine  v.2.4.g
MapHelper.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 "../../../Platforms/Basic/interface/DebugUtilities.hpp"
34 #include "../../../Common/interface/RefCntAutoPtr.hpp"
35 #include "../../GraphicsEngine/interface/DeviceContext.h"
36 #include "../../GraphicsEngine/interface/Buffer.h"
37 
38 namespace Diligent
39 {
40 
42 
53 template <typename DataType, bool KeepStrongReferences = false>
54 class MapHelper
55 {
56 public:
58  // clang-format off
60  m_pBuffer {nullptr},
61  m_pContext {nullptr},
62  m_pMappedData {nullptr},
63  m_MapType {static_cast<MAP_TYPE>(-1)},
64  m_MapFlags {static_cast<Uint32>(-1) }
65  // clang-format on
66  {
67  }
68 
71  MapHelper(IDeviceContext* pContext, IBuffer* pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags) :
72  MapHelper()
73  {
74  Map(pContext, pBuffer, MapType, MapFlags);
75  }
76 
78  // clang-format off
79  MapHelper(MapHelper&& Helper) :
80  m_pBuffer {std::move(Helper.m_pBuffer) },
81  m_pMappedData {std::move(Helper.m_pMappedData)},
82  m_pContext {std::move(Helper.m_pContext) },
83  m_MapType {std::move(Helper.m_MapType) },
84  m_MapFlags {std::move(Helper.m_MapFlags) }
85  // clang-format on
86  {
87  Helper.m_pBuffer = nullptr;
88  Helper.m_pContext = nullptr;
89  Helper.m_pMappedData = nullptr;
90  Helper.m_MapType = static_cast<MAP_TYPE>(-1);
91  Helper.m_MapFlags = static_cast<Uint32>(-1);
92  }
93 
96  {
97  m_pBuffer = std::move(Helper.m_pBuffer);
98  m_pMappedData = std::move(Helper.m_pMappedData);
99  m_pContext = std::move(Helper.m_pContext);
100  m_MapType = std::move(Helper.m_MapType);
101  m_MapFlags = std::move(Helper.m_MapFlags);
102 
103  Helper.m_pBuffer = nullptr;
104  Helper.m_pContext = nullptr;
105  Helper.m_pMappedData = nullptr;
106  Helper.m_MapType = static_cast<MAP_TYPE>(-1);
107  Helper.m_MapFlags = static_cast<Uint32>(-1);
108 
109  return *this;
110  }
111 
113 
118  void Map(IDeviceContext* pContext, IBuffer* pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags)
119  {
120  VERIFY(!m_pBuffer && !m_pMappedData && !m_pContext, "Object already mapped");
121  Unmap();
122 #ifdef DILIGENT_DEBUG
123  {
124  auto& BuffDesc = pBuffer->GetDesc();
125  VERIFY(sizeof(DataType) <= BuffDesc.uiSizeInBytes, "Data type size exceeds buffer size");
126  }
127 #endif
128  pContext->MapBuffer(pBuffer, MapType, MapFlags, (PVoid&)m_pMappedData);
129  if (m_pMappedData != nullptr)
130  {
131  m_pContext = pContext;
132  m_pBuffer = pBuffer;
133  m_MapType = MapType;
134  m_MapFlags = MapFlags;
135  }
136  }
137 
139  void Unmap()
140  {
141  if (m_pBuffer)
142  {
143  m_pContext->UnmapBuffer(m_pBuffer, m_MapType);
144  m_pBuffer = nullptr;
145  m_MapType = static_cast<MAP_TYPE>(-1);
146  m_MapFlags = static_cast<Uint32>(-1);
147  }
148  m_pContext = nullptr;
149  m_pMappedData = nullptr;
150  }
151 
153  operator DataType*() { return m_pMappedData; }
154 
156  operator const DataType*() const { return m_pMappedData; }
157 
159  DataType* operator->() { return m_pMappedData; }
160 
162  const DataType* operator->() const { return m_pMappedData; }
163 
166  {
167  Unmap();
168  }
169 
170 private:
171  MapHelper(const MapHelper&) = delete;
172  MapHelper& operator=(const MapHelper&) = delete;
173 
174  template <typename PtrType, bool UseStrongReference>
175  struct PtrTypeSelector
176  {};
177 
178  template <typename PtrType>
179  struct PtrTypeSelector<PtrType, true>
180  {
181  typedef RefCntAutoPtr<PtrType> Type;
182  };
183 
184  template <typename PtrType>
185  struct PtrTypeSelector<PtrType, false>
186  {
187  typedef PtrType* Type;
188  };
189 
192 
195 
197  DataType* m_pMappedData;
198 
199  MAP_TYPE m_MapType;
200 
201  Uint32 m_MapFlags;
202 };
203 
204 } // namespace Diligent
Diligent::MAP_FLAGS
MAP_FLAGS
Special map flags.
Definition: GraphicsTypes.h:227
Diligent::PVoid
void * PVoid
Definition: BasicTypes.h:56
Diligent::IBuffer
Buffer interface.
Definition: Buffer.h:187
Diligent::MapHelper::operator=
MapHelper & operator=(MapHelper &&Helper)
Move-assignement operator: takes over resource ownership from Helper.
Definition: MapHelper.hpp:95
Diligent::MapHelper::Map
void Map(IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags)
Maps the provided resource.
Definition: MapHelper.hpp:118
Diligent::MapHelper::MapHelper
MapHelper()
Initializes the class member with null values.
Definition: MapHelper.hpp:59
Diligent::IDeviceContext::MapBuffer
virtual void METHOD() MapBuffer(IBuffer *pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags, PVoid &pMappedData)
Maps the buffer.
Diligent::MapHelper::MapHelper
MapHelper(MapHelper &&Helper)
Move constructor: takes over resource ownership from Helper.
Definition: MapHelper.hpp:79
Diligent::MapHelper::MapHelper
MapHelper(IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, MAP_FLAGS MapFlags)
Initializes the object and maps the provided resource. See Map() for details.
Definition: MapHelper.hpp:71
Diligent::MapHelper::operator->
const DataType * operator->() const
Operator const ->
Definition: MapHelper.hpp:162
Type
const D3D12_PIPELINE_STATE_SUBOBJECT_TYPE Type
Definition: PipelineStateD3D12Impl.cpp:69
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::MapHelper::operator->
DataType * operator->()
Operator ->
Definition: MapHelper.hpp:159
Diligent::IDeviceContext
Device context interface.
Definition: DeviceContext.h:1460
VERIFY
#define VERIFY(...)
Definition: DebugUtilities.hpp:76
Diligent::MapHelper::~MapHelper
~MapHelper()
Unamps the resource.
Definition: MapHelper.hpp:165
Diligent::MAP_TYPE
MAP_TYPE
Resource mapping type.
Definition: GraphicsTypes.h:206
Diligent::MapHelper
Facilitates resource mapping.
Definition: MapHelper.hpp:54
Diligent::MapHelper::Unmap
void Unmap()
Unamps the resource and resets the object state to default.
Definition: MapHelper.hpp:139
Diligent::IBuffer::GetDesc
virtual const BufferDesc &METHOD() GetDesc() const override=0
Returns the buffer description used to create the object.
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37