title: iOS - pass UIImage to shader as texture
body: <p>I am trying to pass <strong>UIImage</strong> to GLSL shader. The fragment shader is:</p>
  
  <pre><code>varying highp vec2 textureCoordinate;
  
  uniform sampler2D inputImageTexture;
  uniform sampler2D inputImageTexture2;
  
  void main()
  {
    highp vec4 color = texture2D(inputImageTexture, textureCoordinate); 
    highp vec4 color2 = texture2D(inputImageTexture2, textureCoordinate);
  
    gl_FragColor = color * color2;
  
  }  
  </code></pre>
  
  <p>What I want to do is send images from camera and do <strong>multiply blend</strong> with texture. When I just send data from camera, everything is fine. So problem should be with sending another texture to shader. I am doing it this way:</p>
  
  <pre><code>- (void)setTexture:(UIImage*)image forUniform:(NSString*)uniform {
  
    CGSize sizeOfImage = [image size];
    CGFloat scaleOfImage = [image scale];
    CGSize pixelSizeOfImage = CGSizeMake(scaleOfImage * sizeOfImage.width, scaleOfImage * sizeOfImage.height);
  
    //create context
    GLubyte * spriteData = (GLubyte *)malloc(pixelSizeOfImage.width * pixelSizeOfImage.height * 4 * sizeof(GLubyte));
    CGContextRef spriteContext = CGBitmapContextCreate(spriteData, pixelSizeOfImage.width, pixelSizeOfImage.height, 8, pixelSizeOfImage.width * 4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);
  
    //draw image into context
    CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, pixelSizeOfImage.width, pixelSizeOfImage.height), image.CGImage);
  
    //get uniform of texture
    GLuint uniformIndex = glGetUniformLocation(__programPointer, [uniform UTF8String]);
  
    //generate texture
    GLuint textureIndex;
    glGenTextures(1, &amp;textureIndex);
    glBindTexture(GL_TEXTURE_2D, textureIndex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  
    //create texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixelSizeOfImage.width, pixelSizeOfImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);
  
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, textureIndex);
    //"send" to shader
    glUniform1i(uniformIndex, 1); 
  
    free(spriteData);
    CGContextRelease(spriteContext);
  }
  </code></pre>
  
  <p>Uniform for texture is fine, <strong>glGetUniformLocation</strong> function do not returns -1. The texture is PNG file of resolution 2000x2000 pixels. </p>
  
  <p><strong>PROBLEM:</strong> When the texture is passed to shader, I have got "black screen". Maybe problem are parameters of the <strong>CGContext</strong> or parameters of the function <strong>glTexImage2D</strong></p>
  
  <p><strong>UPDATE:</strong> I have just found out I need to pass texture unit number to uniform every draw cycle and everything works just fine. It is really necessary to do it?</p>
  
  <p>Thank you</p>