function createProgram(gl, shaders, location) {
location = location || {}
function createShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, "#version 300 es\nprecision highp float;\n"+source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(shader));
return shader;
}
const program = gl.createProgram();
for(let [shaderType, shaderSource] of shaders) {
gl.attachShader(program, createShader(shaderType, shaderSource));
}
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error("cannot link program");
const numAttribs = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i=0; i < numAttribs; i++) {
const attribInfo = gl.getActiveAttrib(program, i);
const index = gl.getAttribLocation(program, attribInfo.name);
location[attribInfo.name] = index;
}
return program;
}