From e03d0f332298e95242ce267e2c0a61474516c88c Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 18 Nov 2025 12:09:20 +0100 Subject: [PATCH] Fix shader compilation with HLSL 2021 --- Content/Shaders/VolumetricFog.flax | 4 +-- Source/Shaders/Common.hlsl | 34 +++++++++++++++++++++++ Source/Shaders/GammaCorrectionCommon.hlsl | 2 +- Source/Shaders/Math.hlsl | 2 +- Source/Shaders/VolumetricFog.shader | 2 +- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Content/Shaders/VolumetricFog.flax b/Content/Shaders/VolumetricFog.flax index 6f8776336..6dfecbb26 100644 --- a/Content/Shaders/VolumetricFog.flax +++ b/Content/Shaders/VolumetricFog.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4a3e331b7d688d4d3a11da6bb50d4608dbc6437978433a83cc79a365f520bc58 -size 13206 +oid sha256:872ac3560279bfd0aeb989ebac1b49750dd142b985bc40058888dfd2b63fe9b2 +size 13214 diff --git a/Source/Shaders/Common.hlsl b/Source/Shaders/Common.hlsl index 834348ade..10b2855f4 100644 --- a/Source/Shaders/Common.hlsl +++ b/Source/Shaders/Common.hlsl @@ -93,6 +93,40 @@ #endif +// Compiler support for HLSL 2021 that is stricter (need to use or/and/select for vector-based logical operators) +#if !defined(__DXC_VERSION_MAJOR) || (__DXC_VERSION_MAJOR <= 1 && __DXC_VERSION_MINOR < 7) + +bool InternalAnd(bool a, bool b) { return bool(a && b); } +bool2 InternalAnd(bool2 a, bool2 b) { return bool2(a.x && b.x, a.y && b.y); } +bool3 InternalAnd(bool3 a, bool3 b) { return bool3(a.x && b.x, a.y && b.y, a.z && b.z); } +bool4 InternalAnd(bool4 a, bool4 b) { return bool4(a.x && b.x, a.y && b.y, a.z && b.z, a.w && b.w); } + +bool InternalOr(bool a, bool b) { return bool(a || b); } +bool2 InternalOr(bool2 a, bool2 b) { return bool2(a.x || b.x, a.y || b.y); } +bool3 InternalOr(bool3 a, bool3 b) { return bool3(a.x || b.x, a.y || b.y, a.z || b.z); } +bool4 InternalOr(bool4 a, bool4 b) { return bool4(a.x || b.x, a.y || b.y, a.z || b.z, a.w || b.w); } + +#define SELECT_INTERNAL(type) \ + type InternalSelect(bool c, type a, type b) { return type (c ? a.x : b.x); } \ + type##2 InternalSelect(bool c, type##2 a, type##2 b) { return type##2(c ? a.x : b.x, c ? a.y : b.y); } \ + type##2 InternalSelect(bool2 c, type a, type b) { return type##2(c.x ? a : b, c.y ? a : b); } \ + type##2 InternalSelect(bool2 c, type##2 a, type##2 b) { return type##2(c.x ? a.x : b.x, c.y ? a.y : b.y); } \ + type##3 InternalSelect(bool c, type##3 a, type##3 b) { return type##3(c ? a.x : b.x, c ? a.y : b.y, c ? a.z : b.z); } \ + type##3 InternalSelect(bool3 c, type a, type b) { return type##3(c.x ? a : b, c.y ? a : b, c.z ? a : b); } \ + type##3 InternalSelect(bool3 c, type##3 a, type##3 b) { return type##3(c.x ? a.x : b.x, c.y ? a.y : b.y, c.z ? a.z : b.z); } \ + type##4 InternalSelect(bool c, type##4 a, type##4 b) { return type##4(c ? a.x : b.x, c ? a.y : b.y, c ? a.z : b.z, c ? a.w : b.w); } \ + type##4 InternalSelect(bool4 c, type a, type b) { return type##4(c.x ? a : b, c.y ? a : b, c.z ? a : b, c.w ? a : b); } \ + type##4 InternalSelect(bool4 c, type##4 a, type##4 b) { return type##4(c.x ? a.x : b.x, c.y ? a.y : b.y, c.z ? a.z : b.z, c.w ? a.w : b.w); } +SELECT_INTERNAL(uint) +SELECT_INTERNAL(float) +#undef SELECT_INTERNAL + +#define and(a, b) InternalAnd(a, b) +#define or(a, b) InternalOr(a, b) +#define select(c, a, b) InternalSelect(c, a, b) + +#endif + // Compiler attribute fallback #ifndef UNROLL #define UNROLL diff --git a/Source/Shaders/GammaCorrectionCommon.hlsl b/Source/Shaders/GammaCorrectionCommon.hlsl index e410400ad..df188b347 100644 --- a/Source/Shaders/GammaCorrectionCommon.hlsl +++ b/Source/Shaders/GammaCorrectionCommon.hlsl @@ -52,7 +52,7 @@ float3 LinearToSrgb(float3 linearColor) float3 sRGBToLinear(float3 color) { color = max(6.10352e-5, color); - return color > 0.04045 ? pow(color * (1.0 / 1.055) + 0.0521327, 2.4) : color * (1.0 / 12.92); + return select(color > 0.04045, pow(color * (1.0 / 1.055) + 0.0521327, 2.4), color * (1.0 / 12.92)); } float3 LogToLinear(float3 logColor) diff --git a/Source/Shaders/Math.hlsl b/Source/Shaders/Math.hlsl index 4ba7bbbbb..b590b60b4 100644 --- a/Source/Shaders/Math.hlsl +++ b/Source/Shaders/Math.hlsl @@ -8,7 +8,7 @@ uint NextPow2(uint value) { - uint mask = (1 << firstbithigh(value)) - 1; + uint mask = (1u << firstbithigh(value)) - 1u; return (value + mask) & ~mask; } diff --git a/Source/Shaders/VolumetricFog.shader b/Source/Shaders/VolumetricFog.shader index d3885c20b..c55242996 100644 --- a/Source/Shaders/VolumetricFog.shader +++ b/Source/Shaders/VolumetricFog.shader @@ -341,7 +341,7 @@ void CS_LightScattering(uint3 GroupId : SV_GroupID, uint3 DispatchThreadId : SV_ if (all(gridCoordinate < GridSizeInt)) { - scatteringAndExtinction = isnan(scatteringAndExtinction) || isinf(scatteringAndExtinction) ? 0 : scatteringAndExtinction; + scatteringAndExtinction = select(or(isnan(scatteringAndExtinction), isinf(scatteringAndExtinction)), 0, scatteringAndExtinction); RWLightScattering[gridCoordinate] = max(scatteringAndExtinction, 0); } }