forked from baldurk/renderdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
391 lines (338 loc) · 10.7 KB
/
common.h
File metadata and controls
391 lines (338 loc) · 10.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2015-2017 Baldur Karlsson
* Copyright (c) 2014 Crytek
*
* 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.
******************************************************************************/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "api/replay/renderdoc_replay.h"
#include "globalconfig.h"
/////////////////////////////////////////////////
// Utility macros
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
do \
{ \
if(p) \
{ \
delete(p); \
(p) = NULL; \
} \
} while((void)0, 0)
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) \
do \
{ \
if(p) \
{ \
delete[](p); \
(p) = NULL; \
} \
} while((void)0, 0)
#endif
#ifndef SAFE_ADDREF
#define SAFE_ADDREF(p) \
do \
{ \
if(p) \
{ \
(p)->AddRef(); \
} \
} while((void)0, 0)
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) \
do \
{ \
if(p) \
{ \
(p)->Release(); \
(p) = NULL; \
} \
} while((void)0, 0)
#define SAFE_RELEASE_NOCLEAR(p) \
do \
{ \
if(p) \
{ \
(p)->Release(); \
} \
} while((void)0, 0)
#endif
#ifndef ARRAY_COUNT
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
#endif
#define STRINGIZE2(a) #a
#define STRINGIZE(a) STRINGIZE2(a)
#define CONCAT2(a, b) a##b
#define CONCAT(a, b) CONCAT2(a, b)
#include "os/os_specific.h"
#define RDCEraseMem(a, b) memset(a, 0, b)
#define RDCEraseEl(a) memset(&a, 0, sizeof(a))
template <typename T>
T RDCCLAMP(const T &val, const T &mn, const T &mx)
{
return val < mn ? mn : (val > mx ? mx : val);
}
template <typename T>
T RDCMIN(const T &a, const T &b)
{
return a < b ? a : b;
}
template <typename T>
T RDCMAX(const T &a, const T &b)
{
return a > b ? a : b;
}
template <typename T>
T RDCLERP(const T &a, const T &b, const T &step)
{
return (1.0f - step) * a + step * b;
}
template <typename T>
inline T AlignUp4(T x)
{
return (x + 0x3) & (~0x3);
}
template <typename T>
inline T AlignUp16(T x)
{
return (x + 0xf) & (~0xf);
}
template <typename T>
inline T AlignUp(T x, T a)
{
return (x + (a - 1)) & (~(a - 1));
}
template <typename T, typename A>
inline T AlignUpPtr(T x, A a)
{
return (T)AlignUp<uintptr_t>((uintptr_t)x, (uintptr_t)a);
}
#define MAKE_FOURCC(a, b, c, d) \
(((uint32_t)(d) << 24) | ((uint32_t)(c) << 16) | ((uint32_t)(b) << 8) | (uint32_t)(a))
bool FindDiffRange(void *a, void *b, size_t bufSize, size_t &diffStart, size_t &diffEnd);
uint32_t CalcNumMips(int Width, int Height, int Depth);
uint32_t Log2Floor(uint32_t value);
#if ENABLED(RDOC_X64)
uint64_t Log2Floor(uint64_t value);
#endif
// super ugly - on apple size_t is a separate type, so we need a new overload
#if ENABLED(RDOC_APPLE)
inline size_t Log2Floor(size_t value)
{
#if ENABLED(RDOC_X64)
return (size_t)Log2Floor((uint64_t)value);
#else
return (size_t)Log2Floor((uint32_t)value);
#endif
}
#endif
template <typename T, BucketRecordType bucketType = T::BucketType>
struct BucketForRecord
{
static size_t Get(size_t value);
};
template <typename T>
struct BucketForRecord<T, BucketRecordType::Linear>
{
static size_t Get(size_t value)
{
const size_t size = T::BucketSize;
const size_t count = T::BucketCount;
const size_t maximum = size * count;
const size_t index = (value < maximum) ? (value / size) : (count - 1);
return index;
}
};
template <typename T>
struct BucketForRecord<T, BucketRecordType::Pow2>
{
static size_t Get(size_t value)
{
const size_t count = T::BucketCount;
static_assert(count <= (sizeof(size_t) * 8),
"Unexpected correspondence between bucket size and sizeof(size_t)");
const size_t maximum = (size_t)1 << count;
const size_t index = (value < maximum) ? (size_t)(Log2Floor(value)) : (count - 1);
return index;
}
};
/////////////////////////////////////////////////
// Debugging features
#define RDCDUMP() \
do \
{ \
OSUtility::ForceCrash(); \
} while((void)0, 0)
#if ENABLED(RDOC_DEVEL) || ENABLED(FORCE_DEBUGBREAK)
#define RDCBREAK() \
do \
{ \
if(OSUtility::DebuggerPresent()) \
OS_DEBUG_BREAK(); \
} while((void)0, 0)
#else
#define RDCBREAK() \
do \
{ \
} while((void)0, 0)
#endif
#define RDCUNIMPLEMENTED(...) \
do \
{ \
rdclog(LogType::Warning, "Unimplemented: " __VA_ARGS__); \
RDCBREAK(); \
} while((void)0, 0)
//
// Logging
//
#if ENABLED(STRIP_LOG)
#define RDCLOGFILE(fn) \
do \
{ \
} while((void)0, 0)
#define RDCLOGDELETE() \
do \
{ \
} while((void)0, 0)
#define RDCDEBUG(...) \
do \
{ \
} while((void)0, 0)
#define RDCLOG(...) \
do \
{ \
} while((void)0, 0)
#define RDCWARN(...) \
do \
{ \
} while((void)0, 0)
#define RDCERR(...) \
do \
{ \
} while((void)0, 0)
#define RDCFATAL(...) \
do \
{ \
RDCDUMP(); \
exit(0); \
} while((void)0, 0)
#define RDCDUMPMSG(message) \
do \
{ \
RDCDUMP(); \
exit(0); \
} while((void)0, 0)
#else
// perform any operations necessary to flush the log
void rdclog_flush();
// actual low-level print to log output streams defined (useful for if we need to print
// fatal error messages from within the more complex log function).
void rdclogprint_int(LogType type, const char *fullMsg, const char *msg);
#if !defined(RDCLOG_PROJECT)
#define RDCLOG_PROJECT "RDOC"
#endif
// printf() style main logger function
void rdclog_int(LogType type, const char *project, const char *file, unsigned int line,
const char *fmt, ...);
#define rdclog(type, ...) rdclog_int(type, RDCLOG_PROJECT, __FILE__, __LINE__, __VA_ARGS__)
const char *rdclog_getfilename();
void rdclog_filename(const char *filename);
void rdclog_enableoutput();
void rdclog_closelog(const char *filename);
#define RDCLOGFILE(fn) rdclog_filename(fn)
#define RDCGETLOGFILE() rdclog_getfilename()
#define RDCLOGOUTPUT() rdclog_enableoutput()
#define RDCSTOPLOGGING(filename) rdclog_closelog(filename)
#if(ENABLED(RDOC_DEVEL) || ENABLED(FORCE_DEBUG_LOGS)) && DISABLED(STRIP_DEBUG_LOGS)
#define RDCDEBUG(...) rdclog(LogType::Debug, __VA_ARGS__)
#else
#define RDCDEBUG(...) \
do \
{ \
} while((void)0, 0)
#endif
#define RDCLOG(...) rdclog(LogType::Comment, __VA_ARGS__)
#define RDCWARN(...) rdclog(LogType::Warning, __VA_ARGS__)
#if ENABLED(DEBUGBREAK_ON_ERROR_LOG)
#define RDCERR(...) \
do \
{ \
rdclog(LogType::Error, __VA_ARGS__); \
rdclog_flush(); \
RDCBREAK(); \
} while((void)0, 0)
#else
#define RDCERR(...) rdclog(LogType::Error, __VA_ARGS__)
#endif
#define RDCFATAL(...) \
do \
{ \
rdclog(LogType::Fatal, __VA_ARGS__); \
rdclog_flush(); \
RDCDUMP(); \
exit(0); \
} while((void)0, 0)
#define RDCDUMPMSG(message) \
do \
{ \
rdclogprint_int(LogType::Fatal, message, message); \
rdclog_flush(); \
RDCDUMP(); \
exit(0); \
} while((void)0, 0)
#endif
//
// Assert
//
#if ENABLED(RDOC_DEVEL) || ENABLED(FORCE_ASSERTS)
void rdcassert(const char *msg, const char *file, unsigned int line, const char *func);
// this defines the root macro, RDCASSERTMSG(msg, cond, ...)
// where it will check cond, then print msg (if it's not "") and the values of all values passed via
// varargs.
// the other asserts are defined in terms of that
#include "custom_assert.h"
#else
#define RDCASSERTMSG(cond) \
do \
{ \
} while((void)0, 0)
#endif
#define RDCASSERT(...) RDCASSERTMSG("", __VA_ARGS__)
#define RDCASSERTEQUAL(a, b) RDCASSERTMSG("", a == b, a, b)
#define RDCASSERTNOTEQUAL(a, b) RDCASSERTMSG("", a != b, a, b)
//
// Compile asserts
//
#if ENABLED(STRIP_COMPILE_ASSERTS)
#define RDCCOMPILE_ASSERT(condition, message) \
do \
{ \
} while((void)0, 0)
#else
#define RDCCOMPILE_ASSERT(condition, message) static_assert(condition, message)
#endif
typedef uint8_t byte;