From: Steinar H. Gunderson Date: Fri, 21 Mar 2014 23:29:21 +0000 (+0100) Subject: Have separate FBOs per resolution and format. X-Git-Tag: 1.1~27 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;ds=sidebyside;h=09c983894685554b41f622dadd40ac1a4efc527d;p=movit Have separate FBOs per resolution and format. Seemingly this _also_ costs on NVidia; the demo app is down 0.9 ms/frame or so. This rapidly started approaching complexity worthy of the ResourcePool, so I moved the functionality in there even though it's not context-shareable. --- diff --git a/effect_chain.cpp b/effect_chain.cpp index b6f2a5b..c3e1fba 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -68,11 +68,6 @@ EffectChain::~EffectChain() if (owns_resource_pool) { delete resource_pool; } - for (map::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 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)); } @@ -1536,6 +1519,9 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height CHECK(dither_effect->set_int("output_height", height)); } } else { + fbo = resource_pool->create_fbo(context, GL_RGBA16F, phase->output_width, phase->output_height); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + check_error(); glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, @@ -1573,6 +1559,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::const_iterator texture_it = output_textures.begin(); diff --git a/effect_chain.h b/effect_chain.h index 9051195..6ce2332 100644 --- a/effect_chain.h +++ b/effect_chain.h @@ -287,7 +287,6 @@ private: std::map node_map; Effect *dither_effect; - std::map fbos; // One for each OpenGL context. std::vector inputs; // Also contained in nodes. std::vector phases; diff --git a/resource_pool.cpp b/resource_pool.cpp index 72b3095..b6f6a31 100644 --- a/resource_pool.cpp +++ b/resource_pool.cpp @@ -17,10 +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 @@ ResourcePool::~ResourcePool() } assert(texture_formats.empty()); assert(texture_freelist_bytes == 0); + + for (list::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) @@ -238,6 +252,60 @@ void ResourcePool::release_2d_texture(GLuint texture_num) pthread_mutex_unlock(&lock); } +GLuint ResourcePool::create_fbo(void *context, GLint internal_format, GLsizei width, GLsizei height) +{ + pthread_mutex_lock(&lock); + // See if there's an FBO on the freelist we can use. + for (list::iterator freelist_it = fbo_freelist.begin(); + freelist_it != fbo_freelist.end(); + ++freelist_it) { + GLuint fbo_num = *freelist_it; + map::const_iterator format_it = fbo_formats.find(fbo_num); + assert(format_it != fbo_formats.end()); + if (format_it->second.context == context && + format_it->second.internal_format == internal_format && + format_it->second.width == width && + format_it->second.height == height) { + 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(); + + FBO fbo_format; + fbo_format.context = context; + fbo_format.internal_format = internal_format; + fbo_format.width = width; + fbo_format.height = height; + 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); +} + size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format) { size_t bytes_per_pixel; diff --git a/resource_pool.h b/resource_pool.h index 14c75e2..86d0059 100644 --- a/resource_pool.h +++ b/resource_pool.h @@ -40,7 +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. @@ -59,6 +60,19 @@ public: GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height); void release_2d_texture(GLuint texture_num); + // Allocate an FBO used for the given internal format and dimensions, + // or fetch a previous used if possible. 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 + // resolution or pixel formats can have an adverse effect on performance, + // in particular on NVidia cards. Also, keep in mind that FBOs are not + // shareable across contexts. + GLuint create_fbo(void *context, GLint internal_format, GLsizei width, GLsizei height); + void release_fbo(GLuint fbo_num); + private: // Delete the given program and both its shaders. void delete_program(GLuint program_num); @@ -66,7 +80,7 @@ private: // 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, GLuint> programs; @@ -102,6 +116,22 @@ private: std::list texture_freelist; size_t texture_freelist_bytes; + struct FBO { + void *context; + GLint internal_format; + GLsizei width, height; + }; + + // 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 fbo_formats; + + // A list of all FBOs that are release but not freed (most recently freed + // first). Once this reaches , the last element + // will be deleted. + std::list fbo_freelist; + // See the caveats at the constructor. static size_t estimate_texture_size(const Texture2D &texture_format); };