Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniform bool value in WebGL shader is always set to false when code is optimized #1970

Open
MartinSStewart opened this issue Aug 18, 2019 · 1 comment · May be fixed by #2120
Open

Uniform bool value in WebGL shader is always set to false when code is optimized #1970

MartinSStewart opened this issue Aug 18, 2019 · 1 comment · May be fixed by #2120

Comments

@MartinSStewart
Copy link

MartinSStewart commented Aug 18, 2019

A WebGL shader containing a uniform bool will always be assigned to false when the --optimize compiler flag is used.

In the following example (lifted from the elm-explorations/webgl's triangle.elm), I have a triangle that should be rendered with the color black if the uniform value noColor is set to true. If you run elm make src/Main.elm this will be the case. Running it with elm make src/Main.elm --optimize will incorrectly make it colorful.

SSCCE

module Main exposing (main)

import Html exposing (Html)
import Html.Attributes exposing (height, style, width)
import Math.Matrix4 as Mat4 exposing (Mat4)
import Math.Vector3 as Vec3 exposing (Vec3, vec3)
import WebGL exposing (Mesh, Shader)


main : Html msg
main =
    WebGL.toHtml
        [ width 400, height 400, style "display" "block" ]
        [ WebGL.entity
            vertexShader
            fragmentShader
            mesh
            { perspective = perspective
            , noColor = True
            }
        ]


perspective : Mat4
perspective =
    Mat4.mul
        (Mat4.makePerspective 45 1 0.01 100)
        (Mat4.makeLookAt (vec3 0 0 1) (vec3 0 0 0) (vec3 0 1 0))



-- Mesh


type alias Vertex =
    { position : Vec3
    , color : Vec3
    }


mesh : Mesh Vertex
mesh =
    WebGL.triangles
        [ ( Vertex (vec3 0 0 0) (vec3 1 0 0)
          , Vertex (vec3 1 1 0) (vec3 0 1 0)
          , Vertex (vec3 1 -1 0) (vec3 0 0 1)
          )
        ]


type alias Uniforms =
    { perspective : Mat4
    , noColor : Bool
    }


vertexShader : Shader Vertex Uniforms { vcolor : Vec3 }
vertexShader =
    [glsl|
        attribute vec3 position;
        attribute vec3 color;
        uniform mat4 perspective;
        uniform bool noColor;
        varying vec3 vcolor;
        void main () {
            gl_Position = perspective * vec4(position, 1.0);
            vcolor = vec3(noColor ? 0.0 : color.x, noColor ? 0.0 : color.y, noColor ? 0.0 : color.z);
        }
    |]


fragmentShader : Shader {} Uniforms { vcolor : Vec3 }
fragmentShader =
    [glsl|
        precision mediump float;
        varying vec3 vcolor;
        void main () {
            gl_FragColor = vec4(vcolor, 1.0);
        }
    |]
  • Elm: 0.19.0
  • Browser: Firefox and Chrome
  • Operating System: Windows 10 and Mac
@w0rm
Copy link

w0rm commented Aug 18, 2019

@MartinSStewart thanks for this SSCCE! WebGL supports the Bool uniform. The compiler doesn't extract it from a shader. Should be pretty easy to fix.

The reason why this breaks in the optimize mode, is that the bool property doesn't make it into the conversion map between the minified record field name and the full original name of the uniform.

@emiflake emiflake linked a pull request Jun 18, 2020 that will close this issue
mikaxyz pushed a commit to mikaxyz/elm-gltf that referenced this issue Jul 16, 2024
- bool is not supported but apparently works in non --optimize builds
  elm/compiler#1970
- Building examples with --optimize still does not work due to usage of
  samplerCube in the forked webgl library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants