s2maps-gpu
Version:
S2 Maps GPU - An open source, high-performance, and GPU-accelerated map engine for rendering large-scale, interactive maps.
27 lines (26 loc) • 976 B
JavaScript
/**
* Load a WebGL shader
* @param gl - WebGL or WebGL2 rendering context
* @param shaderSource - shader source code
* @param shaderType - shader type
* @returns the compiled shader
*/
export default function loadShader(gl, shaderSource, shaderType) {
// Create the shader object
const shader = gl.createShader(shaderType);
if (shader === null)
throw Error(`Failed to create shader : ${shaderSource}`);
// Load the shader source
gl.shaderSource(shader, shaderSource);
// Compile the shader
gl.compileShader(shader);
// Check the compile status
const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (compiled === null) {
// Something went wrong during compilation get the error
const lastError = gl.getShaderInfoLog(shader);
gl.deleteShader(shader);
throw Error(`*** Error compiling shader '${JSON.stringify(shader)}': ${lastError ?? 'unknown'}`);
}
return shader;
}