mutter/cogl/cogl-pipeline-hash-table.h
Neil Roberts 5085919acc pipeline-cache: Prune old unused pipelines when the cache gets too big
Previously when a pipeline is added to the cache it would never be
removed. If the application is generating a lot of unique pipelines
this can end up effectively leaking a large number of resources
including the GL program objects. Arguably this isn't really a problem
because if the application is generating that many unique pipelines
then it is doing something wrong anyway. It also implies that it will
be recompiling shaders very often so the cache leaking will likely be
the least of the problems.

This patch makes it keep track of which pipelines in the cache are in
use. The cache now returns a struct representing the entry instead of
directly returning the pipeline. This entry contains a usage counter
which the pipeline backends can use to mark when there is a pipeline
alive that is using the cache entry. When the hash table decides that
it's a good time to prune some entries, it will make a list of all of
the pipelines that are not in use and then remove the least recently
used half of the pipelines. That way it is less likely to remove
pipelines that the application is actually regenerating often even if
they aren't in use all of the time.

When the cache is pruned the hash table makes a note of how small the
cache could be if it removed all of the unused pipelines. The hash
table starts pruning when there are more entries than twice this
minimum expected size. The idea is that if that case it hit then the
hash table is more than half full of useless pipelines so the
application is generating lots of redundant pipelines and it is a good
time to remove them.

Reviewed-by: Robert Bragg <robert@linux.intel.com>

(cherry picked from commit c21aac22992bb7fef5a8d0913130b8245e67f2eb)

Conflicts:
	cogl/driver/gl/cogl-pipeline-fragend-glsl.c
	cogl/driver/gl/cogl-pipeline-progend-glsl.c
	cogl/driver/gl/cogl-pipeline-vertend-glsl.c
	cogl/driver/gl/gl/cogl-pipeline-fragend-arbfp.c
2014-01-14 12:05:17 +00:00

75 lines
2.6 KiB
C

/*
* Cogl
*
* An object oriented GL/GLES Abstraction/Utility Layer
*
* Copyright (C) 2013 Intel Corporation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#ifndef __COGL_PIPELINE_HASH_H__
#define __COGL_PIPELINE_HASH_H__
#include "cogl-pipeline-cache.h"
typedef struct
{
/* Total number of pipelines that were ever added to the hash. This
* is not decremented when a pipeline is removed. It is only used to
* generate a warning if an unusually high number of pipelines are
* generated */
int n_unique_pipelines;
/* This is the expected minimum size we could prune the hash table
* to if we were to remove all pipelines that are not in use. This
* is only updated after we prune the table */
int expected_min_size;
/* String that will be used to describe the usage of this hash table
* in the debug warning when too many pipelines are generated. This
* must be a static string because it won't be copied or freed */
const char *debug_string;
unsigned int main_state;
unsigned int layer_state;
GHashTable *table;
} CoglPipelineHashTable;
void
_cogl_pipeline_hash_table_init (CoglPipelineHashTable *hash,
unsigned int main_state,
unsigned int layer_state,
const char *debug_string);
void
_cogl_pipeline_hash_table_destroy (CoglPipelineHashTable *hash);
/*
* Gets a pipeline from the hash that has the same state as
* @key_pipeline according to the limited state bits passed to
* _cogl_pipeline_hash_table_init(). If there is no matching pipelines
* already then a copy of key_pipeline is stored in the hash so that
* it will be used next time the function is called with a similar
* pipeline. In that case the copy itself will be returned
*/
CoglPipelineCacheEntry *
_cogl_pipeline_hash_table_get (CoglPipelineHashTable *hash,
CoglPipeline *key_pipeline);
#endif /* __COGL_PIPELINE_HASH_H__ */