File: dx12_object_wrapper_util.h

package info (click to toggle)
gfxreconstruct 0.9.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,636 kB
  • sloc: cpp: 328,961; ansic: 25,454; python: 18,156; xml: 255; sh: 128; makefile: 6
file content (277 lines) | stat: -rw-r--r-- 12,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
** Copyright (c) 2021 LunarG, Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/

#ifndef GFXRECON_ENCODE_DX12_OBJECT_WRAPPER_UTIL_H
#define GFXRECON_ENCODE_DX12_OBJECT_WRAPPER_UTIL_H

#include "encode/handle_unwrap_memory.h"
#include "encode/iunknown_wrapper.h"
#include "format/format.h"
#include "util/defines.h"

#include <mutex>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(encode)

//----------------------------------------------------------------------------
/// \brief Retrieves an object from an object wrapper.
///
/// Retrieves a wrapped object from an object wrapper.  Assumes that
/// #wrapped_object, when not null, is a valid wrapped object.  The IUnknown
/// function overload should be used when #wrapped_object is not guaranteed
/// to be an object wrapper.
///
/// \param wrapped_object Pointer to the object wrapper to be processed.
///
/// \return A pointer to the object retrieved from the object wrapper, or
///         nullptr if #wrapped_object was null.
//----------------------------------------------------------------------------
template <typename Object>
Object* GetWrappedObject(Object* wrapped_object)
{
    return (wrapped_object != nullptr)
               ? reinterpret_cast<IUnknown_Wrapper*>(wrapped_object)->GetWrappedObjectAs<Object>()
               : nullptr;
}

//----------------------------------------------------------------------------
/// \brief Retrieves a const object from a const object wrapper.
///
/// Retrieves a wrapped object from a const object wrapper.  Assumes that
/// #wrapped_object, when not null, is a valid wrapped object.  The IUnknown
/// function overload should be used when #wrapped_object is not guaranteed
/// to be an object wrapper.
///
/// \param wrapped_object Const pointer to the object wrapper to be processed.
///
/// \return A const pointer to the object retrieved from the object wrapper,
///         or nullptr if #wrapped_object was null.
//----------------------------------------------------------------------------
template <typename Object>
const Object* GetWrappedObject(const Object* wrapped_object)
{
    return (wrapped_object != nullptr)
               ? reinterpret_cast<const IUnknown_Wrapper*>(wrapped_object)->GetWrappedObjectAs<Object>()
               : nullptr;
}

//----------------------------------------------------------------------------
/// \brief Retrieves an object from an object wrapper.
///
/// Retrieves a wrapped object from an object wrapper.  This version of the
/// function is overloaded for the IUnknown type, which uses
/// IUnknown::QueryInterface() to determine if #wrapped_object is really
/// an object wrapper before attempting to process it for unwrapping.  If the
/// query fails, unwrapping will be skipped and the value of the unmodified
/// #wrapped_object parameter will be returned.
///
/// \param wrapped_object Pointer to the object wrapper to be processed.
///
/// \return A pointer to the object retrieved from the object wrapper, or
///         nullptr if #wrapped_object was either null or was not a valid
///         object wrapper.
//----------------------------------------------------------------------------
template <>
IUnknown* GetWrappedObject<IUnknown>(IUnknown* wrapped_object);

//----------------------------------------------------------------------------
/// \brief Retrieves a const object from a const object wrapper.
///
/// Retrieves a const wrapped object from a const object wrapper.  This
/// version of the function is overloaded for the IUnknown type, which
/// uses IUnknown::QueryInterface() to determine if #wrapped_object is really
/// an object wrapper before attempting to process it for unwrapping.  If the
/// query fails, unwrapping will be skipped and the value of the unmodified
/// #wrapped_object parameter will be returned.
///
/// \param wrapped_object Pointer to the object wrapper to be processed.
///
/// \return A pointer to the object retrieved from the object wrapper, or
///         nullptr if #wrapped_object was either null or was not a valid
///         object wrapper.
//----------------------------------------------------------------------------
template <>
const IUnknown* GetWrappedObject<IUnknown>(const IUnknown* wrapped_object);

//----------------------------------------------------------------------------
/// \brief Retrieves an object ID from an object wrapper.
///
/// Retrieves an object ID from an object wrapper.  Assumes that
/// #wrapped_object, when not null, is a valid wrapped object.  The IUnknown
/// function overload should be used when #wrapped_object is not guaranteed
/// to be an object wrapper.
///
/// \param wrapped_object Pointer to the object wrapper to be processed.
///
/// \return The unique object ID retrieved from the object wrapper, or
///         format::kNullHandleId if #wrapped_object was null.
//----------------------------------------------------------------------------
template <typename Object>
format::HandleId GetDx12WrappedId(const Object* wrapped_object)
{
    return (wrapped_object != nullptr) ? reinterpret_cast<const IUnknown_Wrapper*>(wrapped_object)->GetCaptureId()
                                       : format::kNullHandleId;
}

//----------------------------------------------------------------------------
/// \brief Retrieves an object ID from a const object wrapper.
///
/// Retrieves an object ID from a const object wrapper.  This version of the
/// function is overloaded for the IUnknown type, which uses
/// IUnknown::QueryInterface() to determine if #wrapped_object is really
/// an object wrapper before attempting to retrieve an ID from it.  If the
/// query fails, ID retrieval will be skipped and format::kNullHandleId will
/// be returned.
///
/// \param wrapped_object Pointer to the object wrapper to be processed.
///
/// \return The unique object ID retrieved from the object wrapper, or
///         format::kNullHandleId if #wrapped_object was either null or was
///         not a valid object wrapper.
//----------------------------------------------------------------------------
template <>
format::HandleId GetDx12WrappedId<IUnknown>(const IUnknown* wrapped_object);

//----------------------------------------------------------------------------
/// \brief Unwraps an array of handles.
///
/// Returns an array of unwrapped objects retrieved from the objects in an
/// array of object wrappers.  The unwrapped object will be stored at the
/// same index in the array as the object wrapper that is was retrieved from.
/// The caller must provide a pointer to a HandleUnwrapMemory object that
/// will provide the memory used to store the unwrapped objects.  If the
/// pointer to the object wrapper array is null, or the array size is zero,
/// the pointer to the object wrapper array will be returned with no other
/// action performed.
///
/// \param objects       Pointer to an array of object wrappers to be
///                      processed for unwrapping.
/// \param len           Number of items in the array of object wrappers.
/// \param unwrap_memory Pointer to a HandleUnwrapMemory object that will
///                      provide the memory used to store the unwrapped
///                      objects.
///
/// \return Pointer to the array of unwrapped objects, stored in memory
///         provided by the HandleUnwrapMemory object, if the array of
///         object wrappers is not empty.  Pointer to the array of object
///         wrappers if the array is empty.
//----------------------------------------------------------------------------
template <typename Object>
Object* const* UnwrapObjects(Object* const* objects, uint32_t len, HandleUnwrapMemory* unwrap_memory)
{
    if ((objects != nullptr) && (len > 0))
    {
        assert(unwrap_memory != nullptr);

        size_t num_bytes         = len * sizeof(Object*);
        auto   unwrapped_objects = reinterpret_cast<Object**>(unwrap_memory->GetBuffer(num_bytes));

        for (uint32_t i = 0; i < len; ++i)
        {
            unwrapped_objects[i] = GetWrappedObject<Object>(objects[i]);
        }

        return unwrapped_objects;
    }

    // Leave the original memory in place when the pointer is not null, but size is zero.
    return objects;
}

//----------------------------------------------------------------------------
/// \brief Utility function to add an object wrapper to a table.
///
/// Add an object wrapper entry to a table keyed by the pointer of the
/// unwrapped object.
///
/// \param object          A pointer to the unwrapped object to be used as
///                        the key for the map entry.
/// \param wrapper         A pointer to the wrapper to be added to the map.
/// \param object_map      A reference to the map that the wrapper will be
///                        added to.
/// \param object_map_lock A reference to a std::mutex to be locked when the
///                        object_map is modified.
//----------------------------------------------------------------------------
template <typename Object, typename Wrapper, typename Map>
void AddWrapperMapEntry(Object* object, Wrapper* wrapper, Map& object_map, std::mutex& object_map_lock)
{
    std::lock_guard<std::mutex> lock(object_map_lock);
    object_map[object] = wrapper;
}

//----------------------------------------------------------------------------
/// \brief Utility function to remove an object wrapper from a table.
///
/// Remove an object wrapper entry from a table.
///
/// \param object          A pointer to the unwrapped object to be used as
///                        the key for the map entry.
/// \param object_map      A reference to the map that the wrapper will be
///                        removed from.
/// \param object_map_lock A reference to a std::mutex to be locked when the
///                        object_map is modified.
//----------------------------------------------------------------------------
template <typename Object, typename Map>
void RemoveWrapperMapEntry(Object* object, Map& object_map, std::mutex& object_map_lock)
{
    std::lock_guard<std::mutex> lock(object_map_lock);
    object_map.erase(object);
}

//----------------------------------------------------------------------------
/// \brief Utility function to retrieve an object wrapper from a table.
///
/// Retreive an object wrapper entry from a table.
///
/// \param object          A pointer to the unwrapped object to be used as
///                        the key for the map entry.
/// \param object_map      A reference to the map that the wrapper will be
///                        retrieved from.
/// \param object_map_lock A reference to a std::mutex to be locked when the
///                        object_map is accessed.
///
/// \return A pointer to the wrapper object retrieved from the map.
//----------------------------------------------------------------------------
template <typename Wrapper, typename Object, typename Map>
Wrapper* FindMapEntry(Object* object, Map& object_map, std::mutex& object_map_lock)
{
    Wrapper*            wrapper = nullptr;
    Map::const_iterator entry;

    {
        std::lock_guard<std::mutex> lock(object_map_lock);
        entry = object_map.find(object);
    }

    if (entry != object_map.end())
    {
        wrapper = entry->second;
    }

    return wrapper;
}

GFXRECON_END_NAMESPACE(encode)
GFXRECON_END_NAMESPACE(gfxrecon)

#endif // GFXRECON_ENCODE_DX12_OBJECT_WRAPPER_UTIL_H