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"
41 #include "transportque.inc"
59 // These should be passed to VFrame::make_shader to construct shaders.
60 // Can't hard code sampler2D
64 static const char *yuv_to_rgb_frag =
65 "uniform sampler2D tex;\n"
66 "uniform mat3 yuv_to_rgb_matrix;\n"
67 "uniform float yminf;\n"
70 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
71 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
72 " gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
75 static const char *yuva_to_yuv_frag =
76 "uniform sampler2D tex;\n"
79 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
80 " float a = yuva.a;\n"
81 " float anti_a = 1.0 - a;\n"
83 " yuva.g = yuva.g * a + 0.5 * anti_a;\n"
84 " yuva.b = yuva.b * a + 0.5 * anti_a;\n"
86 " gl_FragColor = yuva;\n"
89 static const char *yuva_to_rgb_frag =
90 "uniform sampler2D tex;\n"
91 "uniform mat3 yuv_to_rgb_matrix;\n"
92 "uniform float yminf;\n"
95 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
96 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
97 " yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
98 " yuva.rgb *= yuva.a;\n"
100 " gl_FragColor = yuva;\n"
103 static const char *rgb_to_yuv_frag =
104 "uniform sampler2D tex;\n"
105 "uniform mat3 rgb_to_yuv_matrix;\n"
106 "uniform float yminf;\n"
109 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
110 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
111 " rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
112 " gl_FragColor = rgba;\n"
116 static const char *rgba_to_rgb_frag =
117 "uniform sampler2D tex;\n"
120 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
121 " rgba.rgb *= rgba.a;\n"
123 " gl_FragColor = rgba;\n"
126 static const char *rgba_to_yuv_frag =
127 "uniform sampler2D tex;\n"
128 "uniform mat3 rgb_to_yuv_matrix;\n"
129 "uniform float yminf;\n"
132 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
133 " rgba.rgb *= rgba.a;\n"
135 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
136 " rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
137 " gl_FragColor = rgba;\n"
140 //static const char *rgba_to_rgb_flatten =
142 // " gl_FragColor.rgb *= gl_FragColor.a;\n"
143 // " gl_FragColor.a = 1.0;\n"
146 #define GL_STD_BLEND(FN) \
147 static const char *blend_##FN##_frag = \
148 "uniform sampler2D tex2;\n" \
149 "uniform vec2 tex2_dimensions;\n" \
150 "uniform float alpha;\n" \
152 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
154 " result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
155 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
156 " gl_FragColor = mix(canvas, result, alpha);\n" \
159 #define GL_VEC_BLEND(FN) \
160 static const char *blend_##FN##_frag = \
161 "uniform sampler2D tex2;\n" \
162 "uniform vec2 tex2_dimensions;\n" \
163 "uniform float alpha;\n" \
165 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
167 " result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
168 " result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
169 " result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
170 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
171 " result = clamp(result, 0.0, 1.0);\n" \
172 " gl_FragColor = mix(canvas, result, alpha);\n" \
190 static const char *blend_NORMAL_frag =
191 "uniform sampler2D tex2;\n"
192 "uniform vec2 tex2_dimensions;\n"
193 "uniform float alpha;\n"
195 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
196 " vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
197 " gl_FragColor = mix(canvas, result, alpha);\n"
201 static const char *blend_REPLACE_frag =
202 "uniform float alpha;\n"
207 static const char *blend_ADDITION_frag =
208 "uniform sampler2D tex2;\n"
209 "uniform vec2 tex2_dimensions;\n"
210 "uniform float alpha;\n"
212 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
213 " vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
214 " gl_FragColor = mix(canvas, result, alpha);\n"
218 static const char *blend_SUBTRACT_frag =
219 "uniform sampler2D tex2;\n"
220 "uniform vec2 tex2_dimensions;\n"
221 "uniform float alpha;\n"
223 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
224 " vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
225 " gl_FragColor = mix(canvas, result, alpha);\n"
228 GL_STD_BLEND(MULTIPLY);
229 GL_VEC_BLEND(DIVIDE);
232 GL_VEC_BLEND(DARKEN);
233 GL_VEC_BLEND(LIGHTEN);
235 GL_STD_BLEND(DST_ATOP);
236 GL_STD_BLEND(DST_IN);
237 GL_STD_BLEND(DST_OUT);
238 GL_STD_BLEND(DST_OVER);
240 GL_STD_BLEND(SRC_ATOP);
241 GL_STD_BLEND(SRC_IN);
242 GL_STD_BLEND(SRC_OUT);
243 GL_STD_BLEND(SRC_OVER);
247 GL_VEC_BLEND(OVERLAY);
248 GL_STD_BLEND(SCREEN);
251 GL_VEC_BLEND(HARDLIGHT);
252 GL_VEC_BLEND(SOFTLIGHT);
253 GL_VEC_BLEND(DIFFERENCE);
255 static const char *read_texture_frag =
256 "uniform sampler2D tex;\n"
259 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
262 static const char *in_vertex_frag =
263 "#version 430 // vertex shader\n"
266 " gl_Position = vec4(in_pos-vec3(0.5,0.5,0.), .5);\n"
269 static const char *feather_frag =
271 "layout(location=0) out vec4 color;\n"
272 "uniform sampler2D tex;\n"
273 // apparently, only doubles index properly in shared buffers
274 "buffer buf { dvec2 points[]; };\n"
278 " vec2 tex_st = gl_FragCoord.xy/textureSize(tex,0);\n"
279 " color = texture(tex, tex_st);\n"
280 " if( r==0. ) return;\n"
281 " float rv = r*v>0. ? 1 : -1;\n"
282 " float rr = r*r, dr = 1./rr;\n"
283 " float vv = v>=0 ? 1.-v : 1.+v;\n"
284 " float fg = rv>=0 ? vv : 1.;\n"
285 " float bg = rv>=0 ? 1. : vv;\n"
286 " int len = points.length();\n"
287 " for( int i=0; i<len; ++i ) {\n"
288 " float dx = float(points[i].x) - gl_FragCoord.x;\n"
289 " float dy = float(points[i].y) - gl_FragCoord.y;\n"
290 " float dd = dx*dx + dy*dy;\n"
291 " if( dd >= rr ) continue;\n"
292 " float d = dd*dr;\n"
293 " float a = (1.-d)*fg + d*bg;\n"
294 " if( rv*(color.a-a) > 0 ) color = vec4(a);\n"
298 static const char *alpha_frag =
299 "uniform sampler2D tex;\n"
300 "uniform sampler2D tex2;\n"
301 "uniform vec2 tex2_dimensions;\n"
303 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
304 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
305 " gl_FragColor.a = canvas.a;\n"
308 static const char *fade_rgba_frag =
309 "uniform sampler2D tex;\n"
310 "uniform float alpha;\n"
313 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
314 " gl_FragColor.a *= alpha;\n"
317 static const char *fade_yuv_frag =
318 "uniform sampler2D tex;\n"
319 "uniform float alpha;\n"
322 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
323 " gl_FragColor.r *= alpha;\n"
324 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
325 " gl_FragColor.g *= alpha;\n"
326 " gl_FragColor.b *= alpha;\n"
327 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
333 Playback3DCommand::Playback3DCommand()
334 : BC_SynchronousCommand()
340 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
342 Playback3DCommand *ptr = (Playback3DCommand*)command;
343 this->canvas = ptr->canvas;
344 this->is_cleared = ptr->is_cleared;
346 this->in_x1 = ptr->in_x1;
347 this->in_y1 = ptr->in_y1;
348 this->in_x2 = ptr->in_x2;
349 this->in_y2 = ptr->in_y2;
350 this->out_x1 = ptr->out_x1;
351 this->out_y1 = ptr->out_y1;
352 this->out_x2 = ptr->out_x2;
353 this->out_y2 = ptr->out_y2;
354 this->alpha = ptr->alpha;
355 this->mode = ptr->mode;
356 this->interpolation_type = ptr->interpolation_type;
358 this->input = ptr->input;
359 this->start_position_project = ptr->start_position_project;
360 this->keyframe_set = ptr->keyframe_set;
361 this->keyframe = ptr->keyframe;
362 this->default_auto = ptr->default_auto;
363 this->plugin_client = ptr->plugin_client;
364 this->want_texture = ptr->want_texture;
365 this->is_nested = ptr->is_nested;
366 this->dst_cmodel = ptr->dst_cmodel;
368 BC_SynchronousCommand::copy_from(command);
371 Playback3D::Playback3D(MWindow *mwindow)
374 this->mwindow = mwindow;
378 Playback3D::~Playback3D()
385 BC_SynchronousCommand* Playback3D::new_command()
387 return new Playback3DCommand;
392 void Playback3D::handle_command(BC_SynchronousCommand *command)
394 //printf("Playback3D::handle_command 1 %d\n", command->command);
395 switch(command->command)
397 case Playback3DCommand::WRITE_BUFFER:
398 write_buffer_sync((Playback3DCommand*)command);
401 case Playback3DCommand::CLEAR_OUTPUT:
402 clear_output_sync((Playback3DCommand*)command);
405 case Playback3DCommand::CLEAR_INPUT:
406 clear_input_sync((Playback3DCommand*)command);
409 case Playback3DCommand::DO_CAMERA:
410 do_camera_sync((Playback3DCommand*)command);
413 case Playback3DCommand::OVERLAY:
414 overlay_sync((Playback3DCommand*)command);
417 case Playback3DCommand::DO_FADE:
418 do_fade_sync((Playback3DCommand*)command);
421 case Playback3DCommand::DO_MASK:
422 do_mask_sync((Playback3DCommand*)command);
425 case Playback3DCommand::PLUGIN:
426 run_plugin_sync((Playback3DCommand*)command);
429 case Playback3DCommand::COPY_FROM:
430 copy_from_sync((Playback3DCommand*)command);
433 case Playback3DCommand::CONVERT_CMODEL:
434 convert_cmodel_sync((Playback3DCommand*)command);
437 // case Playback3DCommand::DRAW_REFRESH:
438 // draw_refresh_sync((Playback3DCommand*)command);
441 //printf("Playback3D::handle_command 10\n");
447 void Playback3D::copy_from(Canvas *canvas,
452 Playback3DCommand command;
453 command.command = Playback3DCommand::COPY_FROM;
454 command.canvas = canvas;
457 command.want_texture = want_texture;
458 send_command(&command);
461 void Playback3D::copy_from_sync(Playback3DCommand *command)
464 BC_WindowBase *window =
465 command->canvas->lock_canvas("Playback3D::copy_from_sync");
467 window->enable_opengl();
469 int w = command->input->get_w();
470 int h = command->input->get_h();
472 if(command->input->get_opengl_state() == VFrame::SCREEN &&
473 w == command->frame->get_w() && h == command->frame->get_h())
475 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
476 // command->input->get_w(),
477 // command->input->get_h(),
478 // command->frame->get_w(),
479 // command->frame->get_h(),
480 // command->frame->get_color_model());
481 // With NVidia at least,
484 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
488 if(command->want_texture)
490 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
491 // Screen_to_texture requires the source pbuffer enabled.
492 command->input->enable_opengl();
493 command->frame->screen_to_texture();
494 command->frame->set_opengl_state(VFrame::TEXTURE);
499 command->input->to_texture();
500 command->input->bind_texture(0);
501 command->frame->enable_opengl();
502 command->frame->init_screen();
503 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
504 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
506 glUseProgram(shader);
507 int variable = glGetUniformLocation(shader, "tex");
508 glUniform1i(variable, 0);
509 BC_GL_YUV_TO_RGB(shader);
513 command->input->draw_texture(1);
514 command->frame->screen_to_ram();
520 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
521 command->input->get_opengl_state(), w, h,
522 command->frame->get_w(), command->frame->get_h());
525 command->canvas->unlock_canvas();
532 // void Playback3D::draw_refresh(Canvas *canvas,
543 // Playback3DCommand command;
544 // command.command = Playback3DCommand::DRAW_REFRESH;
545 // command.canvas = canvas;
546 // command.frame = frame;
547 // command.in_x1 = in_x1;
548 // command.in_y1 = in_y1;
549 // command.in_x2 = in_x2;
550 // command.in_y2 = in_y2;
551 // command.out_x1 = out_x1;
552 // command.out_y1 = out_y1;
553 // command.out_x2 = out_x2;
554 // command.out_y2 = out_y2;
555 // send_command(&command);
558 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
561 // BC_WindowBase *window =
562 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
564 // window->enable_opengl();
566 // // Read output pbuffer back to RAM in project colormodel
567 // // RGB 8bit is fastest for OpenGL to read back.
568 // command->frame->reallocate(0,
572 // command->frame->get_w(),
573 // command->frame->get_h(),
576 // command->frame->screen_to_ram();
578 // window->clear_box(0,
582 // window->draw_vframe(command->frame,
583 // (int)command->out_x1,
584 // (int)command->out_y1,
585 // (int)(command->out_x2 - command->out_x1),
586 // (int)(command->out_y2 - command->out_y1),
587 // (int)command->in_x1,
588 // (int)command->in_y1,
589 // (int)(command->in_x2 - command->in_x1),
590 // (int)(command->in_y2 - command->in_y1),
594 // command->canvas->unlock_canvas();
602 void Playback3D::write_buffer(Canvas *canvas,
614 Playback3DCommand command;
615 command.command = Playback3DCommand::WRITE_BUFFER;
616 command.canvas = canvas;
617 command.frame = frame;
618 command.in_x1 = in_x1;
619 command.in_y1 = in_y1;
620 command.in_x2 = in_x2;
621 command.in_y2 = in_y2;
622 command.out_x1 = out_x1;
623 command.out_y1 = out_y1;
624 command.out_x2 = out_x2;
625 command.out_y2 = out_y2;
626 command.is_cleared = is_cleared;
627 send_command(&command);
631 void Playback3D::write_buffer_sync(Playback3DCommand *command)
634 BC_WindowBase *window =
635 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
637 window->enable_opengl();
638 // Update hidden cursor
639 window->update_video_cursor();
640 command->frame->enable_opengl();
641 command->frame->init_screen();
642 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
643 int frame_state = command->frame->get_opengl_state();
644 if( frame_state != VFrame::TEXTURE )
645 command->frame->to_texture();
646 if( frame_state != VFrame::RAM ) {
647 command->in_y1 = command->frame->get_h() - command->in_y1;
648 command->in_y2 = command->frame->get_h() - command->in_y2;
650 window->enable_opengl();
651 draw_output(command, 1);
652 command->frame->set_opengl_state(frame_state);
654 command->canvas->unlock_canvas();
660 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
663 int texture_id = command->frame->get_texture_id();
664 BC_WindowBase *window = command->canvas->get_canvas();
666 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
668 // command->canvas->get_canvas());
673 // If virtual console is being used, everything in this function has
674 // already been done except the page flip.
677 canvas_w = window->get_w();
678 canvas_h = window->get_h();
679 VFrame::init_screen(canvas_w, canvas_h);
680 int color_model = command->frame->get_color_model();
681 int is_yuv = BC_CModels::is_yuv(color_model);
683 if(!command->is_cleared)
685 // If we get here, the virtual console was not used.
686 init_frame(command, 0);
690 // Undo any previous shader settings
691 command->frame->bind_texture(0);
693 // Convert colormodel
694 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
696 glUseProgram(shader);
697 // Set texture unit of the texture
698 int variable = glGetUniformLocation(shader, "tex");
699 glUniform1i(variable, 0);
700 BC_GL_YUV_TO_RGB(shader);
703 // if(BC_CModels::components(color_model) == 4)
705 // glEnable(GL_BLEND);
706 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
709 command->frame->draw_texture(
710 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
711 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
715 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
716 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
717 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
721 command->canvas->get_canvas()->flip_opengl();
728 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
731 float gbuv = is_yuv ? 0.5 : 0.0;
732 glClearColor(0.0, gbuv, gbuv, 0.0);
733 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
738 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
740 Playback3DCommand command;
741 command.command = Playback3DCommand::CLEAR_OUTPUT;
742 command.canvas = canvas;
743 command.frame = output;
744 send_command(&command);
747 void Playback3D::clear_output_sync(Playback3DCommand *command)
750 BC_WindowBase *window =
751 command->canvas->lock_canvas("Playback3D::clear_output_sync");
753 // If we get here, the virtual console is being used.
754 command->canvas->get_canvas()->enable_opengl();
756 // Using pbuffer for refresh frame.
757 if( command->frame ) {
758 command->frame->enable_opengl();
759 int color_model = command->canvas->mwindow->edl->session->color_model;
760 is_yuv = BC_CModels::is_yuv(color_model);
763 init_frame(command, is_yuv);
765 command->canvas->unlock_canvas();
770 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
772 Playback3DCommand command;
773 command.command = Playback3DCommand::CLEAR_INPUT;
774 command.canvas = canvas;
775 command.frame = frame;
776 send_command(&command);
779 void Playback3D::clear_input_sync(Playback3DCommand *command)
782 BC_WindowBase *window =
783 command->canvas->lock_canvas("Playback3D::clear_input_sync");
785 command->canvas->get_canvas()->enable_opengl();
786 command->frame->enable_opengl();
787 command->frame->clear_pbuffer();
788 command->frame->set_opengl_state(VFrame::SCREEN);
790 command->canvas->unlock_canvas();
794 void Playback3D::do_camera(Canvas *canvas,
806 Playback3DCommand command;
807 command.command = Playback3DCommand::DO_CAMERA;
808 command.canvas = canvas;
809 command.input = input;
810 command.frame = output;
811 command.in_x1 = in_x1;
812 command.in_y1 = in_y1;
813 command.in_x2 = in_x2;
814 command.in_y2 = in_y2;
815 command.out_x1 = out_x1;
816 command.out_y1 = out_y1;
817 command.out_x2 = out_x2;
818 command.out_y2 = out_y2;
819 send_command(&command);
822 void Playback3D::do_camera_sync(Playback3DCommand *command)
825 BC_WindowBase *window =
826 command->canvas->lock_canvas("Playback3D::do_camera_sync");
828 command->canvas->get_canvas()->enable_opengl();
830 command->input->to_texture();
831 command->frame->enable_opengl();
832 command->frame->init_screen();
833 command->frame->clear_pbuffer();
835 command->input->bind_texture(0);
836 // Must call draw_texture in input frame to get the texture coordinates right.
838 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
844 // (float)command->input->get_h() - command->out_y1,
846 // (float)command->input->get_h() - command->out_y2);
847 command->input->draw_texture(
848 command->in_x1, command->in_y2,
849 command->in_x2, command->in_y1,
851 (float)command->frame->get_h() - command->out_y1,
853 (float)command->frame->get_h() - command->out_y2);
856 command->frame->set_opengl_state(VFrame::SCREEN);
857 command->frame->screen_to_ram();
859 command->canvas->unlock_canvas();
863 void Playback3D::overlay(Canvas *canvas, VFrame *input,
864 float in_x1, float in_y1, float in_x2, float in_y2,
865 float out_x1, float out_y1, float out_x2, float out_y2,
866 float alpha, int mode, int interpolation_type,
867 VFrame *output, int is_nested)
869 Playback3DCommand command;
870 command.command = Playback3DCommand::OVERLAY;
871 command.canvas = canvas;
872 command.frame = output;
873 command.input = input;
874 command.in_x1 = in_x1;
875 command.in_y1 = in_y1;
876 command.in_x2 = in_x2;
877 command.in_y2 = in_y2;
878 command.out_x1 = out_x1;
879 command.out_y1 = out_y1;
880 command.out_x2 = out_x2;
881 command.out_y2 = out_y2;
882 command.alpha = alpha;
884 command.interpolation_type = interpolation_type;
885 command.is_nested = is_nested;
886 send_command(&command);
889 void Playback3D::overlay_sync(Playback3DCommand *command)
892 // To do these operations, we need to copy the input buffer to a texture
893 // and blend 2 textures in a shader
894 static const char * const overlay_shaders[TRANSFER_TYPES] = {
895 blend_NORMAL_frag, // TRANSFER_NORMAL
896 blend_ADDITION_frag, // TRANSFER_ADDITION
897 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
898 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
899 blend_DIVIDE_frag, // TRANSFER_DIVIDE
900 blend_REPLACE_frag, // TRANSFER_REPLACE
901 blend_MAX_frag, // TRANSFER_MAX
902 blend_MIN_frag, // TRANSFER_MIN
903 blend_DARKEN_frag, // TRANSFER_DARKEN
904 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
905 blend_DST_frag, // TRANSFER_DST
906 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
907 blend_DST_IN_frag, // TRANSFER_DST_IN
908 blend_DST_OUT_frag, // TRANSFER_DST_OUT
909 blend_DST_OVER_frag, // TRANSFER_DST_OVER
910 blend_SRC_frag, // TRANSFER_SRC
911 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
912 blend_SRC_IN_frag, // TRANSFER_SRC_IN
913 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
914 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
915 blend_AND_frag, // TRANSFER_AND
916 blend_OR_frag, // TRANSFER_OR
917 blend_XOR_frag, // TRANSFER_XOR
918 blend_OVERLAY_frag, // TRANSFER_OVERLAY
919 blend_SCREEN_frag, // TRANSFER_SCREEN
920 blend_BURN_frag, // TRANSFER_BURN
921 blend_DODGE_frag, // TRANSFER_DODGE
922 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
923 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
924 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
927 BC_WindowBase *window =
928 command->canvas->lock_canvas("Playback3D::overlay_sync");
930 // Make sure OpenGL is enabled first.
931 window->enable_opengl();
932 window->update_video_cursor();
934 glColor4f(1, 1, 1, 1);
938 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
939 switch( command->input->get_opengl_state() ) {
940 // Upload texture and composite to screen
942 command->input->to_texture();
944 // Just composite texture to screen
945 case VFrame::TEXTURE:
947 // read from PBuffer to texture, then composite texture to screen
949 command->input->enable_opengl();
950 command->input->screen_to_texture();
953 printf("Playback3D::overlay_sync unknown state\n");
959 command->frame->enable_opengl();
960 command->frame->set_opengl_state(VFrame::SCREEN);
961 canvas_w = command->frame->get_w();
962 canvas_h = command->frame->get_h();
966 window->enable_opengl();
967 canvas_w = window->get_w();
968 canvas_h = window->get_h();
972 const char *shader_stack[16];
973 memset(shader_stack,0, sizeof(shader_stack));
974 int total_shaders = 0, need_matrix = 0;
976 VFrame::init_screen(canvas_w, canvas_h);
979 command->input->bind_texture(0);
981 // Convert colormodel to RGB if not nested.
982 // The color model setting in the output frame is ignored.
983 // if( command->is_nested <= 0 && // not nested
984 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
986 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
990 #define add_shader(s) \
991 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
992 shader_stack[total_shaders++] = s
994 switch(command->mode) {
995 case TRANSFER_REPLACE:
996 // This requires overlaying an alpha multiplied image on a black screen.
997 if( command->input->get_texture_components() != 4 ) break;
998 add_shader(overlay_shaders[command->mode]);
1001 enable_overlay_texture(command);
1002 add_shader(overlay_shaders[command->mode]);
1006 // if to flatten alpha
1007 // if( command->is_nested < 0 ) {
1008 // switch(command->input->get_color_model()) {
1009 //// yuv has already been converted to rgb
1010 // case BC_YUVA8888:
1011 // case BC_RGBA_FLOAT:
1012 // case BC_RGBA8888:
1013 // add_shader(rgba_to_rgb_flatten);
1020 unsigned int shader = !shader_stack[0] ? 0 :
1021 VFrame::make_shader(shader_stack);
1023 glUseProgram(shader);
1024 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1025 // Set texture unit of the texture
1026 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1027 // Set texture unit of the temp texture
1028 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1030 int variable = glGetUniformLocation(shader, "alpha");
1031 glUniform1f(variable, command->alpha);
1032 // Set dimensions of the temp texture
1034 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1035 (float)temp_texture->get_texture_w(),
1036 (float)temp_texture->get_texture_h());
1042 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1043 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1044 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1046 command->input->draw_texture(
1047 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1048 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1049 !command->is_nested);
1052 // Delete temp texture
1054 delete temp_texture;
1056 glActiveTexture(GL_TEXTURE1);
1057 glDisable(GL_TEXTURE_2D);
1059 glActiveTexture(GL_TEXTURE0);
1060 glDisable(GL_TEXTURE_2D);
1062 command->canvas->unlock_canvas();
1066 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1069 glDisable(GL_BLEND);
1071 glActiveTexture(GL_TEXTURE1);
1072 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1073 command->input->get_color_model());
1074 temp_texture->bind(1);
1076 // Read canvas into texture
1077 glReadBuffer(GL_BACK);
1078 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1083 void Playback3D::do_mask(Canvas *canvas,
1085 int64_t start_position_project,
1086 MaskAutos *keyframe_set,
1088 MaskAuto *default_auto)
1090 Playback3DCommand command;
1091 command.command = Playback3DCommand::DO_MASK;
1092 command.canvas = canvas;
1093 command.frame = output;
1094 command.start_position_project = start_position_project;
1095 command.keyframe_set = keyframe_set;
1096 command.keyframe = keyframe;
1097 command.default_auto = default_auto;
1099 send_command(&command);
1103 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1105 int x1 = iy1 < iy2 ? ix1 : ix2;
1106 int y1 = iy1 < iy2 ? iy1 : iy2;
1107 int x2 = iy1 < iy2 ? ix2 : ix1;
1108 int y2 = iy1 < iy2 ? iy2 : iy1;
1111 int dx = x2-x1, dy = y2-y1;
1112 int dx2 = 2*dx, dy2 = 2*dy;
1113 if( dx < 0 ) dx = -dx;
1114 int m = dx > dy ? dx : dy, n = m;
1116 if( dx2 >= 0 ) do { /* +Y, +X */
1117 spots.append(x, y++);
1118 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
1119 } while( --n >= 0 );
1120 else do { /* +Y, -X */
1121 spots.append(x, y++);
1122 if( (m += dx2) < 0 ) { m += dy2; --x; }
1123 } while( --n >= 0 );
1126 if( dx2 >= 0 ) do { /* +X, +Y */
1127 spots.append(x++, y);
1128 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
1129 } while( --n >= 0 );
1130 else do { /* -X, +Y */
1131 spots.append(x--, y);
1132 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
1133 } while( --n >= 0 );
1138 class fb_texture : public BC_Texture
1141 fb_texture(int w, int h, int colormodel);
1143 void bind(int texture_unit);
1144 void read_screen(int x, int y, int w, int h);
1145 void set_output_texture();
1149 fb_texture::fb_texture(int w, int h, int colormodel)
1150 : BC_Texture(w, h, colormodel)
1153 // glGenRenderbuffers(1, &rb);
1154 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1155 // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1156 glGenFramebuffers(1, &fb);
1157 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1158 // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1161 fb_texture::~fb_texture()
1163 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1164 glDeleteFramebuffers(1, (GLuint *)&fb);
1165 // glBindRenderbuffer(GL_RENDERBUFFER, 0);
1166 // glGenRenderbuffers(1, &rb);
1169 void fb_texture::bind(int texture_unit)
1171 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1172 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1173 BC_Texture::bind(texture_unit);
1176 void fb_texture::read_screen(int x, int y, int w, int h)
1178 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1179 glReadBuffer(GL_BACK);
1180 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1183 void fb_texture::set_output_texture()
1185 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, get_texture_id(), 0);
1186 GLenum dbo[1] = { GL_COLOR_ATTACHMENT0, }; // bind layout(location=0) out vec4 color;
1187 glDrawBuffers(1, dbo);
1188 int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1189 if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1190 printf("glDrawBuffer error 0x%04x\n", ret);
1195 static void combineData(GLdouble coords[3],
1196 GLdouble *vertex_data[4], GLfloat weight[4],
1197 GLdouble **outData, void *data)
1199 ArrayList<double *> *invented = (ArrayList<double *> *)data;
1200 GLdouble *vertex = new double[6];
1201 invented->append(vertex);
1202 vertex[0] = coords[0];
1203 vertex[1] = coords[1];
1204 vertex[2] = coords[2];
1205 for( int i=3; i<6; ++i ) {
1206 vertex[i] = weight[0] * vertex_data[0][i] +
1207 weight[1] * vertex_data[1][i] +
1208 weight[2] * vertex_data[2][i] +
1209 weight[3] * vertex_data[3][i];
1215 static void zglBegin(GLenum mode) { glBegin(mode); }
1216 static void zglEnd() { glEnd(); }
1217 static void zglVertex3dv(const GLdouble *v) { glVertex3dv(v); }
1221 void Playback3D::do_mask_sync(Playback3DCommand *command)
1224 BC_WindowBase *window =
1225 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1227 window->enable_opengl();
1229 switch( command->frame->get_opengl_state() ) {
1231 // upload frame to the texture
1232 command->frame->to_texture();
1235 case VFrame::SCREEN:
1236 // Read back from PBuffer
1237 // Bind context to pbuffer
1238 command->frame->enable_opengl();
1239 command->frame->screen_to_texture();
1243 // Initialize coordinate system
1244 command->frame->enable_opengl();
1245 command->frame->init_screen();
1246 int color_model = command->frame->get_color_model();
1247 int w = command->frame->get_w();
1248 int h = command->frame->get_h();
1250 float faders[SUBMASKS], feathers[SUBMASKS], bg = 1;
1251 MaskPointSet point_set[SUBMASKS];
1252 // Draw every submask as a new polygon
1253 int total_submasks = command->keyframe_set->total_submasks(
1254 command->start_position_project, PLAY_FORWARD);
1256 for(int k = 0; k < total_submasks; k++) {
1257 MaskPointSet &points = point_set[k];
1258 command->keyframe_set->get_points(&points,
1259 k, command->start_position_project, PLAY_FORWARD);
1260 float fader = command->keyframe_set->get_fader(
1261 command->start_position_project, k, PLAY_FORWARD);
1262 float v = fader/100.;
1264 if( v < 0 && (v+=1) < bg ) bg = v;
1265 float feather = command->keyframe_set->get_feather(
1266 command->start_position_project, k, PLAY_FORWARD);
1267 feathers[k] = feather;
1270 glDisable(GL_TEXTURE_2D);
1271 glClearColor(bg, bg, bg, bg);
1272 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1274 for(int k = 0; k < total_submasks; k++) {
1275 MaskPointSet &points = point_set[k];
1276 MaskEdge &edge = *edges.append(new MaskEdge());
1277 int first_point = 0;
1278 // Need to tabulate every vertex in persistent memory because
1279 // gluTessVertex doesn't copy them.
1280 for(int i = 0; i < points.total; i++) {
1281 MaskPoint *point1 = points.values[i];
1282 MaskPoint *point2 = (i >= points.total - 1) ?
1283 points.values[0] : points.values[i + 1];
1287 if( point1->control_x2 == 0 && point1->control_y2 == 0 &&
1288 point2->control_x1 == 0 && point2->control_y1 == 0 )
1291 float x0 = point1->x, y0 = point1->y;
1292 float x1 = point1->x + point1->control_x2;
1293 float y1 = point1->y + point1->control_y2;
1294 float x2 = point2->x + point2->control_x1;
1295 float y2 = point2->y + point2->control_y1;
1296 float x3 = point2->x, y3 = point2->y;
1298 // forward differencing bezier curves implementation taken from GPL code at
1299 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1301 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1308 cx3 = - x0 + 3*x1 - 3*x2 + x3;
1309 cx2 = 3*x0 - 6*x1 + 3*x2;
1313 cy3 = - y0 + 3*y1 - 3*y2 + y3;
1314 cy2 = 3*y0 - 6*y1 + 3*y2;
1318 // This equation is from Graphics Gems I.
1320 // The idea is that since we're approximating a cubic curve with lines,
1321 // any error we incur is due to the curvature of the line, which we can
1322 // estimate by calculating the maximum acceleration of the curve. For
1323 // a cubic, the acceleration (second derivative) is a line, meaning that
1324 // the absolute maximum acceleration must occur at either the beginning
1325 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
1326 // conservative than that, but that's okay.
1327 if (segments == 0) {
1328 float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1329 float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1331 float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1334 if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1335 segments = int(1/h);
1338 for(int j = 0; j <= segments; j++) {
1339 float t = (float)j / segments;
1340 x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1341 y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1343 if(j > 0 || first_point) {
1344 edge.append(x, y - h);
1349 if( edge.size() > 0 ) {
1351 float fader = faders[k];
1352 float v = fader < 0 ? 1 : 1-fader;
1353 glColor4f(v, v, v, v);
1354 int display_list = glGenLists(1);
1355 glNewList(display_list, GL_COMPILE);
1357 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1358 glBegin(GL_POLYGON);
1359 MaskCoord *c = &edge[0];
1360 for( int i=edge.size(); --i>=0; ++c )
1361 glVertex2f(c->x, c->y);
1364 GLUtesselator *tess = gluNewTess();
1365 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex3dv);
1366 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1367 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1368 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1369 ArrayList<double *> invented;
1370 invented.set_array_delete();
1372 gluTessBeginPolygon(tess, &invented);
1373 gluTessBeginContour(tess);
1374 MaskCoord *c = &edge[0];
1375 for( int i=edge.size(); --i>=0; ++c )
1376 gluTessVertex(tess, (GLdouble *)c, c);
1377 gluTessEndContour(tess);
1378 gluTessEndPolygon(tess);
1379 gluDeleteTess(tess);
1380 invented.remove_all_objects();
1383 glCallList(display_list);
1384 glDeleteLists(1, display_list);
1389 fb_texture *in = new fb_texture(w, h, color_model);
1391 in->read_screen(0,0, w,h);
1392 fb_texture *out = new fb_texture(w, h, color_model);
1394 unsigned int frag_shader =
1395 VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1396 if( frag_shader > 0 ) {
1398 glGenBuffers(1, points);
1399 for(int k = 0; k < total_submasks; k++) {
1400 MaskEdge &edge = *edges[k];
1401 if( !edge.size() ) continue;
1402 if( !faders[k] ) continue;
1403 if( !feathers[k] ) continue;
1405 for( int i=0; i<edge.size(); ++i ) {
1406 MaskCoord &a = edge[i];
1407 MaskCoord &b = i<edge.size()-1 ? edge[i+1] : edge[0];
1408 draw_spots(spots, a.x,a.y+h, b.x,b.y+h);
1410 int sz = spots.size() * sizeof(MaskSpot);
1411 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, points[0], 0, sz);
1412 glBufferData(GL_SHADER_STORAGE_BUFFER, sz, &spots[0], GL_DYNAMIC_COPY);
1413 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1414 glUseProgram(frag_shader);
1415 float r = feathers[k], v = faders[k];
1416 glUniform1f(glGetUniformLocation(frag_shader, "r"), r);
1417 glUniform1f(glGetUniformLocation(frag_shader, "v"), v);
1419 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1420 out->set_output_texture();
1421 glViewport(0,0, w,h);
1422 out->draw_texture(0,0, w,h, 0,0, w,h);
1424 fb_texture *t = in; in = out; out = t;
1426 glDeleteBuffers(1, points);
1429 glDrawBuffers(0, 0);
1430 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1432 unsigned int shader = VFrame::make_shader(0, alpha_frag, 0);
1433 glUseProgram(shader);
1435 command->frame->bind_texture(0);
1436 in->BC_Texture::bind(1);
1437 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1438 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1439 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1440 (float)in->get_texture_w(),
1441 (float)in->get_texture_h());
1442 // if( BC_CModels::components(color_model ) == 4) {
1443 // glEnable(GL_BLEND);
1444 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1447 command->frame->draw_texture();
1448 command->frame->set_opengl_state(VFrame::SCREEN);
1453 glDisable(GL_TEXTURE_2D);
1454 glColor4f(1, 1, 1, 1);
1455 glActiveTexture(GL_TEXTURE0);
1456 window->enable_opengl();
1458 command->canvas->unlock_canvas();
1463 void Playback3D::convert_cmodel(Canvas *canvas,
1467 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1468 int src_cmodel = output->get_color_model();
1470 (output->get_opengl_state() == VFrame::TEXTURE ||
1471 output->get_opengl_state() == VFrame::SCREEN) &&
1472 // OpenGL has no floating point.
1473 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1474 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1475 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1476 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1477 // OpenGL sets alpha to 1 on import
1478 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1479 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1480 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1485 Playback3DCommand command;
1486 command.command = Playback3DCommand::CONVERT_CMODEL;
1487 command.canvas = canvas;
1488 command.frame = output;
1489 command.dst_cmodel = dst_cmodel;
1490 send_command(&command);
1493 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1496 BC_WindowBase *window =
1497 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1499 window->enable_opengl();
1501 // Import into hardware
1502 command->frame->enable_opengl();
1503 command->frame->init_screen();
1504 command->frame->to_texture();
1506 // Colormodel permutation
1507 int src_cmodel = command->frame->get_color_model();
1508 int dst_cmodel = command->dst_cmodel;
1512 } cmodel_shader_table_t;
1513 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1515 static cmodel_shader_table_t cmodel_shader_table[] = {
1516 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1517 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1518 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1519 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1520 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1521 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1522 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1523 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1524 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1525 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1526 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1527 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1528 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1529 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1530 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1531 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1532 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1533 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1534 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1535 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1536 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1539 const char *shader = 0;
1540 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1541 for( int i=0; i<table_size; ++i ) {
1542 if( cmodel_shader_table[i].src == src_cmodel &&
1543 cmodel_shader_table[i].dst == dst_cmodel ) {
1544 shader = cmodel_shader_table[i].shader;
1545 type = cmodel_shader_table[i].typ;
1550 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1552 // command->frame->get_color_model(),
1553 // command->dst_cmodel,
1556 const char *shader_stack[9];
1557 memset(shader_stack,0, sizeof(shader_stack));
1558 int current_shader = 0;
1561 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1562 shader_stack[current_shader++] = shader;
1563 shader_stack[current_shader] = 0;
1564 unsigned int shader_id = VFrame::make_shader(shader_stack);
1566 command->frame->bind_texture(0);
1567 glUseProgram(shader_id);
1569 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1572 BC_GL_RGB_TO_YUV(shader_id);
1575 BC_GL_YUV_TO_RGB(shader_id);
1579 command->frame->draw_texture();
1580 if(shader) glUseProgram(0);
1581 command->frame->set_opengl_state(VFrame::SCREEN);
1585 command->canvas->unlock_canvas();
1589 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1591 Playback3DCommand command;
1592 command.command = Playback3DCommand::DO_FADE;
1593 command.canvas = canvas;
1594 command.frame = frame;
1595 command.alpha = fade;
1596 send_command(&command);
1599 void Playback3D::do_fade_sync(Playback3DCommand *command)
1602 BC_WindowBase *window =
1603 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1605 window->enable_opengl();
1606 switch( command->frame->get_opengl_state() ) {
1608 command->frame->to_texture();
1611 case VFrame::SCREEN:
1612 // Read back from PBuffer
1613 // Bind context to pbuffer
1614 command->frame->enable_opengl();
1615 command->frame->screen_to_texture();
1619 command->frame->enable_opengl();
1620 command->frame->init_screen();
1621 command->frame->bind_texture(0);
1623 // glClearColor(0.0, 0.0, 0.0, 0.0);
1624 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1625 glDisable(GL_BLEND);
1626 unsigned int frag_shader = 0;
1627 switch(command->frame->get_color_model())
1629 // For the alpha colormodels, the native function seems to multiply the
1630 // components by the alpha instead of just the alpha.
1634 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1639 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1640 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1645 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1651 glUseProgram(frag_shader);
1653 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1654 glUniform1i(variable, 0);
1655 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1656 glUniform1f(variable, command->alpha);
1659 command->frame->draw_texture();
1660 command->frame->set_opengl_state(VFrame::SCREEN);
1667 glColor4f(1, 1, 1, 1);
1668 glDisable(GL_BLEND);
1670 command->canvas->unlock_canvas();
1675 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1677 Playback3DCommand command;
1678 command.command = Playback3DCommand::PLUGIN;
1679 command.canvas = canvas;
1680 command.plugin_client = client;
1681 return send_command(&command);
1684 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1686 BC_WindowBase *window =
1687 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1689 window->enable_opengl();
1690 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1692 command->canvas->unlock_canvas();