Skip to content

Commit ddccbca

Browse files
committed
Add framebuffer completess check for better compatibility
merged from evanw#2
1 parent 21c61cf commit ddccbca

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

webgl-path-tracing.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,39 @@ function PathTracer() {
692692
// create framebuffer
693693
this.framebuffer = gl.createFramebuffer();
694694

695-
// create textures
696695
this.type = gl.getExtension('OES_texture_float') ? gl.FLOAT : gl.UNSIGNED_BYTE;
696+
var format = gl.RGB;
697+
698+
// Rendering to float texture formats is not necessarily supported. Check support.
699+
var testTexture = gl.createTexture();
700+
gl.bindTexture(gl.TEXTURE_2D, testTexture);
701+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
702+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
703+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 512, 512, 0, gl.RGB, this.type, null);
704+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
705+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, testTexture, 0);
706+
var rgbFloatSupported = (this.type == gl.FLOAT && gl.checkFramebufferStatus == gl.FRAMEBUFFER_COMPLETE);
707+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 512, 512, 0, gl.RGBA, this.type, null);
708+
var rgbaFloatSupported = (this.type == gl.FLOAT && gl.checkFramebufferStatus == gl.FRAMEBUFFER_COMPLETE);
709+
710+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, null, 0);
711+
gl.deleteTexture(testTexture);
712+
713+
if (!rgbFloatSupported && !rgbaFloatSupported) {
714+
this.type = gl.UNSIGNED_BYTE;
715+
} else if (!rgbFloatSupported) {
716+
// RGBA float is preferred to RGB fixed-point.
717+
format = gl.RGBA;
718+
}
719+
720+
// create textures
697721
this.textures = [];
698722
for(var i = 0; i < 2; i++) {
699723
this.textures.push(gl.createTexture());
700724
gl.bindTexture(gl.TEXTURE_2D, this.textures[i]);
701725
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
702726
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
703-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, renderSize, renderSize, 0, gl.RGB, this.type, null);
727+
gl.texImage2D(gl.TEXTURE_2D, 0, format, renderSize, renderSize, 0, format, this.type, null);
704728
}
705729
gl.bindTexture(gl.TEXTURE_2D, null);
706730

0 commit comments

Comments
 (0)