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 "texture_enum.h"
18 EffectChain::EffectChain(unsigned width, unsigned height)
19 : width(width), height(height), finalized(false) {}
21 void EffectChain::add_input(const ImageFormat &format)
23 input_format = format;
24 current_color_space = format.color_space;
25 current_gamma_curve = format.gamma_curve;
28 void EffectChain::add_output(const ImageFormat &format)
30 output_format = format;
33 Effect *instantiate_effect(EffectId effect)
36 case GAMMA_CONVERSION:
37 return new GammaExpansionEffect();
38 case RGB_PRIMARIES_CONVERSION:
39 return new GammaExpansionEffect();
41 return new LiftGammaGainEffect();
46 void EffectChain::normalize_to_linear_gamma()
48 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
49 gamma_conversion->set_int("source_curve", current_gamma_curve);
50 effects.push_back(gamma_conversion);
51 current_gamma_curve = GAMMA_LINEAR;
54 void EffectChain::normalize_to_srgb()
56 assert(current_gamma_curve == GAMMA_LINEAR);
57 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
58 colorspace_conversion->set_int("source_space", current_color_space);
59 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
60 effects.push_back(colorspace_conversion);
61 current_color_space = COLORSPACE_sRGB;
64 Effect *EffectChain::add_effect(EffectId effect_id)
66 Effect *effect = instantiate_effect(effect_id);
68 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
69 normalize_to_linear_gamma();
72 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
77 assert(!effect->needs_many_samples());
78 assert(!effect->needs_mipmaps());
80 effects.push_back(effect);
84 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
85 std::string replace_prefix(const std::string &text, const std::string &prefix)
90 while (start < text.size()) {
91 size_t pos = text.find("PREFIX(", start);
92 if (pos == std::string::npos) {
93 output.append(text.substr(start, std::string::npos));
97 output.append(text.substr(start, pos - start));
98 output.append(prefix);
101 pos += strlen("PREFIX(");
103 // Output stuff until we find the matching ), which we then eat.
105 size_t end_arg_pos = pos;
106 while (end_arg_pos < text.size()) {
107 if (text[end_arg_pos] == '(') {
109 } else if (text[end_arg_pos] == ')') {
117 output.append(text.substr(pos, end_arg_pos - pos));
125 void EffectChain::finalize()
127 // TODO: If we want a non-sRGB output color space, convert.
129 if (current_gamma_curve != output_format.gamma_curve) {
130 if (current_gamma_curve != GAMMA_LINEAR) {
131 normalize_to_linear_gamma();
133 assert(current_gamma_curve == GAMMA_LINEAR);
134 GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
135 gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
136 effects.push_back(gamma_conversion);
137 current_gamma_curve = output_format.gamma_curve;
140 std::string frag_shader = read_file("header.glsl");
142 for (unsigned i = 0; i < effects.size(); ++i) {
144 sprintf(effect_id, "eff%d", i);
147 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
148 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
149 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
150 frag_shader += "#undef PREFIX\n";
151 frag_shader += "#undef FUNCNAME\n";
152 frag_shader += "#undef LAST_INPUT\n";
153 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
156 frag_shader.append(read_file("footer.glsl"));
157 printf("%s\n", frag_shader.c_str());
159 glsl_program_num = glCreateProgram();
160 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
161 GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
162 glAttachShader(glsl_program_num, vs_obj);
164 glAttachShader(glsl_program_num, fs_obj);
166 glLinkProgram(glsl_program_num);
172 void EffectChain::render_to_screen(unsigned char *src)
177 glUseProgram(glsl_program_num);
180 glActiveTexture(GL_TEXTURE0);
181 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
183 // TODO: use sRGB textures if applicable
184 if (input_format.pixel_format == FORMAT_RGB) {
185 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
186 } else if (input_format.pixel_format == FORMAT_RGBA) {
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
192 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
194 //for (unsigned i = 0; i < effects.size(); ++i) {
195 // effects[i]->set_uniforms();
200 glDisable(GL_DEPTH_TEST);
202 glDepthMask(GL_FALSE);
205 glMatrixMode(GL_PROJECTION);
207 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
209 glMatrixMode(GL_MODELVIEW);
214 glTexCoord2f(0.0f, 1.0f);
215 glVertex2f(0.0f, 0.0f);
217 glTexCoord2f(1.0f, 1.0f);
218 glVertex2f(1.0f, 0.0f);
220 glTexCoord2f(1.0f, 0.0f);
221 glVertex2f(1.0f, 1.0f);
223 glTexCoord2f(0.0f, 0.0f);
224 glVertex2f(0.0f, 1.0f);