Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopenglengineshadermanager_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15/*
16 VERTEX SHADERS
17 ==============
18
19 Vertex shaders are specified as multiple (partial) shaders. On desktop,
20 this works fine. On ES, QOpenGLShader & QOpenGLShaderProgram will make partial
21 shaders work by concatenating the source in each QOpenGLShader and compiling
22 it as a single shader. This is abstracted nicely by QOpenGLShaderProgram and
23 the GL2 engine doesn't need to worry about it.
24
25 Generally, there's two vertex shader objects. The position shaders are
26 the ones which set gl_Position. There's also two "main" vertex shaders,
27 one which just calls the position shader and another which also passes
28 through some texture coordinates from a vertex attribute array to a
29 varying. These texture coordinates are used for mask position in text
30 rendering and for the source coordinates in drawImage/drawPixmap. There's
31 also a "Simple" vertex shader for rendering a solid colour (used to render
32 into the stencil buffer where the actual colour value is discarded).
33
34 The position shaders for brushes look scary. This is because many of the
35 calculations which logically belong in the fragment shader have been moved
36 into the vertex shader to improve performance. This is why the position
37 calculation is in a separate shader. Not only does it calculate the
38 position, but it also calculates some data to be passed to the fragment
39 shader as a varying. It is optimal to move as much of the calculation as
40 possible into the vertex shader as this is executed less often.
41
42 The varyings passed to the fragment shaders are interpolated (which is
43 cheap). Unfortunately, GL will apply perspective correction to the
44 interpolation calusing errors. To get around this, the vertex shader must
45 apply perspective correction itself and set the w-value of gl_Position to
46 zero. That way, GL will be tricked into thinking it doesn't need to apply a
47 perspective correction and use linear interpolation instead (which is what
48 we want). Of course, if the brush transform is affeine, no perspective
49 correction is needed and a simpler vertex shader can be used instead.
50
51 So there are the following "main" vertex shaders:
52 qopenglslMainVertexShader
53 qopenglslMainWithTexCoordsVertexShader
54
55 And the following position vertex shaders:
56 qopenglslPositionOnlyVertexShader
57 qopenglslPositionWithTextureBrushVertexShader
58 qopenglslPositionWithPatternBrushVertexShader
59 qopenglslPositionWithLinearGradientBrushVertexShader
60 qopenglslPositionWithRadialGradientBrushVertexShader
61 qopenglslPositionWithConicalGradientBrushVertexShader
62 qopenglslAffinePositionWithTextureBrushVertexShader
63 qopenglslAffinePositionWithPatternBrushVertexShader
64 qopenglslAffinePositionWithLinearGradientBrushVertexShader
65 qopenglslAffinePositionWithRadialGradientBrushVertexShader
66 qopenglslAffinePositionWithConicalGradientBrushVertexShader
67
68 Leading to 23 possible vertex shaders
69
70
71 FRAGMENT SHADERS
72 ================
73
74 Fragment shaders are also specified as multiple (partial) shaders. The
75 different fragment shaders represent the different stages in Qt's fragment
76 pipeline. There are 1-3 stages in this pipeline: First stage is to get the
77 fragment's colour value. The next stage is to get the fragment's mask value
78 (coverage value for anti-aliasing) and the final stage is to blend the
79 incoming fragment with the background (for composition modes not supported
80 by GL).
81
82 Of these, the first stage will always be present. If Qt doesn't need to
83 apply anti-aliasing (because it's off or handled by multisampling) then
84 the coverage value doesn't need to be applied. (Note: There are two types
85 of mask, one for regular anti-aliasing and one for sub-pixel anti-
86 aliasing.) If the composition mode is one which GL supports natively then
87 the blending stage doesn't need to be applied.
88
89 As eash stage can have multiple implementations, they are abstracted as
90 GLSL function calls with the following signatures:
91
92 Brushes & image drawing are implementations of "qcolorp vec4 srcPixel()":
93 qopenglslImageSrcFragShader
94 qopenglslImageSrcWithPatternFragShader
95 qopenglslNonPremultipliedImageSrcFragShader
96 qopenglslSolidBrushSrcFragShader
97 qopenglslTextureBrushSrcFragShader
98 qopenglslTextureBrushWithPatternFragShader
99 qopenglslPatternBrushSrcFragShader
100 qopenglslLinearGradientBrushSrcFragShader
101 qopenglslRadialGradientBrushSrcFragShader
102 qopenglslConicalGradientBrushSrcFragShader
103 NOTE: It is assumed the colour returned by srcPixel() is pre-multiplied
104
105 Masks are implementations of "qcolorp vec4 applyMask(qcolorp vec4 src)":
106 qopenglslMaskFragmentShader
107 qopenglslRgbMaskFragmentShaderPass1
108 qopenglslRgbMaskFragmentShaderPass2
109 qopenglslRgbMaskWithGammaFragmentShader
110
111 Composition modes are "qcolorp vec4 compose(qcolorp vec4 src)":
112 qopenglslColorBurnCompositionModeFragmentShader
113 qopenglslColorDodgeCompositionModeFragmentShader
114 qopenglslDarkenCompositionModeFragmentShader
115 qopenglslDifferenceCompositionModeFragmentShader
116 qopenglslExclusionCompositionModeFragmentShader
117 qopenglslHardLightCompositionModeFragmentShader
118 qopenglslLightenCompositionModeFragmentShader
119 qopenglslMultiplyCompositionModeFragmentShader
120 qopenglslOverlayCompositionModeFragmentShader
121 qopenglslScreenCompositionModeFragmentShader
122 qopenglslSoftLightCompositionModeFragmentShader
123
124
125 Note: In the future, some GLSL compilers will support an extension allowing
126 a new 'color' precision specifier. To support this, qcolorp is used for
127 all color components so it can be defined to colorp or lowp depending upon
128 the implementation.
129
130 So there are different fragment shader main functions, depending on the
131 number & type of pipelines the fragment needs to go through.
132
133 The choice of which main() fragment shader string to use depends on:
134 - Use of global opacity
135 - Brush style (some brushes apply opacity themselves)
136 - Use & type of mask (TODO: Need to support high quality anti-aliasing & text)
137 - Use of non-GL Composition mode
138
139 Leading to the following fragment shader main functions:
140 gl_FragColor = compose(applyMask(srcPixel()*globalOpacity));
141 gl_FragColor = compose(applyMask(srcPixel()));
142 gl_FragColor = applyMask(srcPixel()*globalOpacity);
143 gl_FragColor = applyMask(srcPixel());
144 gl_FragColor = compose(srcPixel()*globalOpacity);
145 gl_FragColor = compose(srcPixel());
146 gl_FragColor = srcPixel()*globalOpacity;
147 gl_FragColor = srcPixel();
148
149 Called:
150 qopenglslMainFragmentShader_CMO
151 qopenglslMainFragmentShader_CM
152 qopenglslMainFragmentShader_MO
153 qopenglslMainFragmentShader_M
154 qopenglslMainFragmentShader_CO
155 qopenglslMainFragmentShader_C
156 qopenglslMainFragmentShader_O
157 qopenglslMainFragmentShader
158
159 Where:
160 M = Mask
161 C = Composition
162 O = Global Opacity
163
164
165 CUSTOM SHADER CODE
166 ==================
167
168 The use of custom shader code is supported by the engine for drawImage and
169 drawPixmap calls. This is implemented via hooks in the fragment pipeline.
170
171 The custom shader is passed to the engine as a partial fragment shader
172 (QOpenGLCustomShaderStage). The shader will implement a pre-defined method name
173 which Qt's fragment pipeline will call:
174
175 lowp vec4 customShader(lowp sampler2d imageTexture, highp vec2 textureCoords)
176
177 The provided src and srcCoords parameters can be used to sample from the
178 source image.
179
180 Transformations, clipping, opacity, and composition modes set using QPainter
181 will be respected when using the custom shader hook.
182*/
183// Qt-Security score:significant reason:default
184
185#ifndef QOPENGLENGINE_SHADER_MANAGER_H
186#define QOPENGLENGINE_SHADER_MANAGER_H
187
188#include <QOpenGLShader>
189#include <QOpenGLShaderProgram>
190#include <QPainter>
191#include <private/qopenglcontext_p.h>
192#include <private/qopenglcustomshaderstage_p.h>
193
195
196
197
198/*
199struct QOpenGLEngineCachedShaderProg
200{
201 QOpenGLEngineCachedShaderProg(QOpenGLEngineShaderManager::ShaderName vertexMain,
202 QOpenGLEngineShaderManager::ShaderName vertexPosition,
203 QOpenGLEngineShaderManager::ShaderName fragMain,
204 QOpenGLEngineShaderManager::ShaderName pixelSrc,
205 QOpenGLEngineShaderManager::ShaderName mask,
206 QOpenGLEngineShaderManager::ShaderName composition);
207
208 int cacheKey;
209 QOpenGLShaderProgram* program;
210}
211*/
212
215static const GLuint QT_OPACITY_ATTR = 2;
219
221
222class Q_OPENGL_EXPORT QOpenGLEngineSharedShaders
223{
224 Q_GADGET
225 Q_DISABLE_COPY_MOVE(QOpenGLEngineSharedShaders)
226public:
227
228 enum SnippetName {
229 MainVertexShader,
230 MainWithTexCoordsVertexShader,
231 MainWithTexCoordsAndOpacityVertexShader,
232
233 // UntransformedPositionVertexShader must be first in the list:
234 UntransformedPositionVertexShader,
235 PositionOnlyVertexShader,
236 ComplexGeometryPositionOnlyVertexShader,
237 PositionWithPatternBrushVertexShader,
238 PositionWithLinearGradientBrushVertexShader,
239 PositionWithConicalGradientBrushVertexShader,
240 PositionWithRadialGradientBrushVertexShader,
241 PositionWithTextureBrushVertexShader,
242 AffinePositionWithPatternBrushVertexShader,
243 AffinePositionWithLinearGradientBrushVertexShader,
244 AffinePositionWithConicalGradientBrushVertexShader,
245 AffinePositionWithRadialGradientBrushVertexShader,
246 AffinePositionWithTextureBrushVertexShader,
247
248 // MainFragmentShader_CMO must be first in the list:
249 MainFragmentShader_MO,
250 MainFragmentShader_M,
251 MainFragmentShader_O,
252 MainFragmentShader,
253 MainFragmentShader_ImageArrays,
254
255 // ImageSrcFragmentShader must be first in the list::
256 ImageSrcFragmentShader,
257 ImageSrcWithPatternFragmentShader,
258 NonPremultipliedImageSrcFragmentShader,
259 GrayscaleImageSrcFragmentShader,
260 AlphaImageSrcFragmentShader,
261 CustomImageSrcFragmentShader,
262 SolidBrushSrcFragmentShader,
263 TextureBrushSrcFragmentShader,
264 TextureBrushSrcWithPatternFragmentShader,
265 PatternBrushSrcFragmentShader,
266 LinearGradientBrushSrcFragmentShader,
267 RadialGradientBrushSrcFragmentShader,
268 ConicalGradientBrushSrcFragmentShader,
269 ShockingPinkSrcFragmentShader,
270
271 // NoMaskFragmentShader must be first in the list:
272 NoMaskFragmentShader,
273 MaskFragmentShader,
274 RgbMaskFragmentShaderPass1,
275 RgbMaskFragmentShaderPass2,
276 RgbMaskWithGammaFragmentShader,
277
278 // NoCompositionModeFragmentShader must be first in the list:
279 NoCompositionModeFragmentShader,
280 MultiplyCompositionModeFragmentShader,
281 ScreenCompositionModeFragmentShader,
282 OverlayCompositionModeFragmentShader,
283 DarkenCompositionModeFragmentShader,
284 LightenCompositionModeFragmentShader,
285 ColorDodgeCompositionModeFragmentShader,
286 ColorBurnCompositionModeFragmentShader,
287 HardLightCompositionModeFragmentShader,
288 SoftLightCompositionModeFragmentShader,
289 DifferenceCompositionModeFragmentShader,
290 ExclusionCompositionModeFragmentShader,
291
292 TotalSnippetCount, InvalidSnippetName
293 };
294#if defined (QT_DEBUG)
295 Q_ENUM(SnippetName)
296 static QByteArray snippetNameStr(SnippetName snippetName);
297#endif
298
299/*
300 // These allow the ShaderName enum to be used as a cache key
301 const int mainVertexOffset = 0;
302 const int positionVertexOffset = (1<<2) - PositionOnlyVertexShader;
303 const int mainFragOffset = (1<<6) - MainFragmentShader_CMO;
304 const int srcPixelOffset = (1<<10) - ImageSrcFragmentShader;
305 const int maskOffset = (1<<14) - NoMaskShader;
306 const int compositionOffset = (1 << 16) - MultiplyCompositionModeFragmentShader;
307*/
308
309 QOpenGLEngineSharedShaders(QOpenGLContext *context);
310 ~QOpenGLEngineSharedShaders();
311
312 QOpenGLShaderProgram *simpleProgram() { return simpleShaderProg; }
313 QOpenGLShaderProgram *blitProgram() { return blitShaderProg; }
314 // Compile the program if it's not already in the cache, return the item in the cache.
315 QOpenGLEngineShaderProg *findProgramInCache(const QOpenGLEngineShaderProg &prog);
316 // Compile the custom shader if it's not already in the cache, return the item in the cache.
317
318 static QOpenGLEngineSharedShaders *shadersForContext(QOpenGLContext *context);
319
320 // Ideally, this would be static and cleanup all programs in all contexts which
321 // contain the custom code. Currently it is just a hint and we rely on deleted
322 // custom shaders being cleaned up by being kicked out of the cache when it's
323 // full.
324 void cleanupCustomStage(QOpenGLCustomShaderStage* stage);
325
326private:
327 QOpenGLShaderProgram *blitShaderProg;
328 QOpenGLShaderProgram *simpleShaderProg;
329 QList<QOpenGLEngineShaderProg*> cachedPrograms;
330
331 static const char* qShaderSnippets[TotalSnippetCount];
332};
333
334
336{
337public:
339
341 if (program)
342 delete program;
343 }
344
351
352 QByteArray customStageSource; //TODO: Decent cache key for custom stages
354
356
360
361 bool operator==(const QOpenGLEngineShaderProg& other) const {
362 // We don't care about the program
363 return ( mainVertexShader == other.mainVertexShader &&
364 positionVertexShader == other.positionVertexShader &&
365 mainFragShader == other.mainFragShader &&
366 srcPixelFragShader == other.srcPixelFragShader &&
367 maskFragShader == other.maskFragShader &&
368 compositionFragShader == other.compositionFragShader &&
369 customStageSource == other.customStageSource
370 );
371 }
372};
373
374class Q_OPENGL_EXPORT QOpenGLEngineShaderManager : public QObject
375{
376 Q_OBJECT
377public:
378 QOpenGLEngineShaderManager(QOpenGLContext* context);
379 ~QOpenGLEngineShaderManager();
380
381 enum MaskType {NoMask, PixelMask, SubPixelMaskPass1, SubPixelMaskPass2, SubPixelWithGammaMask};
382 enum PixelSrcType {
383 ImageSrc = Qt::TexturePattern+1,
384 NonPremultipliedImageSrc = Qt::TexturePattern+2,
385 PatternSrc = Qt::TexturePattern+3,
386 TextureSrcWithPattern = Qt::TexturePattern+4,
387 GrayscaleImageSrc = Qt::TexturePattern+5,
388 AlphaImageSrc = Qt::TexturePattern+6,
389 };
390
391 enum Uniform {
392 ImageTexture,
393 PatternColor,
394 GlobalOpacity,
395 Depth,
396 MaskTexture,
397 FragmentColor,
398 LinearData,
399 Angle,
400 HalfViewportSize,
401 Fmp,
402 Fmp2MRadius2,
403 Inverse2Fmp2MRadius2,
404 SqrFr,
405 BRadius,
406 InvertedTextureSize,
407 BrushTransform,
408 BrushTexture,
409 Matrix,
410 NumUniforms
411 };
412
413 enum OpacityMode {
414 NoOpacity,
415 UniformOpacity,
416 AttributeOpacity
417 };
418
419 // There are optimizations we can do, depending on the brush transform:
420 // 1) May not have to apply perspective-correction
421 // 2) Can use lower precision for matrix
422 void optimiseForBrushTransform(QTransform::TransformationType transformType);
423 void setSrcPixelType(Qt::BrushStyle);
424 void setSrcPixelType(PixelSrcType); // For non-brush sources, like pixmaps & images
425 void setOpacityMode(OpacityMode);
426 void setMaskType(MaskType);
427 void setCompositionMode(QPainter::CompositionMode);
428 void setCustomStage(QOpenGLCustomShaderStage* stage);
429 void removeCustomStage();
430
431 GLuint getUniformLocation(Uniform id);
432
433 void setDirty(); // someone has manually changed the current shader program
434 bool useCorrectShaderProg(); // returns true if the shader program needed to be changed
435
436 void useSimpleProgram();
437 void useBlitProgram();
438 void setHasComplexGeometry(bool hasComplexGeometry)
439 {
440 complexGeometry = hasComplexGeometry;
441 shaderProgNeedsChanging = true;
442 }
443 bool hasComplexGeometry() const
444 {
445 return complexGeometry;
446 }
447
448 QOpenGLShaderProgram* currentProgram(); // Returns pointer to the shader the manager has chosen
449 QOpenGLShaderProgram* simpleProgram(); // Used to draw into e.g. stencil buffers
450 QOpenGLShaderProgram* blitProgram(); // Used to blit a texture into the framebuffer
451
452 QOpenGLEngineSharedShaders* sharedShaders;
453
454private:
455 QOpenGLContext* ctx;
456 bool shaderProgNeedsChanging;
457 bool complexGeometry;
458
459 // Current state variables which influence the choice of shader:
460 QTransform brushTransform;
461 int srcPixelType;
462 OpacityMode opacityMode;
463 MaskType maskType;
464 QPainter::CompositionMode compositionMode;
465 QOpenGLCustomShaderStage* customSrcStage;
466
467 QOpenGLEngineShaderProg* currentShaderProg;
468};
469
470QT_END_NAMESPACE
471
472#endif //QOPENGLENGINE_SHADER_MANAGER_H
QPointer< QOpenGLEngineShaderManager > m_manager
virtual void setUniforms(QOpenGLShaderProgram *)
bool operator==(const QOpenGLEngineShaderProg &other) const
friend class QPainter
Combined button and popup list for selecting options.
static const GLuint QT_TEXTURE_COORDS_ATTR
static const GLuint QT_PMV_MATRIX_2_ATTR
static const GLuint QT_OPACITY_ATTR
static const GLuint QT_PMV_MATRIX_1_ATTR
static QT_BEGIN_NAMESPACE const GLuint QT_VERTEX_COORDS_ATTR
static const GLuint QT_PMV_MATRIX_3_ATTR