blob: 61b8787181d4800faa728a9826e6847a6ffc13e9 [file] [log] [blame]
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -04001// Adds compile-time JS functions to augment the DebuggerView interface.
2// Specifically, anything that should only be on the GPU version of DebuggerView.
3(function(DebuggerView){
4 function makeWebGLContext(canvas, attrs) {
5 // These defaults come from the emscripten _emscripten_webgl_create_context
6 // TODO(nifong): All these settings appear to be ignored. investigate.
7 var contextAttributes = {
8 alpha: 1,
9 depth: 1,
10 stencil: 0,
11 antialias: 1,
12 premultipliedAlpha: 1,
13 preserveDrawingBuffer: 0,
14 preferLowPowerToHighPerformance: 0,
15 failIfMajorPerformanceCaveat: 0,
16 majorVersion: 1,
17 minorVersion: 0,
18 enableExtensionsByDefault: 1,
19 explicitSwapControl: 0,
20 renderViaOffscreenBackBuffer: 0,
21 };
22 if (!canvas) {
23 console.log('null canvas passed into makeWebGLContext');
24 return 0;
25 }
26 // This check is from the emscripten version
27 if (contextAttributes['explicitSwapControl']) {
28 console.log('explicitSwapControl is not supported');
29 return 0;
30 }
31 // GL is an enscripten provided helper
32 // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
33 let context = GL.createContext(canvas, contextAttributes);
34 if (!context) {
35 console.log('Could not get a WebGL context from the canvas element.');
36 }
37 console.log('Made Web Gl Canvas Surface');
38 return context
39 }
40
41 DebuggerView.GetWebGLContext = function(canvas, attrs) {
42 return makeWebGLContext(canvas, attrs);
43 };
44
45 // arg can be of types:
46 // - String - in which case it is interpreted as an id of a
47 // canvas element.
48 // - HTMLCanvasElement - in which the provided canvas element will
49 // be used directly.
50 // Width and height can be provided to override those on the canvas
51 // element, or specify a height for when a context is provided.
52 DebuggerView.MakeWebGLCanvasSurface = function(arg, width, height) {
53 var canvas = arg;
54 if (canvas.tagName !== 'CANVAS') {
55 canvas = document.getElementById(arg);
56 if (!canvas) {
57 throw 'Canvas with id ' + arg + ' was not found';
58 }
59 }
60 // we are ok with all the defaults
61 var ctx = DebuggerView.GetWebGLContext(canvas);
62
63 if (!ctx || ctx < 0) {
64 throw 'failed to create webgl context: err ' + ctx;
65 }
66
67 if (!canvas && (!width || !height)) {
68 throw 'height and width must be provided with context';
69 }
70
71 var grcontext = this.MakeGrContext(ctx);
72 if (!grcontext) {
73 throw (
74 'failed to create grcontext. Open GL driver may not support all needed functions: err '
75 + grcontext);
76 }
77
78 // Maybe better to use clientWidth/height. See:
79 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
80 var surface = this.MakeOnScreenGLSurface(grcontext,
81 width || canvas.width,
82 height || canvas.height);
83 if (!surface) {
84 // Don't fall back silently in the debugger, the user explicitly controls which backend he
85 // wants via the UI. Calling function may catch this and show the user an error.
86 throw ('Failed to create OpenGL surface. GPU Backend unavailable.');
87 }
88 return surface;
89 };
90 // Default to trying WebGL first.
91 DebuggerView.MakeCanvasSurface = DebuggerView.MakeWebGLCanvasSurface;
92}(Module)); // When this file is loaded in, the high level object is "Module";