diff --git a/frontend/src/app/main/ui/workspace/viewport_wasm.cljs b/frontend/src/app/main/ui/workspace/viewport_wasm.cljs index 67e4a1d9cc..5ffcf5118e 100644 --- a/frontend/src/app/main/ui/workspace/viewport_wasm.cljs +++ b/frontend/src/app/main/ui/workspace/viewport_wasm.cljs @@ -296,8 +296,9 @@ (->> wasm.api/module (p/fmap (fn [ready?] (when ready? - (reset! canvas-init? true) - (wasm.api/assign-canvas canvas))))) + (let [init? (wasm.api/init-canvas-context canvas)] + (reset! canvas-init? init?) + (when-not init? (js/alert "WebGL not supported"))))))) (fn [] (wasm.api/clear-canvas)))) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 38326e410e..8396d2bdf3 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -954,7 +954,7 @@ (h/call wasm/internal-module "_init_shapes_pool" total-shapes) (set-objects base-objects))) -(def ^:private canvas-options +(def ^:private context-options #js {:antialias false :depth true :stencil true @@ -971,23 +971,30 @@ (dbg/enabled? :wasm-viewbox) (bit-or 2r00000000000000000000000000000001))) -(defn assign-canvas +(defn set-canvas-size + [canvas] + (set! (.-width canvas) (* dpr (.-clientWidth ^js canvas))) + (set! (.-height canvas) (* dpr (.-clientHeight ^js canvas)))) + +(defn init-canvas-context [canvas] (let [gl (unchecked-get wasm/internal-module "GL") flags (debug-flags) - context (.getContext ^js canvas "webgl2" canvas-options) - ;; Register the context with emscripten - handle (.registerContext ^js gl context #js {"majorVersion" 2})] - (.makeContextCurrent ^js gl handle) + context-id (if (dbg/enabled? :wasm-gl-context-init-error) "fail" "webgl2") + context (.getContext ^js canvas context-id context-options) + context-init? (not (nil? context))] + (when-not (nil? context) + (let [handle (.registerContext ^js gl context #js {"majorVersion" 2})] + (.makeContextCurrent ^js gl handle) - ;; Force the WEBGL_debug_renderer_info extension as emscripten does not enable it - (.getExtension context "WEBGL_debug_renderer_info") + ;; Force the WEBGL_debug_renderer_info extension as emscripten does not enable it + (.getExtension context "WEBGL_debug_renderer_info") - ;; Initialize Wasm Render Engine - (h/call wasm/internal-module "_init" (/ (.-width ^js canvas) dpr) (/ (.-height ^js canvas) dpr)) - (h/call wasm/internal-module "_set_render_options" flags dpr)) - (set! (.-width canvas) (* dpr (.-clientWidth ^js canvas))) - (set! (.-height canvas) (* dpr (.-clientHeight ^js canvas)))) + ;; Initialize Wasm Render Engine + (h/call wasm/internal-module "_init" (/ (.-width ^js canvas) dpr) (/ (.-height ^js canvas) dpr)) + (h/call wasm/internal-module "_set_render_options" flags dpr))) + (set-canvas-size canvas) + context-init?)) (defn clear-canvas [] diff --git a/frontend/src/app/util/debug.cljs b/frontend/src/app/util/debug.cljs index 3c449fd39f..0f5869f4e2 100644 --- a/frontend/src/app/util/debug.cljs +++ b/frontend/src/app/util/debug.cljs @@ -96,9 +96,12 @@ ;; Show some information about the WebGL context. :gl-context - ;; Show viewbox + ;; Show viewbox. :wasm-viewbox + ;; Makes the GL context to fail on initialization. + :wasm-gl-context-init-error + ;; Event times :events-times})