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
24 #include "bcsignals.h"
25 #include "bcwindowbase.h"
28 #include "condition.h"
29 #include "maskautos.h"
32 #include "overlayframe.inc"
33 #include "overlayframe.h"
34 #include "playback3d.h"
35 #include "pluginclient.h"
36 #include "pluginvclient.h"
37 #include "transportque.inc"
55 // These should be passed to VFrame::make_shader to construct shaders.
56 // Can't hard code sampler2D
60 static const char *yuv_to_rgb_frag =
61 "uniform sampler2D tex;\n"
64 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
65 " yuva.rgb -= vec3(0, 0.5, 0.5);\n"
66 " const mat3 yuv_to_rgb_matrix = mat3(\n"
68 " 0, -0.34414, 1.77200, \n"
69 " 1.40200, -0.71414, 0);\n"
70 " gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
73 static const char *yuva_to_yuv_frag =
74 "uniform sampler2D tex;\n"
77 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
78 " float a = yuva.a;\n"
79 " float anti_a = 1.0 - a;\n"
81 " yuva.g = yuva.g * a + 0.5 * anti_a;\n"
82 " yuva.b = yuva.b * a + 0.5 * anti_a;\n"
84 " gl_FragColor = yuva;\n"
87 static const char *yuva_to_rgb_frag =
88 "uniform sampler2D tex;\n"
91 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
92 " yuva.rgb -= vec3(0, 0.5, 0.5);\n"
93 " const mat3 yuv_to_rgb_matrix = mat3(\n"
95 " 0, -0.34414, 1.77200, \n"
96 " 1.40200, -0.71414, 0);\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"
107 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
108 " const mat3 rgb_to_yuv_matrix = mat3(\n"
109 " 0.29900, -0.16874, 0.50000, \n"
110 " 0.58700, -0.33126, -0.41869, \n"
111 " 0.11400, 0.50000, -0.08131);\n"
112 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
113 " rgba.rgb += vec3(0, 0.5, 0.5);\n"
114 " gl_FragColor = rgba;\n"
118 static const char *rgba_to_rgb_frag =
119 "uniform sampler2D tex;\n"
122 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
123 " rgba.rgb *= rgba.a;\n"
125 " gl_FragColor = rgba;\n"
128 static const char *rgba_to_yuv_frag =
129 "uniform sampler2D tex;\n"
132 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
133 " const mat3 rgb_to_yuv_matrix = mat3(\n"
134 " 0.29900, -0.16874, 0.50000, \n"
135 " 0.58700, -0.33126, -0.41869, \n"
136 " 0.11400, 0.50000, -0.08131);\n"
137 " rgba.rgb *= rgba.a;\n"
139 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
140 " rgba.rgb += vec3(0, 0.5, 0.5);\n"
141 " gl_FragColor = rgba;\n"
144 static const char *rgba_to_rgb_flatten =
146 " gl_FragColor.rgb *= gl_FragColor.a;\n"
147 " gl_FragColor.a = 1.0;\n"
150 #define GL_STD_BLEND(FN) \
151 static const char *blend_##FN##_frag = \
152 "uniform sampler2D tex2;\n" \
153 "uniform vec2 tex2_dimensions;\n" \
154 "uniform float alpha;\n" \
156 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
158 " result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
159 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
160 " gl_FragColor = mix(canvas, result, alpha);\n" \
163 #define GL_VEC_BLEND(FN) \
164 static const char *blend_##FN##_frag = \
165 "uniform sampler2D tex2;\n" \
166 "uniform vec2 tex2_dimensions;\n" \
167 "uniform float alpha;\n" \
169 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
171 " result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
172 " result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
173 " result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
174 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
175 " result = clamp(result, 0.0, 1.0);\n" \
176 " gl_FragColor = mix(canvas, result, alpha);\n" \
194 static const char *blend_NORMAL_frag =
195 "uniform sampler2D tex2;\n"
196 "uniform vec2 tex2_dimensions;\n"
197 "uniform float alpha;\n"
199 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
200 " vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
201 " gl_FragColor = mix(canvas, result, alpha);\n"
205 static const char *blend_REPLACE_frag =
206 "uniform float alpha;\n"
211 static const char *blend_ADDITION_frag =
212 "uniform sampler2D tex2;\n"
213 "uniform vec2 tex2_dimensions;\n"
214 "uniform float alpha;\n"
216 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
217 " vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
218 " gl_FragColor = mix(canvas, result, alpha);\n"
222 static const char *blend_SUBTRACT_frag =
223 "uniform sampler2D tex2;\n"
224 "uniform vec2 tex2_dimensions;\n"
225 "uniform float alpha;\n"
227 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
228 " vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
229 " gl_FragColor = mix(canvas, result, alpha);\n"
232 GL_STD_BLEND(MULTIPLY);
233 GL_VEC_BLEND(DIVIDE);
236 GL_VEC_BLEND(DARKEN);
237 GL_VEC_BLEND(LIGHTEN);
239 GL_STD_BLEND(DST_ATOP);
240 GL_STD_BLEND(DST_IN);
241 GL_STD_BLEND(DST_OUT);
242 GL_STD_BLEND(DST_OVER);
244 GL_STD_BLEND(SRC_ATOP);
245 GL_STD_BLEND(SRC_IN);
246 GL_STD_BLEND(SRC_OUT);
247 GL_STD_BLEND(SRC_OVER);
251 GL_VEC_BLEND(OVERLAY);
252 GL_STD_BLEND(SCREEN);
255 GL_VEC_BLEND(HARDLIGHT);
256 GL_VEC_BLEND(SOFTLIGHT);
257 GL_VEC_BLEND(DIFFERENCE);
259 static const char *read_texture_frag =
260 "uniform sampler2D tex;\n"
263 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
266 static const char *multiply_mask4_frag =
267 "uniform sampler2D tex;\n"
268 "uniform sampler2D tex1;\n"
269 "uniform float scale;\n"
272 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
273 " gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
276 static const char *multiply_mask3_frag =
277 "uniform sampler2D tex;\n"
278 "uniform sampler2D tex1;\n"
279 "uniform float scale;\n"
280 "uniform bool is_yuv;\n"
283 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
284 " float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
285 " gl_FragColor.rgb *= vec3(a, a, a);\n"
288 static const char *multiply_yuvmask3_frag =
289 "uniform sampler2D tex;\n"
290 "uniform sampler2D tex1;\n"
291 "uniform float scale;\n"
294 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
295 " float a = texture2D(tex1, gl_TexCoord[0].st / vec2(scale, scale)).r;\n"
296 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
297 " gl_FragColor.rgb *= vec3(a, a, a);\n"
298 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
301 static const char *fade_rgba_frag =
302 "uniform sampler2D tex;\n"
303 "uniform float alpha;\n"
306 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
307 " gl_FragColor.a *= alpha;\n"
310 static const char *fade_yuv_frag =
311 "uniform sampler2D tex;\n"
312 "uniform float alpha;\n"
315 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
316 " gl_FragColor.r *= alpha;\n"
317 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
318 " gl_FragColor.g *= alpha;\n"
319 " gl_FragColor.b *= alpha;\n"
320 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
326 Playback3DCommand::Playback3DCommand()
327 : BC_SynchronousCommand()
333 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
335 Playback3DCommand *ptr = (Playback3DCommand*)command;
336 this->canvas = ptr->canvas;
337 this->is_cleared = ptr->is_cleared;
339 this->in_x1 = ptr->in_x1;
340 this->in_y1 = ptr->in_y1;
341 this->in_x2 = ptr->in_x2;
342 this->in_y2 = ptr->in_y2;
343 this->out_x1 = ptr->out_x1;
344 this->out_y1 = ptr->out_y1;
345 this->out_x2 = ptr->out_x2;
346 this->out_y2 = ptr->out_y2;
347 this->alpha = ptr->alpha;
348 this->mode = ptr->mode;
349 this->interpolation_type = ptr->interpolation_type;
351 this->input = ptr->input;
352 this->start_position_project = ptr->start_position_project;
353 this->keyframe_set = ptr->keyframe_set;
354 this->keyframe = ptr->keyframe;
355 this->default_auto = ptr->default_auto;
356 this->plugin_client = ptr->plugin_client;
357 this->want_texture = ptr->want_texture;
358 this->is_nested = ptr->is_nested;
359 this->dst_cmodel = ptr->dst_cmodel;
361 BC_SynchronousCommand::copy_from(command);
365 ///static void glDebugCallback(GLenum src, GLenum typ, GLuint id,
366 /// GLenum svy, GLsizei len, const GLchar* msg, void* dat)
367 //static void glDebugCallback(unsigned int src, unsigned int typ, unsigned int id,
368 // unsigned int svy, int len, const char* msg, const void* dat)
370 // printf("glDebug: %d:%d; %d/%d %s\n",src,typ,id,svy,msg);
374 Playback3D::Playback3D(MWindow *mwindow)
377 this->mwindow = mwindow;
379 //Enabling OpenGL debug output on nVidia drivers
380 // glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
381 // glEnable(GL_DEBUG_OUTPUT);
382 // glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
383 // glDebugMessageCallback(glDebugCallback, 0);
386 Playback3D::~Playback3D()
393 BC_SynchronousCommand* Playback3D::new_command()
395 return new Playback3DCommand;
400 void Playback3D::handle_command(BC_SynchronousCommand *command)
402 //printf("Playback3D::handle_command 1 %d\n", command->command);
403 switch(command->command)
405 case Playback3DCommand::WRITE_BUFFER:
406 write_buffer_sync((Playback3DCommand*)command);
409 case Playback3DCommand::CLEAR_OUTPUT:
410 clear_output_sync((Playback3DCommand*)command);
413 case Playback3DCommand::CLEAR_INPUT:
414 clear_input_sync((Playback3DCommand*)command);
417 case Playback3DCommand::DO_CAMERA:
418 do_camera_sync((Playback3DCommand*)command);
421 case Playback3DCommand::OVERLAY:
422 overlay_sync((Playback3DCommand*)command);
425 case Playback3DCommand::DO_FADE:
426 do_fade_sync((Playback3DCommand*)command);
429 case Playback3DCommand::DO_MASK:
430 do_mask_sync((Playback3DCommand*)command);
433 case Playback3DCommand::PLUGIN:
434 run_plugin_sync((Playback3DCommand*)command);
437 case Playback3DCommand::COPY_FROM:
438 copy_from_sync((Playback3DCommand*)command);
441 case Playback3DCommand::CONVERT_CMODEL:
442 convert_cmodel_sync((Playback3DCommand*)command);
445 // case Playback3DCommand::DRAW_REFRESH:
446 // draw_refresh_sync((Playback3DCommand*)command);
449 //printf("Playback3D::handle_command 10\n");
455 void Playback3D::copy_from(Canvas *canvas,
460 Playback3DCommand command;
461 command.command = Playback3DCommand::COPY_FROM;
462 command.canvas = canvas;
465 command.want_texture = want_texture;
466 send_command(&command);
469 void Playback3D::copy_from_sync(Playback3DCommand *command)
472 command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
473 BC_WindowBase *window = command->canvas->get_canvas();
476 window->lock_window("Playback3D:draw_refresh_sync");
477 window->enable_opengl();
478 int w = command->input->get_w();
479 int h = command->input->get_h();
481 if(command->input->get_opengl_state() == VFrame::SCREEN &&
482 w == command->frame->get_w() && h == command->frame->get_h())
484 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
485 // command->input->get_w(),
486 // command->input->get_h(),
487 // command->frame->get_w(),
488 // command->frame->get_h(),
489 // command->frame->get_color_model());
490 // With NVidia at least,
493 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
497 if(command->want_texture)
499 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
500 // Screen_to_texture requires the source pbuffer enabled.
501 command->input->enable_opengl();
502 command->frame->screen_to_texture();
503 command->frame->set_opengl_state(VFrame::TEXTURE);
508 command->input->enable_opengl();
509 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
510 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE,
511 command->frame->get_rows()[0]);
512 command->frame->flip_vert();
513 command->frame->set_opengl_state(VFrame::RAM);
518 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
519 command->input->get_opengl_state(), w, h,
520 command->frame->get_w(), command->frame->get_h());
523 window->unlock_window();
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)
560 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
561 // BC_WindowBase *window = command->canvas->get_canvas();
564 // window->lock_window("Playback3D:draw_refresh_sync");
565 // window->enable_opengl();
567 // // Read output pbuffer back to RAM in project colormodel
568 // // RGB 8bit is fastest for OpenGL to read back.
569 // command->frame->reallocate(0,
573 // command->frame->get_w(),
574 // command->frame->get_h(),
577 // command->frame->screen_to_ram();
579 // window->clear_box(0,
583 // window->draw_vframe(command->frame,
584 // (int)command->out_x1,
585 // (int)command->out_y1,
586 // (int)(command->out_x2 - command->out_x1),
587 // (int)(command->out_y2 - command->out_y1),
588 // (int)command->in_x1,
589 // (int)command->in_y1,
590 // (int)(command->in_x2 - command->in_x1),
591 // (int)(command->in_y2 - command->in_y1),
594 // window->unlock_window();
596 // command->canvas->unlock_canvas();
603 void Playback3D::write_buffer(Canvas *canvas,
615 Playback3DCommand command;
616 command.command = Playback3DCommand::WRITE_BUFFER;
617 command.canvas = canvas;
618 command.frame = frame;
619 command.in_x1 = in_x1;
620 command.in_y1 = in_y1;
621 command.in_x2 = in_x2;
622 command.in_y2 = in_y2;
623 command.out_x1 = out_x1;
624 command.out_y1 = out_y1;
625 command.out_x2 = out_x2;
626 command.out_y2 = out_y2;
627 command.is_cleared = is_cleared;
628 send_command(&command);
632 void Playback3D::write_buffer_sync(Playback3DCommand *command)
634 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
635 if(command->canvas->get_canvas())
637 BC_WindowBase *window = command->canvas->get_canvas();
638 window->lock_window("Playback3D::write_buffer_sync");
639 // Update hidden cursor
640 window->update_video_cursor();
641 // Make sure OpenGL is enabled first.
642 window->enable_opengl();
645 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
646 switch(command->frame->get_opengl_state())
648 // Upload texture and composite to screen
650 command->frame->to_texture();
651 draw_output(command);
653 // Composite texture to screen and swap buffer
654 case VFrame::TEXTURE:
655 draw_output(command);
659 window->flip_opengl();
662 printf("Playback3D::write_buffer_sync unknown state\n");
665 window->unlock_window();
668 command->canvas->unlock_canvas();
673 void Playback3D::draw_output(Playback3DCommand *command)
676 int texture_id = command->frame->get_texture_id();
677 BC_WindowBase *window = command->canvas->get_canvas();
679 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
681 // command->canvas->get_canvas());
686 // If virtual console is being used, everything in this function has
687 // already been done except the page flip.
690 canvas_w = window->get_w();
691 canvas_h = window->get_h();
692 VFrame::init_screen(canvas_w, canvas_h);
694 if(!command->is_cleared)
696 // If we get here, the virtual console was not used.
701 // Undo any previous shader settings
702 command->frame->bind_texture(0);
707 // Convert colormodel
708 unsigned int frag_shader = 0;
709 switch(command->frame->get_color_model())
713 frag_shader = VFrame::make_shader(0,
722 glUseProgram(frag_shader);
723 int variable = glGetUniformLocation(frag_shader, "tex");
724 // Set texture unit of the texture
725 glUniform1i(variable, 0);
728 if(BC_CModels::components(command->frame->get_color_model()) == 4)
731 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
734 command->frame->draw_texture(
735 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
736 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
740 // printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
752 command->canvas->get_canvas()->flip_opengl();
759 void Playback3D::init_frame(Playback3DCommand *command)
762 canvas_w = command->canvas->get_canvas()->get_w();
763 canvas_h = command->canvas->get_canvas()->get_h();
765 glClearColor(0.0, 0.0, 0.0, 0.0);
766 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
771 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
773 Playback3DCommand command;
774 command.command = Playback3DCommand::CLEAR_OUTPUT;
775 command.canvas = canvas;
776 command.frame = output;
777 send_command(&command);
780 void Playback3D::clear_output_sync(Playback3DCommand *command)
782 command->canvas->lock_canvas("Playback3D::clear_output_sync");
783 if(command->canvas->get_canvas())
785 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
786 // If we get here, the virtual console is being used.
787 command->canvas->get_canvas()->enable_opengl();
789 // Using pbuffer for refresh frame.
792 command->frame->enable_opengl();
797 command->canvas->get_canvas()->unlock_window();
799 command->canvas->unlock_canvas();
803 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
805 Playback3DCommand command;
806 command.command = Playback3DCommand::CLEAR_INPUT;
807 command.canvas = canvas;
808 command.frame = frame;
809 send_command(&command);
812 void Playback3D::clear_input_sync(Playback3DCommand *command)
814 command->canvas->lock_canvas("Playback3D::clear_output_sync");
815 if(command->canvas->get_canvas())
817 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
818 command->canvas->get_canvas()->enable_opengl();
819 command->frame->enable_opengl();
820 command->frame->clear_pbuffer();
821 command->frame->set_opengl_state(VFrame::SCREEN);
822 command->canvas->get_canvas()->unlock_window();
824 command->canvas->unlock_canvas();
827 void Playback3D::do_camera(Canvas *canvas,
839 Playback3DCommand command;
840 command.command = Playback3DCommand::DO_CAMERA;
841 command.canvas = canvas;
842 command.input = input;
843 command.frame = output;
844 command.in_x1 = in_x1;
845 command.in_y1 = in_y1;
846 command.in_x2 = in_x2;
847 command.in_y2 = in_y2;
848 command.out_x1 = out_x1;
849 command.out_y1 = out_y1;
850 command.out_x2 = out_x2;
851 command.out_y2 = out_y2;
852 send_command(&command);
855 void Playback3D::do_camera_sync(Playback3DCommand *command)
857 command->canvas->lock_canvas("Playback3D::do_camera_sync");
858 if(command->canvas->get_canvas())
860 command->canvas->get_canvas()->lock_window("Playback3D::clear_output_sync");
861 command->canvas->get_canvas()->enable_opengl();
863 command->input->to_texture();
864 command->frame->enable_opengl();
865 command->frame->init_screen();
866 command->frame->clear_pbuffer();
868 command->input->bind_texture(0);
869 // Must call draw_texture in input frame to get the texture coordinates right.
871 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
877 // (float)command->input->get_h() - command->out_y1,
879 // (float)command->input->get_h() - command->out_y2);
880 command->input->draw_texture(
881 command->in_x1, command->in_y2,
882 command->in_x2, command->in_y1,
884 (float)command->frame->get_h() - command->out_y1,
886 (float)command->frame->get_h() - command->out_y2);
889 command->frame->set_opengl_state(VFrame::SCREEN);
890 command->frame->screen_to_ram();
891 command->canvas->get_canvas()->unlock_window();
893 command->canvas->unlock_canvas();
896 void Playback3D::overlay(Canvas *canvas, VFrame *input,
897 float in_x1, float in_y1, float in_x2, float in_y2,
898 float out_x1, float out_y1, float out_x2, float out_y2,
899 float alpha, int mode, int interpolation_type,
900 VFrame *output, int is_nested)
902 Playback3DCommand command;
903 command.command = Playback3DCommand::OVERLAY;
904 command.canvas = canvas;
905 command.frame = output;
906 command.input = input;
907 command.in_x1 = in_x1;
908 command.in_y1 = in_y1;
909 command.in_x2 = in_x2;
910 command.in_y2 = in_y2;
911 command.out_x1 = out_x1;
912 command.out_y1 = out_y1;
913 command.out_x2 = out_x2;
914 command.out_y2 = out_y2;
915 command.alpha = alpha;
917 command.interpolation_type = interpolation_type;
918 command.is_nested = is_nested;
919 send_command(&command);
922 void Playback3D::overlay_sync(Playback3DCommand *command)
925 // To do these operations, we need to copy the input buffer to a texture
926 // and blend 2 textures in a shader
927 static const char * const overlay_shaders[TRANSFER_TYPES] = {
928 blend_NORMAL_frag, // TRANSFER_NORMAL
929 blend_ADDITION_frag, // TRANSFER_ADDITION
930 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
931 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
932 blend_DIVIDE_frag, // TRANSFER_DIVIDE
933 blend_REPLACE_frag, // TRANSFER_REPLACE
934 blend_MAX_frag, // TRANSFER_MAX
935 blend_MIN_frag, // TRANSFER_MIN
936 blend_DARKEN_frag, // TRANSFER_DARKEN
937 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
938 blend_DST_frag, // TRANSFER_DST
939 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
940 blend_DST_IN_frag, // TRANSFER_DST_IN
941 blend_DST_OUT_frag, // TRANSFER_DST_OUT
942 blend_DST_OVER_frag, // TRANSFER_DST_OVER
943 blend_SRC_frag, // TRANSFER_SRC
944 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
945 blend_SRC_IN_frag, // TRANSFER_SRC_IN
946 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
947 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
948 blend_AND_frag, // TRANSFER_AND
949 blend_OR_frag, // TRANSFER_OR
950 blend_XOR_frag, // TRANSFER_XOR
951 blend_OVERLAY_frag, // TRANSFER_OVERLAY
952 blend_SCREEN_frag, // TRANSFER_SCREEN
953 blend_BURN_frag, // TRANSFER_BURN
954 blend_DODGE_frag, // TRANSFER_DODGE
955 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
956 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
957 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
960 command->canvas->lock_canvas("Playback3D::overlay_sync");
961 if(command->canvas->get_canvas()) {
962 BC_WindowBase *window = command->canvas->get_canvas();
963 window->lock_window("Playback3D::overlay_sync");
964 // Make sure OpenGL is enabled first.
965 window->enable_opengl();
966 window->update_video_cursor();
968 glColor4f(1, 1, 1, 1);
973 command->frame->enable_opengl();
974 command->frame->set_opengl_state(VFrame::SCREEN);
975 canvas_w = command->frame->get_w();
976 canvas_h = command->frame->get_h();
980 canvas_w = window->get_w();
981 canvas_h = window->get_h();
985 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
986 switch(command->input->get_opengl_state()) {
987 // Upload texture and composite to screen
989 command->input->to_texture();
991 // Just composite texture to screen
992 case VFrame::TEXTURE:
994 // read from PBuffer to texture, then composite texture to screen
996 command->input->enable_opengl();
997 command->input->screen_to_texture();
999 command->frame->enable_opengl();
1001 window->enable_opengl();
1004 printf("Playback3D::overlay_sync unknown state\n");
1009 const char *shader_stack[4] = { 0, 0, 0, 0, };
1010 int total_shaders = 0;
1012 VFrame::init_screen(canvas_w, canvas_h);
1015 command->input->bind_texture(0);
1017 // Convert colormodel to RGB if not nested.
1018 // The color model setting in the output frame is ignored.
1019 if( command->is_nested <= 0 ) { // not nested
1020 switch(command->input->get_color_model()) {
1023 shader_stack[total_shaders++] = yuv_to_rgb_frag;
1029 #define add_shader(s) \
1030 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1031 shader_stack[total_shaders++] = s
1033 switch(command->mode) {
1034 case TRANSFER_REPLACE:
1035 // This requires overlaying an alpha multiplied image on a black screen.
1036 if( command->input->get_texture_components() != 4 ) break;
1037 add_shader(overlay_shaders[command->mode]);
1040 enable_overlay_texture(command);
1041 add_shader(overlay_shaders[command->mode]);
1045 // if to flatten alpha
1046 if( command->is_nested < 0 ) {
1047 switch(command->input->get_color_model()) {
1048 // yuv has already been converted to rgb
1052 add_shader(rgba_to_rgb_flatten);
1058 unsigned int frag_shader = 0;
1059 if(shader_stack[0]) {
1060 frag_shader = VFrame::make_shader(0,
1061 shader_stack[0], shader_stack[1],
1062 shader_stack[2], shader_stack[3], 0);
1064 glUseProgram(frag_shader);
1066 // Set texture unit of the texture
1067 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1068 // Set texture unit of the temp texture
1069 glUniform1i(glGetUniformLocation(frag_shader, "tex2"), 1);
1071 int variable = glGetUniformLocation(frag_shader, "alpha");
1072 glUniform1f(variable, command->alpha);
1073 // Set dimensions of the temp texture
1075 glUniform2f(glGetUniformLocation(frag_shader, "tex2_dimensions"),
1076 (float)temp_texture->get_texture_w(),
1077 (float)temp_texture->get_texture_h());
1083 // printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1084 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1085 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1087 command->input->draw_texture(
1088 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1089 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1090 // Don't flip vertical if nested
1091 command->is_nested > 0 ? 0 : 1);
1094 // Delete temp texture
1096 delete temp_texture;
1098 glActiveTexture(GL_TEXTURE1);
1099 glDisable(GL_TEXTURE_2D);
1101 glActiveTexture(GL_TEXTURE0);
1102 glDisable(GL_TEXTURE_2D);
1104 window->unlock_window();
1106 command->canvas->unlock_canvas();
1110 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1113 glDisable(GL_BLEND);
1115 glActiveTexture(GL_TEXTURE1);
1116 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1117 command->input->get_color_model());
1118 temp_texture->bind(1);
1120 // Read canvas into texture
1121 glReadBuffer(GL_BACK);
1122 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1127 void Playback3D::do_mask(Canvas *canvas,
1129 int64_t start_position_project,
1130 MaskAutos *keyframe_set,
1132 MaskAuto *default_auto)
1134 Playback3DCommand command;
1135 command.command = Playback3DCommand::DO_MASK;
1136 command.canvas = canvas;
1137 command.frame = output;
1138 command.start_position_project = start_position_project;
1139 command.keyframe_set = keyframe_set;
1140 command.keyframe = keyframe;
1141 command.default_auto = default_auto;
1143 send_command(&command);
1149 struct Vertex : ListItem<Vertex>
1153 // this list is only used from the main thread, no locking needed
1154 // this must be a list so that pointers to allocated entries remain valid
1155 // when new entries are added
1156 static List<Vertex> *vertex_cache = 0;
1158 static void combine_callback(GLdouble coords[3],
1159 GLdouble *vertex_data[4],
1163 // can't use malloc here; GLU doesn't delete the memory for us!
1164 Vertex* vertex = vertex_cache->append();
1165 vertex->c[0] = coords[0];
1166 vertex->c[1] = coords[1];
1167 vertex->c[2] = coords[2];
1168 // we don't need to interpolate anything
1170 *dataOut = &vertex->c[0];
1175 void Playback3D::do_mask_sync(Playback3DCommand *command)
1178 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1179 if(command->canvas->get_canvas())
1181 BC_WindowBase *window = command->canvas->get_canvas();
1182 window->lock_window("Playback3D::do_mask_sync");
1183 window->enable_opengl();
1185 switch(command->frame->get_opengl_state())
1188 // Time to upload to the texture
1189 command->frame->to_texture();
1192 case VFrame::SCREEN:
1193 // Read back from PBuffer
1194 // Bind context to pbuffer
1195 command->frame->enable_opengl();
1196 command->frame->screen_to_texture();
1202 // Create PBuffer and draw the mask on it
1203 command->frame->enable_opengl();
1205 // Initialize coordinate system
1206 int w = command->frame->get_w();
1207 int h = command->frame->get_h();
1208 command->frame->init_screen();
1211 glDisable(GL_TEXTURE_2D);
1212 if(command->default_auto->mode == MASK_MULTIPLY_ALPHA)
1214 glClearColor(0.0, 0.0, 0.0, 0.0);
1215 glColor4f((float)command->keyframe->value / 100,
1216 (float)command->keyframe->value / 100,
1217 (float)command->keyframe->value / 100,
1222 glClearColor(1.0, 1.0, 1.0, 1.0);
1223 glColor4f((float)1.0 - (float)command->keyframe->value / 100,
1224 (float)1.0 - (float)command->keyframe->value / 100,
1225 (float)1.0 - (float)command->keyframe->value / 100,
1228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1231 // Draw mask with scaling to simulate feathering
1232 GLUtesselator *tesselator = gluNewTess();
1233 gluTessProperty(tesselator, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD);
1234 gluTessCallback(tesselator, GLU_TESS_VERTEX, (GLvoid (*) ( )) &glVertex3dv);
1235 gluTessCallback(tesselator, GLU_TESS_BEGIN, (GLvoid (*) ( )) &glBegin);
1236 gluTessCallback(tesselator, GLU_TESS_END, (GLvoid (*) ( )) &glEnd);
1237 gluTessCallback(tesselator, GLU_TESS_COMBINE, (GLvoid (*) ( ))&combine_callback);
1239 vertex_cache = new List<Vertex>;
1242 // Draw every submask as a new polygon
1243 int total_submasks = command->keyframe_set->total_submasks(
1244 command->start_position_project,
1246 float scale = command->keyframe->feather + 1;
1247 int display_list = glGenLists(1);
1248 glNewList(display_list, GL_COMPILE);
1249 for(int k = 0; k < total_submasks; k++)
1251 gluTessBeginPolygon(tesselator, NULL);
1252 gluTessBeginContour(tesselator);
1253 ArrayList<MaskPoint*> *points = new ArrayList<MaskPoint*>;
1254 command->keyframe_set->get_points(points,
1256 command->start_position_project,
1259 int first_point = 0;
1260 // Need to tabulate every vertex in persistent memory because
1261 // gluTessVertex doesn't copy them.
1262 ArrayList<GLdouble*> coords;
1263 coords.set_array_delete();
1264 for(int i = 0; i < points->total; i++)
1266 MaskPoint *point1 = points->values[i];
1267 MaskPoint *point2 = (i >= points->total - 1) ?
1269 points->values[i + 1];
1273 if(point1->control_x2 == 0 &&
1274 point1->control_y2 == 0 &&
1275 point2->control_x1 == 0 &&
1276 point2->control_y1 == 0)
1279 float x0 = point1->x;
1280 float y0 = point1->y;
1281 float x1 = point1->x + point1->control_x2;
1282 float y1 = point1->y + point1->control_y2;
1283 float x2 = point2->x + point2->control_x1;
1284 float y2 = point2->y + point2->control_y1;
1285 float x3 = point2->x;
1286 float y3 = point2->y;
1288 // forward differencing bezier curves implementation taken from GPL code at
1289 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1291 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1298 cx3 = - x0 + 3*x1 - 3*x2 + x3;
1299 cx2 = 3*x0 - 6*x1 + 3*x2;
1303 cy3 = - y0 + 3*y1 - 3*y2 + y3;
1304 cy2 = 3*y0 - 6*y1 + 3*y2;
1308 // This equation is from Graphics Gems I.
1310 // The idea is that since we're approximating a cubic curve with lines,
1311 // any error we incur is due to the curvature of the line, which we can
1312 // estimate by calculating the maximum acceleration of the curve. For
1313 // a cubic, the acceleration (second derivative) is a line, meaning that
1314 // the absolute maximum acceleration must occur at either the beginning
1315 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
1316 // conservative than that, but that's okay.
1319 float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1320 float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1322 float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1325 if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1326 segments = int(1/h);
1329 for(int j = 0; j <= segments; j++)
1331 float t = (float)j / segments;
1332 x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1333 y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1335 if(j > 0 || first_point)
1337 GLdouble *coord = new GLdouble[3];
1338 coord[0] = x / scale;
1339 coord[1] = -h + y / scale;
1341 coords.append(coord);
1347 // Now that we know the total vertices, send them to GLU
1348 for(int i = 0; i < coords.total; i++)
1349 gluTessVertex(tesselator, coords.values[i], coords.values[i]);
1351 gluTessEndContour(tesselator);
1352 gluTessEndPolygon(tesselator);
1353 points->remove_all_objects();
1355 coords.remove_all_objects();
1358 glCallList(display_list);
1359 glDeleteLists(display_list, 1);
1360 gluDeleteTess(tesselator);
1362 delete vertex_cache;
1365 glColor4f(1, 1, 1, 1);
1368 // Read mask into temporary texture.
1369 // For feathering, just read the part of the screen after the downscaling.
1372 float w_scaled = w / scale;
1373 float h_scaled = h / scale;
1374 // Don't vary the texture size according to scaling because that
1375 // would waste memory.
1376 // This enables and binds the temporary texture.
1377 glActiveTexture(GL_TEXTURE1);
1378 BC_Texture::new_texture(&temp_texture,
1381 command->frame->get_color_model());
1382 temp_texture->bind(1);
1383 glReadBuffer(GL_BACK);
1385 // Need to add extra size to fill in the bottom right
1386 glCopyTexSubImage2D(GL_TEXTURE_2D,
1392 (int)MIN(w_scaled + 2, w),
1393 (int)MIN(h_scaled + 2, h));
1395 command->frame->bind_texture(0);
1398 // For feathered masks, use a shader to multiply.
1399 // For unfeathered masks, we could use a stencil buffer
1400 // for further optimization but we also need a YUV algorithm.
1401 unsigned int frag_shader = 0;
1402 switch(temp_texture->get_texture_components())
1405 if(command->frame->get_color_model() == BC_YUV888)
1406 frag_shader = VFrame::make_shader(0,
1407 multiply_yuvmask3_frag,
1410 frag_shader = VFrame::make_shader(0,
1411 multiply_mask3_frag,
1415 frag_shader = VFrame::make_shader(0,
1416 multiply_mask4_frag,
1424 glUseProgram(frag_shader);
1425 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1426 glUniform1i(variable, 0);
1427 if((variable = glGetUniformLocation(frag_shader, "tex1")) >= 0)
1428 glUniform1i(variable, 1);
1429 if((variable = glGetUniformLocation(frag_shader, "scale")) >= 0)
1430 glUniform1f(variable, scale);
1435 // Write texture to PBuffer with multiply and scaling for feather.
1438 command->frame->draw_texture(0, 0, w, h, 0, 0, w, h);
1439 command->frame->set_opengl_state(VFrame::SCREEN);
1442 // Disable temp texture
1445 glActiveTexture(GL_TEXTURE1);
1446 glDisable(GL_TEXTURE_2D);
1447 delete temp_texture;
1450 glActiveTexture(GL_TEXTURE0);
1451 glDisable(GL_TEXTURE_2D);
1454 window->enable_opengl();
1455 window->unlock_window();
1457 command->canvas->unlock_canvas();
1470 void Playback3D::convert_cmodel(Canvas *canvas,
1474 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1475 int src_cmodel = output->get_color_model();
1477 (output->get_opengl_state() == VFrame::TEXTURE ||
1478 output->get_opengl_state() == VFrame::SCREEN) &&
1479 // OpenGL has no floating point.
1480 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1481 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1482 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1483 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1484 // OpenGL sets alpha to 1 on import
1485 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1486 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1487 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1492 Playback3DCommand command;
1493 command.command = Playback3DCommand::CONVERT_CMODEL;
1494 command.canvas = canvas;
1495 command.frame = output;
1496 command.dst_cmodel = dst_cmodel;
1497 send_command(&command);
1500 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1503 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1505 if(command->canvas->get_canvas())
1507 BC_WindowBase *window = command->canvas->get_canvas();
1508 window->lock_window("Playback3D::convert_cmodel_sync");
1509 window->enable_opengl();
1511 // Import into hardware
1512 command->frame->enable_opengl();
1513 command->frame->init_screen();
1514 command->frame->to_texture();
1516 // Colormodel permutation
1517 const char *shader = 0;
1518 int src_cmodel = command->frame->get_color_model();
1519 int dst_cmodel = command->dst_cmodel;
1525 } cmodel_shader_table_t;
1526 static cmodel_shader_table_t cmodel_shader_table[] =
1528 { BC_RGB888, BC_YUV888, rgb_to_yuv_frag },
1529 { BC_RGB888, BC_YUVA8888, rgb_to_yuv_frag },
1530 { BC_RGBA8888, BC_RGB888, rgba_to_rgb_frag },
1531 { BC_RGBA8888, BC_RGB_FLOAT, rgba_to_rgb_frag },
1532 { BC_RGBA8888, BC_YUV888, rgba_to_yuv_frag },
1533 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv_frag },
1534 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv_frag },
1535 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv_frag },
1536 { BC_RGBA_FLOAT, BC_RGB888, rgba_to_rgb_frag },
1537 { BC_RGBA_FLOAT, BC_RGB_FLOAT, rgba_to_rgb_frag },
1538 { BC_RGBA_FLOAT, BC_YUV888, rgba_to_yuv_frag },
1539 { BC_RGBA_FLOAT, BC_YUVA8888, rgb_to_yuv_frag },
1540 { BC_YUV888, BC_RGB888, yuv_to_rgb_frag },
1541 { BC_YUV888, BC_RGBA8888, yuv_to_rgb_frag },
1542 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb_frag },
1543 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb_frag },
1544 { BC_YUVA8888, BC_RGB888, yuva_to_rgb_frag },
1545 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb_frag },
1546 { BC_YUVA8888, BC_RGB_FLOAT, yuva_to_rgb_frag },
1547 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb_frag },
1548 { BC_YUVA8888, BC_YUV888, yuva_to_yuv_frag },
1551 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1552 for(int i = 0; i < table_size; i++)
1554 if(cmodel_shader_table[i].src == src_cmodel &&
1555 cmodel_shader_table[i].dst == dst_cmodel)
1557 shader = cmodel_shader_table[i].shader;
1562 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1564 // command->frame->get_color_model(),
1565 // command->dst_cmodel,
1570 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1571 command->frame->bind_texture(0);
1572 unsigned int shader_id = -1;
1575 shader_id = VFrame::make_shader(0,
1578 glUseProgram(shader_id);
1579 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1582 command->frame->draw_texture();
1584 if(shader) glUseProgram(0);
1586 command->frame->set_opengl_state(VFrame::SCREEN);
1589 window->unlock_window();
1592 command->canvas->unlock_canvas();
1596 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1598 Playback3DCommand command;
1599 command.command = Playback3DCommand::DO_FADE;
1600 command.canvas = canvas;
1601 command.frame = frame;
1602 command.alpha = fade;
1603 send_command(&command);
1606 void Playback3D::do_fade_sync(Playback3DCommand *command)
1609 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1610 if(command->canvas->get_canvas())
1612 BC_WindowBase *window = command->canvas->get_canvas();
1613 window->lock_window("Playback3D::do_fade_sync");
1614 window->enable_opengl();
1616 switch(command->frame->get_opengl_state())
1619 command->frame->to_texture();
1622 case VFrame::SCREEN:
1623 // Read back from PBuffer
1624 // Bind context to pbuffer
1625 command->frame->enable_opengl();
1626 command->frame->screen_to_texture();
1631 command->frame->enable_opengl();
1632 command->frame->init_screen();
1633 command->frame->bind_texture(0);
1635 // glClearColor(0.0, 0.0, 0.0, 0.0);
1636 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1637 glDisable(GL_BLEND);
1638 unsigned int frag_shader = 0;
1639 switch(command->frame->get_color_model())
1641 // For the alpha colormodels, the native function seems to multiply the
1642 // components by the alpha instead of just the alpha.
1646 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1651 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1652 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1657 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1664 glUseProgram(frag_shader);
1666 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1667 glUniform1i(variable, 0);
1668 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1669 glUniform1f(variable, command->alpha);
1672 command->frame->draw_texture();
1673 command->frame->set_opengl_state(VFrame::SCREEN);
1680 glColor4f(1, 1, 1, 1);
1681 glDisable(GL_BLEND);
1683 window->unlock_window();
1685 command->canvas->unlock_canvas();
1699 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1701 Playback3DCommand command;
1702 command.command = Playback3DCommand::PLUGIN;
1703 command.canvas = canvas;
1704 command.plugin_client = client;
1705 return send_command(&command);
1708 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1710 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1711 if(command->canvas->get_canvas())
1713 BC_WindowBase *window = command->canvas->get_canvas();
1714 window->lock_window("Playback3D::run_plugin_sync");
1715 window->enable_opengl();
1717 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1719 window->unlock_window();
1721 command->canvas->unlock_canvas();