diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-02-27 10:38:51 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-27 10:38:51 -0500 |
| commit | 3fc4813449f40872cff2320e66785619376cd119 (patch) | |
| tree | a3881ebf55d345ad64ec55081183aede536d4388 | |
| parent | 13ad9c83e7617bf8afef1509e4becde19dd1f59d (diff) | |
* Add 'identity' version of bit casts (asint, asuint, asfloat) for scalar and vector (#864)
* Added identity bit casts for matrix (cos no op). We don't support matrix asint on glsl targets
* Added tests in bit-cast.slang
| -rw-r--r-- | source/slang/hlsl.meta.slang | 82 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang.h | 82 | ||||
| -rw-r--r-- | tests/compute/bit-cast.slang | 25 |
3 files changed, 141 insertions, 48 deletions
diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index fc02395d4..e51016a53 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -375,30 +375,37 @@ double asdouble(uint lowbits, uint highbits); // Reinterpret bits as a float (HLSL SM 4.0) +// GLSL Scalar __target_intrinsic(glsl, "intBitsToFloat") float asfloat(int x); __target_intrinsic(glsl, "uintBitsToFloat") float asfloat(uint x); +__target_intrinsic(glsl, "$0") +float asfloat(float x); +// GLSL Vector +__generic<let N : int> +__target_intrinsic(glsl, "$0") +vector<float,N> asfloat(vector<float,N> x); __generic<let N : int> __target_intrinsic(glsl, "intBitsToFloat") vector<float,N> asfloat(vector< int,N> x); - __generic<let N : int> __target_intrinsic(glsl, "uintBitsToFloat") vector<float,N> asfloat(vector<uint,N> x); -float asfloat( int x); -__target_intrinsic(glsl, uintBitsToFloat) +// GLSL Matrix (only identity for now) +__generic<let N : int, let M : int> +__target_intrinsic(glsl, "$0") +matrix<float,N,M> asfloat(matrix<float,N,M> x); + +// Pass thru to HLSL +float asfloat(float x); float asfloat(uint x); -__generic<let N : int> -__target_intrinsic(glsl, intBitsAsFloat) -vector<float,N> asfloat(vector< int,N> x); -__generic<let N : int> -__target_intrinsic(glsl, uintBitsAsFloat) -vector<float,N> asfloat(vector<uint,N> x); +float asfloat(int x); __generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix< int,N,M> x); __generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix<uint,N,M> x); +__generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix<float,N,M> x); // Inverse sine (HLSL SM 1.0) __generic<T : __BuiltinFloatingPointType> T asin(T x); @@ -407,31 +414,42 @@ __generic<T : __BuiltinFloatingPointType, let N : int, let M : int> matrix<T,N,M // Reinterpret bits as an int (HLSL SM 4.0) +// GLSL scalar __target_intrinsic(glsl, "floatBitsToInt") int asint(float x); +__target_intrinsic(glsl, "int($0)") +int asint(uint x); +__target_intrinsic(glsl, "$0") +int asint(int x); +// GLSL Vector __generic<let N : int> __target_intrinsic(glsl, "floatBitsToInt") vector<int,N> asint(vector<float,N> x); - -__target_intrinsic(glsl, "int($0)") -int asint(uint x); - __generic<let N : int> __target_intrinsic(glsl, "ivec$N0($0)") vector<int,N> asint(vector<uint,N> x); +__generic<let N : int> +__target_intrinsic(glsl, "$0") +vector<int,N> asint(vector<int,N> x); + +// GLSL Matrix (just identity for now) + +__generic<let N : int, let M : int> +__target_intrinsic(glsl, "$0") +matrix<int,N,M> asint(matrix<int,N,M> x); + +// Pass thru HLSL int asint(float x); -__target_intrinsic(glsl, "uint($0)") +int asint(int x); int asint(uint x); -__generic<let N : int> -__target_intrinsic(glsl, floatBitsToInt) -vector<int,N> asint(vector<float,N> x); -__generic<let N : int> -vector<int,N> asint(vector<uint,N> x); +__generic<let N : int> vector<int,N> asint(vector<uint,N> x); +__generic<let N : int> vector<int,N> asint(vector<int,N> x); __generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<float,N,M> x); __generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<uint,N,M> x); +__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<int,N,M> x); // Reinterpret bits of double as a uint (HLSL SM 5.0) @@ -442,26 +460,44 @@ void asuint(double value, out uint lowbits, out uint highbits); void asuint(double value, out uint lowbits, out uint highbits); // Reinterpret bits as a uint (HLSL SM 4.0) + +// GLSL Scalar __target_intrinsic(glsl, "floatBitsToUint") uint asuint(float x); +__target_intrinsic(glsl, "uint($0)") +uint asuint(int x); +__target_intrinsic(glsl, "$0") +uint asuint(uint x); +// GLSL Vector __generic<let N : int> __target_intrinsic(glsl, "floatBitsToUint") vector<uint,N> asuint(vector<float,N> x); - -__target_intrinsic(glsl, "uint($0)") -uint asuint(int x); - __generic<let N : int> __target_intrinsic(glsl, "uvec$N0($0)") vector<uint,N> asuint(vector<int,N> x); +__generic<let N : int> +__target_intrinsic(glsl, "$0") +vector<uint,N> asuint(vector<uint,N> x); + +// GLSL Matrix (Identity only for now) +__generic<let N : int, let M : int> +__target_intrinsic(glsl, "$0") +matrix<uint,N,M> asuint(matrix<uint,N,M> x); + +// Pass thru HLSL uint asuint(float x); uint asuint(int x); +uint asuint(uint x); + __generic<let N : int> vector<uint,N> asuint(vector<float,N> x); __generic<let N : int> vector<uint,N> asuint(vector<int,N> x); +__generic<let N : int> vector<uint,N> asuint(vector<uint,N> x); + __generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<float,N,M> x); __generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<int,N,M> x); +__generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<uint,N,M> x); // Inverse tangent (HLSL SM 1.0) __generic<T : __BuiltinFloatingPointType> T atan(T x); diff --git a/source/slang/hlsl.meta.slang.h b/source/slang/hlsl.meta.slang.h index 1dcf55f2a..5132fbc29 100644 --- a/source/slang/hlsl.meta.slang.h +++ b/source/slang/hlsl.meta.slang.h @@ -420,30 +420,37 @@ SLANG_RAW("double asdouble(uint lowbits, uint highbits);\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits as a float (HLSL SM 4.0)\n") SLANG_RAW("\n") +SLANG_RAW("// GLSL Scalar\n") SLANG_RAW("__target_intrinsic(glsl, \"intBitsToFloat\")\n") SLANG_RAW("float asfloat(int x);\n") SLANG_RAW("__target_intrinsic(glsl, \"uintBitsToFloat\")\n") SLANG_RAW("float asfloat(uint x);\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("float asfloat(float x);\n") SLANG_RAW("\n") +SLANG_RAW("// GLSL Vector\n") +SLANG_RAW("__generic<let N : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("vector<float,N> asfloat(vector<float,N> x);\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"intBitsToFloat\")\n") SLANG_RAW("vector<float,N> asfloat(vector< int,N> x);\n") -SLANG_RAW("\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"uintBitsToFloat\")\n") SLANG_RAW("vector<float,N> asfloat(vector<uint,N> x);\n") SLANG_RAW("\n") -SLANG_RAW("float asfloat( int x);\n") -SLANG_RAW("__target_intrinsic(glsl, uintBitsToFloat)\n") +SLANG_RAW("// GLSL Matrix (only identity for now)\n") +SLANG_RAW("__generic<let N : int, let M : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("matrix<float,N,M> asfloat(matrix<float,N,M> x);\n") +SLANG_RAW("\n") +SLANG_RAW("// Pass thru to HLSL\n") +SLANG_RAW("float asfloat(float x);\n") SLANG_RAW("float asfloat(uint x);\n") -SLANG_RAW("__generic<let N : int>\n") -SLANG_RAW("__target_intrinsic(glsl, intBitsAsFloat)\n") -SLANG_RAW("vector<float,N> asfloat(vector< int,N> x);\n") -SLANG_RAW("__generic<let N : int>\n") -SLANG_RAW("__target_intrinsic(glsl, uintBitsAsFloat)\n") -SLANG_RAW("vector<float,N> asfloat(vector<uint,N> x);\n") +SLANG_RAW("float asfloat(int x);\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix< int,N,M> x);\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix<uint,N,M> x);\n") +SLANG_RAW("__generic<let N : int, let M : int> matrix<float,N,M> asfloat(matrix<float,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("// Inverse sine (HLSL SM 1.0)\n") SLANG_RAW("__generic<T : __BuiltinFloatingPointType> T asin(T x);\n") @@ -452,31 +459,42 @@ SLANG_RAW("__generic<T : __BuiltinFloatingPointType, let N : int, let M : int> m SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits as an int (HLSL SM 4.0)\n") SLANG_RAW("\n") +SLANG_RAW("// GLSL scalar\n") SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToInt\")\n") SLANG_RAW("int asint(float x);\n") +SLANG_RAW("__target_intrinsic(glsl, \"int($0)\")\n") +SLANG_RAW("int asint(uint x);\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("int asint(int x);\n") SLANG_RAW("\n") +SLANG_RAW("// GLSL Vector\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToInt\")\n") SLANG_RAW("vector<int,N> asint(vector<float,N> x);\n") -SLANG_RAW("\n") -SLANG_RAW("__target_intrinsic(glsl, \"int($0)\")\n") -SLANG_RAW("int asint(uint x);\n") -SLANG_RAW("\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"ivec$N0($0)\")\n") SLANG_RAW("vector<int,N> asint(vector<uint,N> x);\n") +SLANG_RAW("__generic<let N : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("vector<int,N> asint(vector<int,N> x);\n") +SLANG_RAW("\n") +SLANG_RAW("// GLSL Matrix (just identity for now)\n") +SLANG_RAW("\n") +SLANG_RAW("__generic<let N : int, let M : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("matrix<int,N,M> asint(matrix<int,N,M> x);\n") +SLANG_RAW("\n") +SLANG_RAW("// Pass thru HLSL\n") SLANG_RAW("\n") SLANG_RAW("int asint(float x);\n") -SLANG_RAW("__target_intrinsic(glsl, \"uint($0)\")\n") +SLANG_RAW("int asint(int x);\n") SLANG_RAW("int asint(uint x);\n") SLANG_RAW("\n") -SLANG_RAW("__generic<let N : int> \n") -SLANG_RAW("__target_intrinsic(glsl, floatBitsToInt)\n") -SLANG_RAW("vector<int,N> asint(vector<float,N> x);\n") -SLANG_RAW("__generic<let N : int> \n") -SLANG_RAW("vector<int,N> asint(vector<uint,N> x);\n") +SLANG_RAW("__generic<let N : int> vector<int,N> asint(vector<uint,N> x);\n") +SLANG_RAW("__generic<let N : int> vector<int,N> asint(vector<int,N> x);\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<float,N,M> x);\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<uint,N,M> x);\n") +SLANG_RAW("__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<int,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits of double as a uint (HLSL SM 5.0)\n") SLANG_RAW("\n") @@ -487,26 +505,44 @@ SLANG_RAW("\n") SLANG_RAW("void asuint(double value, out uint lowbits, out uint highbits);\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits as a uint (HLSL SM 4.0)\n") +SLANG_RAW("\n") +SLANG_RAW("// GLSL Scalar\n") SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToUint\")\n") SLANG_RAW("uint asuint(float x);\n") +SLANG_RAW("__target_intrinsic(glsl, \"uint($0)\")\n") +SLANG_RAW("uint asuint(int x);\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("uint asuint(uint x);\n") SLANG_RAW("\n") +SLANG_RAW("// GLSL Vector\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToUint\")\n") SLANG_RAW("vector<uint,N> asuint(vector<float,N> x);\n") -SLANG_RAW("\n") -SLANG_RAW("__target_intrinsic(glsl, \"uint($0)\")\n") -SLANG_RAW("uint asuint(int x);\n") -SLANG_RAW("\n") SLANG_RAW("__generic<let N : int>\n") SLANG_RAW("__target_intrinsic(glsl, \"uvec$N0($0)\")\n") SLANG_RAW("vector<uint,N> asuint(vector<int,N> x);\n") +SLANG_RAW("__generic<let N : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("vector<uint,N> asuint(vector<uint,N> x);\n") +SLANG_RAW("\n") +SLANG_RAW("// GLSL Matrix (Identity only for now)\n") +SLANG_RAW("__generic<let N : int, let M : int>\n") +SLANG_RAW("__target_intrinsic(glsl, \"$0\")\n") +SLANG_RAW("matrix<uint,N,M> asuint(matrix<uint,N,M> x);\n") +SLANG_RAW("\n") +SLANG_RAW("// Pass thru HLSL\n") SLANG_RAW("\n") SLANG_RAW("uint asuint(float x);\n") SLANG_RAW("uint asuint(int x);\n") +SLANG_RAW("uint asuint(uint x);\n") +SLANG_RAW("\n") SLANG_RAW("__generic<let N : int> vector<uint,N> asuint(vector<float,N> x);\n") SLANG_RAW("__generic<let N : int> vector<uint,N> asuint(vector<int,N> x);\n") +SLANG_RAW("__generic<let N : int> vector<uint,N> asuint(vector<uint,N> x);\n") +SLANG_RAW("\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<float,N,M> x);\n") SLANG_RAW("__generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<int,N,M> x);\n") +SLANG_RAW("__generic<let N : int, let M : int> matrix<uint,N,M> asuint(matrix<uint,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("// Inverse tangent (HLSL SM 1.0)\n") SLANG_RAW("__generic<T : __BuiltinFloatingPointType> T atan(T x);\n") diff --git a/tests/compute/bit-cast.slang b/tests/compute/bit-cast.slang index 324400a34..a6c4813f0 100644 --- a/tests/compute/bit-cast.slang +++ b/tests/compute/bit-cast.slang @@ -13,10 +13,20 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) { int4 i4 = int4(id, id + 1, id + 2, id + 3); + + // Identity - int vector + i4 = asint(i4); + uint3 u3 = asuint(i4.xyz); + // Identity - uint vec + u3 = asuint(u3); + int2 i2 = asint(u3.xy); uint u1 = asuint(i2.x); + // Identity - uint scalar + u1 = asuint(u1); + outputBuffer[id + 0] = int(u1); } @@ -27,18 +37,29 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) uint2 u2 = asuint(i3.xy); int i1 = asint(u2.x); + // Identity - int scalar + i1 = asint(i1); + outputBuffer[id + 4] = i1; } { // Make i4 holds id as floats so we know they are valid float values and not denormals int4 i4 = int4(asint(float(id)), asint(float(id + 1)), asint(float(id + 2)), asint(float(id + 3))); - float4 f4 = asfloat(i4); + float4 f4 = asfloat(asfloat(i4)); + + // Identity - float vector + f4 = asfloat(f4); + uint3 u3 = asuint(f4.xyz); float2 f2 = asfloat(u3.xy); uint u1 = asuint(f2.x); - float f1 = asfloat(u1); + float f1 = asfloat(asfloat(u1)); + + // Identity - float scalar + f1 = asfloat(f1); + int i1 = asint(f1); float f1_ = asfloat(i1); |
