diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2019-01-28 12:28:05 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-28 12:28:05 -0500 |
| commit | df9dc5710725d00630831b77ca7005e390383aa6 (patch) | |
| tree | d75dd22c377ee4396c43e6d75518733589d1cf9a | |
| parent | 41fde4b836dbab627f8643a5002b2b6dd0417211 (diff) | |
Feature/bit cast glsl (#808)
* First attempt at asint, asuint, asfloat intrinsics.
* Test with countbits
* Placing glsl definitions first makes them get picked up.
* Some more improvements around asint.
* Add support for vector versions of asint/asunit
* Fix some typos in asuint/asint intrinsics for glsl.
Simplified and increased coverage of as/u/int tests.
* Added bit-cast-double test.
Added notional support for asdouble bit casts to glsl - but couldn't test because glslang doesn't seem to support the extension.
* Try to get double bit casts working - doesn't work cos of block issue.
* Only output parents on intrinic replacement if return type is not void.
| -rw-r--r-- | source/slang/emit.cpp | 17 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang | 53 | ||||
| -rw-r--r-- | source/slang/hlsl.meta.slang.h | 53 | ||||
| -rw-r--r-- | tests/compute/bit-cast-double.slang | 21 | ||||
| -rw-r--r-- | tests/compute/bit-cast-double.slang.expected.txt | 4 | ||||
| -rw-r--r-- | tests/compute/bit-cast.slang | 42 | ||||
| -rw-r--r-- | tests/compute/bit-cast.slang.expected.txt | 12 |
7 files changed, 198 insertions, 4 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 914aea7fd..71b961616 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3002,9 +3002,19 @@ struct EmitVisitor } else { + // If it returns void -> then we don't need parenthesis + + const auto returnType = inst->getDataType(); + const bool isVoid = as<IRVoidType>(returnType) != nullptr; + + // We could determine here is the return type is void... if so no braces? + // General case: we are going to emit some more complex text. - Emit("("); + if (!isVoid) + { + Emit("("); + } char const* cursor = name.begin(); char const* end = name.end(); @@ -3364,7 +3374,10 @@ struct EmitVisitor } } - Emit(")"); + if (!isVoid) + { + Emit(")"); + } } } diff --git a/source/slang/hlsl.meta.slang b/source/slang/hlsl.meta.slang index 541d12112..55457d864 100644 --- a/source/slang/hlsl.meta.slang +++ b/source/slang/hlsl.meta.slang @@ -365,9 +365,27 @@ bool any(matrix<T,N,M> x); // Reinterpret bits as a double (HLSL SM 5.0)
+
+__target_intrinsic(glsl, "packDouble2x32(uvec2($0, $1))")
+__glsl_extension(GL_ARB_gpu_shader5)
+double asdouble(uint lowbits, uint highbits);
+
double asdouble(uint lowbits, uint highbits);
// Reinterpret bits as a float (HLSL SM 4.0)
+__target_intrinsic(glsl, "intBitsToFloat")
+float asfloat(int x);
+__target_intrinsic(glsl, "uintBitsToFloat")
+float asfloat(uint 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);
float asfloat(uint x);
__generic<let N : int> vector<float,N> asfloat(vector< int,N> x);
@@ -375,13 +393,27 @@ __generic<let N : int> vector<float,N> asfloat(vector<uint,N> 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);
-
// Inverse sine (HLSL SM 1.0)
__generic<T : __BuiltinFloatingPointType> T asin(T x);
__generic<T : __BuiltinFloatingPointType, let N : int> vector<T,N> asin(vector<T,N> x);
__generic<T : __BuiltinFloatingPointType, let N : int, let M : int> matrix<T,N,M> asin(matrix<T,N,M> x);
// Reinterpret bits as an int (HLSL SM 4.0)
+
+__target_intrinsic(glsl, "floatBitsToInt")
+int asint(float x);
+
+__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);
+
int asint(float x);
int asint(uint x);
__generic<let N : int> vector<int,N> asint(vector<float,N> x);
@@ -390,9 +422,28 @@ __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);
// Reinterpret bits of double as a uint (HLSL SM 5.0)
+
+__target_intrinsic(glsl, "{ uvec2 v = unpackDouble2x32($0); $1 = v.x; $2 = v.y; }")
+__glsl_extension(GL_ARB_gpu_shader5)
+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)
+__target_intrinsic(glsl, "floatBitsToUint")
+uint asuint(float x);
+
+__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);
+
uint asuint(float x);
uint asuint(int x);
__generic<let N : int> vector<uint,N> asuint(vector<float,N> x);
diff --git a/source/slang/hlsl.meta.slang.h b/source/slang/hlsl.meta.slang.h index 9fb06b4b0..df6dcea48 100644 --- a/source/slang/hlsl.meta.slang.h +++ b/source/slang/hlsl.meta.slang.h @@ -410,9 +410,27 @@ SLANG_RAW("bool any(matrix<T,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits as a double (HLSL SM 5.0)\n") +SLANG_RAW("\n") +SLANG_RAW("__target_intrinsic(glsl, \"packDouble2x32(uvec2($0, $1))\")\n") +SLANG_RAW("__glsl_extension(GL_ARB_gpu_shader5)\n") +SLANG_RAW("double asdouble(uint lowbits, uint highbits);\n") +SLANG_RAW("\n") 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("__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("\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("float asfloat(uint x);\n") SLANG_RAW("__generic<let N : int> vector<float,N> asfloat(vector< int,N> x);\n") @@ -420,13 +438,27 @@ SLANG_RAW("__generic<let N : int> vector<float,N> asfloat(vector<uint,N> 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("\n") -SLANG_RAW("\n") SLANG_RAW("// Inverse sine (HLSL SM 1.0)\n") SLANG_RAW("__generic<T : __BuiltinFloatingPointType> T asin(T x);\n") SLANG_RAW("__generic<T : __BuiltinFloatingPointType, let N : int> vector<T,N> asin(vector<T,N> x);\n") SLANG_RAW("__generic<T : __BuiltinFloatingPointType, let N : int, let M : int> matrix<T,N,M> asin(matrix<T,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits as an int (HLSL SM 4.0)\n") +SLANG_RAW("\n") +SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToInt\")\n") +SLANG_RAW("int asint(float 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("\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("\n") SLANG_RAW("int asint(float x);\n") SLANG_RAW("int asint(uint x);\n") SLANG_RAW("__generic<let N : int> vector<int,N> asint(vector<float,N> x);\n") @@ -435,9 +467,28 @@ SLANG_RAW("__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<floa SLANG_RAW("__generic<let N : int, let M : int> matrix<int,N,M> asint(matrix<uint,N,M> x);\n") SLANG_RAW("\n") SLANG_RAW("// Reinterpret bits of double as a uint (HLSL SM 5.0)\n") +SLANG_RAW("\n") +SLANG_RAW("__target_intrinsic(glsl, \"{ uvec2 v = unpackDouble2x32($0); $1 = v.x; $2 = v.y; }\")\n") +SLANG_RAW("__glsl_extension(GL_ARB_gpu_shader5)\n") +SLANG_RAW("void asuint(double value, out uint lowbits, out uint highbits);\n") +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("__target_intrinsic(glsl, \"floatBitsToUint\")\n") +SLANG_RAW("uint asuint(float x);\n") +SLANG_RAW("\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("\n") SLANG_RAW("uint asuint(float x);\n") SLANG_RAW("uint asuint(int x);\n") SLANG_RAW("__generic<let N : int> vector<uint,N> asuint(vector<float,N> x);\n") diff --git a/tests/compute/bit-cast-double.slang b/tests/compute/bit-cast-double.slang new file mode 100644 index 000000000..27d0b48cf --- /dev/null +++ b/tests/compute/bit-cast-double.slang @@ -0,0 +1,21 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute + +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + double d = double(dispatchThreadID.x); + + uint hi, low; + asuint(d, low, hi); + + // Reconstruct + double r = asdouble(low, hi); + + outputBuffer[dispatchThreadID.x] = int(r); +}
\ No newline at end of file diff --git a/tests/compute/bit-cast-double.slang.expected.txt b/tests/compute/bit-cast-double.slang.expected.txt new file mode 100644 index 000000000..bc856dafa --- /dev/null +++ b/tests/compute/bit-cast-double.slang.expected.txt @@ -0,0 +1,4 @@ +0 +1 +2 +3 diff --git a/tests/compute/bit-cast.slang b/tests/compute/bit-cast.slang new file mode 100644 index 000000000..050b84f30 --- /dev/null +++ b/tests/compute/bit-cast.slang @@ -0,0 +1,42 @@ +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute +//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 +//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute + +//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +RWStructuredBuffer<int> outputBuffer; + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + const int id = asint(dispatchThreadID.x); + + { + int4 i4 = int4(id, id + 1, id + 2, id + 3); + uint3 u3 = asuint(i4.xyz); + int2 i2 = asint(u3.xy); + uint u1 = asuint(i2.x); + + outputBuffer[id + 0] = int(u1); + } + + { + uint v = asuint(id); + uint4 u4 = uint4(v, v + 1, v + 2, v + 3); + int3 i3 = asint(u4.xyz); + uint2 u2 = asuint(i3.xy); + int i1 = asint(u2.x); + + outputBuffer[id + 4] = i1; + } + + { + int4 i4 = int4(id, id + 1, id + 2, id + 3); + float4 f4 = asfloat(i4); + uint3 u3 = asuint(f4.xyz); + float2 f2 = asfloat(u3.xy); + int i1 = asint(id); + + outputBuffer[id + 8] = i1; + } +}
\ No newline at end of file diff --git a/tests/compute/bit-cast.slang.expected.txt b/tests/compute/bit-cast.slang.expected.txt new file mode 100644 index 000000000..f5414793f --- /dev/null +++ b/tests/compute/bit-cast.slang.expected.txt @@ -0,0 +1,12 @@ +0 +1 +2 +3 +0 +1 +2 +3 +0 +1 +2 +3
\ No newline at end of file |
