Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgdebugdrawsystem.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
6#include "graphobjects/qssgrendermodel_p.h"
8
10#include "resourcemanager/qssgrenderbuffermanager_p.h"
11#include "rendererimpl/qssgrenderableobjects_p.h"
12#include "rendererimpl/qssglayerrenderdata_p.h"
13
15
16QSSGDebugDrawSystem::QSSGDebugDrawSystem()
17{
18
19}
20
21QSSGDebugDrawSystem::~QSSGDebugDrawSystem()
22{
23
24}
25
26bool QSSGDebugDrawSystem::hasContent() const
27{
28 return !m_lines.isEmpty() || !m_persistentLines.isEmpty() || !m_bounds.isEmpty() || !m_persistentBounds.isEmpty() || !m_persistentPoints.isEmpty() || !m_points.isEmpty();
29}
30
31void QSSGDebugDrawSystem::drawLine(const QVector3D &startPoint,
32 const QVector3D &endPoint,
33 const QColor &color,
34 bool isPersistent)
35{
36 LineData line = {startPoint, endPoint, color};
37 if (isPersistent)
38 m_persistentLines.append(line);
39 else
40 m_lines.append(line);
41}
42
43void QSSGDebugDrawSystem::drawBounds(const QSSGBounds3 &bounds,
44 const QColor &color,
45 bool isPersistent)
46{
47 BoundsData bound = {bounds, color};
48 if (isPersistent)
49 m_persistentBounds.append(bound);
50 else
51 m_bounds.append(bound);
52}
53
54void QSSGDebugDrawSystem::drawPoint(const QVector3D &vertex, const QColor &color, bool isPersistent)
55{
56 VertexData point = {vertex, {color.redF(), color.greenF(), color.blueF()}};
57 if (isPersistent)
58 m_persistentPoints.append(point);
59 else
60 m_points.append(point);
61}
62
63
64void QSSGDebugDrawSystem::prepareGeometry(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
65{
66
67 QVector<VertexData> vertexData;
68 QVector<quint32> indexData;
69 QVector<VertexData> pointsData;
70 for (const auto &line : std::as_const(m_persistentLines))
71 generateLine(line, vertexData, indexData);
72 for (const auto &line : std::as_const(m_lines))
73 generateLine(line, vertexData, indexData);
74 for (const auto &bounds : std::as_const(m_persistentBounds))
75 generateBox(bounds, vertexData, indexData);
76 for (const auto &bounds : std::as_const(m_bounds))
77 generateBox(bounds, vertexData, indexData);
78 pointsData = m_persistentPoints + m_points;
79
80 if (!vertexData.isEmpty()) {
81 // Lines
82 QByteArray vertexBufferData(reinterpret_cast<const char*>(vertexData.constData()), qsizetype(vertexData.count() * 6 * sizeof(float)));
83 QByteArray indexBufferData(reinterpret_cast<const char*>(indexData.constData()), qsizetype(indexData.count() * sizeof(quint32)));
84
85 if (m_lineVertexBuffer)
86 m_lineVertexBuffer.reset();
87 if (m_lineIndexBuffer)
88 m_lineIndexBuffer.reset();
89
90 m_lineVertexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
91 QRhiBuffer::Immutable,
92 QRhiBuffer::VertexBuffer,
93 quint32(6 * sizeof(float)),
94 6 * sizeof(float) * vertexData.count());
95 m_lineVertexBuffer->buffer()->setName(QByteArrayLiteral("debug lines vertex buffer"));
96 rub->uploadStaticBuffer(m_lineVertexBuffer->buffer(), vertexBufferData.constData());
97
98 m_lineIndexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
99 QRhiBuffer::Immutable,
100 QRhiBuffer::IndexBuffer,
101 0,
102 indexBufferData.size(),
103 QRhiCommandBuffer::IndexUInt32);
104 m_lineIndexBuffer->buffer()->setName(QByteArrayLiteral("debug lines index buffer"));
105 rub->uploadStaticBuffer(m_lineIndexBuffer->buffer(), indexBufferData.constData());
106
107 m_indexSize = indexData.count();
108 }
109
110 if (!pointsData.isEmpty()) {
111 // Points
112 QByteArray vertexBufferData(reinterpret_cast<const char*>(pointsData.constData()), qsizetype(pointsData.count() * 6 * sizeof(float)));
113
114 if (m_pointVertexBuffer)
115 m_pointVertexBuffer.reset();
116
117 m_pointVertexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
118 QRhiBuffer::Immutable,
119 QRhiBuffer::VertexBuffer,
120 quint32(6 * sizeof(float)),
121 vertexBufferData.size());
122 m_pointVertexBuffer->buffer()->setName(QByteArrayLiteral("debug points vertex buffer"));
123 rub->uploadStaticBuffer(m_pointVertexBuffer->buffer(), vertexBufferData.constData());
124 m_pointsSize = pointsData.count();
125 }
126}
127
128void QSSGDebugDrawSystem::recordRenderDebugObjects(QSSGRhiContext *rhiCtx,
129 QSSGRhiGraphicsPipelineState *ps,
130 QRhiShaderResourceBindings *srb,
131 QRhiRenderPassDescriptor *rpDesc)
132{
133 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
134 ia.inputLayout.setAttributes({
135 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
136 { 0, 1, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) }
137 });
138 ia.inputs << QSSGRhiInputAssemblerState::PositionSemantic
139 << QSSGRhiInputAssemblerState::ColorSemantic;
140 ia.inputLayout.setBindings({6 * sizeof(float)});
141 ia.topology = QRhiGraphicsPipeline::Lines;
142 ps->flags |= QSSGRhiGraphicsPipelineState::Flag::DepthWriteEnabled;
143 ps->flags |= QSSGRhiGraphicsPipelineState::Flag::DepthTestEnabled;
144 ps->cullMode = QRhiGraphicsPipeline::None;
145
146 QSSGRhiContextPrivate *rhiCtxD = QSSGRhiContextPrivate::get(rhiCtx);
147 QRhiCommandBuffer *cb = rhiCtx->commandBuffer();
148 if (m_indexSize > 0) {
149 auto graphicsPipeline = rhiCtxD->pipeline(*ps, rpDesc, srb);
150 cb->setGraphicsPipeline(graphicsPipeline);
151 cb->setShaderResources(srb);
152 cb->setViewport(ps->viewport);
153
154 // Lines
155 QRhiCommandBuffer::VertexInput vb(m_lineVertexBuffer->buffer(), 0);
156 cb->setVertexInput(0, 1, &vb, m_lineIndexBuffer->buffer(), 0, m_lineIndexBuffer->indexFormat());
157 cb->drawIndexed(m_indexSize);
158 }
159
160 // Points
161 if (m_pointsSize > 0) {
162 ia.topology = QRhiGraphicsPipeline::Points;
163 auto graphicsPipeline = rhiCtxD->pipeline(*ps, rpDesc, srb);
164 cb->setGraphicsPipeline(graphicsPipeline);
165 cb->setShaderResources(srb);
166 cb->setViewport(ps->viewport);
167
168 QRhiCommandBuffer::VertexInput vb(m_pointVertexBuffer->buffer(), 0);
169 cb->setVertexInput(0, 1, &vb);
170 cb->draw(m_pointsSize);
171 }
172
173 m_lines.clear();
174 m_bounds.clear();
175 m_points.clear();
176 m_indexSize = 0;
177 m_pointsSize = 0;
178}
179
180void QSSGDebugDrawSystem::setEnabled(bool v)
181{
182 modes = v ? (modes | ModeFlagT(Mode::Other)) : (modes & ~ModeFlagT(Mode::Other));
183}
184
185void QSSGDebugDrawSystem::generateLine(const LineData &line, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
186{
187 const QVector3D color = {line.color.redF(), line.color.greenF(), line.color.blueF()};
188 indexArray.append(vertexArray.count());
189 vertexArray.append({line.startPoint, color});
190 indexArray.append(vertexArray.count());
191 vertexArray.append({line.endPoint, color});
192}
193
194void QSSGDebugDrawSystem::generateBox(const BoundsData &bounds, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
195{
196 const QVector3D color = {bounds.color.redF(), bounds.color.greenF(), bounds.color.blueF()};
197
198 quint32 offset = vertexArray.count();
199 for (const QVector3D point : bounds.bounds.toQSSGBoxPoints())
200 vertexArray.append({point, color});
201
202 // The corners are in the order of QSSGBounds3::toQSSGBoxPoints(): 0-3 the min z face, 4-7 the max z face.
203 static constexpr quint32 edges[12][2] = { {0, 1}, {1, 2}, {2, 3}, {3, 0},
204 {4, 5}, {5, 6}, {6, 7}, {7, 4},
205 {0, 4}, {1, 5}, {2, 6}, {3, 7} };
206 for (const auto &edge : edges) {
207 indexArray.append(offset + edge[0]);
208 indexArray.append(offset + edge[1]);
209 }
210}
211
212QColor QSSGDebugDrawSystem::levelOfDetailColor(quint32 lod)
213{
214 static const QColor colors[] {
215 QColor(Qt::white),
216 QColor(Qt::red),
217 QColor(Qt::green),
218 QColor(Qt::blue),
219 QColor(Qt::yellow),
220 QColor(Qt::cyan),
221 QColor(Qt::magenta),
222 QColor(Qt::darkRed),
223 QColor(Qt::darkGreen),
224 QColor(Qt::darkBlue),
225 QColor(Qt::darkCyan),
226 QColor(Qt::darkMagenta),
227 QColor(Qt::darkYellow),
228 QColor(Qt::darkGray)
229 };
230
231 const size_t idx = qBound<size_t>(0, lod, std::size(colors) - 1);
232 return colors[idx];
233}
234
235void QSSGDebugDrawSystem::debugNormals(QSSGBufferManager &bufferManager, const QSSGModelContext &theModelContext, const QSSGRenderSubset &theSubset, quint32 subsetLevelOfDetail, float lineLength)
236{
237 const auto &model = theModelContext.model;
238
239 QSSGMesh::Mesh mesh;
240 if (model.geometry)
241 mesh = bufferManager.loadMeshData(model.geometry);
242 else
243 mesh = bufferManager.loadMeshData(model.meshPath);
244
245 if (!mesh.isValid())
246 return; // invalid mesh
247
248 QByteArray vertexData = mesh.vertexBuffer().data;
249 if (vertexData.isEmpty())
250 return; // no vertex dat
251 quint32 vertexStride = mesh.vertexBuffer().stride;
252 QByteArray indexData = mesh.indexBuffer().data;
253 if (indexData.isEmpty())
254 return; // no index data, not what we're after
255 if (mesh.indexBuffer().componentType != QSSGMesh::Mesh::ComponentType::UnsignedInt32)
256 return; // not uint3d, not what we're after either
257
258 quint32 positionOffset = UINT_MAX;
259 quint32 normalOffset = UINT_MAX;
260
261 const auto vertexBuffer = mesh.vertexBuffer();
262 for (const QSSGMesh::Mesh::VertexBufferEntry &vbe : vertexBuffer.entries) {
263 if (vbe.name == QSSGMesh::MeshInternal::getPositionAttrName()) {
264 positionOffset = vbe.offset;
265 if (vbe.componentType != QSSGMesh::Mesh::ComponentType::Float32 &&
266 vbe.componentCount != 3)
267 return; // not a vec3, some weird stuff
268 } else if (vbe.name == QSSGMesh::MeshInternal::getNormalAttrName()) {
269 normalOffset = vbe.offset;
270 if (vbe.componentType != QSSGMesh::Mesh::ComponentType::Float32 &&
271 vbe.componentCount != 3)
272 return; // not a vec3, really weird normals I guess
273 }
274 }
275
276 const auto &globalTransform = theModelContext.globalTransform;
277 // Draw original vertex normals as blue lines
278 {
279 // Get Indexes
280 const quint32 *p = reinterpret_cast<const quint32 *>(indexData.constData());
281 const char *vp = vertexData.constData();
282 p += theSubset.offset;
283 for (uint i = 0; i < theSubset.count; ++i) {
284 const quint32 index = *(p + i);
285 const char * posPtr = vp + (index * vertexStride) + positionOffset;
286 const float *fPosPtr = reinterpret_cast<const float *>(posPtr);
287 QVector3D position(fPosPtr[0], fPosPtr[1], fPosPtr[2]);
288 const char * normalPtr = vp + (index * vertexStride) + normalOffset;
289 const float *fNormalPtr = reinterpret_cast<const float *>(normalPtr);
290 QVector3D normal(fNormalPtr[0], fNormalPtr[1], fNormalPtr[2]);
291 position = globalTransform.map(position);
292 normal = QSSGUtils::mat33::transform(theModelContext.normalMatrix, normal);
293 normal = normal.normalized();
294 drawLine(position, position + (normal * lineLength), QColor(Qt::blue));
295 }
296 }
297
298 // Draw lod vertex normals as red lines
299 if (subsetLevelOfDetail != 0) {
300 // Get Indexes
301 const quint32 *p = reinterpret_cast<const quint32 *>(indexData.constData());
302 const char *vp = vertexData.constData();
303 p += theSubset.lodOffset(subsetLevelOfDetail);
304 const quint32 indexCount = theSubset.lodCount(subsetLevelOfDetail);
305 for (uint i = 0; i < indexCount; ++i) {
306 const quint32 index = *(p + i);
307 const char * posPtr = vp + (index * vertexStride) + positionOffset;
308 const float *fPosPtr = reinterpret_cast<const float *>(posPtr);
309 QVector3D position(fPosPtr[0], fPosPtr[1], fPosPtr[2]);
310 const char * normalPtr = vp + (index * vertexStride) + normalOffset;
311 const float *fNormalPtr = reinterpret_cast<const float *>(normalPtr);
312 QVector3D normal(fNormalPtr[0], fNormalPtr[1], fNormalPtr[2]);
313 position = globalTransform.map(position);
314 normal = QSSGUtils::mat33::transform(theModelContext.normalMatrix, normal);
315 normal = normal.normalized();
316 drawLine(position, position + (normal * lineLength), QColor(Qt::red));
317 }
318 }
319}
320
321QT_END_NAMESPACE
Combined button and popup list for selecting options.