title: SIGSEGV in OpenGL fragment shader on Android device
body: <p>I have a fragment shader in an android app using GLES 2.0 which works fine on the emulator but crashes with a "Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)" on a Nexus 4 when I try to link the shader program. </p>
  
  <p>I basically started from Google's tutorial (<a href="http://developer.android.com/training/graphics/opengl/draw.html" rel="nofollow">http://developer.android.com/training/graphics/opengl/draw.html</a>) and took it from there.</p>
  
  <p>This is the vertex shader:</p>
  
  <pre><code>uniform mat4 uMVPMatrix;
  varying vec2 vTexcoord;
  attribute vec4 vPosition;
  void main() {
    gl_Position = uMVPMatrix * vPosition;
    vTexcoord = vPosition.xy * vec2(0.5) + vec2(0.5);
  }
  </code></pre>
  
  <p>This is the fragment shader:</p>
  
  <pre><code>precision mediump float;
  uniform sampler2D uTexture1;
  uniform sampler2D uTexture2;
  uniform float pointX;
  uniform float pointY;
  uniform float opening;
  uniform float openingBufferZone;
  uniform vec4 aColor;
  varying vec2 vTexcoord;
  float sizeX = 2048.0;
  float sizeY = 512.0;
  void main() {
    float x = (vTexcoord.x * sizeX) - pointX;
    float y = (vTexcoord.y * sizeY) - pointY;
    float distance = x * x + y * y;
    if (distance &lt; opening) {
      gl_FragColor = texture2D(uTexture2, vTexcoord) * aColor;
    } else {
      if (distance &gt;= opening &amp;&amp; distance &lt; opening + openingBufferZone) {
        float inner = (distance-opening)/openingBufferZone;
        vec4 c1 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture1, vTexcoord) * aColor);
        vec4 c2 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture2, vTexcoord) * aColor);
        gl_FragColor = c1 + c2;
      } else {
        gl_FragColor = texture2D(uTexture1, vTexcoord) * aColor;
      }
    }
  }
  </code></pre>
  
  <p>This is how I load the shaders:</p>
  
  <pre><code>int vertexShader = GLActivity.loadShader(GLES20.GL_VERTEX_SHADER,
                                                     vertexShaderCode);
  int fragmentShader = GLActivity.loadShader(GLES20.GL_FRAGMENT_SHADER,
                                                       fragmentShaderCode);
  mProgram = GLES20.glCreateProgram();             
  GLES20.glAttachShader(mProgram, vertexShader);   
  GLES20.glAttachShader(mProgram, fragmentShader); 
  GLES20.glLinkProgram(mProgram);                  
  </code></pre>
  
  <p>The error occurs when trying to execute that last line <code>GLES20.glLinkProgram(mProgram);</code>.</p>
  
  <p>For some reason these 2 lines in the vertex shader seem to cause the trouble:</p>
  
  <pre><code>vec4 c1 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture1, vTexcoord) * aColor);
  vec4 c2 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture2, vTexcoord) * aColor);
  </code></pre>
  
  <p>If I simply change them to </p>
  
  <pre><code>vec4 c1 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture1, vTexcoord) * aColor);
  vec4 c2 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture1, vTexcoord) * aColor);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>vec4 c1 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture2, vTexcoord) * aColor);
  vec4 c2 = (vec4(inner,inner,inner,1.0) * texture2D(uTexture2, vTexcoord) * aColor);
  </code></pre>
  
  <p>and leave everything else as it is, the program runs fine (though obviously not as intended).</p>