+ Phase phase;
+ phase.glsl_program_num = glsl_program_num;
+ phase.input_needs_mipmaps = input_needs_mipmaps;
+ phase.start = start_index;
+ phase.end = end_index;
+
+ return phase;
+}
+
+void EffectChain::finalize()
+{
+ // Add normalizers to get the output format right.
+ if (current_color_space != output_format.color_space) {
+ ColorSpaceConversionEffect *colorspace_conversion = new ColorSpaceConversionEffect();
+ colorspace_conversion->set_int("source_space", current_color_space);
+ colorspace_conversion->set_int("destination_space", output_format.color_space);
+ effects.push_back(colorspace_conversion);
+ current_color_space = output_format.color_space;
+ }
+ if (current_gamma_curve != output_format.gamma_curve) {
+ if (current_gamma_curve != GAMMA_LINEAR) {
+ normalize_to_linear_gamma();
+ }
+ assert(current_gamma_curve == GAMMA_LINEAR);
+ GammaCompressionEffect *gamma_conversion = new GammaCompressionEffect();
+ gamma_conversion->set_int("destination_curve", output_format.gamma_curve);
+ effects.push_back(gamma_conversion);
+ current_gamma_curve = output_format.gamma_curve;
+ }
+
+ // Construct the GLSL programs. We end a program every time we come
+ // to an effect marked as "needs many samples" (ie. "please let me
+ // sample directly from a texture, with no arithmetic in-between"),
+ // and of course at the end.
+ unsigned start = 0;
+ for (unsigned i = 0; i < effects.size(); ++i) {
+ if (effects[i]->needs_texture_bounce() && i != start) {
+ phases.push_back(compile_glsl_program(start, i));
+ start = i;
+ }
+ }
+ phases.push_back(compile_glsl_program(start, effects.size()));
+
+ // If we have more than one phase, we need intermediate render-to-texture.
+ // Construct an FBO, and then as many textures as we need.
+ if (phases.size() > 1) {
+ glGenFramebuffers(1, &fbo);
+
+ unsigned num_textures = std::max<int>(phases.size() - 1, 2);
+ glGenTextures(num_textures, temp_textures);
+
+ for (unsigned i = 0; i < num_textures; ++i) {
+ glBindTexture(GL_TEXTURE_2D, temp_textures[i]);
+ check_error();
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ check_error();
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ check_error();
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+ check_error();
+ }
+ }
+
+ // Translate the input format to OpenGL's enums.