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