6#include <private/qsgmaterialshader_p.h>
8#include <QtGui/private/qguiapplication_p.h>
9#include <qpa/qplatformintegration.h>
10#include <private/qfontengine_p.h>
12#include <QtQuick/qquickwindow.h>
13#include <QtQuick/private/qsgtexture_p.h>
14#include <QtQuick/private/qsgdefaultrendercontext_p.h>
16#include <private/qrawfont_p.h>
17#include <QtCore/qmath.h>
23 float o = c.w() * globalOpacity;
24 return QVector4D(c.x() * o, c.y() * o, c.z() * o, o);
33 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
35 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
45 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb"), viewCount);
46 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb"), viewCount);
60 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
62 Q_ASSERT(oldMaterial ==
nullptr || newMaterial->type() == oldMaterial->type());
69 Q_ASSERT(mat->texture());
70 Q_ASSERT(oldMat ==
nullptr || oldMat->texture());
73 QByteArray *buf = state.uniformData();
74 const int projectionMatrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
77 if (state.isMatrixDirty()) {
78 const QMatrix4x4 mv = state.modelViewMatrix();
79 memcpy(buf->data() + offset, mv.constData(), 64);
84 for (
int viewIndex = 0; viewIndex < projectionMatrixCount; ++viewIndex) {
85 if (state.isMatrixDirty()) {
86 const QMatrix4x4 p = state.projectionMatrix(viewIndex);
87 memcpy(buf->data() + offset, p.constData(), 64);
94 QRhiTexture *oldRtex = oldMat ? oldMat->texture()->rhiTexture() :
nullptr;
95 QRhiTexture *newRtex = mat->texture()->rhiTexture();
96 if (updated || !oldMat || oldRtex != newRtex) {
97 const QVector2D textureScale = QVector2D(1.0f / mat->rhiGlyphCache()->width(),
98 1.0f / mat->rhiGlyphCache()->height());
99 memcpy(buf->data() + offset, &textureScale, 8);
105 float dpr = state.devicePixelRatio();
106 memcpy(buf->data() + offset, &dpr, 4);
111 mat->rhiGlyphCache()->commitResourceUpdates(state.resourceUpdateBatch());
113 m_currentUbufOffset = offset;
118 QSGMaterial *newMaterial, QSGMaterial *)
125 QSGTexture *t = mat->texture();
126 t->setFiltering(QSGTexture::Nearest);
137 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb"), viewCount);
139 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb"), viewCount);
142 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
146 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
148 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
153 QByteArray *buf = state.uniformData();
155 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
156 const QVector4D color = qsg_premultiply(mat->color(), state.opacity());
157 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
160 m_currentUbufOffset += 16;
171 setFlag(UpdatesGraphicsPipelineState,
true);
172 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb"), viewCount);
175 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
177 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
181 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
183 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
188 QByteArray *buf = state.uniformData();
190 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
192 const QVector4D color = qsg_premultiply(mat->color(), state.opacity());
193 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
196 m_currentUbufOffset += 16;
202 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
205 Q_UNUSED(oldMaterial);
208 ps->blendEnable =
true;
209 ps->srcColor = GraphicsPipelineState::ConstantColor;
210 ps->dstColor = GraphicsPipelineState::OneMinusSrcColor;
212 QVector4D color = mat->color();
215 ps->blendConstant = QColor::fromRgbF(color.x(), color.y(), color.z());
226 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb"), viewCount);
229 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
233 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
235 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
240 QByteArray *buf = state.uniformData();
242 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
244 const QVector4D color(0, 0, 0, mat->color().w() * state.opacity());
245 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
248 m_currentUbufOffset += 16;
259 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb"), viewCount);
261 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb"), viewCount);
263 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb"), viewCount);
267 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
271 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
273 bool changed = QSG8BitTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
278 QByteArray *buf = state.uniformData();
280 if (oldMat ==
nullptr || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) {
281 const QVector4D styleColor = qsg_premultiply(mat->styleColor(), state.opacity());
282 memcpy(buf->data() + m_currentUbufOffset, &styleColor, 16);
285 m_currentUbufOffset += 16;
287 if (oldMat ==
nullptr || oldMat->styleShift() != mat->styleShift()) {
288 const QVector2D v = mat->styleShift();
289 memcpy(buf->data() + m_currentUbufOffset, &v, 8);
302 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb"), viewCount);
304 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb"), viewCount);
306 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb"), viewCount);
314 : m_rc(qobject_cast<QSGDefaultRenderContext *>(rc))
325 if (m_retainedFontEngine !=
nullptr)
326 m_rc->unregisterFontengineForCleanup(m_retainedFontEngine);
332 if (m_color == color)
339 if (m_glyphCache !=
nullptr && m_glyphCache->glyphFormat() == QFontEngine::Format_ARGB)
340 updateCache(QFontEngine::Format_ARGB);
345 Q_ASSERT(m_font.isValid());
347 setFlag(Blending,
true);
352 updateCache(glyphFormat);
363 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
365 if (glyphFormat == QFontEngine::Format_None) {
366 glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None
367 ? fontEngine->glyphFormat
368 : QFontEngine::Format_A32;
371 qreal devicePixelRatio;
379 devicePixelRatio = m_rc->currentDevicePixelRatio();
381 QTransform glyphCacheTransform = QTransform::fromScale(devicePixelRatio, devicePixelRatio);
382 if (!fontEngine->supportsTransformation(glyphCacheTransform))
383 glyphCacheTransform = QTransform();
385 QColor color = glyphFormat == QFontEngine::Format_ARGB ? QColor::fromRgbF(m_color.x(), m_color.y(), m_color.z(), m_color.w()) : QColor();
386 m_glyphCache = fontEngine->glyphCache(cacheKey, glyphFormat, glyphCacheTransform, color);
387 if (!m_glyphCache ||
int(m_glyphCache->glyphFormat()) != glyphFormat) {
388 m_glyphCache =
new QSGRhiTextureGlyphCache(m_rc, glyphFormat, glyphCacheTransform, color);
389 fontEngine->setGlyphCache(cacheKey, m_glyphCache.data());
390 if (m_retainedFontEngine !=
nullptr)
391 m_rc->unregisterFontengineForCleanup(m_retainedFontEngine);
395 m_retainedFontEngine = fontEngine;
396 m_rc->registerFontengineForCleanup(fontEngine);
402 const QList<quint32> &glyphIndexes,
403 const QList<QPointF> &glyphPositions,
404 QSGGeometry *geometry,
405 QRectF *boundingRect,
409 Q_ASSERT(m_font.isValid());
410 QPointF position(p.x(), p.y() - m_font.ascent());
411 QList<QFixedPoint> fixedPointPositions;
412 const int glyphPositionsSize = glyphPositions.size();
413 fixedPointPositions.reserve(glyphPositionsSize);
414 for (
int i=0; i < glyphPositionsSize; ++i)
415 fixedPointPositions.append(QFixedPoint::fromPointF(position + glyphPositions.at(i)));
417 QTextureGlyphCache *cache = glyphCache();
419 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
420 cache->populate(fontD->fontEngine,
422 glyphIndexes.constData(),
423 fixedPointPositions.data(),
424 QPainter::RenderHints(),
426 cache->fillInPendingGlyphs();
428 int margin = fontD->fontEngine->glyphMargin(cache->glyphFormat());
430 qreal glyphCacheScaleX = cache->transform().m11();
431 qreal glyphCacheScaleY = cache->transform().m22();
432 qreal glyphCacheInverseScaleX = 1.0 / glyphCacheScaleX;
433 qreal glyphCacheInverseScaleY = 1.0 / glyphCacheScaleY;
434 qreal scaledMargin = margin * glyphCacheInverseScaleX;
436 Q_ASSERT(geometry->indexType() == QSGGeometry::UnsignedShortType);
437 geometry->allocate(glyphIndexes.size() * 4, glyphIndexes.size() * 6);
438 QVector4D *vp = (QVector4D *)geometry->vertexDataAsTexturedPoint2D();
439 Q_ASSERT(geometry->sizeOfVertex() ==
sizeof(QVector4D));
440 ushort *ip = geometry->indexDataAsUShort();
442 bool supportsSubPixelPositions = fontD->fontEngine->supportsHorizontalSubPixelPositions();
443 for (
int i=0; i<glyphIndexes.size(); ++i) {
444 QPointF glyphPosition = glyphPositions.at(i) + position;
445 QFixedPoint fixedPointPosition = fixedPointPositions.at(i);
447 QFixed subPixelPosition;
448 if (supportsSubPixelPositions)
449 subPixelPosition = fontD->fontEngine->subPixelPositionForX(QFixed::fromReal(fixedPointPosition.x.toReal() * glyphCacheScaleX));
451 QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i),
452 QFixedPoint(subPixelPosition, 0));
453 const QTextureGlyphCache::Coord &c = cache->coords.value(glyph);
461 qreal x = (qFloor(glyphPosition.x() * glyphCacheScaleX) * glyphCacheInverseScaleX) +
462 (c.baseLineX * glyphCacheInverseScaleX) - scaledMargin;
463 qreal y = (qRound(glyphPosition.y() * glyphCacheScaleY) * glyphCacheInverseScaleY) -
464 (c.baseLineY * glyphCacheInverseScaleY) - scaledMargin;
466 qreal w = c.w * glyphCacheInverseScaleX;
467 qreal h = c.h * glyphCacheInverseScaleY;
469 *boundingRect |= QRectF(x + scaledMargin, y + scaledMargin, w, h);
471 float cx1 = x - margins.left();
472 float cx2 = x + w + margins.right();
473 float cy1 = y - margins.top();
474 float cy2 = y + h + margins.bottom();
476 float tx1 = c.x - margins.left();
477 float tx2 = c.x + c.w + margins.right();
478 float ty1 = c.y - margins.top();
479 float ty2 = c.y + c.h + margins.bottom();
481 if (baseLine->isNull())
482 *baseLine = glyphPosition;
484 vp[4 * i + 0] = QVector4D(cx1, cy1, tx1, ty1);
485 vp[4 * i + 1] = QVector4D(cx2, cy1, tx2, ty1);
486 vp[4 * i + 2] = QVector4D(cx1, cy2, tx1, ty2);
487 vp[4 * i + 3] = QVector4D(cx2, cy2, tx2, ty2);
490 ip[6 * i + 0] = o + 0;
491 ip[6 * i + 1] = o + 2;
492 ip[6 * i + 2] = o + 3;
493 ip[6 * i + 3] = o + 3;
494 ip[6 * i + 4] = o + 1;
495 ip[6 * i + 5] = o + 0;
501 static QSGMaterialType argb, rgb, gray;
502 switch (glyphCache()->glyphFormat()) {
503 case QFontEngine::Format_ARGB:
505 case QFontEngine::Format_A32:
507 case QFontEngine::Format_A8:
515 return static_cast<QTextureGlyphCache *>(m_glyphCache.data());
520 return static_cast<QSGRhiTextureGlyphCache *>(glyphCache());
525 Q_UNUSED(renderMode);
526 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
527 const QFontEngine::GlyphFormat glyphFormat = gc->glyphFormat();
528 switch (glyphFormat) {
529 case QFontEngine::Format_ARGB:
531 case QFontEngine::Format_A32:
533 case QFontEngine::Format_A8:
542 return a.x() > b.x() ? 1 : -1;
544 return a.y() > b.y() ? 1 : -1;
546 return a.z() > b.z() ? 1 : -1;
548 return a.w() > b.w() ? 1 : -1;
554 Q_ASSERT(o && type() == o->type());
556 if (m_glyphCache != other->m_glyphCache)
557 return m_glyphCache.data() < other->m_glyphCache.data() ? -1 : 1;
558 return qsg_colorDiff(m_color, other->m_color);
563 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
564 QSize glyphCacheSize(gc->width(), gc->height());
565 if (glyphCacheSize != m_size) {
568 m_texture =
new QSGPlainTexture;
569 m_texture->setTexture(gc->texture());
570 m_texture->setTextureSize(QSize(gc->width(), gc->height()));
571 m_texture->setOwnsTexture(
false);
572 m_size = glyphCacheSize;
586 static QSGMaterialType type;
592 Q_UNUSED(renderMode);
593 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
601 if (m_styleShift != other->m_styleShift)
602 return m_styleShift.y() - other->m_styleShift.y();
604 int diff = qsg_colorDiff(m_styleColor, other->m_styleColor);
606 return QSGTextMaskMaterial::compare(o);
618 static QSGMaterialType type;
624 Q_UNUSED(renderMode);
625 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
bool updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to enable the material to provide a custom set of graphics...
QSG24BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount)
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QSG32BitColorTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount)
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QSG8BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture)
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QSGOutlinedTextMaterial(QSGRenderContext *rc, const QRawFont &font)
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QSGOutlinedTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture)
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QSGStyledTextMaterial(QSGRenderContext *rc, const QRawFont &font)
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QSGStyledTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture)
QSGTextMaskMaterial(QSGRenderContext *rc, const QVector4D &color, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat=QFontEngine::Format_None)
void setColor(const QVector4D &color)
virtual ~QSGTextMaskMaterial()
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QTextureGlyphCache * glyphCache() const
QSGRhiTextureGlyphCache * rhiGlyphCache() const
void populate(const QPointF &position, const QList< quint32 > &glyphIndexes, const QList< QPointF > &glyphPositions, QSGGeometry *geometry, QRectF *boundingRect, QPointF *baseLine, const QMargins &margins=QMargins(0, 0, 0, 0))
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
QFontEngine::GlyphFormat m_glyphFormat
QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount)
quint32 m_currentUbufOffset
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity)
static int qsg_colorDiff(const QVector4D &a, const QVector4D &b)