7#include <QtGui/qsurface.h>
8#include <QtGui/qwindow.h>
15 if (Q_LIKELY(!qEnvironmentVariableIsSet(name)))
18 const float value = qgetenv(name).toFloat(&ok);
19 return ok ? value : defaultValue;
28 return base - ((qBound(devScaleMin, glyphScale, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin) * -baseDev + baseDev);
34 return range / glyphScale;
43 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
46 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
56 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext.vert.qsb"), viewCount);
58 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext_a.frag.qsb"), viewCount);
60 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext.frag.qsb"), viewCount);
64 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
66 Q_ASSERT(oldMaterial ==
nullptr || newMaterial->type() == oldMaterial->type());
67 QSGDistanceFieldTextMaterial *mat =
static_cast<QSGDistanceFieldTextMaterial *>(newMaterial);
68 QSGDistanceFieldTextMaterial *oldMat =
static_cast<QSGDistanceFieldTextMaterial *>(oldMaterial);
72 const bool textureUpdated = mat->updateTextureSizeAndWrapper();
73 Q_ASSERT(mat->wrapperTexture());
74 Q_ASSERT(oldMat ==
nullptr || oldMat->texture());
77 QByteArray *buf = state.uniformData();
78 Q_ASSERT(buf->size() >= 104);
80 bool updateRange =
false;
81 if (!oldMat || mat->fontScale() != oldMat->fontScale()) {
85 if (state.isMatrixDirty()) {
86 m_matrixScale = qSqrt(qAbs(state.determinant())) * state.devicePixelRatio();
90 const int matrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
91 for (
int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
92 if (state.isMatrixDirty()) {
93 const QMatrix4x4 m = state.combinedMatrix(viewIndex);
94 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
99 if (textureUpdated || !oldMat || oldMat->texture()->texture != mat->texture()->texture) {
100 const QVector2D ts(1.0f / mat->textureSize().width(), 1.0f / mat->textureSize().height());
101 Q_ASSERT(
sizeof(ts) == 8);
102 memcpy(buf->data() + offset, &ts, 8);
106 if (!oldMat || mat->color() != oldMat->color() || state.isOpacityDirty()) {
107 const QVector4D color = mat->color() * state.opacity();
108 Q_ASSERT(
sizeof(color) == 16);
109 memcpy(buf->data() + offset, &color, 16);
117 const QVector2D alphaMinMax(qMax(0.0f, base - range), qMin(base + range, 1.0f));
118 memcpy(buf->data() + offset, &alphaMinMax, 8);
124 static_cast<QSGRhiDistanceFieldGlyphCache *>(mat->glyphCache())->commitResourceUpdates(state.resourceUpdateBatch());
126 m_currentUbufOffset = offset;
131 QSGMaterial *newMaterial, QSGMaterial *)
137 QSGDistanceFieldTextMaterial *mat =
static_cast<QSGDistanceFieldTextMaterial *>(newMaterial);
138 QSGTexture *t = mat->wrapperTexture();
139 t->setFiltering(QSGTexture::Linear);
152 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext.vert.qsb"), viewCount);
154 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext_a_fwidth.frag.qsb"), viewCount);
156 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldtext_fwidth.frag.qsb"), viewCount);
159QSGDistanceFieldTextMaterial::QSGDistanceFieldTextMaterial()
160 : m_glyph_cache(
nullptr)
163 , m_sgTexture(
nullptr)
165 setFlag(Blending | RequiresDeterminant,
true);
168QSGDistanceFieldTextMaterial::~QSGDistanceFieldTextMaterial()
173QSGMaterialType *QSGDistanceFieldTextMaterial::type()
const
175 static QSGMaterialType type;
179void QSGDistanceFieldTextMaterial::setColor(
const QColor &color)
182 color.getRgbF(&r, &g, &b, &a);
183 m_color = QVector4D(r * a, g * a, b * a, a);
186QSGMaterialShader *QSGDistanceFieldTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
188 if (renderMode == QSGRendererInterface::RenderMode3D && m_glyph_cache->screenSpaceDerivativesSupported())
189 return new DistanceFieldAnisotropicTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
191 return new QSGDistanceFieldTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
194bool QSGDistanceFieldTextMaterial::updateTextureSize()
197 m_texture = m_glyph_cache->glyphTexture(0);
199 if (m_texture->size != m_size) {
200 m_size = m_texture->size;
210bool QSGDistanceFieldTextMaterial::updateTextureSizeAndWrapper()
212 bool updated = updateTextureSize();
216 m_sgTexture =
new QSGPlainTexture;
217 m_sgTexture->setTexture(m_texture->texture);
218 m_sgTexture->setTextureSize(m_size);
219 m_sgTexture->setOwnsTexture(
false);
224int QSGDistanceFieldTextMaterial::compare(
const QSGMaterial *o)
const
226 Q_ASSERT(o && type() == o->type());
227 const QSGDistanceFieldTextMaterial *other =
static_cast<
const QSGDistanceFieldTextMaterial *>(o);
228 if (m_glyph_cache != other->m_glyph_cache)
229 return m_glyph_cache - other->m_glyph_cache;
230 if (m_fontScale != other->m_fontScale) {
231 return int(other->m_fontScale < m_fontScale) -
int(m_fontScale < other->m_fontScale);
233 if (m_color != other->m_color)
234 return &m_color < &other->m_color ? -1 : 1;
235 qintptr t0 = m_texture ? qintptr(m_texture->texture) : 0;
236 qintptr t1 = other->m_texture ? qintptr(other->m_texture->texture) : 0;
237 const qintptr diff = t0 - t1;
238 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
245 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
254 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
256 bool changed = QSGDistanceFieldTextMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
257 QSGDistanceFieldStyledTextMaterial *mat =
static_cast<QSGDistanceFieldStyledTextMaterial *>(newMaterial);
258 QSGDistanceFieldStyledTextMaterial *oldMat =
static_cast<QSGDistanceFieldStyledTextMaterial *>(oldMaterial);
260 QByteArray *buf = state.uniformData();
261 Q_ASSERT(buf->size() >= 128);
264 m_currentUbufOffset += 8;
266 if (!oldMat || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) {
267 QVector4D styleColor = mat->styleColor();
268 styleColor *= state.opacity();
270 memcpy(buf->data() + m_currentUbufOffset, &styleColor, 16);
273 m_currentUbufOffset += 16;
278QSGDistanceFieldStyledTextMaterial::QSGDistanceFieldStyledTextMaterial()
279 : QSGDistanceFieldTextMaterial()
283QSGDistanceFieldStyledTextMaterial::~QSGDistanceFieldStyledTextMaterial()
287void QSGDistanceFieldStyledTextMaterial::setStyleColor(
const QColor &color)
290 color.getRgbF(&r, &g, &b, &a);
291 m_styleColor = QVector4D(r * a, g * a, b * a, a);
294int QSGDistanceFieldStyledTextMaterial::compare(
const QSGMaterial *o)
const
296 Q_ASSERT(o && type() == o->type());
297 const QSGDistanceFieldStyledTextMaterial *other =
static_cast<
const QSGDistanceFieldStyledTextMaterial *>(o);
298 if (m_styleColor != other->m_styleColor)
299 return &m_styleColor < &other->m_styleColor ? -1 : 1;
300 return QSGDistanceFieldTextMaterial::compare(o);
308 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
314 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext.vert.qsb"), viewCount);
316 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext_a.frag.qsb"), viewCount);
318 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext.frag.qsb"), viewCount);
330 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext.vert.qsb"), viewCount);
332 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext_a_fwidth.frag.qsb"), viewCount);
334 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldoutlinetext_fwidth.frag.qsb"), viewCount);
338 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
340 bool changed = DistanceFieldStyledTextMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
341 QSGDistanceFieldOutlineTextMaterial *mat =
static_cast<QSGDistanceFieldOutlineTextMaterial *>(newMaterial);
342 QSGDistanceFieldOutlineTextMaterial *oldMat =
static_cast<QSGDistanceFieldOutlineTextMaterial *>(oldMaterial);
344 QByteArray *buf = state.uniformData();
345 Q_ASSERT(buf->size() >= 136);
347 if (!oldMat || mat->fontScale() != oldMat->fontScale() || state.isMatrixDirty()) {
348 float dfRadius = mat->glyphCache()->distanceFieldRadius();
349 float combinedScale = m_fontScale * m_matrixScale;
352 float outlineLimit = qMax(0.2f, base - 0.5f / dfRadius / m_fontScale);
353 float alphaMin = qMax(0.0f, base - range);
354 float styleAlphaMin0 = qMax(0.0f, outlineLimit - range);
355 float styleAlphaMin1 = qMin(outlineLimit + range, alphaMin);
356 memcpy(buf->data() + m_currentUbufOffset, &styleAlphaMin0, 4);
357 memcpy(buf->data() + m_currentUbufOffset + 4, &styleAlphaMin1, 4);
364QSGDistanceFieldOutlineTextMaterial::QSGDistanceFieldOutlineTextMaterial()
365 : QSGDistanceFieldStyledTextMaterial()
369QSGDistanceFieldOutlineTextMaterial::~QSGDistanceFieldOutlineTextMaterial()
373QSGMaterialType *QSGDistanceFieldOutlineTextMaterial::type()
const
375 static QSGMaterialType type;
379QSGMaterialShader *QSGDistanceFieldOutlineTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
381 if (renderMode == QSGRendererInterface::RenderMode3D && m_glyph_cache->screenSpaceDerivativesSupported())
382 return new DistanceFieldAnisotropicOutlineTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
384 return new DistanceFieldOutlineTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
392 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
398 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext.vert.qsb"), viewCount);
400 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext_a.frag.qsb"), viewCount);
402 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext.frag.qsb"), viewCount);
406 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
408 bool changed = DistanceFieldStyledTextMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
409 QSGDistanceFieldShiftedStyleTextMaterial *mat =
static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(newMaterial);
410 QSGDistanceFieldShiftedStyleTextMaterial *oldMat =
static_cast<QSGDistanceFieldShiftedStyleTextMaterial *>(oldMaterial);
412 QByteArray *buf = state.uniformData();
413 Q_ASSERT(buf->size() >= 136);
415 if (!oldMat || oldMat->fontScale() != mat->fontScale() || oldMat->shift() != mat->shift()
416 || oldMat->textureSize() != mat->textureSize())
418 QVector2D shift(1.0 / mat->fontScale() * mat->shift().x(),
419 1.0 / mat->fontScale() * mat->shift().y());
420 memcpy(buf->data() + m_currentUbufOffset, &shift, 8);
436 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext.vert.qsb"), viewCount);
438 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext_a_fwidth.frag.qsb"), viewCount);
440 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/distancefieldshiftedtext_fwidth.frag.qsb"), viewCount);
443QSGDistanceFieldShiftedStyleTextMaterial::QSGDistanceFieldShiftedStyleTextMaterial()
444 : QSGDistanceFieldStyledTextMaterial()
448QSGDistanceFieldShiftedStyleTextMaterial::~QSGDistanceFieldShiftedStyleTextMaterial()
452QSGMaterialType *QSGDistanceFieldShiftedStyleTextMaterial::type()
const
454 static QSGMaterialType type;
458QSGMaterialShader *QSGDistanceFieldShiftedStyleTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
460 if (renderMode == QSGRendererInterface::RenderMode3D && m_glyph_cache->screenSpaceDerivativesSupported())
461 return new DistanceFieldAnisotropicShiftedTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
463 return new DistanceFieldShiftedStyleTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
466int QSGDistanceFieldShiftedStyleTextMaterial::compare(
const QSGMaterial *o)
const
468 const QSGDistanceFieldShiftedStyleTextMaterial *other =
static_cast<
const QSGDistanceFieldShiftedStyleTextMaterial *>(o);
469 if (m_shift != other->m_shift)
470 return &m_shift < &other->m_shift ? -1 : 1;
471 return QSGDistanceFieldStyledTextMaterial::compare(o);
479 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
481 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
override;
487 setFlag(UpdatesGraphicsPipelineState,
true);
488 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/hiqsubpixeldistancefieldtext.vert.qsb"), viewCount);
490 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/hiqsubpixeldistancefieldtext_a.frag.qsb"), viewCount);
492 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/hiqsubpixeldistancefieldtext.frag.qsb"), viewCount);
496 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
498 bool changed = QSGDistanceFieldTextMaterialRhiShader::updateUniformData(state, newMaterial, oldMaterial);
499 QSGHiQSubPixelDistanceFieldTextMaterial *mat =
static_cast<QSGHiQSubPixelDistanceFieldTextMaterial *>(newMaterial);
500 QSGHiQSubPixelDistanceFieldTextMaterial *oldMat =
static_cast<QSGHiQSubPixelDistanceFieldTextMaterial *>(oldMaterial);
502 QByteArray *buf = state.uniformData();
503 Q_ASSERT(buf->size() >= 128);
505 if (!oldMat || mat->fontScale() != oldMat->fontScale()) {
506 float fontScale = mat->fontScale();
507 memcpy(buf->data() + m_currentUbufOffset, &fontScale, 4);
510 m_currentUbufOffset += 4 + 4;
512 if (!oldMat || state.isMatrixDirty()) {
513 int viewportWidth = state.viewportRect().width();
514 QMatrix4x4 mat = state.combinedMatrix().inverted();
515 QVector4D vecDelta = mat.column(0) * (qreal(2) / viewportWidth);
516 memcpy(buf->data() + m_currentUbufOffset, &vecDelta, 16);
523 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
526 Q_UNUSED(oldMaterial);
527 QSGHiQSubPixelDistanceFieldTextMaterial *mat =
static_cast<QSGHiQSubPixelDistanceFieldTextMaterial *>(newMaterial);
529 ps->blendEnable =
true;
530 ps->srcColor = GraphicsPipelineState::ConstantColor;
531 ps->dstColor = GraphicsPipelineState::OneMinusSrcColor;
533 const QVector4D color = mat->color();
535 ps->blendConstant = QColor::fromRgbF(color.x(), color.y(), color.z(), 1.0f);
540QSGMaterialType *QSGHiQSubPixelDistanceFieldTextMaterial::type()
const
542 static QSGMaterialType type;
546QSGMaterialShader *QSGHiQSubPixelDistanceFieldTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
548 if (renderMode == QSGRendererInterface::RenderMode3D && m_glyph_cache->screenSpaceDerivativesSupported())
549 return new DistanceFieldAnisotropicTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
551 return new QSGHiQSubPixelDistanceFieldTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
563 setShaderFileName(VertexStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/loqsubpixeldistancefieldtext.vert.qsb"), viewCount);
565 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/loqsubpixeldistancefieldtext_a.frag.qsb"), viewCount);
567 setShaderFileName(FragmentStage, QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/loqsubpixeldistancefieldtext.frag.qsb"), viewCount);
570QSGMaterialType *QSGLoQSubPixelDistanceFieldTextMaterial::type()
const
572 static QSGMaterialType type;
576QSGMaterialShader *QSGLoQSubPixelDistanceFieldTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
578 if (renderMode == QSGRendererInterface::RenderMode3D && m_glyph_cache->screenSpaceDerivativesSupported())
579 return new DistanceFieldAnisotropicTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
581 return new QSGLoQSubPixelDistanceFieldTextMaterialRhiShader(m_glyph_cache->eightBitFormatIsAlphaSwizzled(), viewCount());
DistanceFieldAnisotropicOutlineTextMaterialRhiShader(bool alphaTexture, int viewCount)
DistanceFieldAnisotropicShiftedTextMaterialRhiShader(bool alphaTexture, int viewCount)
DistanceFieldAnisotropicTextMaterialRhiShader(bool alphaTexture, 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...
DistanceFieldOutlineTextMaterialRhiShader(bool alphaTexture, 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...
DistanceFieldShiftedStyleTextMaterialRhiShader(bool alphaTexture, int viewCount)
DistanceFieldStyledTextMaterialRhiShader(bool alphaTexture, 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...
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,...
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...
quint32 m_currentUbufOffset
QSGDistanceFieldTextMaterialRhiShader(bool alphaTexture, int viewCount)
QSGHiQSubPixelDistanceFieldTextMaterialRhiShader(bool alphaTexture, int viewCount)
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...
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...
QSGLoQSubPixelDistanceFieldTextMaterialRhiShader(bool alphaTexture, int viewCount)
Combined button and popup list for selecting options.
static float thresholdFunc(float glyphScale)
static QT_BEGIN_NAMESPACE float qt_sg_envFloat(const char *name, float defaultValue)
static float spreadFunc(float glyphScale)