Blender V4.3
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 <epoxy/gl.h>
11
13
14/* --------------------------------------------------------------------
15 * OpenGLShader.
16 */
17
18static const char *VERTEX_SHADER =
19 "#version 330\n"
20 "uniform vec2 fullscreen;\n"
21 "in vec2 texCoord;\n"
22 "in vec2 pos;\n"
23 "out vec2 texCoord_interp;\n"
24 "\n"
25 "vec2 normalize_coordinates()\n"
26 "{\n"
27 " return (vec2(2.0) * (pos / fullscreen)) - vec2(1.0);\n"
28 "}\n"
29 "\n"
30 "void main()\n"
31 "{\n"
32 " gl_Position = vec4(normalize_coordinates(), 0.0, 1.0);\n"
33 " texCoord_interp = texCoord;\n"
34 "}\n\0";
35
36static const char *FRAGMENT_SHADER =
37 "#version 330\n"
38 "uniform sampler2D image_texture;\n"
39 "in vec2 texCoord_interp;\n"
40 "out vec4 fragColor;\n"
41 "\n"
42 "void main()\n"
43 "{\n"
44 " vec4 rgba = texture(image_texture, texCoord_interp);\n"
45 /* Hard-coded Rec.709 gamma, should use OpenColorIO eventually. */
46 " fragColor = pow(rgba, vec4(0.45, 0.45, 0.45, 1.0));\n"
47 "}\n\0";
48
49static void shader_print_errors(const char *task, const char *log, const char *code)
50{
51 LOG(ERROR) << "Shader: " << task << " error:";
52 LOG(ERROR) << "===== shader string ====";
53
54 std::stringstream stream(code);
55 string partial;
56
57 int line = 1;
58 while (getline(stream, partial, '\n')) {
59 if (line < 10) {
60 LOG(ERROR) << " " << line << " " << partial;
61 }
62 else {
63 LOG(ERROR) << line << " " << partial;
64 }
65 line++;
66 }
67 LOG(ERROR) << log;
68}
69
70static int compile_shader_program(void)
71{
72 const struct Shader {
73 const char *source;
74 const GLenum type;
75 } shaders[2] = {{VERTEX_SHADER, GL_VERTEX_SHADER}, {FRAGMENT_SHADER, GL_FRAGMENT_SHADER}};
76
77 const GLuint program = glCreateProgram();
78
79 for (int i = 0; i < 2; i++) {
80 const GLuint shader = glCreateShader(shaders[i].type);
81
82 string source_str = shaders[i].source;
83 const char *c_str = source_str.c_str();
84
85 glShaderSource(shader, 1, &c_str, NULL);
86 glCompileShader(shader);
87
88 GLint compile_status;
89 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
90
91 if (!compile_status) {
92 GLchar log[5000];
93 GLsizei length = 0;
94 glGetShaderInfoLog(shader, sizeof(log), &length, log);
95 shader_print_errors("compile", log, c_str);
96 return 0;
97 }
98
99 glAttachShader(program, shader);
100 }
101
102 /* Link output. */
103 glBindFragDataLocation(program, 0, "fragColor");
104
105 /* Link and error check. */
106 glLinkProgram(program);
107
108 GLint link_status;
109 glGetProgramiv(program, GL_LINK_STATUS, &link_status);
110 if (!link_status) {
111 GLchar log[5000];
112 GLsizei length = 0;
113 glGetShaderInfoLog(program, sizeof(log), &length, log);
116 return 0;
117 }
118
119 return program;
120}
121
123{
125 const uint shader_program = get_shader_program();
126 position_attribute_location_ = glGetAttribLocation(shader_program, position_attribute_name);
127 }
129}
130
132{
134 const uint shader_program = get_shader_program();
135 tex_coord_attribute_location_ = glGetAttribLocation(shader_program, tex_coord_attribute_name);
136 }
138}
139
140void OpenGLShader::bind(int width, int height)
141{
143
144 if (!shader_program_) {
145 return;
146 }
147
148 glUseProgram(shader_program_);
149 glUniform1i(image_texture_location_, 0);
150 glUniform2f(fullscreen_location_, width, height);
151}
152
154
159
161{
163 return;
164 }
165
167
169 if (!shader_program_) {
170 return;
171 }
172
173 glUseProgram(shader_program_);
174
175 image_texture_location_ = glGetUniformLocation(shader_program_, "image_texture");
176 if (image_texture_location_ < 0) {
177 LOG(ERROR) << "Shader doesn't contain the 'image_texture' uniform.";
179 return;
180 }
181
182 fullscreen_location_ = glGetUniformLocation(shader_program_, "fullscreen");
183 if (fullscreen_location_ < 0) {
184 LOG(ERROR) << "Shader doesn't contain the 'fullscreen' uniform.";
186 return;
187 }
188}
189
191{
192 glDeleteProgram(shader_program_);
193 shader_program_ = 0;
194}
195
unsigned int uint
static int compile_shader_program(void)
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
int position_attribute_location_
bool shader_compile_attempted_
void bind(int width, int height)
int get_tex_coord_attrib_location()
int tex_coord_attribute_location_
static constexpr const char * tex_coord_attribute_name
#define CCL_NAMESPACE_END
#define NULL
#define LOG(severity)
Definition log.h:33
ccl_device_inline float3 log(float3 v)
const NodeType * type
Definition graph/node.h:178