From f5e0fbbc042ec9b6fb6d748435ce43bd2041ac44 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 30 Sep 2023 11:52:40 -0700 Subject: [PATCH] EngineGLCreateInfo: added WebGL context initialization attributes --- .../GraphicsEngine/interface/GraphicsTypes.h | 38 +++++++++++++++++++ .../src/GLContextEmscripten.cpp | 11 ++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Graphics/GraphicsEngine/interface/GraphicsTypes.h b/Graphics/GraphicsEngine/interface/GraphicsTypes.h index d8d6f4cbd..a2290874f 100644 --- a/Graphics/GraphicsEngine/interface/GraphicsTypes.h +++ b/Graphics/GraphicsEngine/interface/GraphicsTypes.h @@ -3424,6 +3424,39 @@ struct EngineCreateInfo }; typedef struct EngineCreateInfo EngineCreateInfo; +#if PLATFORM_EMSCRIPTEN +/// WebGL context attributes. +/// +/// \remarks This struct is used to set the members of the EmscriptenWebGLContextAttributes +/// structure that is passed to emscripten_webgl_create_context(). +struct WebGLContextAttribs +{ + /// If true, request alpha channel for the context and enable blending of + /// the canvas with the underlying web page contents. + /// + /// \remarks This corresponds to the alpha member of the EmscriptenWebGLContextAttributes struct. + Bool Alpha DEFAULT_INITIALIZER(true); + + /// If true, enable antialiasing with a browser-specified algorithm and quality level. + /// + /// \remarks This corresponds to the antialias member of the EmscriptenWebGLContextAttributes struct. + Bool Antialias DEFAULT_INITIALIZER(true); + + /// If true, treat the rendered canvas contents as alpha-premultiplied. + /// + /// \remarks This corresponds to the premultipliedAlpha member of the EmscriptenWebGLContextAttributes struct. + Bool PremultipliedAlpha DEFAULT_INITIALIZER(true); + + /// If true, preserve the contents of the drawing buffer between consecutive requestAnimationFrame() calls. + /// If false, clear the color, depth and stencil at the beginning of each requestAnimationFrame(). + /// Generally setting this to false gives better performance. + /// + /// \remarks This corresponds to the preserveDrawingBuffer member of the EmscriptenWebGLContextAttributes struct. + Bool PreserveDrawingBuffer DEFAULT_INITIALIZER(false); +}; +typedef struct WebGLContextAttribs WebGLContextAttribs; +#endif + /// Attributes of the OpenGL-based engine implementation struct EngineGLCreateInfo DILIGENT_DERIVE(EngineCreateInfo) @@ -3434,6 +3467,11 @@ struct EngineGLCreateInfo DILIGENT_DERIVE(EngineCreateInfo) /// Use IRenderDevice::GetDeviceInfo().NDC to get current NDC. Bool ZeroToOneNDZ DEFAULT_INITIALIZER(false); +#if PLATFORM_EMSCRIPTEN + /// WebGL context attributes. + WebGLContextAttribs WebGLAttribs; +#endif + #if DILIGENT_CPP_INTERFACE EngineGLCreateInfo() noexcept : EngineGLCreateInfo{EngineCreateInfo{}} {} diff --git a/Graphics/GraphicsEngineOpenGL/src/GLContextEmscripten.cpp b/Graphics/GraphicsEngineOpenGL/src/GLContextEmscripten.cpp index fa5edb610..b59c85f22 100644 --- a/Graphics/GraphicsEngineOpenGL/src/GLContextEmscripten.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/GLContextEmscripten.cpp @@ -40,10 +40,13 @@ GLContext::GLContext(const EngineGLCreateInfo& InitAttribs, RENDER_DEVICE_TYPE& EmscriptenWebGLContextAttributes ContextAttributes = {}; emscripten_webgl_init_context_attributes(&ContextAttributes); - // TODO: Initialization params - ContextAttributes.depth = true; - ContextAttributes.majorVersion = 3; - ContextAttributes.minorVersion = 0; + ContextAttributes.depth = true; + ContextAttributes.majorVersion = 3; + ContextAttributes.minorVersion = 0; + ContextAttributes.alpha = InitAttribs.WebGLAttribs.Alpha; + ContextAttributes.antialias = InitAttribs.WebGLAttribs.Antialias; + ContextAttributes.premultipliedAlpha = InitAttribs.WebGLAttribs.PremultipliedAlpha; + ContextAttributes.preserveDrawingBuffer = InitAttribs.WebGLAttribs.PreserveDrawingBuffer; m_GLContext = emscripten_webgl_create_context(InitAttribs.Window.pCanvasId, &ContextAttributes); if (m_GLContext == 0)