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 *multiply_mask4_frag =
263 "uniform sampler2D tex;\n"
264 "uniform sampler2D tex1;\n"
265 "uniform float scale;\n"
268 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
269 " gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
272 static const char *multiply_mask3_frag =
273 "uniform sampler2D tex;\n"
274 "uniform sampler2D tex1;\n"
275 "uniform float scale;\n"
276 "uniform bool is_yuv;\n"
279 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
280 " float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
281 " gl_FragColor.rgb *= vec3(a, a, a);\n"
284 static const char *multiply_yuvmask3_frag =
285 "uniform sampler2D tex;\n"
286 "uniform sampler2D tex1;\n"
287 "uniform float scale;\n"
290 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
291 " float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
292 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
293 " gl_FragColor.rgb *= vec3(a, a, a);\n"
294 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
297 static const char *fade_rgba_frag =
298 "uniform sampler2D tex;\n"
299 "uniform float alpha;\n"
302 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
303 " gl_FragColor.a *= alpha;\n"
306 static const char *fade_yuv_frag =
307 "uniform sampler2D tex;\n"
308 "uniform float alpha;\n"
311 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
312 " gl_FragColor.r *= alpha;\n"
313 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
314 " gl_FragColor.g *= alpha;\n"
315 " gl_FragColor.b *= alpha;\n"
316 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
322 Playback3DCommand::Playback3DCommand()
323 : BC_SynchronousCommand()
329 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
331 Playback3DCommand *ptr = (Playback3DCommand*)command;
332 this->canvas = ptr->canvas;
333 this->is_cleared = ptr->is_cleared;
335 this->in_x1 = ptr->in_x1;
336 this->in_y1 = ptr->in_y1;
337 this->in_x2 = ptr->in_x2;
338 this->in_y2 = ptr->in_y2;
339 this->out_x1 = ptr->out_x1;
340 this->out_y1 = ptr->out_y1;
341 this->out_x2 = ptr->out_x2;
342 this->out_y2 = ptr->out_y2;
343 this->alpha = ptr->alpha;
344 this->mode = ptr->mode;
345 this->interpolation_type = ptr->interpolation_type;
347 this->input = ptr->input;
348 this->start_position_project = ptr->start_position_project;
349 this->keyframe_set = ptr->keyframe_set;
350 this->keyframe = ptr->keyframe;
351 this->default_auto = ptr->default_auto;
352 this->plugin_client = ptr->plugin_client;
353 this->want_texture = ptr->want_texture;
354 this->is_nested = ptr->is_nested;
355 this->dst_cmodel = ptr->dst_cmodel;
357 BC_SynchronousCommand::copy_from(command);
361 ///static void glDebugCallback(GLenum src, GLenum typ, GLuint id,
362 /// GLenum svy, GLsizei len, const GLchar* msg, void* dat)
363 //static void glDebugCallback(unsigned int src, unsigned int typ, unsigned int id,
364 // unsigned int svy, int len, const char* msg, const void* dat)
366 // printf("glDebug: %d:%d; %d/%d %s\n",src,typ,id,svy,msg);
370 Playback3D::Playback3D(MWindow *mwindow)
373 this->mwindow = mwindow;
375 //Enabling OpenGL debug output on nVidia drivers
376 // glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
377 // glEnable(GL_DEBUG_OUTPUT);
378 // glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
379 // glDebugMessageCallback(glDebugCallback, 0);
382 Playback3D::~Playback3D()
389 BC_SynchronousCommand* Playback3D::new_command()
391 return new Playback3DCommand;
396 void Playback3D::handle_command(BC_SynchronousCommand *command)
398 //printf("Playback3D::handle_command 1 %d\n", command->command);
399 switch(command->command)
401 case Playback3DCommand::WRITE_BUFFER:
402 write_buffer_sync((Playback3DCommand*)command);
405 case Playback3DCommand::CLEAR_OUTPUT:
406 clear_output_sync((Playback3DCommand*)command);
409 case Playback3DCommand::CLEAR_INPUT:
410 clear_input_sync((Playback3DCommand*)command);
413 case Playback3DCommand::DO_CAMERA:
414 do_camera_sync((Playback3DCommand*)command);
417 case Playback3DCommand::OVERLAY:
418 overlay_sync((Playback3DCommand*)command);
421 case Playback3DCommand::DO_FADE:
422 do_fade_sync((Playback3DCommand*)command);
425 case Playback3DCommand::DO_MASK:
426 do_mask_sync((Playback3DCommand*)command);
429 case Playback3DCommand::PLUGIN:
430 run_plugin_sync((Playback3DCommand*)command);
433 case Playback3DCommand::COPY_FROM:
434 copy_from_sync((Playback3DCommand*)command);
437 case Playback3DCommand::CONVERT_CMODEL:
438 convert_cmodel_sync((Playback3DCommand*)command);
441 // case Playback3DCommand::DRAW_REFRESH:
442 // draw_refresh_sync((Playback3DCommand*)command);
445 //printf("Playback3D::handle_command 10\n");
451 void Playback3D::copy_from(Canvas *canvas,
456 Playback3DCommand command;
457 command.command = Playback3DCommand::COPY_FROM;
458 command.canvas = canvas;
461 command.want_texture = want_texture;
462 send_command(&command);
465 void Playback3D::copy_from_sync(Playback3DCommand *command)
468 command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
469 BC_WindowBase *window = command->canvas->get_canvas();
472 window->lock_window("Playback3D:draw_refresh_sync");
473 window->enable_opengl();
474 int w = command->input->get_w();
475 int h = command->input->get_h();
477 if(command->input->get_opengl_state() == VFrame::SCREEN &&
478 w == command->frame->get_w() && h == command->frame->get_h())
480 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
481 // command->input->get_w(),
482 // command->input->get_h(),
483 // command->frame->get_w(),
484 // command->frame->get_h(),
485 // command->frame->get_color_model());
486 // With NVidia at least,
489 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
493 if(command->want_texture)
495 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
496 // Screen_to_texture requires the source pbuffer enabled.
497 command->input->enable_opengl();
498 command->frame->screen_to_texture();
499 command->frame->set_opengl_state(VFrame::TEXTURE);
504 command->input->to_texture();
505 command->input->bind_texture(0);
506 command->frame->enable_opengl();
507 command->frame->init_screen();
508 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
509 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
511 glUseProgram(shader);
512 int variable = glGetUniformLocation(shader, "tex");
513 glUniform1i(variable, 0);
514 BC_GL_YUV_TO_RGB(shader);
518 command->input->draw_texture(1);
519 command->frame->screen_to_ram();
525 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
526 command->input->get_opengl_state(), w, h,
527 command->frame->get_w(), command->frame->get_h());
530 window->unlock_window();
532 command->canvas->unlock_canvas();
539 // void Playback3D::draw_refresh(Canvas *canvas,
550 // Playback3DCommand command;
551 // command.command = Playback3DCommand::DRAW_REFRESH;
552 // command.canvas = canvas;
553 // command.frame = frame;
554 // command.in_x1 = in_x1;
555 // command.in_y1 = in_y1;
556 // command.in_x2 = in_x2;
557 // command.in_y2 = in_y2;
558 // command.out_x1 = out_x1;
559 // command.out_y1 = out_y1;
560 // command.out_x2 = out_x2;
561 // command.out_y2 = out_y2;
562 // send_command(&command);
565 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
567 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
568 // BC_WindowBase *window = command->canvas->get_canvas();
571 // window->lock_window("Playback3D:draw_refresh_sync");
572 // window->enable_opengl();
574 // // Read output pbuffer back to RAM in project colormodel
575 // // RGB 8bit is fastest for OpenGL to read back.
576 // command->frame->reallocate(0,
580 // command->frame->get_w(),
581 // command->frame->get_h(),
584 // command->frame->screen_to_ram();
586 // window->clear_box(0,
590 // window->draw_vframe(command->frame,
591 // (int)command->out_x1,
592 // (int)command->out_y1,
593 // (int)(command->out_x2 - command->out_x1),
594 // (int)(command->out_y2 - command->out_y1),
595 // (int)command->in_x1,
596 // (int)command->in_y1,
597 // (int)(command->in_x2 - command->in_x1),
598 // (int)(command->in_y2 - command->in_y1),
601 // window->unlock_window();
603 // command->canvas->unlock_canvas();
610 void Playback3D::write_buffer(Canvas *canvas,
622 Playback3DCommand command;
623 command.command = Playback3DCommand::WRITE_BUFFER;
624 command.canvas = canvas;
625 command.frame = frame;
626 command.in_x1 = in_x1;
627 command.in_y1 = in_y1;
628 command.in_x2 = in_x2;
629 command.in_y2 = in_y2;
630 command.out_x1 = out_x1;
631 command.out_y1 = out_y1;
632 command.out_x2 = out_x2;
633 command.out_y2 = out_y2;
634 command.is_cleared = is_cleared;
635 send_command(&command);
639 void Playback3D::write_buffer_sync(Playback3DCommand *command)
641 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
642 if(command->canvas->get_canvas())
644 BC_WindowBase *window = command->canvas->get_canvas();
645 window->lock_window("Playback3D::write_buffer_sync");
646 // Update hidden cursor
647 window->update_video_cursor();
648 // Make sure OpenGL is enabled first.
649 window->enable_opengl();
651 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
652 int flip_y = 0, frame_state = command->frame->get_opengl_state();
653 switch( frame_state ) {
654 // Upload texture and composite to screen
658 command->frame->to_texture();
659 window->enable_opengl();
660 // Composite texture to screen and swap buffer
661 case VFrame::TEXTURE:
663 int fh1 = command->frame->get_h()-1;
664 float in_y1 = fh1 - command->in_y1;
665 float in_y2 = fh1 - command->in_y2;
666 command->in_y1 = in_y2;
667 command->in_y2 = in_y1;
669 draw_output(command, flip_y);
672 printf("Playback3D::write_buffer_sync unknown state\n");
675 command->frame->set_opengl_state(frame_state);
676 window->unlock_window();
679 command->canvas->unlock_canvas();
684 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
687 int texture_id = command->frame->get_texture_id();
688 BC_WindowBase *window = command->canvas->get_canvas();
690 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
692 // command->canvas->get_canvas());
697 // If virtual console is being used, everything in this function has
698 // already been done except the page flip.
701 canvas_w = window->get_w();
702 canvas_h = window->get_h();
703 VFrame::init_screen(canvas_w, canvas_h);
704 int color_model = command->frame->get_color_model();
705 int is_yuv = BC_CModels::is_yuv(color_model);
707 if(!command->is_cleared)
709 // If we get here, the virtual console was not used.
710 init_frame(command, 0);
714 // Undo any previous shader settings
715 command->frame->bind_texture(0);
717 // Convert colormodel
718 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
720 glUseProgram(shader);
721 // Set texture unit of the texture
722 int variable = glGetUniformLocation(shader, "tex");
723 glUniform1i(variable, 0);
724 BC_GL_YUV_TO_RGB(shader);
727 // if(BC_CModels::components(color_model) == 4)
729 // glEnable(GL_BLEND);
730 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
733 command->frame->draw_texture(
734 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
735 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
739 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
751 command->canvas->get_canvas()->flip_opengl();
758 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
761 float gbuv = is_yuv ? 0.5 : 0.0;
762 glClearColor(0.0, gbuv, gbuv, 0.0);
763 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
768 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
770 Playback3DCommand command;
771 command.command = Playback3DCommand::CLEAR_OUTPUT;
772 command.canvas = canvas;
773 command.frame = output;
774 send_command(&command);
777 void Playback3D::clear_output_sync(Playback3DCommand *command)
779 command->canvas->lock_canvas("Playback3D::clear_output_sync");
780 if(command->canvas->get_canvas())
782 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
783 // If we get here, the virtual console is being used.
784 command->canvas->get_canvas()->enable_opengl();
787 // Using pbuffer for refresh frame.
790 command->frame->enable_opengl();
791 int color_model = command->canvas->mwindow->edl->session->color_model;
792 is_yuv = BC_CModels::is_yuv(color_model);
795 init_frame(command, is_yuv);
796 command->canvas->get_canvas()->unlock_window();
798 command->canvas->unlock_canvas();
802 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
804 Playback3DCommand command;
805 command.command = Playback3DCommand::CLEAR_INPUT;
806 command.canvas = canvas;
807 command.frame = frame;
808 send_command(&command);
811 void Playback3D::clear_input_sync(Playback3DCommand *command)
813 command->canvas->lock_canvas("Playback3D::clear_output_sync");
814 if(command->canvas->get_canvas())
816 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
817 command->canvas->get_canvas()->enable_opengl();
818 command->frame->enable_opengl();
819 command->frame->clear_pbuffer();
820 command->frame->set_opengl_state(VFrame::SCREEN);
821 command->canvas->get_canvas()->unlock_window();
823 command->canvas->unlock_canvas();
826 void Playback3D::do_camera(Canvas *canvas,
838 Playback3DCommand command;
839 command.command = Playback3DCommand::DO_CAMERA;
840 command.canvas = canvas;
841 command.input = input;
842 command.frame = output;
843 command.in_x1 = in_x1;
844 command.in_y1 = in_y1;
845 command.in_x2 = in_x2;
846 command.in_y2 = in_y2;
847 command.out_x1 = out_x1;
848 command.out_y1 = out_y1;
849 command.out_x2 = out_x2;
850 command.out_y2 = out_y2;
851 send_command(&command);
854 void Playback3D::do_camera_sync(Playback3DCommand *command)
856 command->canvas->lock_canvas("Playback3D::do_camera_sync");
857 if(command->canvas->get_canvas())
859 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
860 command->canvas->get_canvas()->enable_opengl();
862 command->input->to_texture();
863 command->frame->enable_opengl();
864 command->frame->init_screen();
865 command->frame->clear_pbuffer();
867 command->input->bind_texture(0);
868 // Must call draw_texture in input frame to get the texture coordinates right.
870 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
876 // (float)command->input->get_h() - command->out_y1,
878 // (float)command->input->get_h() - command->out_y2);
879 command->input->draw_texture(
880 command->in_x1, command->in_y2,
881 command->in_x2, command->in_y1,
883 (float)command->frame->get_h() - command->out_y1,
885 (float)command->frame->get_h() - command->out_y2);
888 command->frame->set_opengl_state(VFrame::SCREEN);
889 command->frame->screen_to_ram();
890 command->canvas->get_canvas()->unlock_window();
892 command->canvas->unlock_canvas();
895 void Playback3D::overlay(Canvas *canvas, VFrame *input,
896 float in_x1, float in_y1, float in_x2, float in_y2,
897 float out_x1, float out_y1, float out_x2, float out_y2,
898 float alpha, int mode, int interpolation_type,
899 VFrame *output, int is_nested)
901 Playback3DCommand command;
902 command.command = Playback3DCommand::OVERLAY;
903 command.canvas = canvas;
904 command.frame = output;
905 command.input = input;
906 command.in_x1 = in_x1;
907 command.in_y1 = in_y1;
908 command.in_x2 = in_x2;
909 command.in_y2 = in_y2;
910 command.out_x1 = out_x1;
911 command.out_y1 = out_y1;
912 command.out_x2 = out_x2;
913 command.out_y2 = out_y2;
914 command.alpha = alpha;
916 command.interpolation_type = interpolation_type;
917 command.is_nested = is_nested;
918 send_command(&command);
921 void Playback3D::overlay_sync(Playback3DCommand *command)
924 // To do these operations, we need to copy the input buffer to a texture
925 // and blend 2 textures in a shader
926 static const char * const overlay_shaders[TRANSFER_TYPES] = {
927 blend_NORMAL_frag, // TRANSFER_NORMAL
928 blend_ADDITION_frag, // TRANSFER_ADDITION
929 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
930 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
931 blend_DIVIDE_frag, // TRANSFER_DIVIDE
932 blend_REPLACE_frag, // TRANSFER_REPLACE
933 blend_MAX_frag, // TRANSFER_MAX
934 blend_MIN_frag, // TRANSFER_MIN
935 blend_DARKEN_frag, // TRANSFER_DARKEN
936 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
937 blend_DST_frag, // TRANSFER_DST
938 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
939 blend_DST_IN_frag, // TRANSFER_DST_IN
940 blend_DST_OUT_frag, // TRANSFER_DST_OUT
941 blend_DST_OVER_frag, // TRANSFER_DST_OVER
942 blend_SRC_frag, // TRANSFER_SRC
943 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
944 blend_SRC_IN_frag, // TRANSFER_SRC_IN
945 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
946 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
947 blend_AND_frag, // TRANSFER_AND
948 blend_OR_frag, // TRANSFER_OR
949 blend_XOR_frag, // TRANSFER_XOR
950 blend_OVERLAY_frag, // TRANSFER_OVERLAY
951 blend_SCREEN_frag, // TRANSFER_SCREEN
952 blend_BURN_frag, // TRANSFER_BURN
953 blend_DODGE_frag, // TRANSFER_DODGE
954 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
955 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
956 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
959 command->canvas->lock_canvas("Playback3D::overlay_sync");
960 if(command->canvas->get_canvas()) {
961 BC_WindowBase *window = command->canvas->get_canvas();
962 window->lock_window("Playback3D::overlay_sync");
963 // Make sure OpenGL is enabled first.
964 window->enable_opengl();
965 window->update_video_cursor();
967 glColor4f(1, 1, 1, 1);
971 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
972 switch( command->input->get_opengl_state() ) {
973 // Upload texture and composite to screen
975 command->input->to_texture();
977 // Just composite texture to screen
978 case VFrame::TEXTURE:
980 // read from PBuffer to texture, then composite texture to screen
982 command->input->enable_opengl();
983 command->input->screen_to_texture();
986 printf("Playback3D::overlay_sync unknown state\n");
992 command->frame->enable_opengl();
993 command->frame->set_opengl_state(VFrame::SCREEN);
994 canvas_w = command->frame->get_w();
995 canvas_h = command->frame->get_h();
999 window->enable_opengl();
1000 canvas_w = window->get_w();
1001 canvas_h = window->get_h();
1005 const char *shader_stack[16];
1006 memset(shader_stack,0, sizeof(shader_stack));
1007 int total_shaders = 0, need_matrix = 0;
1009 VFrame::init_screen(canvas_w, canvas_h);
1012 command->input->bind_texture(0);
1014 // Convert colormodel to RGB if not nested.
1015 // The color model setting in the output frame is ignored.
1016 // if( command->is_nested <= 0 && // not nested
1017 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
1019 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
1023 #define add_shader(s) \
1024 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1025 shader_stack[total_shaders++] = s
1027 switch(command->mode) {
1028 case TRANSFER_REPLACE:
1029 // This requires overlaying an alpha multiplied image on a black screen.
1030 if( command->input->get_texture_components() != 4 ) break;
1031 add_shader(overlay_shaders[command->mode]);
1034 enable_overlay_texture(command);
1035 add_shader(overlay_shaders[command->mode]);
1039 // if to flatten alpha
1040 // if( command->is_nested < 0 ) {
1041 // switch(command->input->get_color_model()) {
1042 //// yuv has already been converted to rgb
1043 // case BC_YUVA8888:
1044 // case BC_RGBA_FLOAT:
1045 // case BC_RGBA8888:
1046 // add_shader(rgba_to_rgb_flatten);
1053 unsigned int shader = !shader_stack[0] ? 0 :
1054 VFrame::make_shader(shader_stack);
1056 glUseProgram(shader);
1057 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1058 // Set texture unit of the texture
1059 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1060 // Set texture unit of the temp texture
1061 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1063 int variable = glGetUniformLocation(shader, "alpha");
1064 glUniform1f(variable, command->alpha);
1065 // Set dimensions of the temp texture
1067 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1068 (float)temp_texture->get_texture_w(),
1069 (float)temp_texture->get_texture_h());
1075 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1076 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1077 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1079 command->input->draw_texture(
1080 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1081 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1082 !command->is_nested);
1085 // Delete temp texture
1087 delete temp_texture;
1089 glActiveTexture(GL_TEXTURE1);
1090 glDisable(GL_TEXTURE_2D);
1092 glActiveTexture(GL_TEXTURE0);
1093 glDisable(GL_TEXTURE_2D);
1095 window->unlock_window();
1097 command->canvas->unlock_canvas();
1101 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1104 glDisable(GL_BLEND);
1106 glActiveTexture(GL_TEXTURE1);
1107 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1108 command->input->get_color_model());
1109 temp_texture->bind(1);
1111 // Read canvas into texture
1112 glReadBuffer(GL_BACK);
1113 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1118 void Playback3D::do_mask(Canvas *canvas,
1120 int64_t start_position_project,
1121 MaskAutos *keyframe_set,
1123 MaskAuto *default_auto)
1125 Playback3DCommand command;
1126 command.command = Playback3DCommand::DO_MASK;
1127 command.canvas = canvas;
1128 command.frame = output;
1129 command.start_position_project = start_position_project;
1130 command.keyframe_set = keyframe_set;
1131 command.keyframe = keyframe;
1132 command.default_auto = default_auto;
1134 send_command(&command);
1140 struct Vertex : ListItem<Vertex>
1144 // this list is only used from the main thread, no locking needed
1145 // this must be a list so that pointers to allocated entries remain valid
1146 // when new entries are added
1147 static List<Vertex> *vertex_cache = 0;
1149 static void combine_callback(GLdouble coords[3],
1150 GLdouble *vertex_data[4],
1154 // can't use malloc here; GLU doesn't delete the memory for us!
1155 Vertex* vertex = vertex_cache->append();
1156 vertex->c[0] = coords[0];
1157 vertex->c[1] = coords[1];
1158 vertex->c[2] = coords[2];
1159 // we don't need to interpolate anything
1161 *dataOut = &vertex->c[0];
1166 void Playback3D::do_mask_sync(Playback3DCommand *command)
1169 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1170 if(command->canvas->get_canvas())
1172 BC_WindowBase *window = command->canvas->get_canvas();
1173 window->lock_window("Playback3D::do_mask_sync");
1174 window->enable_opengl();
1176 switch(command->frame->get_opengl_state())
1179 // Time to upload to the texture
1180 command->frame->to_texture();
1183 case VFrame::SCREEN:
1184 // Read back from PBuffer
1185 // Bind context to pbuffer
1186 command->frame->enable_opengl();
1187 command->frame->screen_to_texture();
1193 // Create PBuffer and draw the mask on it
1194 command->frame->enable_opengl();
1196 // Initialize coordinate system
1197 int w = command->frame->get_w();
1198 int h = command->frame->get_h();
1199 command->frame->init_screen();
1202 glDisable(GL_TEXTURE_2D);
1203 if(command->default_auto->mode == MASK_MULTIPLY_ALPHA)
1205 glClearColor(0.0, 0.0, 0.0, 0.0);
1206 glColor4f((float)command->keyframe->value / 100,
1207 (float)command->keyframe->value / 100,
1208 (float)command->keyframe->value / 100,
1213 glClearColor(1.0, 1.0, 1.0, 1.0);
1214 glColor4f((float)1.0 - (float)command->keyframe->value / 100,
1215 (float)1.0 - (float)command->keyframe->value / 100,
1216 (float)1.0 - (float)command->keyframe->value / 100,
1219 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1222 // Draw mask with scaling to simulate feathering
1223 GLUtesselator *tesselator = gluNewTess();
1224 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
1225 gluTessCallback(tesselator, GLU_TESS_VERTEX, (GLvoid (*) ( )) &glVertex3dv);
1226 gluTessCallback(tesselator, GLU_TESS_BEGIN, (GLvoid (*) ( )) &glBegin);
1227 gluTessCallback(tesselator, GLU_TESS_END, (GLvoid (*) ( )) &glEnd);
1228 gluTessCallback(tesselator, GLU_TESS_COMBINE, (GLvoid (*) ( ))&combine_callback);
1230 vertex_cache = new List<Vertex>;
1233 // Draw every submask as a new polygon
1234 int total_submasks = command->keyframe_set->total_submasks(
1235 command->start_position_project,
1237 float scale = command->keyframe->feather + 1;
1238 int display_list = glGenLists(1);
1239 glNewList(display_list, GL_COMPILE);
1240 for(int k = 0; k < total_submasks; k++)
1242 gluTessBeginPolygon(tesselator, NULL);
1243 gluTessBeginContour(tesselator);
1244 ArrayList<MaskPoint*> *points = new ArrayList<MaskPoint*>;
1245 command->keyframe_set->get_points(points,
1247 command->start_position_project,
1250 int first_point = 0;
1251 // Need to tabulate every vertex in persistent memory because
1252 // gluTessVertex doesn't copy them.
1253 ArrayList<GLdouble*> coords;
1254 coords.set_array_delete();
1255 for(int i = 0; i < points->total; i++)
1257 MaskPoint *point1 = points->values[i];
1258 MaskPoint *point2 = (i >= points->total - 1) ?
1260 points->values[i + 1];
1264 if(point1->control_x2 == 0 &&
1265 point1->control_y2 == 0 &&
1266 point2->control_x1 == 0 &&
1267 point2->control_y1 == 0)
1270 float x0 = point1->x;
1271 float y0 = point1->y;
1272 float x1 = point1->x + point1->control_x2;
1273 float y1 = point1->y + point1->control_y2;
1274 float x2 = point2->x + point2->control_x1;
1275 float y2 = point2->y + point2->control_y1;
1276 float x3 = point2->x;
1277 float y3 = point2->y;
1279 // forward differencing bezier curves implementation taken from GPL code at
1280 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1282 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1289 cx3 = - x0 + 3*x1 - 3*x2 + x3;
1290 cx2 = 3*x0 - 6*x1 + 3*x2;
1294 cy3 = - y0 + 3*y1 - 3*y2 + y3;
1295 cy2 = 3*y0 - 6*y1 + 3*y2;
1299 // This equation is from Graphics Gems I.
1301 // The idea is that since we're approximating a cubic curve with lines,
1302 // any error we incur is due to the curvature of the line, which we can
1303 // estimate by calculating the maximum acceleration of the curve. For
1304 // a cubic, the acceleration (second derivative) is a line, meaning that
1305 // the absolute maximum acceleration must occur at either the beginning
1306 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
1307 // conservative than that, but that's okay.
1310 float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1311 float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1313 float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1316 if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1317 segments = int(1/h);
1320 for(int j = 0; j <= segments; j++)
1322 float t = (float)j / segments;
1323 x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1324 y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1326 if(j > 0 || first_point)
1328 GLdouble *coord = new GLdouble[3];
1329 coord[0] = x / scale;
1330 coord[1] = -h + y / scale;
1332 coords.append(coord);
1338 // Now that we know the total vertices, send them to GLU
1339 for(int i = 0; i < coords.total; i++)
1340 gluTessVertex(tesselator, coords.values[i], coords.values[i]);
1342 gluTessEndContour(tesselator);
1343 gluTessEndPolygon(tesselator);
1344 points->remove_all_objects();
1346 coords.remove_all_objects();
1349 glCallList(display_list);
1350 glDeleteLists(display_list, 1);
1351 gluDeleteTess(tesselator);
1353 delete vertex_cache;
1356 glColor4f(1, 1, 1, 1);
1359 // Read mask into temporary texture.
1360 // For feathering, just read the part of the screen after the downscaling.
1363 float w_scaled = w / scale;
1364 float h_scaled = h / scale;
1365 // Don't vary the texture size according to scaling because that
1366 // would waste memory.
1367 // This enables and binds the temporary texture.
1368 glActiveTexture(GL_TEXTURE1);
1369 BC_Texture::new_texture(&temp_texture,
1372 command->frame->get_color_model());
1373 temp_texture->bind(1);
1374 glReadBuffer(GL_BACK);
1376 // Need to add extra size to fill in the bottom right
1377 glCopyTexSubImage2D(GL_TEXTURE_2D,
1383 (int)MIN(w_scaled + 2, w),
1384 (int)MIN(h_scaled + 2, h));
1386 command->frame->bind_texture(0);
1389 // For feathered masks, use a shader to multiply.
1390 // For unfeathered masks, we could use a stencil buffer
1391 // for further optimization but we also need a YUV algorithm.
1392 unsigned int frag_shader = 0;
1393 switch(temp_texture->get_texture_components()) {
1395 frag_shader = VFrame::make_shader(0,
1396 command->frame->get_color_model() == BC_YUV888 ?
1397 multiply_yuvmask3_frag : multiply_mask3_frag,
1401 frag_shader = VFrame::make_shader(0, multiply_mask4_frag, 0);
1407 glUseProgram(frag_shader);
1408 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1409 glUniform1i(variable, 0);
1410 if((variable = glGetUniformLocation(frag_shader, "tex1")) >= 0)
1411 glUniform1i(variable, 1);
1412 if((variable = glGetUniformLocation(frag_shader, "scale")) >= 0)
1413 glUniform1f(variable, scale);
1418 // Write texture to PBuffer with multiply and scaling for feather.
1421 command->frame->draw_texture(0, 0, w, h, 0, 0, w, h);
1422 command->frame->set_opengl_state(VFrame::SCREEN);
1425 // Disable temp texture
1428 glActiveTexture(GL_TEXTURE1);
1429 glDisable(GL_TEXTURE_2D);
1430 delete temp_texture;
1433 glActiveTexture(GL_TEXTURE0);
1434 glDisable(GL_TEXTURE_2D);
1437 window->enable_opengl();
1438 window->unlock_window();
1440 command->canvas->unlock_canvas();
1453 void Playback3D::convert_cmodel(Canvas *canvas,
1457 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1458 int src_cmodel = output->get_color_model();
1460 (output->get_opengl_state() == VFrame::TEXTURE ||
1461 output->get_opengl_state() == VFrame::SCREEN) &&
1462 // OpenGL has no floating point.
1463 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1464 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1465 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1466 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1467 // OpenGL sets alpha to 1 on import
1468 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1469 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1470 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1475 Playback3DCommand command;
1476 command.command = Playback3DCommand::CONVERT_CMODEL;
1477 command.canvas = canvas;
1478 command.frame = output;
1479 command.dst_cmodel = dst_cmodel;
1480 send_command(&command);
1483 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1486 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1488 if( command->canvas->get_canvas() ) {
1489 BC_WindowBase *window = command->canvas->get_canvas();
1490 window->lock_window("Playback3D::convert_cmodel_sync");
1491 window->enable_opengl();
1493 // Import into hardware
1494 command->frame->enable_opengl();
1495 command->frame->init_screen();
1496 command->frame->to_texture();
1498 // Colormodel permutation
1499 int src_cmodel = command->frame->get_color_model();
1500 int dst_cmodel = command->dst_cmodel;
1504 } cmodel_shader_table_t;
1505 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1507 static cmodel_shader_table_t cmodel_shader_table[] = {
1508 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1509 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1510 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1511 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1512 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1513 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1514 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1515 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1516 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1517 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1518 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1519 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1520 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1521 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1522 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1523 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1524 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1525 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1526 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1527 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1528 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1531 const char *shader = 0;
1532 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1533 for( int i=0; i<table_size; ++i ) {
1534 if( cmodel_shader_table[i].src == src_cmodel &&
1535 cmodel_shader_table[i].dst == dst_cmodel ) {
1536 shader = cmodel_shader_table[i].shader;
1537 type = cmodel_shader_table[i].typ;
1542 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1544 // command->frame->get_color_model(),
1545 // command->dst_cmodel,
1548 const char *shader_stack[9];
1549 memset(shader_stack,0, sizeof(shader_stack));
1550 int current_shader = 0;
1553 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1554 shader_stack[current_shader++] = shader;
1555 shader_stack[current_shader] = 0;
1556 unsigned int shader_id = VFrame::make_shader(shader_stack);
1558 command->frame->bind_texture(0);
1559 glUseProgram(shader_id);
1561 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1564 BC_GL_RGB_TO_YUV(shader_id);
1567 BC_GL_YUV_TO_RGB(shader_id);
1571 command->frame->draw_texture();
1572 if(shader) glUseProgram(0);
1573 command->frame->set_opengl_state(VFrame::SCREEN);
1576 window->unlock_window();
1579 command->canvas->unlock_canvas();
1583 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1585 Playback3DCommand command;
1586 command.command = Playback3DCommand::DO_FADE;
1587 command.canvas = canvas;
1588 command.frame = frame;
1589 command.alpha = fade;
1590 send_command(&command);
1593 void Playback3D::do_fade_sync(Playback3DCommand *command)
1596 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1597 if(command->canvas->get_canvas())
1599 BC_WindowBase *window = command->canvas->get_canvas();
1600 window->lock_window("Playback3D::do_fade_sync");
1601 window->enable_opengl();
1603 switch(command->frame->get_opengl_state())
1606 command->frame->to_texture();
1609 case VFrame::SCREEN:
1610 // Read back from PBuffer
1611 // Bind context to pbuffer
1612 command->frame->enable_opengl();
1613 command->frame->screen_to_texture();
1618 command->frame->enable_opengl();
1619 command->frame->init_screen();
1620 command->frame->bind_texture(0);
1622 // glClearColor(0.0, 0.0, 0.0, 0.0);
1623 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1624 glDisable(GL_BLEND);
1625 unsigned int frag_shader = 0;
1626 switch(command->frame->get_color_model())
1628 // For the alpha colormodels, the native function seems to multiply the
1629 // components by the alpha instead of just the alpha.
1633 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1638 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1639 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1644 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1650 glUseProgram(frag_shader);
1652 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1653 glUniform1i(variable, 0);
1654 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1655 glUniform1f(variable, command->alpha);
1658 command->frame->draw_texture();
1659 command->frame->set_opengl_state(VFrame::SCREEN);
1666 glColor4f(1, 1, 1, 1);
1667 glDisable(GL_BLEND);
1669 window->unlock_window();
1671 command->canvas->unlock_canvas();
1685 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1687 Playback3DCommand command;
1688 command.command = Playback3DCommand::PLUGIN;
1689 command.canvas = canvas;
1690 command.plugin_client = client;
1691 return send_command(&command);
1694 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1696 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1697 if(command->canvas->get_canvas())
1699 BC_WindowBase *window = command->canvas->get_canvas();
1700 window->lock_window("Playback3D::run_plugin_sync");
1701 window->enable_opengl();
1703 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1705 window->unlock_window();
1707 command->canvas->unlock_canvas();