|
1 | 1 | (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 | + } |
14 | 14 | }(this, function () {
|
15 | 15 |
|
16 | 16 | // turbo.js
|
|
19 | 19 |
|
20 | 20 | "use strict";
|
21 | 21 |
|
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')); |
23 | 38 |
|
24 | 39 | // turbo.js requires a 32bit float vec4 texture. Some systems only provide 8bit/float
|
25 | 40 | // textures. A workaround is being created, but turbo.js shouldn't be used on those
|
26 | 41 | // systems anyway.
|
27 | 42 | 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.'); |
29 | 44 |
|
30 | 45 | // GPU texture buffer from JS typed array
|
31 | 46 | function newBuffer(data, f, e) {
|
|
76 | 91 | // This should not fail.
|
77 | 92 | if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS))
|
78 | 93 | 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" + |
80 | 95 | "INFO: >REPORT< THIS. That's our fault!\n" + "\n" +
|
81 | 96 | "--- CODE DUMP ---\n" + vertexShaderCode + "\n\n" +
|
82 | 97 | "--- ERROR LOG ---\n" + gl.getShaderInfoLog(vertexShader)
|
|
130 | 145 | gl.linkProgram(program);
|
131 | 146 |
|
132 | 147 | 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.'); |
134 | 149 |
|
135 | 150 | var uTexture = gl.getUniformLocation(program, 'u_texture');
|
136 | 151 | var aPosition = gl.getAttribLocation(program, 'position');
|
|
149 | 164 |
|
150 | 165 | gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, nTexture, 0);
|
151 | 166 |
|
| 167 | + // Test for mobile bug MDN->WebGL_best_practices, bullet 7 |
152 | 168 | var frameBufferStatus = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE);
|
153 | 169 |
|
154 | 170 | 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); |
156 | 172 |
|
157 | 173 | gl.bindTexture(gl.TEXTURE_2D, texture);
|
158 | 174 | gl.activeTexture(gl.TEXTURE0);
|
|
174 | 190 | // A sane limit for most GPUs out there.
|
175 | 191 | // JS falls apart before GLSL limits could ever be reached.
|
176 | 192 | 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!"); |
178 | 194 |
|
179 | 195 | var ns = Math.pow(Math.pow(2, Math.ceil(Math.log(sz) / 1.386) - 1), 2);
|
180 | 196 | return {
|
|
0 commit comments