using QSGVertexColorMaterial for monochrome gradients, when requested by

QskFillNode::PreferColoredGeometry ( = default ). might be useful for
batching
This commit is contained in:
Uwe Rathmann 2024-09-25 15:51:01 +02:00
parent ae9bc90d38
commit 283afee43a

View File

@ -6,6 +6,7 @@
#include "QskShapeNode.h" #include "QskShapeNode.h"
#include "QskGradient.h" #include "QskGradient.h"
#include "QskGradientDirection.h" #include "QskGradientDirection.h"
#include "QskVertex.h"
#include "QskFillNodePrivate.h" #include "QskFillNodePrivate.h"
QSK_QT_PRIVATE_BEGIN QSK_QT_PRIVATE_BEGIN
@ -26,7 +27,7 @@ static void qskUpdateGeometry( const QPainterPath& path,
auto vertexData = reinterpret_cast< float* >( geometry.vertexData() ); auto vertexData = reinterpret_cast< float* >( geometry.vertexData() );
const auto points = ts.vertices.constData(); const auto points = ts.vertices.constData();
for ( int i = 0; i < ts.vertices.count(); i++ ) for ( int i = 0; i < ts.vertices.size(); i++ )
vertexData[i] = points[i]; vertexData[i] = points[i];
memcpy( geometry.indexData(), ts.indices.data(), memcpy( geometry.indexData(), ts.indices.data(),
@ -36,13 +37,13 @@ static void qskUpdateGeometry( const QPainterPath& path,
#endif #endif
static void qskUpdateGeometry( const QPainterPath& path, static void qskUpdateGeometry( const QPainterPath& path,
const QTransform& transform, QSGGeometry& geometry ) const QTransform& transform, const QColor& color, QSGGeometry& geometry )
{ {
const auto ts = qTriangulate( path, transform, 1, false ); const auto ts = qTriangulate( path, transform, 1, false );
/* /*
The triangulation of a random path does not lead to index lists The triangulation of a random path usually does not lead to index lists
that are substantially reducing the number of vertices. that allow substantially reducing the number of vertices.
As we have to iterate over the vertex buffer to copy qreal to float As we have to iterate over the vertex buffer to copy qreal to float
anyway we reorder according to the index buffer and drop anyway we reorder according to the index buffer and drop
@ -60,12 +61,26 @@ static void qskUpdateGeometry( const QPainterPath& path,
geometry.allocate( ts.indices.size() ); geometry.allocate( ts.indices.size() );
if ( color.isValid() )
{
const QskVertex::Color c = color;
auto vertexData = geometry.vertexDataAsColoredPoint2D();
for ( int i = 0; i < ts.indices.size(); i++ )
{
const int j = 2 * indices[i];
vertexData[i].set( points[j], points[j + 1], c.r, c.g, c.b, c.a );
}
}
else
{
auto vertexData = geometry.vertexDataAsPoint2D(); auto vertexData = geometry.vertexDataAsPoint2D();
for ( int i = 0; i < ts.indices.size(); i++ ) for ( int i = 0; i < ts.indices.size(); i++ )
{ {
const int j = 2 * indices[i]; const int j = 2 * indices[i];
vertexData[i].set( points[j], points[j + 1] ); vertexData[i].set( points[j], points[j + 1] );
} }
}
} }
class QskShapeNodePrivate final : public QskFillNodePrivate class QskShapeNodePrivate final : public QskFillNodePrivate
@ -82,7 +97,6 @@ class QskShapeNodePrivate final : public QskFillNodePrivate
QskShapeNode::QskShapeNode() QskShapeNode::QskShapeNode()
: QskFillNode( *new QskShapeNodePrivate ) : QskFillNode( *new QskShapeNodePrivate )
{ {
setColoring( Monochrome );
geometry()->setDrawingMode( QSGGeometry::DrawTriangles ); geometry()->setDrawingMode( QSGGeometry::DrawTriangles );
} }
@ -104,14 +118,24 @@ void QskShapeNode::updateNode( const QPainterPath& path,
return; return;
} }
QColor c;
if ( gradient.isMonochrome() && hasHint( PreferColoredGeometry ) )
c = gradient.startColor();
const bool isDirty = ( isGeometryColored() != c.isValid() );
if ( c.isValid() )
setColoring( QskFillNode::Polychrome );
else
setColoring( rect, gradient ); setColoring( rect, gradient );
if ( ( transform != d->transform ) || ( path != d->path ) ) if ( isDirty || ( transform != d->transform ) || ( path != d->path ) )
{ {
d->path = path; d->path = path;
d->transform = transform; d->transform = transform;
qskUpdateGeometry( path, transform, *geometry() ); qskUpdateGeometry( path, transform, c, *geometry() );
geometry()->markVertexDataDirty(); geometry()->markVertexDataDirty();
markDirty( QSGNode::DirtyGeometry ); markDirty( QSGNode::DirtyGeometry );