-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl_gl_interop_texture.hpp
373 lines (269 loc) · 10.4 KB
/
cl_gl_interop_texture.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#ifndef CL_GL_INTEROP_TEXTURE_HPP_INCLUDED
#define CL_GL_INTEROP_TEXTURE_HPP_INCLUDED
#include <gl/glew.h>
#include <SFML/graphics.hpp>
#include <vector>
#include <boost/compute/source.hpp>
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/iota.hpp>
#include <boost/compute/interop/opengl.hpp>
#include <windows.h>
#include "clstate.h"
#include <atomic>
namespace compute = boost::compute;
///this eats our entire opengl usage
///we cant touch it until this is done
///Ok. OpenGL contexts are per thread, so this HAS to be on the main thread
/*inline
void write_gl(cl_gl_interop_texture* tex)
{
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, tex->current_write_target);
glDrawPixels(tex->w, tex->h, CL_RGBA, GL_BYTE, tex->data);
///glfinish?
tex->finished_async_write = 1;
}*/
inline
void read_callback(cl_event event, cl_int event_command_exec_status, void *user_data);
struct cl_gl_interop_texture
{
int w = 0, h = 0;
int which_item = 0;
bool has_init = false;
GLuint renderbuffer_id = 0;
compute::opengl_renderbuffer g_texture_gl;
sf::RenderTexture sfml_nogl;
sf::Texture sfml_nogl_tex;
compute::image2d g_texture_nogl;
int max_buffer_history = 3;
std::vector<cl_uchar4*> buffer_history;
int cbuffer = 0;
//std::thread async_write;
//std::atomic_int finished_async_write{1};
std::atomic_int finished_async_read{1};
GLuint current_write_target = 0;
cl_mem mem;
///on a context switch, this texture is rebuilt entirely, so we do not
///need to worry about this event becoming invalidated
compute::event ev;
int need_event = 0;
cl_uchar4* get_no_gl_buffer()
{
return buffer_history[cbuffer];
}
void flip_no_gl_buffer()
{
cbuffer++;
cbuffer = cbuffer % max_buffer_history;
}
/*cl_uchar4* get_flip_gl_buffer()
{
buffer_history[cbuffer];
}*/
cl_gl_interop_texture& operator=(const cl_gl_interop_texture& tex)
{
if(this == &tex)
return *this;
w = tex.w;
h = tex.h;
has_init = tex.has_init;
renderbuffer_id = tex.renderbuffer_id;
g_texture_gl = tex.g_texture_gl;
g_texture_nogl = tex.g_texture_nogl;
buffer_history = tex.buffer_history;
max_buffer_history = tex.max_buffer_history;
cbuffer = tex.cbuffer;
//sfml_nogl = tex.sfml_nogl;
sfml_nogl_tex = tex.sfml_nogl_tex;
int val = tex.finished_async_read;
finished_async_read = val;
current_write_target = tex.current_write_target;
mem = tex.mem;
ev = tex.ev;
need_event = tex.need_event;
return *this;
}
cl_gl_interop_texture()
{
}
cl_gl_interop_texture(int _w, int _h, int use_gl_interop, compute::command_queue cqueue)
{
init(_w, _h, use_gl_interop, cqueue);
}
///unacceptable to destroy here due to potential context switch, must be handled manually
void init(int _w, int _h, int use_gl_interop, compute::command_queue cqueue)
{
//if(has_init)
// destroy();
w = _w;
h = _h;
which_item = !use_gl_interop;
if(which_item == 0)
{
init_gl(cqueue);
}
else
{
init_nogl();
}
has_init = true;
}
cl_mem& get()
{
return mem;
}
cl_mem* get_ptr()
{
return &mem;
}
void gl_blit(GLuint target, GLuint source)
{
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
PFNGLBLITFRAMEBUFFEREXTPROC glBlitFramebufferEXT = (PFNGLBLITFRAMEBUFFEREXTPROC)wglGetProcAddress("glBlitFramebufferEXT");
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, source);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, target);
glDrawBuffer(GL_BACK);
int dest_w = w;
int dest_h = h;
///blit buffer to screen
glBlitFramebufferEXT(0 , 0, w, h, 0, 0, dest_w, dest_h, GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
void gl_blit_to_opengl(GLuint target, compute::command_queue cqueue)
{
compute::opengl_enqueue_release_gl_objects(1, &g_texture_gl.get(), cqueue);
gl_blit(target, renderbuffer_id);
compute::opengl_enqueue_acquire_gl_objects(1, &g_texture_gl.get(), cqueue);
}
inline
void cl_blit_to_opengl(GLuint target, compute::command_queue cqueue, bool allow_tick)
{
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
PFNGLBLITFRAMEBUFFEREXTPROC glBlitFramebufferEXT = (PFNGLBLITFRAMEBUFFEREXTPROC)wglGetProcAddress("glBlitFramebufferEXT");
size_t origin[3] = {0,0,0};
size_t region[3] = {w, h, 1};
//if(!finished_async_read)
// cqueue.finish();
//if(finished_async_read)
{
//cl_uchar4* buffer = get_no_gl_buffer();
cl_uchar4* buffer = buffer_history[(cbuffer+1) % max_buffer_history];
sfml_nogl_tex.update((sf::Uint8*)buffer);
///I think the reason why we cant do the below
///is that sfml textures are probably not a framebuffer
///although the rendertextures probably *are*
/*sf::Sprite spr;
spr.setTexture(sfml_nogl_tex);
sfml_nogl.draw(spr);
sfml_nogl.display();
glBindFramebufferEXT(GL_READ_FRAMEBUFFER, sfml_nogl.getTexture().getNativeHandle());
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebufferEXT(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);*/
}
flip_no_gl_buffer();
//finished_async_read = 0;
cl_event read_event;
cl_event* evp = need_event ? &ev.get() : nullptr;
need_event = 0;
evp = nullptr;
cl_int err = clEnqueueReadImage(cqueue, g_texture_nogl.get(), CL_FALSE, origin, region, w * sizeof(cl_uchar4), 0, get_no_gl_buffer(), need_event, evp, &read_event);
if(err != CL_SUCCESS)
{
printf("Derror read image %i\n", err);
return;
}
clSetEventCallback(read_event, CL_COMPLETE, read_callback, this);
//if(need_event)
// clReleaseEvent(ev.get());
current_write_target = target;
need_event = 1;
ev = compute::event(read_event, false);
}
///allow_tick means that we can treat this as a tick function
///Ie read this frame, and blit next frame
///this does not affect cl/gl interop
///but will be a colossal performance win for non interop cl/gl
///ok these do different things
///gl actually blits it
///cl blits it to an sfml texture, which must be externally dealt with
void blit_to_opengl(GLuint target, compute::command_queue cqueue, bool allow_tick = false)
{
if(which_item == 0)
gl_blit_to_opengl(target, cqueue);
if(which_item == 1)
{
cl_blit_to_opengl(target, cqueue, allow_tick);
}
}
void init_gl(compute::command_queue cqueue)
{
PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)wglGetProcAddress("glGenFramebuffersEXT");
PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)wglGetProcAddress("glBindFramebufferEXT");
PFNGLGENRENDERBUFFERSEXTPROC glGenRenderbuffersEXT = (PFNGLGENRENDERBUFFERSEXTPROC)wglGetProcAddress("glGenRenderbuffersEXT");
PFNGLBINDRENDERBUFFEREXTPROC glBindRenderbufferEXT = (PFNGLBINDRENDERBUFFEREXTPROC)wglGetProcAddress("glBindRenderbufferEXT");
PFNGLRENDERBUFFERSTORAGEEXTPROC glRenderbufferStorageEXT = (PFNGLRENDERBUFFERSTORAGEEXTPROC)wglGetProcAddress("glRenderbufferStorageEXT");
PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC glFramebufferRenderbufferEXT = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)wglGetProcAddress("glFramebufferRenderbufferEXT");
GLuint screen_id;
glGenRenderbuffersEXT(1, &screen_id);
glBindRenderbufferEXT(GL_RENDERBUFFER, screen_id);
///generate storage for renderbuffer
glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_RGBA16, w, h);
//glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_RGBA8, w, h);
GLuint framebuf;
///get a framebuffer and bind it
glGenFramebuffersEXT(1, &framebuf);
glBindFramebufferEXT(GL_FRAMEBUFFER, framebuf);
///attach one to the other
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, screen_id);
///have opencl nab this and store in g_screen
compute::opengl_renderbuffer buf = compute::opengl_renderbuffer(cl::context, screen_id, compute::memory_object::read_write);
renderbuffer_id = framebuf;
g_texture_gl = buf;
compute::opengl_enqueue_acquire_gl_objects(1, &g_texture_gl.get(), cqueue);
mem = g_texture_gl.get();
}
void init_nogl()
{
compute::image_format format_opengl(CL_RGBA, CL_HALF_FLOAT);
//compute::image_format format_opengl(CL_RGBA, CL_UNORM_INT8);
g_texture_nogl = compute::image2d(cl::context, CL_MEM_READ_WRITE, format_opengl, w, h, 0, nullptr);
sfml_nogl.create(w, h);
sfml_nogl_tex.create(w, h);
/*no_gl_buffer_1 = new cl_uchar4[w*h]();
no_gl_buffer_2 = new cl_uchar4[w*h]();*/
for(int i=0; i<max_buffer_history; i++)
{
buffer_history.push_back(new cl_uchar4[w*h]);
}
mem = g_texture_nogl.get();
}
void destroy()
{
if(has_init && which_item == 0)
{
///we have to pick a command queue
compute::opengl_enqueue_release_gl_objects(1, &g_texture_gl.get(), cl::cqueue);
g_texture_gl = compute::opengl_renderbuffer();
}
if(has_init && which_item == 1)
{
g_texture_nogl = compute::image2d();
for(auto& i : buffer_history)
{
delete [] i;
}
}
ev = compute::event();
}
~cl_gl_interop_texture()
{
//destroy();
}
};
inline
void read_callback(cl_event event, cl_int event_command_exec_status, void *user_data)
{
cl_gl_interop_texture* me = (cl_gl_interop_texture*)user_data;
me->finished_async_read = 1;
//me->finished_async_write = 0;
//me->async_write = std::thread(std::bind(write_gl, me));
}
#endif // CL_GL_INTEROP_TEXTURE_HPP_INCLUDED