1 #define GL_GLEXT_PROTOTYPES 1
11 #include "effect_chain.h"
12 #include "gamma_expansion_effect.h"
13 #include "gamma_compression_effect.h"
14 #include "lift_gamma_gain_effect.h"
15 #include "colorspace_conversion_effect.h"
16 #include "saturation_effect.h"
17 #include "mirror_effect.h"
18 #include "vignette_effect.h"
19 #include "texture_enum.h"
21 EffectChain::EffectChain(unsigned width, unsigned height)
22 : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
24 void EffectChain::add_input(const ImageFormat &format)
26 input_format = format;
27 current_color_space = format.color_space;
28 current_gamma_curve = format.gamma_curve;
31 void EffectChain::add_output(const ImageFormat &format)
33 output_format = format;
36 Effect *instantiate_effect(EffectId effect)
39 case EFFECT_GAMMA_EXPANSION:
40 return new GammaExpansionEffect();
41 case EFFECT_GAMMA_COMPRESSION:
42 return new GammaCompressionEffect();
43 case EFFECT_COLOR_SPACE_CONVERSION:
44 return new ColorSpaceConversionEffect();
45 case EFFECT_LIFT_GAMMA_GAIN:
46 return new LiftGammaGainEffect();
47 case EFFECT_SATURATION:
48 return new SaturationEffect();
50 return new MirrorEffect();
52 return new VignetteEffect();
57 void EffectChain::normalize_to_linear_gamma()
59 if (current_gamma_curve == GAMMA_sRGB) {
60 // TODO: check if the extension exists
61 use_srgb_texture_format = true;
63 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
64 gamma_conversion->set_int("source_curve", current_gamma_curve);
65 effects.push_back(gamma_conversion);
67 current_gamma_curve = GAMMA_LINEAR;
70 void EffectChain::normalize_to_srgb()
72 assert(current_gamma_curve == GAMMA_LINEAR);
73 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
74 colorspace_conversion->set_int("source_space", current_color_space);
75 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
76 effects.push_back(colorspace_conversion);
77 current_color_space = COLORSPACE_sRGB;
80 Effect *EffectChain::add_effect(EffectId effect_id)
82 Effect *effect = instantiate_effect(effect_id);
84 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
85 normalize_to_linear_gamma();
88 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
93 assert(!effect->needs_many_samples());
94 assert(!effect->needs_mipmaps());
96 effects.push_back(effect);
100 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
101 std::string replace_prefix(const std::string &text, const std::string &prefix)
106 while (start < text.size()) {
107 size_t pos = text.find("PREFIX(", start);
108 if (pos == std::string::npos) {
109 output.append(text.substr(start, std::string::npos));
113 output.append(text.substr(start, pos - start));
114 output.append(prefix);
117 pos += strlen("PREFIX(");
119 // Output stuff until we find the matching ), which we then eat.
121 size_t end_arg_pos = pos;
122 while (end_arg_pos < text.size()) {
123 if (text[end_arg_pos] == '(') {
125 } else if (text[end_arg_pos] == ')') {
133 output.append(text.substr(pos, end_arg_pos - pos));
141 void EffectChain::finalize()
143 if (current_color_space != output_format.color_space) {
144 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
145 colorspace_conversion->set_int("source_space", current_color_space);
146 colorspace_conversion->set_int("destination_space", output_format.color_space);
147 effects.push_back(colorspace_conversion);
148 current_color_space = output_format.color_space;
151 if (current_gamma_curve != output_format.gamma_curve) {
152 if (current_gamma_curve != GAMMA_LINEAR) {
153 normalize_to_linear_gamma();
155 assert(current_gamma_curve == GAMMA_LINEAR);
156 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
157 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
158 effects.push_back(gamma_conversion);
159 current_gamma_curve = output_format.gamma_curve;
162 std::string vert_shader = read_file("header.vert");
163 for (unsigned i = 0; i < effects.size(); ++i) {
165 sprintf(effect_id, "eff%d", i);
168 vert_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
169 vert_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
170 vert_shader += replace_prefix(effects[i]->output_vertex_shader(), effect_id);
171 vert_shader += "#undef PREFIX\n";
172 vert_shader += "#undef FUNCNAME\n";
173 vert_shader += "#undef LAST_INPUT\n";
174 vert_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
177 vert_shader.append(read_file("footer.vert"));
178 printf("%s\n", vert_shader.c_str());
180 std::string frag_shader = read_file("header.frag");
181 for (unsigned i = 0; i < effects.size(); ++i) {
183 sprintf(effect_id, "eff%d", i);
186 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
187 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
188 frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
189 frag_shader += "#undef PREFIX\n";
190 frag_shader += "#undef FUNCNAME\n";
191 frag_shader += "#undef LAST_INPUT\n";
192 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
195 frag_shader.append(read_file("footer.frag"));
196 printf("%s\n", frag_shader.c_str());
198 glsl_program_num = glCreateProgram();
199 GLuint vs_obj = compile_shader(vert_shader, GL_VERTEX_SHADER);
200 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
201 glAttachShader(glsl_program_num, vs_obj);
203 glAttachShader(glsl_program_num, fs_obj);
205 glLinkProgram(glsl_program_num);
208 // Translate the format to OpenGL's enums.
209 GLenum internal_format;
210 if (use_srgb_texture_format) {
211 internal_format = GL_SRGB8;
213 internal_format = GL_RGBA8;
215 if (input_format.pixel_format == FORMAT_RGB) {
218 } else if (input_format.pixel_format == FORMAT_RGBA) {
221 } else if (input_format.pixel_format == FORMAT_BGR) {
224 } else if (input_format.pixel_format == FORMAT_BGRA) {
231 // Create PBO to hold the texture, and then the texture itself.
232 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
234 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
237 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
238 memset(mapped_pbo, 0, width * height * bytes_per_pixel);
239 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
241 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
243 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
245 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
251 void EffectChain::render_to_screen(unsigned char *src)
255 // Copy the pixel data into the PBO.
256 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
258 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
259 memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
260 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
263 // Re-upload the texture from the PBO.
264 glActiveTexture(GL_TEXTURE0);
266 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
268 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
270 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
273 glUseProgram(glsl_program_num);
277 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
279 for (unsigned i = 0; i < effects.size(); ++i) {
281 sprintf(effect_id, "eff%d", i);
282 effects[i]->set_uniforms(glsl_program_num, effect_id);
287 glDisable(GL_DEPTH_TEST);
289 glDepthMask(GL_FALSE);
292 glMatrixMode(GL_PROJECTION);
294 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
296 glMatrixMode(GL_MODELVIEW);
301 glTexCoord2f(0.0f, 1.0f);
302 glVertex2f(0.0f, 0.0f);
304 glTexCoord2f(1.0f, 1.0f);
305 glVertex2f(1.0f, 0.0f);
307 glTexCoord2f(1.0f, 0.0f);
308 glVertex2f(1.0f, 1.0f);
310 glTexCoord2f(0.0f, 0.0f);
311 glVertex2f(0.0f, 1.0f);