Nathaniel Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 1 | // 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 Nifong | de5df65 | 2019-03-28 13:08:51 -0400 | [diff] [blame] | 13 | // for the zoom to be able to access the pixels. Incurs performance penalty |
| 14 | preserveDrawingBuffer: 1, |
Nathaniel Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 15 | 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 Nifong | de5df65 | 2019-03-28 13:08:51 -0400 | [diff] [blame] | 34 | var context = GL.createContext(canvas, contextAttributes); |
Nathaniel Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 35 | 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 Nifong | de5df65 | 2019-03-28 13:08:51 -0400 | [diff] [blame] | 46 | // canvas - a canvas element to use for this surface. |
| 47 | DebuggerView.MakeWebGLCanvasSurface = function(canvas) { |
Nathaniel Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 48 | // 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 Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 55 | 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 Nifong | de5df65 | 2019-03-28 13:08:51 -0400 | [diff] [blame] | 64 | var surface = this.MakeOnScreenGLSurface(grcontext, canvas.width, canvas.height); |
Nathaniel Nifong | 46f5ee1 | 2019-03-15 10:58:31 -0400 | [diff] [blame] | 65 | 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"; |