wasm: make opengles3 (webgl2) default surface format

All major browsers support webgl2 out of the box.
[ChangeLog][WebAssmebly] Default OpenGL ES version raised to 3.0

Fixes: QTBUG-110686
Change-Id: I875b55c2e0f6e955249f121d92214cf10ed416d7
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
(cherry picked from commit ee25bde3edf08e27efbb9619683ffb5517115817)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Lorn Potter 2023-02-13 17:40:41 +10:00 committed by Qt Cherry-pick Bot
parent f67888e8db
commit 93f8c8f3e6
2 changed files with 35 additions and 18 deletions

View File

@ -21,16 +21,16 @@ EMSCRIPTEN_BINDINGS(qwasmopenglcontext)
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QWasmOpenGLContext::QWasmOpenGLContext(QOpenGLContext *context) QWasmOpenGLContext::QWasmOpenGLContext(QOpenGLContext *context)
: m_requestedFormat(context->format()), m_qGlContext(context) : m_actualFormat(context->format()), m_qGlContext(context)
{ {
m_requestedFormat.setRenderableType(QSurfaceFormat::OpenGLES); m_actualFormat.setRenderableType(QSurfaceFormat::OpenGLES);
// if we set one, we need to set the other as well since in webgl, these are tied together // if we set one, we need to set the other as well since in webgl, these are tied together
if (m_requestedFormat.depthBufferSize() < 0 && m_requestedFormat.stencilBufferSize() > 0) if (m_actualFormat.depthBufferSize() < 0 && m_actualFormat.stencilBufferSize() > 0)
m_requestedFormat.setDepthBufferSize(16); m_actualFormat.setDepthBufferSize(16);
if (m_requestedFormat.stencilBufferSize() < 0 && m_requestedFormat.depthBufferSize() > 0) if (m_actualFormat.stencilBufferSize() < 0 && m_actualFormat.depthBufferSize() > 0)
m_requestedFormat.setStencilBufferSize(8); m_actualFormat.setStencilBufferSize(8);
} }
QWasmOpenGLContext::~QWasmOpenGLContext() QWasmOpenGLContext::~QWasmOpenGLContext()
@ -77,8 +77,7 @@ QWasmOpenGLContext::obtainEmscriptenContext(QPlatformSurface *surface)
QOpenGLContextData{ .surface = surface, QOpenGLContextData{ .surface = surface,
.handle = createEmscriptenContext( .handle = createEmscriptenContext(
static_cast<QWasmOffscreenSurface *>(surface)->id(), static_cast<QWasmOffscreenSurface *>(surface)->id(),
m_requestedFormat) }; m_actualFormat) };
return m_ownedWebGLContext.handle;
} }
} else { } else {
destroyWebGLContext(m_ownedWebGLContext.handle); destroyWebGLContext(m_ownedWebGLContext.handle);
@ -87,11 +86,23 @@ QWasmOpenGLContext::obtainEmscriptenContext(QPlatformSurface *surface)
m_ownedWebGLContext = QOpenGLContextData{ m_ownedWebGLContext = QOpenGLContextData{
.surface = surface, .surface = surface,
.handle = createEmscriptenContext(static_cast<QWasmWindow *>(surface)->canvasSelector(), .handle = createEmscriptenContext(static_cast<QWasmWindow *>(surface)->canvasSelector(),
m_requestedFormat) m_actualFormat)
}; };
return m_ownedWebGLContext.handle;
} }
EmscriptenWebGLContextAttributes actualAttributes;
EMSCRIPTEN_RESULT attributesResult = emscripten_webgl_get_context_attributes(m_ownedWebGLContext.handle, &actualAttributes);
if (attributesResult == EMSCRIPTEN_RESULT_SUCCESS) {
if (actualAttributes.majorVersion == 1) {
m_actualFormat.setMajorVersion(2);
} else if (actualAttributes.majorVersion == 2) {
m_actualFormat.setMajorVersion(3);
}
m_actualFormat.setMinorVersion(0);
}
return m_ownedWebGLContext.handle;
} }
void QWasmOpenGLContext::destroyWebGLContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE contextHandle) void QWasmOpenGLContext::destroyWebGLContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE contextHandle)
@ -116,9 +127,8 @@ QWasmOpenGLContext::createEmscriptenContext(const std::string &canvasSelector,
attributes.failIfMajorPerformanceCaveat = false; attributes.failIfMajorPerformanceCaveat = false;
attributes.antialias = true; attributes.antialias = true;
attributes.enableExtensionsByDefault = true; attributes.enableExtensionsByDefault = true;
attributes.majorVersion = format.majorVersion() - 1; attributes.majorVersion = 2; // try highest supported version ES3.0 / WebGL 2.0
attributes.minorVersion = format.minorVersion(); attributes.minorVersion = 0; // emscripten only supports minor version 0
// WebGL doesn't allow separate attach buffers to STENCIL_ATTACHMENT and DEPTH_ATTACHMENT // WebGL doesn't allow separate attach buffers to STENCIL_ATTACHMENT and DEPTH_ATTACHMENT
// we need both or none // we need both or none
const bool useDepthStencil = (format.depthBufferSize() > 0 || format.stencilBufferSize() > 0); const bool useDepthStencil = (format.depthBufferSize() > 0 || format.stencilBufferSize() > 0);
@ -127,13 +137,20 @@ QWasmOpenGLContext::createEmscriptenContext(const std::string &canvasSelector,
attributes.alpha = format.alphaBufferSize() > 0; attributes.alpha = format.alphaBufferSize() > 0;
attributes.depth = useDepthStencil; attributes.depth = useDepthStencil;
attributes.stencil = useDepthStencil; attributes.stencil = useDepthStencil;
EMSCRIPTEN_RESULT contextResult = emscripten_webgl_create_context(canvasSelector.c_str(), &attributes);
return emscripten_webgl_create_context(canvasSelector.c_str(), &attributes); if (contextResult <= 0) {
// fallback to opengles2/webgl1
// for devices that do not support opengles3/webgl2
attributes.majorVersion = 1;
contextResult = emscripten_webgl_create_context(canvasSelector.c_str(), &attributes);
}
return contextResult;
} }
QSurfaceFormat QWasmOpenGLContext::format() const QSurfaceFormat QWasmOpenGLContext::format() const
{ {
return m_requestedFormat; return m_actualFormat;
} }
GLuint QWasmOpenGLContext::defaultFramebufferObject(QPlatformSurface *surface) const GLuint QWasmOpenGLContext::defaultFramebufferObject(QPlatformSurface *surface) const
@ -170,7 +187,7 @@ bool QWasmOpenGLContext::isSharing() const
bool QWasmOpenGLContext::isValid() const bool QWasmOpenGLContext::isValid() const
{ {
if (!isOpenGLVersionSupported(m_requestedFormat)) if (!isOpenGLVersionSupported(m_actualFormat))
return false; return false;
// Note: we get isValid() calls before we see the surface and can // Note: we get isValid() calls before we see the surface and can

View File

@ -43,7 +43,7 @@ private:
static void destroyWebGLContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE contextHandle); static void destroyWebGLContext(EMSCRIPTEN_WEBGL_CONTEXT_HANDLE contextHandle);
QSurfaceFormat m_requestedFormat; QSurfaceFormat m_actualFormat;
QOpenGLContext *m_qGlContext; QOpenGLContext *m_qGlContext;
QOpenGLContextData m_ownedWebGLContext; QOpenGLContextData m_ownedWebGLContext;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE m_usedWebGLContextHandle = 0; EMSCRIPTEN_WEBGL_CONTEXT_HANDLE m_usedWebGLContextHandle = 0;