Writing a Custom Qt 3D Aspect – part 2 Setting up the backend and communications
Introduction
In the previous article we gave an overview of the process for creating a custom aspect and showed how to create (most of) the front end functionality. In this article we shall continue building our custom aspect by implementing the corresponding backend types, registering the types and setting up communication from the frontend to the backend objects. This will get us most of the way there. The next article will wrap up by showing how to implement jobs to process our aspect’s components.
As a reminder of what we are dealing with, here’s the architecture diagram from part 1:
Creating the Backend
One of the nice things about Qt 3D is that it is capable of very high throughput. This is achieved by way of using jobs executed on a threadpool in the backend. To be able to do this without introducing a tangled web of synchronisation points (which would limit the parallelism), we make a classic computer science trade-off and sacrifice memory for the benefit of speed. By having each aspect work on its own copy of the data, it can schedule jobs safe in the knowledge that nothing else will be trampling all over its data.
This is not as costly as it sounds. The backend nodes are not derived from QObject. The base class for backend nodes is Qt3DCore::QBackendNode, which is a pretty lightweight class. Also, note that aspects only store the data that they specifically care about in the backend. For example, the animation aspect does not care about which Material component an Entity has, so no need to store any data from it. Conversely, the render aspect doesn’t care about Animation clips or Animator components.
In our little custom aspect, we only have one type of frontend component, FpsMonitor. Logically, we will only have a single corresponding backend type, which we will imaginatively call FpsMonitorBackend:
class FpsMonitorBackend : public Qt3DCore::QBackendNode { public: FpsMonitorBackend() : Qt3DCore::QBackendNode(Qt3DCore::QBackendNode::ReadWrite) , m_rollingMeanFrameCount(5) {} private: void initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) override { // TODO: Implement me! } int m_rollingMeanFrameCount; };
The class declaration is very simple. We subclass Qt3DCore::QBackendNode as you would expect; add a data member to mirror the information from the frontend FpsMonitor component; and override the initializeFromPeer() virtual function. This function will be called just after Qt 3D creates an instance of our backend type. The argument allows us to get at the data sent from the corresponding frontend object as we will see shortly.
Registering the Types
We now have simple implementations of the frontend and backend components. The next step is to register these with the aspect so that it knows to instantiate the backend node whenever a frontend node is created. Similarly for destruction. We do this by way of an intermediary helper known as a node mapper.
To create a node mapper, just subclass Qt3DCore::QNodeMapper and override the virtuals to create, lookup and destroy the backend objects on demand. The manner in which you create, store, lookup and destroy the objects is entirely up to you as a developer. Qt 3D does not impose any particular management scheme upon you. The render aspect does some fairly fancy things with bucketed memory managers and aligning memory for SIMD types, but here we can do something much simpler.
We will store pointers to the backend nodes in a QHash within the CustomAspect and index them by the node’s Qt3DCore::QNodeId. The node id is used to uniquely identify a given node, even between the frontend and all available aspect backends. On Qt3DCore::QNode the id is available via the id() function, whereas for QBackendNode you access it via the peerId() function. For the two corresponding objects representing the component, the id() and peerId() functions return the same QNodeId value.
Let’s get to it and add some storage for the backend nodes to the CustomAspect along with some helper functions:
class CustomAspect : public Qt3DCore::QAbstractAspect { Q_OBJECT public: ... void addFpsMonitor(Qt3DCore::QNodeId id, FpsMonitorBackend *fpsMonitor) { m_fpsMonitors.insert(id, fpsMonitor); } FpsMonitorBackend *fpsMonitor(Qt3DCore::QNodeId id) { return m_fpsMonitors.value(id, nullptr); } FpsMonitorBackend *takeFpsMonitor(Qt3DCore::QNodeId id) { return m_fpsMonitors.take(id); } ... private: QHash<Qt3DCore::QNodeId, FpsMonitorBackend *> m_fpsMonitors; };
Now we can implement a simple node mapper as:
class FpsMonitorMapper : public Qt3DCore::QBackendNodeMapper { public: explicit FpsMonitorMapper(CustomAspect *aspect); Qt3DCore::QBackendNode *create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const override { auto fpsMonitor = new FpsMonitorBackend; m_aspect->addFpsMonitor(change->subjectId(), fpsMonitor); return fpsMonitor; } Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const override { return m_aspect->fpsMonitor(id); } void destroy(Qt3DCore::QNodeId id) const override { auto fpsMonitor = m_aspect->takeFpsMonitor(id); delete fpsMonitor; } private: CustomAspect *m_aspect; };
To finish this piece of the puzzle, we now need to tell the aspect about how these types and the mapper relate to each other. We do this by calling QAbstractAspect::registerBackendType() template function, passing in a shared pointer to the mapper that will create, find, and destroy the corresponding backend nodes. The template argument is the type of the frontend node for which this mapper should be called. A convenient place to do this is in the constructor of the CustomAspect. In our case it looks like this:
CustomAspect::CustomAspect(QObject *parent) : Qt3DCore::QAbstractAspect(parent) { // Register the mapper to handle creation, lookup, and destruction of backend nodes auto mapper = QSharedPointer<FpsMonitorMapper>::create(this); registerBackendType<FpsMonitor>(mapper); }
And that’s it! With that registration in place, any time an FpsMonitor component is added to the frontend object tree (the scene), the aspect will lookup the node mapper for that type of object. Here, it will find our registered FpsMonitorMapper object and it will call its create() function to create the backend node and manage its storage. A similar story holds for the destruction (technically, it’s the removal from the scene) of the frontend node. The mapper’s get() function is used internally to be able to call virtuals on the backend node at appropriate points in time (e.g. when properties notify that they have been changed).
The Frontend-Backend Communications
Now that we are able to create, access and destroy the backend node for any frontend node, let’s see how we can let them talk to each other. There are 3 main times the frontend and backend nodes communicate with each other:
- Initialisation — When our backend node is first created we get an opportunity to initialise it with data sent from the frontend node.
- Frontend to Backend — Typically when properties on the frontend node get changed we want to send the new property value to the backend node so that it is operating on up to date information.
- Backend to Frontend — When our jobs process the data stored in the backend nodes, sometimes this will result in updated values that should be sent to the frontend node.
Here we will cover the first two cases. The third case will be deferred until the next article when we introduce jobs.
Backend Node Initialisation
All communication between frontend and backend objects operates by sending sub-classed Qt3DCore::QSceneChanges. These are similar in nature and concept to QEvent but the change arbiter that processes the changes has the opportunity to manipulate them in the case of conflicts from multiple aspects, re-order them into priority, or any other manipulations that may be needed in the future.
For the purpose of initialising the backend node upon creation, we use a Qt3DCore::QNodeCreatedChange which is a templated type that we can use to wrap up our type-specific data. When Qt 3D wants to notify the backend about your frontend node’s initial state, it calls the private virtual function QNode::createNodeCreationChange(). This function returns a node created change containing any information that we wish to access in the backend node. We have to do it by copying the data rather than just dereferencing a pointer to the frontend object because by the time the backend processes the request, the frontend object may have been deleted – i.e. a classic data race. For our simple component our implementation looks like this:
struct FpsMonitorData { int rollingMeanFrameCount; };
Qt3DCore::QNodeCreatedChangeBasePtr FpsMonitor::createNodeCreationChange() const { auto creationChange = Qt3DCore::QNodeCreatedChangePtr<FpsMonitorData>::create(this); auto &data = creationChange->data; data.rollingMeanFrameCount = m_rollingMeanFrameCount; return creationChange; }
The change created by our frontend node is passed to the backend node (via the change arbiter) and gets processed by the initializeFromPeer() virtual function:
void FpsMonitorBackend::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) { const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<FpsMonitorData>>(change); const auto &data = typedChange->data; m_rollingMeanFrameCount = data.rollingMeanFrameCount; }
Frontend to Backend Communication
At this point, the backend node mirrors the initial state of the frontend node. But what if the user changes a property on the frontend node? When that happens, our backend node will hold stale data.
The good news is that this is easy to handle. The implementation of Qt3DCore::QNode takes care of the first half of the problem for us. Internally it listens to the Q_PROPERTY notification signals and when it sees that a property has changed, it creates a QPropertyUpdatedChange for us and dispatches it to the change arbiter which in turn delivers it to the backend node’s sceneChangeEvent() function.
So all we need to do as authors of the backend node is to override this function, extract the data from the change object and update our internal state. Often you will then want to mark the backend node as dirty in some way so that the aspect knows it needs to be processed next frame. Here though, we will just update the state to reflect the latest value from the frontend:
void FpsMonitorBackend::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) { if (e->type() == Qt3DCore::PropertyUpdated) { const auto change = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e); if (change->propertyName() == QByteArrayLiteral("rollingMeanFrameCount")) { const auto newValue = change->value().toInt(); if (newValue != m_rollingMeanFrameCount) { m_rollingMeanFrameCount = newValue; // TODO: Update fps calculations } return; } } QBackendNode::sceneChangeEvent(e); }
If you don’t want to use the built in automatic property change dispatch of Qt3DCore::QNode then you can disable it by wrapping the property notification signal emission in a call to QNode::blockNotifications(). This works in exactly the same manner as QObject::blockSignals() except that it only blocks sending the notifications to the backend node, not the signal itself. This means that other connections or property bindings that rely upon your signals will still work.
If you block the default notifications in this way, then you need to send your own to ensure that the backend node has up to date information. Feel free to subclass any class in the Qt3DCore::QSceneChange hierarchy and bend it to your needs. A common approach is to subclass Qt3DCore::QStaticPropertyUpdatedChangeBase, which handles the property name and in the subclass add a strongly typed member for the property value payload. The advantage of this over the built-in mechanism is that it avoids using QVariant which does suffer a little in highly threaded contexts in terms of performance. Usually though, the frontend properties don’t change too frequently and the default is fine.
Summary
In this article we have shown how to implement most of the backend node; how to register the node mapper with the aspect to create, lookup and destroy backend nodes; how to initialise the backend node from the frontend node in a safe way and also how to keep its data in sync with the frontend.
In the next article we will finally make our custom aspect actually do some real (if simple) work, and learn how to get the backend node to send updates to the frontend node (the mean fps value). We will ensure that the heavy lifting parts get executed in the context of the Qt 3D threadpool so that you get an idea of how it can scale. Until next time.
Thanks for writing about aspects. The current official docs mainly state. “Aspects exist. You probably need to use them somehow.” That said, gosh the design of Qt3D seems to be theoretically trying to make some complicated things easy by making some simple things very, very complicated. Stuff like this makes me wonder if I should just be doing everything in raw OpenGL.
This is a fantastic series, but is now nearly 2 years old with the final instalment on “jobs” yet to be published!
Is it possible to receive updates for dynamic objects? Event call of setPropertyTracking for them has no effect. sceneChangeEvent on backend node not called, after I call setProperty for the property.
*properties
Thanks a lot for this excellent introduction to custom aspects! Is this next article about aspect jobs mentioned in the text actually existing yet? (I did not manage to find it, nor does there seem to be a proper documentation about aspect jobs yet.) Thanks again!
Great write-up, when can we expect the third article?