Blender V5.0
app/opengl/shader.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "app/opengl/shader.h"
6
7#include "util/log.h"
8#include "util/string.h"
9
10#include <sstream>
11
12#include <epoxy/gl.h>
13
15
16/* --------------------------------------------------------------------
17 * OpenGLShader.
18 */
19
20static const char *VERTEX_SHADER =
21 "#version 330\n"
22 "uniform vec2 fullscreen;\n"
23 "in vec2 texCoord;\n"
24 "in vec2 pos;\n"
25 "out vec2 texCoord_interp;\n"
26 "\n"
27 "vec2 normalize_coordinates()\n"
28 "{\n"
29 " return (vec2(2.0) * (pos / fullscreen)) - vec2(1.0);\n"
30 "}\n"
31 "\n"
32 "void main()\n"
33 "{\n"
34 " gl_Position = vec4(normalize_coordinates(), 0.0, 1.0);\n"
35 " texCoord_interp = texCoord;\n"
36 "}\n\0";
37
38static const char *FRAGMENT_SHADER =
39 "#version 330\n"
40 "uniform sampler2D image_texture;\n"
41 "in vec2 texCoord_interp;\n"
42 "out vec4 fragColor;\n"
43 "\n"
44 "void main()\n"
45 "{\n"
46 " vec4 rgba = texture(image_texture, texCoord_interp);\n"
47 /* Hard-coded Rec.709 gamma, should use OpenColorIO eventually. */
48 " fragColor = pow(rgba, vec4(0.45, 0.45, 0.45, 1.0));\n"
49 "}\n\0";
50
51static void shader_print_errors(const char *task, const char *log, const char *code)
52{
53 LOG_ERROR << "Shader: " << task << " error:";
54 LOG_ERROR << "===== shader string ====";
55
56 std::stringstream stream(code);
57 string partial;
58
59 int line = 1;
60 while (getline(stream, partial, '\n')) {
61 if (line < 10) {
62 LOG_ERROR << " " << line << " " << partial;
63 }
64 else {
65 LOG_ERROR << line << " " << partial;
66 }
67 line++;
68 }
69 LOG_ERROR << log;
70}
71
73{
74 const struct Shader {
75 const char *source;
76 const GLenum type;
77 } shaders[2] = {{VERTEX_SHADER, GL_VERTEX_SHADER}, {FRAGMENT_SHADER, GL_FRAGMENT_SHADER}};
78
79 const GLuint program = glCreateProgram();
80
81 for (int i = 0; i < 2; i++) {
82 const GLuint shader = glCreateShader(shaders[i].type);
83
84 const string source_str = shaders[i].source;
85 const char *c_str = source_str.c_str();
86
87 glShaderSource(shader, 1, &c_str, nullptr);
88 glCompileShader(shader);
89
90 GLint compile_status;
91 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
92
93 if (!compile_status) {
94 GLchar log[5000];
95 GLsizei length = 0;
96 glGetShaderInfoLog(shader, sizeof(log), &length, log);
97 shader_print_errors("compile", log, c_str);
98 return 0;
99 }
100
101 glAttachShader(program, shader);
102 }
103
104 /* Link output. */
105 glBindFragDataLocation(program, 0, "fragColor");
106
107 /* Link and error check. */
108 glLinkProgram(program);
109
110 GLint link_status;
111 glGetProgramiv(program, GL_LINK_STATUS, &link_status);
112 if (!link_status) {
113 GLchar log[5000];
114 GLsizei length = 0;
115 glGetShaderInfoLog(program, sizeof(log), &length, log);
118 return 0;
119 }
120
121 return program;
122}
123
125{
127 const uint shader_program = get_shader_program();
128 position_attribute_location_ = glGetAttribLocation(shader_program, position_attribute_name);
129 }
131}
132
134{
136 const uint shader_program = get_shader_program();
137 tex_coord_attribute_location_ = glGetAttribLocation(shader_program, tex_coord_attribute_name);
138 }
140}
141
142void OpenGLShader::bind(const int width, const int height)
143{
145
146 if (!shader_program_) {
147 return;
148 }
149
150 glUseProgram(shader_program_);
151 glUniform1i(image_texture_location_, 0);
152 glUniform2f(fullscreen_location_, width, height);
153}
154
156
161
163{
165 return;
166 }
167
169
171 if (!shader_program_) {
172 return;
173 }
174
175 glUseProgram(shader_program_);
176
177 image_texture_location_ = glGetUniformLocation(shader_program_, "image_texture");
178 if (image_texture_location_ < 0) {
179 LOG_ERROR << "Shader doesn't contain the 'image_texture' uniform.";
181 return;
182 }
183
184 fullscreen_location_ = glGetUniformLocation(shader_program_, "fullscreen");
185 if (fullscreen_location_ < 0) {
186 LOG_ERROR << "Shader doesn't contain the 'fullscreen' uniform.";
188 return;
189 }
190}
191
193{
194 glDeleteProgram(shader_program_);
195 shader_program_ = 0;
196}
197
unsigned int uint
static int compile_shader_program()
static CCL_NAMESPACE_BEGIN const char * VERTEX_SHADER
static const char * FRAGMENT_SHADER
static void shader_print_errors(const char *task, const char *log, const char *code)
int get_position_attrib_location()
static constexpr const char * position_attribute_name
void bind(const int width, const int height)
int position_attribute_location_
bool shader_compile_attempted_
int get_tex_coord_attrib_location()
int tex_coord_attribute_location_
static constexpr const char * tex_coord_attribute_name
#define CCL_NAMESPACE_END
#define log
float length(VecOp< float, D >) RET
#define LOG_ERROR
Definition log.h:101
const NodeType * type
Definition graph/node.h:178
i
Definition text_draw.cc:230