1 module dcl.gl.memory;
2 
3 version(clglinterop):
4 
5 import derelict.opengl3.gl;
6 
7 import dcl.base;
8 import dcl.memory;
9 import dcl.commandqueue;
10 import dcl.context;
11 import dcl.event;
12 
13 import des.cl.gl.context;
14 
15 ///
16 class CLGLMemory : CLMemory
17 {
18 protected:
19     ///
20     this( CLGLContext ctx, cl_mem id )
21     {
22         super( id );
23         gl_context = ctx;
24     }
25 
26     CLGLContext gl_context;
27 
28     bool _acquired = false;
29 
30 public:
31 
32     enum Access : Flag
33     {
34         RW = Flag.READ_WRITE,
35         RO = Flag.READ_ONLY,
36         WO = Flag.WRITE_ONLY
37     }
38 
39     ///
40     static auto createFromGLBuffer( CLGLContext ctx, uint gl_id,
41             Access access=Access.RW )
42     in{ assert( ctx !is null ); } body
43     {
44         auto id = checkCode!clCreateFromGLBuffer( ctx.id, access, gl_id );
45         return new CLGLMemory( ctx, id );
46     }
47 
48     /// note: miplevel=0;
49     static auto createFromGLTexture( CLGLContext ctx, uint gl_id,
50             GLenum target, Access access=Access.RW )
51     in{ assert( ctx !is null ); } body
52     {
53         enum miplevel = 0;
54         auto id = checkCode!clCreateFromGLTexture( ctx.id, access, target, miplevel, gl_id );
55         return new CLGLMemory( ctx, id );
56     }
57 
58     ///
59     static auto createFromGLRenderBuffer( CLGLContext ctx, uint gl_id, Access access=Access.RW )
60     in{ assert( ctx !is null ); } body
61     {
62         auto id = checkCode!clCreateFromGLRenderbuffer( ctx.id, access, gl_id );
63         return new CLGLMemory( ctx, id );
64     }
65 
66     ///
67     bool acquired() @property const { return _acquired; }
68 
69     ///
70     void acquireFromGL( CLCommandQueue queue, CLEvent[] wait_list=[], CLEvent* event=null )
71     in{ assert( queue !is null ); } body
72     {
73         if( acquired ) return;
74         checkCallWL!clEnqueueAcquireGLObjects( queue.id, 1u, &id,
75                 wait_list, event );
76         _acquired = true;
77         (cast(CLGLContext)context).registerAcquired( this );
78     }
79 
80     ///
81     void releaseToGL( CLCommandQueue queue, CLEvent[] wait_list=[], CLEvent* event=null )
82     {
83         if( !acquired ) return;
84         checkCallWL!clEnqueueReleaseGLObjects( queue.id, 1u, &id,
85                 wait_list, event );
86         _acquired = false;
87         (cast(CLGLContext)context).unregisterAcquired( this );
88     }
89 
90     override CLContext context() @property
91     {
92         assert( gl_context.id == super.context.id );
93         return gl_context;
94     }
95 
96     package void ctxReleaseToGL() { _acquired = false; }
97 }