Skip to content

Commit b52a2d3

Browse files
committed
Removed context support for constructor (it will be replaced). Refactored canvas, and webgl context builder function to gpu_utils
1 parent c3ca03d commit b52a2d3

3 files changed

Lines changed: 208 additions & 28 deletions

File tree

src/backend/gpu_core.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,38 @@
55
/// *gpu.js* PUBLIC function namespace
66
///
77
/// I know @private makes more sense, but since the documentation engine state is undetirmined.
8-
/// See https://github.com/gpujs/gpu.js/issues/19 regarding documentation engine issue
8+
/// (See https://github.com/gpujs/gpu.js/issues/19 regarding documentation engine issue)
9+
/// File isolation is currently the best way to go
910
///
1011
var gpu_core = (function() {
1112

12-
function gpu_core(ctx) {
13-
var gl, canvas, canvasCpu;
13+
function gpu_core() {
14+
var gl, canvas;
1415

1516
canvas = undefined;
16-
gl = ctx;
1717
if (gl === undefined) {
18-
canvasCpu = document.createElement('canvas');
19-
canvas = document.createElement('canvas');
20-
canvas.width = 2;
21-
canvas.height = 2;
22-
var glOpt = {
23-
depth: false,
24-
antialias: false
25-
};
26-
27-
gl = canvas.getContext("experimental-webgl", glOpt) || canvas.getContext("webgl", glOpt);
18+
canvas = gpu_utils.init_canvas();
19+
gl = gpu_utils.init_webgl(canvas);
2820
}
2921

30-
gl.getExtension('OES_texture_float');
31-
gl.getExtension('OES_texture_float_linear');
32-
gl.getExtension('OES_element_index_uint');
33-
34-
this.gl = gl;
22+
this.webgl = gl;
3523
this.canvas = canvas;
36-
this.canvasCpu = canvasCpu;
3724
this.programCache = {};
3825
this.endianness = gpu_utils.system_endianness();
3926

4027
this.functionBuilder = new functionBuilder(this);
4128
this.functionBuilder.polyfillStandardFunctions();
4229
}
4330

44-
gpu_core.prototype.getGl = function() {
45-
return this.gl;
31+
gpu_core.prototype.getWebgl = function() {
32+
return this.webgl;
4633
};
4734

4835
gpu_core.prototype.getCanvas = function(mode) {
4936
if (mode == "cpu") {
50-
return this.canvasCpu;
37+
return null;
5138
}
52-
39+
5340
return this.canvas;
5441
};
5542

src/backend/gpu_utils.js

Lines changed: 196 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// Class: gpu_utils
33
///
44
/// Various utility functions / snippets of code that GPU.JS uses internally.\
5-
/// This covers various snippets of code that is NOT gpu.js specific
5+
/// This covers various snippets of code that is not entirely gpu.js specific (ie. may find uses elsewhere)
66
///
77
/// Note that all moethods in this class is "static" by nature `gpu_utils.functionName()`
88
///
@@ -11,7 +11,13 @@ var gpu_utils = (function() {
1111
function gpu_utils() {
1212
throw new Error("This is a utility class - do not construct it");
1313
}
14-
14+
15+
//-----------------------------------------------------------------------------
16+
//
17+
// System values support (currently only endianness)
18+
//
19+
//-----------------------------------------------------------------------------
20+
1521
// system_endianness closure based memoizer
1622
var system_endianness_memoizer = null;
1723

@@ -21,7 +27,7 @@ var gpu_utils = (function() {
2127
/// Gets the system endianness, and cache it
2228
///
2329
/// Returns:
24-
/// {String} "LE" or "BE" depending on system settings
30+
/// {String} "LE" or "BE" depending on system settings
2531
///
2632
/// Credit: https://gist.github.com/TooTallNate/4750953
2733
function system_endianness() {
@@ -38,6 +44,12 @@ var gpu_utils = (function() {
3844
throw new Error('unknown endianness');
3945
}
4046
gpu_utils.system_endianness = system_endianness;
47+
48+
//-----------------------------------------------------------------------------
49+
//
50+
// Function and function string validations
51+
//
52+
//-----------------------------------------------------------------------------
4153

4254
///
4355
/// Function: isFunction
@@ -120,6 +132,187 @@ var gpu_utils = (function() {
120132
return result;
121133
}
122134
gpu_utils.getParamNames_fromString = getParamNames_fromString;
135+
136+
//-----------------------------------------------------------------------------
137+
//
138+
// Canvas validation and support
139+
//
140+
//-----------------------------------------------------------------------------
123141

142+
///
143+
/// Function: isCanvas
144+
///
145+
/// Return TRUE, on a valid DOM canvas object
146+
///
147+
/// Note: This does just a VERY simply sanity check. And may give false positives.
148+
///
149+
/// Parameters:
150+
/// canvasObj - {Canvas DOM object} Object to validate
151+
///
152+
/// Returns:
153+
/// {Boolean} TRUE if the object is a DOM canvas
154+
///
155+
function isCanvas( canvasObj ) {
156+
return (
157+
canvasObj != null &&
158+
canvasObj.nodeName &&
159+
canvasObj.getContext &&
160+
canvasObj.nodeName.toUpperCase() === "CANVAS"
161+
);
162+
}
163+
gpu_utils.isCanvas = isCanvas;
164+
165+
// browserSupport_canvas closure based memoizer
166+
var browserSupport_canvas_memoizer = null;
167+
///
168+
/// Function: browserSupport_canvas
169+
///
170+
/// Return TRUE, if browser supports canvas
171+
///
172+
/// Returns:
173+
/// {Boolean} TRUE if browser supports canvas
174+
///
175+
function browserSupport_canvas() {
176+
if( browserSupport_canvas_memoizer !== null ) {
177+
return browserSupport_canvas_memoizer;
178+
}
179+
return browserSupport_canvas_memoizer = isCanvas(document.createElement('canvas'));
180+
}
181+
gpu_utils.browserSupport_canvas = browserSupport_canvas;
182+
183+
///
184+
/// Function: init_canvas
185+
///
186+
/// Initiate and returns a canvas, for usage in init_webgl.
187+
/// Returns only if canvas is supported by browser.
188+
///
189+
/// Returns:
190+
/// {Canvas DOM object} Canvas dom object if supported by browser, else null
191+
///
192+
function init_canvas() {
193+
// Fail fast if browser previously detected no support
194+
if( browserSupport_canvas_memoizer === false ) {
195+
return null;
196+
}
197+
198+
// Create a new canvas DOM
199+
var canvas = document.createElement('canvas');
200+
201+
// First time setup, does the browser support check memoizer
202+
if( browserSupport_canvas_memoizer === null ) {
203+
browserSupport_canvas_memoizer = isCanvas(canvas);
204+
if( browserSupport_canvas_memoizer === false ) {
205+
return null;
206+
}
207+
}
208+
209+
// Default width and height, to fix webgl issue in safari
210+
canvas.width = 2;
211+
canvas.height = 2;
212+
213+
// Returns the canvas
214+
return canvas;
215+
}
216+
gpu_utils.init_canvas = init_canvas;
217+
218+
//-----------------------------------------------------------------------------
219+
//
220+
// Webgl validation and support
221+
//
222+
//-----------------------------------------------------------------------------
223+
224+
///
225+
/// Function: isWebgl
226+
///
227+
/// Return TRUE, on a valid webgl context object
228+
///
229+
/// Note: This does just a VERY simply sanity check. And may give false positives.
230+
///
231+
/// Parameters:
232+
/// webglObj - {webgl context} Object to validate
233+
///
234+
/// Returns:
235+
/// {Boolean} TRUE if the object is a webgl context object
236+
///
237+
function isWebgl( webglObj ) {
238+
return (
239+
webglObj != null &&
240+
webglObj.getExtension
241+
);
242+
}
243+
gpu_utils.isWebgl = isWebgl;
244+
245+
// browserSupport_canvas closure based memoizer
246+
var browserSupport_webgl_memoizer = null;
247+
///
248+
/// Function: browserSupport_webgl
249+
///
250+
/// Return TRUE, if browser supports webgl
251+
///
252+
/// Returns:
253+
/// {Boolean} TRUE if browser supports webgl
254+
///
255+
function browserSupport_webgl() {
256+
if( browserSupport_webgl_memoizer !== null ) {
257+
return browserSupport_webgl_memoizer;
258+
}
259+
return browserSupport_webgl_memoizer = isWebgl(init_webgl(init_canvas()));
260+
}
261+
gpu_utils.browserSupport_webgl = browserSupport_webgl;
262+
263+
// Default webgl options to use
264+
var init_webgl_defaultOptions = {
265+
depth: false,
266+
antialias: false
267+
}
268+
269+
///
270+
/// Function: init_webgl
271+
///
272+
/// Initiate and returns a webgl, from a canvas object
273+
/// Returns only if webgl is supported by browser.
274+
///
275+
/// Parameters:
276+
/// canvasObj - {Canvas DOM object} Object to validate
277+
///
278+
/// Returns:
279+
/// {Canvas DOM object} Canvas dom object if supported by browser, else null
280+
///
281+
function init_webgl(canvasObj) {
282+
283+
// Fail fast for invalid canvas object
284+
if( !isCanvas(canvasObj) ) {
285+
throw new Error("Invalid canvas object - "+canvasObj);
286+
}
287+
288+
// Fail fast if browser previously detected no support
289+
if( browserSupport_canvas_memoizer === false || browserSupport_webgl_memoizer === false ) {
290+
return null;
291+
}
292+
293+
// Create a new canvas DOM
294+
var webgl = (
295+
canvasObj.getContext("experimental-webgl", init_webgl_defaultOptions) ||
296+
canvasObj.getContext("webgl", init_webgl_defaultOptions)
297+
);
298+
299+
// First time setup, does the browser support check memoizer
300+
if( browserSupport_webgl_memoizer === null ) {
301+
browserSupport_webgl_memoizer = isWebgl(webgl);
302+
if( browserSupport_webgl_memoizer === false ) {
303+
return null;
304+
}
305+
}
306+
307+
// Get the extension that is needed
308+
webgl.getExtension('OES_texture_float');
309+
webgl.getExtension('OES_texture_float_linear');
310+
webgl.getExtension('OES_element_index_uint');
311+
312+
// Returns the canvas
313+
return webgl;
314+
}
315+
gpu_utils.init_webgl = init_webgl;
316+
124317
return gpu_utils;
125318
})();

src/backend/mode_gpu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144

145145
GPU.prototype._mode_gpu = function(kernel, opt) {
146146
var gpu = this;
147-
var gl = this.gl;
147+
var gl = this.webgl;
148148
var canvas = this.canvas;
149149

150150
var builder = this.functionBuilder;

0 commit comments

Comments
 (0)