WebGLRenderingContext.attachShader()

The WebGLRenderingContext.attachShader() method of the WebGL API attaches either a fragment or vertex WebGLShader to a WebGLProgram.

Syntax

void gl.attachShader(program, shader);

Parameters

program
A WebGLProgram.
shader
A fragment or vertex WebGLShader.

Examples

The following code attaches pre-existing shaders to a WebGLProgram.

var program = gl.createProgram();

// Attach pre-existing shaders
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);

gl.linkProgram(program);

if ( !gl.getProgramParameter( program, gl.LINK_STATUS) ) {
  var info = gl.getProgramInfoLog(program);
  throw "Could not compile WebGL program. \n\n" + info;
}

Specifications

Specification Status Comment
WebGL 1.0
The definition of 'attachShader' in that specification.
Recommendation Initial definition.
OpenGL ES 2.0
The definition of 'glAttachShader' in that specification.
Standard OpenGL man page.

Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari Servo
Basic Support91214.0111125.1No support
Feature Android Chrome for Android Edge Mobile Firefox for Android IE Mobile Opera Mobile Safari Mobile
Basic Support(Yes)25No support(Yes)111128.1

1. To access the WebGL context, use experimental-webgl rather than the standard webgl

See also

Document Tags and Contributors

Tags: 
 Contributors to this page: teoli, LiebeU, fscholz, gregtatum
 Last updated by: teoli,