This commit is contained in:
schmelczerandras 2020-11-23 13:08:46 +01:00
parent 25caf2fd5f
commit 9c64392e01
3 changed files with 16 additions and 13 deletions

View file

@ -14,7 +14,7 @@ export abstract class FrameBuffer {
constructor(protected readonly gl: UniversalRenderingContext) {} constructor(protected readonly gl: UniversalRenderingContext) {}
public bindAndClear(inputTextures: Array<Texture>) { public bindAndClear(inputTextures: Array<Texture> = []) {
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer); this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer);
inputTextures.forEach((t) => t.bind()); inputTextures.forEach((t) => t.bind());

View file

@ -17,13 +17,11 @@ type ShaderWithSource = WebGLShader & { source: string };
/** @internal */ /** @internal */
export class ParallelCompiler { export class ParallelCompiler {
private extension?: any; private parallelCompileExtension?: any;
private gl: UniversalRenderingContext;
private programs: Array<CompilingProgram> = []; private programs: Array<CompilingProgram> = [];
public constructor(gl: UniversalRenderingContext) { public constructor(private readonly gl: UniversalRenderingContext) {
this.gl = gl; this.parallelCompileExtension = tryEnableExtension(gl, 'KHR_parallel_shader_compile');
this.extension = tryEnableExtension(gl, 'KHR_parallel_shader_compile');
} }
public createProgram( public createProgram(
@ -105,8 +103,11 @@ export class ParallelCompiler {
this.programs.forEach((p) => { this.programs.forEach((p) => {
if ( if (
!this.extension || !this.parallelCompileExtension ||
this.gl.getProgramParameter(p.program, this.extension.COMPLETION_STATUS_KHR) this.gl.getProgramParameter(
p.program,
this.parallelCompileExtension.COMPLETION_STATUS_KHR
)
) { ) {
this.checkProgram(p); this.checkProgram(p);
done.push(p); done.push(p);

View file

@ -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 { ParallelCompiler } from '../../graphics-library/parallel-compiler';
import { FragmentShaderOnlyProgram } from '../../graphics-library/program/fragment-shader-only-program'; import { FragmentShaderOnlyProgram } from '../../graphics-library/program/fragment-shader-only-program';
import { getUniversalRenderingContext } from '../../graphics-library/universal-rendering-context'; 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 * @param ignoreWebGL2 Ignore WebGL2, even when it's available
*/ */
export const renderNoise = async ( export const renderNoise = async (
textureSize: vec2, textureSize: ReadonlyVec2,
scale: number, scale: number,
amplitude: number, amplitude: number,
ignoreWebGL2 = false ignoreWebGL2 = false
): Promise<HTMLCanvasElement> => { ): Promise<HTMLCanvasElement> => {
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
canvas.width = textureSize.x;
canvas.height = textureSize.y;
const gl = getUniversalRenderingContext(canvas, ignoreWebGL2); const gl = getUniversalRenderingContext(canvas, ignoreWebGL2);
const frameBuffer = new DefaultFrameBuffer(gl, textureSize);
const program = new FragmentShaderOnlyProgram(gl); const program = new FragmentShaderOnlyProgram(gl);
const compiler = new ParallelCompiler(gl); const compiler = new ParallelCompiler(gl);
@ -39,11 +39,13 @@ export const renderNoise = async (
await compiler.compilePrograms(); await compiler.compilePrograms();
await programPromise; await programPromise;
frameBuffer.bindAndClear();
program.draw({ program.draw({
scale, scale,
amplitude, amplitude,
}); });
frameBuffer.destroy();
program.destroy(); program.destroy();
return canvas; return canvas;