Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/src/common/angleutils.h
1693 views
1
//
2
// Copyright 2002 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
5
//
6
7
// angleutils.h: Common ANGLE utilities.
8
9
#ifndef COMMON_ANGLEUTILS_H_
10
#define COMMON_ANGLEUTILS_H_
11
12
#include "common/platform.h"
13
14
#if defined(ANGLE_USE_ABSEIL)
15
# include "absl/container/flat_hash_map.h"
16
# include "absl/container/flat_hash_set.h"
17
#endif // defined(ANGLE_USE_ABSEIL)
18
19
#if defined(ANGLE_WITH_LSAN)
20
# include <sanitizer/lsan_interface.h>
21
#endif // defined(ANGLE_WITH_LSAN)
22
23
#include <climits>
24
#include <cstdarg>
25
#include <cstddef>
26
#include <set>
27
#include <sstream>
28
#include <string>
29
#include <unordered_map>
30
#include <unordered_set>
31
#include <vector>
32
33
// A helper class to disallow copy and assignment operators
34
namespace angle
35
{
36
37
#if defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
38
using Microsoft::WRL::ComPtr;
39
#endif // defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
40
41
#if defined(ANGLE_USE_ABSEIL)
42
template <typename Key, typename T, class Hash = absl::container_internal::hash_default_hash<Key>>
43
using HashMap = absl::flat_hash_map<Key, T, Hash>;
44
template <typename Key, class Hash = absl::container_internal::hash_default_hash<Key>>
45
using HashSet = absl::flat_hash_set<Key, Hash>;
46
#else
47
template <typename Key, typename T, class Hash = std::hash<Key>>
48
using HashMap = std::unordered_map<Key, T, Hash>;
49
template <typename Key, class Hash = std::hash<Key>>
50
using HashSet = std::unordered_set<Key, Hash>;
51
#endif // defined(ANGLE_USE_ABSEIL)
52
53
class NonCopyable
54
{
55
protected:
56
constexpr NonCopyable() = default;
57
~NonCopyable() = default;
58
59
private:
60
NonCopyable(const NonCopyable &) = delete;
61
void operator=(const NonCopyable &) = delete;
62
};
63
64
extern const uintptr_t DirtyPointer;
65
66
} // namespace angle
67
68
template <typename T, size_t N>
69
constexpr inline size_t ArraySize(T (&)[N])
70
{
71
return N;
72
}
73
74
template <typename T>
75
class WrappedArray final : angle::NonCopyable
76
{
77
public:
78
template <size_t N>
79
constexpr WrappedArray(const T (&data)[N]) : mArray(&data[0]), mSize(N)
80
{}
81
82
constexpr WrappedArray() : mArray(nullptr), mSize(0) {}
83
constexpr WrappedArray(const T *data, size_t size) : mArray(data), mSize(size) {}
84
85
WrappedArray(WrappedArray &&other) : WrappedArray()
86
{
87
std::swap(mArray, other.mArray);
88
std::swap(mSize, other.mSize);
89
}
90
91
~WrappedArray() {}
92
93
constexpr const T *get() const { return mArray; }
94
constexpr size_t size() const { return mSize; }
95
96
private:
97
const T *mArray;
98
size_t mSize;
99
};
100
101
template <typename T, unsigned int N>
102
void SafeRelease(T (&resourceBlock)[N])
103
{
104
for (unsigned int i = 0; i < N; i++)
105
{
106
SafeRelease(resourceBlock[i]);
107
}
108
}
109
110
template <typename T>
111
void SafeRelease(T &resource)
112
{
113
if (resource)
114
{
115
resource->Release();
116
resource = nullptr;
117
}
118
}
119
120
template <typename T>
121
void SafeDelete(T *&resource)
122
{
123
delete resource;
124
resource = nullptr;
125
}
126
127
template <typename T>
128
void SafeDeleteContainer(T &resource)
129
{
130
for (auto &element : resource)
131
{
132
SafeDelete(element);
133
}
134
resource.clear();
135
}
136
137
template <typename T>
138
void SafeDeleteArray(T *&resource)
139
{
140
delete[] resource;
141
resource = nullptr;
142
}
143
144
// Provide a less-than function for comparing structs
145
// Note: struct memory must be initialized to zero, because of packing gaps
146
template <typename T>
147
inline bool StructLessThan(const T &a, const T &b)
148
{
149
return (memcmp(&a, &b, sizeof(T)) < 0);
150
}
151
152
// Provide a less-than function for comparing structs
153
// Note: struct memory must be initialized to zero, because of packing gaps
154
template <typename T>
155
inline bool StructEquals(const T &a, const T &b)
156
{
157
return (memcmp(&a, &b, sizeof(T)) == 0);
158
}
159
160
template <typename T>
161
inline void StructZero(T *obj)
162
{
163
memset(obj, 0, sizeof(T));
164
}
165
166
template <typename T>
167
inline bool IsMaskFlagSet(T mask, T flag)
168
{
169
// Handles multibit flags as well
170
return (mask & flag) == flag;
171
}
172
173
inline const char *MakeStaticString(const std::string &str)
174
{
175
// On the heap so that no destructor runs on application exit.
176
static std::set<std::string> *strings = new std::set<std::string>;
177
std::set<std::string>::iterator it = strings->find(str);
178
if (it != strings->end())
179
{
180
return it->c_str();
181
}
182
183
return strings->insert(str).first->c_str();
184
}
185
186
std::string ArrayString(unsigned int i);
187
188
// Indices are stored in vectors with the outermost index in the back. In the output of the function
189
// the indices are reversed.
190
std::string ArrayIndexString(const std::vector<unsigned int> &indices);
191
192
inline std::string Str(int i)
193
{
194
std::stringstream strstr;
195
strstr << i;
196
return strstr.str();
197
}
198
199
size_t FormatStringIntoVector(const char *fmt, va_list vararg, std::vector<char> &buffer);
200
201
template <typename T>
202
std::string ToString(const T &value)
203
{
204
std::ostringstream o;
205
o << value;
206
return o.str();
207
}
208
209
inline bool IsLittleEndian()
210
{
211
constexpr uint32_t kEndiannessTest = 1;
212
const bool isLittleEndian = *reinterpret_cast<const uint8_t *>(&kEndiannessTest) == 1;
213
return isLittleEndian;
214
}
215
216
// snprintf is not defined with MSVC prior to to msvc14
217
#if defined(_MSC_VER) && _MSC_VER < 1900
218
# define snprintf _snprintf
219
#endif
220
221
#define GL_A1RGB5_ANGLEX 0x6AC5
222
#define GL_BGRX8_ANGLEX 0x6ABA
223
#define GL_BGR565_ANGLEX 0x6ABB
224
#define GL_BGRA4_ANGLEX 0x6ABC
225
#define GL_BGR5_A1_ANGLEX 0x6ABD
226
#define GL_INT_64_ANGLEX 0x6ABE
227
#define GL_UINT_64_ANGLEX 0x6ABF
228
#define GL_BGRA8_SRGB_ANGLEX 0x6AC0
229
#define GL_BGR10_A2_ANGLEX 0x6AF9
230
231
// These are fake formats used to fit typeless D3D textures that can be bound to EGL pbuffers into
232
// the format system (for extension EGL_ANGLE_d3d_texture_client_buffer):
233
#define GL_RGBA8_TYPELESS_ANGLEX 0x6AC1
234
#define GL_RGBA8_TYPELESS_SRGB_ANGLEX 0x6AC2
235
#define GL_BGRA8_TYPELESS_ANGLEX 0x6AC3
236
#define GL_BGRA8_TYPELESS_SRGB_ANGLEX 0x6AC4
237
238
#define GL_R8_SSCALED_ANGLEX 0x6AC6
239
#define GL_RG8_SSCALED_ANGLEX 0x6AC7
240
#define GL_RGB8_SSCALED_ANGLEX 0x6AC8
241
#define GL_RGBA8_SSCALED_ANGLEX 0x6AC9
242
#define GL_R8_USCALED_ANGLEX 0x6ACA
243
#define GL_RG8_USCALED_ANGLEX 0x6ACB
244
#define GL_RGB8_USCALED_ANGLEX 0x6ACC
245
#define GL_RGBA8_USCALED_ANGLEX 0x6ACD
246
247
#define GL_R16_SSCALED_ANGLEX 0x6ACE
248
#define GL_RG16_SSCALED_ANGLEX 0x6ACF
249
#define GL_RGB16_SSCALED_ANGLEX 0x6AD0
250
#define GL_RGBA16_SSCALED_ANGLEX 0x6AD1
251
#define GL_R16_USCALED_ANGLEX 0x6AD2
252
#define GL_RG16_USCALED_ANGLEX 0x6AD3
253
#define GL_RGB16_USCALED_ANGLEX 0x6AD4
254
#define GL_RGBA16_USCALED_ANGLEX 0x6AD5
255
256
#define GL_R32_SSCALED_ANGLEX 0x6AD6
257
#define GL_RG32_SSCALED_ANGLEX 0x6AD7
258
#define GL_RGB32_SSCALED_ANGLEX 0x6AD8
259
#define GL_RGBA32_SSCALED_ANGLEX 0x6AD9
260
#define GL_R32_USCALED_ANGLEX 0x6ADA
261
#define GL_RG32_USCALED_ANGLEX 0x6ADB
262
#define GL_RGB32_USCALED_ANGLEX 0x6ADC
263
#define GL_RGBA32_USCALED_ANGLEX 0x6ADD
264
265
#define GL_R32_SNORM_ANGLEX 0x6ADE
266
#define GL_RG32_SNORM_ANGLEX 0x6ADF
267
#define GL_RGB32_SNORM_ANGLEX 0x6AE0
268
#define GL_RGBA32_SNORM_ANGLEX 0x6AE1
269
#define GL_R32_UNORM_ANGLEX 0x6AE2
270
#define GL_RG32_UNORM_ANGLEX 0x6AE3
271
#define GL_RGB32_UNORM_ANGLEX 0x6AE4
272
#define GL_RGBA32_UNORM_ANGLEX 0x6AE5
273
274
#define GL_R32_FIXED_ANGLEX 0x6AE6
275
#define GL_RG32_FIXED_ANGLEX 0x6AE7
276
#define GL_RGB32_FIXED_ANGLEX 0x6AE8
277
#define GL_RGBA32_FIXED_ANGLEX 0x6AE9
278
279
#define GL_RGB10_A2_SINT_ANGLEX 0x6AEA
280
#define GL_RGB10_A2_SNORM_ANGLEX 0x6AEB
281
#define GL_RGB10_A2_SSCALED_ANGLEX 0x6AEC
282
#define GL_RGB10_A2_USCALED_ANGLEX 0x6AED
283
284
// EXT_texture_type_2_10_10_10_REV
285
#define GL_RGB10_UNORM_ANGLEX 0x6AEE
286
287
// These are fake formats for OES_vertex_type_10_10_10_2
288
#define GL_A2_RGB10_UNORM_ANGLEX 0x6AEF
289
#define GL_A2_RGB10_SNORM_ANGLEX 0x6AF0
290
#define GL_A2_RGB10_USCALED_ANGLEX 0x6AF1
291
#define GL_A2_RGB10_SSCALED_ANGLEX 0x6AF2
292
#define GL_X2_RGB10_UINT_ANGLEX 0x6AF3
293
#define GL_X2_RGB10_SINT_ANGLEX 0x6AF4
294
#define GL_X2_RGB10_USCALED_ANGLEX 0x6AF5
295
#define GL_X2_RGB10_SSCALED_ANGLEX 0x6AF6
296
#define GL_X2_RGB10_UNORM_ANGLEX 0x6AF7
297
#define GL_X2_RGB10_SNORM_ANGLEX 0x6AF8
298
299
#define ANGLE_CHECK_GL_ALLOC(context, result) \
300
ANGLE_CHECK(context, result, "Failed to allocate host memory", GL_OUT_OF_MEMORY)
301
302
#define ANGLE_CHECK_GL_MATH(context, result) \
303
ANGLE_CHECK(context, result, "Integer overflow.", GL_INVALID_OPERATION)
304
305
#define ANGLE_GL_UNREACHABLE(context) \
306
UNREACHABLE(); \
307
ANGLE_CHECK(context, false, "Unreachable Code.", GL_INVALID_OPERATION)
308
309
#if defined(ANGLE_WITH_LSAN)
310
# define ANGLE_SCOPED_DISABLE_LSAN() __lsan::ScopedDisabler lsanDisabler
311
#else
312
# define ANGLE_SCOPED_DISABLE_LSAN()
313
#endif
314
315
// The ANGLE_NO_SANITIZE_MEMORY macro suppresses MemorySanitizer checks for
316
// use-of-uninitialized-data. It can be used to decorate functions with known
317
// false positives.
318
#ifdef __clang__
319
# define ANGLE_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
320
#else
321
# define ANGLE_NO_SANITIZE_MEMORY
322
#endif
323
324
// Similar to the above, but for thread sanitization.
325
#ifdef __clang__
326
# define ANGLE_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
327
#else
328
# define ANGLE_NO_SANITIZE_THREAD
329
#endif
330
331
// The below inlining code lifted from V8.
332
#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
333
# define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline))
334
# define ANGLE_HAS___FORCEINLINE 0
335
#elif defined(_MSC_VER)
336
# define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0
337
# define ANGLE_HAS___FORCEINLINE 1
338
#else
339
# define ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE 0
340
# define ANGLE_HAS___FORCEINLINE 0
341
#endif
342
343
#if defined(NDEBUG) && ANGLE_HAS_ATTRIBUTE_ALWAYS_INLINE
344
# define ANGLE_INLINE inline __attribute__((always_inline))
345
#elif defined(NDEBUG) && ANGLE_HAS___FORCEINLINE
346
# define ANGLE_INLINE __forceinline
347
#else
348
# define ANGLE_INLINE inline
349
#endif
350
351
#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
352
# if __has_attribute(noinline)
353
# define ANGLE_NOINLINE __attribute__((noinline))
354
# else
355
# define ANGLE_NOINLINE
356
# endif
357
#elif defined(_MSC_VER)
358
# define ANGLE_NOINLINE __declspec(noinline)
359
#else
360
# define ANGLE_NOINLINE
361
#endif
362
363
#if defined(__clang__) || (defined(__GNUC__) && defined(__has_attribute))
364
# if __has_attribute(format)
365
# define ANGLE_FORMAT_PRINTF(fmt, args) __attribute__((format(__printf__, fmt, args)))
366
# else
367
# define ANGLE_FORMAT_PRINTF(fmt, args)
368
# endif
369
#else
370
# define ANGLE_FORMAT_PRINTF(fmt, args)
371
#endif
372
373
// Format messes up the # inside the macro.
374
// clang-format off
375
#ifndef ANGLE_STRINGIFY
376
# define ANGLE_STRINGIFY(x) #x
377
#endif
378
// clang-format on
379
380
#ifndef ANGLE_MACRO_STRINGIFY
381
# define ANGLE_MACRO_STRINGIFY(x) ANGLE_STRINGIFY(x)
382
#endif
383
384
// Detect support for C++17 [[nodiscard]]
385
#if !defined(__has_cpp_attribute)
386
# define __has_cpp_attribute(name) 0
387
#endif // !defined(__has_cpp_attribute)
388
389
#if __has_cpp_attribute(nodiscard)
390
# define ANGLE_NO_DISCARD [[nodiscard]]
391
#else
392
# define ANGLE_NO_DISCARD
393
#endif // __has_cpp_attribute(nodiscard)
394
395
#if __has_cpp_attribute(maybe_unused)
396
# define ANGLE_MAYBE_UNUSED [[maybe_unused]]
397
#else
398
# define ANGLE_MAYBE_UNUSED
399
#endif // __has_cpp_attribute(maybe_unused)
400
401
#if __has_cpp_attribute(require_constant_initialization)
402
# define ANGLE_REQUIRE_CONSTANT_INIT [[require_constant_initialization]]
403
#else
404
# define ANGLE_REQUIRE_CONSTANT_INIT
405
#endif // __has_cpp_attribute(require_constant_initialization)
406
407
#endif // COMMON_ANGLEUTILS_H_
408
409