9#include <QtCore/qthreadpool.h>
12#include <QtGui/qvector2d.h>
13#include <QtGui/qvector4d.h>
14#include <QtGui/private/qtriangulator_p.h>
15#include <QtGui/private/qtriangulatingstroker_p.h>
16#include <QtGui/private/qrhi_p.h>
18#include <QtQuick/private/qsgcurvefillnode_p.h>
19#include <QtQuick/private/qsgcurvestrokenode_p.h>
20#include <QtQuick/private/qquadpath_p.h>
21#include <QtQuick/private/qsgcurveprocessor_p.h>
22#include <QtQuick/qsgmaterial.h>
26Q_LOGGING_CATEGORY(lcShapeCurveRenderer,
"qt.shape.curverenderer");
31
32
33
34
35enum WireFrameType { SimpleWFT, StrokeWFT };
37class QQuickShapeWireFrameMaterialShader :
public QSGMaterialShader
40 QQuickShapeWireFrameMaterialShader(WireFrameType wft,
int viewCount) : m_wftype(wft)
42 setShaderFileName(VertexStage, wft == StrokeWFT ?
43 QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/shapestroke_wireframe.vert.qsb") :
44 QStringLiteral(
":/qt-project.org/shapes/shaders_ng/wireframe.vert.qsb"), viewCount);
45 setShaderFileName(FragmentStage,
47 QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/shapestroke_wireframe.frag.qsb") :
48 QStringLiteral(
":/qt-project.org/shapes/shaders_ng/wireframe.frag.qsb"), viewCount);
51 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
override;
53 WireFrameType m_wftype;
56class QQuickShapeWireFrameMaterial :
public QSGMaterial
59 QQuickShapeWireFrameMaterial(WireFrameType wft) : m_wftype(wft)
61 setFlag(Blending,
true);
64 int compare(
const QSGMaterial *other)
const override
66 return (type() - other->type());
69 void setCosmeticStroke(
bool c)
74 void setStrokeWidth(
float width)
76 m_strokeWidth = width;
81 return (m_cosmeticStroke ? -1.0 : 1.0) * qAbs(m_strokeWidth);
85 QSGMaterialType *type()
const override
87 static QSGMaterialType t;
90 QSGMaterialShader *createShader(QSGRendererInterface::RenderMode)
const override
92 return new QQuickShapeWireFrameMaterialShader(m_wftype, viewCount());
95 WireFrameType m_wftype;
96 bool m_cosmeticStroke =
false;
97 float m_strokeWidth = 1.0f;
100bool QQuickShapeWireFrameMaterialShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
102 QByteArray *buf = state.uniformData();
103 Q_ASSERT(buf->size() >= 64);
104 const int matrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
105 bool changed =
false;
106 float localScale = 1.0f;
108 for (
int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
109 if (state.isMatrixDirty()) {
110 QMatrix4x4 m = state.combinedMatrix(viewIndex);
111 if (m_wftype == StrokeWFT)
113 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
118 const float matrixScale = qSqrt(qAbs(state.determinant())) * state.devicePixelRatio() * localScale;
119 memcpy(buf->data() + matrixCount * 64, &matrixScale, 4);
120 const float dpr = state.devicePixelRatio();
121 memcpy(buf->data() + matrixCount * 64 + 8, &dpr, 4);
122 const float opacity = 1.0;
123 memcpy(buf->data() + matrixCount * 64 + 4, &opacity, 4);
124 const float strokeWidth =
static_cast<QQuickShapeWireFrameMaterial *>(newMaterial)->strokeWidth();
125 memcpy(buf->data() + matrixCount * 64 + 12, &strokeWidth, 4);
132template <WireFrameType wftype>
133class QQuickShapeWireFrameNode :
public QSGCurveAbstractNode
136 struct WireFrameVertex
138 float x, y, u, v, w, nx, ny, sw;
141 QQuickShapeWireFrameNode()
144 setFlag(OwnsGeometry,
true);
145 setGeometry(
new QSGGeometry(attributes(), 0, 0));
149 void setColor(QColor col)
override
154 void setUseStandardDerivatives(
bool useStandardDerivatives) override
156 Q_UNUSED(useStandardDerivatives);
159 void setCosmeticStroke(
bool c)
161 m_material->setCosmeticStroke(c);
164 void setStrokeWidth(
float width)
166 m_material->setStrokeWidth(width);
169 void activateMaterial()
171 m_material.reset(
new QQuickShapeWireFrameMaterial(wftype));
172 setMaterial(m_material.data());
175 static const QSGGeometry::AttributeSet &attributes()
177 static QSGGeometry::Attribute data[] = {
178 QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType, QSGGeometry::PositionAttribute),
179 QSGGeometry::Attribute::createWithAttributeType(1, 3, QSGGeometry::FloatType, QSGGeometry::TexCoordAttribute),
180 QSGGeometry::Attribute::createWithAttributeType(2, 3, QSGGeometry::FloatType, QSGGeometry::TexCoordAttribute),
182 static QSGGeometry::AttributeSet attrs = { 3,
sizeof(WireFrameVertex), data };
186 void cookGeometry() override
192 QScopedPointer<QQuickShapeWireFrameMaterial> m_material;
196QQuickShapeCurveRenderer::~QQuickShapeCurveRenderer()
198 for (
const PathData &pd : std::as_const(m_paths)) {
199 if (pd.currentRunner) {
200 pd.currentRunner->orphaned =
true;
201 if (!pd.currentRunner->isAsync || pd.currentRunner->isDone)
202 delete pd.currentRunner;
207void QQuickShapeCurveRenderer::beginSync(
int totalCount,
bool *countChanged)
209 if (countChanged !=
nullptr && totalCount != m_paths.size())
210 *countChanged =
true;
211 for (
int i = totalCount; i < m_paths.size(); i++) {
212 setFillTextureProvider(i,
nullptr);
213 m_removedPaths.append(m_paths.at(i));
215 m_paths.resize(totalCount);
218void QQuickShapeCurveRenderer::setPath(
int index,
const QPainterPath &path, QQuickShapePath::PathHints pathHints)
220 auto &pathData = m_paths[index];
221 pathData.originalPath = path;
222 pathData.pathHints = pathHints;
223 pathData.m_dirty |= PathDirty;
226void QQuickShapeCurveRenderer::setStrokeColor(
int index,
const QColor &color)
228 auto &pathData = m_paths[index];
229 const bool wasVisible = pathData.isStrokeVisible();
230 pathData.pen.setColor(color);
231 if (pathData.isStrokeVisible() != wasVisible)
232 pathData.m_dirty |= StrokeDirty;
234 pathData.m_dirty |= UniformsDirty;
237void QQuickShapeCurveRenderer::setStrokeWidth(
int index, qreal w)
239 auto &pathData = m_paths[index];
241 pathData.validPenWidth =
true;
242 pathData.pen.setWidthF(w);
244 pathData.validPenWidth =
false;
246 pathData.m_dirty |= StrokeDirty;
249void QQuickShapeCurveRenderer::setCosmeticStroke(
int index,
bool c)
251 auto &pathData = m_paths[index];
252 pathData.pen.setCosmetic(c);
253 pathData.m_dirty |= StrokeDirty;
256void QQuickShapeCurveRenderer::setFillColor(
int index,
const QColor &color)
258 auto &pathData = m_paths[index];
259 const bool wasVisible = pathData.isFillVisible();
260 pathData.fillColor = color;
261 if (pathData.isFillVisible() != wasVisible)
262 pathData.m_dirty |= FillDirty;
264 pathData.m_dirty |= UniformsDirty;
267void QQuickShapeCurveRenderer::setFillRule(
int index, QQuickShapePath::FillRule fillRule)
269 auto &pathData = m_paths[index];
270 pathData.fillRule = Qt::FillRule(fillRule);
271 pathData.m_dirty |= PathDirty;
274void QQuickShapeCurveRenderer::setJoinStyle(
int index,
275 QQuickShapePath::JoinStyle joinStyle,
278 auto &pathData = m_paths[index];
279 pathData.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
280 pathData.pen.setMiterLimit(miterLimit);
281 pathData.m_dirty |= StrokeDirty;
284void QQuickShapeCurveRenderer::setCapStyle(
int index, QQuickShapePath::CapStyle capStyle)
286 auto &pathData = m_paths[index];
287 pathData.pen.setCapStyle(Qt::PenCapStyle(capStyle));
288 pathData.m_dirty |= StrokeDirty;
291void QQuickShapeCurveRenderer::setStrokeStyle(
int index,
292 QQuickShapePath::StrokeStyle strokeStyle,
294 const QList<qreal> &dashPattern)
296 auto &pathData = m_paths[index];
297 pathData.pen.setStyle(Qt::PenStyle(strokeStyle));
298 if (strokeStyle == QQuickShapePath::DashLine) {
299 pathData.pen.setDashPattern(dashPattern);
300 pathData.pen.setDashOffset(dashOffset);
302 pathData.m_dirty |= StrokeDirty;
306 QSGGradientCache::GradientDesc *dst)
308 QGradient::Type gradientType = QGradient::NoGradient;
309 if (
const QQuickShapeLinearGradient *g = qobject_cast<
const QQuickShapeLinearGradient *>(gradient)) {
310 gradientType = QGradient::LinearGradient;
311 dst->a = QPointF(g->x1(), g->y1());
312 dst->b = QPointF(g->x2(), g->y2());
313 }
else if (
const QQuickShapeRadialGradient *g = qobject_cast<
const QQuickShapeRadialGradient *>(gradient)) {
314 gradientType = QGradient::RadialGradient;
315 dst->a = QPointF(g->centerX(), g->centerY());
316 dst->b = QPointF(g->focalX(), g->focalY());
317 dst->v0 = g->centerRadius();
318 dst->v1 = g->focalRadius();
319 }
else if (
const QQuickShapeConicalGradient *g = qobject_cast<
const QQuickShapeConicalGradient *>(gradient)) {
320 gradientType = QGradient::ConicalGradient;
321 dst->a = QPointF(g->centerX(), g->centerY());
322 dst->v0 = g->angle();
323 }
else if (gradient !=
nullptr) {
324 static bool warned =
false;
327 qCWarning(lcShapeCurveRenderer) <<
"Unsupported gradient";
331 if (gradientType != QGradient::NoGradient) {
332 dst->stops = gradient->gradientStops();
333 dst->spread = QGradient::Spread(gradient->spread());
339void QQuickShapeCurveRenderer::setFillGradient(
int index, QQuickShapeGradient *gradient)
341 PathData &pd(m_paths[index]);
342 const bool wasVisible = pd.isFillVisible();
343 pd.gradientType = copyCurveGradient(gradient, &pd.gradient);
344 pd.m_dirty |= (pd.isFillVisible() != wasVisible) ? FillDirty : UniformsDirty;
347void QQuickShapeCurveRenderer::setStrokeGradient(
int index, QQuickShapeGradient *gradient)
349 PathData &pd(m_paths[index]);
350 const bool wasVisible = pd.isStrokeVisible();
351 pd.strokeGradientType = copyCurveGradient(gradient, &pd.strokeGradient);
352 pd.m_dirty |= (pd.isStrokeVisible() != wasVisible) ? StrokeDirty : UniformsDirty;
355void QQuickShapeCurveRenderer::setFillTransform(
int index,
const QSGTransform &transform)
357 auto &pathData = m_paths[index];
358 pathData.fillTransform = transform;
359 pathData.m_dirty |= UniformsDirty;
362void QQuickShapeCurveRenderer::setFillTextureProvider(
int index, QQuickItem *textureProviderItem)
364 auto &pathData = m_paths[index];
365 const bool wasVisible = pathData.isFillVisible();
366 if (pathData.fillTextureProviderItem !=
nullptr)
367 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->derefWindow();
368 pathData.fillTextureProviderItem = textureProviderItem;
369 if (pathData.fillTextureProviderItem !=
nullptr)
370 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->refWindow(m_item->window());
371 pathData.m_dirty |= (pathData.isFillVisible() != wasVisible) ? FillDirty : UniformsDirty;
374void QQuickShapeCurveRenderer::handleSceneChange(QQuickWindow *window)
376 for (
auto &pathData : m_paths) {
377 if (pathData.fillTextureProviderItem !=
nullptr) {
378 if (window ==
nullptr)
379 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->derefWindow();
381 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->refWindow(window);
386void QQuickShapeCurveRenderer::setAsyncCallback(
void (*callback)(
void *),
void *data)
388 m_asyncCallback = callback;
389 m_asyncCallbackData = data;
392void QQuickShapeCurveRenderer::endSync(
bool async)
394 bool asyncThreadsRunning =
false;
396 for (PathData &pathData : m_paths) {
397 if (!pathData.m_dirty)
400 if (pathData.m_dirty == UniformsDirty) {
405 if (pathData.currentRunner) {
408 if (pathData.currentRunner->isAsync) {
411 asyncThreadsRunning =
true;
415 delete pathData.currentRunner;
416 pathData.currentRunner =
nullptr;
420 pathData.currentRunner =
new QQuickShapeCurveRunnable;
421 setUpRunner(&pathData);
425 pathData.currentRunner->isAsync =
true;
426 QThreadPool::globalInstance()->start(pathData.currentRunner);
427 asyncThreadsRunning =
true;
431 pathData.currentRunner->run();
435 if (async && !asyncThreadsRunning && m_asyncCallback)
436 m_asyncCallback(m_asyncCallbackData);
439void QQuickShapeCurveRenderer::setUpRunner(PathData *pathData)
441 Q_ASSERT(pathData->currentRunner);
442 QQuickShapeCurveRunnable *runner = pathData->currentRunner;
443 runner->isDone =
false;
444 runner->pathData = *pathData;
445 runner->pathData.fillNodes.clear();
446 runner->pathData.strokeNodes.clear();
447 runner->pathData.currentRunner =
nullptr;
448 pathData->m_dirty = 0;
449 if (!runner->isInitialized) {
450 runner->isInitialized =
true;
451 runner->setAutoDelete(
false);
452 QObject::connect(runner, &QQuickShapeCurveRunnable::done, qApp,
453 [
this](QQuickShapeCurveRunnable *r) {
457 }
else if (r->isAsync) {
458 maybeUpdateAsyncItem();
464void QQuickShapeCurveRenderer::maybeUpdateAsyncItem()
466 for (
const PathData &pd : std::as_const(m_paths)) {
467 if (pd.currentRunner && !pd.currentRunner->isDone)
473 m_asyncCallback(m_asyncCallbackData);
478 qDeleteAll(pathData.fillNodes);
479 qDeleteAll(pathData.strokeNodes);
484 QQuickShapeCurveRenderer::processPath(&pathData);
490 static bool d = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DISABLE_STANDARD_DERIVATIVES") != 0;
494void QQuickShapeCurveRenderer::updateNode()
499 auto updateUniforms = [](
const PathData &pathData) {
500 for (
auto &pathNode : std::as_const(pathData.fillNodes)) {
501 if (pathNode->isDebugNode)
503 QSGCurveFillNode *fillNode =
static_cast<QSGCurveFillNode *>(pathNode);
504 fillNode->setColor(pathData.fillColor);
505 fillNode->setGradientType(pathData.gradientType);
506 fillNode->setFillGradient(pathData.gradient);
507 fillNode->setFillTransform(pathData.fillTransform);
508 fillNode->setFillTextureProvider(pathData.fillTextureProviderItem !=
nullptr
509 ? pathData.fillTextureProviderItem->textureProvider()
512 for (QSGCurveAbstractNode *pathNode : std::as_const(pathData.strokeNodes)) {
513 pathNode->setColor(pathData.pen.color());
514 if (pathNode->isDebugNode) {
515 auto *wfNode =
static_cast<QQuickShapeWireFrameNode<StrokeWFT> *>(pathNode);
516 wfNode->setStrokeWidth(pathData.pen.widthF());
517 wfNode->setCosmeticStroke(pathData.pen.isCosmetic());
519 auto *strokeNode =
static_cast<QSGCurveStrokeNode *>(pathNode);
520 strokeNode->setStrokeWidth(pathData.pen.widthF());
521 strokeNode->setCosmeticStroke(pathData.pen.isCosmetic());
522 strokeNode->setStrokeGradient(pathData.strokeGradient);
523 strokeNode->setGradientType(pathData.strokeGradientType);
528 NodeList toBeDeleted;
530 for (
const PathData &pathData : std::as_const(m_removedPaths)) {
531 toBeDeleted += pathData.fillNodes;
532 toBeDeleted += pathData.strokeNodes;
534 m_removedPaths.clear();
536 const bool supportsDerivatives = m_item !=
nullptr
537 && m_item->window() !=
nullptr
538 && m_item->window()->rhi() !=
nullptr
539 && !disableScreenSpaceDerivativeShader()
540 ? m_item->window()->rhi()->isFeatureSupported(QRhi::ScreenSpaceDerivatives)
543 for (
int i = 0; i < m_paths.size(); i++) {
544 PathData &pathData = m_paths[i];
545 if (pathData.currentRunner) {
546 if (!pathData.currentRunner->isDone)
549 QSGNode *nextNode = pathData.strokeNodes.value(0);
551 for (
int j = i + 1; !nextNode && j < m_paths.size(); j++) {
552 const PathData &pd = m_paths[j];
553 nextNode = pd.fillNodes.isEmpty() ? pd.strokeNodes.value(0) : pd.fillNodes.value(0);
556 PathData &newData = pathData.currentRunner->pathData;
557 if (newData.m_dirty & PathDirty)
558 pathData.path = newData.path;
559 if (newData.m_dirty & FillDirty) {
560 pathData.fillPath = newData.fillPath;
561 for (
auto *node : std::as_const(newData.fillNodes)) {
562 node->setUseStandardDerivatives(supportsDerivatives);
564 m_rootNode->insertChildNodeBefore(node, nextNode);
566 m_rootNode->appendChildNode(node);
568 toBeDeleted += pathData.fillNodes;
569 pathData.fillNodes = newData.fillNodes;
571 if (newData.m_dirty & StrokeDirty) {
572 for (
auto *node : std::as_const(newData.strokeNodes)) {
573 node->setUseStandardDerivatives(supportsDerivatives);
575 m_rootNode->insertChildNodeBefore(node, nextNode);
577 m_rootNode->appendChildNode(node);
579 toBeDeleted += pathData.strokeNodes;
580 pathData.strokeNodes = newData.strokeNodes;
582 if (newData.m_dirty & UniformsDirty)
583 updateUniforms(newData);
586 newData.fillNodes.clear();
587 newData.strokeNodes.clear();
590 if (pathData.currentRunner->isAsync && (pathData.m_dirty & ~UniformsDirty)) {
592 setUpRunner(&pathData);
593 QThreadPool::globalInstance()->start(pathData.currentRunner);
597 pathData.currentRunner->deleteLater();
598 pathData.currentRunner =
nullptr;
602 if (pathData.m_dirty == UniformsDirty && !pathData.currentRunner) {
604 updateUniforms(pathData);
605 pathData.m_dirty = 0;
608 qDeleteAll(toBeDeleted);
611void QQuickShapeCurveRenderer::processPath(PathData *pathData)
613 static const bool doOverlapSolving = !qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DISABLE_OVERLAP_SOLVER");
614 static const bool doIntersetionSolving = !qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DISABLE_INTERSECTION_SOLVER");
615 static const bool useTriangulatingStroker = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_TRIANGULATING_STROKER");
616 static const bool simplifyPath = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_SIMPLIFY_PATHS");
618 int &dirtyFlags = pathData->m_dirty;
620 if (dirtyFlags & PathDirty) {
622 pathData->path = QQuadPath::fromPainterPath(pathData->originalPath.simplified(), QQuadPath::PathLinear | QQuadPath::PathNonIntersecting | QQuadPath::PathNonOverlappingControlPointTriangles);
624 pathData->path = QQuadPath::fromPainterPath(pathData->originalPath, QQuadPath::PathHints(
int(pathData->pathHints)));
625 pathData->path.setFillRule(pathData->fillRule);
626 pathData->fillPath = {};
627 dirtyFlags |= (FillDirty | StrokeDirty);
630 if (dirtyFlags & FillDirty) {
631 if (pathData->isFillVisible()) {
632 if (pathData->fillPath.isEmpty()) {
633 pathData->fillPath = pathData->path.subPathsClosed();
634 if (doIntersetionSolving)
635 QSGCurveProcessor::solveIntersections(pathData->fillPath);
636 pathData->fillPath.addCurvatureData();
637 if (doOverlapSolving)
638 QSGCurveProcessor::solveOverlaps(pathData->fillPath);
640 pathData->fillNodes = addFillNodes(pathData->fillPath);
641 dirtyFlags |= (StrokeDirty | UniformsDirty);
645 if (dirtyFlags & StrokeDirty) {
646 if (pathData->isStrokeVisible()) {
647 const QPen &pen = pathData->pen;
648 const bool solid = (pen.style() == Qt::SolidLine);
649 const QQuadPath &strokePath = solid ? pathData->path
650 : pathData->path.dashed(pen.widthF(),
653 if (useTriangulatingStroker)
654 pathData->strokeNodes = addTriangulatingStrokerNodes(strokePath, pen);
656 pathData->strokeNodes = addCurveStrokeNodes(strokePath, pen);
657 dirtyFlags |= UniformsDirty;
662QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addFillNodes(
const QQuadPath &path)
665 std::unique_ptr<QSGCurveFillNode> node(
new QSGCurveFillNode);
666 std::unique_ptr<QQuickShapeWireFrameNode<SimpleWFT>> wfNode;
668 const qsizetype approxDataCount = 20 * path.elementCount();
669 node->reserve(approxDataCount);
671 const int debugFlags = debugVisualization();
672 const bool wireFrame = debugFlags & DebugWireframe;
674 if (Q_LIKELY(!wireFrame)) {
675 QSGCurveProcessor::processFill(path,
677 [&node](
const std::array<QVector2D, 3> &v,
678 const std::array<QVector2D, 3> &n,
679 QSGCurveProcessor::uvForPointCallback uvForPoint)
681 node->appendTriangle(v, n, uvForPoint);
684 QList<QQuickShapeWireFrameNode<SimpleWFT>::WireFrameVertex> wfVertices;
685 wfVertices.reserve(approxDataCount);
686 QSGCurveProcessor::processFill(path,
688 [&wfVertices, &node](
const std::array<QVector2D, 3> &v,
689 const std::array<QVector2D, 3> &n,
690 QSGCurveProcessor::uvForPointCallback uvForPoint)
692 node->appendTriangle(v, n, uvForPoint);
694 wfVertices.append({v.at(0).x(), v.at(0).y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
695 wfVertices.append({v.at(1).x(), v.at(1).y(), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
696 wfVertices.append({v.at(2).x(), v.at(2).y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f });
699 wfNode.reset(
new QQuickShapeWireFrameNode<SimpleWFT>);
700 const QList<quint32> indices = node->uncookedIndexes();
701 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<SimpleWFT>::attributes(),
704 QSGGeometry::UnsignedIntType);
705 wfNode->setGeometry(wfg);
707 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
708 memcpy(wfg->indexData(),
710 indices.size() * wfg->sizeOfIndex());
711 memcpy(wfg->vertexData(),
713 wfg->vertexCount() * wfg->sizeOfVertex());
716 if (Q_UNLIKELY(debugFlags & DebugCurves))
717 node->setDebug(0.5f);
719 if (node->uncookedIndexes().size() > 0) {
720 node->cookGeometry();
721 ret.append(node.release());
723 ret.append(wfNode.release());
729QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addTriangulatingStrokerNodes(
const QQuadPath &path,
const QPen &pen)
732 const QColor &color = pen.color();
734 QList<QQuickShapeWireFrameNode<StrokeWFT>::WireFrameVertex> wfVertices;
736 QTriangulatingStroker stroker;
737 const auto painterPath = path.toPainterPath();
738 const QVectorPath &vp = qtVectorPathForPath(painterPath);
739 stroker.process(vp, pen, {}, {});
741 auto *node =
new QSGCurveFillNode;
743 auto uvForPoint = [](QVector2D v1, QVector2D v2, QVector2D p)
745 double divisor = v1.x() * v2.y() - v2.x() * v1.y();
747 float u = (p.x() * v2.y() - p.y() * v2.x()) / divisor;
748 float v = (p.y() * v1.x() - p.x() * v1.y()) / divisor;
750 return QVector2D(u, v);
755 auto curveUv = [uvForPoint](QVector2D p0, QVector2D p1, QVector2D p2, QVector2D p)
757 QVector2D v1 = 2 * (p1 - p0);
758 QVector2D v2 = p2 - v1 - p0;
759 return uvForPoint(v1, v2, p - p0);
762 auto findPointOtherSide = [](
const QVector2D &startPoint,
const QVector2D &endPoint,
const QVector2D &referencePoint){
764 QVector2D baseLine = endPoint - startPoint;
765 QVector2D insideVector = referencePoint - startPoint;
766 QVector2D normal = QVector2D(-baseLine.y(), baseLine.x());
768 bool swap = QVector2D::dotProduct(insideVector, normal) < 0;
770 return swap ? startPoint + normal : startPoint - normal;
773 static bool disableExtraTriangles = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_WIP_DISABLE_EXTRA_STROKE_TRIANGLES");
775 auto addStrokeTriangle = [&](
const QVector2D &p1,
const QVector2D &p2,
const QVector2D &p3){
776 if (p1 == p2 || p2 == p3) {
780 auto uvForPoint = [&p1, &p2, &p3, curveUv](QVector2D p) {
781 auto uv = curveUv(p1, p2, p3, p);
782 return QVector3D(uv.x(), uv.y(), 0.0f);
785 node->appendTriangle(p1, p2, p3, uvForPoint);
788 wfVertices.append({p1.x(), p1.y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
789 wfVertices.append({p2.x(), p2.y(), 0.0f, 0.1f, 0.0f, 0.0f, 0.0f, 1.0f});
790 wfVertices.append({p3.x(), p3.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f});
792 if (!disableExtraTriangles) {
795 QVector2D op = findPointOtherSide(p1, p3, p2);
796 node->appendTriangle(p1, op, p3, uvForPoint);
798 wfVertices.append({p1.x(), p1.y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
799 wfVertices.append({op.x(), op.y(), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f});
800 wfVertices.append({p3.x(), p3.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f});
804 const int vertCount = stroker.vertexCount() / 2;
805 const float *verts = stroker.vertices();
806 for (
int i = 0; i < vertCount - 2; ++i) {
808 for (
int j = 0; j < 3; ++j) {
809 p[j] = QVector2D(verts[(i+j)*2], verts[(i+j)*2 + 1]);
811 addStrokeTriangle(p[0], p[1], p[2]);
814 QList<quint32> indices = node->uncookedIndexes();
815 if (indices.size() > 0) {
816 node->setColor(color);
818 node->cookGeometry();
821 const bool wireFrame = debugVisualization() & DebugWireframe;
823 QQuickShapeWireFrameNode<StrokeWFT> *wfNode =
new QQuickShapeWireFrameNode<StrokeWFT>;
824 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<StrokeWFT>::attributes(),
827 QSGGeometry::UnsignedIntType);
828 wfNode->setGeometry(wfg);
830 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
831 memcpy(wfg->indexData(),
833 indices.size() * wfg->sizeOfIndex());
834 memcpy(wfg->vertexData(),
836 wfg->vertexCount() * wfg->sizeOfVertex());
844void QQuickShapeCurveRenderer::setRootNode(QSGNode *node)
846 clearNodeReferences();
850void QQuickShapeCurveRenderer::clearNodeReferences()
852 for (PathData &pd : m_paths) {
853 pd.fillNodes.clear();
854 pd.strokeNodes.clear();
858int QQuickShapeCurveRenderer::debugVisualizationFlags = QQuickShapeCurveRenderer::NoDebug;
860int QQuickShapeCurveRenderer::debugVisualization()
862 static const int envFlags = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DEBUG");
863 return debugVisualizationFlags | envFlags;
866void QQuickShapeCurveRenderer::setDebugVisualization(
int options)
868 if (debugVisualizationFlags == options)
870 debugVisualizationFlags = options;
874
875
876
877
878QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addCurveStrokeNodes(
const QQuadPath &path,
const QPen &pen)
882 const bool debug = debugVisualization() & DebugCurves;
883 auto *node =
new QSGCurveStrokeNode;
884 node->setDebug(0.2f * debug);
885 QList<QQuickShapeWireFrameNode<StrokeWFT>::WireFrameVertex> wfVertices;
887 const float penWidth = pen.widthF();
889 static const int subdivisions = qEnvironmentVariable(
"QT_QUICKSHAPES_STROKE_SUBDIVISIONS", QStringLiteral(
"3")).toInt();
891 const bool wireFrame = debugVisualization() & DebugWireframe;
892 QSGCurveProcessor::processStroke(path,
894 penWidth, pen.isCosmetic(),
898 [&wfVertices, &node, &wireFrame](
const std::array<QVector2D, 3> &vtx,
899 const std::array<QVector2D, 3> &ctl,
900 const std::array<QVector2D, 3> &n,
901 const std::array<
float, 3> &ex,
902 QSGCurveStrokeNode::TriangleFlags flags)
904 const QVector2D &v0 = vtx.at(0);
905 const QVector2D &v1 = vtx.at(1);
906 const QVector2D &v2 = vtx.at(2);
907 if (flags.testFlag(QSGCurveStrokeNode::TriangleFlag::Line))
908 node->appendTriangle(vtx, std::array<QVector2D, 2>{ctl.at(0), ctl.at(2)}, n, ex);
910 node->appendTriangle(vtx, ctl, n, ex);
912 if (Q_UNLIKELY(wireFrame)) {
913 wfVertices.append({v0.x(), v0.y(), 1.0f, 0.0f, 0.0f, n.at(0).x(), n.at(0).y(), ex.at(0)});
914 wfVertices.append({v1.x(), v1.y(), 0.0f, 1.0f, 0.0f, n.at(1).x(), n.at(1).y(), ex.at(1)});
915 wfVertices.append({v2.x(), v2.y(), 0.0f, 0.0f, 1.0f, n.at(2).x(), n.at(2).y(), ex.at(2)});
920 auto indexCopy = node->uncookedIndexes();
922 node->setColor(pen.color());
923 node->setStrokeWidth(penWidth);
924 node->setCosmeticStroke(pen.isCosmetic());
925 node->cookGeometry();
928 if (Q_UNLIKELY(wireFrame)) {
929 QQuickShapeWireFrameNode<StrokeWFT> *wfNode =
new QQuickShapeWireFrameNode<StrokeWFT>;
931 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<StrokeWFT>::attributes(),
934 QSGGeometry::UnsignedIntType);
935 wfNode->setGeometry(wfg);
936 wfNode->setCosmeticStroke(pen.isCosmetic());
937 wfNode->setStrokeWidth(penWidth);
939 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
940 memcpy(wfg->indexData(),
942 indexCopy.size() * wfg->sizeOfIndex());
943 memcpy(wfg->vertexData(),
945 wfg->vertexCount() * wfg->sizeOfVertex());
void run() override
Implement this pure virtual function in your subclass.
Combined button and popup list for selecting options.
static bool disableScreenSpaceDerivativeShader()
static QGradient::Type copyCurveGradient(const QQuickShapeGradient *gradient, QSGGradientCache::GradientDesc *dst)