52
Qt and Multithreaded OpenGL in Cross-Platform Applications Matthew Hunt Senior Software Design Engineer

Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

  • Upload
    others

  • View
    34

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Qt and Multithreaded

OpenGL in Cross-Platform

Applications

Matthew Hunt

Senior Software Design Engineer

Page 2: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

2

Agenda

• Navico and Qt

• Product and Design Overview

• QPA with Windowed OpenGL

• Multithreaded OpenGL in Qt

Page 3: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Who are we?

• Navico is the world’s largest marine

electronics company

• Navico designs, develops, and manufactures a

wide range of products for leisure and

professional marine markets

3

Page 4: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

What do we make?

4

Page 5: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

What do we make?

5

Page 6: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

What do we make?

6

Page 7: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

What do we make?

7

Page 8: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

8

Agenda

• Navico and Qt

• Product and Design Overview

• QPA with Windowed OpenGL

• Multithreaded OpenGL in Qt

Page 9: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Navico and Qt

• Began investigating Qt in 2006

• Needed to invest in development of a new

codebase that could support many platforms

– Existing codebase had reached its limits

(reusability vs. speed)

– Easier driver / BSP support in Linux

– Need to maximize GUI code reuse

9

Page 10: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Navico and Qt

• Started development with Qt v4.4

• Development to support three platforms

simultaneously

– Windows

– X11 Linux

– Embedded Linux with QWS

10

Page 11: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Navico and Qt

• Initial OpenGL Support

– Windows: Modern OpenGL (2.0+)

– X11 Linux: OpenGL 1.5

– Embedded Linux: OpenGL ES 1.1

– Embedded Linux: OpenGL ES 2.0

11

Page 12: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

12

Agenda

• Navico and Qt

• Product and Design Overview

• QPA with Windowed OpenGL

• Multithreaded OpenGL in Qt

Page 13: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Multi-Function Devices (MFDs)

13

Page 14: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Multi-Function Devices (MFDs)

14

Page 15: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Design Constraints

• Common codebase, but different OpenGL

versions

• Third Party closed-source modules

• Multiple independent OpenGL windows

• � Windowed, multithreaded OpenGL

15

Page 16: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Product Demos

Page 17: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

17

Agenda

• Navico and Qt

• Product and Design Overview

• QPA with Windowed OpenGL

• Multithreaded OpenGL in Qt

Page 18: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Implementation Challenges

• The Dark Times: Achieving windowed,

threaded OpenGL support in Qt v4.4

18

Image from Wikipedia

Page 19: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

A New Direction: Lighthouse

• The Lighthouse project was realized as the Qt

Platform Abstraction (QPA) in Qt 4.8

• Replaced the QWS / Qt for Embedded Linux

gfx-plugin approach

• Navico migrated completely to Qt 4.8 to take

advantage of the QPA on embedded devices

19

Page 20: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA

20

Page 21: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA in Qt 4.8

• QPA Plugin Creation Challenges

– Little documentation

– No guarantee of API forward compatibility

• Only for embedded platforms

– Windows and X11 still had their own platform-

specific code in QtGui

21

Page 22: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA in Qt5

• No more platform specific code in QtGui!

• QPA now used for all platforms, Windows,

Linux X11, Embedded Linux, Android, etc.

//

// W A R N I N G

// -------------

//

// This file is part of the QPA API and is not meant to be used

// in applications. Usage of this API may make your code

// source and binary incompatible with future versions of Qt.

//

Page 23: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA in Qt5

• The bad news

– Still lacking good documentation

– Still no guarantee of API forward-compatibility

• The good news

– Changes in QPA between Qt v4.8 and Qt5 have

been mostly additions and not modifications or

removals

Page 24: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA Basic Classes

• QPlatformIntegration

– Provides the basic links between Qt and the

underlying windowing system

– Manages the physical display(s)

– Creates native windows and/or surfaces for

QWidgets

– Creates native pixel storage for QPixmaps

– Handles font abstractions

Page 25: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA Basic Classes

• QPlatformScreen

– Represents the physical display

– Manages geometry and format for the screen

Page 26: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA Basic Classes

• QPlatformWindow

– Represents a window within the screen

– Does not include the surface / content

• Only represents the window

– Provides access to the OpenGL context for the

window (if available)

Page 27: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA Basic Classes

• QWindowSurface (Qt 4.8)

• QPlatformBackingStore (Qt 5)

– Defines the surface / content for a window

– Manages painting and flushing to the surface

Page 28: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA Basic Classes

• QPlatformGLContext (Qt 4.8)

• QPlatformOpenGLContext (Qt 5)

– Abstraction for OpenGL rendering context

– Provides basic makeCurrent() / doneCurrent() /

swapBuffers() APIs

Page 29: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

29

QPA Class Relationship

QPA in Qt 4.8

QPlatformIntegration

QPlatformScreen

QPlatformWindow

QWindowSurface

QPlatformGLContext

QPA in Qt 5

QPlatformIntegration

QPlatformScreen

QPlatformWindow

QPlatformBackingStore

QPlatformOpenGLContext

Page 30: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

– What Is It?

• Provides the abstraction between rendering

APIs and the underlying windowing system

• Similar in purpose to the QPA

Page 31: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

EGL Overview

Page 32: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QPA / EGL Relationship

Page 33: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

EGL – Basic Components

• EGLDisplay

– Represents the abstract display

– Equivalent to QPlatformScreen

• EGLSurface

– Represents a rendering surface

Page 34: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

EGL – Basic Components

• EGLContext

– State machine for the rendering API

– Corresponds to QPlatformGLContext /

QPlatformOpenGLContext

– On the EGL side, a context can be “made current”

on any valid EGLSurface associated with the

EGLDisplay

Page 35: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

35

Agenda

• Navico and Qt

• Product and Design Overview

• QPA with Windowed OpenGL

• Multithreaded OpenGL in Qt

Page 36: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Threaded OpenGL

• Why have multiple threads?

– Need to keep the GUI thread responsive

– Texture uploads can be slow

– Buffer swapping can be slow

Page 37: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Mobile Architecture Challenges

• Memory Bandwidth

– Necessarily restricted

– Shared with CPU

• Cache Limitations

• Architectural Differences

– Mobile GPUs are not just scaled-down desktop

GPUs

Page 38: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

The “QGL” Classes

• QGLWidget

– Encapsulates both a window and an OpenGL context

– Not really designed for threaded rendering

• All default implementations will attempt to re-bind context to GUI thread

• QGLContext

– Encapsulates an OpenGL context

Page 39: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

The “QGL” Classes

• Other Helpful Classes

– QGLBuffer

– QGLShader

– QGLShaderProgram

– QGLFrameBufferObject

Page 40: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Threaded QGL 4.8 Example

Page 41: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

OpenGL without QGLWidget?

• All we really need is a Window and an OpenGL

Context

• QGLWidget default implementation may not

be helpful

• Alternative: Use QWidget and QGLContext

Page 42: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QWidget OpenGL Example

Page 43: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Threaded QGL in Qt5

• Support for QGL-classes in Qt 5: What

changed?

– The QWidget approach no longer works so easily

– QGLContext now keeps track of its thread affinity

via QThread

• makeCurrent()/doneCurrent() isn’t sufficient

Page 44: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

ANGLE in Qt5

• Qt5 can be configured to use desktop OpenGL

or ANGLE

• ANGLE provides transparent cross-platform

development with OpenGL ES 2.0

• Unfortunately, the current ANGLE

implementation is not fully thread safe, so

multiple contexts may not work.

Page 45: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Threaded QGL 5.0 Example

Page 46: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QOpenGL

• New with Qt5

• Intended as a replacement for “QGL”

– But, can be used alongside existing QGL classes

• Recommended for all new OpenGL

development

• Can be integrated into existing widget-based

applications

Page 47: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QOpenGL Classes

• QWindow

– Effectively replaces QGLWidget

– Back-end equivalent is QPlatformWindow

• QOpenGLContext

– Replaces QGLContext

– Back-end equivalent is QPlatformOpenGLContext

Page 48: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

QOpenGL Classes

• Other helpful classes

– QOpenGLBuffer

– QOpenGLShaderProgram

– QOpenGLFrameBufferObject

• These replace their QGL-equivalents

Page 49: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Integrating QOpenGL

• QWidget::createWindowContainer() can be

used to insert QWindows into QWidget-based

applications

Page 50: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Threaded QOpenGL Example

Page 51: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,

Questions?

Page 52: Qt and Multithreaded OpenGL in Cross-Platform Applications€¦ · QPA in Qt5 • No more platform specific code in QtGui! • QPA now used for all platforms, Windows, Linux X11,