Skip to content

Defer checking shader compile status #216

Open
@bchirlsbiodigital

Description

@bchirlsbiodigital

I wrote my own (much simpler) function to compile shader programs, in which I don't check compile status of individual shaders until linking fails. It then goes back and gets the error status and log of each shader. I had a big batch of 55 shader programs to compile and link that went from 980ms down to 870ms, about 11% faster.

Reference: Don't check shader compile status unless linking fails

...unless you know of some reason there would be a shader error with no linking error?

Here's the code you can use as a reference, but it doesn't include any of your fancy options:

function createProgramInfo(gl: WebGLRenderingContext, shaderSources: string[]) {
	const shaders = shaderSources.map((source, i) => {
		const shaderType = shaderTypes[i];
		const shader = gl.createShader(gl[shaderType]);
		gl.shaderSource(shader, source);
		gl.compileShader(shader);
		return shader;
	});

	const program = gl.createProgram();
	shaders.forEach(shader => {
		gl.attachShader(program, shader);
	});

	gl.linkProgram(program);

	const linked = gl.getProgramParameter(program, gl.LINK_STATUS);
	if (!linked) {
		let compileError = false;
		shaders.forEach((shader, i) => {
			const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
			if (!compiled) {
				compileError = true;

				const shaderError = gl.getShaderInfoLog(shader);
				const msg = addLineNumbersWithError(gl.getShaderSource(shader)) +
					'\nError compiling: ' + shaderTypes[i] + ':\n' + shaderError;
				console.error(msg);
			}
			gl.deleteShader(shader);
		});

		gl.deleteProgram(program);

		if (!compileError) {
			const linkError = gl.getProgramInfoLog(program);
			console.error(`Error in program linking: ${linkError}`);
		}

		return null;
	}

	return createProgramInfoFromProgram(gl, program);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions