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