web api instance method
WebGLRenderingContext: attachShader() method
Available in workers
The WebGLRenderingContext.attachShader() method of the WebGL API attaches either a fragment or
vertex WebGLShader to a WebGLProgram.
Syntax
attachShader(program, shader)
Parameters
program- : A
WebGLProgram.
- : A
shader- : A fragment or vertex
WebGLShader.
- : A fragment or vertex
Return value
None (undefined).
Examples
The following code attaches pre-existing shaders to a WebGLProgram.
const 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)) {
const info = gl.getProgramInfoLog(program);
throw new Error(`Could not compile WebGL program. \n\n${info}`);
}
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.
See also
WebGLProgramWebGLShadercompileShader()createProgram()createShader()deleteProgram()deleteShader()detachShader()getAttachedShaders()getProgramParameter()getProgramInfoLog()getShaderParameter()getShaderPrecisionFormat()getShaderInfoLog()getShaderSource()isProgram()isShader()linkProgram()shaderSource()useProgram()validateProgram()