4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #define GL_GLEXT_PROTOTYPES
25 #include "bcsignals.h"
26 #include "bcwindowbase.h"
29 #include "condition.h"
31 #include "maskautos.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"
42 #include "transportque.inc"
60 // These should be passed to VFrame::make_shader to construct shaders.
61 // Can't hard code sampler2D
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"
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"
76 static const char *yuva_to_yuv_frag =
77 "uniform sampler2D tex;\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"
84 " yuva.g = yuva.g * a + 0.5 * anti_a;\n"
85 " yuva.b = yuva.b * a + 0.5 * anti_a;\n"
87 " gl_FragColor = yuva;\n"
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"
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"
101 " gl_FragColor = yuva;\n"
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"
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"
117 static const char *rgba_to_rgb_frag =
118 "uniform sampler2D tex;\n"
121 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
122 " rgba.rgb *= rgba.a;\n"
124 " gl_FragColor = rgba;\n"
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"
133 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
134 " rgba.rgb *= rgba.a;\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"
141 //static const char *rgba_to_rgb_flatten =
143 // " gl_FragColor.rgb *= gl_FragColor.a;\n"
144 // " gl_FragColor.a = 1.0;\n"
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" \
153 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\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" \
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" \
166 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\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" \
191 static const char *blend_NORMAL_frag =
192 "uniform sampler2D tex2;\n"
193 "uniform vec2 tex2_dimensions;\n"
194 "uniform float alpha;\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"
202 static const char *blend_REPLACE_frag =
203 "uniform float alpha;\n"
208 static const char *blend_ADDITION_frag =
209 "uniform sampler2D tex2;\n"
210 "uniform vec2 tex2_dimensions;\n"
211 "uniform float alpha;\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"
219 static const char *blend_SUBTRACT_frag =
220 "uniform sampler2D tex2;\n"
221 "uniform vec2 tex2_dimensions;\n"
222 "uniform float alpha;\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"
229 GL_STD_BLEND(MULTIPLY);
230 GL_VEC_BLEND(DIVIDE);
233 GL_VEC_BLEND(DARKEN);
234 GL_VEC_BLEND(LIGHTEN);
236 GL_STD_BLEND(DST_ATOP);
237 GL_STD_BLEND(DST_IN);
238 GL_STD_BLEND(DST_OUT);
239 GL_STD_BLEND(DST_OVER);
241 GL_STD_BLEND(SRC_ATOP);
242 GL_STD_BLEND(SRC_IN);
243 GL_STD_BLEND(SRC_OUT);
244 GL_STD_BLEND(SRC_OVER);
248 GL_VEC_BLEND(OVERLAY);
249 GL_STD_BLEND(SCREEN);
252 GL_VEC_BLEND(HARDLIGHT);
253 GL_VEC_BLEND(SOFTLIGHT);
254 GL_VEC_BLEND(DIFFERENCE);
256 static const char *read_texture_frag =
257 "uniform sampler2D tex;\n"
260 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
263 static const char *in_vertex_frag =
264 "#version 430 // vertex shader\n"
267 " gl_Position = vec4(in_pos-vec3(0.5,0.5,0.), .5);\n"
270 static const char *feather_frag =
272 "layout(location=0) out vec4 color;\n"
273 "uniform sampler2D tex;\n"
274 // apparently, only doubles index properly in shared buffers
275 "buffer buf { dvec2 points[]; };\n"
279 " vec2 tex_st = gl_FragCoord.xy/textureSize(tex,0);\n"
280 " color = texture(tex, tex_st);\n"
281 " if( r==0. ) return;\n"
282 " float rv = r*v>0. ? 1 : -1;\n"
283 " float rr = r*r, dr = 1./rr;\n"
284 " float vv = v>=0 ? 1.-v : 1.+v;\n"
285 " float fg = rv>=0 ? vv : 1.;\n"
286 " float bg = rv>=0 ? 1. : vv;\n"
287 " int len = points.length();\n"
288 " for( int i=0; i<len; ++i ) {\n"
289 " float dx = float(points[i].x) - gl_FragCoord.x;\n"
290 " float dy = float(points[i].y) - gl_FragCoord.y;\n"
291 " float dd = dx*dx + dy*dy;\n"
292 " if( dd >= rr ) continue;\n"
293 " float d = dd*dr;\n"
294 " float a = (1.-d)*fg + d*bg;\n"
295 " if( rv*(color.a-a) > 0 ) color = vec4(a);\n"
299 static const char *multiply_mask4_frag =
300 "uniform sampler2D tex;\n"
301 "uniform sampler2D tex1;\n"
304 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
305 " gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st).r;\n"
308 static const char *multiply_mask3_frag =
309 "uniform sampler2D tex;\n"
310 "uniform sampler2D tex1;\n"
313 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
314 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
315 " gl_FragColor.rgb *= vec3(a, a, a);\n"
318 static const char *multiply_yuvmask3_frag =
319 "uniform sampler2D tex;\n"
320 "uniform sampler2D tex1;\n"
323 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
324 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
325 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
326 " gl_FragColor.rgb *= vec3(a, a, a);\n"
327 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
330 static const char *fade_rgba_frag =
331 "uniform sampler2D tex;\n"
332 "uniform float alpha;\n"
335 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
336 " gl_FragColor.a *= alpha;\n"
339 static const char *fade_yuv_frag =
340 "uniform sampler2D tex;\n"
341 "uniform float alpha;\n"
344 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
345 " gl_FragColor.r *= alpha;\n"
346 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
347 " gl_FragColor.g *= alpha;\n"
348 " gl_FragColor.b *= alpha;\n"
349 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
355 Playback3DCommand::Playback3DCommand()
356 : BC_SynchronousCommand()
362 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
364 Playback3DCommand *ptr = (Playback3DCommand*)command;
365 this->canvas = ptr->canvas;
366 this->is_cleared = ptr->is_cleared;
368 this->in_x1 = ptr->in_x1;
369 this->in_y1 = ptr->in_y1;
370 this->in_x2 = ptr->in_x2;
371 this->in_y2 = ptr->in_y2;
372 this->out_x1 = ptr->out_x1;
373 this->out_y1 = ptr->out_y1;
374 this->out_x2 = ptr->out_x2;
375 this->out_y2 = ptr->out_y2;
376 this->alpha = ptr->alpha;
377 this->mode = ptr->mode;
378 this->interpolation_type = ptr->interpolation_type;
380 this->input = ptr->input;
381 this->start_position_project = ptr->start_position_project;
382 this->keyframe_set = ptr->keyframe_set;
383 this->keyframe = ptr->keyframe;
384 this->default_auto = ptr->default_auto;
385 this->plugin_client = ptr->plugin_client;
386 this->want_texture = ptr->want_texture;
387 this->is_nested = ptr->is_nested;
388 this->dst_cmodel = ptr->dst_cmodel;
390 BC_SynchronousCommand::copy_from(command);
393 Playback3D::Playback3D(MWindow *mwindow)
396 this->mwindow = mwindow;
400 Playback3D::~Playback3D()
407 BC_SynchronousCommand* Playback3D::new_command()
409 return new Playback3DCommand;
414 void Playback3D::handle_command(BC_SynchronousCommand *command)
416 //printf("Playback3D::handle_command 1 %d\n", command->command);
417 switch(command->command)
419 case Playback3DCommand::WRITE_BUFFER:
420 write_buffer_sync((Playback3DCommand*)command);
423 case Playback3DCommand::CLEAR_OUTPUT:
424 clear_output_sync((Playback3DCommand*)command);
427 case Playback3DCommand::CLEAR_INPUT:
428 clear_input_sync((Playback3DCommand*)command);
431 case Playback3DCommand::DO_CAMERA:
432 do_camera_sync((Playback3DCommand*)command);
435 case Playback3DCommand::OVERLAY:
436 overlay_sync((Playback3DCommand*)command);
439 case Playback3DCommand::DO_FADE:
440 do_fade_sync((Playback3DCommand*)command);
443 case Playback3DCommand::DO_MASK:
444 do_mask_sync((Playback3DCommand*)command);
447 case Playback3DCommand::PLUGIN:
448 run_plugin_sync((Playback3DCommand*)command);
451 case Playback3DCommand::COPY_FROM:
452 copy_from_sync((Playback3DCommand*)command);
455 case Playback3DCommand::CONVERT_CMODEL:
456 convert_cmodel_sync((Playback3DCommand*)command);
459 // case Playback3DCommand::DRAW_REFRESH:
460 // draw_refresh_sync((Playback3DCommand*)command);
463 //printf("Playback3D::handle_command 10\n");
469 void Playback3D::copy_from(Canvas *canvas,
474 Playback3DCommand command;
475 command.command = Playback3DCommand::COPY_FROM;
476 command.canvas = canvas;
479 command.want_texture = want_texture;
480 send_command(&command);
483 void Playback3D::copy_from_sync(Playback3DCommand *command)
486 BC_WindowBase *window =
487 command->canvas->lock_canvas("Playback3D::copy_from_sync");
489 window->enable_opengl();
491 int w = command->input->get_w();
492 int h = command->input->get_h();
494 if(command->input->get_opengl_state() == VFrame::SCREEN &&
495 w == command->frame->get_w() && h == command->frame->get_h())
497 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
498 // command->input->get_w(),
499 // command->input->get_h(),
500 // command->frame->get_w(),
501 // command->frame->get_h(),
502 // command->frame->get_color_model());
503 // With NVidia at least,
506 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
510 if(command->want_texture)
512 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
513 // Screen_to_texture requires the source pbuffer enabled.
514 command->input->enable_opengl();
515 command->frame->screen_to_texture();
516 command->frame->set_opengl_state(VFrame::TEXTURE);
521 command->input->to_texture();
522 command->input->bind_texture(0);
523 command->frame->enable_opengl();
524 command->frame->init_screen();
525 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
526 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
528 glUseProgram(shader);
529 int variable = glGetUniformLocation(shader, "tex");
530 glUniform1i(variable, 0);
531 BC_GL_YUV_TO_RGB(shader);
535 command->input->draw_texture(1);
536 command->frame->screen_to_ram();
542 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
543 command->input->get_opengl_state(), w, h,
544 command->frame->get_w(), command->frame->get_h());
547 command->canvas->unlock_canvas();
554 // void Playback3D::draw_refresh(Canvas *canvas,
565 // Playback3DCommand command;
566 // command.command = Playback3DCommand::DRAW_REFRESH;
567 // command.canvas = canvas;
568 // command.frame = frame;
569 // command.in_x1 = in_x1;
570 // command.in_y1 = in_y1;
571 // command.in_x2 = in_x2;
572 // command.in_y2 = in_y2;
573 // command.out_x1 = out_x1;
574 // command.out_y1 = out_y1;
575 // command.out_x2 = out_x2;
576 // command.out_y2 = out_y2;
577 // send_command(&command);
580 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
583 // BC_WindowBase *window =
584 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
586 // window->enable_opengl();
588 // // Read output pbuffer back to RAM in project colormodel
589 // // RGB 8bit is fastest for OpenGL to read back.
590 // command->frame->reallocate(0,
594 // command->frame->get_w(),
595 // command->frame->get_h(),
598 // command->frame->screen_to_ram();
600 // window->clear_box(0,
604 // window->draw_vframe(command->frame,
605 // (int)command->out_x1,
606 // (int)command->out_y1,
607 // (int)(command->out_x2 - command->out_x1),
608 // (int)(command->out_y2 - command->out_y1),
609 // (int)command->in_x1,
610 // (int)command->in_y1,
611 // (int)(command->in_x2 - command->in_x1),
612 // (int)(command->in_y2 - command->in_y1),
616 // command->canvas->unlock_canvas();
624 void Playback3D::write_buffer(Canvas *canvas,
636 Playback3DCommand command;
637 command.command = Playback3DCommand::WRITE_BUFFER;
638 command.canvas = canvas;
639 command.frame = frame;
640 command.in_x1 = in_x1;
641 command.in_y1 = in_y1;
642 command.in_x2 = in_x2;
643 command.in_y2 = in_y2;
644 command.out_x1 = out_x1;
645 command.out_y1 = out_y1;
646 command.out_x2 = out_x2;
647 command.out_y2 = out_y2;
648 command.is_cleared = is_cleared;
649 send_command(&command);
653 void Playback3D::write_buffer_sync(Playback3DCommand *command)
656 BC_WindowBase *window =
657 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
659 window->enable_opengl();
660 // Update hidden cursor
661 window->update_video_cursor();
662 command->frame->enable_opengl();
663 command->frame->init_screen();
664 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
665 int frame_state = command->frame->get_opengl_state();
666 if( frame_state != VFrame::TEXTURE )
667 command->frame->to_texture();
668 if( frame_state != VFrame::RAM ) {
669 command->in_y1 = command->frame->get_h() - command->in_y1;
670 command->in_y2 = command->frame->get_h() - command->in_y2;
672 window->enable_opengl();
673 draw_output(command, 1);
674 command->frame->set_opengl_state(frame_state);
676 command->canvas->unlock_canvas();
682 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
685 int texture_id = command->frame->get_texture_id();
686 BC_WindowBase *window = command->canvas->get_canvas();
688 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
690 // command->canvas->get_canvas());
695 // If virtual console is being used, everything in this function has
696 // already been done except the page flip.
699 canvas_w = window->get_w();
700 canvas_h = window->get_h();
701 VFrame::init_screen(canvas_w, canvas_h);
702 int color_model = command->frame->get_color_model();
703 int is_yuv = BC_CModels::is_yuv(color_model);
705 if(!command->is_cleared)
707 // If we get here, the virtual console was not used.
708 init_frame(command, 0);
712 // Undo any previous shader settings
713 command->frame->bind_texture(0);
715 // Convert colormodel
716 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
718 glUseProgram(shader);
719 // Set texture unit of the texture
720 int variable = glGetUniformLocation(shader, "tex");
721 glUniform1i(variable, 0);
722 BC_GL_YUV_TO_RGB(shader);
725 // if(BC_CModels::components(color_model) == 4)
727 // glEnable(GL_BLEND);
728 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
731 command->frame->draw_texture(
732 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
733 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
737 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
738 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
739 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
743 command->canvas->get_canvas()->flip_opengl();
750 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
753 float gbuv = is_yuv ? 0.5 : 0.0;
754 glClearColor(0.0, gbuv, gbuv, 0.0);
755 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
760 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
762 Playback3DCommand command;
763 command.command = Playback3DCommand::CLEAR_OUTPUT;
764 command.canvas = canvas;
765 command.frame = output;
766 send_command(&command);
769 void Playback3D::clear_output_sync(Playback3DCommand *command)
772 BC_WindowBase *window =
773 command->canvas->lock_canvas("Playback3D::clear_output_sync");
775 // If we get here, the virtual console is being used.
776 command->canvas->get_canvas()->enable_opengl();
778 // Using pbuffer for refresh frame.
779 if( command->frame ) {
780 command->frame->enable_opengl();
781 int color_model = command->canvas->mwindow->edl->session->color_model;
782 is_yuv = BC_CModels::is_yuv(color_model);
785 init_frame(command, is_yuv);
787 command->canvas->unlock_canvas();
792 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
794 Playback3DCommand command;
795 command.command = Playback3DCommand::CLEAR_INPUT;
796 command.canvas = canvas;
797 command.frame = frame;
798 send_command(&command);
801 void Playback3D::clear_input_sync(Playback3DCommand *command)
804 BC_WindowBase *window =
805 command->canvas->lock_canvas("Playback3D::clear_input_sync");
807 command->canvas->get_canvas()->enable_opengl();
808 command->frame->enable_opengl();
809 command->frame->clear_pbuffer();
810 command->frame->set_opengl_state(VFrame::SCREEN);
812 command->canvas->unlock_canvas();
816 void Playback3D::do_camera(Canvas *canvas,
828 Playback3DCommand command;
829 command.command = Playback3DCommand::DO_CAMERA;
830 command.canvas = canvas;
831 command.input = input;
832 command.frame = output;
833 command.in_x1 = in_x1;
834 command.in_y1 = in_y1;
835 command.in_x2 = in_x2;
836 command.in_y2 = in_y2;
837 command.out_x1 = out_x1;
838 command.out_y1 = out_y1;
839 command.out_x2 = out_x2;
840 command.out_y2 = out_y2;
841 send_command(&command);
844 void Playback3D::do_camera_sync(Playback3DCommand *command)
847 BC_WindowBase *window =
848 command->canvas->lock_canvas("Playback3D::do_camera_sync");
850 command->canvas->get_canvas()->enable_opengl();
852 command->input->to_texture();
853 command->frame->enable_opengl();
854 command->frame->init_screen();
855 command->frame->clear_pbuffer();
857 command->input->bind_texture(0);
858 // Must call draw_texture in input frame to get the texture coordinates right.
860 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
866 // (float)command->input->get_h() - command->out_y1,
868 // (float)command->input->get_h() - command->out_y2);
869 command->input->draw_texture(
870 command->in_x1, command->in_y2,
871 command->in_x2, command->in_y1,
873 (float)command->frame->get_h() - command->out_y1,
875 (float)command->frame->get_h() - command->out_y2);
878 command->frame->set_opengl_state(VFrame::SCREEN);
879 command->frame->screen_to_ram();
881 command->canvas->unlock_canvas();
885 void Playback3D::overlay(Canvas *canvas, VFrame *input,
886 float in_x1, float in_y1, float in_x2, float in_y2,
887 float out_x1, float out_y1, float out_x2, float out_y2,
888 float alpha, int mode, int interpolation_type,
889 VFrame *output, int is_nested)
891 Playback3DCommand command;
892 command.command = Playback3DCommand::OVERLAY;
893 command.canvas = canvas;
894 command.frame = output;
895 command.input = input;
896 command.in_x1 = in_x1;
897 command.in_y1 = in_y1;
898 command.in_x2 = in_x2;
899 command.in_y2 = in_y2;
900 command.out_x1 = out_x1;
901 command.out_y1 = out_y1;
902 command.out_x2 = out_x2;
903 command.out_y2 = out_y2;
904 command.alpha = alpha;
906 command.interpolation_type = interpolation_type;
907 command.is_nested = is_nested;
908 send_command(&command);
911 void Playback3D::overlay_sync(Playback3DCommand *command)
914 // To do these operations, we need to copy the input buffer to a texture
915 // and blend 2 textures in a shader
916 static const char * const overlay_shaders[TRANSFER_TYPES] = {
917 blend_NORMAL_frag, // TRANSFER_NORMAL
918 blend_ADDITION_frag, // TRANSFER_ADDITION
919 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
920 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
921 blend_DIVIDE_frag, // TRANSFER_DIVIDE
922 blend_REPLACE_frag, // TRANSFER_REPLACE
923 blend_MAX_frag, // TRANSFER_MAX
924 blend_MIN_frag, // TRANSFER_MIN
925 blend_DARKEN_frag, // TRANSFER_DARKEN
926 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
927 blend_DST_frag, // TRANSFER_DST
928 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
929 blend_DST_IN_frag, // TRANSFER_DST_IN
930 blend_DST_OUT_frag, // TRANSFER_DST_OUT
931 blend_DST_OVER_frag, // TRANSFER_DST_OVER
932 blend_SRC_frag, // TRANSFER_SRC
933 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
934 blend_SRC_IN_frag, // TRANSFER_SRC_IN
935 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
936 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
937 blend_AND_frag, // TRANSFER_AND
938 blend_OR_frag, // TRANSFER_OR
939 blend_XOR_frag, // TRANSFER_XOR
940 blend_OVERLAY_frag, // TRANSFER_OVERLAY
941 blend_SCREEN_frag, // TRANSFER_SCREEN
942 blend_BURN_frag, // TRANSFER_BURN
943 blend_DODGE_frag, // TRANSFER_DODGE
944 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
945 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
946 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
949 BC_WindowBase *window =
950 command->canvas->lock_canvas("Playback3D::overlay_sync");
952 // Make sure OpenGL is enabled first.
953 window->enable_opengl();
954 window->update_video_cursor();
956 glColor4f(1, 1, 1, 1);
960 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
961 switch( command->input->get_opengl_state() ) {
962 // Upload texture and composite to screen
964 command->input->to_texture();
966 // Just composite texture to screen
967 case VFrame::TEXTURE:
969 // read from PBuffer to texture, then composite texture to screen
971 command->input->enable_opengl();
972 command->input->screen_to_texture();
975 printf("Playback3D::overlay_sync unknown state\n");
981 command->frame->enable_opengl();
982 command->frame->set_opengl_state(VFrame::SCREEN);
983 canvas_w = command->frame->get_w();
984 canvas_h = command->frame->get_h();
988 window->enable_opengl();
989 canvas_w = window->get_w();
990 canvas_h = window->get_h();
994 const char *shader_stack[16];
995 memset(shader_stack,0, sizeof(shader_stack));
996 int total_shaders = 0, need_matrix = 0;
998 VFrame::init_screen(canvas_w, canvas_h);
1001 command->input->bind_texture(0);
1003 // Convert colormodel to RGB if not nested.
1004 // The color model setting in the output frame is ignored.
1005 // if( command->is_nested <= 0 && // not nested
1006 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
1008 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
1012 #define add_shader(s) \
1013 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1014 shader_stack[total_shaders++] = s
1016 switch(command->mode) {
1017 case TRANSFER_REPLACE:
1018 // This requires overlaying an alpha multiplied image on a black screen.
1019 if( command->input->get_texture_components() != 4 ) break;
1020 add_shader(overlay_shaders[command->mode]);
1023 enable_overlay_texture(command);
1024 add_shader(overlay_shaders[command->mode]);
1028 // if to flatten alpha
1029 // if( command->is_nested < 0 ) {
1030 // switch(command->input->get_color_model()) {
1031 //// yuv has already been converted to rgb
1032 // case BC_YUVA8888:
1033 // case BC_RGBA_FLOAT:
1034 // case BC_RGBA8888:
1035 // add_shader(rgba_to_rgb_flatten);
1042 unsigned int shader = !shader_stack[0] ? 0 :
1043 VFrame::make_shader(shader_stack);
1045 glUseProgram(shader);
1046 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1047 // Set texture unit of the texture
1048 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1049 // Set texture unit of the temp texture
1050 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1052 int variable = glGetUniformLocation(shader, "alpha");
1053 glUniform1f(variable, command->alpha);
1054 // Set dimensions of the temp texture
1056 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1057 (float)temp_texture->get_texture_w(),
1058 (float)temp_texture->get_texture_h());
1064 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1065 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1066 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1068 command->input->draw_texture(
1069 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1070 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1071 !command->is_nested);
1074 // Delete temp texture
1076 delete temp_texture;
1078 glActiveTexture(GL_TEXTURE1);
1079 glDisable(GL_TEXTURE_2D);
1081 glActiveTexture(GL_TEXTURE0);
1082 glDisable(GL_TEXTURE_2D);
1084 command->canvas->unlock_canvas();
1088 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1091 glDisable(GL_BLEND);
1093 glActiveTexture(GL_TEXTURE1);
1094 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1095 command->input->get_color_model());
1096 temp_texture->bind(1);
1098 // Read canvas into texture
1099 glReadBuffer(GL_BACK);
1100 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1105 void Playback3D::do_mask(Canvas *canvas,
1107 int64_t start_position_project,
1108 MaskAutos *keyframe_set,
1110 MaskAuto *default_auto)
1112 Playback3DCommand command;
1113 command.command = Playback3DCommand::DO_MASK;
1114 command.canvas = canvas;
1115 command.frame = output;
1116 command.start_position_project = start_position_project;
1117 command.keyframe_set = keyframe_set;
1118 command.keyframe = keyframe;
1119 command.default_auto = default_auto;
1121 send_command(&command);
1125 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1127 int x1 = iy1 < iy2 ? ix1 : ix2;
1128 int y1 = iy1 < iy2 ? iy1 : iy2;
1129 int x2 = iy1 < iy2 ? ix2 : ix1;
1130 int y2 = iy1 < iy2 ? iy2 : iy1;
1133 int dx = x2-x1, dy = y2-y1;
1134 int dx2 = 2*dx, dy2 = 2*dy;
1135 if( dx < 0 ) dx = -dx;
1136 int m = dx > dy ? dx : dy, n = m;
1138 if( dx2 >= 0 ) do { /* +Y, +X */
1139 spots.append(x, y++);
1140 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
1141 } while( --n >= 0 );
1142 else do { /* +Y, -X */
1143 spots.append(x, y++);
1144 if( (m += dx2) < 0 ) { m += dy2; --x; }
1145 } while( --n >= 0 );
1148 if( dx2 >= 0 ) do { /* +X, +Y */
1149 spots.append(x++, y);
1150 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
1151 } while( --n >= 0 );
1152 else do { /* -X, +Y */
1153 spots.append(x--, y);
1154 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
1155 } while( --n >= 0 );
1160 class fb_texture : public BC_Texture
1163 fb_texture(int w, int h, int colormodel);
1165 void bind(int texture_unit);
1166 void read_screen(int x, int y, int w, int h);
1167 void set_output_texture();
1171 fb_texture::fb_texture(int w, int h, int colormodel)
1172 : BC_Texture(w, h, colormodel)
1175 // glGenRenderbuffers(1, &rb);
1176 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1177 // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1178 glGenFramebuffers(1, &fb);
1179 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1180 // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1183 fb_texture::~fb_texture()
1185 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1186 glDeleteFramebuffers(1, (GLuint *)&fb);
1187 // glBindRenderbuffer(GL_RENDERBUFFER, 0);
1188 // glGenRenderbuffers(1, &rb);
1191 void fb_texture::bind(int texture_unit)
1193 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1194 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1195 BC_Texture::bind(texture_unit);
1198 void fb_texture::read_screen(int x, int y, int w, int h)
1200 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1201 glReadBuffer(GL_BACK);
1202 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1205 void fb_texture::set_output_texture()
1207 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, get_texture_id(), 0);
1208 GLenum dbo[1] = { GL_COLOR_ATTACHMENT0, }; // bind layout(location=0) out vec4 color;
1209 glDrawBuffers(1, dbo);
1210 int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1211 if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1212 printf("glDrawBuffer error 0x%04x\n", ret);
1217 static void combineData(GLdouble coords[3],
1218 GLdouble *vertex_data[4], GLfloat weight[4],
1219 GLdouble **outData, void *data)
1221 ArrayList<double *> *invented = (ArrayList<double *> *)data;
1222 GLdouble *vertex = new double[6];
1223 invented->append(vertex);
1224 vertex[0] = coords[0];
1225 vertex[1] = coords[1];
1226 vertex[2] = coords[2];
1227 for( int i=3; i<6; ++i ) {
1229 for( int k=0; k<4; ++k ) {
1230 if( !weight[k] || !vertex_data[k] ) continue;
1231 v += weight[k] * vertex_data[k][i];
1239 static void zglBegin(GLenum mode) { glBegin(mode); }
1240 static void zglEnd() { glEnd(); }
1241 static void zglVertex3dv(const GLdouble *v) { glVertex3dv(v); }
1245 void Playback3D::do_mask_sync(Playback3DCommand *command)
1248 BC_WindowBase *window =
1249 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1251 window->enable_opengl();
1253 switch( command->frame->get_opengl_state() ) {
1255 // upload frame to the texture
1256 command->frame->to_texture();
1259 case VFrame::SCREEN:
1260 // Read back from PBuffer
1261 // Bind context to pbuffer
1262 command->frame->enable_opengl();
1263 command->frame->screen_to_texture();
1267 // Initialize coordinate system
1268 command->frame->enable_opengl();
1269 command->frame->init_screen();
1270 int color_model = command->frame->get_color_model();
1271 int w = command->frame->get_w();
1272 int h = command->frame->get_h();
1274 float faders[SUBMASKS], feathers[SUBMASKS], bg = 1;
1275 MaskPoints point_set[SUBMASKS];
1276 // Draw every submask as a new polygon
1277 int total_submasks = command->keyframe_set->total_submasks(
1278 command->start_position_project, PLAY_FORWARD);
1280 for(int k = 0; k < total_submasks; k++) {
1281 MaskPoints &points = point_set[k];
1282 command->keyframe_set->get_points(&points,
1283 k, command->start_position_project, PLAY_FORWARD);
1284 float fader = command->keyframe_set->get_fader(
1285 command->start_position_project, k, PLAY_FORWARD);
1286 float v = fader/100.;
1288 if( v < 0 && (v+=1) < bg ) bg = v;
1289 float feather = command->keyframe_set->get_feather(
1290 command->start_position_project, k, PLAY_FORWARD);
1291 feathers[k] = feather;
1294 glDisable(GL_TEXTURE_2D);
1295 glClearColor(bg, bg, bg, bg);
1296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1298 int show_mask = command->keyframe_set->track->masks;
1299 for(int k = 0; k < total_submasks; k++) {
1300 MaskEdge &edge = *edges.append(new MaskEdge());
1301 if( !((show_mask>>k) & 1) ) continue;
1302 edge.load(point_set[k], h);
1303 if( edge.size() > 0 ) {
1305 float fader = faders[k];
1306 float v = fader < 0 ? 1 : 1-fader;
1307 glColor4f(v, v, v, v);
1308 int display_list = glGenLists(1);
1309 glNewList(display_list, GL_COMPILE);
1311 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1312 glBegin(GL_POLYGON);
1313 MaskCoord *c = &edge[0];
1314 for( int i=edge.size(); --i>=0; ++c )
1315 glVertex2f(c->x, c->y);
1318 GLUtesselator *tess = gluNewTess();
1319 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex3dv);
1320 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1321 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1322 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1323 ArrayList<double *> invented;
1324 invented.set_array_delete();
1326 gluTessBeginPolygon(tess, &invented);
1327 gluTessBeginContour(tess);
1328 MaskCoord *c = &edge[0];
1329 for( int i=edge.size(); --i>=0; ++c )
1330 gluTessVertex(tess, (GLdouble *)c, c);
1331 gluTessEndContour(tess);
1332 gluTessEndPolygon(tess);
1333 gluDeleteTess(tess);
1334 invented.remove_all_objects();
1337 glCallList(display_list);
1338 glDeleteLists(1, display_list);
1343 fb_texture *in = new fb_texture(w, h, color_model);
1345 in->read_screen(0,0, w,h);
1346 fb_texture *out = new fb_texture(w, h, color_model);
1348 unsigned int frag_shader =
1349 VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1350 if( frag_shader > 0 ) {
1352 glGenBuffers(1, points);
1353 for(int k = 0; k < total_submasks; k++) {
1354 MaskEdge &edge = *edges[k];
1355 if( !edge.size() ) continue;
1356 if( !faders[k] ) continue;
1357 if( !feathers[k] ) continue;
1359 for( int i=0; i<edge.size(); ++i ) {
1360 MaskCoord &a = edge[i];
1361 MaskCoord &b = i<edge.size()-1 ? edge[i+1] : edge[0];
1362 draw_spots(spots, a.x,a.y+h, b.x,b.y+h);
1364 int sz = spots.size() * sizeof(MaskSpot);
1365 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, points[0], 0, sz);
1366 glBufferData(GL_SHADER_STORAGE_BUFFER, sz, &spots[0], GL_DYNAMIC_COPY);
1367 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1368 glUseProgram(frag_shader);
1369 float r = feathers[k], v = faders[k];
1370 glUniform1f(glGetUniformLocation(frag_shader, "r"), r);
1371 glUniform1f(glGetUniformLocation(frag_shader, "v"), v);
1373 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1374 out->set_output_texture();
1375 glViewport(0,0, w,h);
1376 out->draw_texture(0,0, w,h, 0,0, w,h);
1378 fb_texture *t = in; in = out; out = t;
1380 glDeleteBuffers(1, points);
1383 glDrawBuffers(0, 0);
1384 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1386 const char *alpha_shader = BC_CModels::has_alpha(color_model) ?
1387 multiply_mask4_frag :
1388 !BC_CModels::is_yuv(color_model) ?
1389 multiply_mask3_frag :
1390 multiply_yuvmask3_frag;
1391 unsigned int shader = VFrame::make_shader(0, alpha_shader, 0);
1392 glUseProgram(shader);
1394 command->frame->bind_texture(0);
1395 in->BC_Texture::bind(1);
1396 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1397 glUniform1i(glGetUniformLocation(shader, "tex1"), 1);
1399 command->frame->draw_texture();
1400 command->frame->set_opengl_state(VFrame::SCREEN);
1405 glDisable(GL_TEXTURE_2D);
1406 glColor4f(1, 1, 1, 1);
1407 glActiveTexture(GL_TEXTURE0);
1408 window->enable_opengl();
1410 command->canvas->unlock_canvas();
1415 void Playback3D::convert_cmodel(Canvas *canvas,
1419 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1420 int src_cmodel = output->get_color_model();
1422 (output->get_opengl_state() == VFrame::TEXTURE ||
1423 output->get_opengl_state() == VFrame::SCREEN) &&
1424 // OpenGL has no floating point.
1425 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1426 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1427 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1428 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1429 // OpenGL sets alpha to 1 on import
1430 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1431 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1432 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1437 Playback3DCommand command;
1438 command.command = Playback3DCommand::CONVERT_CMODEL;
1439 command.canvas = canvas;
1440 command.frame = output;
1441 command.dst_cmodel = dst_cmodel;
1442 send_command(&command);
1445 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1448 BC_WindowBase *window =
1449 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1451 window->enable_opengl();
1453 // Import into hardware
1454 command->frame->enable_opengl();
1455 command->frame->init_screen();
1456 command->frame->to_texture();
1458 // Colormodel permutation
1459 int src_cmodel = command->frame->get_color_model();
1460 int dst_cmodel = command->dst_cmodel;
1464 } cmodel_shader_table_t;
1465 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1467 static cmodel_shader_table_t cmodel_shader_table[] = {
1468 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1469 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1470 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1471 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1472 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1473 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1474 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1475 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1476 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1477 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1478 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1479 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1480 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1481 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1482 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1483 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1484 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1485 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1486 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1487 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1488 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1491 const char *shader = 0;
1492 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1493 for( int i=0; i<table_size; ++i ) {
1494 if( cmodel_shader_table[i].src == src_cmodel &&
1495 cmodel_shader_table[i].dst == dst_cmodel ) {
1496 shader = cmodel_shader_table[i].shader;
1497 type = cmodel_shader_table[i].typ;
1502 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1504 // command->frame->get_color_model(),
1505 // command->dst_cmodel,
1508 const char *shader_stack[9];
1509 memset(shader_stack,0, sizeof(shader_stack));
1510 int current_shader = 0;
1513 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1514 shader_stack[current_shader++] = shader;
1515 shader_stack[current_shader] = 0;
1516 unsigned int shader_id = VFrame::make_shader(shader_stack);
1518 command->frame->bind_texture(0);
1519 glUseProgram(shader_id);
1521 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1524 BC_GL_RGB_TO_YUV(shader_id);
1527 BC_GL_YUV_TO_RGB(shader_id);
1531 command->frame->draw_texture();
1532 if(shader) glUseProgram(0);
1533 command->frame->set_opengl_state(VFrame::SCREEN);
1537 command->canvas->unlock_canvas();
1541 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1543 Playback3DCommand command;
1544 command.command = Playback3DCommand::DO_FADE;
1545 command.canvas = canvas;
1546 command.frame = frame;
1547 command.alpha = fade;
1548 send_command(&command);
1551 void Playback3D::do_fade_sync(Playback3DCommand *command)
1554 BC_WindowBase *window =
1555 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1557 window->enable_opengl();
1558 switch( command->frame->get_opengl_state() ) {
1560 command->frame->to_texture();
1563 case VFrame::SCREEN:
1564 // Read back from PBuffer
1565 // Bind context to pbuffer
1566 command->frame->enable_opengl();
1567 command->frame->screen_to_texture();
1571 command->frame->enable_opengl();
1572 command->frame->init_screen();
1573 command->frame->bind_texture(0);
1575 // glClearColor(0.0, 0.0, 0.0, 0.0);
1576 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1577 glDisable(GL_BLEND);
1578 unsigned int frag_shader = 0;
1579 switch(command->frame->get_color_model())
1581 // For the alpha colormodels, the native function seems to multiply the
1582 // components by the alpha instead of just the alpha.
1586 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1591 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1592 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1597 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1603 glUseProgram(frag_shader);
1605 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1606 glUniform1i(variable, 0);
1607 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1608 glUniform1f(variable, command->alpha);
1611 command->frame->draw_texture();
1612 command->frame->set_opengl_state(VFrame::SCREEN);
1619 glColor4f(1, 1, 1, 1);
1620 glDisable(GL_BLEND);
1622 command->canvas->unlock_canvas();
1627 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1629 Playback3DCommand command;
1630 command.command = Playback3DCommand::PLUGIN;
1631 command.canvas = canvas;
1632 command.plugin_client = client;
1633 return send_command(&command);
1636 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1638 BC_WindowBase *window =
1639 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1641 window->enable_opengl();
1642 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1644 command->canvas->unlock_canvas();