f5ef5c4f6b866c88601ba1a8717e19f08627efea
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playback3d.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #define GL_GLEXT_PROTOTYPES
23
24 #include "bccolors.h"
25 #include "bcsignals.h"
26 #include "bcwindowbase.h"
27 #include "canvas.h"
28 #include "clip.h"
29 #include "condition.h"
30 #include "edl.h"
31 #include "maskautos.h"
32 #include "maskauto.h"
33 #include "mutex.h"
34 #include "mwindow.h"
35 #include "overlayframe.inc"
36 #include "overlayframe.h"
37 #include "playback3d.h"
38 #include "pluginclient.h"
39 #include "pluginvclient.h"
40 #include "edlsession.h"
41 #include "track.h"
42 #include "transportque.inc"
43 #include "vframe.h"
44
45 #ifdef HAVE_GL
46 #include <GL/gl.h>
47 #include <GL/glext.h>
48 #include <GL/glu.h>
49 #endif
50
51 #include <string.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54
55 #define QQ(q)#q
56 #define SS(s)QQ(s)
57
58
59 // Shaders
60 // These should be passed to VFrame::make_shader to construct shaders.
61 // Can't hard code sampler2D
62
63
64 #ifdef HAVE_GL
65 static const char *yuv_to_rgb_frag =
66         "uniform sampler2D tex;\n"
67         "uniform mat3 yuv_to_rgb_matrix;\n"
68         "uniform float yminf;\n"
69         "void main()\n"
70         "{\n"
71         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
72         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
73         "       gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
74         "}\n";
75
76 static const char *yuva_to_yuv_frag =
77         "uniform sampler2D tex;\n"
78         "void main()\n"
79         "{\n"
80         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
81         "       float a = yuva.a;\n"
82         "       float anti_a = 1.0 - a;\n"
83         "       yuva.r *= a;\n"
84         "       yuva.g = yuva.g * a + 0.5 * anti_a;\n"
85         "       yuva.b = yuva.b * a + 0.5 * anti_a;\n"
86         "       yuva.a = 1.0;\n"
87         "       gl_FragColor = yuva;\n"
88         "}\n";
89
90 static const char *yuva_to_rgb_frag =
91         "uniform sampler2D tex;\n"
92         "uniform mat3 yuv_to_rgb_matrix;\n"
93         "uniform float yminf;\n"
94         "void main()\n"
95         "{\n"
96         "       vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
97         "       yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
98         "       yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
99         "       yuva.rgb *= yuva.a;\n"
100         "       yuva.a = 1.0;\n"
101         "       gl_FragColor = yuva;\n"
102         "}\n";
103
104 static const char *rgb_to_yuv_frag =
105         "uniform sampler2D tex;\n"
106         "uniform mat3 rgb_to_yuv_matrix;\n"
107         "uniform float yminf;\n"
108         "void main()\n"
109         "{\n"
110         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
111         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
112         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
113         "       gl_FragColor = rgba;\n"
114         "}\n";
115
116
117 static const char *rgba_to_rgb_frag =
118         "uniform sampler2D tex;\n"
119         "void main()\n"
120         "{\n"
121         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
122         "       rgba.rgb *= rgba.a;\n"
123         "       rgba.a = 1.0;\n"
124         "       gl_FragColor = rgba;\n"
125         "}\n";
126
127 static const char *rgba_to_yuv_frag =
128         "uniform sampler2D tex;\n"
129         "uniform mat3 rgb_to_yuv_matrix;\n"
130         "uniform float yminf;\n"
131         "void main()\n"
132         "{\n"
133         "       vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
134         "       rgba.rgb *= rgba.a;\n"
135         "       rgba.a = 1.0;\n"
136         "       rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
137         "       rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
138         "       gl_FragColor = rgba;\n"
139         "}\n";
140
141 //static const char *rgba_to_rgb_flatten =
142 //      "void main() {\n"
143 //      "       gl_FragColor.rgb *= gl_FragColor.a;\n"
144 //      "       gl_FragColor.a = 1.0;\n"
145 //      "}\n";
146
147 #define GL_STD_BLEND(FN) \
148 static const char *blend_##FN##_frag = \
149         "uniform sampler2D tex2;\n" \
150         "uniform vec2 tex2_dimensions;\n" \
151         "uniform float alpha;\n" \
152         "void main() {\n" \
153         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
154         "       vec4 result;\n" \
155         "       result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
156         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
157         "       gl_FragColor = mix(canvas, result, alpha);\n" \
158         "}\n"
159
160 #define GL_VEC_BLEND(FN) \
161 static const char *blend_##FN##_frag = \
162         "uniform sampler2D tex2;\n" \
163         "uniform vec2 tex2_dimensions;\n" \
164         "uniform float alpha;\n" \
165         "void main() {\n" \
166         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
167         "       vec4 result;\n" \
168         "       result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
169         "       result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
170         "       result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
171         "       result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
172         "       result = clamp(result, 0.0, 1.0);\n" \
173         "       gl_FragColor = mix(canvas, result, alpha);\n" \
174         "}\n"
175
176 #undef mabs
177 #define mabs abs
178 #undef mmin
179 #define mmin min
180 #undef mmax
181 #define mmax max
182
183 #undef ZERO
184 #define ZERO 0.0
185 #undef ONE
186 #define ONE 1.0
187 #undef TWO
188 #define TWO 2.0
189
190 // NORMAL
191 static const char *blend_NORMAL_frag =
192         "uniform sampler2D tex2;\n"
193         "uniform vec2 tex2_dimensions;\n"
194         "uniform float alpha;\n"
195         "void main() {\n"
196         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
197         "       vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
198         "       gl_FragColor = mix(canvas, result, alpha);\n"
199         "}\n";
200
201 // REPLACE
202 static const char *blend_REPLACE_frag =
203         "uniform float alpha;\n"
204         "void main() {\n"
205         "}\n";
206
207 // ADDITION
208 static const char *blend_ADDITION_frag =
209         "uniform sampler2D tex2;\n"
210         "uniform vec2 tex2_dimensions;\n"
211         "uniform float alpha;\n"
212         "void main() {\n"
213         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
214         "       vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
215         "       gl_FragColor = mix(canvas, result, alpha);\n"
216         "}\n";
217
218 // SUBTRACT
219 static const char *blend_SUBTRACT_frag =
220         "uniform sampler2D tex2;\n"
221         "uniform vec2 tex2_dimensions;\n"
222         "uniform float alpha;\n"
223         "void main() {\n"
224         "       vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
225         "       vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
226         "       gl_FragColor = mix(canvas, result, alpha);\n"
227         "}\n";
228
229 GL_STD_BLEND(MULTIPLY);
230 GL_VEC_BLEND(DIVIDE);
231 GL_VEC_BLEND(MAX);
232 GL_VEC_BLEND(MIN);
233 GL_VEC_BLEND(DARKEN);
234 GL_VEC_BLEND(LIGHTEN);
235 GL_STD_BLEND(DST);
236 GL_STD_BLEND(DST_ATOP);
237 GL_STD_BLEND(DST_IN);
238 GL_STD_BLEND(DST_OUT);
239 GL_STD_BLEND(DST_OVER);
240 GL_STD_BLEND(SRC);
241 GL_STD_BLEND(SRC_ATOP);
242 GL_STD_BLEND(SRC_IN);
243 GL_STD_BLEND(SRC_OUT);
244 GL_STD_BLEND(SRC_OVER);
245 GL_STD_BLEND(AND);
246 GL_STD_BLEND(OR);
247 GL_STD_BLEND(XOR);
248 GL_VEC_BLEND(OVERLAY);
249 GL_STD_BLEND(SCREEN);
250 GL_VEC_BLEND(BURN);
251 GL_VEC_BLEND(DODGE);
252 GL_VEC_BLEND(HARDLIGHT);
253 GL_VEC_BLEND(SOFTLIGHT);
254 GL_VEC_BLEND(DIFFERENCE);
255
256 static const char *read_texture_frag =
257         "uniform sampler2D tex;\n"
258         "void main()\n"
259         "{\n"
260         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
261         "}\n";
262
263 static const char *in_vertex_frag =
264         "#version 430 // vertex shader\n"
265         "in vec3 in_pos;\n"
266         "void main() {\n"
267         "       gl_Position = vec4(in_pos-vec3(0.5,0.5,0.), .5);\n"
268         "}\n";
269
270 static const char *feather_frag =
271         "#version 430\n"
272         "layout(location=0) out vec4 color;\n"
273         "uniform sampler2D tex;\n"
274 // apparently, only doubles index properly in shared buffers
275         "buffer buf { dvec2 points[]; };\n"
276         "uniform float r;\n"
277         "uniform float v;\n"
278         "void main() {\n"
279         "       vec2 tex_st = gl_FragCoord.xy/textureSize(tex,0);\n"
280         "       color = texture(tex, tex_st);\n"
281         "       if( r==0. ) return;\n"
282         "       float rv = r*v>0. ? 1 : -1;\n"
283         "       float rr = r*r, dr = 1./rr;\n"
284         "       float vv = v>=0 ? 1.-v : 1.+v;\n"
285         "       float fg = rv>=0 ? vv : 1.;\n"
286         "       float bg = rv>=0 ? 1. : vv;\n"
287         "       int len = points.length();\n"
288         "       float sig2 = -log(255.0);\n"
289         "       for( int i=0; i<len; ++i ) {\n"
290         "               float dx = float(points[i].x) - gl_FragCoord.x;\n"
291         "               float dy = float(points[i].y) - gl_FragCoord.y;\n"
292         "               float dd = dx*dx + dy*dy;\n"
293         "               if( dd >= rr ) continue;\n"
294         "               float ss = dd / rr;\n"
295         "               float d = exp(ss * sig2);\n"
296         "               float a = d*fg + (1.-d)*bg;\n"
297         "               if( rv*(color.a-a) > 0 ) color = vec4(a);\n"
298         "       }\n"
299         "}\n";
300
301 static const char *multiply_mask4_frag =
302         "uniform sampler2D tex;\n"
303         "uniform sampler2D tex1;\n"
304         "void main()\n"
305         "{\n"
306         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
307         "       gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st).r;\n"
308         "}\n";
309
310 static const char *multiply_mask3_frag =
311         "uniform sampler2D tex;\n"
312         "uniform sampler2D tex1;\n"
313         "void main()\n"
314         "{\n"
315         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
316         "       float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
317         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
318         "}\n";
319
320 static const char *multiply_yuvmask3_frag =
321         "uniform sampler2D tex;\n"
322         "uniform sampler2D tex1;\n"
323         "void main()\n"
324         "{\n"
325         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
326         "       float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
327         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
328         "       gl_FragColor.rgb *= vec3(a, a, a);\n"
329         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
330         "}\n";
331
332 static const char *fade_rgba_frag =
333         "uniform sampler2D tex;\n"
334         "uniform float alpha;\n"
335         "void main()\n"
336         "{\n"
337         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
338         "       gl_FragColor.a *= alpha;\n"
339         "}\n";
340
341 static const char *fade_yuv_frag =
342         "uniform sampler2D tex;\n"
343         "uniform float alpha;\n"
344         "void main()\n"
345         "{\n"
346         "       gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
347         "       gl_FragColor.r *= alpha;\n"
348         "       gl_FragColor.gb -= vec2(0.5, 0.5);\n"
349         "       gl_FragColor.g *= alpha;\n"
350         "       gl_FragColor.b *= alpha;\n"
351         "       gl_FragColor.gb += vec2(0.5, 0.5);\n"
352         "}\n";
353
354 #endif
355
356
357 Playback3DCommand::Playback3DCommand()
358  : BC_SynchronousCommand()
359 {
360         canvas = 0;
361         is_nested = 0;
362 }
363
364 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
365 {
366         Playback3DCommand *ptr = (Playback3DCommand*)command;
367         this->canvas = ptr->canvas;
368         this->is_cleared = ptr->is_cleared;
369
370         this->in_x1 = ptr->in_x1;
371         this->in_y1 = ptr->in_y1;
372         this->in_x2 = ptr->in_x2;
373         this->in_y2 = ptr->in_y2;
374         this->out_x1 = ptr->out_x1;
375         this->out_y1 = ptr->out_y1;
376         this->out_x2 = ptr->out_x2;
377         this->out_y2 = ptr->out_y2;
378         this->alpha = ptr->alpha;
379         this->mode = ptr->mode;
380         this->interpolation_type = ptr->interpolation_type;
381
382         this->input = ptr->input;
383         this->start_position_project = ptr->start_position_project;
384         this->keyframe_set = ptr->keyframe_set;
385         this->keyframe = ptr->keyframe;
386         this->default_auto = ptr->default_auto;
387         this->plugin_client = ptr->plugin_client;
388         this->want_texture = ptr->want_texture;
389         this->is_nested = ptr->is_nested;
390         this->dst_cmodel = ptr->dst_cmodel;
391
392         BC_SynchronousCommand::copy_from(command);
393 }
394
395 Playback3D::Playback3D(MWindow *mwindow)
396  : BC_Synchronous()
397 {
398         this->mwindow = mwindow;
399         temp_texture = 0;
400 }
401
402 Playback3D::~Playback3D()
403 {
404 }
405
406
407
408
409 BC_SynchronousCommand* Playback3D::new_command()
410 {
411         return new Playback3DCommand;
412 }
413
414
415
416 void Playback3D::handle_command(BC_SynchronousCommand *command)
417 {
418 //printf("Playback3D::handle_command 1 %d\n", command->command);
419         switch(command->command)
420         {
421                 case Playback3DCommand::WRITE_BUFFER:
422                         write_buffer_sync((Playback3DCommand*)command);
423                         break;
424
425                 case Playback3DCommand::CLEAR_OUTPUT:
426                         clear_output_sync((Playback3DCommand*)command);
427                         break;
428
429                 case Playback3DCommand::CLEAR_INPUT:
430                         clear_input_sync((Playback3DCommand*)command);
431                         break;
432
433                 case Playback3DCommand::DO_CAMERA:
434                         do_camera_sync((Playback3DCommand*)command);
435                         break;
436
437                 case Playback3DCommand::OVERLAY:
438                         overlay_sync((Playback3DCommand*)command);
439                         break;
440
441                 case Playback3DCommand::DO_FADE:
442                         do_fade_sync((Playback3DCommand*)command);
443                         break;
444
445                 case Playback3DCommand::DO_MASK:
446                         do_mask_sync((Playback3DCommand*)command);
447                         break;
448
449                 case Playback3DCommand::PLUGIN:
450                         run_plugin_sync((Playback3DCommand*)command);
451                         break;
452
453                 case Playback3DCommand::COPY_FROM:
454                         copy_from_sync((Playback3DCommand*)command);
455                         break;
456
457                 case Playback3DCommand::CONVERT_CMODEL:
458                         convert_cmodel_sync((Playback3DCommand*)command);
459                         break;
460
461 //              case Playback3DCommand::DRAW_REFRESH:
462 //                      draw_refresh_sync((Playback3DCommand*)command);
463 //                      break;
464         }
465 //printf("Playback3D::handle_command 10\n");
466 }
467
468
469
470
471 void Playback3D::copy_from(Canvas *canvas,
472         VFrame *dst,
473         VFrame *src,
474         int want_texture)
475 {
476         Playback3DCommand command;
477         command.command = Playback3DCommand::COPY_FROM;
478         command.canvas = canvas;
479         command.frame = dst;
480         command.input = src;
481         command.want_texture = want_texture;
482         send_command(&command);
483 }
484
485 void Playback3D::copy_from_sync(Playback3DCommand *command)
486 {
487 #ifdef HAVE_GL
488         BC_WindowBase *window =
489                 command->canvas->lock_canvas("Playback3D::copy_from_sync");
490         if( window ) {
491                 window->enable_opengl();
492                 glFinish();
493                 int w = command->input->get_w();
494                 int h = command->input->get_h();
495
496                 if(command->input->get_opengl_state() == VFrame::SCREEN &&
497                         w == command->frame->get_w() && h == command->frame->get_h())
498                 {
499 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
500 // command->input->get_w(),
501 // command->input->get_h(),
502 // command->frame->get_w(),
503 // command->frame->get_h(),
504 // command->frame->get_color_model());
505 // With NVidia at least,
506                         if(w % 4)
507                         {
508                                 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
509                         }
510                         else
511 // Copy to texture
512                         if(command->want_texture)
513                         {
514 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
515 // Screen_to_texture requires the source pbuffer enabled.
516                                 command->input->enable_opengl();
517                                 command->frame->screen_to_texture();
518                                 command->frame->set_opengl_state(VFrame::TEXTURE);
519                         }
520                         else
521 // Copy to RAM
522                         {
523                                 command->input->to_texture();
524                                 command->input->bind_texture(0);
525                                 command->frame->enable_opengl();
526                                 command->frame->init_screen();
527                                 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
528                                         VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
529                                 if( shader > 0 ) {
530                                         glUseProgram(shader);
531                                         int variable = glGetUniformLocation(shader, "tex");
532                                         glUniform1i(variable, 0);
533                                         BC_GL_YUV_TO_RGB(shader);
534                                 }
535                                 else
536                                         glUseProgram(0);
537                                 command->input->draw_texture(1);
538                                 command->frame->screen_to_ram();
539                                 glUseProgram(0);
540                         }
541                 }
542                 else
543                 {
544                         printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
545                                 command->input->get_opengl_state(), w, h,
546                                 command->frame->get_w(), command->frame->get_h());
547                 }
548         }
549         command->canvas->unlock_canvas();
550 #endif
551 }
552
553
554
555
556 // void Playback3D::draw_refresh(Canvas *canvas,
557 //      VFrame *frame,
558 //      float in_x1,
559 //      float in_y1,
560 //      float in_x2,
561 //      float in_y2,
562 //      float out_x1,
563 //      float out_y1,
564 //      float out_x2,
565 //      float out_y2)
566 // {
567 //      Playback3DCommand command;
568 //      command.command = Playback3DCommand::DRAW_REFRESH;
569 //      command.canvas = canvas;
570 //      command.frame = frame;
571 //      command.in_x1 = in_x1;
572 //      command.in_y1 = in_y1;
573 //      command.in_x2 = in_x2;
574 //      command.in_y2 = in_y2;
575 //      command.out_x1 = out_x1;
576 //      command.out_y1 = out_y1;
577 //      command.out_x2 = out_x2;
578 //      command.out_y2 = out_y2;
579 //      send_command(&command);
580 // }
581 //
582 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
583 // {
584 // #ifdef HAVE_GL
585 //      BC_WindowBase *window =
586 //              command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
587 //      if( window ) {
588 //              window->enable_opengl();
589 //
590 // // Read output pbuffer back to RAM in project colormodel
591 // // RGB 8bit is fastest for OpenGL to read back.
592 //              command->frame->reallocate(0,
593 //                      0,
594 //                      0,
595 //                      0,
596 //                      command->frame->get_w(),
597 //                      command->frame->get_h(),
598 //                      BC_RGB888,
599 //                      -1);
600 //              command->frame->screen_to_ram();
601 //
602 //              window->clear_box(0,
603 //                                              0,
604 //                                              window->get_w(),
605 //                                              window->get_h());
606 //              window->draw_vframe(command->frame,
607 //                                                      (int)command->out_x1,
608 //                                                      (int)command->out_y1,
609 //                                                      (int)(command->out_x2 - command->out_x1),
610 //                                                      (int)(command->out_y2 - command->out_y1),
611 //                                                      (int)command->in_x1,
612 //                                                      (int)command->in_y1,
613 //                                                      (int)(command->in_x2 - command->in_x1),
614 //                                                      (int)(command->in_y2 - command->in_y1),
615 //                                                      0);
616 //
617 //      }
618 //      command->canvas->unlock_canvas();
619 // #endif
620 // }
621
622
623
624
625
626 void Playback3D::write_buffer(Canvas *canvas,
627         VFrame *frame,
628         float in_x1,
629         float in_y1,
630         float in_x2,
631         float in_y2,
632         float out_x1,
633         float out_y1,
634         float out_x2,
635         float out_y2,
636         int is_cleared)
637 {
638         Playback3DCommand command;
639         command.command = Playback3DCommand::WRITE_BUFFER;
640         command.canvas = canvas;
641         command.frame = frame;
642         command.in_x1 = in_x1;
643         command.in_y1 = in_y1;
644         command.in_x2 = in_x2;
645         command.in_y2 = in_y2;
646         command.out_x1 = out_x1;
647         command.out_y1 = out_y1;
648         command.out_x2 = out_x2;
649         command.out_y2 = out_y2;
650         command.is_cleared = is_cleared;
651         send_command(&command);
652 }
653
654
655 void Playback3D::write_buffer_sync(Playback3DCommand *command)
656 {
657 #ifdef HAVE_GL
658         BC_WindowBase *window =
659                 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
660         if( window ) {
661                 window->enable_opengl();
662 // Update hidden cursor
663                 window->update_video_cursor();
664                 command->frame->enable_opengl();
665                 command->frame->init_screen();
666 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
667                 int frame_state = command->frame->get_opengl_state();
668                 if( frame_state != VFrame::TEXTURE )
669                         command->frame->to_texture();
670                 if( frame_state != VFrame::RAM ) {
671                         command->in_y1 = command->frame->get_h() - command->in_y1;
672                         command->in_y2 = command->frame->get_h() - command->in_y2;
673                 }
674                 window->enable_opengl();
675                 draw_output(command, 1);
676                 command->frame->set_opengl_state(frame_state);
677         }
678         command->canvas->unlock_canvas();
679 #endif
680 }
681
682
683
684 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
685 {
686 #ifdef HAVE_GL
687         int texture_id = command->frame->get_texture_id();
688         BC_WindowBase *window = command->canvas->get_canvas();
689
690 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
691 // texture_id,
692 // command->canvas->get_canvas());
693
694
695
696
697 // If virtual console is being used, everything in this function has
698 // already been done except the page flip.
699         if(texture_id >= 0)
700         {
701                 canvas_w = window->get_w();
702                 canvas_h = window->get_h();
703                 VFrame::init_screen(canvas_w, canvas_h);
704                 int color_model = command->frame->get_color_model();
705                 int is_yuv = BC_CModels::is_yuv(color_model);
706
707                 if(!command->is_cleared)
708                 {
709 // If we get here, the virtual console was not used.
710                         color_frame(command, 0,0,0,0);
711                 }
712
713 // Texture
714 // Undo any previous shader settings
715                 command->frame->bind_texture(0);
716
717 // Convert colormodel
718                 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
719                 if( shader > 0 ) {
720                         glUseProgram(shader);
721 // Set texture unit of the texture
722                         int variable = glGetUniformLocation(shader, "tex");
723                         glUniform1i(variable, 0);
724                         BC_GL_YUV_TO_RGB(shader);
725                 }
726
727 //              if(BC_CModels::components(color_model) == 4)
728 //              {
729 //                      glEnable(GL_BLEND);
730 //                      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
731 //              }
732
733                 command->frame->draw_texture(
734                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
735                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
736                         flip_y);
737
738
739 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
740 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
741 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
742
743                 glUseProgram(0);
744
745                 command->canvas->get_canvas()->flip_opengl();
746
747         }
748 #endif
749 }
750
751
752 void Playback3D::color_frame(Playback3DCommand *command,
753                 float r, float g, float b, float a)
754 {
755 #ifdef HAVE_GL
756         glClearColor(r, g, b, a);
757         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
758 #endif
759 }
760
761
762 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
763 {
764         Playback3DCommand command;
765         command.command = Playback3DCommand::CLEAR_OUTPUT;
766         command.canvas = canvas;
767         command.frame = output;
768         send_command(&command);
769 }
770
771 void Playback3D::clear_output_sync(Playback3DCommand *command)
772 {
773 #ifdef HAVE_GL
774         BC_WindowBase *window =
775                 command->canvas->lock_canvas("Playback3D::clear_output_sync");
776         if( window ) {
777 // If we get here, the virtual console is being used.
778                 command->canvas->get_canvas()->enable_opengl();
779                 int is_yuv = 0;
780                 int color = BLACK, alpha = 0;
781 // Using pbuffer for refresh frame.
782                 if( command->frame ) {
783                         command->frame->enable_opengl();
784                         color = command->frame->get_clear_color();
785                         alpha = command->frame->get_clear_alpha();
786                         int color_model = command->canvas->mwindow->edl->session->color_model;
787                         is_yuv = BC_CModels::is_yuv(color_model);
788                 }
789                 int a = alpha;
790                 int r = (color>>16) & 0xff;
791                 int g = (color>>8) & 0xff;
792                 int b = (color>>0) & 0xff;
793                 if( is_yuv ) YUV::yuv.rgb_to_yuv_8(r, g, b);
794                 color_frame(command, r/255.f, g/255.f, b/255.f, a/255.f);
795         }
796         command->canvas->unlock_canvas();
797 #endif
798 }
799
800
801 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
802 {
803         Playback3DCommand command;
804         command.command = Playback3DCommand::CLEAR_INPUT;
805         command.canvas = canvas;
806         command.frame = frame;
807         send_command(&command);
808 }
809
810 void Playback3D::clear_input_sync(Playback3DCommand *command)
811 {
812 #ifdef HAVE_GL
813         BC_WindowBase *window =
814                 command->canvas->lock_canvas("Playback3D::clear_input_sync");
815         if( window ) {
816                 command->canvas->get_canvas()->enable_opengl();
817                 command->frame->enable_opengl();
818                 command->frame->clear_pbuffer();
819                 command->frame->set_opengl_state(VFrame::SCREEN);
820         }
821         command->canvas->unlock_canvas();
822 #endif
823 }
824
825 void Playback3D::do_camera(Canvas *canvas,
826         VFrame *output,
827         VFrame *input,
828         float in_x1,
829         float in_y1,
830         float in_x2,
831         float in_y2,
832         float out_x1,
833         float out_y1,
834         float out_x2,
835         float out_y2)
836 {
837         Playback3DCommand command;
838         command.command = Playback3DCommand::DO_CAMERA;
839         command.canvas = canvas;
840         command.input = input;
841         command.frame = output;
842         command.in_x1 = in_x1;
843         command.in_y1 = in_y1;
844         command.in_x2 = in_x2;
845         command.in_y2 = in_y2;
846         command.out_x1 = out_x1;
847         command.out_y1 = out_y1;
848         command.out_x2 = out_x2;
849         command.out_y2 = out_y2;
850         send_command(&command);
851 }
852
853 void Playback3D::do_camera_sync(Playback3DCommand *command)
854 {
855 #ifdef HAVE_GL
856         BC_WindowBase *window =
857                 command->canvas->lock_canvas("Playback3D::do_camera_sync");
858         if( window ) {
859                 command->canvas->get_canvas()->enable_opengl();
860
861                 command->input->to_texture();
862                 command->frame->enable_opengl();
863                 command->frame->init_screen();
864                 command->frame->clear_pbuffer();
865
866                 command->input->bind_texture(0);
867 // Must call draw_texture in input frame to get the texture coordinates right.
868
869 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
870 // command->in_x1,
871 // command->in_y2,
872 // command->in_x2,
873 // command->in_y1,
874 // command->out_x1,
875 // (float)command->input->get_h() - command->out_y1,
876 // command->out_x2,
877 // (float)command->input->get_h() - command->out_y2);
878                 command->input->draw_texture(
879                         command->in_x1, command->in_y2,
880                         command->in_x2, command->in_y1,
881                         command->out_x1,
882                         (float)command->frame->get_h() - command->out_y1,
883                         command->out_x2,
884                         (float)command->frame->get_h() - command->out_y2);
885
886
887                 command->frame->set_opengl_state(VFrame::SCREEN);
888                 command->frame->screen_to_ram();
889         }
890         command->canvas->unlock_canvas();
891 #endif
892 }
893
894 void Playback3D::overlay(Canvas *canvas, VFrame *input,
895         float in_x1, float in_y1, float in_x2, float in_y2,
896         float out_x1, float out_y1, float out_x2, float out_y2,
897         float alpha, int mode, int interpolation_type,
898         VFrame *output, int is_nested)
899 {
900         Playback3DCommand command;
901         command.command = Playback3DCommand::OVERLAY;
902         command.canvas = canvas;
903         command.frame = output;
904         command.input = input;
905         command.in_x1 = in_x1;
906         command.in_y1 = in_y1;
907         command.in_x2 = in_x2;
908         command.in_y2 = in_y2;
909         command.out_x1 = out_x1;
910         command.out_y1 = out_y1;
911         command.out_x2 = out_x2;
912         command.out_y2 = out_y2;
913         command.alpha = alpha;
914         command.mode = mode;
915         command.interpolation_type = interpolation_type;
916         command.is_nested = is_nested;
917         send_command(&command);
918 }
919
920 void Playback3D::overlay_sync(Playback3DCommand *command)
921 {
922 #ifdef HAVE_GL
923 // To do these operations, we need to copy the input buffer to a texture
924 // and blend 2 textures in a shader
925         static const char * const overlay_shaders[TRANSFER_TYPES] = {
926                 blend_NORMAL_frag,      // TRANSFER_NORMAL
927                 blend_ADDITION_frag,    // TRANSFER_ADDITION
928                 blend_SUBTRACT_frag,    // TRANSFER_SUBTRACT
929                 blend_MULTIPLY_frag,    // TRANSFER_MULTIPLY
930                 blend_DIVIDE_frag,      // TRANSFER_DIVIDE
931                 blend_REPLACE_frag,     // TRANSFER_REPLACE
932                 blend_MAX_frag,         // TRANSFER_MAX
933                 blend_MIN_frag,         // TRANSFER_MIN
934                 blend_DARKEN_frag,      // TRANSFER_DARKEN
935                 blend_LIGHTEN_frag,     // TRANSFER_LIGHTEN
936                 blend_DST_frag,         // TRANSFER_DST
937                 blend_DST_ATOP_frag,    // TRANSFER_DST_ATOP
938                 blend_DST_IN_frag,      // TRANSFER_DST_IN
939                 blend_DST_OUT_frag,     // TRANSFER_DST_OUT
940                 blend_DST_OVER_frag,    // TRANSFER_DST_OVER
941                 blend_SRC_frag,         // TRANSFER_SRC
942                 blend_SRC_ATOP_frag,    // TRANSFER_SRC_ATOP
943                 blend_SRC_IN_frag,      // TRANSFER_SRC_IN
944                 blend_SRC_OUT_frag,     // TRANSFER_SRC_OUT
945                 blend_SRC_OVER_frag,    // TRANSFER_SRC_OVER
946                 blend_AND_frag,         // TRANSFER_AND
947                 blend_OR_frag,          // TRANSFER_OR
948                 blend_XOR_frag,         // TRANSFER_XOR
949                 blend_OVERLAY_frag,     // TRANSFER_OVERLAY
950                 blend_SCREEN_frag,      // TRANSFER_SCREEN
951                 blend_BURN_frag,        // TRANSFER_BURN
952                 blend_DODGE_frag,       // TRANSFER_DODGE
953                 blend_HARDLIGHT_frag,   // TRANSFER_HARDLIGHT
954                 blend_SOFTLIGHT_frag,   // TRANSFER_SOFTLIGHT
955                 blend_DIFFERENCE_frag,  // TRANSFER_DIFFERENCE
956         };
957
958         BC_WindowBase *window =
959                 command->canvas->lock_canvas("Playback3D::overlay_sync");
960         if( window ) {
961 // Make sure OpenGL is enabled first.
962                 window->enable_opengl();
963                 window->update_video_cursor();
964
965                 glColor4f(1, 1, 1, 1);
966                 glDisable(GL_BLEND);
967
968
969 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
970                 switch( command->input->get_opengl_state() ) {
971 // Upload texture and composite to screen
972                 case VFrame::RAM:
973                         command->input->to_texture();
974                         break;
975 // Just composite texture to screen
976                 case VFrame::TEXTURE:
977                         break;
978 // read from PBuffer to texture, then composite texture to screen
979                 case VFrame::SCREEN:
980                         command->input->enable_opengl();
981                         command->input->screen_to_texture();
982                         break;
983                 default:
984                         printf("Playback3D::overlay_sync unknown state\n");
985                         break;
986                 }
987
988                 if(command->frame) {
989 // Render to PBuffer
990                         command->frame->enable_opengl();
991                         command->frame->set_opengl_state(VFrame::SCREEN);
992                         canvas_w = command->frame->get_w();
993                         canvas_h = command->frame->get_h();
994                 }
995                 else {
996 // Render to canvas
997                         window->enable_opengl();
998                         canvas_w = window->get_w();
999                         canvas_h = window->get_h();
1000                 }
1001
1002
1003                 const char *shader_stack[16];
1004                 memset(shader_stack,0, sizeof(shader_stack));
1005                 int total_shaders = 0, need_matrix = 0;
1006
1007                 VFrame::init_screen(canvas_w, canvas_h);
1008
1009 // Enable texture
1010                 command->input->bind_texture(0);
1011
1012 // Convert colormodel to RGB if not nested.
1013 // The color model setting in the output frame is ignored.
1014 //              if( command->is_nested <= 0 &&  // not nested
1015 //                  BC_CModels::is_yuv(command->input->get_color_model()) ) {
1016 //                      need_matrix = 1;
1017 //                      shader_stack[total_shaders++] = yuv_to_rgb_frag;
1018 //              }
1019
1020 // get the shaders
1021 #define add_shader(s) \
1022   if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1023   shader_stack[total_shaders++] = s
1024
1025                 switch(command->mode) {
1026                 case TRANSFER_REPLACE:
1027 // This requires overlaying an alpha multiplied image on a black screen.
1028                         if( command->input->get_texture_components() != 4 ) break;
1029                         add_shader(overlay_shaders[command->mode]);
1030                         break;
1031                 default:
1032                         enable_overlay_texture(command);
1033                         add_shader(overlay_shaders[command->mode]);
1034                         break;
1035                 }
1036
1037 // if to flatten alpha
1038 //              if( command->is_nested < 0 ) {
1039 //                      switch(command->input->get_color_model()) {
1040 //// yuv has already been converted to rgb
1041 //                      case BC_YUVA8888:
1042 //                      case BC_RGBA_FLOAT:
1043 //                      case BC_RGBA8888:
1044 //                              add_shader(rgba_to_rgb_flatten);
1045 //                              break;
1046 //                      }
1047 //              }
1048
1049 // run the shaders
1050                 add_shader(0);
1051                 unsigned int shader = !shader_stack[0] ? 0 :
1052                         VFrame::make_shader(shader_stack);
1053                 if( shader > 0 ) {
1054                         glUseProgram(shader);
1055                         if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1056 // Set texture unit of the texture
1057                         glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1058 // Set texture unit of the temp texture
1059                         glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1060 // Set alpha
1061                         int variable = glGetUniformLocation(shader, "alpha");
1062                         glUniform1f(variable, command->alpha);
1063 // Set dimensions of the temp texture
1064                         if(temp_texture)
1065                                 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1066                                         (float)temp_texture->get_texture_w(),
1067                                         (float)temp_texture->get_texture_h());
1068                 }
1069                 else
1070                         glUseProgram(0);
1071
1072
1073 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1074 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1075 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1076
1077                 command->input->draw_texture(
1078                         command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1079                         command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1080                         !command->is_nested);
1081                 glUseProgram(0);
1082
1083 // Delete temp texture
1084                 if(temp_texture) {
1085                         delete temp_texture;
1086                         temp_texture = 0;
1087                         glActiveTexture(GL_TEXTURE1);
1088                         glDisable(GL_TEXTURE_2D);
1089                 }
1090                 glActiveTexture(GL_TEXTURE0);
1091                 glDisable(GL_TEXTURE_2D);
1092         }
1093         command->canvas->unlock_canvas();
1094 #endif
1095 }
1096
1097 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1098 {
1099 #ifdef HAVE_GL
1100         glDisable(GL_BLEND);
1101
1102         glActiveTexture(GL_TEXTURE1);
1103         BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1104                 command->input->get_color_model());
1105         temp_texture->bind(1);
1106
1107 // Read canvas into texture
1108         glReadBuffer(GL_BACK);
1109         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1110 #endif
1111 }
1112
1113
1114 void Playback3D::do_mask(Canvas *canvas,
1115         VFrame *output,
1116         int64_t start_position_project,
1117         MaskAutos *keyframe_set,
1118         MaskAuto *keyframe,
1119         MaskAuto *default_auto)
1120 {
1121         Playback3DCommand command;
1122         command.command = Playback3DCommand::DO_MASK;
1123         command.canvas = canvas;
1124         command.frame = output;
1125         command.start_position_project = start_position_project;
1126         command.keyframe_set = keyframe_set;
1127         command.keyframe = keyframe;
1128         command.default_auto = default_auto;
1129
1130         send_command(&command);
1131 }
1132
1133
1134 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1135 {
1136         int x1 = iy1 < iy2 ? ix1 : ix2;
1137         int y1 = iy1 < iy2 ? iy1 : iy2;
1138         int x2 = iy1 < iy2 ? ix2 : ix1;
1139         int y2 = iy1 < iy2 ? iy2 : iy1;
1140
1141         int x = x1, y = y1;
1142         int dx = x2-x1, dy = y2-y1;
1143         int dx2 = 2*dx, dy2 = 2*dy;
1144         if( dx < 0 ) dx = -dx;
1145         int m = dx > dy ? dx : dy, n = m;
1146         if( dy >= dx ) {
1147                 if( dx2 >= 0 ) do {     /* +Y, +X */
1148                         spots.append(x, y++);
1149                         if( (m -= dx2) < 0 ) { m += dy2;  ++x; }
1150                 } while( --n >= 0 );
1151                 else do {              /* +Y, -X */
1152                         spots.append(x, y++);
1153                         if( (m += dx2) < 0 ) { m += dy2;  --x; }
1154                 } while( --n >= 0 );
1155         }
1156         else {
1157                 if( dx2 >= 0 ) do {     /* +X, +Y */
1158                         spots.append(x++, y);
1159                         if( (m -= dy2) < 0 ) { m += dx2;  ++y; }
1160                 } while( --n >= 0 );
1161                 else do {              /* -X, +Y */
1162                         spots.append(x--, y);
1163                         if( (m -= dy2) < 0 ) { m -= dx2;  ++y; }
1164                 } while( --n >= 0 );
1165         }
1166 }
1167
1168 #ifdef HAVE_GL
1169 class fb_texture : public BC_Texture
1170 {
1171 public:
1172         fb_texture(int w, int h, int colormodel);
1173         ~fb_texture();
1174         void bind(int texture_unit);
1175         void read_screen(int x, int y, int w, int h);
1176         void set_output_texture();
1177         GLuint fb, rb;
1178 };
1179
1180 fb_texture::fb_texture(int w, int h, int colormodel)
1181  : BC_Texture(w, h, colormodel)
1182 {
1183         fb = 0;  rb = 0;
1184 //      glGenRenderbuffers(1, &rb);
1185 //      glBindRenderbuffer(GL_RENDERBUFFER, rb);
1186 //      glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1187         glGenFramebuffers(1, &fb);
1188         glBindFramebuffer(GL_FRAMEBUFFER, fb);
1189 //      glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1190 }
1191
1192 fb_texture::~fb_texture()
1193 {
1194         glBindFramebuffer(GL_FRAMEBUFFER, 0);
1195         glDeleteFramebuffers(1, (GLuint *)&fb);
1196 //      glBindRenderbuffer(GL_RENDERBUFFER, 0);
1197 //      glGenRenderbuffers(1, &rb);
1198 }
1199
1200 void fb_texture::bind(int texture_unit)
1201 {
1202         glBindFramebuffer(GL_FRAMEBUFFER, fb);
1203 //      glBindRenderbuffer(GL_RENDERBUFFER, rb);
1204         BC_Texture::bind(texture_unit);
1205 }
1206
1207 void fb_texture::read_screen(int x, int y, int w, int h)
1208 {
1209         glBindFramebuffer(GL_FRAMEBUFFER, 0);
1210         glReadBuffer(GL_BACK);
1211         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1212 }
1213
1214 void fb_texture::set_output_texture()
1215 {
1216         glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, get_texture_id(), 0);
1217         GLenum dbo[1] = { GL_COLOR_ATTACHMENT0, }; // bind layout(location=0) out vec4 color;
1218         glDrawBuffers(1, dbo);
1219         int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1220         if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1221                 printf("glDrawBuffer error 0x%04x\n", ret);
1222                 return;
1223         }
1224 }
1225
1226 static void combineData(GLdouble coords[3],
1227                 GLdouble *vertex_data[4], GLfloat weight[4],
1228                 GLdouble **outData, void *data)
1229 {
1230         ArrayList<double *> *invented = (ArrayList<double *> *)data;
1231         GLdouble *vertex = new double[6];
1232         invented->append(vertex);
1233         vertex[0] = coords[0];
1234         vertex[1] = coords[1];
1235         vertex[2] = coords[2];
1236         for( int i=3; i<6; ++i ) {
1237                 GLdouble v = 0;
1238                 for( int k=0; k<4; ++k ) {
1239                         if( !weight[k] || !vertex_data[k] ) continue;
1240                         v += weight[k] * vertex_data[k][i];
1241                 }
1242                 vertex[i] = v;
1243         }
1244         *outData = vertex;
1245 }
1246
1247 // dbug
1248 static void zglBegin(GLenum mode) { glBegin(mode); }
1249 static void zglEnd() { glEnd(); }
1250 static void zglVertex3dv(const GLdouble *v) { glVertex3dv(v); }
1251
1252 #endif
1253
1254 void Playback3D::do_mask_sync(Playback3DCommand *command)
1255 {
1256 #ifdef HAVE_GL
1257         BC_WindowBase *window =
1258                 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1259         if( window ) {
1260                 window->enable_opengl();
1261
1262                 switch( command->frame->get_opengl_state() ) {
1263                 case VFrame::RAM:
1264 // upload frame to the texture
1265                         command->frame->to_texture();
1266                         break;
1267
1268                 case VFrame::SCREEN:
1269 // Read back from PBuffer
1270 // Bind context to pbuffer
1271                         command->frame->enable_opengl();
1272                         command->frame->screen_to_texture();
1273                         break;
1274                 }
1275
1276 // Initialize coordinate system
1277                 command->frame->enable_opengl();
1278                 command->frame->init_screen();
1279                 int color_model = command->frame->get_color_model();
1280                 int w = command->frame->get_w();
1281                 int h = command->frame->get_h();
1282                 MaskEdges edges;
1283                 float faders[SUBMASKS], feathers[SUBMASKS], bg = 1;
1284                 MaskPoints point_set[SUBMASKS];
1285 // Draw every submask as a new polygon
1286                 int total_submasks = command->keyframe_set->total_submasks(
1287                         command->start_position_project, PLAY_FORWARD);
1288
1289                 for(int k = 0; k < total_submasks; k++) {
1290                         MaskPoints &points = point_set[k];
1291                         command->keyframe_set->get_points(&points,
1292                                 k, command->start_position_project, PLAY_FORWARD);
1293                         float fader = command->keyframe_set->get_fader(
1294                                 command->start_position_project, k, PLAY_FORWARD);
1295                         float v = fader/100.;
1296                         faders[k] = v;
1297                         if( v < 0 && (v+=1) < bg ) bg = v;
1298                         float feather = command->keyframe_set->get_feather(
1299                                 command->start_position_project, k, PLAY_FORWARD);
1300                         feathers[k] = feather;
1301                 }
1302 // clear screen
1303                 glDisable(GL_TEXTURE_2D);
1304                 glClearColor(bg, bg, bg, bg);
1305                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1306
1307                 int show_mask = command->keyframe_set->track->masks;
1308                 for(int k = 0; k < total_submasks; k++) {
1309                         MaskEdge &edge = *edges.append(new MaskEdge());
1310                         if( !((show_mask>>k) & 1) ) continue;
1311                         edge.load(point_set[k], h);
1312                         if( edge.size() > 0 ) {
1313 // draw polygon
1314                                 float fader = faders[k];
1315                                 float v = fader < 0 ? 1 : 1-fader;
1316                                 glColor4f(v, v, v, v);
1317                                 int display_list = glGenLists(1);
1318                                 glNewList(display_list, GL_COMPILE);
1319 #if 0
1320                                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1321                                 glBegin(GL_POLYGON);
1322                                 MaskCoord *c = &edge[0];
1323                                 for( int i=edge.size(); --i>=0; ++c )
1324                                         glVertex2f(c->x, c->y);
1325                                 glEnd();
1326 #else
1327                                 GLUtesselator *tess = gluNewTess();
1328                                 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex3dv);
1329                                 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1330                                 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1331                                 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1332                                 ArrayList<double *> invented;
1333                                 invented.set_array_delete();
1334
1335                                 gluTessBeginPolygon(tess, &invented);
1336                                 gluTessBeginContour(tess);
1337                                 MaskCoord *c = &edge[0];
1338                                 for( int i=edge.size(); --i>=0; ++c )
1339                                         gluTessVertex(tess, (GLdouble *)c, c);
1340                                 gluTessEndContour(tess);
1341                                 gluTessEndPolygon(tess);
1342                                 gluDeleteTess(tess);
1343                                 invented.remove_all_objects();
1344 #endif
1345                                 glEndList();
1346                                 glCallList(display_list);
1347                                 glDeleteLists(1, display_list);
1348                         }
1349                 }
1350
1351 // in/out textures
1352                 fb_texture *in = new fb_texture(w, h, color_model);
1353                 in->bind(0);
1354                 in->read_screen(0,0, w,h);
1355                 fb_texture *out = new fb_texture(w, h, color_model);
1356
1357                 unsigned int frag_shader =
1358                         VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1359                 if( frag_shader > 0 ) {
1360                         GLuint points[1];
1361                         glGenBuffers(1, points);
1362                         for(int k = 0; k < total_submasks; k++) {
1363                                 MaskEdge &edge = *edges[k];
1364                                 if( !edge.size() ) continue;
1365                                 if( !faders[k] ) continue;
1366                                 if( !feathers[k] ) continue;
1367                                 MaskSpots spots;
1368                                 for( int i=0; i<edge.size(); ++i ) {
1369                                         MaskCoord &a = edge[i];
1370                                         MaskCoord &b = i<edge.size()-1 ? edge[i+1] : edge[0];
1371                                         draw_spots(spots, a.x,a.y+h, b.x,b.y+h);
1372                                 }
1373                                 int sz = spots.size() * sizeof(MaskSpot);
1374                                 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, points[0], 0, sz);
1375                                 glBufferData(GL_SHADER_STORAGE_BUFFER, sz, &spots[0], GL_DYNAMIC_COPY);
1376                                 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1377                                 glUseProgram(frag_shader);
1378                                 float r = feathers[k], v = faders[k];
1379                                 glUniform1f(glGetUniformLocation(frag_shader, "r"), r);
1380                                 glUniform1f(glGetUniformLocation(frag_shader, "v"), v);
1381                                 in->bind(0);
1382                                 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1383                                 out->set_output_texture();
1384                                 glViewport(0,0, w,h);
1385                                 out->draw_texture(0,0, w,h, 0,0, w,h);
1386                                 glUseProgram(0);
1387                                 fb_texture *t = in;  in = out;  out = t;
1388                         }
1389                         glDeleteBuffers(1, points);
1390                 }
1391
1392                 glDrawBuffers(0, 0);
1393                 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1394
1395                 const char *alpha_shader = BC_CModels::has_alpha(color_model) ?
1396                                 multiply_mask4_frag :
1397                         !BC_CModels::is_yuv(color_model) ?
1398                                 multiply_mask3_frag :
1399                                 multiply_yuvmask3_frag;
1400                 unsigned int shader = VFrame::make_shader(0, alpha_shader, 0);
1401                 glUseProgram(shader);
1402                 if( shader > 0 ) {
1403                         command->frame->bind_texture(0);
1404                         in->BC_Texture::bind(1);
1405                         glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1406                         glUniform1i(glGetUniformLocation(shader, "tex1"), 1);
1407                 }
1408                 command->frame->draw_texture();
1409                 command->frame->set_opengl_state(VFrame::SCREEN);
1410                 glUseProgram(0);
1411                 delete in;
1412                 delete out;
1413 // Default drawable
1414                 glDisable(GL_TEXTURE_2D);
1415                 glColor4f(1, 1, 1, 1);
1416                 glActiveTexture(GL_TEXTURE0);
1417                 window->enable_opengl();
1418         }
1419         command->canvas->unlock_canvas();
1420 #endif
1421 }
1422
1423
1424 void Playback3D::convert_cmodel(Canvas *canvas,
1425         VFrame *output,
1426         int dst_cmodel)
1427 {
1428 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1429         int src_cmodel = output->get_color_model();
1430         if(
1431                 (output->get_opengl_state() == VFrame::TEXTURE ||
1432                 output->get_opengl_state() == VFrame::SCREEN) &&
1433 // OpenGL has no floating point.
1434                 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1435                   (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1436                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1437                   (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1438 // OpenGL sets alpha to 1 on import
1439                   (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1440                   (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1441                   (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1442                 ) return;
1443
1444
1445
1446         Playback3DCommand command;
1447         command.command = Playback3DCommand::CONVERT_CMODEL;
1448         command.canvas = canvas;
1449         command.frame = output;
1450         command.dst_cmodel = dst_cmodel;
1451         send_command(&command);
1452 }
1453
1454 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1455 {
1456 #ifdef HAVE_GL
1457         BC_WindowBase *window =
1458                 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1459         if( window ) {
1460                 window->enable_opengl();
1461
1462 // Import into hardware
1463                 command->frame->enable_opengl();
1464                 command->frame->init_screen();
1465                 command->frame->to_texture();
1466
1467 // Colormodel permutation
1468                 int src_cmodel = command->frame->get_color_model();
1469                 int dst_cmodel = command->dst_cmodel;
1470                 typedef struct {
1471                         int src, dst, typ;
1472                         const char *shader;
1473                 } cmodel_shader_table_t;
1474                 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1475                 int type = -1;
1476                 static cmodel_shader_table_t cmodel_shader_table[]  = {
1477                         { BC_RGB888,    BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1478                         { BC_RGB888,    BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1479                         { BC_RGBA8888,  BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1480                         { BC_RGBA8888,  BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1481                         { BC_RGBA8888,  BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1482                         { BC_RGBA8888,  BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1483                         { BC_RGB_FLOAT, BC_YUV888,      rgb_to_yuv, rgb_to_yuv_frag  },
1484                         { BC_RGB_FLOAT, BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1485                         { BC_RGBA_FLOAT,BC_RGB888,      rgb_to_rgb, rgba_to_rgb_frag },
1486                         { BC_RGBA_FLOAT,BC_RGB_FLOAT,   rgb_to_rgb, rgba_to_rgb_frag },
1487                         { BC_RGBA_FLOAT,BC_YUV888,      rgb_to_yuv, rgba_to_yuv_frag },
1488                         { BC_RGBA_FLOAT,BC_YUVA8888,    rgb_to_yuv, rgb_to_yuv_frag  },
1489                         { BC_YUV888,    BC_RGB888,      yuv_to_rgb, yuv_to_rgb_frag  },
1490                         { BC_YUV888,    BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1491                         { BC_YUV888,    BC_RGB_FLOAT,   yuv_to_rgb, yuv_to_rgb_frag  },
1492                         { BC_YUV888,    BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1493                         { BC_YUVA8888,  BC_RGB888,      yuv_to_rgb, yuva_to_rgb_frag },
1494                         { BC_YUVA8888,  BC_RGBA8888,    yuv_to_rgb, yuv_to_rgb_frag  },
1495                         { BC_YUVA8888,  BC_RGB_FLOAT,   yuv_to_rgb, yuva_to_rgb_frag },
1496                         { BC_YUVA8888,  BC_RGBA_FLOAT,  yuv_to_rgb, yuv_to_rgb_frag  },
1497                         { BC_YUVA8888,  BC_YUV888,      yuv_to_yuv, yuva_to_yuv_frag },
1498                 };
1499
1500                 const char *shader = 0;
1501                 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1502                 for( int i=0; i<table_size; ++i ) {
1503                         if( cmodel_shader_table[i].src == src_cmodel &&
1504                             cmodel_shader_table[i].dst == dst_cmodel ) {
1505                                 shader = cmodel_shader_table[i].shader;
1506                                 type = cmodel_shader_table[i].typ;
1507                                 break;
1508                         }
1509                 }
1510
1511 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1512 // __LINE__,
1513 // command->frame->get_color_model(),
1514 // command->dst_cmodel,
1515 // shader);
1516
1517                 const char *shader_stack[9];
1518                 memset(shader_stack,0, sizeof(shader_stack));
1519                 int current_shader = 0;
1520
1521                 if( shader ) {
1522 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1523                         shader_stack[current_shader++] = shader;
1524                         shader_stack[current_shader] = 0;
1525                         unsigned int shader_id = VFrame::make_shader(shader_stack);
1526
1527                         command->frame->bind_texture(0);
1528                         glUseProgram(shader_id);
1529
1530                         glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1531                         switch( type ) {
1532                         case rgb_to_yuv:
1533                                 BC_GL_RGB_TO_YUV(shader_id);
1534                                 break;
1535                         case yuv_to_rgb:
1536                                 BC_GL_YUV_TO_RGB(shader_id);
1537                                 break;
1538                         }
1539
1540                         command->frame->draw_texture();
1541                         if(shader) glUseProgram(0);
1542                         command->frame->set_opengl_state(VFrame::SCREEN);
1543                 }
1544         }
1545
1546         command->canvas->unlock_canvas();
1547 #endif // HAVE_GL
1548 }
1549
1550 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1551 {
1552         Playback3DCommand command;
1553         command.command = Playback3DCommand::DO_FADE;
1554         command.canvas = canvas;
1555         command.frame = frame;
1556         command.alpha = fade;
1557         send_command(&command);
1558 }
1559
1560 void Playback3D::do_fade_sync(Playback3DCommand *command)
1561 {
1562 #ifdef HAVE_GL
1563         BC_WindowBase *window =
1564                 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1565         if( window ) {
1566                 window->enable_opengl();
1567                 switch( command->frame->get_opengl_state() ) {
1568                 case VFrame::RAM:
1569                         command->frame->to_texture();
1570                         break;
1571
1572                 case VFrame::SCREEN:
1573 // Read back from PBuffer
1574 // Bind context to pbuffer
1575                         command->frame->enable_opengl();
1576                         command->frame->screen_to_texture();
1577                         break;
1578                 }
1579
1580                 command->frame->enable_opengl();
1581                 command->frame->init_screen();
1582                 command->frame->bind_texture(0);
1583
1584 //              glClearColor(0.0, 0.0, 0.0, 0.0);
1585 //              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1586                 glDisable(GL_BLEND);
1587                 unsigned int frag_shader = 0;
1588                 switch(command->frame->get_color_model())
1589                 {
1590 // For the alpha colormodels, the native function seems to multiply the
1591 // components by the alpha instead of just the alpha.
1592                         case BC_RGBA8888:
1593                         case BC_RGBA_FLOAT:
1594                         case BC_YUVA8888:
1595                                 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1596                                 break;
1597
1598                         case BC_RGB888:
1599                                 glEnable(GL_BLEND);
1600                                 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1601                                 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1602                                 break;
1603
1604
1605                         case BC_YUV888:
1606                                 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1607                                 break;
1608                 }
1609
1610
1611                 if( frag_shader ) {
1612                         glUseProgram(frag_shader);
1613                         int variable;
1614                         if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1615                                 glUniform1i(variable, 0);
1616                         if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1617                                 glUniform1f(variable, command->alpha);
1618                 }
1619
1620                 command->frame->draw_texture();
1621                 command->frame->set_opengl_state(VFrame::SCREEN);
1622
1623                 if(frag_shader)
1624                 {
1625                         glUseProgram(0);
1626                 }
1627
1628                 glColor4f(1, 1, 1, 1);
1629                 glDisable(GL_BLEND);
1630         }
1631         command->canvas->unlock_canvas();
1632 #endif
1633 }
1634
1635
1636 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1637 {
1638         Playback3DCommand command;
1639         command.command = Playback3DCommand::PLUGIN;
1640         command.canvas = canvas;
1641         command.plugin_client = client;
1642         return send_command(&command);
1643 }
1644
1645 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1646 {
1647         BC_WindowBase *window =
1648                 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1649         if( window ) {
1650                 window->enable_opengl();
1651                 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1652         }
1653         command->canvas->unlock_canvas();
1654 }
1655
1656