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