]> git.sesse.net Git - nageru/blob - futatabi/pbo_pool.h
Set CEF autoplay policy to be more lenient.
[nageru] / futatabi / pbo_pool.h
1 #ifndef _PBO_POOL_H
2 #define _PBO_POOL_H 1
3
4 // Keeps a pool of persistently mapped PBOs around that can be used as staging
5 // buffers for texture uploads. (Uploading from a PBO is asynchronous and done
6 // by the GPU, so assuming we don't need an extra copy into the PBO, this is a
7 // significant win over uploading from regular malloc-ed RAM.)
8 //
9 // Unlike Nageru's PBOFrameAllocator, these are not connected to
10 // a given frame, since we can have thousands of frames in the cache
11 // at any given time. Thus, we need to have separate fences for each PBO
12 // to know that the upload is done.
13
14 #include <mutex>
15 #include <queue>
16
17 #include <epoxy/gl.h>
18
19 #include "shared/ref_counted_gl_sync.h"
20
21 struct PBO {
22         GLuint pbo;
23         uint8_t *ptr;  // Mapped memory.
24         RefCountedGLsync upload_done;
25 };
26
27 class PBOPool {
28 public:
29         PBOPool(size_t pbo_size = 8 << 20,  // 8 MB, large enough for 1080p 4:2:2.
30                 size_t num_pbos = 8,
31                 GLenum buffer = GL_PIXEL_UNPACK_BUFFER_ARB,
32                 GLenum permissions = GL_MAP_WRITE_BIT,
33                 GLenum map_bits = GL_MAP_FLUSH_EXPLICIT_BIT);
34
35         PBO alloc_pbo();
36         void release_pbo(PBO pbo);  // Set a fence on upload_done if the PBO may still be in use.
37
38 private:
39         PBO create_pbo();
40
41         std::mutex freelist_mutex;
42         std::queue<PBO> freelist;
43
44         size_t pbo_size;
45         GLenum buffer, permissions, map_bits;
46 };
47
48 extern PBOPool *global_pbo_pool;
49 void init_pbo_pool();  // Idempotent.
50
51 #endif  // !defined(_PBO_POOL_H)