]> git.sesse.net Git - movit/blobdiff - effect_chain.cpp
Factor out RTT sampler setting in its own function.
[movit] / effect_chain.cpp
index b6f2a5b50b9441649bd9658e77aacfa9efb7e0e3..e75441276009b69a535017ec78784463770275a7 100644 (file)
@@ -68,11 +68,6 @@ EffectChain::~EffectChain()
        if (owns_resource_pool) {
                delete resource_pool;
        }
-       for (map<void *, GLuint>::const_iterator fbo_it = fbos.begin();
-            fbo_it != fbos.end(); ++fbo_it) {
-               glDeleteFramebuffers(1, &fbo_it->second);
-               check_error();
-       }
 }
 
 Input *EffectChain::add_input(Input *input)
@@ -1459,18 +1454,6 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
        glDepthMask(GL_FALSE);
        check_error();
 
-       if (phases.size() > 1) {
-               if (fbos.count(context) == 0) {
-                       glGenFramebuffers(1, &fbo);
-                       check_error();
-                       fbos.insert(make_pair(context, fbo));
-               } else {
-                       fbo = fbos[context];
-               }
-               glBindFramebuffer(GL_FRAMEBUFFER, fbo);
-               check_error();
-       }
-
        set<Phase *> generated_mipmaps;
 
        // We choose the simplest option of having one texture per output,
@@ -1485,7 +1468,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                if (phase_num != phases.size() - 1) {
                        find_output_size(phase);
 
-                       GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F_ARB, phase->output_width, phase->output_height);
+                       GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F, phase->output_width, phase->output_height);
                        output_textures.insert(make_pair(phase, tex_num));
                }
 
@@ -1501,22 +1484,12 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                        input->output_node->bound_sampler_num = sampler;
                        glBindTexture(GL_TEXTURE_2D, output_textures[input]);
                        check_error();
-                       if (phase->input_needs_mipmaps) {
-                               if (generated_mipmaps.count(input) == 0) {
-                                       glGenerateMipmap(GL_TEXTURE_2D);
-                                       check_error();
-                                       generated_mipmaps.insert(input);
-                               }
-                               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
-                               check_error();
-                       } else {
-                               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+                       if (phase->input_needs_mipmaps && generated_mipmaps->count(input) == 0) {
+                               glGenerateMipmap(GL_TEXTURE_2D);
                                check_error();
+                               generated_mipmaps->insert(input);
                        }
-                       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();
+                       setup_rtt_sampler(glsl_program_num, sampler, phase->effect_ids[input->output_node], phase->input_needs_mipmaps);
 
                        string texture_name = string("tex_") + phase->effect_ids[input->output_node];
                        glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler);
@@ -1536,15 +1509,8 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                                CHECK(dither_effect->set_int("output_height", height));
                        }
                } else {
-                       glFramebufferTexture2D(
-                               GL_FRAMEBUFFER,
-                               GL_COLOR_ATTACHMENT0,
-                               GL_TEXTURE_2D,
-                               output_textures[phase],
-                               0);
-                       check_error();
-                       GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
-                       assert(status == GL_FRAMEBUFFER_COMPLETE);
+                       fbo = resource_pool->create_fbo(context, output_textures[phase]);
+                       glBindFramebuffer(GL_FRAMEBUFFER, fbo);
                        glViewport(0, 0, phase->output_width, phase->output_height);
                }
 
@@ -1573,6 +1539,9 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
                        Node *node = phase->effects[i];
                        node->effect->clear_gl_state();
                }
+               if (phase_num != phases.size() - 1) {
+                       resource_pool->release_fbo(fbo);
+               }
        }
 
        for (map<Phase *, GLuint>::const_iterator texture_it = output_textures.begin();
@@ -1589,4 +1558,25 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height
        check_error();
 }
 
+void EffectChain::setup_rtt_sampler(GLuint glsl_program_num, int sampler_num, const string &effect_id, bool use_mipmaps)
+{
+       glActiveTexture(GL_TEXTURE0 + sampler_num);
+       check_error();
+       if (use_mipmaps) {
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
+               check_error();
+       } else {
+               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();
+
+       string texture_name = string("tex_") + effect_id;
+       glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler_num);
+       check_error();
+}
+
 }  // namespace movit