1 #define GL_GLEXT_PROTOTYPES 1
11 #include "effect_chain.h"
12 #include "gamma_expansion_effect.h"
13 #include "lift_gamma_gain_effect.h"
14 #include "colorspace_conversion_effect.h"
15 #include "texture_enum.h"
17 EffectChain::EffectChain(unsigned width, unsigned height)
18 : width(width), height(height), finalized(false) {}
20 void EffectChain::add_input(const ImageFormat &format)
22 input_format = format;
23 current_color_space = format.color_space;
24 current_gamma_curve = format.gamma_curve;
27 void EffectChain::add_output(const ImageFormat &format)
29 output_format = format;
32 Effect *instantiate_effect(EffectId effect)
35 case GAMMA_CONVERSION:
36 return new GammaExpansionEffect();
37 case RGB_PRIMARIES_CONVERSION:
38 return new GammaExpansionEffect();
40 return new LiftGammaGainEffect();
45 Effect *EffectChain::add_effect(EffectId effect_id)
47 Effect *effect = instantiate_effect(effect_id);
49 if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
50 GammaExpansionEffect *gamma_conversion = new GammaExpansionEffect();
51 gamma_conversion->set_int("source_curve", current_gamma_curve);
52 effects.push_back(gamma_conversion);
53 current_gamma_curve = GAMMA_LINEAR;
56 if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
57 assert(current_gamma_curve == GAMMA_LINEAR);
58 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
59 colorspace_conversion->set_int("source_space", current_color_space);
60 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
61 effects.push_back(colorspace_conversion);
62 current_color_space = COLORSPACE_sRGB;
66 assert(!effect->needs_many_samples());
67 assert(!effect->needs_mipmaps());
69 effects.push_back(effect);
73 // GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
74 std::string replace_prefix(const std::string &text, const std::string &prefix)
79 while (start < text.size()) {
80 size_t pos = text.find("PREFIX(", start);
81 if (pos == std::string::npos) {
82 output.append(text.substr(start, std::string::npos));
86 output.append(text.substr(start, pos - start));
87 output.append(prefix);
90 pos += strlen("PREFIX(");
92 // Output stuff until we find the matching ), which we then eat.
94 size_t end_arg_pos = pos;
95 while (end_arg_pos < text.size()) {
96 if (text[end_arg_pos] == '(') {
98 } else if (text[end_arg_pos] == ')') {
106 output.append(text.substr(pos, end_arg_pos - pos));
114 void EffectChain::finalize()
116 std::string frag_shader = read_file("header.glsl");
118 for (unsigned i = 0; i < effects.size(); ++i) {
120 sprintf(effect_id, "eff%d", i);
123 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
124 frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
125 frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
126 frag_shader += "#undef PREFIX\n";
127 frag_shader += "#undef FUNCNAME\n";
128 frag_shader += "#undef LAST_INPUT\n";
129 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
132 frag_shader.append(read_file("footer.glsl"));
133 printf("%s\n", frag_shader.c_str());
135 glsl_program_num = glCreateProgram();
136 GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
137 GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
138 glAttachShader(glsl_program_num, vs_obj);
140 glAttachShader(glsl_program_num, fs_obj);
142 glLinkProgram(glsl_program_num);
148 void EffectChain::render_to_screen(unsigned char *src)
153 glUseProgram(glsl_program_num);
156 glActiveTexture(GL_TEXTURE0);
157 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
159 // TODO: use sRGB textures if applicable
160 if (input_format.pixel_format == FORMAT_RGB) {
161 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
162 } else if (input_format.pixel_format == FORMAT_RGBA) {
163 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
168 glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
170 //for (unsigned i = 0; i < effects.size(); ++i) {
171 // effects[i]->set_uniforms();
176 glDisable(GL_DEPTH_TEST);
178 glDepthMask(GL_FALSE);
181 glMatrixMode(GL_PROJECTION);
183 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
185 glMatrixMode(GL_MODELVIEW);
190 glTexCoord2f(0.0f, 1.0f);
191 glVertex2f(0.0f, 0.0f);
193 glTexCoord2f(1.0f, 1.0f);
194 glVertex2f(1.0f, 0.0f);
196 glTexCoord2f(1.0f, 0.0f);
197 glVertex2f(1.0f, 1.0f);
199 glTexCoord2f(0.0f, 0.0f);
200 glVertex2f(0.0f, 1.0f);