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 "texture_enum.h"
19 EffectChain::EffectChain(unsigned width, unsigned height)
20 : width(width), height(height), use_srgb_texture_format(false), finalized(false) {}
22 void EffectChain::add_input(const ImageFormat &format)
24 input_format = format;
25 current_color_space = format.color_space;
26 current_gamma_curve = format.gamma_curve;
29 void EffectChain::add_output(const ImageFormat &format)
31 output_format = format;
34 Effect *instantiate_effect(EffectId effect)
37 case EFFECT_GAMMA_EXPANSION:
38 return new GammaExpansionEffect();
39 case EFFECT_GAMMA_COMPRESSION:
40 return new GammaCompressionEffect();
41 case EFFECT_LIFT_GAMMA_GAIN:
42 return new LiftGammaGainEffect();
43 case EFFECT_SATURATION:
44 return new SaturationEffect();
49 void EffectChain::normalize_to_linear_gamma()
51 if (current_gamma_curve == GAMMA_sRGB) {
52 // TODO: check if the extension exists
53 use_srgb_texture_format = true;
55 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
56 gamma_conversion->set_int("source_curve", current_gamma_curve);
57 effects.push_back(gamma_conversion);
59 current_gamma_curve = GAMMA_LINEAR;
62 void EffectChain::normalize_to_srgb()
64 assert(current_gamma_curve == GAMMA_LINEAR);
65 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
66 colorspace_conversion->set_int("source_space", current_color_space);
67 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
68 effects.push_back(colorspace_conversion);
69 current_color_space = COLORSPACE_sRGB;
72 Effect *EffectChain::add_effect(EffectId effect_id)
74 Effect *effect = instantiate_effect(effect_id);
76 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
77 normalize_to_linear_gamma();
80 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
85 assert(!effect->needs_many_samples());
86 assert(!effect->needs_mipmaps());
88 effects.push_back(effect);
92 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
93 std::string replace_prefix(const std::string &text, const std::string &prefix)
98 while (start < text.size()) {
99 size_t pos = text.find("PREFIX(", start);
100 if (pos == std::string::npos) {
101 output.append(text.substr(start, std::string::npos));
105 output.append(text.substr(start, pos - start));
106 output.append(prefix);
109 pos += strlen("PREFIX(");
111 // Output stuff until we find the matching ), which we then eat.
113 size_t end_arg_pos = pos;
114 while (end_arg_pos < text.size()) {
115 if (text[end_arg_pos] == '(') {
117 } else if (text[end_arg_pos] == ')') {
125 output.append(text.substr(pos, end_arg_pos - pos));
133 void EffectChain::finalize()
135 // TODO: If we want a non-sRGB output color space, convert.
137 if (current_gamma_curve != output_format.gamma_curve) {
138 if (current_gamma_curve != GAMMA_LINEAR) {
139 normalize_to_linear_gamma();
141 assert(current_gamma_curve == GAMMA_LINEAR);
142 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
143 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
144 effects.push_back(gamma_conversion);
145 current_gamma_curve = output_format.gamma_curve;
148 std::string frag_shader = read_file("header.glsl");
150 for (unsigned i = 0; i < effects.size(); ++i) {
152 sprintf(effect_id, "eff%d", i);
155 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
156 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
157 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
158 frag_shader += "#undef PREFIX\n";
159 frag_shader += "#undef FUNCNAME\n";
160 frag_shader += "#undef LAST_INPUT\n";
161 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
164 frag_shader.append(read_file("footer.glsl"));
165 printf("%s\n", frag_shader.c_str());
167 glsl_program_num = glCreateProgram();
168 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
169 GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
170 glAttachShader(glsl_program_num, vs_obj);
172 glAttachShader(glsl_program_num, fs_obj);
174 glLinkProgram(glsl_program_num);
180 void EffectChain::render_to_screen(unsigned char *src)
185 glUseProgram(glsl_program_num);
188 glActiveTexture(GL_TEXTURE0);
189 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
191 GLenum internal_format = GL_RGBA8;
192 if (use_srgb_texture_format) {
193 internal_format = GL_SRGB8;
196 if (input_format.pixel_format == FORMAT_RGB) {
197 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
198 } else if (input_format.pixel_format == FORMAT_RGBA) {
199 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
204 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
206 for (unsigned i = 0; i < effects.size(); ++i) {
208 sprintf(effect_id, "eff%d", i);
209 effects[i]->set_uniforms(glsl_program_num, effect_id);
214 glDisable(GL_DEPTH_TEST);
216 glDepthMask(GL_FALSE);
219 glMatrixMode(GL_PROJECTION);
221 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
223 glMatrixMode(GL_MODELVIEW);
228 glTexCoord2f(0.0f, 1.0f);
229 glVertex2f(0.0f, 0.0f);
231 glTexCoord2f(1.0f, 1.0f);
232 glVertex2f(1.0f, 0.0f);
234 glTexCoord2f(1.0f, 0.0f);
235 glVertex2f(1.0f, 1.0f);
237 glTexCoord2f(0.0f, 0.0f);
238 glVertex2f(0.0f, 1.0f);