Diligent Engine API Reference: D:/GitHub/DiligentEngine/DiligentCore/Graphics/GraphicsEngine/interface/MapHelper.h Source File

Diligent Engine API Reference

MapHelper.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 #include "DebugUtilities.h"
30
31 namespace Diligent
32 {
33
35
46 template<typename DataType, bool KeepStrongReferences = false>
47 class MapHelper
48 {
49 public:
50
52 MapHelper() :
53 m_pMappedData(nullptr),
54 m_pBuffer(nullptr),
55 m_pContext(nullptr),
56 m_MapType(static_cast<MAP_TYPE>(-1)),
57 m_MapFlags(static_cast<Uint32>(-1))
58 {
59 }
60
63 MapHelper( IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, Uint32 MapFlags ) :
64 MapHelper()
65 {
66 Map(pContext, pBuffer, MapType, MapFlags);
67 }
68
70 MapHelper(MapHelper&& Helper) :
71 m_pBuffer( std::move(Helper.m_pBuffer) ),
72 m_pMappedData( std::move(Helper.m_pMappedData) ),
73 m_pContext( std::move(Helper.m_pContext) ),
74 m_MapType( std::move(Helper.m_MapType) ),
75 m_MapFlags( std::move(Helper.m_MapFlags) )
76 {
77 Helper.m_pBuffer = nullptr;
78 Helper.m_pContext = nullptr;
79 Helper.m_pMappedData = nullptr;
80 Helper.m_MapType = static_cast<MAP_TYPE>(-1);
81 Helper.m_MapFlags = static_cast<Uint32>(-1);
82 }
83
85 MapHelper& operator = (MapHelper&& Helper)
86 {
87 m_pBuffer = std::move(Helper.m_pBuffer);
88 m_pMappedData = std::move(Helper.m_pMappedData);
89 m_pContext = std::move( Helper.m_pContext );
90 m_MapType = std::move(Helper.m_MapType);
91 m_MapFlags = std::move(Helper.m_MapFlags);
92 Helper.m_pBuffer = nullptr;
93 Helper.m_pContext = nullptr;
94 Helper.m_pMappedData = nullptr;
95 Helper.m_MapType = static_cast<MAP_TYPE>(-1);
96 Helper.m_MapFlags = static_cast<Uint32>(-1);
97 return *this;
98 }
99
101
106 void Map( IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, Uint32 MapFlags )
107 {
108 VERIFY(!m_pBuffer && !m_pMappedData && !m_pContext, "Object already mapped");
109 Unmap();
110 #ifdef _DEBUG
111 auto &BuffDesc = pBuffer->GetDesc();
112 VERIFY(sizeof(DataType) <= BuffDesc.uiSizeInBytes, "Data type size exceeds buffer size");
113 #endif
114 m_pContext = pContext;
115 m_pBuffer = pBuffer;
116 m_MapType = MapType;
117 m_MapFlags = MapFlags;
118 m_pBuffer->Map(m_pContext, MapType, MapFlags, (PVoid&)m_pMappedData);
119 VERIFY( m_pMappedData, "Map failed" );
120 }
121
123 void Unmap()
124 {
125 if( m_pBuffer )
126 {
127 m_pBuffer->Unmap(m_pContext, m_MapType, m_MapFlags);
128 m_pBuffer = nullptr;
129 m_MapType = static_cast<MAP_TYPE>(-1);
130 m_MapFlags = static_cast<Uint32>(-1);
131 }
132 m_pContext = nullptr;
133 m_pMappedData = nullptr;
134 }
135
137 operator DataType* (){return m_pMappedData;}
138
140 operator const DataType* ()const{return m_pMappedData;}
141
143 DataType* operator->() {return m_pMappedData;}
144
146 const DataType* operator->() const{return m_pMappedData;}
147
149 ~MapHelper()
150 {
151 Unmap();
152 }
153
154 private:
155 MapHelper(const MapHelper&);
156 MapHelper& operator=(const MapHelper&);
157
158 template<typename PtrType, bool UseStrongReference>
159 struct PtrTypeSelector{};
160
161 template<typename PtrType>
162 struct PtrTypeSelector<PtrType, true>
163 {
164 typedef RefCntAutoPtr<PtrType> Type;
165 };
166
167 template<typename PtrType>
168 struct PtrTypeSelector<PtrType, false>
169 {
170 typedef PtrType* Type;
171 };
172
174 typename PtrTypeSelector<IBuffer, KeepStrongReferences>::Type m_pBuffer;
175
177 typename PtrTypeSelector<IDeviceContext, KeepStrongReferences>::Type m_pContext;
178
180 DataType *m_pMappedData;
181
182 MAP_TYPE m_MapType;
183
184 Uint32 m_MapFlags;
185 };
186
187 }
Diligent::MapHelper::operator->
const DataType * operator->() const
Operator const ->
Definition: MapHelper.h:146
Diligent::MapHelper
Facilitates resource mapping.
Definition: MapHelper.h:47
Diligent
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
Diligent::MapHelper::Unmap
void Unmap()
Unamps the resource and resets the object state to default.
Definition: MapHelper.h:123
std
Definition: AdvancedMath.h:316
Diligent::MapHelper::MapHelper
MapHelper(MapHelper &&Helper)
Move constructor: takes over resource ownership from Helper.
Definition: MapHelper.h:70
Diligent::MapHelper::MapHelper
MapHelper()
Initializes the class member with null values.
Definition: MapHelper.h:52
Diligent::MapHelper::MapHelper
MapHelper(IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, Uint32 MapFlags)
Initializes the object and maps the provided resource. See Map() for details.
Definition: MapHelper.h:63
Diligent::IBuffer
Buffer interface.
Definition: Buffer.h:200
Diligent::IDeviceContext
Device context interface.
Definition: DeviceContext.h:443
Diligent::MapHelper::operator->
DataType * operator->()
Operator ->
Definition: MapHelper.h:143
Diligent::MapHelper::~MapHelper
~MapHelper()
Unamps the resource.
Definition: MapHelper.h:149
Diligent::MapHelper::Map
void Map(IDeviceContext *pContext, IBuffer *pBuffer, MAP_TYPE MapType, Uint32 MapFlags)
Maps the provided resource.
Definition: MapHelper.h:106
Diligent::MapHelper::operator=
MapHelper & operator=(MapHelper &&Helper)
Move-assignement operator: takes over resource ownership from Helper.
Definition: MapHelper.h:85
Diligent::MAP_TYPE
MAP_TYPE
Resource mapping type.
Definition: GraphicsTypes.h:125
Diligent::IBuffer::GetDesc
virtual const BufferDesc & GetDesc() const =0
Returns the buffer description used to create the object.