qtquick-opengl 下载本文

内容发布更新时间 : 2024/5/18 5:39:18星期一 下面是文章的全部内容请认真阅读。

The Scene Graph in Qt Quick

Qt Quick 2 makes use of a dedicated scene graph based on OpenGL ES 2.0 or OpenGL 2.0 for its rendering. Using a scene graph for graphics rather than the traditional imperative painting systems (QPainter and similar), means the scene to be rendered can be retained between frames and the complete set of primitives to render is known before rendering starts. This opens up for a number of

optimizations, such as batch rendering to minimize state changes and discarding obscured primitives.

For example, say a user-interface contains a list of ten items where each item has a background color, an icon and a text. Using the traditional drawing techniques, this would result in 30 draw calls and a similar amount of state changes. A scene graph, on the other hand, could reorganize the primitives to render such that all backgrounds are drawn in one call, then all icons, then all the text, reducing the total amount of draw calls to only 3. (集中渲染以提升性能)Batching and state change reduction like this can greatly improve performance on some hardware. The scene graph is closely tied to Qt Quick 2.0 and can not be used stand-alone. The scene graph is managed and rendered by the QQuickWindow class and custom Item types can add their graphical primitives into the scene graph through a call to QQuickItem::updatePaintNode().

The scene graph is a graphical representation of the Item scene, an independent structure that contains enough information to render all the items. Once it has been set up, it can be manipulated and rendered independently of the state of the items. On many platforms, the scene graph will even be rendered on a dedicated render thread while the GUI thread is preparing the next frame's state.(视图渲染在单独的线程中进行) Qt Quick Scene Graph Structure

The scene graph is composed of a number of predefined node types, each serving a dedicated purpose. Although we refer to it as a scene graph, a more precise definition is node tree.(节点树) The tree is built from QQuickItem types in the QML scene and internally the scene is then processed by a renderer which draws the scene. The nodes themselves do not contain any active drawing code nor virtual paint()function.

Even though the node tree is mostly built internally by the existing Qt Quick QML types, it is possible for users to also add complete subtrees with their own content, including subtrees that represent 3D models. Nodes

The most important node for users is the QSGGeometryNode. It is used to define custom graphics by defining its geometry and material. The geometry is defined using QSGGeometry and describes the shape or mesh of the graphical primitive. It can be a line, a rectangle, a polygon, many disconnected rectangles, or complex 3D mesh. The material defines how the pixels in this shape are filled.

A node can have any number of children and geometry nodes will be rendered so they appear in child-order with parents behind their children.

Note: This does not say anything about the actual rendering order in the renderer. Only the visual output is guaranteed. The available nodes are:(有以下节点可用) QSGNode

The base class for all nodes in the scene graph

QSGGeometryNode Used for all rendered content in the scene graph QSGClipNode

Implements the clipping functionality in the scene graph

QSGTransformNode Implements transformations in the scene graph QSGOpacityNode

Used to change opacity of nodes

Custom nodes are added to the scene graph by

subclassing QQuickItem::updatePaintNode() and setting the QQuickItem::ItemHasContents flag.

Warning: It is crucial that OpenGL operations and interaction with the scene graph happens exclusively on the render thread, primarily during the

updatePaintNode() call. The rule of thumb is to only use classes with the \prefix inside the QQuickItem::updatePaintNode() function. For more details, see the Scene Graph - Custom Geometry. Preprocessing

Nodes have a virtual QSGNode::preprocess() function, which will be called the scene graph is rendered. Node subclasses can set the

flag QSGNode::UsePreprocess and override theQSGNode::preprocess() function to do final preparation of their node. For example, dividing a bezier curve into

correct level of detail for the current scale factor or updating a section of a texture. Node Ownership

Ownership of the nodes is either done explicitly by the creator or by the scene graph by setting the flag QSGNode::OwnedByParent. Assigning ownership to scene graph is often preferable as it simplifies cleanup when the scene graph lives outside the GUI thread. Materials

The material describes how the interior of a geometry in a QSGGeometryNode is filled. It encapsulates an OpenGL shader program and provides ample flexibility in what can be achieved, though most of the Qt Quick items themselves only use very basic materials, such as solid color and texture fills.

For users who just want to apply custom shading to a QML Item type, it is possible to do this directly in QML using the ShaderEffect type. Below is a complete list of material classes: QSGMaterialShader

Represents an OpenGL shader program in the renderer

Used as a unique type token in combination with QSGMaterial

Encapsulates rendering state for a shader program

Convenient way of rendering solid colored geometry in the scene graph

Convenient way of building custom materials for the scene graph

Template generated class used to store the state used with a QSGSimpleMateralShader

QSGMaterialType

QSGMaterial

QSGFlatColorMaterial

QSGSimpleMaterialShader

QSGSimpleMaterial

QSGOpaqueTextureMaterial Convenient way of rendering textured geometry

in the scene graph QSGTextureMaterial

Convenient way of rendering textured geometry in the scene graph

Convenient way of rendering per-vertex colored geometry in the scene graph

QSGVertexColorMaterial

For more details, see the Scene Graph - Simple Material Convenience Nodes

The scene graph API is very low-level and focuses on performance rather than convenience. Writing custom geometries and materials from scratch, even the most basic ones, requires a non-trivial amount of code. For this reason, the API includes a few convenience classes to make the most common custom nodes readily available.

QSGSimpleRectNode - a QSGGeometryNode subclass which defines a rectangular geometry with a solid color material.

QSGSimpleTextureNode - a QSGGeometryNode subclass which defines a rectangular geometry with a texture material. Scene Graph and Rendering

The rendering of the scene graph happens internally in the QQuickWindow class, and there is no public API to access it. There are however, a few places in the rendering pipeline where the user can attach application code. This can be to custom scene graph content or render raw OpenGL content. The integration points are defined by the render loop.

For detailed description of how the scene graph renderer works, see Qt Quick Scene Graph Renderer. Threaded Render Loop

On many configurations, the scene graph rendering will happen on a dedicated render thread. This is done to increase parallelism of multi-core processors and make better use of stall times such as waiting for a blocking swap buffer call.

offers significant performance improvements, but imposes certain restrictions on where and when interaction with the scene graph can happen.

The following is a simple outline of how a frame gets composed with the threaded render loop.

A change occurs in the QML scene, causing QQuickItem::update() to be called. This can be the result of for instance an animation or user input. An event is posted to the render thread to initiate a new frame.

The render thread prepares to draw a new frame and makes the OpenGL context current and initiates a blocks on the GUI thread.