QUItCoding / qnanopainter

Library for implementing OpenGL accelerated Qt (Quick) C++ UI components.
http://quitcoding.com
Other
394 stars 77 forks source link

qnanopainter demos crash using Qt5.12's WebGL backend. #38

Open NielsMayer opened 5 years ago

NielsMayer commented 5 years ago

Qt5.12LTS has the ability to run apps on the browser: https://blog.qt.io/blog/2018/11/23/qt-quick-webgl-release-512/

Unfortunately, QNanoPainter-based apps crash under the new WebGL backend.

In order to debug qnanopainter examples under Qtcreator 4.8.0, with the new webgl backend: (0) Load qnanopainter.pro in qtcreator (1) Click "Projects" mode -> "Build & Run" -> "Your Desktop platform" -> Run a) Under "run configuration" select "gallery" then click Add->"Clone Selected" b) Name the cloned run configuration, for example, "gallery-webgl" c) Under "Command line arguments" for run configuration "gallery-webgl" enter -platform webgl:port=8998 (2) Select run configuration "gallery-webgl" and click the "Start Debugging" button. a) application will launch and wait for browser. (3) in browser visit " http://localhost:8998 " (4) The QtQuick component menus of app "gallery" will now be visible in the browser. (5) Clicking any of the menus in the browser will crash the app once QNanoPainter is invoked. The other examples will crash immediately on startup (in the same code) as they init QNanoPainter right away.

The crash occurs at https://github.com/QUItCoding/qnanopainter/blob/master/libqnanopainter/qnanopainter.cpp#L157

Q_ASSERT_X(m_nvgContext, "QNanoPainter::QNanoPainter", "Could not init nanovg!"); Prior to the crash the following output is provided on stdout/stderr (note environment "QSG_RENDER_LOOP=threaded" and "QSG_INFO=1", running on Kubuntu 18.04LTS)

11:02:16: Debugging starts QML debugging is enabled. Only use this in a safe environment. QML Debugger: Waiting for connection on port 35553... Running Qt >= 5.10, so enabling QML Shape example qt.scenegraph.general: threaded render loop qt.scenegraph.general: Using sg animation driver qt.scenegraph.general: Animation Driver: using vsync: 16.67 ms libpng warning: iCCP: known incorrect sRGB profile qt.scenegraph.general: Using sg animation driver qt.scenegraph.general: Animation Driver: using vsync: 16.67 ms qt.scenegraph.general: Using sg animation driver qt.scenegraph.general: Animation Driver: using vsync: 16.67 ms qt.scenegraph.general: texture atlas dimensions: 2048x1024 qt.scenegraph.general: R/G/B/A Buffers: -1 -1 -1 -1 qt.scenegraph.general: Depth Buffer: 24 qt.scenegraph.general: Stencil Buffer: 8 qt.scenegraph.general: Samples: -1 qt.scenegraph.general: GL_VENDOR: WebKit qt.scenegraph.general: GL_RENDERER: WebKit WebGL qt.scenegraph.general: GL_VERSION: WebGL 1.0 (OpenGL ES 2.0 Chromium) qt.scenegraph.general: GL_EXTENSIONS: GL_OES_packed_depth_stencil GL_OES_element_index_uint GL_OES_depth_texture GL_OES_standard_derivatives qt.scenegraph.general: Max Texture Size: 16384 qt.scenegraph.general: Debug context: false Creating suitable QNanoBackend for OpenGL 2.0 context Using backend: "OpenGL 2" ASSERT failure in QNanoPainter::QNanoPainter: "Could not init nanovg!", file ../../../qnanopainter/libqnanopainter/qnanopainter.cpp, line 157 11:02:46: Debugging has finished

NielsMayer commented 5 years ago

As corollary, the same crash could occur in hellowidget example but it would happen because widgets are not supported, and not because of the issue with initializing nanovg.

13:54:33: Debugging starts QOpenGLWidget is not supported on this platform. qt.qpa.webgl: WebGL QPA platform plugin: Raster surfaces are not supported 13:54:50: Debugging has finished

From the link at beginning of this page:

Since it’s for OpenGL (ES) things only, WebGL streaming does not work with Widgets or any other non-OpenGL stuff. In fact, if you try to launch some “non-compatible” Qt application using WebGL QPA, most likely you’ll get the following error: qt.qpa.webgl: WebGL QPA platform plugin: Raster surfaces are not supported

To clarify, the 'hellowidget' example is not part of this bug. All the other examples are.

gunrot commented 5 years ago

Hi Niels, your output shows Creating suitable QNanoBackend for OpenGL 2.0 context Using backend: "OpenGL 2"

So it is no wonder that nanvg init returns a nullptr. The question is why qnanopainter detects open gl instead of gles. The code uses bool isGLES = (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES); so it looks like qt returns it wrong.

The docs says: Note: A desktop OpenGL implementation may be capable of creating ES-compatible contexts too. Therefore in most cases it is more appropriate to check QSurfaceFormat::renderableType() or use the convenience function isOpenGLES().

Note: This function requires that the QGuiApplication instance is already created.

Regards, Gunnar

NielsMayer commented 4 years ago

To Build with Qt5.15 and wasm, one needs to explicitly force

equals(QT_ARCH,"wasm") {
    DEFINES += QNANO_BUILD_GLES_BACKENDS
}

Or you get error using the default build (which doesn't detect WASM -- the .pro and .pri files need the above change to make this work automatically.)

In file included from ../../libqnanopainter/private/qnanobackendgl2.cpp:12:
../../libqnanopainter/nanovg/nanovg_gl_wrapped.h:750:51: error: use of undeclared identifier 'GL_GENERATE_MIPMAP'
                QNANO_GLFWRAPPER glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Note that wasm requires GLES, per https://wiki.qt.io/Qt_for_WebAssembly#General_Notes

"Qt will detect OpenGL support as OpenGL ES. In reality the browser will be providing WebGL. WebGL is based on ES and is very similar, but there are some incompatibilities. See WebGL and OpenGL Differences"

QUItCoding commented 4 years ago

Thanks! I pushed patch above according to your help which should fix this.