Krita Source Code Documentation
Loading...
Searching...
No Matches
KisOpenGLShaderLoader Class Reference

#include <kis_opengl_shader_loader.h>

Public Member Functions

KisShaderProgramloadCheckerShader ()
 
KisShaderProgramloadDisplayShader (QSharedPointer< KisDisplayFilter > displayFilter, bool useHiQualityFiltering)
 
KisShaderProgramloadSolidColorShader ()
 

Private Member Functions

KisShaderProgramloadShader (QString vertPath, QString fragPath, QByteArray vertHeader, QByteArray fragHeader)
 

Detailed Description

A utility class for loading various shaders we use in Krita. It provides specific methods for shaders that pick the correct vertex and fragment files depending on the availability of OpenGL3. Additionally, it provides a generic shader loading method to prevent duplication.

Definition at line 71 of file kis_opengl_shader_loader.h.

Member Function Documentation

◆ loadCheckerShader()

KisShaderProgram * KisOpenGLShaderLoader::loadCheckerShader ( )

Specific checker shader loading function. It picks the appropriate shader files depending on the availability of OpenGL3 on the target machine.

Definition at line 176 of file kis_opengl_shader_loader.cpp.

177{
178 QString vertPath, fragPath;
179 // Select appropriate shader files
181 vertPath = "matrix_transform.vert";
182 fragPath = "simple_texture.frag";
183 } else {
184 vertPath = "matrix_transform_legacy.vert";
185 fragPath = "simple_texture_legacy.frag";
186 }
187
188 KisShaderProgram *shader = loadShader(vertPath, fragPath, QByteArray(), QByteArray());
189
190 return shader;
191}
KisShaderProgram * loadShader(QString vertPath, QString fragPath, QByteArray vertHeader, QByteArray fragHeader)
static bool supportsLoD()

References loadShader(), and KisOpenGL::supportsLoD().

◆ loadDisplayShader()

KisShaderProgram * KisOpenGLShaderLoader::loadDisplayShader ( QSharedPointer< KisDisplayFilter > displayFilter,
bool useHiQualityFiltering )

Specific display shader loading function. It adds the appropriate extra code to the fragment shader depending on what is available on the target machine. Additionally, it picks the appropriate shader files depending on the availability of OpenGL3.

Definition at line 138 of file kis_opengl_shader_loader.cpp.

139{
140 QByteArray fragHeader;
141
143 fragHeader.append("#define DIRECT_LOD_FETCH\n");
144 if (useHiQualityFiltering) {
145 fragHeader.append("#define HIGHQ_SCALING\n");
146 }
147 }
148
149 // If we have an OCIO display filter and it contains a function we add
150 // it to our shader header which will sit on top of the fragment code.
151 bool haveDisplayFilter = displayFilter && !displayFilter->program().isEmpty();
152 if (haveDisplayFilter) {
153 fragHeader.append("#define USE_OCIO\n");
154 fragHeader.append(displayFilter->program().toLatin1());
155 }
156
157 QString vertPath, fragPath;
158 // Select appropriate shader files
160 vertPath = "matrix_transform.vert";
161 fragPath = "highq_downscale.frag";
162 } else {
163 vertPath = "matrix_transform_legacy.vert";
164 fragPath = "simple_texture_legacy.frag";
165 }
166
167 KisShaderProgram *shader = loadShader(vertPath, fragPath, QByteArray(), fragHeader);
168
169 return shader;
170}

References loadShader(), and KisOpenGL::supportsLoD().

◆ loadShader()

KisShaderProgram * KisOpenGLShaderLoader::loadShader ( QString vertPath,
QString fragPath,
QByteArray vertHeader,
QByteArray fragHeader )
private

Generic shader loading function that will compile a shader program given a vertex shader and fragment shader resource path. Extra code can be prepended to each shader respectively using the header parameters.

Parameters
vertPathResource path to a vertex shader
fragPathResource path to a fragment shader
vertHeaderExtra code which will be prepended to the vertex shader
fragHeaderExtra code which will be prepended to the fragment shader

Definition at line 44 of file kis_opengl_shader_loader.cpp.

46{
47 bool result;
48
49 KisShaderProgram *shader = new KisShaderProgram();
50
51 // Load vertex shader
52 QByteArray vertSource;
53
55 vertSource.append("#version 300 es\n");
56 } else {
57#ifdef Q_OS_MACOS
58 vertSource.append(KisOpenGL::hasOpenGL3() ? "#version 150 core\n" : "#version 120\n");
59 vertSource.append("#define texture2D texture\n");
60 vertSource.append("#define texture3D texture\n");
61#else
62 vertSource.append(KisOpenGL::supportsLoD() ? "#version 130\n" : "#version 120\n");
63#endif
64 }
65 vertSource.append(vertHeader);
66 QFile vertexShaderFile(":/" + vertPath);
67 if (!vertexShaderFile.open(QIODevice::ReadOnly)) {
68 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to open vertex shader source file", vertPath, vertexShaderFile.errorString()));
69 }
70 vertSource.append(vertexShaderFile.readAll());
71
72 result = shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertSource);
73 if (!result)
74 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to add vertex shader source from file", vertPath, shader->log()));
75
76 // Load fragment shader
77 QByteArray fragSource;
78
80 fragSource.append("#version 300 es\n");
82 fragSource.append("#extension GL_EXT_shader_texture_lod : enable\n");
83 }
84 fragSource.append(
85 "precision mediump float;\n"
86 "precision mediump sampler3D;\n");
87
88 // OpenColorIO doesn't support OpenGL ES.
89 fragSource.append("#define texture2D texture\n");
90 fragSource.append("#define texture3D texture\n");
92 fragSource.append(
93 "#if __VERSION__ < 300\n"
94 "#define textureLod texture2DLodEXT\n"
95 "#endif\n"
96 );
97 }
98 } else {
99#ifdef Q_OS_MACOS
100 fragSource.append(KisOpenGL::hasOpenGL3() ? "#version 150 core\n" : "#version 120\n");
101 fragSource.append("#define texture2D texture\n");
102 fragSource.append("#define texture3D texture\n");
103#else
104 fragSource.append(KisOpenGL::supportsLoD() ? "#version 130\n" : "#version 120\n");
105#endif
106 }
107 fragSource.append(fragHeader);
108 QFile fragmentShaderFile(":/" + fragPath);
109 if (!fragmentShaderFile.open(QIODevice::ReadOnly)) {
110 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to open fragment shader source file", fragPath, fragmentShaderFile.errorString()));
111 }
112 fragSource.append(fragmentShaderFile.readAll());
113
114 result = shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragSource);
115 if (!result)
116 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to add fragment shader source from file", fragPath, shader->log()));
117
118 // Bind attributes
119 shader->bindAttributeLocation("a_vertexPosition", PROGRAM_VERTEX_ATTRIBUTE);
120 shader->bindAttributeLocation("a_textureCoordinate", PROGRAM_TEXCOORD_ATTRIBUTE);
121
122 // Link
123 result = shader->link();
124 if (!result)
125 throw ShaderLoaderException(QString("Failed to link shader: ").append(vertPath));
126
127 Q_ASSERT(shader->isLinked());
128
129 return shader;
130}
static bool hasOpenGLES()
static bool hasOpenGL3()
#define PROGRAM_TEXCOORD_ATTRIBUTE
#define PROGRAM_VERTEX_ATTRIBUTE

References KisOpenGL::hasOpenGL3(), KisOpenGL::hasOpenGLES(), PROGRAM_TEXCOORD_ATTRIBUTE, PROGRAM_VERTEX_ATTRIBUTE, and KisOpenGL::supportsLoD().

◆ loadSolidColorShader()

KisShaderProgram * KisOpenGLShaderLoader::loadSolidColorShader ( )

Specific uniform shader loading function. It picks the appropriate shader files depending on the availability of OpenGL3 on the target machine.

Definition at line 197 of file kis_opengl_shader_loader.cpp.

198{
199 QString vertPath, fragPath;
200 // Select appropriate shader files
202 vertPath = "solid_color.vert";
203 fragPath = "solid_color.frag";
204 } else {
205 vertPath = "solid_color_legacy.vert";
206 fragPath = "solid_color_legacy.frag";
207 }
208
209 KisShaderProgram *shader = loadShader(vertPath, fragPath, QByteArray(), QByteArray());
210
211 return shader;
212}

References loadShader(), and KisOpenGL::supportsLoD().


The documentation for this class was generated from the following files: