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