VVISF & VVGL
GLTexToCPUCopier.hpp
1 #ifndef VVGL_GLTexToCPUCopier_hpp
2 #define VVGL_GLTexToCPUCopier_hpp
3 
4 #include "VVGL_Defines.hpp"
5 #include "GLBufferPool.hpp"
6 
7 #include <mutex>
8 #include <queue>
9 
10 
11 
12 
13 // none of this stuff should be available if we're running ES
14 #if !defined(VVGL_TARGETENV_GLES) && !defined(VVGL_TARGETENV_GLES3)
15 
16 
17 
18 
19 namespace VVGL
20 {
21 
22 
23 
24 
25 using namespace std;
26 
27 
28 
29 
31 
36 class VVGL_EXPORT GLTexToCPUCopier {
37  private:
38  recursive_mutex _queueLock; // this should be used to serialize access to all member vars
39  GLContextRef _queueCtx = nullptr; // this is the context used to perform all GL action
40  int _queueSize = 1; // the number of buffers that should be in 'queue' before popping a new buffer off of it (double-buffering is 1)
41  queue<GLBufferRef> _cpuQueue; // queue of CPU-based images
42  queue<GLBufferRef> _pboQueue; // queue of PBOs
43  queue<GLBufferRef> _texQueue; // queue of textures
44  queue<GLBufferRef> _fboQueue; // queue of FBOs. the fastest texture download pipeline involves attaching the texture to an FBO so we can use glReadPixels() instead of glGetTexImage().
45 
46  GLBufferPoolRef _privatePool = nullptr; // by default this is null and the scene will try to use the global buffer pool to create interim resources (temp/persistent buffers). if non-null, the scene will use this pool to create interim resources.
47 
48  private:
49  // before calling either of these functions, _queueLock should be locked and a GL context needs to be made current on this thread.
50  void _beginProcessing(const GLBufferRef & inCPUBuffer, const GLBufferRef & inPBOBuffer, const GLBufferRef & inTexBuffer, const GLBufferRef & inFBOBuffer);
51  void _finishProcessing(const GLBufferRef & inCPUBuffer, const GLBufferRef & inPBOBuffer, const GLBufferRef & inTexBuffer, const GLBufferRef & inFBOBuffer);
52 
53  public:
55  GLTexToCPUCopier(const GLContextRef & inCtx) : _queueCtx(inCtx) {};
57 
59  void clearStream();
60 
62  void setQueueSize(const int & inNewQueueSize);
64  inline int queueSize() { lock_guard<recursive_mutex> lock(_queueLock); return _queueSize; };
65 
67 
72  GLBufferRef downloadTexToCPU(const GLBufferRef & inTexBuffer, const GLBufferRef & inCPUBuffer=nullptr, const bool & createInCurrentContext=false);
73 
75 
80  GLBufferRef streamTexToCPU(const GLBufferRef & inTexBuffer, const GLBufferRef & inCPUBuffer=nullptr, const bool & createInCurrentContext=false);
81 
83  void setPrivatePool(const GLBufferPoolRef & n) { _privatePool=n; }
85  GLBufferPoolRef privatePool() { return _privatePool; }
86 };
87 
88 
89 
90 
95 inline GLTexToCPUCopierRef CreateGLTexToCPUCopierRef() { return make_shared<GLTexToCPUCopier>(); }
100 inline GLTexToCPUCopierRef CreateGLTexToCPUCopierRefUsing(const GLContextRef & inCtx) { return make_shared<GLTexToCPUCopier>(inCtx); }
101 
102 
103 
104 
105 }
106 
107 
108 
109 
110 #endif // !defined(VVGL_TARGETENV_GLES) && !defined(VVGL_TARGETENV_GLES3)
111 
112 
113 
114 
115 #endif /* VVGL_GLTexToCPUCopier_hpp */
Definition: GLBuffer.hpp:13
int queueSize()
Returns the size of the queue used for streaming.
Definition: GLTexToCPUCopier.hpp:64
Downloads texture-based GLBuffers (Type_Tex) to CPU memory.
Definition: GLTexToCPUCopier.hpp:36
STL namespace.
GLTexToCPUCopierRef CreateGLTexToCPUCopierRef()
Creates and returns a GLTexToCPUCopier. The scene makes a new GL context which shares the context of ...
Definition: GLTexToCPUCopier.hpp:95
shared_ptr< GLTexToCPUCopier > GLTexToCPUCopierRef
A GLTexToCPUCopierRef is a shared pointer around a GLTexToCPUCopier.
Definition: VVGL_Base.hpp:80
GLTexToCPUCopierRef CreateGLTexToCPUCopierRefUsing(const GLContextRef &inCtx)
Creates and returns a GLTexToCPUCopier. The downloader uses the passed GL context to perform its GL o...
Definition: GLTexToCPUCopier.hpp:100
GLBufferPoolRef privatePool()
Gets the receiver&#39;s private buffer pool- null by default, only non-null if something called setPrivat...
Definition: GLTexToCPUCopier.hpp:85
void setPrivatePool(const GLBufferPoolRef &n)
Sets the receiver&#39;s private buffer pool (which should default to null). If non-null, this buffer pool will be used to generate any GL resources required by this scene. Handy if you have a variety of GL contexts that aren&#39;t shared and you have to switch between them rapidly on a per-frame basis.
Definition: GLTexToCPUCopier.hpp:83