From a16f712bb99e426519c9a556b17b54bcc4d1d22d Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Wed, 7 Feb 2024 12:12:15 -0800 Subject: Implement basic GLSL built-in functions (#3525) * Implement basic GLSL built-in functions Partially resolves #3362 This change implemented GLSL build-in functions described in the following sections of "OpenGL Spec" document. 8.1. Angle and Trigonometry Functions 8.2. Exponential Functions 8.3. Common Functions 8.5. Geometric Functions 8.7. Vector Relational Functions 8.8. Integer Functions About 40 functions are newly implemented and about 150 functions were preexisted on HLSL side implementation. The implementation of new functions hasn't been tested yet. * Unify some of GLSL functions into hlsl.meta.slang Partially resoves #3362 This change moves Some of GLSL functions from glsl.meta.slang to hlsl.meta.slang, because those functions are generic enough to be used for HLSL. Those functions are: dot, normalize, fma, and reflect. There was "fma" for double in hlsl.meta.slang and it is converted to use __BuiltinFloatingPointType type, which required some modifications in diff.meta.slang. The implementation for "fma" in diff.meta.slang is very similar to how "mad" is implemented. * Implement more GLSL built-in functions Partially resolves #3362 This change implements more GLSL built-in functions mentioned in the following sections. 8.4. Floating-Point Pack and Unpack Functions 8.6. Matrix Functions This change implemented 11 new GLSL built-in functions and there were 3 already working functions. The mistake in "normalize" is fixed. "refract" function is moved from glsl.meta.slang to hlsl.meta.slang. * Implement basic GLSL built-in functions Partially resolves #3362 This change implemented GLSL build-in functions described in the following sections of "OpenGL Spec" document. 8.1. Angle and Trigonometry Functions 8.2. Exponential Functions 8.3. Common Functions 8.5. Geometric Functions 8.7. Vector Relational Functions 8.8. Integer Functions About 40 functions are newly implemented and about 150 functions were preexisted on HLSL side implementation. The implementation of new functions hasn't been tested yet. * Unify some of GLSL functions into hlsl.meta.slang Partially resoves #3362 This change moves Some of GLSL functions from glsl.meta.slang to hlsl.meta.slang, because those functions are generic enough to be used for HLSL. Those functions are: dot, normalize, fma, and reflect. There was "fma" for double in hlsl.meta.slang and it is converted to use __BuiltinFloatingPointType type, which required some modifications in diff.meta.slang. The implementation for "fma" in diff.meta.slang is very similar to how "mad" is implemented. * Implement more GLSL built-in functions Partially resolves #3362 This change implements more GLSL built-in functions mentioned in the following sections. 8.4. Floating-Point Pack and Unpack Functions 8.6. Matrix Functions This change implemented 11 new GLSL built-in functions and there were 3 already working functions. The mistake in "normalize" is fixed. "refract" function is moved from glsl.meta.slang to hlsl.meta.slang. * Fix a few minor bugs on GLSL builtin functions Partially resovles #3362 Following bugs were addressed: 1. "bitCounts" had to have a "Capability" on its function declaration. 2. "roundEven" is implemented. It is almost same to "round()" but the behaivor is slightly different the given value is 1.5, 3.5, 5.5 and so on. 3. umulExtended and imulExtended are simplified. 4. exp2 is implemented with "__target_switch" for GLSL and SPIR-V. 5. "tests/glsl-intrinsic/intrinsic-basic.slang" checks the results from the GLSL functions. Currently it is mainly to test if the functions exist or not, but it can now also test for a simple case where the input value is zero and the result is most of the time zero or one. * Disable GLSL exp2 double type tests This change disables some of GLSL exp2 related tests as a workaround. The spir-v needs to handle the double-type argument for exp2 properly. * Fix exp2(double) problem for SPIR-V SPIR-V can handle a double-type input for exp2 with this change. However, the slang-test is will failing to test it with an error message saying, "abort compilation:". With a simpler test case, I verified that SPIR-V assembly code is properly generated for exp2(double) and I am not sure why slang-test is still failing. We will need to revisit this issue later. The simple testing is done with a following line: outputBuffer.result[0] = float(exp2(double(outputBuffer.result[0]))); And it generated following lines and it looks correct: ; Function main %main = OpFunction %void None %3 %5 = OpLabel %16 = OpAccessChain %_ptr_Uniform_float %outputBuffer_0 %int_0 %uint_0 %17 = OpLoad %float %16 %19 = OpFConvert %double %17 %20 = OpFConvert %float %19 %21 = OpExtInst %float %1 Exp2 %20 %22 = OpAccessChain %_ptr_Uniform_float %outputBuffer_0 %int_0 %uint_0 OpStore %22 %21 OpReturn OpFunctionEnd * Add __floatCast that is safer than slang_noop_cast Adding __floatCast that can be used for exp2 function. --- source/slang/diff.meta.slang | 62 +-- source/slang/glsl.meta.slang | 891 ++++++++++++++++++++++++++++++++++++++++++- source/slang/hlsl.meta.slang | 100 ++++- 3 files changed, 970 insertions(+), 83 deletions(-) (limited to 'source') diff --git a/source/slang/diff.meta.slang b/source/slang/diff.meta.slang index 6f4888a5d..8a46f7d60 100644 --- a/source/slang/diff.meta.slang +++ b/source/slang/diff.meta.slang @@ -1563,71 +1563,27 @@ void __d_clamp(inout DifferentialPair dpx, inout DifferentialPair dpMin, i VECTOR_MATRIX_TERNARY_DIFF_IMPL(clamp) // fma +__generic [BackwardDifferentiable] [ForwardDerivativeOf(fma)] [PreferRecompute] -DifferentialPair __d_fma(DifferentialPair dpx, DifferentialPair dpy, DifferentialPair dpz) +DifferentialPair __d_fma(DifferentialPair dpx, DifferentialPair dpy, DifferentialPair dpz) { - return DifferentialPair( + return DifferentialPair( fma(dpx.p, dpy.p, dpz.p), - dpy.p * dpx.d + dpx.p * dpy.d + dpz.d); + T.dadd(T.dadd(__mul_p_d(dpy.p, dpx.d), __mul_p_d(dpx.p, dpy.d)), dpz.d)); } +__generic [BackwardDifferentiable] [BackwardDerivativeOf(fma)] [PreferRecompute] -void __d_fma(inout DifferentialPair dpx, inout DifferentialPair dpy, inout DifferentialPair dpz, double dOut) +void __d_fma(inout DifferentialPair dpx, inout DifferentialPair dpy, inout DifferentialPair dpz, T.Differential dOut) { - dpx = diffPair(dpx.p, dpy.p * dOut); - dpy = diffPair(dpy.p, dpx.p * dOut); + dpx = diffPair(dpx.p, __mul_p_d(dpy.p, dOut)); + dpy = diffPair(dpy.p, __mul_p_d(dpx.p, dOut)); dpz = diffPair(dpz.p, dOut); } -__generic -[BackwardDifferentiable] -[ForwardDerivativeOf(fma)] -[PreferRecompute] -DifferentialPair> __d_fma_vector( - DifferentialPair> dpx, - DifferentialPair> dpy, - DifferentialPair> dpz) -{ - vector result; - vector.Differential d_result; - [ForceUnroll] for (int i = 0; i < N; ++i) - { - DifferentialPair dp_elem = __d_fma( - DifferentialPair(dpx.p[i], dpx.d[i]), - DifferentialPair(dpy.p[i], dpy.d[i]), - DifferentialPair(dpz.p[i], dpz.d[i])); - result[i] = dp_elem.p; - d_result[i] = dp_elem.d; - } - return DifferentialPair>(result, d_result); -} -__generic -[BackwardDifferentiable] -[BackwardDerivativeOf(fma)] -[PreferRecompute] -void __d_fma_vector( - inout DifferentialPair> dpx, - inout DifferentialPair> dpy, - inout DifferentialPair> dpz, - vector dOut) -{ - vector.Differential x_d_result, y_d_result, z_d_result; - [ForceUnroll] for (int i = 0; i < N; ++i) - { - DifferentialPair x_dp = diffPair(dpx.p[i], 0.0); - DifferentialPair y_dp = diffPair(dpy.p[i], 0.0); - DifferentialPair z_dp = diffPair(dpz.p[i], 0.0); - __d_fma(x_dp, y_dp, z_dp, dOut[i]); - x_d_result[i] = x_dp.d; - y_d_result[i] = y_dp.d; - z_d_result[i] = z_dp.d; - } - dpx = diffPair(dpx.p, x_d_result); - dpy = diffPair(dpy.p, y_d_result); - dpz = diffPair(dpz.p, z_d_result); -} +VECTOR_MATRIX_TERNARY_DIFF_IMPL(fma) // mad __generic diff --git a/source/slang/glsl.meta.slang b/source/slang/glsl.meta.slang index 4fe56acf8..8403d1391 100644 --- a/source/slang/glsl.meta.slang +++ b/source/slang/glsl.meta.slang @@ -1,5 +1,22 @@ +// TODO: These keywords are not recognized but they should be. +#define highp +#define mediump +#define lowp + +#define VECTOR_MAP_UNARY(TYPE, COUNT, FUNC, VALUE) \ + vector result; for(int i = 0; i < COUNT; ++i) { result[i] = FUNC(VALUE[i]); } return result + +#define VECTOR_MAP_TRINARY(TYPE, COUNT, FUNC, A, B, C) \ + vector result; for(int i = 0; i < COUNT; ++i) { result[i] = FUNC(A[i], B[i], C[i]); } return result + +#define REQUIRE_KHRONOS [require(glsl)] [require(spirv)] + +// +// OpenGL 4.60 spec +// + // -// From the GLSL spec, section 4.1. 'asic Types' +// Section 4.1. 'asic Types' // public typealias vec2 = vector; @@ -109,21 +126,21 @@ public in int gl_ViewportIndex : SV_ViewportArrayIndex; [OverloadRank(15)] [ForceInline] -public matrix operator*(matrix m1, matrix m2) +public matrix operator*(matrix m1, matrix m2) { return mul(m2, m1); } [OverloadRank(15)] [ForceInline] -public matrix operator*(matrix m1, matrix m2) +public matrix operator*(matrix m1, matrix m2) { return mul(m2, m1); } [OverloadRank(15)] [ForceInline] -public matrix operator*(matrix m1, matrix m2) +public matrix operator*(matrix m1, matrix m2) { return mul(m2, m1); } @@ -150,7 +167,7 @@ public vector operator* matrixCompMult(matrix left, matrix right); +public matrix matrixCompMult(matrix left, matrix right); __intrinsic_op(cmpLE) public vector lessThanEqual(vector x, vector y); @@ -180,42 +197,42 @@ public extension vector [ForceInline] [OverloadRank(15)] -public bool operator==(vector left, vector right) +public bool operator==(vector left, vector right) { return all(equal(left, right)); } [ForceInline] [OverloadRank(15)] -public bool operator!=(vector left, vector right) +public bool operator!=(vector left, vector right) { return any(notEqual(left, right)); } [ForceInline] [OverloadRank(14)] -public bool operator==(vector left, vector right) +public bool operator==(vector left, vector right) { return all(equal(left, right)); } [ForceInline] [OverloadRank(14)] -public bool operator!=(vector left, vector right) +public bool operator!=(vector left, vector right) { return any(notEqual(left, right)); } [ForceInline] [OverloadRank(14)] -public bool operator==(vector left, vector right) +public bool operator==(vector left, vector right) { return all(equal(left, right)); } [ForceInline] [OverloadRank(14)] -public bool operator!=(vector left, vector right) +public bool operator!=(vector left, vector right) { return any(notEqual(left, right)); } @@ -227,14 +244,14 @@ for (auto type : kBaseTypes) { }}}} [ForceInline] [OverloadRank(15)] -public bool operator==(vector<$(typeName), N> left, vector<$(typeName), N> right) +public bool operator==(vector<$(typeName), N> left, vector<$(typeName), N> right) { return all(equal(left, right)); } [ForceInline] [OverloadRank(15)] -public bool operator!=(vector<$(typeName), N> left, vector<$(typeName), N> right) +public bool operator!=(vector<$(typeName), N> left, vector<$(typeName), N> right) { return any(notEqual(left, right)); } @@ -242,17 +259,801 @@ ${{{{ } }}}} -[ForceInline] public int findLSB(int v) { return firstbitlow(v); } -[ForceInline] public uint findLSB(uint v) { return firstbitlow(v); } -[ForceInline] public vector findLSB(vector value) +// +// Section 8.1. Angle and Trigonometry Functions +// + +__generic +[__readNone] +[ForceInline] +public T atan(T y, T x) +{ + return atan2(y, x); +} + +__generic +[__readNone] +[ForceInline] +public vector atan(vector y, vector x) +{ + return atan2(y, x); +} + +__generic +__target_intrinsic(cuda, "$P_asinh($0)") +__target_intrinsic(cpp, "$P_asinh($0)") +[__readNone] +[ForceInline] +public T asinh(T x) +{ + return log(x + sqrt(x * x + T(1))); +} + +__generic +[__readNone] +[ForceInline] +public vector asinh(vector x) +{ + VECTOR_MAP_UNARY(T, N, asinh, x); +} + +__generic +__target_intrinsic(cuda, "$P_acosh($0)") +__target_intrinsic(cpp, "$P_acosh($0)") +[__readNone] +[ForceInline] +public T acosh(T x) +{ + return log(x + sqrt( x * x - T(1))); +} + +__generic +[__readNone] +[ForceInline] +public vector acosh(vector x) +{ + VECTOR_MAP_UNARY(T, N, acosh, x); +} + +__generic +__target_intrinsic(cuda, "$P_atanh($0)") +__target_intrinsic(cpp, "$P_atanh($0)") +[__readNone] +[ForceInline] +public T atanh(T x) +{ + return T(0.5) * log((T(1) + x) / (T(1) - x)); +} + +__generic +[__readNone] +[ForceInline] +public vector atanh(vector x) +{ + VECTOR_MAP_UNARY(T, N, atanh, x); +} + +// +// Section 8.2. Exponential Functions +// + +__generic +[__readNone] +[ForceInline] +public T inversesqrt(T x) +{ + return rsqrt(x); +} + +__generic +[__readNone] +[ForceInline] +public vector inversesqrt(vector x) +{ + return rsqrt(x); +} + +// +// Section 8.3. Common Functions +// + +__generic +[__readNone] +[ForceInline] +public T roundEven(T x) +{ + T i; + if (T(0.5) <= fmod(x, i)) + { + bool evenInteger = (fmod(i, T(2)) == T(0)); + if (!evenInteger) + { + x += T(0.1); + } + } + return round(x); +} + +__generic +[__readNone] +[ForceInline] +public vector roundEven(vector x) +{ + VECTOR_MAP_UNARY(T, N, roundEven, x); +} + +__generic +[__readNone] +[ForceInline] +public T fract(T x) +{ + return frac(x); +} + +__generic +[__readNone] +[ForceInline] +public vector fract(vector x) +{ + return frac(x); +} + +__generic +[__readNone] +[ForceInline] +public T mod(T x, T y) +{ + return fmod(x, y); +} + +__generic +[__readNone] +[ForceInline] +public vector mod(vector x, T y) +{ + return fmod(x, vector(y)); +} + +__generic +[__readNone] +[ForceInline] +public vector mod(vector x, vector y) +{ + return fmod(x, y); +} + +__generic +[__readNone] +[ForceInline] +public T mix(T x, T y, T a) +{ + return lerp(x, y, a); +} + +__generic +[__readNone] +[ForceInline] +public vector mix(vector x, vector y, T a) +{ + return lerp(x, y, vector(a)); +} + +__generic +[__readNone] +[ForceInline] +public vector mix(vector x, vector y, vector a) +{ + return lerp(x, y, a); +} + +__generic +[__readNone] +[ForceInline] +public T mix(T x, T y, bool a) +{ + return (a ? y : x); +} + +__generic +[__readNone] +[ForceInline] +public vector mix(vector x, vector y, vector a) +{ + vector result; + for (int i = 0; i < N; i++) + { + result[i] = (a[i] ? y[i] : x[i]); + } + return result; +} + +[__readNone] +[ForceInline] +public int floatBitsToInt(highp float x) +{ + return asint(x); +} + +__generic +[__readNone] +[ForceInline] +public vector floatBitsToInt(highp vector x) +{ + return asint(x); +} + +[__readNone] +[ForceInline] +public uint floatBitsToUint(highp float x) +{ + return asuint(x); +} + +__generic +[__readNone] +[ForceInline] +public vector floatBitsToUint(highp vector x) +{ + return asuint(x); +} + +[__readNone] +[ForceInline] +public float intBitsToFloat(highp int x) +{ + return asfloat(x); +} + +__generic +[__readNone] +[ForceInline] +public vector intBitsToFloat(highp vector x) +{ + return asfloat(x); +} + +[__readNone] +[ForceInline] +public float uintBitsToFloat(highp uint x) +{ + return asfloat(x); +} + +__generic +[__readNone] +[ForceInline] +public vector uintBitsToFloat(highp vector x) +{ + return asfloat(x); +} + +// +// Section 8.4. Floating-Point Pack and Unpack Functions +// + +[__readNone] +[ForceInline] +uint packUnorm1x16(float c) +{ + return uint(clamp(c, 0.0, 1.0) * 65535.0 + 0.5); +} + +[__readNone] +[ForceInline] +uint packSnorm1x16(float v) +{ + return uint(clamp(v ,-1.0, 1.0) * 32767.0 + 32767.5); +} + +[__readNone] +[ForceInline] +uint packUnorm1x8(float c) +{ + return uint(clamp(c, 0.0, 1.0) * 255.0 + 0.5); +} + +[__readNone] +[ForceInline] +uint packSnorm1x8(float c) +{ + return uint(clamp(c, -1.0, 1.0) * 127.0 + 127.5); +} + +[__readNone] +[ForceInline] +float unpackUnorm1x16(uint p) +{ + return float(p) / 65535.0; +} + +[__readNone] +[ForceInline] +float unpackSnorm1x16(uint p) +{ + return clamp((float(p) - 32767.0) / 32767.0, -1.0, 1.0); +} + +[__readNone] +[ForceInline] +float unpackUnorm1x8(uint p) +{ + return float(p) / 255.0; +} + +[__readNone] +[ForceInline] +float unpackSnorm1x8(uint p) +{ + return clamp((float(p) - 127.0) / 127.0, -1.0, 1.0); +} + +[__readNone] +[ForceInline] +uint float2half(float f) +{ + uint u = floatBitsToUint(f); + uint s = ((u >> uint(16)) & uint(0x8000)); + uint e = 0; + uint m = ((u >> uint(13)) & uint(0x03ff)); + if (m != 0) + { + e = ((((u & uint(0x7f800000)) - uint(0x38000000)) >> uint(13)) & uint(0x7c00)); + } + return (s | e | m); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uint packUnorm2x16(vec2 v) +{ + return packUnorm1x16(v.x) | (packUnorm1x16(v.y) << uint(16)); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uint packSnorm2x16(vec2 v) +{ + return packSnorm1x16(v.x) | (packSnorm1x16(v.y) << uint(16)); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uint packUnorm4x8(vec4 v) +{ + return packUnorm1x8(v.x) | (packUnorm1x8(v.y) << uint(8)) | (packUnorm1x8(v.z) << uint(16)) | (packUnorm1x8(v.w) << uint(24)); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uint packSnorm4x8(vec4 v) +{ + return packSnorm1x8(v.x) | (packSnorm1x8(v.y) << uint(8)) | (packSnorm1x8(v.z) << uint(16)) | (packSnorm1x8(v.w) << uint(24)); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public vec2 unpackUnorm2x16(uint p) +{ + return vec2(unpackUnorm1x16(p & uint(0xffff)), unpackUnorm1x16(p >> uint(16))); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public vec2 unpackSnorm2x16(uint p) +{ + return vec2(unpackSnorm1x16(p & uint(0xffff)), unpackSnorm1x16(p >> uint(16))); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public vec4 unpackUnorm4x8(highp uint p) +{ + return vec4(unpackUnorm1x8(p & uint(0xffff)), unpackUnorm1x8(p >> uint(8)), unpackUnorm1x8(p >> uint(16)), unpackUnorm1x8(p >> uint(24))); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public vec4 unpackSnorm4x8(highp uint p) +{ + return vec4(unpackSnorm1x8(p & uint(0xffff)), unpackSnorm1x8(p >> uint(8)), unpackSnorm1x8(p >> uint(16)), unpackSnorm1x8(p >> uint(24))); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uint packHalf2x16(vec2 v) +{ + return float2half(v.x) | (float2half(v.y) << uint(16)); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public float half2float(uint h) +{ + uint s = ((h & uint(0x8000)) << uint(16)); + uint e = 0; + uint m = ((h & uint(0x03ff)) << uint(13)); + if (m != 0) + { + e = (((h & uint(0x7c00)) + uint(0x1c000)) << uint(13)); + } + return uintBitsToFloat(s | e | m); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public vec2 unpackHalf2x16(uint p) +{ + return vec2(half2float(p & uint(0xffff)), half2float(p >> uint(16))); +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public double packDouble2x32(uvec2 v) +{ + // TODO: there is no "asdouble()" + //return asdouble(uint64_t(v.x) | (uint64_t(v.y) << 32)); + return 0.0; +} + +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +public uvec2 unpackDouble2x32(double v) +{ + // TODO: there is no "asuint64()" + uint64_t u = 0; // asuint64(v); + return uvec2(uint(u & 0xFFFFFFFF), uint(u >> 32)); +} + +// +// Section 8.5. Geometric Functions +// + +__generic +[__readNone] +[ForceInline] +public T faceforward(T n, T i, T ng) +{ + return dot(ng, i) < T(0.0f) ? n : -n; +} + +// +// Section 8.6. Matrix Functions +// + +__generic +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +[OverloadRank(15)] +public matrix outerProduct(vector c, vector r) +{ + // Column major matrix in GLSL + matrix result; + for (int i = 0; i < C; ++i) + { + for (int j = 0; j < R; ++j) + { + result[i][j] = c[i] * r[j]; + } + } + return result; +} + +__generic +__target_intrinsic(hlsl) +__target_intrinsic(glsl) +matrix inverse(matrix m); + +// +// Section 8.8. Integer Functions +// + +[__readNone] +[ForceInline] +public uint uaddCarry(highp uint x, highp uint y, out lowp uint carry) +{ + let result = x * y; + carry = ((result < x || result < y) ? 1 : 0); + return result; +} + +__generic +[__readNone] +[ForceInline] +public vector uaddCarry(highp vector x, highp vector y, out lowp vector carry) +{ + VECTOR_MAP_TRINARY(uint, N, uaddCarry, x, y, carry); +} + +[__readNone] +[ForceInline] +public uint usubBorrow(highp uint x, highp uint y, out lowp uint borrow) +{ + borrow = (y > x) ? 1 : 0; + return x - y; +} + +__generic +[__readNone] +[ForceInline] +public vector usubBorrow(highp vector x, highp vector y, out lowp vector borrow) +{ + VECTOR_MAP_TRINARY(uint, N, usubBorrow, x, y, borrow); +} + +[__readNone] +[ForceInline] +public void umulExtended(highp uint x, highp uint y, out highp uint msb, out highp uint lsb) +{ + uint64_t result = x * y; + msb = uint(result >> 32); + lsb = uint(result); +} + +__generic +[__readNone] +[ForceInline] +public void umulExtended(highp vector x, highp vector y, out highp vector msb, out highp vector lsb) +{ + for(int i = 0; i < N; ++i) + { + umulExtended(x[i], y[i], msb[i], lsb[i]); + } +} + +[__readNone] +[ForceInline] +public void imulExtended(highp int x, highp int y, out highp int msb, out highp int lsb) +{ + int64_t result = x * y; + msb = int(result >> 32); + lsb = int(result); +} + +__generic +[__readNone] +[ForceInline] +public void imulExtended(highp vector x, highp vector y, out highp vector msb, out highp vector lsb) +{ + for(int i = 0; i < N; ++i) + { + imulExtended(x[i], y[i], msb[i], lsb[i]); + } +} + +[__readNone] +[ForceInline] +public int bitfieldExtract(int value, int offset, int bits) +{ + return int(uint(value >> offset) & ((1u << bits) - 1)); +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldExtract(vector value, int offset, int bits) +{ + vector result; + for (int i = 0; i < N; ++i) + { + result[i] = bitfieldExtract(value[i], offset, bits); + } + return result; +} + +[__readNone] +[ForceInline] +public uint bitfieldExtract(uint value, int offset, int bits) +{ + return (value >> offset) & ((1u << bits) - 1); +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldExtract(vector value, int offset, int bits) +{ + vector result; + for (int i = 0; i < N; ++i) + { + result[i] = bitfieldExtract(value[i], offset, bits); + } + return result; +} + +[__readNone] +[ForceInline] +public uint bitfieldInsert(uint base, uint insert, int offset, int bits) +{ + uint clearMask = ~(((1u << bits) - 1u) << offset); + uint clearedBase = base & clearMask; + uint maskedInsert = (insert & ((1u << bits) - 1u)) << offset; + return clearedBase | maskedInsert; +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldInsert(vector base, vector insert, int offset, int bits) +{ + vector result; + for (int i = 0; i < N; ++i) + { + result[i] = bitfieldInsert(base[i], insert[i], offset, bits); + } + return result; +} + +[__readNone] +[ForceInline] +public int bitfieldInsert(int base, int insert, int offset, int bits) +{ + uint clearMask = ~(((1u << bits) - 1u) << offset); + uint clearedBase = base & clearMask; + uint maskedInsert = (insert & ((1u << bits) - 1u)) << offset; + return clearedBase | maskedInsert; +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldInsert(vector base, vector insert, int offset, int bits) +{ + vector result; + for (int i = 0; i < N; ++i) + { + result[i] = bitfieldInsert(base[i], insert[i], offset, bits); + } + return result; +} + +[__readNone] +[ForceInline] +public int bitfieldReverse(highp int value) +{ + value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); + value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); + value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); + value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); + value = ((value & 0xFFFF0000) >> 16) | ((value & 0x0000FFFF) << 16); + return value; +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldReverse(highp vector value) +{ + VECTOR_MAP_UNARY(int, N, bitfieldReverse, value); +} + +[__readNone] +[ForceInline] +public uint bitfieldReverse(highp uint value) +{ + value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); + value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); + value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); + value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); + value = ((value & 0xFFFF0000) >> 16) | ((value & 0x0000FFFF) << 16); + return value; +} + +__generic +[__readNone] +[ForceInline] +public vector bitfieldReverse(highp vector value) +{ + VECTOR_MAP_UNARY(int, N, bitfieldReverse, value); +} + +[__readNone] [ForceInline] REQUIRE_KHRONOS +public uint bitCount(uint value) +{ + return countbits(value); +} + +__generic +[__readNone] [ForceInline] REQUIRE_KHRONOS +public vector bitCount(vector value) +{ + VECTOR_MAP_UNARY(uint, N, countbits, value); +} + +[__readNone] [ForceInline] REQUIRE_KHRONOS +public int bitCount(int value) +{ + return countbits(uint(value)); +} + +__generic +[__readNone] [ForceInline] REQUIRE_KHRONOS +public vector bitCount(vector value) +{ + VECTOR_MAP_UNARY(int, N, countbits, value); +} + +[__readNone] +[ForceInline] +public int findLSB(int v) +{ + return firstbitlow(v); +} + +__generic +[__readNone] +[ForceInline] +public vector findLSB(vector value) { return firstbitlow(value); } -[ForceInline] public vector findLSB(vector value) + +[__readNone] +[ForceInline] +public uint findLSB(uint v) +{ + return firstbitlow(v); +} + +__generic +[__readNone] +[ForceInline] +public vector findLSB(vector value) { return firstbitlow(value); } +[__readNone] +[ForceInline] +public int findMSB(int value) +{ + return firstbithigh(value); +} + +__generic +[__readNone] +[ForceInline] +public vector findMSB(vector value) +{ + return firstbithigh(value); +} + +[__readNone] +[ForceInline] +public uint findMSB(uint value) +{ + return firstbithigh(value); +} + +__generic +[__readNone] +[ForceInline] +public vector findMSB(vector value) +{ + return firstbithigh(value); +} + +__generic +[__readNone] +[ForceInline] +public vector not(vector x) +{ + return !x; +} + // // Section 8.9.1. Texture Query Functions // @@ -1986,3 +2787,59 @@ public vec4 shadow2DProjLod(sampler2DShadow sampler, vec4 coord, float lod) return textureProjLod(sampler, coord, lod); } +// +// Ray tracing +// + +public typealias rayQueryEXT = RayQuery; + +__glsl_extension(GL_EXT_ray_query) +__glsl_version(460) +[ForceInline] +public void rayQueryConfirmIntersectionEXT(inout rayQueryEXT q) +{ + q.CommitNonOpaqueTriangleHit(); +} + +__glsl_extension(GL_EXT_ray_query) +__glsl_version(460) +[ForceInline] +public bool rayQueryProceedEXT(inout rayQueryEXT q) +{ + return q.Proceed(); +} + +__glsl_extension(GL_EXT_ray_query) +__glsl_version(460) +[__NoSideEffect] +public uint rayQueryGetIntersectionTypeEXT(rayQueryEXT q, bool committed) +{ + if (committed) + { + q.CommittedStatus(); + } + else + { + q.CandidateType(); + } + return 0; +} + + +// +// Subgroup +// + +__glsl_extension(KHR_shader_subgroup) +__glsl_version(450) +public void subgroupBarrier() +{ + //__subgroupBarrier(); +} + +__glsl_extension(KHR_shader_subgroup) +__glsl_version(450) +public void subgroupMemoryBarrier() +{ +} + diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 2bf0c1d80..8183c2030 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -2,6 +2,9 @@ typedef uint UINT; +__intrinsic_op($(kIROp_FloatCast)) +T __floatCast(U v); + [sealed] interface IBufferDataLayout { @@ -4407,6 +4410,16 @@ T distance(T x, T y) // Vector dot product +__generic +__target_intrinsic(hlsl) +__target_intrinsic(glsl) +[__readNone] +[ForceInline] +T dot(T x, T y) +{ + return x * y; +} + __generic __target_intrinsic(hlsl) __target_intrinsic(glsl) @@ -4561,16 +4574,34 @@ matrix exp(matrix x) __generic __target_intrinsic(hlsl) -__target_intrinsic(glsl) __target_intrinsic(cuda, "$P_exp2($0)") __target_intrinsic(cpp, "$P_exp2($0)") -__target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Exp2 _0") [__readNone] -T exp2(T x); +T exp2(T x) +{ + __target_switch + { + case glsl: + if (__isHalf()) + __intrinsic_asm "exp2($0)"; + __intrinsic_asm "exp2(float($0))"; + case spirv: + if (__isHalf()) + { + return spirv_asm { OpExtInst $$T result glsl450 Exp2 $x }; + } + else + { + float xf = __floatCast(x); + return T(spirv_asm { + result:$$float = OpExtInst glsl450 Exp2 $xf + }); + } + } +} __generic __target_intrinsic(hlsl) -__target_intrinsic(glsl) __target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Exp2 _0") [__readNone] vector exp2(vector x) @@ -4786,31 +4817,41 @@ matrix floor(matrix x) MATRIX_MAP_UNARY(T, N, M, floor, x); } -// Fused multiply-add for doubles -__target_intrinsic(hlsl) +// Fused multiply-add +__generic __target_intrinsic(glsl) __target_intrinsic(cuda, "$P_fma($0, $1, $2)") __target_intrinsic(cpp, "$P_fma($0, $1, $2)") __target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Fma _0 _1 _2") [__readNone] -double fma(double a, double b, double c); +T fma(T a, T b, T c) +{ + __target_switch + { + case hlsl: + if (__isFloat() || __isHalf()) + return mad(a, b, c); + else + __intrinsic_asm "fma($0, $1, $2)"; + } +} -__generic +__generic __target_intrinsic(hlsl) __target_intrinsic(glsl) __target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Fma _0 _1 _2") [__readNone] -vector fma(vector a, vector b, vector c) +vector fma(vector a, vector b, vector c) { - VECTOR_MAP_TRINARY(double, N, fma, a, b, c); + VECTOR_MAP_TRINARY(T, N, fma, a, b, c); } -__generic +__generic __target_intrinsic(hlsl) [__readNone] -matrix fma(matrix a, matrix b, matrix c) +matrix fma(matrix a, matrix b, matrix c) { - MATRIX_MAP_TRINARY(double, N, M, fma, a, b, c); + MATRIX_MAP_TRINARY(T, N, M, fma, a, b, c); } // Floating point remainder of x/y @@ -6414,6 +6455,16 @@ vector normalize(vector x) return x / length(x); } +__generic +__target_intrinsic(hlsl) +__target_intrinsic(glsl) +__target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Normalize _0") +[__readNone] +T normalize(T x) +{ + return x / length(x); +} + // Raise to a power __generic __target_intrinsic(hlsl) @@ -6618,6 +6669,16 @@ matrix rcp(matrix x) } // Reflect incident vector across plane with given normal +__generic +__target_intrinsic(hlsl) +__target_intrinsic(glsl) +__target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Reflect _0 _1") +[__readNone] +T reflect(T i, T n) +{ + return i - T(2) * dot(n,i) * n; +} + __generic __target_intrinsic(hlsl) __target_intrinsic(glsl) @@ -6642,6 +6703,19 @@ vector refract(vector i, vector n, T eta) return eta * i - (eta * dotNI + sqrt(k)) * n; } +__generic +__target_intrinsic(hlsl) +__target_intrinsic(glsl) +__target_intrinsic(spirv, "OpExtInst resultType resultId glsl450 Refract _0 _1 _2") +[__readNone] +T refract(T i, T n, T eta) +{ + let dotNI = dot(n,i); + let k = T(1) - eta*eta*(T(1) - dotNI * dotNI); + if(k < T(0)) return T(0); + return eta * i - (eta * dotNI + sqrt(k)) * n; +} + // Reverse order of bits [__readNone] uint reversebits(uint value) -- cgit v1.2.3