| 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 | |
| 184 | #ifndef QOPENGLENGINE_SHADER_MANAGER_H |
| 185 | #define QOPENGLENGINE_SHADER_MANAGER_H |
| 186 | |
| 187 | #include <QOpenGLShader> |
| 188 | #include <QOpenGLShaderProgram> |
| 189 | #include <QPainter> |
| 190 | #include <private/qopenglcontext_p.h> |
| 191 | #include <private/qopenglcustomshaderstage_p.h> |
| 192 | |
| 193 | QT_BEGIN_NAMESPACE |
| 194 | |
| 195 | |
| 196 | |
| 197 | /* |
| 198 | struct QOpenGLEngineCachedShaderProg |
| 199 | { |
| 200 | QOpenGLEngineCachedShaderProg(QOpenGLEngineShaderManager::ShaderName vertexMain, |
| 201 | QOpenGLEngineShaderManager::ShaderName vertexPosition, |
| 202 | QOpenGLEngineShaderManager::ShaderName fragMain, |
| 203 | QOpenGLEngineShaderManager::ShaderName pixelSrc, |
| 204 | QOpenGLEngineShaderManager::ShaderName mask, |
| 205 | QOpenGLEngineShaderManager::ShaderName composition); |
| 206 | |
| 207 | int cacheKey; |
| 208 | QOpenGLShaderProgram* program; |
| 209 | } |
| 210 | */ |
| 211 | |
| 212 | static const GLuint QT_VERTEX_COORDS_ATTR = 0; |
| 213 | static const GLuint QT_TEXTURE_COORDS_ATTR = 1; |
| 214 | static const GLuint QT_OPACITY_ATTR = 2; |
| 215 | static const GLuint QT_PMV_MATRIX_1_ATTR = 3; |
| 216 | static const GLuint QT_PMV_MATRIX_2_ATTR = 4; |
| 217 | static const GLuint QT_PMV_MATRIX_3_ATTR = 5; |
| 218 | |
| 219 | class QOpenGLEngineShaderProg; |
| 220 | |
| 221 | class Q_OPENGL_EXPORT QOpenGLEngineSharedShaders |
| 222 | { |
| 223 | Q_GADGET |
| 224 | public: |
| 225 | |
| 226 | enum SnippetName { |
| 227 | MainVertexShader, |
| 228 | MainWithTexCoordsVertexShader, |
| 229 | MainWithTexCoordsAndOpacityVertexShader, |
| 230 | |
| 231 | // UntransformedPositionVertexShader must be first in the list: |
| 232 | UntransformedPositionVertexShader, |
| 233 | PositionOnlyVertexShader, |
| 234 | ComplexGeometryPositionOnlyVertexShader, |
| 235 | PositionWithPatternBrushVertexShader, |
| 236 | PositionWithLinearGradientBrushVertexShader, |
| 237 | PositionWithConicalGradientBrushVertexShader, |
| 238 | PositionWithRadialGradientBrushVertexShader, |
| 239 | PositionWithTextureBrushVertexShader, |
| 240 | AffinePositionWithPatternBrushVertexShader, |
| 241 | AffinePositionWithLinearGradientBrushVertexShader, |
| 242 | AffinePositionWithConicalGradientBrushVertexShader, |
| 243 | AffinePositionWithRadialGradientBrushVertexShader, |
| 244 | AffinePositionWithTextureBrushVertexShader, |
| 245 | |
| 246 | // MainFragmentShader_CMO must be first in the list: |
| 247 | MainFragmentShader_MO, |
| 248 | MainFragmentShader_M, |
| 249 | MainFragmentShader_O, |
| 250 | MainFragmentShader, |
| 251 | MainFragmentShader_ImageArrays, |
| 252 | |
| 253 | // ImageSrcFragmentShader must be first in the list:: |
| 254 | ImageSrcFragmentShader, |
| 255 | ImageSrcWithPatternFragmentShader, |
| 256 | NonPremultipliedImageSrcFragmentShader, |
| 257 | GrayscaleImageSrcFragmentShader, |
| 258 | AlphaImageSrcFragmentShader, |
| 259 | CustomImageSrcFragmentShader, |
| 260 | SolidBrushSrcFragmentShader, |
| 261 | TextureBrushSrcFragmentShader, |
| 262 | TextureBrushSrcWithPatternFragmentShader, |
| 263 | PatternBrushSrcFragmentShader, |
| 264 | LinearGradientBrushSrcFragmentShader, |
| 265 | RadialGradientBrushSrcFragmentShader, |
| 266 | ConicalGradientBrushSrcFragmentShader, |
| 267 | ShockingPinkSrcFragmentShader, |
| 268 | |
| 269 | // NoMaskFragmentShader must be first in the list: |
| 270 | NoMaskFragmentShader, |
| 271 | MaskFragmentShader, |
| 272 | RgbMaskFragmentShaderPass1, |
| 273 | RgbMaskFragmentShaderPass2, |
| 274 | RgbMaskWithGammaFragmentShader, |
| 275 | |
| 276 | // NoCompositionModeFragmentShader must be first in the list: |
| 277 | NoCompositionModeFragmentShader, |
| 278 | MultiplyCompositionModeFragmentShader, |
| 279 | ScreenCompositionModeFragmentShader, |
| 280 | OverlayCompositionModeFragmentShader, |
| 281 | DarkenCompositionModeFragmentShader, |
| 282 | LightenCompositionModeFragmentShader, |
| 283 | ColorDodgeCompositionModeFragmentShader, |
| 284 | ColorBurnCompositionModeFragmentShader, |
| 285 | HardLightCompositionModeFragmentShader, |
| 286 | SoftLightCompositionModeFragmentShader, |
| 287 | DifferenceCompositionModeFragmentShader, |
| 288 | ExclusionCompositionModeFragmentShader, |
| 289 | |
| 290 | TotalSnippetCount, InvalidSnippetName |
| 291 | }; |
| 292 | #if defined (QT_DEBUG) |
| 293 | Q_ENUM(SnippetName) |
| 294 | static QByteArray snippetNameStr(SnippetName snippetName); |
| 295 | #endif |
| 296 | |
| 297 | /* |
| 298 | // These allow the ShaderName enum to be used as a cache key |
| 299 | const int mainVertexOffset = 0; |
| 300 | const int positionVertexOffset = (1<<2) - PositionOnlyVertexShader; |
| 301 | const int mainFragOffset = (1<<6) - MainFragmentShader_CMO; |
| 302 | const int srcPixelOffset = (1<<10) - ImageSrcFragmentShader; |
| 303 | const int maskOffset = (1<<14) - NoMaskShader; |
| 304 | const int compositionOffset = (1 << 16) - MultiplyCompositionModeFragmentShader; |
| 305 | */ |
| 306 | |
| 307 | QOpenGLEngineSharedShaders(QOpenGLContext *context); |
| 308 | ~QOpenGLEngineSharedShaders(); |
| 309 | |
| 310 | QOpenGLShaderProgram *simpleProgram() { return simpleShaderProg; } |
| 311 | QOpenGLShaderProgram *blitProgram() { return blitShaderProg; } |
| 312 | // Compile the program if it's not already in the cache, return the item in the cache. |
| 313 | QOpenGLEngineShaderProg *findProgramInCache(const QOpenGLEngineShaderProg &prog); |
| 314 | // Compile the custom shader if it's not already in the cache, return the item in the cache. |
| 315 | |
| 316 | static QOpenGLEngineSharedShaders *shadersForContext(QOpenGLContext *context); |
| 317 | |
| 318 | // Ideally, this would be static and cleanup all programs in all contexts which |
| 319 | // contain the custom code. Currently it is just a hint and we rely on deleted |
| 320 | // custom shaders being cleaned up by being kicked out of the cache when it's |
| 321 | // full. |
| 322 | void cleanupCustomStage(QOpenGLCustomShaderStage* stage); |
| 323 | |
| 324 | private: |
| 325 | QOpenGLShaderProgram *blitShaderProg; |
| 326 | QOpenGLShaderProgram *simpleShaderProg; |
| 327 | QList<QOpenGLEngineShaderProg*> cachedPrograms; |
| 328 | |
| 329 | static const char* qShaderSnippets[TotalSnippetCount]; |
| 330 | }; |
| 331 | |
| 332 | |
| 333 | class QOpenGLEngineShaderProg |
| 334 | { |
| 335 | public: |
| 336 | QOpenGLEngineShaderProg() : program(nullptr) {} |
| 337 | |
| 338 | ~QOpenGLEngineShaderProg() { |
| 339 | if (program) |
| 340 | delete program; |
| 341 | } |
| 342 | |
| 343 | QOpenGLEngineSharedShaders::SnippetName mainVertexShader; |
| 344 | QOpenGLEngineSharedShaders::SnippetName positionVertexShader; |
| 345 | QOpenGLEngineSharedShaders::SnippetName mainFragShader; |
| 346 | QOpenGLEngineSharedShaders::SnippetName srcPixelFragShader; |
| 347 | QOpenGLEngineSharedShaders::SnippetName maskFragShader; |
| 348 | QOpenGLEngineSharedShaders::SnippetName compositionFragShader; |
| 349 | |
| 350 | QByteArray customStageSource; //TODO: Decent cache key for custom stages |
| 351 | QOpenGLShaderProgram* program; |
| 352 | |
| 353 | QList<uint> uniformLocations; |
| 354 | |
| 355 | bool useTextureCoords; |
| 356 | bool useOpacityAttribute; |
| 357 | bool usePmvMatrixAttribute; |
| 358 | |
| 359 | bool operator==(const QOpenGLEngineShaderProg& other) const { |
| 360 | // We don't care about the program |
| 361 | return ( mainVertexShader == other.mainVertexShader && |
| 362 | positionVertexShader == other.positionVertexShader && |
| 363 | mainFragShader == other.mainFragShader && |
| 364 | srcPixelFragShader == other.srcPixelFragShader && |
| 365 | maskFragShader == other.maskFragShader && |
| 366 | compositionFragShader == other.compositionFragShader && |
| 367 | customStageSource == other.customStageSource |
| 368 | ); |
| 369 | } |
| 370 | }; |
| 371 | |
| 372 | class Q_OPENGL_EXPORT QOpenGLEngineShaderManager : public QObject |
| 373 | { |
| 374 | Q_OBJECT |
| 375 | public: |
| 376 | QOpenGLEngineShaderManager(QOpenGLContext* context); |
| 377 | ~QOpenGLEngineShaderManager(); |
| 378 | |
| 379 | enum MaskType {NoMask, PixelMask, SubPixelMaskPass1, SubPixelMaskPass2, SubPixelWithGammaMask}; |
| 380 | enum PixelSrcType { |
| 381 | ImageSrc = Qt::TexturePattern+1, |
| 382 | NonPremultipliedImageSrc = Qt::TexturePattern+2, |
| 383 | PatternSrc = Qt::TexturePattern+3, |
| 384 | TextureSrcWithPattern = Qt::TexturePattern+4, |
| 385 | GrayscaleImageSrc = Qt::TexturePattern+5, |
| 386 | AlphaImageSrc = Qt::TexturePattern+6, |
| 387 | }; |
| 388 | |
| 389 | enum Uniform { |
| 390 | ImageTexture, |
| 391 | PatternColor, |
| 392 | GlobalOpacity, |
| 393 | Depth, |
| 394 | MaskTexture, |
| 395 | FragmentColor, |
| 396 | LinearData, |
| 397 | Angle, |
| 398 | HalfViewportSize, |
| 399 | Fmp, |
| 400 | Fmp2MRadius2, |
| 401 | Inverse2Fmp2MRadius2, |
| 402 | SqrFr, |
| 403 | BRadius, |
| 404 | InvertedTextureSize, |
| 405 | BrushTransform, |
| 406 | BrushTexture, |
| 407 | Matrix, |
| 408 | NumUniforms |
| 409 | }; |
| 410 | |
| 411 | enum OpacityMode { |
| 412 | NoOpacity, |
| 413 | UniformOpacity, |
| 414 | AttributeOpacity |
| 415 | }; |
| 416 | |
| 417 | // There are optimizations we can do, depending on the brush transform: |
| 418 | // 1) May not have to apply perspective-correction |
| 419 | // 2) Can use lower precision for matrix |
| 420 | void optimiseForBrushTransform(QTransform::TransformationType transformType); |
| 421 | void setSrcPixelType(Qt::BrushStyle); |
| 422 | void setSrcPixelType(PixelSrcType); // For non-brush sources, like pixmaps & images |
| 423 | void setOpacityMode(OpacityMode); |
| 424 | void setMaskType(MaskType); |
| 425 | void setCompositionMode(QPainter::CompositionMode); |
| 426 | void setCustomStage(QOpenGLCustomShaderStage* stage); |
| 427 | void removeCustomStage(); |
| 428 | |
| 429 | GLuint getUniformLocation(Uniform id); |
| 430 | |
| 431 | void setDirty(); // someone has manually changed the current shader program |
| 432 | bool useCorrectShaderProg(); // returns true if the shader program needed to be changed |
| 433 | |
| 434 | void useSimpleProgram(); |
| 435 | void useBlitProgram(); |
| 436 | void setHasComplexGeometry(bool hasComplexGeometry) |
| 437 | { |
| 438 | complexGeometry = hasComplexGeometry; |
| 439 | shaderProgNeedsChanging = true; |
| 440 | } |
| 441 | bool hasComplexGeometry() const |
| 442 | { |
| 443 | return complexGeometry; |
| 444 | } |
| 445 | |
| 446 | QOpenGLShaderProgram* currentProgram(); // Returns pointer to the shader the manager has chosen |
| 447 | QOpenGLShaderProgram* simpleProgram(); // Used to draw into e.g. stencil buffers |
| 448 | QOpenGLShaderProgram* blitProgram(); // Used to blit a texture into the framebuffer |
| 449 | |
| 450 | QOpenGLEngineSharedShaders* sharedShaders; |
| 451 | |
| 452 | private: |
| 453 | QOpenGLContext* ctx; |
| 454 | bool shaderProgNeedsChanging; |
| 455 | bool complexGeometry; |
| 456 | |
| 457 | // Current state variables which influence the choice of shader: |
| 458 | QTransform brushTransform; |
| 459 | int srcPixelType; |
| 460 | OpacityMode opacityMode; |
| 461 | MaskType maskType; |
| 462 | QPainter::CompositionMode compositionMode; |
| 463 | QOpenGLCustomShaderStage* customSrcStage; |
| 464 | |
| 465 | QOpenGLEngineShaderProg* currentShaderProg; |
| 466 | }; |
| 467 | |
| 468 | QT_END_NAMESPACE |
| 469 | |
| 470 | #endif //QOPENGLENGINE_SHADER_MANAGER_H |
| 471 | |