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 frag_shader = read_file("header.frag");
163 for (unsigned i = 0; i < effects.size(); ++i) {
165 sprintf(effect_id, "eff%d", i);
168 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
169 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
170 frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id);
171 frag_shader += "#undef PREFIX\n";
172 frag_shader += "#undef FUNCNAME\n";
173 frag_shader += "#undef LAST_INPUT\n";
174 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
177 frag_shader.append(read_file("footer.frag"));
178 printf("%s\n", frag_shader.c_str());
180 glsl_program_num = glCreateProgram();
181 GLuint vs_obj = compile_shader(read_file("vs.vert"), GL_VERTEX_SHADER);
182 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
183 glAttachShader(glsl_program_num, vs_obj);
185 glAttachShader(glsl_program_num, fs_obj);
187 glLinkProgram(glsl_program_num);
190 // Translate the format to OpenGL's enums.
191 GLenum internal_format;
192 if (use_srgb_texture_format) {
193 internal_format = GL_SRGB8;
195 internal_format = GL_RGBA8;
197 if (input_format.pixel_format == FORMAT_RGB) {
200 } else if (input_format.pixel_format == FORMAT_RGBA) {
203 } else if (input_format.pixel_format == FORMAT_BGR) {
206 } else if (input_format.pixel_format == FORMAT_BGRA) {
213 // Create PBO to hold the texture, and then the texture itself.
214 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
216 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW);
219 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
220 memset(mapped_pbo, 0, width * height * bytes_per_pixel);
221 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
223 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
225 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
227 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
233 void EffectChain::render_to_screen(unsigned char *src)
237 // Copy the pixel data into the PBO.
238 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2);
240 void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
241 memcpy(mapped_pbo, src, width * height * bytes_per_pixel);
242 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
245 // Re-upload the texture from the PBO.
246 glActiveTexture(GL_TEXTURE0);
248 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
250 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
252 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
255 glUseProgram(glsl_program_num);
259 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
261 for (unsigned i = 0; i < effects.size(); ++i) {
263 sprintf(effect_id, "eff%d", i);
264 effects[i]->set_uniforms(glsl_program_num, effect_id);
269 glDisable(GL_DEPTH_TEST);
271 glDepthMask(GL_FALSE);
274 glMatrixMode(GL_PROJECTION);
276 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
278 glMatrixMode(GL_MODELVIEW);
283 glTexCoord2f(0.0f, 1.0f);
284 glVertex2f(0.0f, 0.0f);
286 glTexCoord2f(1.0f, 1.0f);
287 glVertex2f(1.0f, 0.0f);
289 glTexCoord2f(1.0f, 0.0f);
290 glVertex2f(1.0f, 1.0f);
292 glTexCoord2f(0.0f, 0.0f);
293 glVertex2f(0.0f, 1.0f);