From 0eeb619b35b8394a6a4f765b6fb01bf2695c1a30 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 2 Oct 2026 20:09:44 +0200 Subject: [PATCH] CultMath HLSL: write the shader in the subset GLSL ES 3.00 shares, bit for bit Each edit is legal HLSL that computes the same bits, and closes one dialect gap the GLSL lowering would otherwise have to special-case: - snoise and snoise_grad: step(h, 0.0) -> step(h, float4(0.0, 0.0, 0.0, 0.0)); GLSL has no step(genType, float). - pcg: (state >> 28) + 4 -> + 4u; GLSL has no implicit int -> uint. - cellular and phacelle jitter, cellular id: (float)((uint)hash.x >> 8) before the float multiply; GLSL has no implicit uint -> float. - damp (float2/3/4): the scalar amount is splatted explicitly; GLSL has no lerp(vecN, vecN, float) promotion. - pcg3d(float3) is defined before pcg3d(float2), which calls it; GLSL needs a declaration before use. Both tracked copies; the C# mirror stays green, FXC cs_5_0 compiles both copies. Co-Authored-By: Claude Opus 5.5 --- .../cultmath/shaders/CultMath.Phacelle.hlsl | 6 ++--- packages/cultmath/shaders/CultMath.hlsl | 25 +++++++++++-------- .../Shaders/CultMath.Phacelle.hlsl | 6 ++--- .../Shaders/CultMath.hlsl | 25 +++++++++++-------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/cultmath/shaders/CultMath.Phacelle.hlsl b/packages/cultmath/shaders/CultMath.Phacelle.hlsl index 1db0a2fc..c95aafc6 100644 --- a/packages/cultmath/shaders/CultMath.Phacelle.hlsl +++ b/packages/cultmath/shaders/CultMath.Phacelle.hlsl @@ -49,9 +49,9 @@ CultPhasor cultmath_phacelle(float3 p, float3 side, float offset, float normaliz float3 gridStep = float3(dx, dy, dz); int3 hash = cultmath_pcg3d(int3(cell + gridStep)); float3 jitter = float3( - ((uint)hash.x >> 8) * (1.0 / 16777216.0) - 0.5, - ((uint)hash.y >> 8) * (1.0 / 16777216.0) - 0.5, - ((uint)hash.z >> 8) * (1.0 / 16777216.0) - 0.5); + (float)((uint)hash.x >> 8) * (1.0 / 16777216.0) - 0.5, + (float)((uint)hash.y >> 8) * (1.0 / 16777216.0) - 0.5, + (float)((uint)hash.z >> 8) * (1.0 / 16777216.0) - 0.5); float3 v = local - gridStep - jitter; float falloff = exp(-2.0 * dot(v, v)); float w = max(0.0, falloff - 0.01111); diff --git a/packages/cultmath/shaders/CultMath.hlsl b/packages/cultmath/shaders/CultMath.hlsl index da3362a6..cbb13e4e 100644 --- a/packages/cultmath/shaders/CultMath.hlsl +++ b/packages/cultmath/shaders/CultMath.hlsl @@ -111,7 +111,7 @@ float cultmath_snoise(float3 value) float4 b1 = float4(x.zw, y.zw); float4 s0 = floor(b0) * 2.0 + 1.0; float4 s1 = floor(b1) * 2.0 + 1.0; - float4 sh = -step(h, 0.0); + float4 sh = -step(h, float4(0.0, 0.0, 0.0, 0.0)); float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; @@ -197,7 +197,7 @@ float4 cultmath_snoise_grad(float3 value) float4 b1 = float4(x.zw, y.zw); float4 s0 = floor(b0) * 2.0 + 1.0; float4 s1 = floor(b1) * 2.0 + 1.0; - float4 sh = -step(h, 0.0); + float4 sh = -step(h, float4(0.0, 0.0, 0.0, 0.0)); float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; @@ -316,17 +316,20 @@ float cultmath_damp(float start, float end, float lambda_value, float dt) float2 cultmath_damp(float2 start, float2 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float2(amount, amount)); } float3 cultmath_damp(float3 start, float3 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float3(amount, amount, amount)); } float4 cultmath_damp(float4 start, float4 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float4(amount, amount, amount, amount)); } float cultmath_catmullrom(float p0, float p1, float p2, float p3, float t) @@ -393,7 +396,7 @@ float cultmath_hash(float3 value) { return cultmath_hash(dot(value, float3(127.1 uint cultmath_pcg(uint value) { uint state = value * 747796405u + 2891336453u; - uint word = ((state >> (int)((state >> 28) + 4)) ^ state) * 277803737u; + uint word = ((state >> (int)((state >> 28) + 4u)) ^ state) * 277803737u; return (word >> 22) ^ word; } @@ -406,8 +409,8 @@ int3 cultmath_pcg3d(int3 value) return int3((int)x, (int)y, (int)z); } -int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } int3 cultmath_pcg3d(float3 value) { return cultmath_pcg3d(int3((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z))); } +int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } int4 cultmath_pcg4d(int4 value) { @@ -468,9 +471,9 @@ CultCellular cultmath_cellular(float3 p) float3 neighbor = cell + float3(dx, dy, dz); int3 hash = cultmath_pcg3d(int3(neighbor)); float3 jitter = float3( - ((uint)hash.x >> 8) * (1.0 / 16777216.0), - ((uint)hash.y >> 8) * (1.0 / 16777216.0), - ((uint)hash.z >> 8) * (1.0 / 16777216.0)); + (float)((uint)hash.x >> 8) * (1.0 / 16777216.0), + (float)((uint)hash.y >> 8) * (1.0 / 16777216.0), + (float)((uint)hash.z >> 8) * (1.0 / 16777216.0)); float3 feature = neighbor + jitter; float d = cultmath_distance(p, feature); @@ -499,7 +502,7 @@ CultCellular cultmath_cellular(float3 p) CultCellular result; result.nearest = float4(grad1, f1); result.edge = float4(grad2 - grad1, f2 - f1); - result.id = ((uint)idHash.w >> 8) * (1.0 / 16777216.0); + result.id = (float)((uint)idHash.w >> 8) * (1.0 / 16777216.0); return result; } diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Phacelle.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Phacelle.hlsl index 1db0a2fc..c95aafc6 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Phacelle.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.Phacelle.hlsl @@ -49,9 +49,9 @@ CultPhasor cultmath_phacelle(float3 p, float3 side, float offset, float normaliz float3 gridStep = float3(dx, dy, dz); int3 hash = cultmath_pcg3d(int3(cell + gridStep)); float3 jitter = float3( - ((uint)hash.x >> 8) * (1.0 / 16777216.0) - 0.5, - ((uint)hash.y >> 8) * (1.0 / 16777216.0) - 0.5, - ((uint)hash.z >> 8) * (1.0 / 16777216.0) - 0.5); + (float)((uint)hash.x >> 8) * (1.0 / 16777216.0) - 0.5, + (float)((uint)hash.y >> 8) * (1.0 / 16777216.0) - 0.5, + (float)((uint)hash.z >> 8) * (1.0 / 16777216.0) - 0.5); float3 v = local - gridStep - jitter; float falloff = exp(-2.0 * dot(v, v)); float w = max(0.0, falloff - 0.01111); diff --git a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl index da3362a6..cbb13e4e 100644 --- a/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl +++ b/packages/cultmath/unity/org.gamecult.cultmath/Shaders/CultMath.hlsl @@ -111,7 +111,7 @@ float cultmath_snoise(float3 value) float4 b1 = float4(x.zw, y.zw); float4 s0 = floor(b0) * 2.0 + 1.0; float4 s1 = floor(b1) * 2.0 + 1.0; - float4 sh = -step(h, 0.0); + float4 sh = -step(h, float4(0.0, 0.0, 0.0, 0.0)); float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; @@ -197,7 +197,7 @@ float4 cultmath_snoise_grad(float3 value) float4 b1 = float4(x.zw, y.zw); float4 s0 = floor(b0) * 2.0 + 1.0; float4 s1 = floor(b1) * 2.0 + 1.0; - float4 sh = -step(h, 0.0); + float4 sh = -step(h, float4(0.0, 0.0, 0.0, 0.0)); float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; float4 a1 = b1.xzyw + s1.xzyw * sh.zzww; @@ -316,17 +316,20 @@ float cultmath_damp(float start, float end, float lambda_value, float dt) float2 cultmath_damp(float2 start, float2 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float2(amount, amount)); } float3 cultmath_damp(float3 start, float3 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float3(amount, amount, amount)); } float4 cultmath_damp(float4 start, float4 end, float lambda_value, float dt) { - return cultmath_lerp(start, end, 1.0 - exp(-lambda_value * dt)); + float amount = 1.0 - exp(-lambda_value * dt); + return cultmath_lerp(start, end, float4(amount, amount, amount, amount)); } float cultmath_catmullrom(float p0, float p1, float p2, float p3, float t) @@ -393,7 +396,7 @@ float cultmath_hash(float3 value) { return cultmath_hash(dot(value, float3(127.1 uint cultmath_pcg(uint value) { uint state = value * 747796405u + 2891336453u; - uint word = ((state >> (int)((state >> 28) + 4)) ^ state) * 277803737u; + uint word = ((state >> (int)((state >> 28) + 4u)) ^ state) * 277803737u; return (word >> 22) ^ word; } @@ -406,8 +409,8 @@ int3 cultmath_pcg3d(int3 value) return int3((int)x, (int)y, (int)z); } -int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } int3 cultmath_pcg3d(float3 value) { return cultmath_pcg3d(int3((int)asuint(value.x), (int)asuint(value.y), (int)asuint(value.z))); } +int3 cultmath_pcg3d(float2 value) { return cultmath_pcg3d(float3(value, 0.0)); } int4 cultmath_pcg4d(int4 value) { @@ -468,9 +471,9 @@ CultCellular cultmath_cellular(float3 p) float3 neighbor = cell + float3(dx, dy, dz); int3 hash = cultmath_pcg3d(int3(neighbor)); float3 jitter = float3( - ((uint)hash.x >> 8) * (1.0 / 16777216.0), - ((uint)hash.y >> 8) * (1.0 / 16777216.0), - ((uint)hash.z >> 8) * (1.0 / 16777216.0)); + (float)((uint)hash.x >> 8) * (1.0 / 16777216.0), + (float)((uint)hash.y >> 8) * (1.0 / 16777216.0), + (float)((uint)hash.z >> 8) * (1.0 / 16777216.0)); float3 feature = neighbor + jitter; float d = cultmath_distance(p, feature); @@ -499,7 +502,7 @@ CultCellular cultmath_cellular(float3 p) CultCellular result; result.nearest = float4(grad1, f1); result.edge = float4(grad2 - grad1, f2 - f1); - result.id = ((uint)idHash.w >> 8) * (1.0 / 16777216.0); + result.id = (float)((uint)idHash.w >> 8) * (1.0 / 16777216.0); return result; }