]> git.sesse.net Git - movit/commitdiff
Merge branch 'master' into epoxy
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 22 Mar 2014 13:57:53 +0000 (14:57 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 22 Mar 2014 13:57:53 +0000 (14:57 +0100)
Conflicts:
flat_input.cpp

1  2 
effect_chain.cpp
effect_chain.h
effect_chain_test.cpp
resource_pool.cpp
resource_pool.h

diff --combined effect_chain.cpp
index 82f1b2939cc088e8268c0d883f6a1c69e0979dc1,ca6a7775eb56de0aab5b2e43dbe53a8003c34aaf..47938455b02bee9f9ff1b1be7b3cf5cbe1fd0a95
@@@ -1,6 -1,6 +1,6 @@@
  #define GL_GLEXT_PROTOTYPES 1
  
 -#include <GL/glew.h>
 +#include <epoxy/gl.h>
  #include <assert.h>
  #include <locale.h>
  #include <math.h>
@@@ -68,11 -68,6 +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)
@@@ -243,7 -238,7 +238,7 @@@ string replace_prefix(const string &tex
  
  void EffectChain::compile_glsl_program(Phase *phase)
  {
 -      string frag_shader = read_file("header.frag");
 +      string frag_shader = read_version_dependent_file("header", "frag");
  
        // Create functions for all the texture inputs that we need.
        for (unsigned i = 0; i < phase->inputs.size(); ++i) {
        
                frag_shader += string("uniform sampler2D tex_") + effect_id + ";\n";
                frag_shader += string("vec4 ") + effect_id + "(vec2 tc) {\n";
 -              frag_shader += "\treturn texture2D(tex_" + string(effect_id) + ", tc);\n";
 +              frag_shader += "\treturn tex2D(tex_" + string(effect_id) + ", tc);\n";
                frag_shader += "}\n";
                frag_shader += "\n";
        }
                frag_shader += "\n";
        }
        frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
 -      frag_shader.append(read_file("footer.frag"));
 +      frag_shader.append(read_version_dependent_file("footer", "frag"));
  
 -      phase->glsl_program_num = resource_pool->compile_glsl_program(read_file("vs.vert"), frag_shader);
 +      string vert_shader = read_version_dependent_file("vs", "vert");
 +      phase->glsl_program_num = resource_pool->compile_glsl_program(vert_shader, frag_shader);
  
        // Prepare the geometry for the fullscreen quad used in this phase.
        // (We have separate VAOs per shader, since the bindings can in theory
@@@ -1460,47 -1454,37 +1455,37 @@@ void EffectChain::render_to_fbo(GLuint 
        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,
        // since otherwise this turns into an (albeit simple) register allocation problem.
        map<Phase *, GLuint> output_textures;
  
-       for (unsigned phase = 0; phase < phases.size(); ++phase) {
+       for (unsigned phase_num = 0; phase_num < phases.size(); ++phase_num) {
+               Phase *phase = phases[phase_num];
                // Find a texture for this phase.
-               inform_input_sizes(phases[phase]);
-               if (phase != phases.size() - 1) {
-                       find_output_size(phases[phase]);
+               inform_input_sizes(phase);
+               if (phase_num != phases.size() - 1) {
+                       find_output_size(phase);
  
-                       GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F_ARB, phases[phase]->output_width, phases[phase]->output_height);
-                       output_textures.insert(make_pair(phases[phase], tex_num));
+                       GLuint tex_num = resource_pool->create_2d_texture(GL_RGBA16F, phase->output_width, phase->output_height);
+                       output_textures.insert(make_pair(phase, tex_num));
                }
  
-               const GLuint glsl_program_num = phases[phase]->glsl_program_num;
+               const GLuint glsl_program_num = phase->glsl_program_num;
                check_error();
                glUseProgram(glsl_program_num);
                check_error();
  
                // Set up RTT inputs for this phase.
-               for (unsigned sampler = 0; sampler < phases[phase]->inputs.size(); ++sampler) {
+               for (unsigned sampler = 0; sampler < phase->inputs.size(); ++sampler) {
                        glActiveTexture(GL_TEXTURE0 + sampler);
-                       Phase *input = phases[phase]->inputs[sampler];
+                       Phase *input = phase->inputs[sampler];
                        input->output_node->bound_sampler_num = sampler;
                        glBindTexture(GL_TEXTURE_2D, output_textures[input]);
                        check_error();
-                       if (phases[phase]->input_needs_mipmaps) {
+                       if (phase->input_needs_mipmaps) {
                                if (generated_mipmaps.count(input) == 0) {
                                        glGenerateMipmap(GL_TEXTURE_2D);
                                        check_error();
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
                        check_error();
  
-                       string texture_name = string("tex_") + phases[phase]->effect_ids[input->output_node];
+                       string texture_name = string("tex_") + phase->effect_ids[input->output_node];
                        glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler);
                        check_error();
                }
  
                // And now the output.
-               if (phase == phases.size() - 1) {
+               if (phase_num == phases.size() - 1) {
                        // Last phase goes to the output the user specified.
                        glBindFramebuffer(GL_FRAMEBUFFER, dest_fbo);
                        check_error();
                                CHECK(dither_effect->set_int("output_height", height));
                        }
                } else {
-                       glFramebufferTexture2D(
-                               GL_FRAMEBUFFER,
-                               GL_COLOR_ATTACHMENT0,
-                               GL_TEXTURE_2D,
-                               output_textures[phases[phase]],
-                               0);
-                       check_error();
-                       GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
-                       assert(status == GL_FRAMEBUFFER_COMPLETE);
-                       glViewport(0, 0, phases[phase]->output_width, phases[phase]->output_height);
+                       fbo = resource_pool->create_fbo(context, output_textures[phase]);
+                       glBindFramebuffer(GL_FRAMEBUFFER, fbo);
+                       glViewport(0, 0, phase->output_width, phase->output_height);
                }
  
                // Give the required parameters to all the effects.
-               unsigned sampler_num = phases[phase]->inputs.size();
-               for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
-                       Node *node = phases[phase]->effects[i];
+               unsigned sampler_num = phase->inputs.size();
+               for (unsigned i = 0; i < phase->effects.size(); ++i) {
+                       Node *node = phase->effects[i];
                        unsigned old_sampler_num = sampler_num;
-                       node->effect->set_gl_state(glsl_program_num, phases[phase]->effect_ids[node], &sampler_num);
+                       node->effect->set_gl_state(glsl_program_num, phase->effect_ids[node], &sampler_num);
                        check_error();
  
                        if (node->effect->is_single_texture()) {
                        }
                }
  
-               glBindVertexArray(phases[phase]->vao);
+               glBindVertexArray(phase->vao);
                check_error();
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
                check_error();
  
-               for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) {
-                       Node *node = phases[phase]->effects[i];
+               for (unsigned i = 0; i < phase->effects.size(); ++i) {
+                       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();
diff --combined effect_chain.h
index 1ecee63d660cbac4d2565e846c908cc26efad0b0,6ce2332848e658a16e66068634987fd1b696adca..a1bf3cb80241167b6f0bda72bf1ea3e974ed98d3
@@@ -17,7 -17,7 +17,7 @@@
  // the EffectChain holds textures and other OpenGL objects that are tied to the
  // context.
  
 -#include <GL/glew.h>
 +#include <epoxy/gl.h>
  #include <stdio.h>
  #include <map>
  #include <set>
@@@ -287,7 -287,6 +287,6 @@@ private
        std::map<Effect *, Node *> node_map;
        Effect *dither_effect;
  
-       std::map<void *, GLuint> fbos;  // One for each OpenGL context.
        std::vector<Input *> inputs;  // Also contained in nodes.
        std::vector<Phase *> phases;
  
diff --combined effect_chain_test.cpp
index 2552b6f88410ef6c62fffebeef8475c095b1fbd6,673e9cc3665fd1acac09d9815eaa84947d416e22..50d004db1c52c1192946b87add1b785149f585fc
@@@ -2,7 -2,7 +2,7 @@@
  //
  // Note that this also contains the tests for some of the simpler effects.
  
 -#include <GL/glew.h>
 +#include <epoxy/gl.h>
  #include <assert.h>
  
  #include "effect.h"
@@@ -152,23 -152,19 +152,23 @@@ TEST(EffectChainTest, RewritingWorksAnd
  
  TEST(EffectChainTest, RewritingWorksAndTexturesAreAskedForsRGB) {
        unsigned char data[] = {
 -              0, 64,
 -              128, 255,
 +                0,   0,   0, 255,
 +               64,  64,  64, 255,
 +              128, 128, 128, 255,
 +              255, 255, 255, 255,
        };
 -      float expected_data[4] = {
 -              1.0f, 0.9771f,
 -              0.8983f, 0.0f,
 +      float expected_data[] = {
 +              1.0000f, 1.0000f, 1.0000f, 1.0000f,
 +              0.9771f, 0.9771f, 0.9771f, 1.0000f,
 +              0.8983f, 0.8983f, 0.8983f, 1.0000f,
 +              0.0000f, 0.0000f, 0.0000f, 1.0000f
        };
 -      float out_data[4];
 -      EffectChainTester tester(NULL, 2, 2);
 -      tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
 +      float out_data[4 * 4];
 +      EffectChainTester tester(NULL, 1, 4);
 +      tester.add_input(data, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
        RewritingEffect<InvertEffect> *effect = new RewritingEffect<InvertEffect>();
        tester.get_chain()->add_effect(effect);
 -      tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
 +      tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
  
        Node *node = effect->replaced_node;
        ASSERT_EQ(1, node->incoming_links.size());
        EXPECT_EQ("FlatInput", node->incoming_links[0]->effect->effect_type_id());
        EXPECT_EQ("GammaCompressionEffect", node->outgoing_links[0]->effect->effect_type_id());
  
 -      expect_equal(expected_data, out_data, 2, 2);
 +      expect_equal(expected_data, out_data, 4, 4);
  }
  
  TEST(EffectChainTest, RewritingWorksAndColorspaceConversionsAreInserted) {
@@@ -227,7 -223,7 +227,7 @@@ private
        GammaCurve overridden_gamma_curve;
  };
  
- TEST(EffectChainTester, HandlesInputChangingColorspace) {
+ TEST(EffectChainTest, HandlesInputChangingColorspace) {
        const int size = 4;
  
        float data[size] = {
diff --combined resource_pool.cpp
index 9beae1b507bd623fe7c9e6e93038f9b713cc5619,a83add2a7117ccfb99bea2cce3afcdb1ed062cdb..10eeb9bb491f54ed5d8b9ee32c30719c18bf18c1
@@@ -6,8 -6,8 +6,8 @@@
  #include <map>
  #include <string>
  #include <utility>
 +#include <epoxy/gl.h>
  
 -#include "glew.h"
  #include "init.h"
  #include "resource_pool.h"
  #include "util.h"
@@@ -17,10 -17,13 +17,13 @@@ using namespace std
  namespace movit {
  
  ResourcePool::ResourcePool(size_t program_freelist_max_length,
-                            size_t texture_freelist_max_bytes)
+                            size_t texture_freelist_max_bytes,
+                            size_t fbo_freelist_max_length)
        : program_freelist_max_length(program_freelist_max_length),
          texture_freelist_max_bytes(texture_freelist_max_bytes),
-         texture_freelist_bytes(0) {
+         fbo_freelist_max_length(fbo_freelist_max_length),
+         texture_freelist_bytes(0)
+ {
        pthread_mutex_init(&lock, NULL);
  }
  
@@@ -48,6 -51,17 +51,17 @@@ ResourcePool::~ResourcePool(
        }
        assert(texture_formats.empty());
        assert(texture_freelist_bytes == 0);
+       for (list<GLuint>::const_iterator freelist_it = fbo_freelist.begin();
+            freelist_it != fbo_freelist.end();
+            ++freelist_it) {
+               GLuint free_fbo_num = *freelist_it;
+               assert(fbo_formats.count(free_fbo_num) != 0);
+               fbo_formats.erase(free_fbo_num);
+               glDeleteFramebuffers(1, &free_fbo_num);
+               check_error();
+       }
+       assert(fbo_formats.empty());
  }
  
  void ResourcePool::delete_program(GLuint glsl_program_num)
@@@ -186,18 -200,10 +200,18 @@@ GLuint ResourcePool::create_2d_texture(
        case GL_SRGB8_ALPHA8:
                format = GL_RGBA;
                break;
 +      case GL_RGB32F:
 +      case GL_RGB16F:
 +      case GL_RGB8:
 +              format = GL_RGB;
 +              break;
        case GL_RG32F:
        case GL_RG16F:
 +      case GL_RG8:
                format = GL_RG;
                break;
 +      case GL_R32F:
 +      case GL_R16F:
        case GL_R8:
                format = GL_RED;
                break;
                assert(false);
        }
  
 +      // Same with type; GLES is stricter than desktop OpenGL here.
 +      GLenum type;
 +      switch (internal_format) {
 +      case GL_RGBA32F_ARB:
 +      case GL_RGBA16F_ARB:
 +      case GL_RGB32F:
 +      case GL_RGB16F:
 +      case GL_RG32F:
 +      case GL_RG16F:
 +      case GL_R32F:
 +      case GL_R16F:
 +              type = GL_FLOAT;
 +              break;
 +      case GL_SRGB8_ALPHA8:
 +      case GL_RGBA8:
 +      case GL_RGB8:
 +      case GL_RG8:
 +      case GL_R8:
 +              type = GL_UNSIGNED_BYTE;
 +              break;
 +      default:
 +              // TODO: Add more here as needed.
 +              assert(false);
 +      }
 +
 +
        GLuint texture_num;
        glGenTextures(1, &texture_num);
        check_error();
        glBindTexture(GL_TEXTURE_2D, texture_num);
        check_error();
 -      glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
 +      glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
        check_error();
        glBindTexture(GL_TEXTURE_2D, 0);
        check_error();
@@@ -268,6 -248,83 +282,83 @@@ void ResourcePool::release_2d_texture(G
                texture_formats.erase(free_texture_num);
                glDeleteTextures(1, &free_texture_num);
                check_error();
+               // Delete any FBO related to this texture.
+               for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
+                    fbo_freelist_it != fbo_freelist.end(); ) {
+                       GLuint fbo_num = *fbo_freelist_it;
+                       map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
+                       assert(format_it != fbo_formats.end());
+                       if (format_it->second.texture_num == free_texture_num) {
+                               glDeleteFramebuffers(1, &fbo_num);
+                               fbo_freelist.erase(fbo_freelist_it++);
+                       } else {
+                               ++fbo_freelist_it;
+                       }
+               }
+       }
+       pthread_mutex_unlock(&lock);
+ }
+ GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
+ {
+       pthread_mutex_lock(&lock);
+       // See if there's an FBO on the freelist we can use.
+       for (list<GLuint>::iterator freelist_it = fbo_freelist.begin();
+            freelist_it != fbo_freelist.end();
+            ++freelist_it) {
+               GLuint fbo_num = *freelist_it;
+               map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
+               assert(format_it != fbo_formats.end());
+               if (format_it->second.context == context &&
+                   format_it->second.texture_num == texture_num) {
+                       fbo_freelist.erase(freelist_it);
+                       pthread_mutex_unlock(&lock);
+                       return fbo_num;
+               }
+       }
+       // Create a new one.
+       GLuint fbo_num;
+       glGenFramebuffers(1, &fbo_num);
+       check_error();
+       glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
+       check_error();
+       glFramebufferTexture2D(
+               GL_FRAMEBUFFER,
+               GL_COLOR_ATTACHMENT0,
+               GL_TEXTURE_2D,
+               texture_num,
+               0);
+       check_error();
+       GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
+       assert(status == GL_FRAMEBUFFER_COMPLETE);
+       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+       check_error();
+       FBO fbo_format;
+       fbo_format.context = context;
+       fbo_format.texture_num = texture_num;
+       assert(fbo_formats.count(fbo_num) == 0);
+       fbo_formats.insert(make_pair(fbo_num, fbo_format));
+       pthread_mutex_unlock(&lock);
+       return fbo_num;
+ }
+ void ResourcePool::release_fbo(GLuint fbo_num)
+ {
+       pthread_mutex_lock(&lock);
+       fbo_freelist.push_front(fbo_num);
+       assert(fbo_formats.count(fbo_num) != 0);
+       while (fbo_freelist.size() > fbo_freelist_max_length) {
+               GLuint free_fbo_num = fbo_freelist.front();
+               fbo_freelist.pop_front();
+               assert(fbo_formats.count(free_fbo_num) != 0);
+               fbo_formats.erase(free_fbo_num);
+               glDeleteFramebuffers(1, &free_fbo_num);
+               check_error();
        }
        pthread_mutex_unlock(&lock);
  }
@@@ -283,32 -340,16 +374,32 @@@ size_t ResourcePool::estimate_texture_s
        case GL_RGBA16F_ARB:
                bytes_per_pixel = 8;
                break;
 +      case GL_RGB32F_ARB:
 +              bytes_per_pixel = 12;
 +              break;
 +      case GL_RGB16F_ARB:
 +              bytes_per_pixel = 6;
 +              break;
        case GL_RGBA8:
        case GL_SRGB8_ALPHA8:
                bytes_per_pixel = 4;
                break;
 +      case GL_RGB8:
 +      case GL_SRGB8:
 +              bytes_per_pixel = 3;
 +              break;
        case GL_RG32F:
                bytes_per_pixel = 8;
                break;
        case GL_RG16F:
                bytes_per_pixel = 4;
                break;
 +      case GL_R32F:
 +              bytes_per_pixel = 4;
 +              break;
 +      case GL_R16F:
 +              bytes_per_pixel = 2;
 +              break;
        case GL_R8:
                bytes_per_pixel = 1;
                break;
diff --combined resource_pool.h
index d8d8ceca3ca1deb2b50dfd2b45fa256af908beeb,4ae4110851387cd7dcfe21eb67f3f3f0e71cd695..7029fbfd5e795cbd405a099ddd0b1450ccf187c2
@@@ -16,7 -16,7 +16,7 @@@
  // safely called from multiple threads at the same time, provided they have
  // separate (but sharing) OpenGL contexts.
  
 -#include <GL/glew.h>
 +#include <epoxy/gl.h>
  #include <pthread.h>
  #include <stddef.h>
  #include <list>
@@@ -40,7 -40,8 +40,8 @@@ public
        // This means you should be prepared for actual memory usage of the freelist being
        // twice this estimate or more.
        ResourcePool(size_t program_freelist_max_length = 100,
-                    size_t texture_freelist_max_bytes = 100 << 20);  // 100 MB.
+                    size_t texture_freelist_max_bytes = 100 << 20,  // 100 MB.
+                    size_t fbo_freelist_max_length = 100);
        ~ResourcePool();
  
        // All remaining functions are intended for calls from EffectChain only.
        // or fetch a previous used if possible. Unbinds GL_TEXTURE_2D afterwards.
        // Keeps ownership of the texture; you must call release_2d_texture() instead
        // of deleting it when you no longer want it.
-       //
-       // Note: Currently we do not actually have a freelist, but this will change soon.
        GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height);
        void release_2d_texture(GLuint texture_num);
  
+       // Allocate an FBO with the the given texture bound as a framebuffer attachment,
+       // or fetch a previous used if possible. Unbinds GL_FRAMEBUFFER afterwards.
+       // Keeps ownership of the FBO; you must call release_fbo() of deleting
+       // it when you no longer want it. You can get an appropriate context
+       // pointer from get_gl_context_identifier().
+       //
+       // NOTE: In principle, the FBO doesn't have a resolution or pixel format;
+       // you can bind almost whatever texture you want to it. However, changing
+       // textures can have an adverse effect on performance due to validation,
+       // in particular on NVidia cards. Also, keep in mind that FBOs are not
+       // shareable across contexts.
+       GLuint create_fbo(void *context, GLuint texture_num);
+       void release_fbo(GLuint fbo_num);
  private:
        // Delete the given program and both its shaders.
        void delete_program(GLuint program_num);
@@@ -68,7 -81,7 +81,7 @@@
        // Protects all the other elements in the class.
        pthread_mutex_t lock;
  
-       size_t program_freelist_max_length, texture_freelist_max_bytes;
+       size_t program_freelist_max_length, texture_freelist_max_bytes, fbo_freelist_max_length;
                
        // A mapping from vertex/fragment shader source strings to compiled program number.
        std::map<std::pair<std::string, std::string>, GLuint> programs;
        std::list<GLuint> texture_freelist;
        size_t texture_freelist_bytes;
  
+       struct FBO {
+               void *context;
+               GLuint texture_num;
+       };
+       // A mapping from FBO number to format details. This is filled if the
+       // FBO is given out to a client or on the freelist, but not if it is
+       // deleted from the freelist.
+       std::map<GLuint, FBO> fbo_formats;
+       // A list of all FBOs that are release but not freed (most recently freed
+       // first). Once this reaches <fbo_freelist_max_length>, the last element
+       // will be deleted.
+       std::list<GLuint> fbo_freelist;
        // See the caveats at the constructor.
        static size_t estimate_texture_size(const Texture2D &texture_format);
  };