Add more improvements

This commit is contained in:
schmelczerandras 2020-09-16 14:45:08 +02:00
parent e1c74a8054
commit bc16bdd62e
29 changed files with 288 additions and 160 deletions

View file

@ -3,7 +3,7 @@ import { checkShader } from './check-shader';
export const checkProgram = (gl: WebGL2RenderingContext, program: WebGLProgram) => {
const success = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!success) {
if (!success && !gl.isContextLost()) {
gl.getAttachedShaders(program)?.forEach((s) => {
checkShader(gl, s);
});

View file

@ -1,7 +1,7 @@
export const checkShader = (gl: WebGL2RenderingContext, shader: WebGLShader) => {
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!success) {
if (!success && !gl.isContextLost()) {
throw new Error(gl.getShaderInfoLog(shader)!);
}
};

View file

@ -7,11 +7,8 @@ export const createProgram = (
fragmentShaderSource: string,
substitutions: { [name: string]: string }
): WebGLProgram => {
const program = gl.createProgram();
if (!program) {
throw new Error('Could not create program');
}
// can only return null on lost context
const program = gl.createProgram()!;
// const extension = tryEnableExtension(gl, 'KHR_parallel_shader_compile');
@ -43,5 +40,8 @@ export const createProgram = (
checkProgram(gl, program);
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
return program;
};

View file

@ -9,11 +9,8 @@ export const createShader = (
return Number.isInteger(value) ? `${value}.0` : value;
});
const shader = gl.createShader(type);
if (!shader) {
throw new Error('Could not create shader');
}
// can only return null on lost context
const shader = gl.createShader(type)!;
gl.shaderSource(shader, source);
gl.compileShader(shader);

View file

@ -1,8 +1,8 @@
import { FrameBuffer } from './frame-buffer';
export class DefaultFrameBuffer extends FrameBuffer {
constructor(gl: WebGL2RenderingContext) {
super(gl);
constructor(gl: WebGL2RenderingContext, enableHighDpiRendering: boolean) {
super(gl, enableHighDpiRendering);
this.frameBuffer = null;
this.setSize();
}

View file

@ -1,16 +1,17 @@
import { vec2 } from 'gl-matrix';
import { settings } from '../../settings';
export abstract class FrameBuffer {
public renderScale = 1;
public enableHighDpiRendering = settings.enableHighDpiRendering;
protected size = vec2.create();
// null means the default framebuffer
protected frameBuffer: WebGLFramebuffer | null = null;
constructor(protected gl: WebGL2RenderingContext) {}
constructor(
protected gl: WebGL2RenderingContext,
private enableHighDpiRendering: boolean
) {}
public bindAndClear(colorInput?: WebGLTexture) {
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer);

View file

@ -5,8 +5,8 @@ export class IntermediateFrameBuffer extends FrameBuffer {
private frameTexture: WebGLTexture;
private floatLinearEnabled = false;
constructor(gl: WebGL2RenderingContext) {
super(gl);
constructor(gl: WebGL2RenderingContext, enableHighDpiRendering: boolean) {
super(gl, enableHighDpiRendering);
enableExtension(gl, 'EXT_color_buffer_float');
@ -17,18 +17,12 @@ export class IntermediateFrameBuffer extends FrameBuffer {
// it's okay
}
const texture = this.gl.createTexture();
if (!texture) {
throw new Error('Could not create texture');
}
this.frameTexture = texture;
// can only return null on lost context
this.frameTexture = this.gl.createTexture()!;
this.configureTexture();
const frameBuffer = this.gl.createFramebuffer();
if (!frameBuffer) {
throw new Error('Could not create frame buffer');
}
this.frameBuffer = frameBuffer;
// can only return null on lost context
this.frameBuffer = this.gl.createFramebuffer()!;
this.configureFrameBuffer();
this.setSize();

View file

@ -1,11 +1,13 @@
import { Insights } from '../../rendering/insights';
const extensions: Map<string, any> = new Map();
const printExtensions = () => {
const logExtensions = () => {
const values = {};
for (const [k, v] of extensions.entries()) {
values[k] = v !== null;
}
//InfoText.modifyRecord('extensions', values);
Insights.setValue('extensions', values);
};
export const tryEnableExtension = (
@ -23,7 +25,7 @@ export const tryEnableExtension = (
extensions.set(name, extension);
printExtensions();
logExtensions();
return extension;
};

View file

@ -1,3 +1,4 @@
import { Insights } from '../../rendering/insights';
import { enableExtension } from './enable-extension';
// https://www.khronos.org/registry/webgl/extensions/EXT_disjoint_timer_query_webgl2/
@ -37,7 +38,7 @@ export class WebGlStopwatch {
this.gl.QUERY_RESULT
);
//InfoText.modifyRecord('Draw time', `${this.resultsInMilliSeconds.toFixed(2)} ms`);
Insights.setValue('GPU draw time', `${this.resultsInMilliSeconds.toFixed(2)} ms`);
this.isReady = true;
}

View file

@ -35,11 +35,9 @@ export class FragmentShaderOnlyProgram extends Program {
new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]),
this.gl.STATIC_DRAW
);
const vao = this.gl.createVertexArray();
if (!vao) {
throw new Error('Could not create vertex array object');
}
this.vao = vao;
// can only return null on lost context
this.vao = this.gl.createVertexArray()!;
this.gl.bindVertexArray(this.vao);
this.gl.enableVertexAttribArray(positionAttributeLocation);

View file

@ -1,5 +1,4 @@
import { mat2d, vec2 } from 'gl-matrix';
import { settings } from '../../settings';
import { createProgram } from '../compiling/create-program';
import { loadUniform } from '../helper/load-uniform';
import { IProgram } from './i-program';
@ -20,7 +19,7 @@ export default abstract class Program implements IProgram {
[vertexShaderSource, fragmentShaderSource]: [string, string],
substitutions: { [name: string]: string }
) {
substitutions = { ...settings.shaderMacros, ...substitutions };
substitutions = { ...substitutions };
this.program = createProgram(
this.gl,

View file

@ -18,7 +18,8 @@ export class UniformArrayAutoScalingProgram implements IProgram {
constructor(
private gl: WebGL2RenderingContext,
shaderSources: [string, string],
private descriptors: Array<DrawableDescriptor>
private descriptors: Array<DrawableDescriptor>,
private substitutions: { [name: string]: any }
) {
for (const combination of getCombinations(
descriptors.map((o) => o.shaderCombinationSteps)
@ -75,6 +76,7 @@ export class UniformArrayAutoScalingProgram implements IProgram {
shaderSources: [string, string]
): FragmentShaderOnlyProgram {
const substitutions = {
...this.substitutions,
macroDefinitions: this.getMacroDefinitions(combination, descriptors),
declarations: this.getDeclarations(combination, descriptors),
functionCalls: this.getFunctionCalls(combination, descriptors),