Light | Dark



glShaderSource — replace the source code in a shader object

C Specification

void glShaderSource(GLuint shader,
 GLsizei count,
 const GLchar * const *string,
 const GLint *length);



Specifies the handle of the shader object whose source code is to be replaced.


Specifies the number of elements in the string and length arrays.


Specifies an array of pointers to strings containing the source code to be loaded into the shader.


Specifies an array of string lengths.


For implementations that support a shader compiler, glShaderSource sets the source code in shader to the source code in the array of strings specified by string. Any source code previously stored in the shader object is completely replaced. The number of strings in the array is specified by count. If length is NULL, each string is assumed to be null terminated. If length is a value other than NULL, it points to an array containing a string length for each of the corresponding elements of string. Each element in the length array may contain the length of the corresponding string (the null character is not counted as part of the string length) or a value less than 0 to indicate that the string is null terminated. The source code strings are not scanned or parsed at this time; they are simply copied into the specified shader object.


Shader compiler support is optional, and thus must be queried before use by calling glGet with argument GL_SHADER_COMPILER. glShaderSource, glCompileShader, glGetShaderPrecisionFormat, and glReleaseShaderCompiler will each generate GL_INVALID_OPERATION on implementations that do not support a shader compiler. Such implementations instead offer the glShaderBinary alternative for supplying a pre-compiled shader binary.

OpenGL copies the shader source code strings when glShaderSource is called, so an application may free its copy of the source code strings immediately after the function returns.


GL_INVALID_OPERATION is generated if a shader compiler is not supported.

GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL.

GL_INVALID_OPERATION is generated if shader is not a shader object.

GL_INVALID_VALUE is generated if count is less than 0.

Associated Gets

glGet with argument GL_SHADER_COMPILER

glGetShaderiv with arguments shader and GL_SHADER_SOURCE_LENGTH

glGetShaderSource with argument shader



Compile a program from a vertex shader and a fragment shader.
GLuint vshader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vshader, 1, &vertex_shader_source, NULL); // vertex_shader_source is a GLchar* containing glsl shader source code

GLint vertex_compiled;
glGetShaderiv(vshader, GL_COMPILE_STATUS, &vertex_compiled);
if (vertex_compiled != GL_TRUE)
    GLsizei log_length = 0;
    GLchar message[1024];
    glGetShaderInfoLog(vshader, 1024, &log_length, message);
    // Write the error to a log

GLuint fshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fshader, 1, &fragment_shader_source, NULL); // fragment_shader_source is a GLchar* containing glsl shader source code

GLint fragment_compiled;
glGetShaderiv(fshader, GL_COMPILE_STATUS, &fragment_compiled);
if (fragment_compiled != GL_TRUE)
    GLsizei log_length = 0;
    GLchar message[1024];
    glGetShaderInfoLog(fshader, 1024, &log_length, message);
    // Write the error to a log

GLuint program = glCreateProgram();

// This step is unnecessary if you use the location specifier in your shader
// e.g. layout (location = 0) in vec3 position;
glBindAttribLocation(program, 0, "position"); // The index passed into glBindAttribLocation is
glBindAttribLocation(program, 1, "texcoord"); // used by glEnableVertexAttribArray. "position"
glBindAttribLocation(program, 2, "normal");   // "texcoord" "normal" and "color" are the names of the
glBindAttribLocation(program, 3, "color");    // respective inputs in your fragment shader.

glAttachShader(program, vshader);
glAttachShader(program, fshader);

GLint program_linked;
glGetProgramiv(program, GL_LINK_STATUS, &program_linked);
if (program_linked != GL_TRUE)
    GLsizei log_length = 0;
    GLchar message[1024];
    glGetProgramInfoLog(program, 1024, &log_length, message);
    // Write the error to a log
Think you can improve this page? Edit this page on GitHub.