5#include <private/qsgmaterialshader_p.h>
7#include <QtGui/private/qguiapplication_p.h>
8#include <qpa/qplatformintegration.h>
9#include <private/qfontengine_p.h>
11#include <QtQuick/qquickwindow.h>
12#include <QtQuick/private/qsgtexture_p.h>
13#include <QtQuick/private/qsgdefaultrendercontext_p.h>
15#include <private/qrawfont_p.h>
16#include <QtCore/qmath.h>
22 float o = c.w() * globalOpacity;
23 return QVector4D(c.x() * o, c.y() * o, c.z() * o, o);
32 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
34 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
44 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb"), viewCount);
45 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb"), viewCount);
59 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
61 Q_ASSERT(oldMaterial ==
nullptr || newMaterial->type() == oldMaterial->type());
68 Q_ASSERT(mat->texture());
69 Q_ASSERT(oldMat ==
nullptr || oldMat->texture());
72 QByteArray *buf = state.uniformData();
73 const int projectionMatrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
76 if (state.isMatrixDirty()) {
77 const QMatrix4x4 mv = state.modelViewMatrix();
78 memcpy(buf->data() + offset, mv.constData(), 64);
83 for (
int viewIndex = 0; viewIndex < projectionMatrixCount; ++viewIndex) {
84 if (state.isMatrixDirty()) {
85 const QMatrix4x4 p = state.projectionMatrix(viewIndex);
86 memcpy(buf->data() + offset, p.constData(), 64);
93 QRhiTexture *oldRtex = oldMat ? oldMat->texture()->rhiTexture() :
nullptr;
94 QRhiTexture *newRtex = mat->texture()->rhiTexture();
95 if (updated || !oldMat || oldRtex != newRtex) {
96 const QVector2D textureScale = QVector2D(1.0f / mat->rhiGlyphCache()->width(),
97 1.0f / mat->rhiGlyphCache()->height());
98 memcpy(buf->data() + offset, &textureScale, 8);
104 float dpr = state.devicePixelRatio();
105 memcpy(buf->data() + offset, &dpr, 4);
110 mat->rhiGlyphCache()->commitResourceUpdates(state.resourceUpdateBatch());
112 m_currentUbufOffset = offset;
117 QSGMaterial *newMaterial, QSGMaterial *)
124 QSGTexture *t = mat->texture();
125 t->setFiltering(QSGTexture::Nearest);
136 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb"), viewCount);
138 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb"), viewCount);
141 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
145 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
147 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
152 QByteArray *buf = state.uniformData();
154 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
155 const QVector4D color = qsg_premultiply(mat->color(), state.opacity());
156 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
159 m_currentUbufOffset += 16;
170 setFlag(UpdatesGraphicsPipelineState,
true);
171 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb"), viewCount);
174 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
176 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
180 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
182 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
187 QByteArray *buf = state.uniformData();
189 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
191 const QVector4D color = qsg_premultiply(mat->color(), state.opacity());
192 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
195 m_currentUbufOffset += 16;
201 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
204 Q_UNUSED(oldMaterial);
207 ps->blendEnable =
true;
208 ps->srcColor = GraphicsPipelineState::ConstantColor;
209 ps->dstColor = GraphicsPipelineState::OneMinusSrcColor;
211 QVector4D color = mat->color();
214 ps->blendConstant = QColor::fromRgbF(color.x(), color.y(), color.z());
225 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb"), viewCount);
228 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
232 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
234 bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
239 QByteArray *buf = state.uniformData();
241 if (oldMat ==
nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) {
243 const QVector4D color(0, 0, 0, mat->color().w() * state.opacity());
244 memcpy(buf->data() + m_currentUbufOffset, &color, 16);
247 m_currentUbufOffset += 16;
258 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb"), viewCount);
260 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb"), viewCount);
262 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb"), viewCount);
266 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
270 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
272 bool changed = QSG8BitTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial);
277 QByteArray *buf = state.uniformData();
279 if (oldMat ==
nullptr || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) {
280 const QVector4D styleColor = qsg_premultiply(mat->styleColor(), state.opacity());
281 memcpy(buf->data() + m_currentUbufOffset, &styleColor, 16);
284 m_currentUbufOffset += 16;
286 if (oldMat ==
nullptr || oldMat->styleShift() != mat->styleShift()) {
287 const QVector2D v = mat->styleShift();
288 memcpy(buf->data() + m_currentUbufOffset, &v, 8);
301 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb"), viewCount);
303 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb"), viewCount);
305 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb"), viewCount);
313 : m_rc(qobject_cast<QSGDefaultRenderContext *>(rc))
324 if (m_retainedFontEngine !=
nullptr)
325 m_rc->unregisterFontengineForCleanup(m_retainedFontEngine);
331 if (m_color == color)
338 if (m_glyphCache !=
nullptr && m_glyphCache->glyphFormat() == QFontEngine::Format_ARGB)
339 updateCache(QFontEngine::Format_ARGB);
344 Q_ASSERT(m_font.isValid());
346 setFlag(Blending,
true);
351 updateCache(glyphFormat);
362 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
364 if (glyphFormat == QFontEngine::Format_None) {
365 glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None
366 ? fontEngine->glyphFormat
367 : QFontEngine::Format_A32;
370 qreal devicePixelRatio;
378 devicePixelRatio = m_rc->currentDevicePixelRatio();
380 QTransform glyphCacheTransform = QTransform::fromScale(devicePixelRatio, devicePixelRatio);
381 if (!fontEngine->supportsTransformation(glyphCacheTransform))
382 glyphCacheTransform = QTransform();
384 QColor color = glyphFormat == QFontEngine::Format_ARGB ? QColor::fromRgbF(m_color.x(), m_color.y(), m_color.z(), m_color.w()) : QColor();
385 m_glyphCache = fontEngine->glyphCache(cacheKey, glyphFormat, glyphCacheTransform, color);
386 if (!m_glyphCache ||
int(m_glyphCache->glyphFormat()) != glyphFormat) {
387 m_glyphCache =
new QSGRhiTextureGlyphCache(m_rc, glyphFormat, glyphCacheTransform, color);
388 fontEngine->setGlyphCache(cacheKey, m_glyphCache.data());
389 if (m_retainedFontEngine !=
nullptr)
390 m_rc->unregisterFontengineForCleanup(m_retainedFontEngine);
394 m_retainedFontEngine = fontEngine;
395 m_rc->registerFontengineForCleanup(fontEngine);
401 const QVector<quint32> &glyphIndexes,
402 const QVector<QPointF> &glyphPositions,
403 QSGGeometry *geometry,
404 QRectF *boundingRect,
408 Q_ASSERT(m_font.isValid());
409 QPointF position(p.x(), p.y() - m_font.ascent());
410 QVector<QFixedPoint> fixedPointPositions;
411 const int glyphPositionsSize = glyphPositions.size();
412 fixedPointPositions.reserve(glyphPositionsSize);
413 for (
int i=0; i < glyphPositionsSize; ++i)
414 fixedPointPositions.append(QFixedPoint::fromPointF(position + glyphPositions.at(i)));
416 QTextureGlyphCache *cache = glyphCache();
418 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
419 cache->populate(fontD->fontEngine,
421 glyphIndexes.constData(),
422 fixedPointPositions.data(),
423 QPainter::RenderHints(),
425 cache->fillInPendingGlyphs();
427 int margin = fontD->fontEngine->glyphMargin(cache->glyphFormat());
429 qreal glyphCacheScaleX = cache->transform().m11();
430 qreal glyphCacheScaleY = cache->transform().m22();
431 qreal glyphCacheInverseScaleX = 1.0 / glyphCacheScaleX;
432 qreal glyphCacheInverseScaleY = 1.0 / glyphCacheScaleY;
433 qreal scaledMargin = margin * glyphCacheInverseScaleX;
435 Q_ASSERT(geometry->indexType() == QSGGeometry::UnsignedShortType);
436 geometry->allocate(glyphIndexes.size() * 4, glyphIndexes.size() * 6);
437 QVector4D *vp = (QVector4D *)geometry->vertexDataAsTexturedPoint2D();
438 Q_ASSERT(geometry->sizeOfVertex() ==
sizeof(QVector4D));
439 ushort *ip = geometry->indexDataAsUShort();
441 bool supportsSubPixelPositions = fontD->fontEngine->supportsHorizontalSubPixelPositions();
442 for (
int i=0; i<glyphIndexes.size(); ++i) {
443 QPointF glyphPosition = glyphPositions.at(i) + position;
444 QFixedPoint fixedPointPosition = fixedPointPositions.at(i);
446 QFixed subPixelPosition;
447 if (supportsSubPixelPositions)
448 subPixelPosition = fontD->fontEngine->subPixelPositionForX(QFixed::fromReal(fixedPointPosition.x.toReal() * glyphCacheScaleX));
450 QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i),
451 QFixedPoint(subPixelPosition, 0));
452 const QTextureGlyphCache::Coord &c = cache->coords.value(glyph);
460 qreal x = (qFloor(glyphPosition.x() * glyphCacheScaleX) * glyphCacheInverseScaleX) +
461 (c.baseLineX * glyphCacheInverseScaleX) - scaledMargin;
462 qreal y = (qRound(glyphPosition.y() * glyphCacheScaleY) * glyphCacheInverseScaleY) -
463 (c.baseLineY * glyphCacheInverseScaleY) - scaledMargin;
465 qreal w = c.w * glyphCacheInverseScaleX;
466 qreal h = c.h * glyphCacheInverseScaleY;
468 *boundingRect |= QRectF(x + scaledMargin, y + scaledMargin, w, h);
470 float cx1 = x - margins.left();
471 float cx2 = x + w + margins.right();
472 float cy1 = y - margins.top();
473 float cy2 = y + h + margins.bottom();
475 float tx1 = c.x - margins.left();
476 float tx2 = c.x + c.w + margins.right();
477 float ty1 = c.y - margins.top();
478 float ty2 = c.y + c.h + margins.bottom();
480 if (baseLine->isNull())
481 *baseLine = glyphPosition;
483 vp[4 * i + 0] = QVector4D(cx1, cy1, tx1, ty1);
484 vp[4 * i + 1] = QVector4D(cx2, cy1, tx2, ty1);
485 vp[4 * i + 2] = QVector4D(cx1, cy2, tx1, ty2);
486 vp[4 * i + 3] = QVector4D(cx2, cy2, tx2, ty2);
489 ip[6 * i + 0] = o + 0;
490 ip[6 * i + 1] = o + 2;
491 ip[6 * i + 2] = o + 3;
492 ip[6 * i + 3] = o + 3;
493 ip[6 * i + 4] = o + 1;
494 ip[6 * i + 5] = o + 0;
500 static QSGMaterialType argb, rgb, gray;
501 switch (glyphCache()->glyphFormat()) {
502 case QFontEngine::Format_ARGB:
504 case QFontEngine::Format_A32:
506 case QFontEngine::Format_A8:
514 return static_cast<QTextureGlyphCache *>(m_glyphCache.data());
519 return static_cast<QSGRhiTextureGlyphCache *>(glyphCache());
524 Q_UNUSED(renderMode);
525 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
526 const QFontEngine::GlyphFormat glyphFormat = gc->glyphFormat();
527 switch (glyphFormat) {
528 case QFontEngine::Format_ARGB:
530 case QFontEngine::Format_A32:
532 case QFontEngine::Format_A8:
541 return a.x() > b.x() ? 1 : -1;
543 return a.y() > b.y() ? 1 : -1;
545 return a.z() > b.z() ? 1 : -1;
547 return a.w() > b.w() ? 1 : -1;
553 Q_ASSERT(o && type() == o->type());
555 if (m_glyphCache != other->m_glyphCache)
556 return m_glyphCache.data() < other->m_glyphCache.data() ? -1 : 1;
557 return qsg_colorDiff(m_color, other->m_color);
562 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
563 QSize glyphCacheSize(gc->width(), gc->height());
564 if (glyphCacheSize != m_size) {
567 m_texture =
new QSGPlainTexture;
568 m_texture->setTexture(gc->texture());
569 m_texture->setTextureSize(QSize(gc->width(), gc->height()));
570 m_texture->setOwnsTexture(
false);
571 m_size = glyphCacheSize;
585 static QSGMaterialType type;
591 Q_UNUSED(renderMode);
592 QSGRhiTextureGlyphCache *gc = rhiGlyphCache();
600 if (m_styleShift != other->m_styleShift)
601 return m_styleShift.y() - other->m_styleShift.y();
603 int diff = qsg_colorDiff(m_styleColor, other->m_styleColor);
605 return QSGTextMaskMaterial::compare(o);
617 static QSGMaterialType type;
623 Q_UNUSED(renderMode);
624 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 QVector< quint32 > &glyphIndexes, const QVector< 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...
static QT_BEGIN_NAMESPACE QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity)
static int qsg_colorDiff(const QVector4D &a, const QVector4D &b)