Skip to content

Commit 7258ef4

Browse files
authored
User defined downstream compiler prelude (shader-slang#1028)
* Added setDownstreamCompilerPrelude Renamed setPassThroughPath to setDownstreamCompilerPath. Fixed tests. Added prelude directory & code to TestToolUtil to setup default preludes for testing/command line apis. * Fix merge problem * Remove hacks to make prelude work by adding a search path as no longer needed with 'user prelude'. * Split up prelude into scalar intrinsics, and types. Use slang.h for main header. slang-cpp-prelude.h can now just include what it needs (relative to prelude directory) and define the few remaining things/work arounds. * Fix typo.
1 parent 3e78e46 commit 7258ef4

20 files changed

+572
-1788
lines changed

prelude/slang-cpp-prelude.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef SLANG_CPP_PRELUDE_H
2+
#define SLANG_CPP_PRELUDE_H
3+
4+
#include "../slang.h"
5+
6+
#include <math.h>
7+
#include <assert.h>
8+
#include <stdlib.h>
9+
10+
#if defined(_MSC_VER)
11+
# define SLANG_PRELUDE_SHARED_LIB_EXPORT __declspec(dllexport)
12+
#else
13+
# define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__((__visibility__("default")))
14+
//# define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__ ((dllexport)) __attribute__((__visibility__("default")))
15+
#endif
16+
17+
#ifdef __cplusplus
18+
# define SLANG_PRELUDE_EXTERN_C extern "C"
19+
#else
20+
# define SLANG_PRELUDE_EXTERN_C
21+
#endif
22+
23+
#define SLANG_PRELUDE_EXPORT SLANG_PRELUDE_EXTERN_C SLANG_PRELUDE_SHARED_LIB_EXPORT
24+
25+
#include "slang-cpp-types.h"
26+
#include "slang-cpp-scalar-intrinsics.h"
27+
28+
// TODO(JS): Hack! Output C++ code from slang can copy uninitialized variables.
29+
#if SLANG_VC
30+
# pragma warning(disable : 4700)
31+
#endif
32+
33+
#endif

prelude/slang-cpp-scalar-intrinsics.h

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#ifndef SLANG_PRELUDE_SCALAR_INTRINSICS_H
2+
#define SLANG_PRELUDE_SCALAR_INTRINSICS_H
3+
4+
#include "../slang.h"
5+
6+
#ifdef SLANG_PRELUDE_NAMESPACE
7+
namespace SLANG_PRELUDE_NAMESPACE {
8+
#endif
9+
10+
#ifndef SLANG_PRELUDE_PI
11+
# define SLANG_PRELUDE_PI 3.14159265358979323846
12+
#endif
13+
14+
// ----------------------------- F32 -----------------------------------------
15+
16+
union Union32
17+
{
18+
uint32_t u;
19+
int32_t i;
20+
float f;
21+
};
22+
23+
// Helpers
24+
SLANG_FORCE_INLINE float F32_calcSafeRadians(float radians)
25+
{
26+
float a = radians * (1.0f / float(SLANG_PRELUDE_PI));
27+
a = (a < 0.0f) ? (::ceilf(a) - a) : (a - ::floorf(a));
28+
return (a * float(SLANG_PRELUDE_PI));
29+
}
30+
31+
// Unary
32+
SLANG_FORCE_INLINE float F32_ceil(float f) { return ::ceilf(f); }
33+
SLANG_FORCE_INLINE float F32_floor(float f) { return ::floorf(f); }
34+
SLANG_FORCE_INLINE float F32_sin(float f) { return ::sinf(F32_calcSafeRadians(f)); }
35+
SLANG_FORCE_INLINE float F32_cos(float f) { return ::cosf(F32_calcSafeRadians(f)); }
36+
SLANG_FORCE_INLINE float F32_tan(float f) { return ::tanf(f); }
37+
SLANG_FORCE_INLINE float F32_asin(float f) { return ::asinf(f); }
38+
SLANG_FORCE_INLINE float F32_acos(float f) { return ::acosf(f); }
39+
SLANG_FORCE_INLINE float F32_atan(float f) { return ::atanf(f); }
40+
SLANG_FORCE_INLINE float F32_log2(float f) { return ::log2f(f); }
41+
SLANG_FORCE_INLINE float F32_exp2(float f) { return ::exp2f(f); }
42+
SLANG_FORCE_INLINE float F32_exp(float f) { return ::expf(f); }
43+
SLANG_FORCE_INLINE float F32_abs(float f) { return ::fabsf(f); }
44+
SLANG_FORCE_INLINE float F32_trunc(float f) { return ::truncf(f); }
45+
SLANG_FORCE_INLINE float F32_sqrt(float f) { return ::sqrtf(f); }
46+
SLANG_FORCE_INLINE float F32_rsqrt(float f) { return 1.0f / F32_sqrt(f); }
47+
SLANG_FORCE_INLINE float F32_rcp(float f) { return 1.0f / f; }
48+
SLANG_FORCE_INLINE float F32_sign(float f) { return ( f == 0.0f) ? f : (( f < 0.0f) ? -1.0f : 1.0f); }
49+
SLANG_FORCE_INLINE float F32_saturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; }
50+
SLANG_FORCE_INLINE float F32_frac(float f) { return f - F32_floor(f); }
51+
SLANG_FORCE_INLINE float F32_radians(float f) { return f * 0.01745329222f; }
52+
53+
// Binary
54+
SLANG_FORCE_INLINE float F32_min(float a, float b) { return a < b ? a : b; }
55+
SLANG_FORCE_INLINE float F32_max(float a, float b) { return a > b ? a : b; }
56+
SLANG_FORCE_INLINE float F32_pow(float a, float b) { return ::powf(a, b); }
57+
SLANG_FORCE_INLINE float F32_fmod(float a, float b) { return ::fmodf(a, b); }
58+
SLANG_FORCE_INLINE float F32_step(float a, float b) { return float(a >= b); }
59+
SLANG_FORCE_INLINE float F32_atan2(float a, float b) { return float(atan2(a, b)); }
60+
61+
// Ternary
62+
SLANG_FORCE_INLINE float F32_smoothstep(float min, float max, float x) { return x < min ? min : ((x > max) ? max : x / (max - min)); }
63+
SLANG_FORCE_INLINE float F32_lerp(float x, float y, float s) { return x + s * (y - x); }
64+
SLANG_FORCE_INLINE float F32_clamp(float x, float min, float max) { return ( x < min) ? min : ((x > max) ? max : x); }
65+
SLANG_FORCE_INLINE void F32_sincos(float f, float& outSin, float& outCos) { outSin = F32_sin(f); outCos = F32_cos(f); }
66+
67+
SLANG_FORCE_INLINE uint32_t F32_asuint(float f) { Union32 u; u.f = f; return u.u; }
68+
SLANG_FORCE_INLINE int32_t F32_asint(float f) { Union32 u; u.f = f; return u.i; }
69+
70+
// ----------------------------- F64 -----------------------------------------
71+
72+
SLANG_FORCE_INLINE double F64_calcSafeRadians(double radians)
73+
{
74+
double a = radians * (1.0 / SLANG_PRELUDE_PI);
75+
a = (a < 0.0) ? (::ceil(a) - a) : (a - ::floor(a));
76+
return (a * SLANG_PRELUDE_PI);
77+
}
78+
79+
// Unary
80+
SLANG_FORCE_INLINE double F64_ceil(double f) { return ::ceil(f); }
81+
SLANG_FORCE_INLINE double F64_floor(double f) { return ::floor(f); }
82+
SLANG_FORCE_INLINE double F64_sin(double f) { return ::sin(F64_calcSafeRadians(f)); }
83+
SLANG_FORCE_INLINE double F64_cos(double f) { return ::cos(F64_calcSafeRadians(f)); }
84+
SLANG_FORCE_INLINE double F64_tan(double f) { return ::tan(f); }
85+
SLANG_FORCE_INLINE double F64_asin(double f) { return ::asin(f); }
86+
SLANG_FORCE_INLINE double F64_acos(double f) { return ::acos(f); }
87+
SLANG_FORCE_INLINE double F64_atan(double f) { return ::atan(f); }
88+
SLANG_FORCE_INLINE double F64_log2(double f) { return ::log2(f); }
89+
SLANG_FORCE_INLINE double F64_exp2(double f) { return ::exp2(f); }
90+
SLANG_FORCE_INLINE double F64_exp(double f) { return ::exp(f); }
91+
SLANG_FORCE_INLINE double F64_abs(double f) { return ::fabs(f); }
92+
SLANG_FORCE_INLINE double F64_trunc(double f) { return ::trunc(f); }
93+
SLANG_FORCE_INLINE double F64_sqrt(double f) { return ::sqrt(f); }
94+
SLANG_FORCE_INLINE double F64_rsqrt(double f) { return 1.0 / F64_sqrt(f); }
95+
SLANG_FORCE_INLINE double F64_rcp(double f) { return 1.0 / f; }
96+
SLANG_FORCE_INLINE double F64_sign(double f) { return (f == 0.0) ? f : ((f < 0.0) ? -1.0 : 1.0); }
97+
SLANG_FORCE_INLINE double F64_saturate(double f) { return (f < 0.0) ? 0.0 : (f > 1.0) ? 1.0 : f; }
98+
SLANG_FORCE_INLINE double F64_frac(double f) { return f - F64_floor(f); }
99+
SLANG_FORCE_INLINE double F64_radians(double f) { return f * 0.01745329222; }
100+
101+
// Binary
102+
SLANG_FORCE_INLINE double F64_min(double a, double b) { return a < b ? a : b; }
103+
SLANG_FORCE_INLINE double F64_max(double a, double b) { return a > b ? a : b; }
104+
SLANG_FORCE_INLINE double F64_pow(double a, double b) { return ::pow(a, b); }
105+
SLANG_FORCE_INLINE double F64_fmod(double a, double b) { return ::fmod(a, b); }
106+
SLANG_FORCE_INLINE double F64_step(double a, double b) { return double(a >= b); }
107+
SLANG_FORCE_INLINE double F64_atan2(double a, double b) { return atan2(a, b); }
108+
109+
// Ternary
110+
SLANG_FORCE_INLINE double F64_smoothstep(double min, double max, double x) { return x < min ? min : ((x > max) ? max : x / (max - min)); }
111+
SLANG_FORCE_INLINE double F64_lerp(double x, double y, double s) { return x + s * (y - x); }
112+
SLANG_FORCE_INLINE double F64_clamp(double x, double min, double max) { return (x < min) ? min : ((x > max) ? max : x); }
113+
SLANG_FORCE_INLINE void F64_sincos(double f, double& outSin, double& outCos) { outSin = F64_sin(f); outCos = F64_cos(f); }
114+
115+
// TODO!
116+
//uint32_t F64_asuint(float f);
117+
//int32_t F64_asint(float f);
118+
119+
// ----------------------------- I32 -----------------------------------------
120+
121+
SLANG_FORCE_INLINE int32_t I32_abs(int32_t f) { return (f < 0) ? -f : f; }
122+
123+
SLANG_FORCE_INLINE int32_t I32_min(int32_t a, int32_t b) { return a < b ? a : b; }
124+
SLANG_FORCE_INLINE int32_t I32_max(int32_t a, int32_t b) { return a > b ? a : b; }
125+
126+
SLANG_FORCE_INLINE int32_t I32_clamp(int32_t x, int32_t min, int32_t max) { return ( x < min) ? min : ((x > max) ? max : x); }
127+
128+
SLANG_FORCE_INLINE float I32_asfloat(int32_t x) { Union32 u; u.i = x; return u.f; }
129+
SLANG_FORCE_INLINE uint32_t I32_asuint(int32_t x) { return uint32_t(x); }
130+
131+
// ----------------------------- U32 -----------------------------------------
132+
133+
SLANG_FORCE_INLINE uint32_t U32_abs(uint32_t f) { return f; }
134+
135+
SLANG_FORCE_INLINE uint32_t U32_min(uint32_t a, uint32_t b) { return a < b ? a : b; }
136+
SLANG_FORCE_INLINE uint32_t U32_max(uint32_t a, uint32_t b) { return a > b ? a : b; }
137+
138+
SLANG_FORCE_INLINE uint32_t U32_clamp(uint32_t x, uint32_t min, uint32_t max) { return ( x < min) ? min : ((x > max) ? max : x); }
139+
140+
SLANG_FORCE_INLINE float U32_asfloat(uint32_t x) { Union32 u; u.u = x; return u.f; }
141+
SLANG_FORCE_INLINE uint32_t U32_asint(int32_t x) { return uint32_t(x); }
142+
143+
#ifdef SLANG_PRELUDE_NAMESPACE
144+
}
145+
#endif
146+
147+
#endif

0 commit comments

Comments
 (0)