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 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 4 deletions(-) (limited to 'source') 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") -- cgit v1.2.3