kovasb / gamma

glsl shaders made simple
Eclipse Public License 1.0
306 stars 20 forks source link

Inferring floats for shader output #9

Open sgrove opened 9 years ago

sgrove commented 9 years ago

This shader:

(def program-source
  (p/program
   {:vertex-shader   {(g/gl-position) (-> u-p-matrix
                                          (g/* u-mv-matrix)
                                          (g/* (g/vec4 a-position 1)))
                      v-texture-coord a-texture-coord
                      v-light-weighting (g/if u-use-lighting
                                          (let [transformed-normal          (g/* u-n-matrix a-vertex-normal)
                                                directional-light-weighting (g/max (g/dot transformed-normal u-lighting-direction) 0.0)]
                                            (g/* directional-light-weighting
                                                 (g/+ u-ambient-color u-directional-color)))
                                          (g/vec3 1 1 1))}})}))

is output as:

uniform mat3 uNMatrix;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform vec3 uAmbientColor;
uniform vec3 uDirectionalColor;
uniform vec3 uLightingDirection;
attribute vec3 aVertexNormal;
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform bool uUseLighting;
varying mediump vec2 vTextureCoord;
varying mediump vec3 vLightWeighting;
void main(void){
vec3 v90;
if(uUseLighting){
  (v90 = (max(dot((uNMatrix * aVertexNormal), uLightingDirection), 0) * (uAmbientColor + uDirectionalColor)));}
  else {
    (v90 = vec3(1, 1, 1));}
(gl_Position = ((uPMatrix * uMVMatrix) * vec4(aVertexPosition, 1)));
(vTextureCoord = aTextureCoord);
(vLightWeighting = v90);
}

And will fail because the 0.0 is output as 0, and according to this max requires a float. There also doesn't seem to be any casting functions to manually work around this for the time being.