1 #ifndef _MOVIT_RESOURCE_POOL_H
2 #define _MOVIT_RESOURCE_POOL_H 1
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.
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).
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.
19 // Memory management (only relevant if you use multiple contexts): Some objects,
20 // like FBOs, are not shareable across contexts, and can only be deleted from
21 // the context they were created in. Thus, you will need to tell the
22 // ResourcePool explicitly if you delete a context, or they will leak (and the
23 // ResourcePool destructor will assert-fail). See clean_context().
39 // program_freelist_max_length is how many compiled programs that are unused to keep
40 // around after they are no longer in use (in case another EffectChain
41 // wants that exact program later). Shaders are expensive to compile and do not
42 // need a lot of resources to keep around, so this should be a reasonable number.
44 // texture_freelist_max_bytes is how many bytes of unused textures to keep around
45 // after they are no longer in use (in case a new texture of the same dimensions
46 // and format is needed). Note that the size estimate is very coarse; it does not
47 // take into account padding, metadata, and most importantly mipmapping.
48 // This means you should be prepared for actual memory usage of the freelist being
49 // twice this estimate or more.
50 ResourcePool(size_t program_freelist_max_length = 100,
51 size_t texture_freelist_max_bytes = 100 << 20, // 100 MB.
52 size_t fbo_freelist_max_length = 100); // Per context.
55 // All remaining functions are intended for calls from EffectChain only.
57 // Compile the given vertex+fragment shader pair, or fetch an already
58 // compiled program from the cache if possible. Keeps ownership of the
59 // program; you must call release_glsl_program() instead of deleting it
60 // when you no longer want it.
62 // If <fragment_shader_outputs> contains more than one value, the given
63 // outputs will be bound to fragment shader output colors in the order
64 // they appear in the vector. Otherwise, output order is undefined and
65 // determined by the OpenGL driver.
66 GLuint compile_glsl_program(const std::string& vertex_shader,
67 const std::string& fragment_shader,
68 const std::vector<std::string>& frag_shader_outputs);
69 void release_glsl_program(GLuint glsl_program_num);
71 // Since uniforms belong to the program and not to the context,
72 // a given GLSL program number can't be used by more than one thread
73 // at a time. Thus, if two threads want to use the same program
74 // (usually because two EffectChains share them via caching),
75 // we will need to make a clone. use_glsl_program() makes such
76 // a clone if needed, calls glUseProgram(), and returns the real
77 // program number that was used; this must be given to
78 // unuse_glsl_program() to release it. unuse_glsl_program() does not
79 // actually change any OpenGL state, though.
80 GLuint use_glsl_program(GLuint glsl_program_num);
81 void unuse_glsl_program(GLuint instance_program_num);
83 // Allocate a 2D texture of the given internal format and dimensions,
84 // or fetch a previous used if possible. Unbinds GL_TEXTURE_2D afterwards.
85 // Keeps ownership of the texture; you must call release_2d_texture() instead
86 // of deleting it when you no longer want it.
87 GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height);
88 void release_2d_texture(GLuint texture_num);
90 // Allocate an FBO with the the given texture(s) bound as framebuffer attachment(s),
91 // or fetch a previous used if possible. Unbinds GL_FRAMEBUFFER afterwards.
92 // Keeps ownership of the FBO; you must call release_fbo() of deleting
93 // it when you no longer want it.
95 // NOTE: In principle, the FBO doesn't have a resolution or pixel format;
96 // you can bind almost whatever texture you want to it. However, changing
97 // textures can have an adverse effect on performance due to validation,
98 // in particular on NVidia cards. Also, keep in mind that FBOs are not
99 // shareable across contexts, so you must have the context that's supposed
100 // to own the FBO current when you create or release it.
101 GLuint create_fbo(GLuint texture0_num,
102 GLuint texture1_num = 0,
103 GLuint texture2_num = 0,
104 GLuint texture3_num = 0);
105 void release_fbo(GLuint fbo_num);
107 // Informs the ResourcePool that the current context is going away soon,
108 // and that any resources held for it in the freelist should be deleted.
110 // You do not need to do this for the last context; the regular destructor
111 // will take care of that. This means that if you only ever use one
112 // thread/context, you never need to call this function.
113 void clean_context();
116 // Delete the given program and both its shaders.
117 void delete_program(GLuint program_num);
119 // Deletes all FBOs for the given context that belong to deleted textures.
120 void cleanup_unlinked_fbos(void *context);
122 // Remove FBOs off the end of the freelist for <context>, until it
123 // is no more than <max_length> elements long.
124 void shrink_fbo_freelist(void *context, size_t max_length);
126 // Link the given vertex and fragment shaders into a full GLSL program.
127 // See compile_glsl_program() for explanation of <fragment_shader_outputs>.
128 static GLuint link_program(GLuint vs_obj,
130 const std::vector<std::string>& fragment_shader_outputs);
132 // Protects all the other elements in the class.
133 pthread_mutex_t lock;
135 size_t program_freelist_max_length, texture_freelist_max_bytes, fbo_freelist_max_length;
137 // A mapping from vertex/fragment shader source strings to compiled program number.
138 std::map<std::pair<std::string, std::string>, GLuint> programs;
140 // A mapping from compiled program number to number of current users.
141 // Once this reaches zero, the program is taken out of this map and instead
142 // put on the freelist (after which it may be deleted).
143 std::map<GLuint, int> program_refcount;
145 // A mapping from program number to vertex and fragment shaders.
146 // Contains everything needed to re-link the program.
148 GLuint vs_obj, fs_obj;
149 std::vector<std::string> fragment_shader_outputs;
151 std::map<GLuint, ShaderSpec> program_shaders;
153 // For each program, a list of other programs that are exactly like it.
154 // By default, will only contain the program itself, but due to cloning
155 // (see use_glsl_program()), may grow. Programs are taken off this list
156 // while they are in use (by use_glsl_program()).
157 std::map<GLuint, std::stack<GLuint> > program_instances;
159 // For each program, the master program that created it
160 // (inverse of program_instances).
161 std::map<GLuint, GLuint> program_masters;
163 // A list of programs that are no longer in use, most recently freed first.
164 // Once this reaches <program_freelist_max_length>, the last element
166 std::list<GLuint> program_freelist;
169 GLint internal_format;
170 GLsizei width, height;
173 // A mapping from texture number to format details. This is filled if the
174 // texture is given out to a client or on the freelist, but not if it is
175 // deleted from the freelist.
176 std::map<GLuint, Texture2D> texture_formats;
178 // A list of all textures that are release but not freed (most recently freed
179 // first), and an estimate of their current memory usage. Once
180 // <texture_freelist_bytes> goes above <texture_freelist_max_bytes>,
181 // elements are deleted off the end of the list until we are under the limit
183 std::list<GLuint> texture_freelist;
184 size_t texture_freelist_bytes;
186 static const unsigned num_fbo_attachments = 4;
189 // GL_INVALID_INDEX means associated to a texture that has since been deleted.
190 // 0 means the output isn't bound.
191 GLuint texture_num[num_fbo_attachments];
194 // For each context, a mapping from FBO number to format details. This is
195 // filled if the FBO is given out to a client or on the freelist, but
196 // not if it is deleted from the freelist.
197 std::map<std::pair<void *, GLuint>, FBO> fbo_formats;
198 typedef std::map<std::pair<void *, GLuint>, FBO>::iterator FBOFormatIterator;
200 // For each context, a list of all FBOs that are released but not freed
201 // (most recently freed first). Once this reaches <fbo_freelist_max_length>,
202 // the last element will be deleted.
204 // We store iterators directly into <fbo_format> for efficiency.
205 std::map<void *, std::list<FBOFormatIterator> > fbo_freelist;
207 // See the caveats at the constructor.
208 static size_t estimate_texture_size(const Texture2D &texture_format);
213 #endif // !defined(_MOVIT_RESOURCE_POOL_H)