Skip to content
This repository was archived by the owner on Jul 5, 2020. It is now read-only.

Commit 9917d08

Browse files
committed
1.0.2 - Implement Mozilla best practices
1 parent 00f879d commit 9917d08

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

turbo.js

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
(function (root, factory) {
2-
if (typeof define === 'function' && define.amd) {
3-
// AMD. Register as an anonymous module.
4-
define([], factory);
5-
} else if (typeof module === 'object' && module.exports) {
6-
// Node. Does not work with strict CommonJS, but
7-
// only CommonJS-like environments that support module.exports,
8-
// like Node.
9-
module.exports = factory();
10-
} else {
11-
// Browser globals (root is window)
12-
root.turbojs = factory();
13-
}
2+
if (typeof define === 'function' && define.amd) {
3+
// AMD. Register as an anonymous module.
4+
define([], factory);
5+
} else if (typeof module === 'object' && module.exports) {
6+
// Node. Does not work with strict CommonJS, but
7+
// only CommonJS-like environments that support module.exports,
8+
// like Node.
9+
module.exports = factory();
10+
} else {
11+
// Browser globals (root is window)
12+
root.turbojs = factory();
13+
}
1414
}(this, function () {
1515

1616
// turbo.js
@@ -19,13 +19,28 @@
1919

2020
"use strict";
2121

22-
var gl = document.createElement('canvas').getContext('experimental-webgl', {alpha : false, antialias : false});
22+
// Mozilla reference init implementation
23+
var initGLFromCanvas = function(canvas) {
24+
var gl = null;
25+
var attr = {alpha : false, antialias : false};
26+
27+
// Try to grab the standard context. If it fails, fallback to experimental.
28+
gl = canvas.getContext("webgl", attr) || canvas.getContext("experimental-webgl", attr);
29+
30+
// If we don't have a GL context, give up now
31+
if (!gl)
32+
throw new Error("turbojs: Unable to initialize WebGL. Your browser may not support it.");
33+
34+
return gl;
35+
}
36+
37+
var gl = initGLFromCanvas(document.createElement('canvas'));
2338

2439
// turbo.js requires a 32bit float vec4 texture. Some systems only provide 8bit/float
2540
// textures. A workaround is being created, but turbo.js shouldn't be used on those
2641
// systems anyway.
2742
if (!gl.getExtension('OES_texture_float'))
28-
throw new Error('turbojs requires OES_texture_float extension.');
43+
throw new Error('turbojs: Required texture format OES_texture_float not supported.');
2944

3045
// GPU texture buffer from JS typed array
3146
function newBuffer(data, f, e) {
@@ -76,7 +91,7 @@
7691
// This should not fail.
7792
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS))
7893
throw new Error(
79-
"\nERROR: Could not build internal vertex shader (fatal).\n" + "\n" +
94+
"\nturbojs: Could not build internal vertex shader (fatal).\n" + "\n" +
8095
"INFO: >REPORT< THIS. That's our fault!\n" + "\n" +
8196
"--- CODE DUMP ---\n" + vertexShaderCode + "\n\n" +
8297
"--- ERROR LOG ---\n" + gl.getShaderInfoLog(vertexShader)
@@ -130,7 +145,7 @@
130145
gl.linkProgram(program);
131146

132147
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
133-
throw new Error('ERROR: Could not initialize shaders (fatal).\n');
148+
throw new Error('turbojs: Failed to link GLSL program code.');
134149

135150
var uTexture = gl.getUniformLocation(program, 'u_texture');
136151
var aPosition = gl.getAttribLocation(program, 'position');
@@ -149,10 +164,11 @@
149164

150165
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, nTexture, 0);
151166

167+
// Test for mobile bug MDN->WebGL_best_practices, bullet 7
152168
var frameBufferStatus = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE);
153169

154170
if (!frameBufferStatus)
155-
throw new Error('ERROR: (fatal): ' + frameBufferStatus.message);
171+
throw new Error('turbojs: Error attaching float texture to framebuffer. Your device is probably incompatible. Error info: ' + frameBufferStatus.message);
156172

157173
gl.bindTexture(gl.TEXTURE_2D, texture);
158174
gl.activeTexture(gl.TEXTURE0);
@@ -174,7 +190,7 @@
174190
// A sane limit for most GPUs out there.
175191
// JS falls apart before GLSL limits could ever be reached.
176192
if (sz > 16777216)
177-
throw new Error("Whoops, the maximum array size is exceeded!");
193+
throw new Error("turbojs: Whoops, the maximum array size is exceeded!");
178194

179195
var ns = Math.pow(Math.pow(2, Math.ceil(Math.log(sz) / 1.386) - 1), 2);
180196
return {

0 commit comments

Comments
 (0)