blob: 5549c9c70e412361d6026ebcb9c889cfe94a114c [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,
Nathaniel Nifongde5df652019-03-28 13:08:51 -040013 // for the zoom to be able to access the pixels. Incurs performance penalty
14 preserveDrawingBuffer: 1,
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040015 preferLowPowerToHighPerformance: 0,
16 failIfMajorPerformanceCaveat: 0,
17 majorVersion: 1,
18 minorVersion: 0,
19 enableExtensionsByDefault: 1,
20 explicitSwapControl: 0,
21 renderViaOffscreenBackBuffer: 0,
22 };
23 if (!canvas) {
24 console.log('null canvas passed into makeWebGLContext');
25 return 0;
26 }
27 // This check is from the emscripten version
28 if (contextAttributes['explicitSwapControl']) {
29 console.log('explicitSwapControl is not supported');
30 return 0;
31 }
32 // GL is an enscripten provided helper
33 // See https://github.com/emscripten-core/emscripten/blob/incoming/src/library_webgl.js
Nathaniel Nifongde5df652019-03-28 13:08:51 -040034 var context = GL.createContext(canvas, contextAttributes);
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040035 if (!context) {
36 console.log('Could not get a WebGL context from the canvas element.');
37 }
38 console.log('Made Web Gl Canvas Surface');
39 return context
40 }
41
42 DebuggerView.GetWebGLContext = function(canvas, attrs) {
43 return makeWebGLContext(canvas, attrs);
44 };
45
Nathaniel Nifongde5df652019-03-28 13:08:51 -040046 // canvas - a canvas element to use for this surface.
47 DebuggerView.MakeWebGLCanvasSurface = function(canvas) {
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040048 // we are ok with all the defaults
49 var ctx = DebuggerView.GetWebGLContext(canvas);
50
51 if (!ctx || ctx < 0) {
52 throw 'failed to create webgl context: err ' + ctx;
53 }
54
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040055 var grcontext = this.MakeGrContext(ctx);
56 if (!grcontext) {
57 throw (
58 'failed to create grcontext. Open GL driver may not support all needed functions: err '
59 + grcontext);
60 }
61
62 // Maybe better to use clientWidth/height. See:
63 // https://webglfundamentals.org/webgl/lessons/webgl-anti-patterns.html
Nathaniel Nifongde5df652019-03-28 13:08:51 -040064 var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height);
Nathaniel Nifong46f5ee12019-03-15 10:58:31 -040065 if (!surface) {
66 // Don't fall back silently in the debugger, the user explicitly controls which backend he
67 // wants via the UI. Calling function may catch this and show the user an error.
68 throw ('Failed to create OpenGL surface. GPU Backend unavailable.');
69 }
70 return surface;
71 };
72 // Default to trying WebGL first.
73 DebuggerView.MakeCanvasSurface = DebuggerView.MakeWebGLCanvasSurface;
74}(Module)); // When this file is loaded in, the high level object is "Module";