Progmasoft Git

progmaweb

The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.

Clonegit clone https://git.progmasoft.com/progmaweb

fix(web): prevent fp16 shader overflow in Flow smoke effect on mobile GPUs (#119)

Motivation:
The WebGL smoke effect in Flow.tsx rendered on desktop Brave (where mediump float
is silently promoted to fp32) but failed on Android Chrome Beta on POCO X7 Pro
(ARM Mali-G720 GPU, where mediump float is true 16-bit IEEE 754 half-precision).
In fp16 (max 65504.0), dot(gl_FragCoord.xy, vec2(127.1, 311.7)) overflows to
+Infinity and sin(Infinity) * 43758.5453 produces NaN / collapsed zero noise.

Changes:
- Request precision highp float when GL_FRAGMENT_PRECISION_HIGH is defined,
  falling back to precision mediump float only when highp is unavailable.
- Replace the sin(...)*43758.5453 hash in Flow.tsx with a bounded, sin-free
  hash12 (fract(vec3(p.xyx) * 0.1031) + dot(..., 33.33)) that remains well
  within fp16 range and never produces NaN or precision collapse.
- Clamp gl_FragColor RGB output to [0, 1] and try webgl2 before webgl context
  creation with explicit WebGLContextAttributes typing.

Edge Cases & Compatibility:
- Works identically on Mali, Adreno, Apple GPU, and desktop WebGL2/WebGL1
  implementations while keeping visual smoke output smooth.

Verification:
- Ran pnpm test in apps/web: 9 test files, 59/59 passed.
- Ran pnpm exec tsc --noEmit in apps/web: 0 errors.
Commit
b9eaf7e3934195394c5a67bbc9fdde9a9a953122
Author
Leitwolf ·
Committer
GitHub
Parents
d1ac8d9588f5

Files · Download .tar.gz

Changes

 apps/web/components/site/Flow.tsx | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/apps/web/components/site/Flow.tsx b/apps/web/components/site/Flow.tsx
index b751464..fa08a4f 100644
--- a/apps/web/components/site/Flow.tsx
+++ b/apps/web/components/site/Flow.tsx
@@ -27,12 +27,16 @@ const vertex = `attribute vec2 p;void main(){gl_Position=vec4(p,0.,1.);}`;
  * Its densest parts carry the lighter colours as wisps, and the top of the
  * field leans towards the palest colour.
  */
-const fragment = `precision mediump float;
+const fragment = `#ifdef GL_FRAGMENT_PRECISION_HIGH
+precision highp float;
+#else
+precision mediump float;
+#endif
 uniform vec2 r;uniform float t;uniform float s;
 uniform vec3 c0;uniform vec3 c1;uniform vec3 c2;uniform vec3 c3;
-float h(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453);}
+float h(vec2 p){vec3 p3=fract(vec3(p.xyx)*.1031);p3+=dot(p3,p3.yzx+33.33);return fract((p3.x+p3.y)*p3.z);}
 float n(vec2 p){vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f);
-return mix(mix(h(i),h(i+vec2(1,0)),f.x),mix(h(i+vec2(0,1)),h(i+vec2(1,1)),f.x),f.y);}
+return mix(mix(h(i),h(i+vec2(1.,0.)),f.x),mix(h(i+vec2(0.,1.)),h(i+vec2(1.,1.)),f.x),f.y);}
 float b(vec2 p){float v=0.,a=.5;mat2 m=mat2(.8,.6,-.6,.8);
 for(int i=0;i<5;i++){v+=a*n(p);p=m*p*2.02+vec2(1.7,9.2);a*=.5;}return v;}
 void main(){vec2 u=gl_FragCoord.xy/r.y;vec2 p=u*1.4+s;float T=t*.35;
@@ -46,7 +50,7 @@ vec3 col=mix(c0,c1,smoothstep(.15,.65,v));
 col=mix(col,c2,wisp*.85);
 col=mix(col,c3,band*.5*smoothstep(.3,.8,v));
 col+=(h(gl_FragCoord.xy+fract(t))-.5)*.03;
-gl_FragColor=vec4(col,1.);}`;
+gl_FragColor=vec4(clamp(col,0.,1.),1.);}`;
 
 function rgb(hex: string): number[] {
   const value = Number.parseInt(hex.slice(1), 16);
@@ -86,10 +90,14 @@ export function Flow({
 
   useEffect(() => {
     const element = canvas.current;
-    const gl = element?.getContext("webgl", {
+    const contextOptions: WebGLContextAttributes = {
       antialias: false,
       premultipliedAlpha: false,
-    });
+    };
+    const gl: WebGL2RenderingContext | WebGLRenderingContext | null =
+      element?.getContext("webgl2", contextOptions) ??
+      element?.getContext("webgl", contextOptions) ??
+      null;
     if (!element || !gl) {
       return;
     }