6#include "graphobjects/qssgrendermodel_p.h"
10#include "resourcemanager/qssgrenderbuffermanager_p.h"
11#include "rendererimpl/qssgrenderableobjects_p.h"
12#include "rendererimpl/qssglayerrenderdata_p.h"
16QSSGDebugDrawSystem::QSSGDebugDrawSystem()
21QSSGDebugDrawSystem::~QSSGDebugDrawSystem()
26bool QSSGDebugDrawSystem::hasContent()
const
28 return !m_lines.isEmpty() || !m_persistentLines.isEmpty() || !m_bounds.isEmpty() || !m_persistentBounds.isEmpty() || !m_persistentPoints.isEmpty() || !m_points.isEmpty();
31void QSSGDebugDrawSystem::drawLine(
const QVector3D &startPoint,
32 const QVector3D &endPoint,
36 LineData line = {startPoint, endPoint, color};
38 m_persistentLines.append(line);
43void QSSGDebugDrawSystem::drawBounds(
const QSSGBounds3 &bounds,
47 BoundsData bound = {bounds, color};
49 m_persistentBounds.append(bound);
51 m_bounds.append(bound);
54void QSSGDebugDrawSystem::drawPoint(
const QVector3D &vertex,
const QColor &color,
bool isPersistent)
56 VertexData point = {vertex, {color.redF(), color.greenF(), color.blueF()}};
58 m_persistentPoints.append(point);
60 m_points.append(point);
64void QSSGDebugDrawSystem::prepareGeometry(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
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;
80 if (!vertexData.isEmpty()) {
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)));
85 if (m_lineVertexBuffer)
86 m_lineVertexBuffer.reset();
87 if (m_lineIndexBuffer)
88 m_lineIndexBuffer.reset();
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());
98 m_lineIndexBuffer = std::make_shared<QSSGRhiBuffer>(*rhiCtx,
99 QRhiBuffer::Immutable,
100 QRhiBuffer::IndexBuffer,
102 indexBufferData.size(),
103 QRhiCommandBuffer::IndexUInt32);
104 m_lineIndexBuffer->buffer()->setName(QByteArrayLiteral(
"debug lines index buffer"));
105 rub->uploadStaticBuffer(m_lineIndexBuffer->buffer(), indexBufferData.constData());
107 m_indexSize = indexData.count();
110 if (!pointsData.isEmpty()) {
112 QByteArray vertexBufferData(
reinterpret_cast<
const char*>(pointsData.constData()), qsizetype(pointsData.count() * 6 *
sizeof(
float)));
114 if (m_pointVertexBuffer)
115 m_pointVertexBuffer.reset();
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();
128void QSSGDebugDrawSystem::recordRenderDebugObjects(QSSGRhiContext *rhiCtx,
129 QSSGRhiGraphicsPipelineState *ps,
130 QRhiShaderResourceBindings *srb,
131 QRhiRenderPassDescriptor *rpDesc)
133 auto &ia = QSSGRhiInputAssemblerStatePrivate::get(*ps);
134 ia.inputLayout.setAttributes({
135 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
136 { 0, 1, QRhiVertexInputAttribute::Float3, 3 *
sizeof(
float) }
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;
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);
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);
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);
168 QRhiCommandBuffer::VertexInput vb(m_pointVertexBuffer->buffer(), 0);
169 cb->setVertexInput(0, 1, &vb);
170 cb->draw(m_pointsSize);
180void QSSGDebugDrawSystem::setEnabled(
bool v)
182 modes = v ? (modes | ModeFlagT(Mode::Other)) : (modes & ~ModeFlagT(Mode::Other));
185void QSSGDebugDrawSystem::generateLine(
const LineData &line, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
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});
194void QSSGDebugDrawSystem::generateBox(
const BoundsData &bounds, QVector<VertexData> &vertexArray, QVector<quint32> &indexArray)
196 const QVector3D color = {bounds.color.redF(), bounds.color.greenF(), bounds.color.blueF()};
198 quint32 offset = vertexArray.count();
199 for (
const QVector3D point : bounds.bounds.toQSSGBoxPoints())
200 vertexArray.append({point, color});
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]);
212QColor QSSGDebugDrawSystem::levelOfDetailColor(quint32 lod)
214 static const QColor colors[] {
223 QColor(Qt::darkGreen),
224 QColor(Qt::darkBlue),
225 QColor(Qt::darkCyan),
226 QColor(Qt::darkMagenta),
227 QColor(Qt::darkYellow),
231 const size_t idx = qBound<size_t>(0, lod, std::size(colors) - 1);
235void QSSGDebugDrawSystem::debugNormals(QSSGBufferManager &bufferManager,
const QSSGModelContext &theModelContext,
const QSSGRenderSubset &theSubset, quint32 subsetLevelOfDetail,
float lineLength)
237 const auto &model = theModelContext.model;
241 mesh = bufferManager.loadMeshData(model.geometry);
243 mesh = bufferManager.loadMeshData(model.meshPath);
248 QByteArray vertexData = mesh.vertexBuffer().data;
249 if (vertexData.isEmpty())
251 quint32 vertexStride = mesh.vertexBuffer().stride;
252 QByteArray indexData = mesh.indexBuffer().data;
253 if (indexData.isEmpty())
255 if (mesh.indexBuffer().componentType != QSSGMesh::Mesh::ComponentType::UnsignedInt32)
258 quint32 positionOffset = UINT_MAX;
259 quint32 normalOffset = UINT_MAX;
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)
268 }
else if (vbe.name == QSSGMesh::MeshInternal::getNormalAttrName()) {
269 normalOffset = vbe.offset;
270 if (vbe.componentType != QSSGMesh::Mesh::ComponentType::Float32 &&
271 vbe.componentCount != 3)
276 const auto &globalTransform = theModelContext.globalTransform;
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));
299 if (subsetLevelOfDetail != 0) {
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));
Combined button and popup list for selecting options.