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 175 of file kis_opengl_shader_loader.cpp.

176{
177 QString vertPath, fragPath;
178 // Select appropriate shader files
180 vertPath = "matrix_transform.vert";
181 fragPath = "simple_texture.frag";
182 } else {
183 vertPath = "matrix_transform_legacy.vert";
184 fragPath = "simple_texture_legacy.frag";
185 }
186
187 KisShaderProgram *shader = loadShader(vertPath, fragPath, QByteArray(), QByteArray());
188
189 return shader;
190}
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 134 of file kis_opengl_shader_loader.cpp.

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

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 vertexShaderFile.open(QIODevice::ReadOnly);
68 vertSource.append(vertexShaderFile.readAll());
69
70 result = shader->addShaderFromSourceCode(QOpenGLShader::Vertex, vertSource);
71 if (!result)
72 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to add vertex shader source from file", vertPath, shader->log()));
73
74 // Load fragment shader
75 QByteArray fragSource;
76
78 fragSource.append("#version 300 es\n");
80 fragSource.append("#extension GL_EXT_shader_texture_lod : enable\n");
81 }
82 fragSource.append(
83 "precision mediump float;\n"
84 "precision mediump sampler3D;\n");
85
86 // OpenColorIO doesn't support OpenGL ES.
87 fragSource.append("#define texture2D texture\n");
88 fragSource.append("#define texture3D texture\n");
90 fragSource.append(
91 "#if __VERSION__ < 300\n"
92 "#define textureLod texture2DLodEXT\n"
93 "#endif\n"
94 );
95 }
96 } else {
97#ifdef Q_OS_MACOS
98 fragSource.append(KisOpenGL::hasOpenGL3() ? "#version 150 core\n" : "#version 120\n");
99 fragSource.append("#define texture2D texture\n");
100 fragSource.append("#define texture3D texture\n");
101#else
102 fragSource.append(KisOpenGL::supportsLoD() ? "#version 130\n" : "#version 120\n");
103#endif
104 }
105 fragSource.append(fragHeader);
106 QFile fragmentShaderFile(":/" + fragPath);
107 fragmentShaderFile.open(QIODevice::ReadOnly);
108 fragSource.append(fragmentShaderFile.readAll());
109
110 result = shader->addShaderFromSourceCode(QOpenGLShader::Fragment, fragSource);
111 if (!result)
112 throw ShaderLoaderException(QString("%1: %2 - Cause: %3").arg("Failed to add fragment shader source from file", fragPath, shader->log()));
113
114 // Bind attributes
115 shader->bindAttributeLocation("a_vertexPosition", PROGRAM_VERTEX_ATTRIBUTE);
116 shader->bindAttributeLocation("a_textureCoordinate", PROGRAM_TEXCOORD_ATTRIBUTE);
117
118 // Link
119 result = shader->link();
120 if (!result)
121 throw ShaderLoaderException(QString("Failed to link shader: ").append(vertPath));
122
123 Q_ASSERT(shader->isLinked());
124
125 return shader;
126}
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 196 of file kis_opengl_shader_loader.cpp.

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

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


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