]> git.sesse.net Git - movit/blob - resource_pool.h
Remove the finalize() member function from Input.
[movit] / resource_pool.h
1 #ifndef _MOVIT_RESOURCE_POOL_H
2 #define _MOVIT_RESOURCE_POOL_H 1
3
4 // A ResourcePool governs resources that are shared between multiple EffectChains;
5 // in particular, resources that might be expensive to acquire or hold. Thus,
6 // if you have many EffectChains, hooking them up to the same ResourcePool is
7 // probably a good idea.
8 //
9 // However, hooking an EffectChain to a ResourcePool extends the OpenGL context
10 // demands (see effect_chain.h) to that of the ResourcePool; all chains must then
11 // only be used in OpenGL contexts sharing resources with each other. This is
12 // the reason why there isn't just one global ResourcePool singleton (although
13 // most practical users will just want one).
14 //
15 // Thread-safety: All functions except the constructor and destructor can be
16 // safely called from multiple threads at the same time, provided they have
17 // separate (but sharing) OpenGL contexts.
18
19 #include <GL/glew.h>
20 #include <pthread.h>
21 #include <stddef.h>
22 #include <list>
23 #include <map>
24 #include <string>
25 #include <utility>
26
27 class ResourcePool {
28 public:
29         // program_freelist_max_length is how many compiled programs that are unused to keep
30         // around after they are no longer in use (in case another EffectChain
31         // wants that exact program later). Shaders are expensive to compile and do not
32         // need a lot of resources to keep around, so this should be a reasonable number.
33         //
34         // texture_freelist_max_bytes is how many bytes of unused textures to keep around
35         // after they are no longer in use (in case a new texture of the same dimensions
36         // and format is needed). Note that the size estimate is very coarse; it does not
37         // take into account padding, metadata, and most importantly mipmapping.
38         // This means you should be prepared for actual memory usage of the freelist being
39         // twice this estimate or more.
40         ResourcePool(size_t program_freelist_max_length = 100,
41                      size_t texture_freelist_max_bytes = 100 << 20);  // 100 MB.
42         ~ResourcePool();
43
44         // All remaining functions are intended for calls from EffectChain only.
45
46         // Compile the given vertex+fragment shader pair, or fetch an already
47         // compiled program from the cache if possible. Keeps ownership of the
48         // program; you must call release_glsl_program() instead of deleting it
49         // when you no longer want it.
50         GLuint compile_glsl_program(const std::string& vertex_shader, const std::string& fragment_shader);
51         void release_glsl_program(GLuint glsl_program_num);
52
53         // Allocate a 2D texture of the given internal format and dimensions,
54         // or fetch a previous used if possible. Unbinds GL_TEXTURE_2D afterwards.
55         // Keeps ownership of the texture; you must call release_2d_texture() instead
56         // of deleting it when you no longer want it.
57         //
58         // Note: Currently we do not actually have a freelist, but this will change soon.
59         GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height);
60         void release_2d_texture(GLuint texture_num);
61
62 private:
63         // Delete the given program and both its shaders.
64         void delete_program(GLuint program_num);
65
66         // Protects all the other elements in the class.
67         pthread_mutex_t lock;
68
69         size_t program_freelist_max_length, texture_freelist_max_bytes;
70                 
71         // A mapping from vertex/fragment shader source strings to compiled program number.
72         std::map<std::pair<std::string, std::string>, GLuint> programs;
73
74         // A mapping from compiled program number to number of current users.
75         // Once this reaches zero, the program is taken out of this map and instead
76         // put on the freelist (after which it may be deleted).
77         std::map<GLuint, int> program_refcount;
78
79         // A mapping from program number to vertex and fragment shaders.
80         std::map<GLuint, std::pair<GLuint, GLuint> > program_shaders;
81
82         // A list of programs that are no longer in use, most recently freed first.
83         // Once this reaches <program_freelist_max_length>, the last element
84         // will be deleted.
85         std::list<GLuint> program_freelist;
86
87         struct Texture2D {
88                 GLint internal_format;
89                 GLsizei width, height;
90         };
91
92         // A mapping from texture number to format details. This is filled if the
93         // texture is given out to a client or on the freelist, but not if it is
94         // deleted from the freelist.
95         std::map<GLuint, Texture2D> texture_formats;
96
97         // A list of all textures that are release but not freed (most recently freed
98         // first), and an estimate of their current memory usage. Once
99         // <texture_freelist_bytes> goes above <texture_freelist_max_bytes>,
100         // elements are deleted off the end of the list until we are under the limit
101         // again.
102         std::list<GLuint> texture_freelist;
103         size_t texture_freelist_bytes;
104
105         // See the caveats at the constructor.
106         static size_t estimate_texture_size(const Texture2D &texture_format);
107 };
108
109 #endif  // !defined(_MOVIT_RESOURCE_POOL_H)