title: Sharing a glProgram among multiple OpenGL-ES contexts?
body: <p>I have an iPad app with multiple OpenGL-ES views that use OpenGL ES 2.0 and a custom fragment shader. Currently the shaders get compiled and linked for each individual OpenGL view. I'd like to compile and link the shaders once, and re-use them for each view. In theory this should just be a matter of calling </p>
  
  <p><code>gluseProgram(gProgramHandle);</code></p>
  
  <p>in my render() method, prior to rendering, and loading gProgramHandle once, right? But this doesn't work. When I switch to using a single gProgramHandle (set to -1 at initialization), only one OpenGL view works, and the others show up as a dark green rectangle. What am I doing wrong?</p>
  
  <pre><code>- (void)loadShaders
  {
     if (gProgramHandle == -1)
  {
      NSLog(@"Compiling shaders...");
  
      GLuint vertexShader = [self compileShader:@"AIMGsiVertexShader" withType:GL_VERTEX_SHADER];
      GLuint fragmentShader = [self compileShader:@"AIMGsiFragmentShader" withType:GL_FRAGMENT_SHADER];
  
      gProgramHandle = glCreateProgram();
      glAttachShader(gProgramHandle, vertexShader);
      glAttachShader(gProgramHandle, fragmentShader);
      glLinkProgram(gProgramHandle);
  
      GLint linkSuccess;
      glGetProgramiv(gProgramHandle, GL_LINK_STATUS, &amp;linkSuccess);
      if (linkSuccess == GL_FALSE)
      {
              // If there was an error when compiling the gsls shaders, report the compile error and quit.
          GLchar messages[256];
          glGetProgramInfoLog(gProgramHandle, sizeof(messages), 0, &amp;messages[0]);
          NSString *messageString = [NSString stringWithUTF8String:messages];
          NSLog(@"%@", messageString);
          exit(1);
      }
  
      NSLog(@"Done compiling and linking shaders.");
  }
  
      // We can efficiently switch between shaders by calling glUseProgram() to use the program with the shaders we want to use.
  glUseProgram(gProgramHandle);
  
      // Gradient map values are sent in this vector: numValuesInCache, factor, offset
  _gradientValsUniform = glGetUniformLocation(gProgramHandle, "GradientVals");
  _numColorsInGradient = glGetUniformLocation(gProgramHandle, "NumColorsInCache");
  _gradientColorsArray = glGetUniformLocation(gProgramHandle, "ColorsArray");
  
  
  _positionSlot = glGetAttribLocation(gProgramHandle, "Position");
  
  glEnableVertexAttribArray(_positionSlot);
  
  _projectionUniform = glGetUniformLocation(gProgramHandle, "Projection");
  _modelViewUniform = glGetUniformLocation(gProgramHandle, "Modelview");
  
  _texCoordSlot = glGetAttribLocation(gProgramHandle, "TexCoordIn");
  glEnableVertexAttribArray(_texCoordSlot);
  _textureUniform = glGetUniformLocation(gProgramHandle, "Texture");
  
  }
  </code></pre>