title: corrupt data loaded by GLES20.glTexImage2D using GL_LUMINANCE_ALPHA
body: <p>I am using opengl ES2.0 to render 16bit greyscale data on Android. (I'm handling 16bit to 8bit dynamic range scaling in the shader, the 16bit input is mandatory because it is incoming format of the data.) The resolution is 640x512.</p>
  
  <p>At the moment I have this working by pushing pixels 2 at a time into a 320x512 32bit RGBA texture. i.e:</p>
  
  <p><code>texture2D(thData, vTextureCoord)[0]</code>  is pixel <code>i</code> lower byte,</p>
  
  <p><code>texture2D(thData, vTextureCoord)[1]</code>  is pixel <code>i</code> upper byte,</p>
  
  <p><code>texture2D(thData, vTextureCoord)[2]</code>  is pixel <code>i+1</code> lower byte,</p>
  
  <p><code>texture2D(thData, vTextureCoord)[3]</code>  is pixel <code>i+1</code> upper byte.</p>
  
  <p>I am able to reconstruct the data from this but I have reconstruct the original resolution by rendering to a 640x512 buffer and using gl_FragCoord.x and a conditional statement to determine whether to draw the pixel from channels 0,1 or 2,3. The code looks like this:</p>
  
  <pre><code> private final String mFragmentShader =
      "precision highp float;\n" + 
      "varying vec2 vTextureCoord;\n" +
      "uniform sampler2D thData;\n" +
      "void main() {\n" +
  
      //This works out whether the pixel is in an odd or even column:
      "int odd = 0;\n" +
      "if (fract(gl_FragCoord.x/2.) &gt;= 0.5) odd = 2;\n"+
  
      //This chooses the channels based on the column number
      "float data =((texture2D(thData, vTextureCoord)[odd]) + (texture2D(thData, vTextureCoord)[odd+1]*256.))*256.;\n" +
  </code></pre>
  
  <p>This would be fine in principle but for some reason I get seem to be getting some columns swapped, my guess is that it could be rounding errors causing an incorrect result from the conditional statement.</p>
  
  <p>What I'd like to do is avoid the down sampling by using a 16bit full resolution texture in the first place, this seems really simple; I have just changed the glTexImage2D code from:</p>
  
  <pre><code>GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 320, 512, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, sBuffer);
  </code></pre>
  
  <p>To</p>
  
  <pre><code>GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE_ALPHA, 640, 512, 0, GLES20.GL_LUMINANCE_ALPHA, GLES20.GL_UNSIGNED_BYTE, sBuffer);
  </code></pre>
  
  <p>However, when I do this I appear to be getting strange data in the texture. <code>texture2D(thData, vTextureCoord)[0]</code> appears to be correct but <code>texture2D(thData, vTextureCoord)[1]</code> has the same data as element 0, the upper byte of the pixel is nowhere to be seen. </p>
  
  <p>Am I missing anything here?</p>
  
  <p>Or can anyone suggest an alternative approach?</p>