diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-07-17 10:26:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-17 10:26:37 -0400 |
| commit | 749634a2a6e03acf435c39f78b933a01b90a7440 (patch) | |
| tree | 950203d3fc29610428b0ca03eb756911b9b11f47 /tests/cross-compile | |
| parent | f52f5cd4a7b5b71617b949fc62a78abe8c4822b3 (diff) | |
Slang -> C++ -> SharedLibrary -> Test (#999)
* WIP: Adding support for C/C++ compilation to slang API.
* Removed BackEndType in test harness -> use SlangPassThrough to identify backends
Only require stage for targets that require it.
Detection of all different backends.
* Windows/Unix create temporary filename.
* WIP: Output CPU binaries.
* Added a pass-through c/c++ test.
* Compile C++/C and store in temporary file.
* Read the binary back into memory.
* Set debug info and optimization flags for C/C++.
Make the CPPCompiler debug/optimization levels match slangs.
* Handling of include paths and math precision.
* Dumping c++/c source and exe/shared library.
* Put hex dump into own util.
* End to end pass through c compilation test.
* WIP: Simple execute test working on Linux/Unix.
* Fix typo on linux.
* WIP: To compile slang to cpp shared library. Report backend compiler errors.
* Compiles slang -> cpp and loads as shared library.
* Fix problem on c-cross-compile test because prelude is now included with <> quotes.
* Run slang generated cpp code - using hard coded data.
* Added cpp-execute-simple, and test output.
* Fix warning that broke win32 build.
* Fix compilation problem on osx.
Diffstat (limited to 'tests/cross-compile')
| -rw-r--r-- | tests/cross-compile/cpp-execute-simple.slang | 12 | ||||
| -rw-r--r-- | tests/cross-compile/cpp-execute-simple.slang.expected | 1 | ||||
| -rw-r--r-- | tests/cross-compile/cpp-execute.slang | 106 | ||||
| -rw-r--r-- | tests/cross-compile/cpp-execute.slang.expected | 1 | ||||
| -rw-r--r-- | tests/cross-compile/slang-cpp-prelude.h | 204 |
5 files changed, 271 insertions, 53 deletions
diff --git a/tests/cross-compile/cpp-execute-simple.slang b/tests/cross-compile/cpp-execute-simple.slang new file mode 100644 index 000000000..74a3ec634 --- /dev/null +++ b/tests/cross-compile/cpp-execute-simple.slang @@ -0,0 +1,12 @@ +//TEST:CPU_EXECUTE: -profile cs_5_0 -entry computeMain -target sharedlib + +[numthreads(4, 1, 1)] +void computeMain( + uint3 dispatchThreadID : SV_DispatchThreadID, +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + RWStructuredBuffer<int> outputBuffer) +{ + uint tid = dispatchThreadID.x; + + outputBuffer[tid] = int(tid * tid); +}
\ No newline at end of file diff --git a/tests/cross-compile/cpp-execute-simple.slang.expected b/tests/cross-compile/cpp-execute-simple.slang.expected new file mode 100644 index 000000000..b84777e95 --- /dev/null +++ b/tests/cross-compile/cpp-execute-simple.slang.expected @@ -0,0 +1 @@ +0, 1, 4, 9 diff --git a/tests/cross-compile/cpp-execute.slang b/tests/cross-compile/cpp-execute.slang new file mode 100644 index 000000000..1c90c8dd2 --- /dev/null +++ b/tests/cross-compile/cpp-execute.slang @@ -0,0 +1,106 @@ +//TEST:CPU_EXECUTE: -profile cs_5_0 -entry computeMain -target sharedlib + +enum Color +{ + Red, + Green = 2, + Blue, +} + +int test(int val) +{ + Color c = Color.Red; + + if(val > 1) + { + c = Color.Green; + } + + if(c == Color.Red) + { + if(val & 1) + { + c = Color.Blue; + } + } + + switch(c) + { + case Color.Red: + val = 1; + break; + + case Color.Green: + val = 2; + break; + + case Color.Blue: + val = 3; + break; + + default: + val = -1; + break; + } + + return (val << 4) + int(c); +} + +float sum(float a[3]) +{ + float total = a[0]; + for (int i = 1; i < 3; ++i) + { + total += a[i]; + } + return total; +} + +struct Thing +{ + int a; + float b; +}; + +[numthreads(4, 1, 1)] +void computeMain( + uint3 dispatchThreadID : SV_DispatchThreadID, +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + RWStructuredBuffer<int> outputBuffer) +{ + uint tid = dispatchThreadID.x; + + Thing thing = { 10, -1.0 }; + + float array[3] = { thing.a, 2, 3}; + + float anotherArray[] = { 1, 2, 5 }; + + array[0] += anotherArray[1]; + + matrix<float, 2, 3> mat = { { sum(array), 1, 2 }, { 3, 4, 5} }; + vector<float, 2> vec = { float(tid + 1), float(tid + 2) }; + + vector<float, 3> vec2 = max(sin(mul(vec, mat)), float3(1, 2, -1)); + vector<float, 3> vec3 = mul(vec, mat); + + float3 vec4 = lerp(vec2, vec3, float3(tid * (1.0f / 4), 1, 1)); + + float3 crossVec = normalize(cross(vec4, vec4)); + + vec2.x = fmod(crossVec.y, crossVec.x); + + vec2 = fmod(vec2, crossVec); + + vec2 += (-vec2.zyx) * 2 + crossVec * length(crossVec) + reflect(vec4, normalize(crossVec)); + + vector<bool, 3> z = vec2 > 0; + + int val = (int(tid) + (any(z) ? 1 : 0) + (all(z) ? 2 : 0)) % 100; + + val = asint(asfloat(asuint(asfloat(val)))); + + val = test(val); + + outputBuffer[tid] = val + int(dot(vec2, vec4)); +}
\ No newline at end of file diff --git a/tests/cross-compile/cpp-execute.slang.expected b/tests/cross-compile/cpp-execute.slang.expected new file mode 100644 index 000000000..65e3ed534 --- /dev/null +++ b/tests/cross-compile/cpp-execute.slang.expected @@ -0,0 +1 @@ +-2147483632, -2147483597, -2147483614, -2147483614 diff --git a/tests/cross-compile/slang-cpp-prelude.h b/tests/cross-compile/slang-cpp-prelude.h index f60fc8518..f2635eb2c 100644 --- a/tests/cross-compile/slang-cpp-prelude.h +++ b/tests/cross-compile/slang-cpp-prelude.h @@ -1,3 +1,5 @@ +#ifndef SLANG_CPP_PRELUDE_H +#define SLANG_CPP_PRELUDE_H #include <inttypes.h> #include <math.h> @@ -6,24 +8,32 @@ #include <assert.h> #include <stdlib.h> -#ifndef M_PI -# define M_PI 3.14159265358979323846 +#ifndef SLANG_FORCE_INLINE +# define SLANG_FORCE_INLINE inline +#endif + +#ifndef SLANG_PRELUDE_PI +# define SLANG_PRELUDE_PI 3.14159265358979323846 #endif #if defined(_MSC_VER) -# define SLANG_SHARED_LIB_EXPORT __declspec(dllexport) +# define SLANG_PRELUDE_SHARED_LIB_EXPORT __declspec(dllexport) #else -# define SLANG_SHARED_LIB_EXPORT __attribute__((__visibility__("default"))) -//# define SLANG_SHARED_LIB_EXPORT __attribute__ ((dllexport)) __attribute__((__visibility__("default"))) +# define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__((__visibility__("default"))) +//# define SLANG_PRELUDE_SHARED_LIB_EXPORT __attribute__ ((dllexport)) __attribute__((__visibility__("default"))) #endif #ifdef __cplusplus -# define SLANG_EXTERN_C extern "C" +# define SLANG_PRELUDE_EXTERN_C extern "C" #else -# define SLANG_EXTERN_C +# define SLANG_PRELUDE_EXTERN_C #endif -#define SLANG_EXPORT SLANG_EXTERN_C SLANG_SHARED_LIB_EXPORT +#define SLANG_PRELUDE_EXPORT SLANG_PRELUDE_EXTERN_C SLANG_PRELUDE_SHARED_LIB_EXPORT + +#ifdef SLANG_PRELUDE_NAMESPACE +namespace SLANG_PRELUDE_NAMESPACE { +#endif template <typename T, size_t SIZE> struct FixedArray @@ -43,6 +53,39 @@ struct RWStructuredBuffer size_t count; }; +template <typename T, int COUNT> +struct Vector; + +template <typename T> +struct Vector<T, 1> +{ + T x; +}; + +template <typename T> +struct Vector<T, 2> +{ + T x, y; +}; + +template <typename T> +struct Vector<T, 3> +{ + T x, y, z; +}; + +template <typename T> +struct Vector<T, 4> +{ + T x, y, z, w; +}; + +template <typename T, int ROWS, int COLS> +struct Matrix +{ + Vector<T, COLS> rows[ROWS]; +}; + // ----------------------------- F32 ----------------------------------------- union Union32 @@ -53,70 +96,125 @@ union Union32 }; // Helpers -float F32_calcSafeRadians(float radians) +SLANG_FORCE_INLINE float F32_calcSafeRadians(float radians) { - float a = radians * (1.0f / M_PI); + float a = radians * (1.0f / float(SLANG_PRELUDE_PI)); a = (a < 0.0f) ? (::ceilf(a) - a) : (a - ::floorf(a)); - return (a * M_PI); + return (a * float(SLANG_PRELUDE_PI)); } // Unary -float F32_ceil(float f) { return ::ceilf(f); } -float F32_floor(float f) { return ::floorf(f); } -float F32_sin(float f) { return ::sinf(F32_calcSafeRadians(f)); } -float F32_cos(float f) { return ::cosf(F32_calcSafeRadians(f)); } -float F32_tan(float f) { return ::tanf(f); } -float F32_asin(float f) { return ::asinf(f); } -float F32_acos(float f) { return ::acosf(f); } -float F32_atan(float f) { return ::atanf(f); } -float F32_log2(float f) { return ::log2f(f); } -float F32_exp2(float f) { return ::exp2f(f); } -float F32_exp(float f) { return ::expf(f); } -float F32_abs(float f) { return ::fabsf(f); } -float F32_trunc(float f) { return ::truncf(f); } -float F32_sqrt(float f) { return ::sqrtf(f); } -float F32_rsqrt(float f) { return 1.0f / F32_sqrt(f); } -float F32_rcp(float f) { return 1.0f / f; } -float F32_sign(float f) { return ( f == 0.0f) ? f : (( f < 0.0f) ? -1.0f : 1.0f); } -float F32_saturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } -float F32_frac(float f) { return f - F32_floor(f); } -float F32_radians(float f) { return f * 0.01745329222f; } +SLANG_FORCE_INLINE float F32_ceil(float f) { return ::ceilf(f); } +SLANG_FORCE_INLINE float F32_floor(float f) { return ::floorf(f); } +SLANG_FORCE_INLINE float F32_sin(float f) { return ::sinf(F32_calcSafeRadians(f)); } +SLANG_FORCE_INLINE float F32_cos(float f) { return ::cosf(F32_calcSafeRadians(f)); } +SLANG_FORCE_INLINE float F32_tan(float f) { return ::tanf(f); } +SLANG_FORCE_INLINE float F32_asin(float f) { return ::asinf(f); } +SLANG_FORCE_INLINE float F32_acos(float f) { return ::acosf(f); } +SLANG_FORCE_INLINE float F32_atan(float f) { return ::atanf(f); } +SLANG_FORCE_INLINE float F32_log2(float f) { return ::log2f(f); } +SLANG_FORCE_INLINE float F32_exp2(float f) { return ::exp2f(f); } +SLANG_FORCE_INLINE float F32_exp(float f) { return ::expf(f); } +SLANG_FORCE_INLINE float F32_abs(float f) { return ::fabsf(f); } +SLANG_FORCE_INLINE float F32_trunc(float f) { return ::truncf(f); } +SLANG_FORCE_INLINE float F32_sqrt(float f) { return ::sqrtf(f); } +SLANG_FORCE_INLINE float F32_rsqrt(float f) { return 1.0f / F32_sqrt(f); } +SLANG_FORCE_INLINE float F32_rcp(float f) { return 1.0f / f; } +SLANG_FORCE_INLINE float F32_sign(float f) { return ( f == 0.0f) ? f : (( f < 0.0f) ? -1.0f : 1.0f); } +SLANG_FORCE_INLINE float F32_saturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } +SLANG_FORCE_INLINE float F32_frac(float f) { return f - F32_floor(f); } +SLANG_FORCE_INLINE float F32_radians(float f) { return f * 0.01745329222f; } // Binary -float F32_min(float a, float b) { return a < b ? a : b; } -float F32_max(float a, float b) { return a > b ? a : b; } -float F32_pow(float a, float b) { return ::powf(a, b); } -float F32_fmod(float a, float b) { return ::fmodf(a, b); } -float F32_step(float a, float b) { return float(a >= b); } -float F32_atan2(float a, float b) { return float(atan2(a, b)); } +SLANG_FORCE_INLINE float F32_min(float a, float b) { return a < b ? a : b; } +SLANG_FORCE_INLINE float F32_max(float a, float b) { return a > b ? a : b; } +SLANG_FORCE_INLINE float F32_pow(float a, float b) { return ::powf(a, b); } +SLANG_FORCE_INLINE float F32_fmod(float a, float b) { return ::fmodf(a, b); } +SLANG_FORCE_INLINE float F32_step(float a, float b) { return float(a >= b); } +SLANG_FORCE_INLINE float F32_atan2(float a, float b) { return float(atan2(a, b)); } // Ternary -float F32_smoothstep(float min, float max, float x) { return x < min ? min : ((x > max) ? max : x / (max - min)); } -float F32_lerp(float x, float y, float s) { return x + s * (y - x); } -float F32_clamp(float x, float min, float max) { return ( x < min) ? min : ((x > max) ? max : x); } -void F32_sincos(float f, float& outSin, float& outCos) { outSin = F32_sin(f); outCos = F32_cos(f); } +SLANG_FORCE_INLINE float F32_smoothstep(float min, float max, float x) { return x < min ? min : ((x > max) ? max : x / (max - min)); } +SLANG_FORCE_INLINE float F32_lerp(float x, float y, float s) { return x + s * (y - x); } +SLANG_FORCE_INLINE float F32_clamp(float x, float min, float max) { return ( x < min) ? min : ((x > max) ? max : x); } +SLANG_FORCE_INLINE void F32_sincos(float f, float& outSin, float& outCos) { outSin = F32_sin(f); outCos = F32_cos(f); } + +SLANG_FORCE_INLINE uint32_t F32_asuint(float f) { Union32 u; u.f = f; return u.u; } +SLANG_FORCE_INLINE int32_t F32_asint(float f) { Union32 u; u.f = f; return u.i; } -uint32_t F32_asuint(float f) { Union32 u; u.f = f; return u.u; } -int32_t F32_asint(float f) { Union32 u; u.f = f; return u.i; } +// ----------------------------- F64 ----------------------------------------- + +SLANG_FORCE_INLINE double F64_calcSafeRadians(double radians) +{ + double a = radians * (1.0 / SLANG_PRELUDE_PI); + a = (a < 0.0) ? (::ceil(a) - a) : (a - ::floor(a)); + return (a * SLANG_PRELUDE_PI); +} + +// Unary +SLANG_FORCE_INLINE double F64_ceil(double f) { return ::ceil(f); } +SLANG_FORCE_INLINE double F64_floor(double f) { return ::floor(f); } +SLANG_FORCE_INLINE double F64_sin(double f) { return ::sin(F64_calcSafeRadians(f)); } +SLANG_FORCE_INLINE double F64_cos(double f) { return ::cos(F64_calcSafeRadians(f)); } +SLANG_FORCE_INLINE double F64_tan(double f) { return ::tan(f); } +SLANG_FORCE_INLINE double F64_asin(double f) { return ::asin(f); } +SLANG_FORCE_INLINE double F64_acos(double f) { return ::acos(f); } +SLANG_FORCE_INLINE double F64_atan(double f) { return ::atan(f); } +SLANG_FORCE_INLINE double F64_log2(double f) { return ::log2(f); } +SLANG_FORCE_INLINE double F64_exp2(double f) { return ::exp2(f); } +SLANG_FORCE_INLINE double F64_exp(double f) { return ::exp(f); } +SLANG_FORCE_INLINE double F64_abs(double f) { return ::fabs(f); } +SLANG_FORCE_INLINE double F64_trunc(double f) { return ::trunc(f); } +SLANG_FORCE_INLINE double F64_sqrt(double f) { return ::sqrt(f); } +SLANG_FORCE_INLINE double F64_rsqrt(double f) { return 1.0 / F64_sqrt(f); } +SLANG_FORCE_INLINE double F64_rcp(double f) { return 1.0 / f; } +SLANG_FORCE_INLINE double F64_sign(double f) { return (f == 0.0) ? f : ((f < 0.0) ? -1.0 : 1.0); } +SLANG_FORCE_INLINE double F64_saturate(double f) { return (f < 0.0) ? 0.0 : (f > 1.0) ? 1.0 : f; } +SLANG_FORCE_INLINE double F64_frac(double f) { return f - F64_floor(f); } +SLANG_FORCE_INLINE double F64_radians(double f) { return f * 0.01745329222; } + +// Binary +SLANG_FORCE_INLINE double F64_min(double a, double b) { return a < b ? a : b; } +SLANG_FORCE_INLINE double F64_max(double a, double b) { return a > b ? a : b; } +SLANG_FORCE_INLINE double F64_pow(double a, double b) { return ::pow(a, b); } +SLANG_FORCE_INLINE double F64_fmod(double a, double b) { return ::fmod(a, b); } +SLANG_FORCE_INLINE double F64_step(double a, double b) { return double(a >= b); } +SLANG_FORCE_INLINE double F64_atan2(double a, double b) { return atan2(a, b); } + +// Ternary +SLANG_FORCE_INLINE double F64_smoothstep(double min, double max, double x) { return x < min ? min : ((x > max) ? max : x / (max - min)); } +SLANG_FORCE_INLINE double F64_lerp(double x, double y, double s) { return x + s * (y - x); } +SLANG_FORCE_INLINE double F64_clamp(double x, double min, double max) { return (x < min) ? min : ((x > max) ? max : x); } +SLANG_FORCE_INLINE void F64_sincos(double f, double& outSin, double& outCos) { outSin = F64_sin(f); outCos = F64_cos(f); } + +// TODO! +//uint32_t F64_asuint(float f); +//int32_t F64_asint(float f); // ----------------------------- I32 ----------------------------------------- -int32_t I32_abs(int32_t f) { return (f < 0) ? -f : f; } +SLANG_FORCE_INLINE int32_t I32_abs(int32_t f) { return (f < 0) ? -f : f; } -int32_t I32_min(int32_t a, int32_t b) { return a < b ? a : b; } -int32_t I32_max(int32_t a, int32_t b) { return a > b ? a : b; } +SLANG_FORCE_INLINE int32_t I32_min(int32_t a, int32_t b) { return a < b ? a : b; } +SLANG_FORCE_INLINE int32_t I32_max(int32_t a, int32_t b) { return a > b ? a : b; } -int32_t I32_clamp(int32_t x, int32_t min, int32_t max) { return ( x < min) ? min : ((x > max) ? max : x); } +SLANG_FORCE_INLINE int32_t I32_clamp(int32_t x, int32_t min, int32_t max) { return ( x < min) ? min : ((x > max) ? max : x); } -float I32_asfloat(int32_t x) { Union32 u; u.i = x; return u.f; } +SLANG_FORCE_INLINE float I32_asfloat(int32_t x) { Union32 u; u.i = x; return u.f; } // ----------------------------- U32 ----------------------------------------- -uint32_t U32_abs(uint32_t f) { return f; } +SLANG_FORCE_INLINE uint32_t U32_abs(uint32_t f) { return f; } -uint32_t U32_min(uint32_t a, uint32_t b) { return a < b ? a : b; } -uint32_t U32_max(uint32_t a, uint32_t b) { return a > b ? a : b; } +SLANG_FORCE_INLINE uint32_t U32_min(uint32_t a, uint32_t b) { return a < b ? a : b; } +SLANG_FORCE_INLINE uint32_t U32_max(uint32_t a, uint32_t b) { return a > b ? a : b; } -uint32_t U32_clamp(uint32_t x, uint32_t min, uint32_t max) { return ( x < min) ? min : ((x > max) ? max : x); } +SLANG_FORCE_INLINE uint32_t U32_clamp(uint32_t x, uint32_t min, uint32_t max) { return ( x < min) ? min : ((x > max) ? max : x); } -float U32_asfloat(uint32_t x) { Union32 u; u.u = x; return u.f; } +SLANG_FORCE_INLINE float U32_asfloat(uint32_t x) { Union32 u; u.u = x; return u.f; } + +#ifdef SLANG_PRELUDE_NAMESPACE +} +#endif + +#endif |
