Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qssgrendereffect.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
5#include <QtQuick3DRuntimeRender/private/qssgrendereffect_p.h>
6#include <QtQuick3DRuntimeRender/private/qssgrenderlayer_p.h>
7#include <QtQuick3DRuntimeRender/private/qssgrendercommands_p.h>
10
11#include <QtGui/QVector2D>
12#include <QtGui/QVector3D>
13
15
17
22
24{
25 flags |= FlagT(Flags::Dirty);
26}
27
29{
30 flags &= ~FlagT(Flags::Dirty);
31}
32
33// Suffix snippets added to the end of the shader strings. These are appended
34// after processing so it must be valid GLSL as-is, no more magic keywords.
35
36static const char *effect_vertex_main_pre =
37 "void main()\n"
38 "{\n"
39 " qt_inputUV = attr_uv;\n"
40 " qt_textureUV = qt_effectTextureMapUV(attr_uv);\n"
41 " vec4 qt_vertPosition = vec4(attr_pos, 1.0);\n"
42 "#if QSHADER_VIEW_COUNT >= 2\n"
43 " qt_viewIndex = gl_ViewIndex;\n"
44 "#else\n"
45 " qt_viewIndex = 0;\n"
46 "#endif\n"
47 " qt_customMain(qt_vertPosition.xyz);\n";
48
49static const char *effect_vertex_main_position =
50 " gl_Position = qt_modelViewProjection * qt_vertPosition;\n";
51
52static const char *effect_vertex_main_post =
53 "}\n";
54
55static const char *effect_fragment_main =
56 "void main()\n"
57 "{\n"
58 " qt_customMain();\n"
59 "}\n";
60
62 "#include \"tonemapping.glsllib\"\n"
63 "void main()\n"
64 "{\n"
65 " qt_customMain();\n"
66 " fragOutput = qt_tonemap(fragOutput);\n"
67 "}\n";
68
70{
72
73 // this is called on every frame, so do nothing if there are no changes
74 if (!shaderPrepData.valid)
75 return;
76
77 QRhi *rhi = renderContext->rhiContext()->rhi();
78
79 for (int i = 0, ie = shaderPrepData.passes.size(); i != ie; ++i) {
80 const ShaderPrepPassData &pass(shaderPrepData.passes[i]);
81
82 // The fragment shader of the last pass of the last effect may need to
83 // perform the built-in tonemapping.
84 const bool isLastEffect = m_nextEffect == nullptr;
85 const bool isLastPass = i == ie - 1;
86 const bool shouldTonemapIfEnabled = isLastEffect && isLastPass;
87
88 QSSGShaderFeatures features;
89 QByteArray completeVertexShader;
90 QByteArray completeFragmentShader;
91 QByteArray sourceCodeForHash;
92
93 const bool multiview = renderContext->rhiContext()->mainPassViewCount() >= 2;
95
96 if (!pass.vertexShaderCode[srcIdx].isEmpty()) {
97 QByteArray code = pass.vertexShaderCode[srcIdx];
98 // add the real main(), with or without assigning gl_Position at the end
103 completeVertexShader = code;
104 sourceCodeForHash += code;
105 }
106
107 if (!pass.fragmentShaderCode[srcIdx].isEmpty()) {
108 QByteArray code = pass.fragmentShaderCode[srcIdx];
109 if (shouldTonemapIfEnabled)
111 else
113 completeFragmentShader = code;
114 sourceCodeForHash += code;
115 }
116
117 QByteArray shaderPathKey = pass.shaderPathKeyPrefix;
118 shaderPathKey.append(':' + QCryptographicHash::hash(sourceCodeForHash, QCryptographicHash::Algorithm::Sha1).toHex());
119
120 // QSSGRhiEffectSystem will vary the vertex shader code based on this
121 // flag from the QRhi. It is therefore important to capture this in the
122 // cache key as well.
123 shaderPathKey.append(':' + QByteArray::number(rhi->isYUpInFramebuffer() ? 1 : 0));
124
125 if (shouldTonemapIfEnabled) {
126 // This does not always mean there will be tonemapping: if the mode
127 // is TonemapModeNone, then no extra feature defines are set, and
128 // so qt_tonemap() in the shader will not alter the color.
129 const QSSGRenderLayer::TonemapMode tonemapMode = layer.tonemapMode;
130 shaderPathKey.append(':' + QByteArray::number(int(tonemapMode)));
131 QSSGLayerRenderData::setTonemapFeatures(features, tonemapMode);
132 }
133
134 shaderPathKey.append(':' + QByteArray::number(multiview ? 1 : 0));
135
136 // Now that the final shaderPathKey is known, store the source and
137 // related data; it will be retrieved later by the QSSGRhiEffectSystem.
138 if (!completeVertexShader.isEmpty()) {
139 renderContext->shaderLibraryManager()->setShaderSource(shaderPathKey,
141 completeVertexShader,
142 pass.vertexMetaData[srcIdx]);
143 }
144 if (!completeFragmentShader.isEmpty()) {
145 QSSGCustomShaderMetaData metaData = pass.fragmentMetaData[srcIdx];
146 metaData.features = features;
147 renderContext->shaderLibraryManager()->setShaderSource(shaderPathKey,
149 completeFragmentShader,
150 metaData);
151 }
152
153 // and update the command
154 delete commands[pass.bindShaderCmdIndex].command;
155 commands[pass.bindShaderCmdIndex] = { new QSSGBindShader(shaderPathKey), true };
156 }
157
158 shaderPrepData.valid = false;
159}
160
162{
163 for (const Command &cmd : commands) {
164 if (cmd.own)
165 delete cmd.command;
166 }
167 commands.clear();
168 shaderPrepData.passes.clear();
169}
170
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:107
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QByteArray hash(QByteArrayView data, Algorithm method)
Returns the hash of data using method.
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
bool isYUpInFramebuffer() const
Definition qrhi.cpp:10030
static void setTonemapFeatures(QSSGShaderFeatures &features, QSSGRenderLayer::TonemapMode tonemapMode)
const std::unique_ptr< QSSGRhiContext > & rhiContext() const
const std::shared_ptr< QSSGShaderLibraryManager > & shaderLibraryManager() const
QString & append(QChar c)
Definition qstring.cpp:3252
Combined button and popup list for selecting options.
EGLOutputLayerEXT layer
GLsizei const GLubyte * commands
GLbitfield flags
static const char * effect_vertex_main_pre
static const char * effect_vertex_main_post
static const char * effect_fragment_main_with_tonemapping
static const char * effect_fragment_main
static const char * effect_vertex_main_position
#define Q_UNUSED(x)
QSSGCustomShaderMetaData vertexMetaData[2]
QSSGCustomShaderMetaData fragmentMetaData[2]
QVector< Command > commands
struct QSSGRenderEffect::@755 shaderPrepData
void finalizeShaders(const QSSGRenderLayer &layer, QSSGRenderContextInterface *renderContext)
QSSGRenderEffect * m_nextEffect
std::underlying_type_t< Flags > FlagT
Definition moc.h:23