]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Add a unit test for EffectChain without shared ResourcePool.
[movit] / effect_chain.cpp
index e6ba841ad6bd4737bc3a506384963233311c0678..cb807720b09de0620a09cac6a75fdfc95f507d6b 100644 (file)
@@ -44,9 +44,6 @@ EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *res
 EffectChain::~EffectChain()
 {
        for (unsigned i = 0; i < nodes.size(); ++i) {
-               if (nodes[i]->output_texture != 0) {
-                       glDeleteTextures(1, &nodes[i]->output_texture);
-               }
                delete nodes[i]->effect;
                delete nodes[i];
        }
@@ -80,20 +77,16 @@ Node *EffectChain::add_node(Effect *effect)
                assert(nodes[i]->effect != effect);
        }
 
-       char effect_id[256];
-       sprintf(effect_id, "eff%u", (unsigned)nodes.size());
-
        Node *node = new Node;
        node->effect = effect;
        node->disabled = false;
-       node->effect_id = effect_id;
        node->output_color_space = COLORSPACE_INVALID;
        node->output_gamma_curve = GAMMA_INVALID;
        node->output_alpha_type = ALPHA_INVALID;
-       node->output_texture = 0;
 
        nodes.push_back(node);
        node_map[effect] = node;
+       effect->inform_added(this);
        return node;
 }
 
@@ -224,6 +217,7 @@ Phase *EffectChain::compile_glsl_program(
        const std::vector<Node *> &inputs,
        const std::vector<Node *> &effects)
 {
+       Phase *phase = new Phase;
        assert(!effects.empty());
 
        // Deduplicate the inputs.
@@ -237,10 +231,13 @@ Phase *EffectChain::compile_glsl_program(
        // Create functions for all the texture inputs that we need.
        for (unsigned i = 0; i < true_inputs.size(); ++i) {
                Node *input = true_inputs[i];
+               char effect_id[256];
+               sprintf(effect_id, "in%u", i);
+               phase->effect_ids.insert(std::make_pair(input, effect_id));
        
-               frag_shader += std::string("uniform sampler2D tex_") + input->effect_id + ";\n";
-               frag_shader += std::string("vec4 ") + input->effect_id + "(vec2 tc) {\n";
-               frag_shader += "\treturn texture2D(tex_" + input->effect_id + ", tc);\n";
+               frag_shader += std::string("uniform sampler2D tex_") + effect_id + ";\n";
+               frag_shader += std::string("vec4 ") + effect_id + "(vec2 tc) {\n";
+               frag_shader += "\treturn texture2D(tex_" + std::string(effect_id) + ", tc);\n";
                frag_shader += "}\n";
                frag_shader += "\n";
        }
@@ -249,21 +246,24 @@ Phase *EffectChain::compile_glsl_program(
 
        for (unsigned i = 0; i < sorted_effects.size(); ++i) {
                Node *node = sorted_effects[i];
+               char effect_id[256];
+               sprintf(effect_id, "eff%u", i);
+               phase->effect_ids.insert(std::make_pair(node, effect_id));
 
                if (node->incoming_links.size() == 1) {
-                       frag_shader += std::string("#define INPUT ") + node->incoming_links[0]->effect_id + "\n";
+                       frag_shader += std::string("#define INPUT ") + phase->effect_ids[node->incoming_links[0]] + "\n";
                } else {
                        for (unsigned j = 0; j < node->incoming_links.size(); ++j) {
                                char buf[256];
-                               sprintf(buf, "#define INPUT%d %s\n", j + 1, node->incoming_links[j]->effect_id.c_str());
+                               sprintf(buf, "#define INPUT%d %s\n", j + 1, phase->effect_ids[node->incoming_links[j]].c_str());
                                frag_shader += buf;
                        }
                }
        
                frag_shader += "\n";
-               frag_shader += std::string("#define FUNCNAME ") + node->effect_id + "\n";
-               frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), node->effect_id);
-               frag_shader += replace_prefix(node->effect->output_fragment_shader(), node->effect_id);
+               frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
+               frag_shader += replace_prefix(node->effect->output_convenience_uniforms(), effect_id);
+               frag_shader += replace_prefix(node->effect->output_fragment_shader(), effect_id);
                frag_shader += "#undef PREFIX\n";
                frag_shader += "#undef FUNCNAME\n";
                if (node->incoming_links.size() == 1) {
@@ -285,10 +285,9 @@ Phase *EffectChain::compile_glsl_program(
                        CHECK(node->effect->set_int("needs_mipmaps", input_needs_mipmaps));
                }
        }
-       frag_shader += std::string("#define INPUT ") + sorted_effects.back()->effect_id + "\n";
+       frag_shader += std::string("#define INPUT ") + phase->effect_ids[sorted_effects.back()] + "\n";
        frag_shader.append(read_file("footer.frag"));
 
-       Phase *phase = new Phase;
        phase->glsl_program_num = resource_pool->compile_glsl_program(read_file("vs.vert"), frag_shader);
        phase->input_needs_mipmaps = input_needs_mipmaps;
        phase->inputs = true_inputs;
@@ -1411,34 +1410,6 @@ void EffectChain::finalize()
 
        output_dot("step19-split-to-phases.dot");
 
-       // If we have more than one phase, we need intermediate render-to-texture.
-       // Construct an FBO, and then as many textures as we need.
-       // We choose the simplest option of having one texture per output,
-       // since otherwise this turns into an (albeit simple)
-       // register allocation problem.
-       if (phases.size() > 1) {
-               for (unsigned i = 0; i < phases.size() - 1; ++i) {
-                       inform_input_sizes(phases[i]);
-                       find_output_size(phases[i]);
-
-                       Node *output_node = phases[i]->effects.back();
-                       glGenTextures(1, &output_node->output_texture);
-                       check_error();
-                       glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
-                       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, phases[i]->output_width, phases[i]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-                       check_error();
-
-                       output_node->output_texture_width = phases[i]->output_width;
-                       output_node->output_texture_height = phases[i]->output_height;
-               }
-               inform_input_sizes(phases.back());
-       }
-               
        for (unsigned i = 0; i < inputs.size(); ++i) {
                inputs[i]->finalize();
        }
@@ -1490,29 +1461,18 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
 
        std::set<Node *> generated_mipmaps;
 
+       // We choose the simplest option of having one texture per output,
+       // since otherwise this turns into an (albeit simple) register allocation problem.
+       std::map<Phase *, GLuint> output_textures;
+
        for (unsigned phase = 0; phase < phases.size(); ++phase) {
-               // See if the requested output size has changed. If so, we need to recreate
-               // the texture (and before we start setting up inputs).
+               // Find a texture for this phase.
                inform_input_sizes(phases[phase]);
                if (phase != phases.size() - 1) {
                        find_output_size(phases[phase]);
 
-                       Node *output_node = phases[phase]->effects.back();
-
-                       if (output_node->output_texture_width != phases[phase]->output_width ||
-                           output_node->output_texture_height != phases[phase]->output_height) {
-                               glActiveTexture(GL_TEXTURE0);
-                               check_error();
-                               glBindTexture(GL_TEXTURE_2D, output_node->output_texture);
-                               check_error();
-                               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-                               check_error();
-                               glBindTexture(GL_TEXTURE_2D, 0);
-                               check_error();
-
-                               output_node->output_texture_width = phases[phase]->output_width;
-                               output_node->output_texture_height = phases[phase]->output_height;
-                       }
+                       GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height);
+                       output_textures.insert(std::make_pair(phases[phase], tex_num));
                }
 
                glUseProgram(phases[phase]->glsl_program_num);
@@ -1522,7 +1482,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
                        glActiveTexture(GL_TEXTURE0 + sampler);
                        Node *input = phases[phase]->inputs[sampler];
-                       glBindTexture(GL_TEXTURE_2D, input->output_texture);
+                       glBindTexture(GL_TEXTURE_2D, output_textures[input->phase]);
                        check_error();
                        if (phases[phase]->input_needs_mipmaps) {
                                if (generated_mipmaps.count(input) == 0) {
@@ -1536,8 +1496,12 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                                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();
 
-                       std::string texture_name = std::string("tex_") + input->effect_id;
+                       std::string texture_name = std::string("tex_") + phases[phase]->effect_ids[input];
                        glUniform1i(glGetUniformLocation(phases[phase]->glsl_program_num, texture_name.c_str()), sampler);
                        check_error();
                }
@@ -1555,12 +1519,11 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                                CHECK(dither_effect->set_int("output_height", height));
                        }
                } else {
-                       Node *output_node = phases[phase]->effects.back();
                        glFramebufferTexture2D(
                                GL_FRAMEBUFFER,
                                GL_COLOR_ATTACHMENT0,
                                GL_TEXTURE_2D,
-                               output_node->output_texture,
+                               output_textures[phases[phase]],
                                0);
                        check_error();
                        GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
@@ -1572,7 +1535,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                unsigned sampler_num = phases[phase]->inputs.size();
                for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
                        Node *node = phases[phase]->effects[i];
-                       node->effect->set_gl_state(phases[phase]->glsl_program_num, node->effect_id, &sampler_num);
+                       node->effect->set_gl_state(phases[phase]->glsl_program_num, phases[phase]->effect_ids[node], &sampler_num);
                        check_error();
                }
 
@@ -1600,6 +1563,12 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                }
        }
 
+       for (std::map<Phase *, GLuint>::const_iterator texture_it = output_textures.begin();
+            texture_it != output_textures.end();
+            ++texture_it) {
+               resource_pool->release_2d_texture(texture_it->second);
+       }
+
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        check_error();