laurentlb / shader-minifier

Minify and obfuscate GLSL or HLSL code

Home Page:https://ctrl-alt-test.fr/minifier/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Obtaining infinity by dividing by 0 breaks minification

jroessel opened this issue · comments

In rewriter.fs, constant folding for numeric expressions is performed:

| FunCall(Op "/", [Float (i1,su); Float (i2,_)]) as e ->
    let div = Float (i1 / i2, su)
    if (Printer.exprToS e).Length <= (Printer.exprToS div).Length then e
    else div

However, for 1. / 0. this results in a division by 0 exception. Can be fixed with

@@ -158,7 +158,7 @@
     | FunCall(Op "-", [Float (i1,su); Float (i2,_)]) -> Float (i1 - i2, su)
     | FunCall(Op "+", [Float (i1,su); Float (i2,_)]) -> Float (i1 + i2, su)
     | FunCall(Op "*", [Float (i1,su); Float (i2,_)]) -> Float (i1 * i2, su)
-    | FunCall(Op "/", [Float (i1,su); Float (i2,_)]) as e ->
+    | FunCall(Op "/", [Float (i1,su); Float (i2,_)]) as e when i2 <> 0.0m ->
         let div = Float (i1 / i2, su)
         if (Printer.exprToS e).Length <= (Printer.exprToS div).Length then e
         else div

Although I wonder whether using decimal is actually desirable here, considering that shaders work with IEEE 754 floats. Or is that more to allow both integers and floats to pass through the same code in the minifier?

Beware that leaving 1./0. in the shader can result in a similar error in the driver shader compiler itself, at least with OpenGL.

Fixed last year, forgot to close.