From 9c64392e01020393d6c9bac3718b5d7e3230d962 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Mon, 23 Nov 2020 13:08:46 +0100 Subject: [PATCH] Refactor --- .../graphics-library/frame-buffer/frame-buffer.ts | 2 +- .../graphics-library/parallel-compiler.ts | 15 ++++++++------- src/graphics/rendering/renderer/noise-renderer.ts | 12 +++++++----- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/graphics/graphics-library/frame-buffer/frame-buffer.ts b/src/graphics/graphics-library/frame-buffer/frame-buffer.ts index 99cbf45..e13db93 100644 --- a/src/graphics/graphics-library/frame-buffer/frame-buffer.ts +++ b/src/graphics/graphics-library/frame-buffer/frame-buffer.ts @@ -14,7 +14,7 @@ export abstract class FrameBuffer { constructor(protected readonly gl: UniversalRenderingContext) {} - public bindAndClear(inputTextures: Array) { + public bindAndClear(inputTextures: Array = []) { this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer); inputTextures.forEach((t) => t.bind()); diff --git a/src/graphics/graphics-library/parallel-compiler.ts b/src/graphics/graphics-library/parallel-compiler.ts index 08998c4..268c039 100644 --- a/src/graphics/graphics-library/parallel-compiler.ts +++ b/src/graphics/graphics-library/parallel-compiler.ts @@ -17,13 +17,11 @@ type ShaderWithSource = WebGLShader & { source: string }; /** @internal */ export class ParallelCompiler { - private extension?: any; - private gl: UniversalRenderingContext; + private parallelCompileExtension?: any; private programs: Array = []; - public constructor(gl: UniversalRenderingContext) { - this.gl = gl; - this.extension = tryEnableExtension(gl, 'KHR_parallel_shader_compile'); + public constructor(private readonly gl: UniversalRenderingContext) { + this.parallelCompileExtension = tryEnableExtension(gl, 'KHR_parallel_shader_compile'); } public createProgram( @@ -105,8 +103,11 @@ export class ParallelCompiler { this.programs.forEach((p) => { if ( - !this.extension || - this.gl.getProgramParameter(p.program, this.extension.COMPLETION_STATUS_KHR) + !this.parallelCompileExtension || + this.gl.getProgramParameter( + p.program, + this.parallelCompileExtension.COMPLETION_STATUS_KHR + ) ) { this.checkProgram(p); done.push(p); diff --git a/src/graphics/rendering/renderer/noise-renderer.ts b/src/graphics/rendering/renderer/noise-renderer.ts index 7c004d1..ef4ac3a 100644 --- a/src/graphics/rendering/renderer/noise-renderer.ts +++ b/src/graphics/rendering/renderer/noise-renderer.ts @@ -1,4 +1,5 @@ -import { vec2 } from 'gl-matrix'; +import { ReadonlyVec2 } from 'gl-matrix'; +import { DefaultFrameBuffer } from '../../graphics-library/frame-buffer/default-frame-buffer'; import { ParallelCompiler } from '../../graphics-library/parallel-compiler'; import { FragmentShaderOnlyProgram } from '../../graphics-library/program/fragment-shader-only-program'; import { getUniversalRenderingContext } from '../../graphics-library/universal-rendering-context'; @@ -18,16 +19,15 @@ import randomVertex from '../shaders/random-vs.glsl'; * @param ignoreWebGL2 Ignore WebGL2, even when it's available */ export const renderNoise = async ( - textureSize: vec2, + textureSize: ReadonlyVec2, scale: number, amplitude: number, ignoreWebGL2 = false ): Promise => { const canvas = document.createElement('canvas'); - canvas.width = textureSize.x; - canvas.height = textureSize.y; - const gl = getUniversalRenderingContext(canvas, ignoreWebGL2); + + const frameBuffer = new DefaultFrameBuffer(gl, textureSize); const program = new FragmentShaderOnlyProgram(gl); const compiler = new ParallelCompiler(gl); @@ -39,11 +39,13 @@ export const renderNoise = async ( await compiler.compilePrograms(); await programPromise; + frameBuffer.bindAndClear(); program.draw({ scale, amplitude, }); + frameBuffer.destroy(); program.destroy(); return canvas;