slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
slang-cpp-prelude.h
浏览该文件的文档.
1#ifndef SLANG_CPP_PRELUDE_H
2#define SLANG_CPP_PRELUDE_H
3
4// Because the signature of isnan, isfinite, and is isinf changed in C++, we use the macro
5// to use the version in the std namespace.
6// https://stackoverflow.com/questions/39130040/cmath-hides-isnan-in-math-h-in-c14-c11
7
8#ifdef SLANG_LLVM
9#include "slang-llvm.h"
10#else // SLANG_LLVM
11#if SLANG_GCC_FAMILY && __GNUC__ < 6
12#include <cmath>
13#define SLANG_PRELUDE_STD std::
14#else
15#include <math.h>
16#define SLANG_PRELUDE_STD
17#endif
18
19#include <assert.h>
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23#endif // SLANG_LLVM
24
25// Is intptr_t not equal to equal-width sized integer type?
26#if defined(__APPLE__)
27#define SLANG_INTPTR_TYPE_IS_DISTINCT 1
28#else
29#define SLANG_INTPTR_TYPE_IS_DISTINCT 0
30#endif
31
32#if defined(_MSC_VER)
33#define SLANG_PRELUDE_SHARED_LIB_EXPORT __declspec(dllexport)
34#else
35#define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__((__visibility__("default")))
36// # define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__ ((dllexport))
37// __attribute__((__visibility__("default")))
38#endif
39
40#ifdef __cplusplus
41#define SLANG_PRELUDE_EXTERN_C extern "C"
42#define SLANG_PRELUDE_EXTERN_C_START \
43 extern "C" \
44 {
45#define SLANG_PRELUDE_EXTERN_C_END }
46#else
47#define SLANG_PRELUDE_EXTERN_C
48#define SLANG_PRELUDE_EXTERN_C_START
49#define SLANG_PRELUDE_EXTERN_C_END
50#endif
51
52#define SLANG_PRELUDE_EXPORT SLANG_PRELUDE_EXTERN_C SLANG_PRELUDE_SHARED_LIB_EXPORT
53#define SLANG_PRELUDE_EXPORT_START SLANG_PRELUDE_EXTERN_C_START SLANG_PRELUDE_SHARED_LIB_EXPORT
54#define SLANG_PRELUDE_EXPORT_END SLANG_PRELUDE_EXTERN_C_END
55
56#ifndef INFINITY
57// Must overflow for double
58#define INFINITY float(1e+300 * 1e+300)
59#endif
60
61#ifndef SLANG_INFINITY
62#define SLANG_INFINITY INFINITY
63#endif
64
65// Detect the compiler type
66
67#ifndef SLANG_COMPILER
68#define SLANG_COMPILER
69
70/*
71Compiler defines, see http://sourceforge.net/p/predef/wiki/Compilers/
72NOTE that SLANG_VC holds the compiler version - not just 1 or 0
73*/
74#if defined(_MSC_VER)
75#if _MSC_VER >= 1900
76#define SLANG_VC 14
77#elif _MSC_VER >= 1800
78#define SLANG_VC 12
79#elif _MSC_VER >= 1700
80#define SLANG_VC 11
81#elif _MSC_VER >= 1600
82#define SLANG_VC 10
83#elif _MSC_VER >= 1500
84#define SLANG_VC 9
85#else
86#error "unknown version of Visual C++ compiler"
87#endif
88#elif defined(__clang__)
89#define SLANG_CLANG 1
90#elif defined(__SNC__)
91#define SLANG_SNC 1
92#elif defined(__ghs__)
93#define SLANG_GHS 1
94#elif defined(__GNUC__) /* note: __clang__, __SNC__, or __ghs__ imply __GNUC__ */
95#define SLANG_GCC 1
96#else
97#error "unknown compiler"
98#endif
99/*
100Any compilers not detected by the above logic are now now explicitly zeroed out.
101*/
102#ifndef SLANG_VC
103#define SLANG_VC 0
104#endif
105#ifndef SLANG_CLANG
106#define SLANG_CLANG 0
107#endif
108#ifndef SLANG_SNC
109#define SLANG_SNC 0
110#endif
111#ifndef SLANG_GHS
112#define SLANG_GHS 0
113#endif
114#ifndef SLANG_GCC
115#define SLANG_GCC 0
116#endif
117#endif /* SLANG_COMPILER */
118
119/*
120The following section attempts to detect the target platform being compiled for.
121
122If an application defines `SLANG_PLATFORM` before including this header,
123they take responsibility for setting any compiler-dependent macros
124used later in the file.
125
126Most applications should not need to touch this section.
127*/
128#ifndef SLANG_PLATFORM
129#define SLANG_PLATFORM
133#if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_PARTITION_APP
134#define SLANG_WINRT 1 /* Windows Runtime, either on Windows RT or Windows 8 */
135#elif defined(XBOXONE)
136#define SLANG_XBOXONE 1
137#elif defined(_WIN64) /* note: XBOXONE implies _WIN64 */
138#define SLANG_WIN64 1
139#elif defined(_M_PPC)
140#define SLANG_X360 1
141#elif defined(_WIN32) /* note: _M_PPC implies _WIN32 */
142#define SLANG_WIN32 1
143#elif defined(__ANDROID__)
144#define SLANG_ANDROID 1
145#elif defined(__linux__) || defined(__CYGWIN__) /* note: __ANDROID__ implies __linux__ */
146#define SLANG_LINUX 1
147#elif defined(__APPLE__) && !defined(SLANG_LLVM)
148#include "TargetConditionals.h"
149#if TARGET_OS_MAC
150#define SLANG_OSX 1
151#else
152#define SLANG_IOS 1
153#endif
154#elif defined(__APPLE__)
155// On `slang-llvm` we can't inclue "TargetConditionals.h" in general, so for now assume its
156// OSX.
157#define SLANG_OSX 1
158#elif defined(__CELLOS_LV2__)
159#define SLANG_PS3 1
160#elif defined(__ORBIS__)
161#define SLANG_PS4 1
162#elif defined(__SNC__) && defined(__arm__)
163#define SLANG_PSP2 1
164#elif defined(__ghs__)
165#define SLANG_WIIU 1
166#else
167#error "unknown target platform"
168#endif
169
170
171/*
172Any platforms not detected by the above logic are now now explicitly zeroed out.
173*/
174#ifndef SLANG_WINRT
175#define SLANG_WINRT 0
176#endif
177#ifndef SLANG_XBOXONE
178#define SLANG_XBOXONE 0
179#endif
180#ifndef SLANG_WIN64
181#define SLANG_WIN64 0
182#endif
183#ifndef SLANG_X360
184#define SLANG_X360 0
185#endif
186#ifndef SLANG_WIN32
187#define SLANG_WIN32 0
188#endif
189#ifndef SLANG_ANDROID
190#define SLANG_ANDROID 0
191#endif
192#ifndef SLANG_LINUX
193#define SLANG_LINUX 0
194#endif
195#ifndef SLANG_IOS
196#define SLANG_IOS 0
197#endif
198#ifndef SLANG_OSX
199#define SLANG_OSX 0
200#endif
201#ifndef SLANG_PS3
202#define SLANG_PS3 0
203#endif
204#ifndef SLANG_PS4
205#define SLANG_PS4 0
206#endif
207#ifndef SLANG_PSP2
208#define SLANG_PSP2 0
209#endif
210#ifndef SLANG_WIIU
211#define SLANG_WIIU 0
212#endif
213#endif /* SLANG_PLATFORM */
214
215/* Shorthands for "families" of compilers/platforms */
216#define SLANG_GCC_FAMILY (SLANG_CLANG || SLANG_SNC || SLANG_GHS || SLANG_GCC)
217#define SLANG_WINDOWS_FAMILY (SLANG_WINRT || SLANG_WIN32 || SLANG_WIN64)
218#define SLANG_MICROSOFT_FAMILY (SLANG_XBOXONE || SLANG_X360 || SLANG_WINDOWS_FAMILY)
219#define SLANG_LINUX_FAMILY (SLANG_LINUX || SLANG_ANDROID)
220#define SLANG_APPLE_FAMILY (SLANG_IOS || SLANG_OSX) /* equivalent to #if __APPLE__ */
221#define SLANG_UNIX_FAMILY \
222 (SLANG_LINUX_FAMILY || SLANG_APPLE_FAMILY) /* shortcut for unix/posix platforms */
223
224// GCC Specific
225#if SLANG_GCC_FAMILY
226
227#if INTPTR_MAX == INT64_MAX
228#define SLANG_64BIT 1
229#else
230#define SLANG_64BIT 0
231#endif
232
233#define SLANG_BREAKPOINT(id) __builtin_trap()
234
235// Use this macro instead of offsetof, because gcc produces warning if offsetof is used on a
236// non POD type, even though it produces the correct result
237#define SLANG_OFFSET_OF(T, ELEMENT) (size_t(&((T*)1)->ELEMENT) - 1)
238#endif // SLANG_GCC_FAMILY
239
240// Microsoft VC specific
241#if SLANG_VC
242
243#define SLANG_BREAKPOINT(id) __debugbreak();
244
245#endif // SLANG_VC
246
247// Default impls
248
249#ifndef SLANG_OFFSET_OF
250#define SLANG_OFFSET_OF(X, Y) offsetof(X, Y)
251#endif
252
253#ifndef SLANG_BREAKPOINT
254// Make it crash with a write to 0!
255#define SLANG_BREAKPOINT(id) (*((int*)0) = int(id));
256#endif
257
258// If slang.h has been included we don't need any of these definitions
259#ifndef SLANG_H
260
261/* Macro for declaring if a method is no throw. Should be set before the return parameter. */
262#ifndef SLANG_NO_THROW
263#if SLANG_WINDOWS_FAMILY && !defined(SLANG_DISABLE_EXCEPTIONS)
264#define SLANG_NO_THROW __declspec(nothrow)
265#endif
266#endif
267#ifndef SLANG_NO_THROW
268#define SLANG_NO_THROW
269#endif
270
271/* The `SLANG_STDCALL` and `SLANG_MCALL` defines are used to set the calling
272convention for interface methods.
273*/
274#ifndef SLANG_STDCALL
275#if SLANG_MICROSOFT_FAMILY
276#define SLANG_STDCALL __stdcall
277#else
278#define SLANG_STDCALL
279#endif
280#endif
281#ifndef SLANG_MCALL
282#define SLANG_MCALL SLANG_STDCALL
283#endif
284
285#ifndef SLANG_FORCE_INLINE
286#define SLANG_FORCE_INLINE inline
287#endif
288
289// TODO(JS): Should these be in slang-cpp-types.h?
290// They are more likely to clash with slang.h
291
293{
294 uint32_t data1;
295 uint16_t data2;
296 uint16_t data3;
297 uint8_t data4[8];
298};
299
300typedef int32_t SlangResult;
301
303{
305 queryInterface(SlangUUID const& uuid, void** outObject) = 0;
306 virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() = 0;
307 virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() = 0;
308};
309
310#define SLANG_COM_INTERFACE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
311public: \
312 SLANG_FORCE_INLINE static const SlangUUID& getTypeGuid() \
313 { \
314 static const SlangUUID guid = {a, b, c, d0, d1, d2, d3, d4, d5, d6, d7}; \
315 return guid; \
316 }
317#endif // SLANG_H
318
319// Includes
320
322#include "slang-cpp-types.h"
323
324// Atomic helpers for the CPU target. Needed so kIROp_AtomicAdd and
325// friends can lower to native atomic operations on the host — matching
326// the semantics of HLSL `InterlockedAdd`, SPIR-V `OpAtomicIAdd`, etc.
327// Uses compiler builtins so no <atomic> header is required.
328//
329// Compiler coverage:
330// - MSVC: `_InterlockedExchangeAdd`
331// - GCC / Clang: `__atomic_fetch_add`
332// - SNC / GHS etc.: falls back to a non-atomic load/add/store.
333// These are console / embedded toolchains not known to ship
334// `__atomic_*` builtins; the fallback is racy under concurrency
335// but keeps the prelude buildable there. Platforms that need
336// real atomics on those compilers can add a bespoke branch.
337#if SLANG_VC
338#include <intrin.h>
339static_assert(
340 sizeof(long) == 4,
341 "_InterlockedExchangeAdd uses `long`; MSVC LLP64 requires sizeof(long)==4");
342static inline uint32_t _slang_atomic_add_u32(uint32_t* ptr, uint32_t val)
343{
344 // Returns the PRIOR value, matching HLSL InterlockedAdd and GLSL atomicAdd.
345 return static_cast<uint32_t>(
346 _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(ptr), static_cast<long>(val)));
347}
348static inline int32_t _slang_atomic_add_i32(int32_t* ptr, int32_t val)
349{
350 return static_cast<int32_t>(
351 _InterlockedExchangeAdd(reinterpret_cast<volatile long*>(ptr), static_cast<long>(val)));
352}
353#elif SLANG_GCC || SLANG_CLANG
354static inline uint32_t _slang_atomic_add_u32(uint32_t* ptr, uint32_t val)
355{
356 return __atomic_fetch_add(ptr, val, __ATOMIC_RELAXED);
357}
358static inline int32_t _slang_atomic_add_i32(int32_t* ptr, int32_t val)
359{
360 return __atomic_fetch_add(ptr, val, __ATOMIC_RELAXED);
361}
362#else
363// Non-atomic fallback for compilers without a known atomic builtin
364// (Sony SNC, Green Hills MULTI, etc.). Racy under concurrent invocation
365// but keeps the prelude compilable; CPU-target coverage on these
366// platforms is single-threaded in practice.
367static inline uint32_t _slang_atomic_add_u32(uint32_t* ptr, uint32_t val)
368{
369 uint32_t old = *ptr;
370 *ptr = old + val;
371 return old;
372}
373static inline int32_t _slang_atomic_add_i32(int32_t* ptr, int32_t val)
374{
375 int32_t old = *ptr;
376 *ptr = old + val;
377 return old;
378}
379#endif
380
381// TODO(JS): Hack! Output C++ code from slang can copy uninitialized variables.
382#if defined(_MSC_VER)
383#pragma warning(disable : 4700)
384#endif
385
386#ifndef SLANG_UNROLL
387#define SLANG_UNROLL
388#endif
389
390#endif
int32_t SlangResult
Definition slang-cpp-prelude.h:300
static int32_t _slang_atomic_add_i32(int32_t *ptr, int32_t val)
Definition slang-cpp-prelude.h:373
static uint32_t _slang_atomic_add_u32(uint32_t *ptr, uint32_t val)
Definition slang-cpp-prelude.h:367
int32_t SlangResult
Definition slang.h:1243
#define SLANG_NO_THROW
Definition slang.h:212
#define SLANG_MCALL
Definition slang.h:226
Definition slang-cpp-prelude.h:303
void ** outObject
Definition slang.h:1406
virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef()=0
virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const &uuid, void **outObject)=0
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release()=0
Definition slang-cpp-prelude.h:293
uint16_t data3
Definition slang-cpp-prelude.h:296
uint32_t data1
Definition slang-cpp-prelude.h:294
uint16_t data2
Definition slang-cpp-prelude.h:295
uint8_t data4[8]
Definition slang-cpp-prelude.h:297