]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Explicitly bind fragment shader outputs in order.
[movit] / effect_chain.cpp
index 4c2df4c2de6dbce9563c4f34700891b113acdc7e..c2402daeaef400d9ded80dcbce3378be9f94ac42 100644 (file)
@@ -37,8 +37,11 @@ namespace movit {
 EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool)
        : aspect_nom(aspect_nom),
          aspect_denom(aspect_denom),
+         output_color_rgba(false),
+         output_color_ycbcr(false),
          dither_effect(NULL),
          num_dither_bits(0),
+         output_origin(OUTPUT_ORIGIN_BOTTOM_LEFT),
          finalized(false),
          resource_pool(resource_pool),
          do_phase_timing(false) {
@@ -76,18 +79,20 @@ Input *EffectChain::add_input(Input *input)
 void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
 {
        assert(!finalized);
+       assert(!output_color_rgba);
        output_format = format;
        output_alpha_format = alpha_format;
-       output_color_type = OUTPUT_COLOR_RGB;
+       output_color_rgba = true;
 }
 
 void EffectChain::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format,
                                    const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
 {
        assert(!finalized);
+       assert(!output_color_ycbcr);
        output_format = format;
        output_alpha_format = alpha_format;
-       output_color_type = OUTPUT_COLOR_YCBCR;
+       output_color_ycbcr = true;
        output_ycbcr_format = ycbcr_format;
        output_ycbcr_splitting = output_splitting;
 
@@ -179,6 +184,13 @@ GLenum EffectChain::get_input_sampler(Node *node, unsigned input_num) const
        return GL_TEXTURE0 + node->incoming_links[input_num]->bound_sampler_num;
 }
 
+GLenum EffectChain::has_input_sampler(Node *node, unsigned input_num) const
+{
+       assert(input_num < node->incoming_links.size());
+       return node->incoming_links[input_num]->bound_sampler_num >= 0 &&
+               node->incoming_links[input_num]->bound_sampler_num < 8;
+}
+
 void EffectChain::find_all_nonlinear_inputs(Node *node, vector<Node *> *nonlinear_inputs)
 {
        if (node->output_gamma_curve == GAMMA_LINEAR &&
@@ -207,7 +219,7 @@ Effect *EffectChain::add_effect(Effect *effect, const vector<Effect *> &inputs)
        return effect;
 }
 
-// GLSL pre-1.30 doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
+// ESSL doesn't support token pasting. Replace PREFIX(x) with <effect_id>_x.
 string replace_prefix(const string &text, const string &prefix)
 {
        string output;
@@ -364,22 +376,37 @@ void EffectChain::compile_glsl_program(Phase *phase)
        frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
 
        // If we're the last phase, add the right #defines for Y'CbCr multi-output as needed.
-       if (phase->output_node->outgoing_links.empty() && output_color_type == OUTPUT_COLOR_YCBCR) {
+       vector<string> frag_shader_outputs;  // In order.
+       if (phase->output_node->outgoing_links.empty() && output_color_ycbcr) {
                switch (output_ycbcr_splitting) {
                case YCBCR_OUTPUT_INTERLEAVED:
                        // No #defines set.
+                       frag_shader_outputs.push_back("FragColor");
                        break;
                case YCBCR_OUTPUT_SPLIT_Y_AND_CBCR:
                        frag_shader += "#define YCBCR_OUTPUT_SPLIT_Y_AND_CBCR 1\n";
+                       frag_shader_outputs.push_back("Y");
+                       frag_shader_outputs.push_back("Chroma");
                        break;
                case YCBCR_OUTPUT_PLANAR:
                        frag_shader += "#define YCBCR_OUTPUT_PLANAR 1\n";
+                       frag_shader_outputs.push_back("Y");
+                       frag_shader_outputs.push_back("Cb");
+                       frag_shader_outputs.push_back("Cr");
                        break;
                default:
                        assert(false);
                }
+
+               if (output_color_rgba) {
+                       // Note: Needs to come in the header, because not only the
+                       // output needs to see it (YCbCrConversionEffect and DitherEffect
+                       // do, too).
+                       frag_shader_header += "#define YCBCR_ALSO_OUTPUT_RGBA 1\n";
+                       frag_shader_outputs.push_back("RGBA");
+               }
        }
-       frag_shader.append(read_version_dependent_file("footer", "frag"));
+       frag_shader.append(read_file("footer.frag"));
 
        // Collect uniforms from all effects and output them. Note that this needs
        // to happen after output_fragment_shader(), even though the uniforms come
@@ -399,7 +426,9 @@ void EffectChain::compile_glsl_program(Phase *phase)
                extract_uniform_declarations(effect->uniforms_vec2, "vec2", effect_id, &phase->uniforms_vec2, &frag_shader_uniforms);
                extract_uniform_declarations(effect->uniforms_vec3, "vec3", effect_id, &phase->uniforms_vec3, &frag_shader_uniforms);
                extract_uniform_declarations(effect->uniforms_vec4, "vec4", effect_id, &phase->uniforms_vec4, &frag_shader_uniforms);
+               extract_uniform_array_declarations(effect->uniforms_float_array, "float", effect_id, &phase->uniforms_float, &frag_shader_uniforms);
                extract_uniform_array_declarations(effect->uniforms_vec2_array, "vec2", effect_id, &phase->uniforms_vec2, &frag_shader_uniforms);
+               extract_uniform_array_declarations(effect->uniforms_vec3_array, "vec3", effect_id, &phase->uniforms_vec3, &frag_shader_uniforms);
                extract_uniform_array_declarations(effect->uniforms_vec4_array, "vec4", effect_id, &phase->uniforms_vec4, &frag_shader_uniforms);
                extract_uniform_declarations(effect->uniforms_mat3, "mat3", effect_id, &phase->uniforms_mat3, &frag_shader_uniforms);
        }
@@ -407,7 +436,18 @@ void EffectChain::compile_glsl_program(Phase *phase)
        frag_shader = frag_shader_header + frag_shader_uniforms + frag_shader;
 
        string vert_shader = read_version_dependent_file("vs", "vert");
-       phase->glsl_program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
+
+       // If we're the last phase and need to flip the picture to compensate for
+       // the origin, tell the vertex shader so.
+       if (phase->output_node->outgoing_links.empty() && output_origin == OUTPUT_ORIGIN_TOP_LEFT) {
+               const string needle = "#define FLIP_ORIGIN 0";
+               size_t pos = vert_shader.find(needle);
+               assert(pos != string::npos);
+
+               vert_shader[pos + needle.size() - 1] = '1';
+       }
+
+       phase->glsl_program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader, frag_shader_outputs);
 
        // Collect the resulting location numbers for each uniform.
        collect_uniform_locations(phase->glsl_program_num, &phase->uniforms_sampler2d);
@@ -477,7 +517,8 @@ Phase *EffectChain::construct_phase(Node *output, map<Node *, Phase *> *complete
                        bool start_new_phase = false;
 
                        if (node->effect->needs_texture_bounce() &&
-                           !deps[i]->effect->is_single_texture()) {
+                           !deps[i]->effect->is_single_texture() &&
+                           !deps[i]->effect->override_disable_bounce()) {
                                start_new_phase = true;
                        }
 
@@ -1507,8 +1548,8 @@ void EffectChain::fix_output_gamma()
 // gamma-encoded data.
 void EffectChain::add_ycbcr_conversion_if_needed()
 {
-       assert(output_color_type == OUTPUT_COLOR_RGB || output_color_type == OUTPUT_COLOR_YCBCR);
-       if (output_color_type != OUTPUT_COLOR_YCBCR) {
+       assert(output_color_rgba || output_color_ycbcr);
+       if (!output_color_ycbcr) {
                return;
        }
        Node *output = find_output_node();
@@ -1623,6 +1664,12 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
 {
        assert(finalized);
 
+       // This needs to be set anew, in case we are coming from a different context
+       // from when we initialized.
+       check_error();
+       glDisable(GL_DITHER);
+       check_error();
+
        // Save original viewport.
        GLuint x = 0, y = 0;
 
@@ -1636,6 +1683,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
        }
 
        // Basic state.
+       check_error();
        glDisable(GL_BLEND);
        check_error();
        glDisable(GL_DEPTH_TEST);