]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Disable dither explicitly per frame; fixes some weird artifacts I found.
[movit] / effect_chain.cpp
index 471d603079ef201d0d41e2e32c193c8899fbd867..b6b5f59925df18b83ea75100e276e6d153076310 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,19 +79,22 @@ 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)
+                                   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;
 
        assert(ycbcr_format.chroma_subsampling_x == 1);
        assert(ycbcr_format.chroma_subsampling_y == 1);
@@ -206,7 +212,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;
@@ -361,7 +367,31 @@ void EffectChain::compile_glsl_program(Phase *phase)
                frag_shader += "\n";
        }
        frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
-       frag_shader.append(read_version_dependent_file("footer", "frag"));
+
+       // 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_ycbcr) {
+               switch (output_ycbcr_splitting) {
+               case YCBCR_OUTPUT_INTERLEAVED:
+                       // No #defines set.
+                       break;
+               case YCBCR_OUTPUT_SPLIT_Y_AND_CBCR:
+                       frag_shader += "#define YCBCR_OUTPUT_SPLIT_Y_AND_CBCR 1\n";
+                       break;
+               case YCBCR_OUTPUT_PLANAR:
+                       frag_shader += "#define YCBCR_OUTPUT_PLANAR 1\n";
+                       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.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
@@ -389,6 +419,17 @@ 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");
+
+       // 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);
 
        // Collect the resulting location numbers for each uniform.
@@ -1489,8 +1530,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();
@@ -1605,6 +1646,10 @@ 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.
+       glDisable(GL_DITHER);
+
        // Save original viewport.
        GLuint x = 0, y = 0;
 
@@ -1625,6 +1670,23 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
        glDepthMask(GL_FALSE);
        check_error();
 
+       // Generate a VAO. All the phases should have exactly the same vertex attributes,
+       // so it's safe to reuse this.
+       float vertices[] = {
+               0.0f, 2.0f,
+               0.0f, 0.0f,
+               2.0f, 0.0f
+       };
+
+       GLuint vao;
+       glGenVertexArrays(1, &vao);
+       check_error();
+       glBindVertexArray(vao);
+       check_error();
+
+       GLuint position_vbo = fill_vertex_attribute(phases[0]->glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
+       GLuint texcoord_vbo = fill_vertex_attribute(phases[0]->glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
+
        set<Phase *> generated_mipmaps;
 
        // We choose the simplest option of having one texture per output,
@@ -1666,6 +1728,12 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
        glUseProgram(0);
        check_error();
 
+       cleanup_vertex_attribute(phases[0]->glsl_program_num, "position", position_vbo);
+       cleanup_vertex_attribute(phases[0]->glsl_program_num, "texcoord", texcoord_vbo);
+
+       glDeleteVertexArrays(1, &vao);
+       check_error();
+
        if (do_phase_timing) {
                // Get back the timer queries.
                for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) {
@@ -1779,28 +1847,9 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, map<Phase *, GLui
        // from there.
        setup_uniforms(phase);
 
-       // Now draw!
-       float vertices[] = {
-               0.0f, 2.0f,
-               0.0f, 0.0f,
-               2.0f, 0.0f
-       };
-
-       GLuint vao;
-       glGenVertexArrays(1, &vao);
-       check_error();
-       glBindVertexArray(vao);
-       check_error();
-
-       GLuint position_vbo = fill_vertex_attribute(glsl_program_num, "position", 2, GL_FLOAT, sizeof(vertices), vertices);
-       GLuint texcoord_vbo = fill_vertex_attribute(glsl_program_num, "texcoord", 2, GL_FLOAT, sizeof(vertices), vertices);  // Same as vertices.
-
        glDrawArrays(GL_TRIANGLES, 0, 3);
        check_error();
 
-       cleanup_vertex_attribute(glsl_program_num, "position", position_vbo);
-       cleanup_vertex_attribute(glsl_program_num, "texcoord", texcoord_vbo);
-       
        glUseProgram(0);
        check_error();
 
@@ -1812,9 +1861,6 @@ void EffectChain::execute_phase(Phase *phase, bool last_phase, map<Phase *, GLui
        if (!last_phase) {
                resource_pool->release_fbo(fbo);
        }
-
-       glDeleteVertexArrays(1, &vao);
-       check_error();
 }
 
 void EffectChain::setup_uniforms(Phase *phase)