Various improvements

This commit is contained in:
schmelczerandras 2020-09-18 15:27:21 +02:00
parent ca2ac3fd2d
commit 8e73aee9ba
18 changed files with 259 additions and 115 deletions

View file

@ -23,6 +23,10 @@ export abstract class FrameBuffer {
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
}
public destroy(): void {
this.gl.deleteFramebuffer(this.frameBuffer);
}
public setSize(): boolean {
const realToCssPixels =
(this.enableHighDpiRendering ? window.devicePixelRatio : 1) * this.renderScale;

View file

@ -28,6 +28,10 @@ export class IntermediateFrameBuffer extends FrameBuffer {
this.setSize();
}
public destroy(): void {
this.gl.deleteTexture(this.frameTexture);
}
public get colorTexture(): WebGLTexture {
return this.frameTexture;
}

View file

@ -50,6 +50,7 @@ export abstract class ParallelCompiler {
return promise;
}
@Insights.measure('compile programs')
public static async compilePrograms(): Promise<void> {
ParallelCompiler.programs.forEach((p) => ParallelCompiler.gl.linkProgram(p.program));

View file

@ -20,10 +20,16 @@ export class FragmentShaderOnlyProgram extends Program {
this.gl.bindVertexArray(this.vao!);
}
public draw() {
public draw(uniforms: { [name: string]: any }) {
super.draw(uniforms);
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
}
public destroy(): void {
this.gl.deleteVertexArray(this.vao!);
super.destroy();
}
private prepareScreenQuad(attributeName: string) {
const positionAttributeLocation = this.gl.getAttribLocation(
this.program!,

View file

@ -2,7 +2,6 @@ import { vec2 } from 'gl-matrix';
export interface IProgram {
setDrawingRectangleUV(bottomLeft: vec2, size: vec2): void;
bindAndSetUniforms(values: { [name: string]: any }): void;
draw(): void;
delete(): void;
draw(values: { [name: string]: any }): void;
destroy(): void;
}

View file

@ -31,11 +31,6 @@ export default abstract class Program implements IProgram {
this.queryUniforms();
}
public bindAndSetUniforms(values: { [name: string]: any }) {
this.bind();
this.setUniforms({ modelTransform: this.modelTransform, ...values });
}
public setDrawingRectangleUV(bottomLeft: vec2, size: vec2) {
mat2d.invert(this.modelTransform, this.ndcToUv);
mat2d.translate(this.modelTransform, this.modelTransform, bottomLeft);
@ -56,12 +51,14 @@ export default abstract class Program implements IProgram {
});
}
public delete() {
this.gl.getAttachedShaders(this.program!)?.forEach((s) => this.gl.deleteShader(s));
public destroy() {
this.gl.deleteProgram(this.program!);
}
public abstract draw(): void;
public draw(values: { [name: string]: any }): void {
this.bind();
this.setUniforms({ modelTransform: this.modelTransform, ...values });
}
protected bind() {
this.gl.useProgram(this.program!);

View file

@ -38,7 +38,12 @@ export class UniformArrayAutoScalingProgram implements IProgram {
await Promise.all(promises);
}
public bindAndSetUniforms(uniforms: { [name: string]: any }): void {
public setDrawingRectangleUV(bottomLeft: vec2, size: vec2) {
this.drawingRectangleBottomLeft = bottomLeft;
this.drawingRectangleSize = size;
}
public draw(uniforms: { [name: string]: any }): void {
const values = this.descriptors!.map((d) =>
uniforms[d.uniformName] ? uniforms[d.uniformName].length : 0
);
@ -56,28 +61,16 @@ export class UniformArrayAutoScalingProgram implements IProgram {
});
}
this.current?.setDrawingRectangleUV(
this.current!.setDrawingRectangleUV(
this.drawingRectangleBottomLeft,
this.drawingRectangleSize
);
this.current?.bindAndSetUniforms(uniforms);
this.current!.draw(uniforms);
}
public setDrawingRectangleUV(bottomLeft: vec2, size: vec2) {
this.drawingRectangleBottomLeft = bottomLeft;
this.drawingRectangleSize = size;
}
public draw(): void {
if (!this.current) {
throw new Error('Method bindAndSetUniforms have not been called yet');
}
this.current.draw();
}
public delete(): void {
this.programs.forEach((p) => p.program.delete());
public destroy(): void {
this.programs.forEach((p) => p.program.destroy());
}
private async createProgram(