Start actually piecing together the GLSL shaders from the effect chain.
[movit] / effect_chain.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2
3 #include <stdio.h>
4 #include <assert.h>
5
6 #include <GL/gl.h>
7 #include <GL/glext.h>
8
9 #include "util.h"
10 #include "effect_chain.h"
11 #include "gamma_expansion_effect.h"
12 #include "lift_gamma_gain_effect.h"
13 #include "colorspace_conversion_effect.h"
14
15 EffectChain::EffectChain(unsigned width, unsigned height)
16         : width(width), height(height), finalized(false) {}
17
18 void EffectChain::add_input(const ImageFormat &format)
19 {
20         input_format = format;
21         current_color_space = format.color_space;
22         current_gamma_curve = format.gamma_curve;
23 }
24
25 void EffectChain::add_output(const ImageFormat &format)
26 {
27         output_format = format;
28 }
29         
30 Effect *instantiate_effect(EffectId effect)
31 {
32         switch (effect) {
33         case GAMMA_CONVERSION:
34                 return new GammaExpansionEffect();
35         case RGB_PRIMARIES_CONVERSION:
36                 return new GammaExpansionEffect();
37         case LIFT_GAMMA_GAIN:
38                 return new LiftGammaGainEffect();
39         }
40         assert(false);
41 }
42
43 Effect *EffectChain::add_effect(EffectId effect_id)
44 {
45         Effect *effect = instantiate_effect(effect_id);
46
47         if (effect->needs_linear_light() && current_gamma_curve != GAMMA_LINEAR) {
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;
52         }
53
54         if (effect->needs_srgb_primaries() && current_color_space != COLORSPACE_sRGB) {
55                 assert(current_gamma_curve == GAMMA_LINEAR);
56                 ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
57                 colorspace_conversion->set_int("source_space", current_color_space);
58                 colorspace_conversion->set_int("destination_space", COLORSPACE_sRGB);
59                 effects.push_back(colorspace_conversion);
60                 current_color_space = COLORSPACE_sRGB;
61         }
62
63         // not handled yet
64         assert(!effect->needs_many_samples());
65         assert(!effect->needs_mipmaps());
66
67         effects.push_back(effect);
68         return effect;
69 }
70
71 void EffectChain::finalize()
72 {
73         std::string frag_shader = read_file("header.glsl");
74
75         for (unsigned i = 0; i < effects.size(); ++i) {
76                 char effect_id[256];
77                 sprintf(effect_id, "eff%d", i);
78         
79                 frag_shader += "\n";
80                 frag_shader += std::string("#define PREFIX(x) ") + effect_id + "_ ## x\n";
81                 frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
82                 frag_shader += effects[i]->output_glsl();
83                 frag_shader += "#undef PREFIX\n";
84                 frag_shader += "#undef FUNCNAME\n";
85                 frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
86                 frag_shader += "\n";
87         }
88         printf("%s\n", frag_shader.c_str());
89         
90         glsl_program_num = glCreateProgram();
91         GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
92         GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
93         glAttachObjectARB(glsl_program_num, vs_obj);
94         check_error();
95         glAttachObjectARB(glsl_program_num, fs_obj);
96         check_error();
97         glLinkProgram(glsl_program_num);
98         check_error();
99 }