From df9dc5710725d00630831b77ca7005e390383aa6 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 28 Jan 2019 12:28:05 -0500 Subject: 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. --- source/slang/emit.cpp | 17 +++++++- source/slang/hlsl.meta.slang | 53 +++++++++++++++++++++++- source/slang/hlsl.meta.slang.h | 53 +++++++++++++++++++++++- tests/compute/bit-cast-double.slang | 21 ++++++++++ tests/compute/bit-cast-double.slang.expected.txt | 4 ++ tests/compute/bit-cast.slang | 42 +++++++++++++++++++ tests/compute/bit-cast.slang.expected.txt | 12 ++++++ 7 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 tests/compute/bit-cast-double.slang create mode 100644 tests/compute/bit-cast-double.slang.expected.txt create mode 100644 tests/compute/bit-cast.slang create mode 100644 tests/compute/bit-cast.slang.expected.txt 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(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 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 +__target_intrinsic(glsl, "intBitsToFloat") +vector asfloat(vector< int,N> x); + +__generic +__target_intrinsic(glsl, "uintBitsToFloat") +vector asfloat(vector x); + float asfloat( int x); float asfloat(uint x); __generic vector asfloat(vector< int,N> x); @@ -375,13 +393,27 @@ __generic vector asfloat(vector x); __generic matrix asfloat(matrix< int,N,M> x); __generic matrix asfloat(matrix x); - // Inverse sine (HLSL SM 1.0) __generic T asin(T x); __generic vector asin(vector x); __generic matrix asin(matrix x); // Reinterpret bits as an int (HLSL SM 4.0) + +__target_intrinsic(glsl, "floatBitsToInt") +int asint(float x); + +__generic +__target_intrinsic(glsl, "floatBitsToInt") +vector asint(vector x); + +__target_intrinsic(glsl, "int($0)") +int asint(uint x); + +__generic +__target_intrinsic(glsl, "ivec$N0($0)") +vector asint(vector x); + int asint(float x); int asint(uint x); __generic vector asint(vector x); @@ -390,9 +422,28 @@ __generic matrix asint(matrix x); __generic matrix asint(matrix 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 +__target_intrinsic(glsl, "floatBitsToUint") +vector asuint(vector x); + +__target_intrinsic(glsl, "uint($0)") +uint asuint(int x); + +__generic +__target_intrinsic(glsl, "uvec$N0($0)") +vector asuint(vector x); + uint asuint(float x); uint asuint(int x); __generic vector asuint(vector 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 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\n") +SLANG_RAW("__target_intrinsic(glsl, \"intBitsToFloat\")\n") +SLANG_RAW("vector asfloat(vector< int,N> x);\n") +SLANG_RAW("\n") +SLANG_RAW("__generic\n") +SLANG_RAW("__target_intrinsic(glsl, \"uintBitsToFloat\")\n") +SLANG_RAW("vector asfloat(vector x);\n") +SLANG_RAW("\n") SLANG_RAW("float asfloat( int x);\n") SLANG_RAW("float asfloat(uint x);\n") SLANG_RAW("__generic vector asfloat(vector< int,N> x);\n") @@ -420,13 +438,27 @@ SLANG_RAW("__generic vector asfloat(vector x);\n") SLANG_RAW("__generic matrix asfloat(matrix< int,N,M> x);\n") SLANG_RAW("__generic matrix asfloat(matrix x);\n") SLANG_RAW("\n") -SLANG_RAW("\n") SLANG_RAW("// Inverse sine (HLSL SM 1.0)\n") SLANG_RAW("__generic T asin(T x);\n") SLANG_RAW("__generic vector asin(vector x);\n") SLANG_RAW("__generic matrix asin(matrix 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\n") +SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToInt\")\n") +SLANG_RAW("vector asint(vector 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\n") +SLANG_RAW("__target_intrinsic(glsl, \"ivec$N0($0)\")\n") +SLANG_RAW("vector asint(vector x);\n") +SLANG_RAW("\n") SLANG_RAW("int asint(float x);\n") SLANG_RAW("int asint(uint x);\n") SLANG_RAW("__generic vector asint(vector x);\n") @@ -435,9 +467,28 @@ SLANG_RAW("__generic matrix asint(matrix matrix asint(matrix 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\n") +SLANG_RAW("__target_intrinsic(glsl, \"floatBitsToUint\")\n") +SLANG_RAW("vector asuint(vector 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\n") +SLANG_RAW("__target_intrinsic(glsl, \"uvec$N0($0)\")\n") +SLANG_RAW("vector asuint(vector x);\n") +SLANG_RAW("\n") SLANG_RAW("uint asuint(float x);\n") SLANG_RAW("uint asuint(int x);\n") SLANG_RAW("__generic vector asuint(vector 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 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 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 -- cgit v1.2.3