Blender  V3.3
app/opengl/shader.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "app/opengl/shader.h"
5 
6 #include "util/log.h"
7 #include "util/string.h"
8 
9 #include <GL/glew.h>
10 
12 
13 /* --------------------------------------------------------------------
14  * OpenGLShader.
15  */
16 
17 static const char *VERTEX_SHADER =
18  "#version 330\n"
19  "uniform vec2 fullscreen;\n"
20  "in vec2 texCoord;\n"
21  "in vec2 pos;\n"
22  "out vec2 texCoord_interp;\n"
23  "\n"
24  "vec2 normalize_coordinates()\n"
25  "{\n"
26  " return (vec2(2.0) * (pos / fullscreen)) - vec2(1.0);\n"
27  "}\n"
28  "\n"
29  "void main()\n"
30  "{\n"
31  " gl_Position = vec4(normalize_coordinates(), 0.0, 1.0);\n"
32  " texCoord_interp = texCoord;\n"
33  "}\n\0";
34 
35 static const char *FRAGMENT_SHADER =
36  "#version 330\n"
37  "uniform sampler2D image_texture;\n"
38  "in vec2 texCoord_interp;\n"
39  "out vec4 fragColor;\n"
40  "\n"
41  "void main()\n"
42  "{\n"
43  " vec4 rgba = texture(image_texture, texCoord_interp);\n"
44  /* Harcoded Rec.709 gamma, should use OpenColorIO eventually. */
45  " fragColor = pow(rgba, vec4(0.45, 0.45, 0.45, 1.0));\n"
46  "}\n\0";
47 
48 static void shader_print_errors(const char *task, const char *log, const char *code)
49 {
50  LOG(ERROR) << "Shader: " << task << " error:";
51  LOG(ERROR) << "===== shader string ====";
52 
53  stringstream stream(code);
54  string partial;
55 
56  int line = 1;
57  while (getline(stream, partial, '\n')) {
58  if (line < 10) {
59  LOG(ERROR) << " " << line << " " << partial;
60  }
61  else {
62  LOG(ERROR) << line << " " << partial;
63  }
64  line++;
65  }
66  LOG(ERROR) << log;
67 }
68 
69 static int compile_shader_program(void)
70 {
71  const struct Shader {
72  const char *source;
73  const GLenum type;
74  } shaders[2] = {{VERTEX_SHADER, GL_VERTEX_SHADER}, {FRAGMENT_SHADER, GL_FRAGMENT_SHADER}};
75 
76  const GLuint program = glCreateProgram();
77 
78  for (int i = 0; i < 2; i++) {
79  const GLuint shader = glCreateShader(shaders[i].type);
80 
81  string source_str = shaders[i].source;
82  const char *c_str = source_str.c_str();
83 
84  glShaderSource(shader, 1, &c_str, NULL);
85  glCompileShader(shader);
86 
87  GLint compile_status;
88  glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
89 
90  if (!compile_status) {
91  GLchar log[5000];
92  GLsizei length = 0;
93  glGetShaderInfoLog(shader, sizeof(log), &length, log);
94  shader_print_errors("compile", log, c_str);
95  return 0;
96  }
97 
98  glAttachShader(program, shader);
99  }
100 
101  /* Link output. */
102  glBindFragDataLocation(program, 0, "fragColor");
103 
104  /* Link and error check. */
105  glLinkProgram(program);
106 
107  GLint link_status;
108  glGetProgramiv(program, GL_LINK_STATUS, &link_status);
109  if (!link_status) {
110  GLchar log[5000];
111  GLsizei length = 0;
112  glGetShaderInfoLog(program, sizeof(log), &length, log);
115  return 0;
116  }
117 
118  return program;
119 }
120 
122 {
123  if (position_attribute_location_ == -1) {
124  const uint shader_program = get_shader_program();
125  position_attribute_location_ = glGetAttribLocation(shader_program, position_attribute_name);
126  }
128 }
129 
131 {
132  if (tex_coord_attribute_location_ == -1) {
133  const uint shader_program = get_shader_program();
134  tex_coord_attribute_location_ = glGetAttribLocation(shader_program, tex_coord_attribute_name);
135  }
137 }
138 
140 {
142 
143  if (!shader_program_) {
144  return;
145  }
146 
147  glUseProgram(shader_program_);
148  glUniform1i(image_texture_location_, 0);
149  glUniform2f(fullscreen_location_, width, height);
150 }
151 
153 {
154 }
155 
157 {
158  return shader_program_;
159 }
160 
162 {
164  return;
165  }
166 
168 
170  if (!shader_program_) {
171  return;
172  }
173 
174  glUseProgram(shader_program_);
175 
176  image_texture_location_ = glGetUniformLocation(shader_program_, "image_texture");
177  if (image_texture_location_ < 0) {
178  LOG(ERROR) << "Shader doesn't contain the 'image_texture' uniform.";
179  destroy_shader();
180  return;
181  }
182 
183  fullscreen_location_ = glGetUniformLocation(shader_program_, "fullscreen");
184  if (fullscreen_location_ < 0) {
185  LOG(ERROR) << "Shader doesn't contain the 'fullscreen' uniform.";
186  destroy_shader();
187  return;
188  }
189 }
190 
192 {
193  glDeleteProgram(shader_program_);
194  shader_program_ = 0;
195 }
196 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
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)
void create_shader_if_needed()
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
Definition: cuda/compat.h:9
#define LOG(severity)
Definition: log.h:36
ccl_device_inline float3 log(float3 v)
Definition: math_float3.h:397
struct blender::compositor::@179::@181 task
T length(const vec_base< T, Size > &a)
const NodeType * type
Definition: graph/node.h:175