Skip to content

Commit 23228eb

Browse files
committed
save
1 parent 90e3572 commit 23228eb

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

src/environment_util.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,31 @@ function hasExtension(gl: WebGLRenderingContext, extensionName: string) {
277277

278278
function createFloatTextureAndBindToFramebuffer(
279279
gl: WebGLRenderingContext, webGLVersion: number): boolean {
280-
const frameBuffer = gl.createFramebuffer();
281-
const texture = gl.createTexture();
280+
let frameBuffer = gl.getParameter(gl.FRAMEBUFFER_BINDING);
281+
let cleanupFrameBuffer = false;
282+
if (frameBuffer == null) {
283+
frameBuffer = gl.createFramebuffer();
284+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
285+
cleanupFrameBuffer = true;
286+
}
282287

288+
const texture = gl.createTexture();
283289
gl.bindTexture(gl.TEXTURE_2D, texture);
284-
285290
// tslint:disable-next-line:no-any
286291
const internalFormat = webGLVersion === 2 ? (gl as any).RGBA32F : gl.RGBA;
287292
gl.texImage2D(
288293
gl.TEXTURE_2D, 0, internalFormat, 1, 1, 0, gl.RGBA, gl.FLOAT, null);
289-
290-
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
291294
gl.framebufferTexture2D(
292295
gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
293296

294297
const isFrameBufferComplete =
295298
gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE;
296299

297300
gl.bindTexture(gl.TEXTURE_2D, null);
298-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
299301
gl.deleteTexture(texture);
300-
gl.deleteFramebuffer(frameBuffer);
302+
if (cleanupFrameBuffer) {
303+
gl.deleteFramebuffer(frameBuffer);
304+
}
301305

302306
return isFrameBufferComplete;
303307
}

src/kernels/webgl/gpgpu_context.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class GPGPUContext {
7575
this.vertexBuffer = gpgpu_util.createVertexBuffer(this.gl);
7676
this.indexBuffer = gpgpu_util.createIndexBuffer(this.gl);
7777
this.framebuffer = webgl_util.createFramebuffer(this.gl);
78+
webgl_util.bindFramebuffer(this.gl, this.framebuffer);
7879

7980
this.textureConfig =
8081
gpgpu_util.getTextureConfig(this.gl, this.textureHalfFloatExtension);
@@ -99,7 +100,7 @@ export class GPGPUContext {
99100
}
100101
const gl = this.gl;
101102
webgl_util.callAndCheck(gl, () => gl.finish());
102-
webgl_util.callAndCheck(gl, () => gl.bindFramebuffer(gl.FRAMEBUFFER, null));
103+
webgl_util.bindFramebuffer(gl, null);
103104
webgl_util.callAndCheck(gl, () => gl.deleteFramebuffer(this.framebuffer));
104105
webgl_util.callAndCheck(gl, () => gl.bindBuffer(gl.ARRAY_BUFFER, null));
105106
webgl_util.callAndCheck(
@@ -158,7 +159,6 @@ export class GPGPUContext {
158159
public deleteMatrixTexture(texture: WebGLTexture) {
159160
this.throwIfDisposed();
160161
if (this.outputTexture === texture) {
161-
webgl_util.unbindColorTextureFromFramebuffer(this.gl, this.framebuffer);
162162
this.outputTexture = null;
163163
}
164164
webgl_util.callAndCheck(this.gl, () => this.gl.deleteTexture(texture));
@@ -282,9 +282,6 @@ export class GPGPUContext {
282282
webgl_util.callAndCheck(gl, () => gl.attachShader(program, vertexShader));
283283
webgl_util.callAndCheck(gl, () => gl.attachShader(program, fragmentShader));
284284
webgl_util.linkProgram(gl, program);
285-
if (this.autoDebugValidate) {
286-
webgl_util.validateProgram(gl, program);
287-
}
288285
if (!this.vertexAttrsAreBound) {
289286
this.setProgram(program);
290287
this.vertexAttrsAreBound = gpgpu_util.bindVertexProgramAttributeStreams(
@@ -306,9 +303,6 @@ export class GPGPUContext {
306303
public setProgram(program: WebGLProgram|null) {
307304
this.throwIfDisposed();
308305
this.program = program;
309-
if ((this.program != null) && this.autoDebugValidate) {
310-
webgl_util.validateProgram(this.gl, this.program);
311-
}
312306
webgl_util.callAndCheck(this.gl, () => this.gl.useProgram(program));
313307
}
314308

@@ -373,19 +367,13 @@ export class GPGPUContext {
373367
throw new Error('setOutputPackedMatrixWriteRegion not implemented.');
374368
}
375369

376-
public debugValidate() {
377-
if (this.program != null) {
378-
webgl_util.validateProgram(this.gl, this.program);
379-
}
380-
webgl_util.validateFramebuffer(this.gl);
381-
}
382-
383370
public executeProgram() {
384371
this.throwIfDisposed();
385372
this.throwIfNoProgram();
386373
const gl = this.gl;
387374
if (this.autoDebugValidate) {
388-
this.debugValidate();
375+
webgl_util.validateProgram(this.gl, this.program);
376+
webgl_util.validateFramebuffer(this.gl);
389377
}
390378
webgl_util.callAndCheck(
391379
gl, () => gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0));
@@ -543,8 +531,7 @@ export class GPGPUContext {
543531

544532
private bindTextureToFrameBuffer(texture: WebGLTexture) {
545533
this.throwIfDisposed();
546-
webgl_util.bindColorTextureToFramebuffer(
547-
this.gl, texture, this.framebuffer);
534+
webgl_util.bindColorTextureToFramebuffer(this.gl, texture);
548535
if (this.autoDebugValidate) {
549536
webgl_util.validateFramebuffer(this.gl);
550537
}

src/kernels/webgl/webgl_util.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,16 @@ export function bindTextureToProgramUniformSampler(
252252
}
253253

254254
export function bindColorTextureToFramebuffer(
255-
gl: WebGLRenderingContext, texture: WebGLTexture,
256-
framebuffer: WebGLFramebuffer) {
257-
callAndCheck(gl, () => gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer));
255+
gl: WebGLRenderingContext, texture: WebGLTexture) {
258256
callAndCheck(
259257
gl,
260258
() => gl.framebufferTexture2D(
261259
gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0));
262260
}
263261

264-
export function unbindColorTextureFromFramebuffer(
262+
export function bindFramebuffer(
265263
gl: WebGLRenderingContext, framebuffer: WebGLFramebuffer) {
266264
callAndCheck(gl, () => gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer));
267-
callAndCheck(
268-
gl,
269-
() => gl.framebufferTexture2D(
270-
gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, null, 0));
271265
}
272266

273267
export function validateFramebuffer(gl: WebGLRenderingContext) {

0 commit comments

Comments
 (0)