]> git.sesse.net Git - movit/blob - resource_pool.h
Have separate FBOs per resolution and format.
[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 namespace movit {
28
29 class ResourcePool {
30 public:
31         // program_freelist_max_length is how many compiled programs that are unused to keep
32         // around after they are no longer in use (in case another EffectChain
33         // wants that exact program later). Shaders are expensive to compile and do not
34         // need a lot of resources to keep around, so this should be a reasonable number.
35         //
36         // texture_freelist_max_bytes is how many bytes of unused textures to keep around
37         // after they are no longer in use (in case a new texture of the same dimensions
38         // and format is needed). Note that the size estimate is very coarse; it does not
39         // take into account padding, metadata, and most importantly mipmapping.
40         // This means you should be prepared for actual memory usage of the freelist being
41         // twice this estimate or more.
42         ResourcePool(size_t program_freelist_max_length = 100,
43                      size_t texture_freelist_max_bytes = 100 << 20,  // 100 MB.
44                      size_t fbo_freelist_max_length = 100);
45         ~ResourcePool();
46
47         // All remaining functions are intended for calls from EffectChain only.
48
49         // Compile the given vertex+fragment shader pair, or fetch an already
50         // compiled program from the cache if possible. Keeps ownership of the
51         // program; you must call release_glsl_program() instead of deleting it
52         // when you no longer want it.
53         GLuint compile_glsl_program(const std::string& vertex_shader, const std::string& fragment_shader);
54         void release_glsl_program(GLuint glsl_program_num);
55
56         // Allocate a 2D texture of the given internal format and dimensions,
57         // or fetch a previous used if possible. Unbinds GL_TEXTURE_2D afterwards.
58         // Keeps ownership of the texture; you must call release_2d_texture() instead
59         // of deleting it when you no longer want it.
60         GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height);
61         void release_2d_texture(GLuint texture_num);
62
63         // Allocate an FBO used for the given internal format and dimensions,
64         // or fetch a previous used if possible. Keeps ownership of the FBO;
65         // you must call release_fbo() of deleting it when you no longer want it.
66         // You can get an appropriate context pointer from get_gl_context_identifier().
67         //
68         // NOTE: In principle, the FBO doesn't have a resolution or pixel format;
69         // you can bind almost whatever texture you want to it. However, changing
70         // resolution or pixel formats can have an adverse effect on performance,
71         // in particular on NVidia cards. Also, keep in mind that FBOs are not
72         // shareable across contexts.
73         GLuint create_fbo(void *context, GLint internal_format, GLsizei width, GLsizei height);
74         void release_fbo(GLuint fbo_num);
75
76 private:
77         // Delete the given program and both its shaders.
78         void delete_program(GLuint program_num);
79
80         // Protects all the other elements in the class.
81         pthread_mutex_t lock;
82
83         size_t program_freelist_max_length, texture_freelist_max_bytes, fbo_freelist_max_length;
84                 
85         // A mapping from vertex/fragment shader source strings to compiled program number.
86         std::map<std::pair<std::string, std::string>, GLuint> programs;
87
88         // A mapping from compiled program number to number of current users.
89         // Once this reaches zero, the program is taken out of this map and instead
90         // put on the freelist (after which it may be deleted).
91         std::map<GLuint, int> program_refcount;
92
93         // A mapping from program number to vertex and fragment shaders.
94         std::map<GLuint, std::pair<GLuint, GLuint> > program_shaders;
95
96         // A list of programs that are no longer in use, most recently freed first.
97         // Once this reaches <program_freelist_max_length>, the last element
98         // will be deleted.
99         std::list<GLuint> program_freelist;
100
101         struct Texture2D {
102                 GLint internal_format;
103                 GLsizei width, height;
104         };
105
106         // A mapping from texture number to format details. This is filled if the
107         // texture is given out to a client or on the freelist, but not if it is
108         // deleted from the freelist.
109         std::map<GLuint, Texture2D> texture_formats;
110
111         // A list of all textures that are release but not freed (most recently freed
112         // first), and an estimate of their current memory usage. Once
113         // <texture_freelist_bytes> goes above <texture_freelist_max_bytes>,
114         // elements are deleted off the end of the list until we are under the limit
115         // again.
116         std::list<GLuint> texture_freelist;
117         size_t texture_freelist_bytes;
118
119         struct FBO {
120                 void *context;
121                 GLint internal_format;
122                 GLsizei width, height;
123         };
124
125         // A mapping from FBO number to format details. This is filled if the
126         // FBO is given out to a client or on the freelist, but not if it is
127         // deleted from the freelist.
128         std::map<GLuint, FBO> fbo_formats;
129
130         // A list of all FBOs that are release but not freed (most recently freed
131         // first). Once this reaches <fbo_freelist_max_length>, the last element
132         // will be deleted.
133         std::list<GLuint> fbo_freelist;
134
135         // See the caveats at the constructor.
136         static size_t estimate_texture_size(const Texture2D &texture_format);
137 };
138
139 }  // namespace movit
140
141 #endif  // !defined(_MOVIT_RESOURCE_POOL_H)