| 1 | // Copyright (C) 2018 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qwaitfence.h" |
| 5 | #include "qwaitfence_p.h" |
| 6 | #include <Qt3DRender/private/qframegraphnode_p.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace Qt3DRender { |
| 11 | |
| 12 | QWaitFencePrivate::QWaitFencePrivate() |
| 13 | : QFrameGraphNodePrivate() |
| 14 | , m_handleType(QWaitFence::NoHandle) |
| 15 | , m_handle(QVariant()) |
| 16 | , m_waitOnCPU(false) |
| 17 | , m_timeout(std::numeric_limits<quint64>::max()) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | /*! |
| 22 | \class Qt3DRender::QWaitFence |
| 23 | \inmodule Qt3DRender |
| 24 | \brief FrameGraphNode used to wait for a fence in the graphics command |
| 25 | stream to become signaled. |
| 26 | |
| 27 | Fence allow to synchronosize GPU and CPU workloads. GPU commands usually |
| 28 | are non-blocking. When issued, commands are inserted in command buffers |
| 29 | which will be read at a later time by the GPU. In some cases, you want to |
| 30 | continue processing or issue specific command only when you are sure a |
| 31 | command has been executed by the hardware. Fences are a way to do so. This |
| 32 | is especially important when using 3rd party engines with Qt3D, Qt3D should |
| 33 | only access shared resources when we know the other engine command are done |
| 34 | modifying the resource. |
| 35 | |
| 36 | QWaitFence is a FrameGraph node that will force to wait for it to become |
| 37 | signaled before subsequent commands are inserted into the command stream. |
| 38 | It can then be used in conjunction with \l QSetFence and contains |
| 39 | properties to configure how long it should wait and whether it should block |
| 40 | on the CPU side. |
| 41 | |
| 42 | \note Qt 3D uploads GPU resources (Texture, Shaders, Buffers) before |
| 43 | issuing draw calls. |
| 44 | |
| 45 | \since 5.13 |
| 46 | */ |
| 47 | |
| 48 | QWaitFence::QWaitFence(Qt3DCore::QNode *parent) |
| 49 | : QFrameGraphNode(*new QWaitFencePrivate(), parent) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | QWaitFence::~QWaitFence() |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | /*! |
| 58 | \qmlproperty bool WaitFence::waitOnCPU |
| 59 | |
| 60 | Specifies whether the CPU should be block while waiting for the fence to |
| 61 | become signaled. This is false by default. |
| 62 | */ |
| 63 | /*! |
| 64 | \property Qt3DRender::QWaitFence::waitOnCPU |
| 65 | |
| 66 | Specifies whether the CPU should be block while waiting for the fence to |
| 67 | become signaled. This is false by default. |
| 68 | */ |
| 69 | bool QWaitFence::waitOnCPU() const |
| 70 | { |
| 71 | Q_D(const QWaitFence); |
| 72 | return d->m_waitOnCPU; |
| 73 | } |
| 74 | |
| 75 | void QWaitFence::setWaitOnCPU(bool waitOnCPU) |
| 76 | { |
| 77 | Q_D(QWaitFence); |
| 78 | if (d->m_waitOnCPU == waitOnCPU) |
| 79 | return; |
| 80 | d->m_waitOnCPU = waitOnCPU; |
| 81 | emit waitOnCPUChanged(waitOnCPU); |
| 82 | } |
| 83 | |
| 84 | /*! |
| 85 | \qmlproperty int WaitFence::timeout |
| 86 | |
| 87 | Specifies the maximum amount of time in nanoseconds to wait for the fence |
| 88 | to become signaled. |
| 89 | */ |
| 90 | /*! |
| 91 | \property Qt3DRender::QWaitFence::timeout |
| 92 | |
| 93 | Specifies the maximum amount of time in nanoseconds to wait for the fence |
| 94 | to become signaled. |
| 95 | */ |
| 96 | quint64 QWaitFence::timeout() const |
| 97 | { |
| 98 | Q_D(const QWaitFence); |
| 99 | return d->m_timeout; |
| 100 | } |
| 101 | |
| 102 | void QWaitFence::setTimeout(quint64 timeout) |
| 103 | { |
| 104 | Q_D(QWaitFence); |
| 105 | if (d->m_timeout == timeout) |
| 106 | return; |
| 107 | d->m_timeout = timeout; |
| 108 | emit timeoutChanged(timeoutChanged: timeout); |
| 109 | } |
| 110 | |
| 111 | QWaitFence::QWaitFence(QWaitFencePrivate &dd, Qt3DCore::QNode *parent) |
| 112 | : QFrameGraphNode(dd, parent) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | \qmlproperty HandleType WaitFence::handleType |
| 118 | |
| 119 | Specifies the type of handle being used. Currently only OpenGL Fence ids |
| 120 | are supported. |
| 121 | */ |
| 122 | /*! |
| 123 | \property Qt3DRender::QWaitFence::handleType |
| 124 | |
| 125 | Specifies the type of handle being used. Currently only OpenGL Fence ids |
| 126 | are supported. |
| 127 | */ |
| 128 | QWaitFence::HandleType QWaitFence::handleType() const |
| 129 | { |
| 130 | Q_D(const QWaitFence); |
| 131 | return d->m_handleType; |
| 132 | } |
| 133 | |
| 134 | void QWaitFence::setHandleType(QWaitFence::HandleType type) |
| 135 | { |
| 136 | Q_D(QWaitFence); |
| 137 | if (d->m_handleType != type) { |
| 138 | d->m_handleType = type; |
| 139 | emit handleTypeChanged(handleType: type); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /*! |
| 144 | \qmlproperty variant WaitFence::handle |
| 145 | |
| 146 | Holds the underlying fence handle wrapped in a variant. |
| 147 | */ |
| 148 | /*! |
| 149 | \property Qt3DRender::QWaitFence::handle |
| 150 | |
| 151 | Holds the underlying fence handle wrapped in a QVariant. |
| 152 | */ |
| 153 | QVariant QWaitFence::handle() const |
| 154 | { |
| 155 | Q_D(const QWaitFence); |
| 156 | return d->m_handle; |
| 157 | } |
| 158 | |
| 159 | void QWaitFence::setHandle(QVariant handle) |
| 160 | { |
| 161 | Q_D(QWaitFence); |
| 162 | if (d->m_handle != handle) { |
| 163 | d->m_handle = handle; |
| 164 | emit handleChanged(handle); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | } // Qt3DRender |
| 169 | |
| 170 | QT_END_NAMESPACE |
| 171 | |
| 172 | #include "moc_qwaitfence.cpp" |
| 173 | |