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 "vignette_effect.h"
18 #include "texture_enum.h"
20 EffectChain::EffectChain(unsigned width, unsigned height)
21 : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
23 void EffectChain::add_input(const ImageFormat &format)
25 input_format = format;
26 current_color_space = format.color_space;
27 current_gamma_curve = format.gamma_curve;
30 void EffectChain::add_output(const ImageFormat &format)
32 output_format = format;
35 Effect *instantiate_effect(EffectId effect)
38 case EFFECT_GAMMA_EXPANSION:
39 return new GammaExpansionEffect();
40 case EFFECT_GAMMA_COMPRESSION:
41 return new GammaCompressionEffect();
42 case EFFECT_COLOR_SPACE_CONVERSION:
43 return new ColorSpaceConversionEffect();
44 case EFFECT_LIFT_GAMMA_GAIN:
45 return new LiftGammaGainEffect();
46 case EFFECT_SATURATION:
47 return new SaturationEffect();
49 return new VignetteEffect();
54 void EffectChain::normalize_to_linear_gamma()
56 if (current_gamma_curve == GAMMA_sRGB) {
57 // TODO: check if the extension exists
58 use_srgb_texture_format = true;
60 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
61 gamma_conversion->set_int("source_curve", current_gamma_curve);
62 effects.push_back(gamma_conversion);
64 current_gamma_curve = GAMMA_LINEAR;
67 void EffectChain::normalize_to_srgb()
69 assert(current_gamma_curve == GAMMA_LINEAR);
70 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
71 colorspace_conversion->set_int("source_space", current_color_space);
72 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
73 effects.push_back(colorspace_conversion);
74 current_color_space = COLORSPACE_sRGB;
77 Effect *EffectChain::add_effect(EffectId effect_id)
79 Effect *effect = instantiate_effect(effect_id);
81 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
82 normalize_to_linear_gamma();
85 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
90 assert(!effect->needs_many_samples());
91 assert(!effect->needs_mipmaps());
93 effects.push_back(effect);
97 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
98 std::string replace_prefix(const std::string &text, const std::string &prefix)
103 while (start < text.size()) {
104 size_t pos = text.find("PREFIX(", start);
105 if (pos == std::string::npos) {
106 output.append(text.substr(start, std::string::npos));
110 output.append(text.substr(start, pos - start));
111 output.append(prefix);
114 pos += strlen("PREFIX(");
116 // Output stuff until we find the matching ), which we then eat.
118 size_t end_arg_pos = pos;
119 while (end_arg_pos < text.size()) {
120 if (text[end_arg_pos] == '(') {
122 } else if (text[end_arg_pos] == ')') {
130 output.append(text.substr(pos, end_arg_pos - pos));
138 void EffectChain::finalize()
140 if (current_color_space != output_format.color_space) {
141 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
142 colorspace_conversion->set_int("source_space", current_color_space);
143 colorspace_conversion->set_int("destination_space", output_format.color_space);
144 effects.push_back(colorspace_conversion);
145 current_color_space = output_format.color_space;
148 if (current_gamma_curve != output_format.gamma_curve) {
149 if (current_gamma_curve != GAMMA_LINEAR) {
150 normalize_to_linear_gamma();
152 assert(current_gamma_curve == GAMMA_LINEAR);
153 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
154 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
155 effects.push_back(gamma_conversion);
156 current_gamma_curve = output_format.gamma_curve;
159 std::string frag_shader = read_file("header.glsl");
161 for (unsigned i = 0; i < effects.size(); ++i) {
163 sprintf(effect_id, "eff%d", i);
166 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
167 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
168 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
169 frag_shader += "#undef PREFIX\n";
170 frag_shader += "#undef FUNCNAME\n";
171 frag_shader += "#undef LAST_INPUT\n";
172 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
175 frag_shader.append(read_file("footer.glsl"));
176 printf("%s\n", frag_shader.c_str());
178 glsl_program_num = glCreateProgram();
179 GLuint vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
180 GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
181 glAttachShader(glsl_program_num, vs_obj);
183 glAttachShader(glsl_program_num, fs_obj);
185 glLinkProgram(glsl_program_num);
191 void EffectChain::render_to_screen(unsigned char *src)
196 glUseProgram(glsl_program_num);
199 glActiveTexture(GL_TEXTURE0);
200 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
202 GLenum format, internal_format;
203 if (use_srgb_texture_format) {
204 internal_format = GL_SRGB8;
206 internal_format = GL_RGBA8;
208 if (input_format.pixel_format == FORMAT_RGB) {
210 } else if (input_format.pixel_format == FORMAT_RGBA) {
212 } else if (input_format.pixel_format == FORMAT_BGR) {
214 } else if (input_format.pixel_format == FORMAT_BGRA) {
220 static bool first = true;
222 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, src);
224 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, src);
227 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
229 for (unsigned i = 0; i < effects.size(); ++i) {
231 sprintf(effect_id, "eff%d", i);
232 effects[i]->set_uniforms(glsl_program_num, effect_id);
237 glDisable(GL_DEPTH_TEST);
239 glDepthMask(GL_FALSE);
242 glMatrixMode(GL_PROJECTION);
244 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
246 glMatrixMode(GL_MODELVIEW);
251 glTexCoord2f(0.0f, 1.0f);
252 glVertex2f(0.0f, 0.0f);
254 glTexCoord2f(1.0f, 1.0f);
255 glVertex2f(1.0f, 0.0f);
257 glTexCoord2f(1.0f, 0.0f);
258 glVertex2f(1.0f, 1.0f);
260 glTexCoord2f(0.0f, 0.0f);
261 glVertex2f(0.0f, 1.0f);