Skip to content

Shader Writing Tips

SleepProgger edited this page Apr 29, 2017 · 2 revisions

Writing Shaders

There are many different gpus out there and not all of them behave quite the same. This page will document behavior specific to certain devices.

Reusing Attribute Names:

Effects: Droid 4 (4.1.2) Reassigning to a name typically works on most hardware, however on at least the devices listed here behavior has been unwanted:

Droid 4 - Shader compilation appears to optimize out the attribute that is renamed. Resulting in a failure to glGetCurrentAttributeLocation it, and a 1281, 501 error during the binding of the Vertexformat stage of rendering.

Do not:

---VERTEX SHADER---
#ifdef GL_ES
    precision highp float;
#endif

/* Outputs to the fragment shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* vertex attributes */
attribute vec2     pos;
attribute vec2     uvs;

/* uniform variables */
uniform mat4       modelview_mat;
uniform mat4       projection_mat;
uniform vec4       color;
uniform float      opacity;

void main (void) {
  frag_color = color * vec4(1.0, 1., 1.0, opacity);
  tex_coord0 = uvs;
  vec4 pos = vec4(pos.xy, 0.0, 1.0);
  gl_Position = projection_mat * modelview_mat * pos;

}

Do:

---VERTEX SHADER---
#ifdef GL_ES
    precision highp float;
#endif

/* Outputs to the fragment shader */
varying vec4 frag_color;
varying vec2 tex_coord0;

/* vertex attributes */
attribute vec2     pos;
attribute vec2     uvs;

/* uniform variables */
uniform mat4       modelview_mat;
uniform mat4       projection_mat;
uniform vec4       color;
uniform float      opacity;

void main (void) {
  frag_color = color * vec4(1.0, 1., 1.0, opacity);
  tex_coord0 = uvs;
  vec4 new_pos = vec4(pos.xy, 0.0, 1.0);
  gl_Position = projection_mat * modelview_mat * new_pos;

}