Qt 6.x
The Qt SDK
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
5
7
9{
10
11}
12
14{
15
16}
17
19{
20 return !m_lines.isEmpty() || !m_persistentLines.isEmpty() || !m_bounds.isEmpty() || !m_persistentBounds.isEmpty() || !m_persistentPoints.isEmpty() || !m_points.isEmpty();
21}
22
24 const QVector3D &endPoint,
25 const QColor &color,
26 bool isPersistent)
27{
28 LineData line = {startPoint, endPoint, color};
29 if (isPersistent)
30 m_persistentLines.append(line);
31 else
32 m_lines.append(line);
33}
34
36 const QColor &color,
37 bool isPersistent)
38{
39 BoundsData bound = {bounds, color};
40 if (isPersistent)
41 m_persistentBounds.append(bound);
42 else
43 m_bounds.append(bound);
44}
45
46void QSSGDebugDrawSystem::drawPoint(const QVector3D &vertex, const QColor &color, bool isPersistent)
47{
48 VertexData point = {vertex, {color.redF(), color.greenF(), color.blueF()}};
49 if (isPersistent)
50 m_persistentPoints.append(point);
51 else
52 m_points.append(point);
53}
54
55
57{
58
59 QVector<VertexData> vertexData;
60 QVector<quint32> indexData;
61 QVector<VertexData> pointsData;
62 for (const auto &line : m_persistentLines)
63 generateLine(line, vertexData, indexData);
64 for (const auto &line : m_lines)
65 generateLine(line, vertexData, indexData);
66 for (const auto &bounds : m_persistentBounds)
67 generateBox(bounds, vertexData, indexData);
68 for (const auto &bounds : m_bounds)
69 generateBox(bounds, vertexData, indexData);
70 pointsData = m_persistentPoints + m_points;
71
72 if (!vertexData.isEmpty()) {
73 // Lines
74 QByteArray vertexBufferData(reinterpret_cast<const char*>(vertexData.constData()), qsizetype(vertexData.count() * 6 * sizeof(float)));
75 QByteArray indexBufferData(reinterpret_cast<const char*>(indexData.constData()), qsizetype(indexData.count() * sizeof(quint32)));
76
77 if (m_lineVertexBuffer)
78 m_lineVertexBuffer.reset();
79 if (m_lineIndexBuffer)
80 m_lineIndexBuffer.reset();
81
82 m_lineVertexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
85 quint32(6 * sizeof(float)),
86 6 * sizeof(float) * vertexData.count());
87 m_lineVertexBuffer->buffer()->setName(QByteArrayLiteral("debug lines vertex buffer"));
88 rub->uploadStaticBuffer(m_lineVertexBuffer->buffer(), vertexBufferData.constData());
89
90 m_lineIndexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
93 0,
94 indexBufferData.size(),
96 m_lineIndexBuffer->buffer()->setName(QByteArrayLiteral("debug lines index buffer"));
97 rub->uploadStaticBuffer(m_lineIndexBuffer->buffer(), indexBufferData.constData());
98
99 m_indexSize = indexData.count();
100 }
101
102 if (!pointsData.isEmpty()) {
103 // Points
104 QByteArray vertexBufferData(reinterpret_cast<const char*>(pointsData.constData()), qsizetype(pointsData.count() * 6 * sizeof(float)));
105
106 if (m_pointVertexBuffer)
107 m_pointVertexBuffer.reset();
108
109 m_pointVertexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
112 quint32(6 * sizeof(float)),
113 vertexBufferData.size());
114 m_pointVertexBuffer->buffer()->setName(QByteArrayLiteral("debug points vertex buffer"));
115 rub->uploadStaticBuffer(m_pointVertexBuffer->buffer(), vertexBufferData.constData());
116 m_pointsSize = pointsData.count();
117 }
118}
119
124{
127 { 0, 1, QRhiVertexInputAttribute::Float3, 3 * sizeof(float) }
128 });
131 ps->ia.inputLayout.setBindings({6 * sizeof(float)});
133 ps->depthWriteEnable = true;
134 ps->depthTestEnable = true;
136
138 if (m_indexSize > 0) {
139 auto graphicsPipeline = rhiCtx->pipeline(QSSGGraphicsPipelineStateKey::create(*ps, rpDesc, srb), rpDesc, srb);
140 cb->setGraphicsPipeline(graphicsPipeline);
141 cb->setShaderResources(srb);
142 cb->setViewport(ps->viewport);
143
144 // Lines
145 QRhiCommandBuffer::VertexInput vb(m_lineVertexBuffer->buffer(), 0);
146 cb->setVertexInput(0, 1, &vb, m_lineIndexBuffer->buffer(), 0, m_lineIndexBuffer->indexFormat());
147 cb->drawIndexed(m_indexSize);
148 }
149
150 // Points
151 if (m_pointsSize > 0) {
153 auto graphicsPipeline = rhiCtx->pipeline(QSSGGraphicsPipelineStateKey::create(*ps, rpDesc, srb), rpDesc, srb);
154 cb->setGraphicsPipeline(graphicsPipeline);
155 cb->setShaderResources(srb);
156 cb->setViewport(ps->viewport);
157
158 QRhiCommandBuffer::VertexInput vb(m_pointVertexBuffer->buffer(), 0);
159 cb->setVertexInput(0, 1, &vb);
160 cb->draw(m_pointsSize);
161 }
162
163 m_lines.clear();
164 m_bounds.clear();
165 m_points.clear();
166 m_indexSize = 0;
167 m_pointsSize = 0;
168}
169
170void QSSGDebugDrawSystem::generateLine(const LineData &line, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
171{
172 const QVector3D color = {line.color.redF(), line.color.greenF(), line.color.blueF()};
173 indexArray.append(vertexArray.count());
174 vertexArray.append({line.startPoint, color});
175 indexArray.append(vertexArray.count());
176 vertexArray.append({line.endPoint, color});
177}
178
179void QSSGDebugDrawSystem::generateBox(const BoundsData &bounds, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
180{
181 const QVector3D color = {bounds.color.redF(), bounds.color.greenF(), bounds.color.blueF()};
182
183 quint32 offset = vertexArray.count();
184 for (const QVector3D point : bounds.bounds.toQSSGBoxPoints())
185 vertexArray.append({point, color});
186
187 indexArray.append(offset + 0);
188 indexArray.append(offset + 3);
189
190 indexArray.append(offset + 3);
191 indexArray.append(offset + 6);
192
193 indexArray.append(offset + 6);
194 indexArray.append(offset + 1);
195
196 indexArray.append(offset + 1);
197 indexArray.append(offset + 0);
198
199 indexArray.append(offset + 2);
200 indexArray.append(offset + 5);
201
202 indexArray.append(offset + 5);
203 indexArray.append(offset + 4);
204
205 indexArray.append(offset + 4);
206 indexArray.append(offset + 7);
207
208 indexArray.append(offset + 7);
209 indexArray.append(offset + 2);
210
211 indexArray.append(offset + 0);
212 indexArray.append(offset + 2);
213
214 indexArray.append(offset + 3);
215 indexArray.append(offset + 5);
216
217 indexArray.append(offset + 6);
218 indexArray.append(offset + 4);
219
220 indexArray.append(offset + 1);
221 indexArray.append(offset + 7);
222
223}
224
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
Definition qlist.h:74
const_pointer constData() const noexcept
Definition qlist.h:416
bool isEmpty() const noexcept
Definition qlist.h:390
qsizetype count() const noexcept
Definition qlist.h:387
void append(parameter_type t)
Definition qlist.h:441
@ Immutable
Definition qrhi.h:837
@ IndexBuffer
Definition qrhi.h:844
@ VertexBuffer
Definition qrhi.h:843
\inmodule QtGui
Definition qrhi.h:1614
QPair< QRhiBuffer *, quint32 > VertexInput
Synonym for QPair<QRhiBuffer *, quint32>.
Definition qrhi.h:1643
\inmodule QtGui
Definition qrhi.h:1119
\inmodule QtGui
Definition qrhi.h:1694
void uploadStaticBuffer(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
Enqueues updating a region of a QRhiBuffer buf created with the type QRhiBuffer::Immutable or QRhiBuf...
Definition qrhi.cpp:8615
\inmodule QtGui
Definition qrhi.h:1190
void setBindings(std::initializer_list< QRhiVertexInputBinding > list)
Sets the bindings from the specified list.
Definition qrhi.h:317
void setAttributes(std::initializer_list< QRhiVertexInputAttribute > list)
Sets the attributes from the specified list.
Definition qrhi.h:329
Class representing 3D range or axis aligned bounding box.
void drawLine(const QVector3D &startPoint, const QVector3D &endPoint, const QColor &color, bool isPersistent=false)
void recordRenderDebugObjects(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc)
void drawPoint(const QVector3D &vertex, const QColor &color, bool isPersistent=false)
void prepareGeometry(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
void drawBounds(const QSSGBounds3 &bounds, const QColor &color, bool isPersistent=false)
QRhiCommandBuffer * commandBuffer() const
QRhiGraphicsPipeline * pipeline(const QSSGGraphicsPipelineStateKey &key, QRhiRenderPassDescriptor *rpDesc, QRhiShaderResourceBindings *srb)
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
list append(new Employee("Blackpool", "Stephen"))
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
GLenum GLuint GLintptr offset
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
unsigned int quint32
Definition qtypes.h:45
ptrdiff_t qsizetype
Definition qtypes.h:70
static QSSGGraphicsPipelineStateKey create(const QSSGRhiGraphicsPipelineState &state, const QRhiRenderPassDescriptor *rpDesc, const QRhiShaderResourceBindings *srb)
QSSGRhiInputAssemblerState ia
QRhiGraphicsPipeline::CullMode cullMode
QVarLengthArray< InputSemantic, 8 > inputs
QRhiVertexInputLayout inputLayout
QRhiGraphicsPipeline::Topology topology