]> git.sesse.net Git - movit/blob - resource_pool.h
Add missing format LUMINANCE8.
[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 <list>
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <GL/glew.h>
24 #include <pthread.h>
25
26 class ResourcePool {
27 public:
28         // program_freelist_max_length is how many compiled programs that are unused to keep
29         // around after they are no longer in use (in case another EffectChain
30         // wants that exact program later). Shaders are expensive to compile and do not
31         // need a lot of resources to keep around, so this should be a reasonable number.
32         ResourcePool(size_t program_freelist_max_length = 100);
33         ~ResourcePool();
34
35         // All remaining functions are intended for calls from EffectChain only.
36
37         // Compile the given vertex+fragment shader pair, or fetch an already
38         // compiled program from the cache if possible. Keeps ownership of the
39         // program; you must call release_glsl_program() instead of deleting it
40         // when you no longer want it.
41         GLuint compile_glsl_program(const std::string& vertex_shader, const std::string& fragment_shader);
42         void release_glsl_program(GLuint glsl_program_num);
43
44         // Allocate a 2D texture of the given internal format and dimensions,
45         // or fetch a previous used if possible. Unbinds GL_TEXTURE_2D afterwards.
46         // Keeps ownership of the texture; you must call release_2d_texture() instead
47         // of deleting it when you no longer want it.
48         //
49         // Note: Currently we do not actually have a freelist, but this will change soon.
50         GLuint create_2d_texture(GLint internal_format, GLsizei width, GLsizei height);
51         void release_2d_texture(GLuint texture_num);
52
53 private:
54         // Delete the given program and both its shaders.
55         void delete_program(GLuint program_num);
56
57         // Protects all the other elements in the class.
58         pthread_mutex_t lock;
59
60         size_t program_freelist_max_length;
61                 
62         // A mapping from vertex/fragment shader source strings to compiled program number.
63         std::map<std::pair<std::string, std::string>, GLuint> programs;
64
65         // A mapping from compiled program number to number of current users.
66         // Once this reaches zero, the program is taken out of this map and instead
67         // put on the freelist (after which it may be deleted).
68         std::map<GLuint, int> program_refcount;
69
70         // A mapping from program number to vertex and fragment shaders.
71         std::map<GLuint, std::pair<GLuint, GLuint> > program_shaders;
72
73         // A list of programs that are no longer in use, most recently freed first.
74         // Once this reaches <program_freelist_max_length>, the last element
75         // will be deleted.
76         std::list<GLuint> program_freelist;
77 };
78
79 #endif  // !defined(_MOVIT_RESOURCE_POOL_H)