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);
373 static void GLAPIENTRY glDebugCallback(GLenum source, GLenum type,
374 GLuint id, GLenum severity, GLsizei length, const GLchar* message,
375 const void* userParam)
377 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
378 ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
379 type, severity, message );
383 Playback3D::Playback3D(MWindow *mwindow)
386 this->mwindow = mwindow;
389 //Enabling OpenGL debug output
390 // this does not work
391 glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
392 glEnable(GL_DEBUG_OUTPUT);
393 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
394 glDebugMessageCallback(glDebugCallback, 0);
395 glEnable(GL_DEBUG_OUTPUT);
399 Playback3D::~Playback3D()
406 BC_SynchronousCommand* Playback3D::new_command()
408 return new Playback3DCommand;
413 void Playback3D::handle_command(BC_SynchronousCommand *command)
415 //printf("Playback3D::handle_command 1 %d\n", command->command);
416 switch(command->command)
418 case Playback3DCommand::WRITE_BUFFER:
419 write_buffer_sync((Playback3DCommand*)command);
422 case Playback3DCommand::CLEAR_OUTPUT:
423 clear_output_sync((Playback3DCommand*)command);
426 case Playback3DCommand::CLEAR_INPUT:
427 clear_input_sync((Playback3DCommand*)command);
430 case Playback3DCommand::DO_CAMERA:
431 do_camera_sync((Playback3DCommand*)command);
434 case Playback3DCommand::OVERLAY:
435 overlay_sync((Playback3DCommand*)command);
438 case Playback3DCommand::DO_FADE:
439 do_fade_sync((Playback3DCommand*)command);
442 case Playback3DCommand::DO_MASK:
443 do_mask_sync((Playback3DCommand*)command);
446 case Playback3DCommand::PLUGIN:
447 run_plugin_sync((Playback3DCommand*)command);
450 case Playback3DCommand::COPY_FROM:
451 copy_from_sync((Playback3DCommand*)command);
454 case Playback3DCommand::CONVERT_CMODEL:
455 convert_cmodel_sync((Playback3DCommand*)command);
458 // case Playback3DCommand::DRAW_REFRESH:
459 // draw_refresh_sync((Playback3DCommand*)command);
462 //printf("Playback3D::handle_command 10\n");
468 void Playback3D::copy_from(Canvas *canvas,
473 Playback3DCommand command;
474 command.command = Playback3DCommand::COPY_FROM;
475 command.canvas = canvas;
478 command.want_texture = want_texture;
479 send_command(&command);
482 void Playback3D::copy_from_sync(Playback3DCommand *command)
485 BC_WindowBase *window =
486 command->canvas->lock_canvas("Playback3D::copy_from_sync");
488 window->enable_opengl();
490 int w = command->input->get_w();
491 int h = command->input->get_h();
493 if(command->input->get_opengl_state() == VFrame::SCREEN &&
494 w == command->frame->get_w() && h == command->frame->get_h())
496 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
497 // command->input->get_w(),
498 // command->input->get_h(),
499 // command->frame->get_w(),
500 // command->frame->get_h(),
501 // command->frame->get_color_model());
502 // With NVidia at least,
505 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
509 if(command->want_texture)
511 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
512 // Screen_to_texture requires the source pbuffer enabled.
513 command->input->enable_opengl();
514 command->frame->screen_to_texture();
515 command->frame->set_opengl_state(VFrame::TEXTURE);
520 command->input->to_texture();
521 command->input->bind_texture(0);
522 command->frame->enable_opengl();
523 command->frame->init_screen();
524 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
525 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
527 glUseProgram(shader);
528 int variable = glGetUniformLocation(shader, "tex");
529 glUniform1i(variable, 0);
530 BC_GL_YUV_TO_RGB(shader);
534 command->input->draw_texture(1);
535 command->frame->screen_to_ram();
541 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
542 command->input->get_opengl_state(), w, h,
543 command->frame->get_w(), command->frame->get_h());
546 command->canvas->unlock_canvas();
553 // void Playback3D::draw_refresh(Canvas *canvas,
564 // Playback3DCommand command;
565 // command.command = Playback3DCommand::DRAW_REFRESH;
566 // command.canvas = canvas;
567 // command.frame = frame;
568 // command.in_x1 = in_x1;
569 // command.in_y1 = in_y1;
570 // command.in_x2 = in_x2;
571 // command.in_y2 = in_y2;
572 // command.out_x1 = out_x1;
573 // command.out_y1 = out_y1;
574 // command.out_x2 = out_x2;
575 // command.out_y2 = out_y2;
576 // send_command(&command);
579 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
582 // BC_WindowBase *window =
583 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
585 // window->enable_opengl();
587 // // Read output pbuffer back to RAM in project colormodel
588 // // RGB 8bit is fastest for OpenGL to read back.
589 // command->frame->reallocate(0,
593 // command->frame->get_w(),
594 // command->frame->get_h(),
597 // command->frame->screen_to_ram();
599 // window->clear_box(0,
603 // window->draw_vframe(command->frame,
604 // (int)command->out_x1,
605 // (int)command->out_y1,
606 // (int)(command->out_x2 - command->out_x1),
607 // (int)(command->out_y2 - command->out_y1),
608 // (int)command->in_x1,
609 // (int)command->in_y1,
610 // (int)(command->in_x2 - command->in_x1),
611 // (int)(command->in_y2 - command->in_y1),
615 // command->canvas->unlock_canvas();
623 void Playback3D::write_buffer(Canvas *canvas,
635 Playback3DCommand command;
636 command.command = Playback3DCommand::WRITE_BUFFER;
637 command.canvas = canvas;
638 command.frame = frame;
639 command.in_x1 = in_x1;
640 command.in_y1 = in_y1;
641 command.in_x2 = in_x2;
642 command.in_y2 = in_y2;
643 command.out_x1 = out_x1;
644 command.out_y1 = out_y1;
645 command.out_x2 = out_x2;
646 command.out_y2 = out_y2;
647 command.is_cleared = is_cleared;
648 send_command(&command);
652 void Playback3D::write_buffer_sync(Playback3DCommand *command)
655 BC_WindowBase *window =
656 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
658 // Update hidden cursor
659 window->update_video_cursor();
660 // Make sure OpenGL is enabled first.
661 window->enable_opengl();
663 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
665 int frame_state = command->frame->get_opengl_state();
666 switch( frame_state ) {
667 // Upload texture and composite to screen
671 command->frame->to_texture();
672 window->enable_opengl();
673 // Composite texture to screen and swap buffer
674 case VFrame::TEXTURE:
675 draw_output(command, flip_y);
678 printf("Playback3D::write_buffer_sync unknown state\n");
681 command->frame->set_opengl_state(frame_state);
683 command->canvas->unlock_canvas();
689 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
692 int texture_id = command->frame->get_texture_id();
693 BC_WindowBase *window = command->canvas->get_canvas();
695 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
697 // command->canvas->get_canvas());
702 // If virtual console is being used, everything in this function has
703 // already been done except the page flip.
706 canvas_w = window->get_w();
707 canvas_h = window->get_h();
708 VFrame::init_screen(canvas_w, canvas_h);
709 int color_model = command->frame->get_color_model();
710 int is_yuv = BC_CModels::is_yuv(color_model);
712 if(!command->is_cleared)
714 // If we get here, the virtual console was not used.
715 init_frame(command, 0);
719 // Undo any previous shader settings
720 command->frame->bind_texture(0);
722 // Convert colormodel
723 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
725 glUseProgram(shader);
726 // Set texture unit of the texture
727 int variable = glGetUniformLocation(shader, "tex");
728 glUniform1i(variable, 0);
729 BC_GL_YUV_TO_RGB(shader);
732 // if(BC_CModels::components(color_model) == 4)
734 // glEnable(GL_BLEND);
735 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
738 command->frame->draw_texture(
739 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
740 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
744 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
756 command->canvas->get_canvas()->flip_opengl();
763 void Playback3D::init_frame(Playback3DCommand *command, int is_yuv)
766 float gbuv = is_yuv ? 0.5 : 0.0;
767 glClearColor(0.0, gbuv, gbuv, 0.0);
768 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
773 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
775 Playback3DCommand command;
776 command.command = Playback3DCommand::CLEAR_OUTPUT;
777 command.canvas = canvas;
778 command.frame = output;
779 send_command(&command);
782 void Playback3D::clear_output_sync(Playback3DCommand *command)
785 BC_WindowBase *window =
786 command->canvas->lock_canvas("Playback3D::clear_output_sync");
788 // If we get here, the virtual console is being used.
789 command->canvas->get_canvas()->enable_opengl();
791 // Using pbuffer for refresh frame.
792 if( command->frame ) {
793 command->frame->enable_opengl();
794 int color_model = command->canvas->mwindow->edl->session->color_model;
795 is_yuv = BC_CModels::is_yuv(color_model);
798 init_frame(command, is_yuv);
800 command->canvas->unlock_canvas();
805 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
807 Playback3DCommand command;
808 command.command = Playback3DCommand::CLEAR_INPUT;
809 command.canvas = canvas;
810 command.frame = frame;
811 send_command(&command);
814 void Playback3D::clear_input_sync(Playback3DCommand *command)
817 BC_WindowBase *window =
818 command->canvas->lock_canvas("Playback3D::clear_input_sync");
820 command->canvas->get_canvas()->enable_opengl();
821 command->frame->enable_opengl();
822 command->frame->clear_pbuffer();
823 command->frame->set_opengl_state(VFrame::SCREEN);
825 command->canvas->unlock_canvas();
829 void Playback3D::do_camera(Canvas *canvas,
841 Playback3DCommand command;
842 command.command = Playback3DCommand::DO_CAMERA;
843 command.canvas = canvas;
844 command.input = input;
845 command.frame = output;
846 command.in_x1 = in_x1;
847 command.in_y1 = in_y1;
848 command.in_x2 = in_x2;
849 command.in_y2 = in_y2;
850 command.out_x1 = out_x1;
851 command.out_y1 = out_y1;
852 command.out_x2 = out_x2;
853 command.out_y2 = out_y2;
854 send_command(&command);
857 void Playback3D::do_camera_sync(Playback3DCommand *command)
860 BC_WindowBase *window =
861 command->canvas->lock_canvas("Playback3D::do_camera_sync");
863 command->canvas->get_canvas()->enable_opengl();
865 command->input->to_texture();
866 command->frame->enable_opengl();
867 command->frame->init_screen();
868 command->frame->clear_pbuffer();
870 command->input->bind_texture(0);
871 // Must call draw_texture in input frame to get the texture coordinates right.
873 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
879 // (float)command->input->get_h() - command->out_y1,
881 // (float)command->input->get_h() - command->out_y2);
882 command->input->draw_texture(
883 command->in_x1, command->in_y2,
884 command->in_x2, command->in_y1,
886 (float)command->frame->get_h() - command->out_y1,
888 (float)command->frame->get_h() - command->out_y2);
891 command->frame->set_opengl_state(VFrame::SCREEN);
892 command->frame->screen_to_ram();
894 command->canvas->unlock_canvas();
898 void Playback3D::overlay(Canvas *canvas, VFrame *input,
899 float in_x1, float in_y1, float in_x2, float in_y2,
900 float out_x1, float out_y1, float out_x2, float out_y2,
901 float alpha, int mode, int interpolation_type,
902 VFrame *output, int is_nested)
904 Playback3DCommand command;
905 command.command = Playback3DCommand::OVERLAY;
906 command.canvas = canvas;
907 command.frame = output;
908 command.input = input;
909 command.in_x1 = in_x1;
910 command.in_y1 = in_y1;
911 command.in_x2 = in_x2;
912 command.in_y2 = in_y2;
913 command.out_x1 = out_x1;
914 command.out_y1 = out_y1;
915 command.out_x2 = out_x2;
916 command.out_y2 = out_y2;
917 command.alpha = alpha;
919 command.interpolation_type = interpolation_type;
920 command.is_nested = is_nested;
921 send_command(&command);
924 void Playback3D::overlay_sync(Playback3DCommand *command)
927 // To do these operations, we need to copy the input buffer to a texture
928 // and blend 2 textures in a shader
929 static const char * const overlay_shaders[TRANSFER_TYPES] = {
930 blend_NORMAL_frag, // TRANSFER_NORMAL
931 blend_ADDITION_frag, // TRANSFER_ADDITION
932 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
933 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
934 blend_DIVIDE_frag, // TRANSFER_DIVIDE
935 blend_REPLACE_frag, // TRANSFER_REPLACE
936 blend_MAX_frag, // TRANSFER_MAX
937 blend_MIN_frag, // TRANSFER_MIN
938 blend_DARKEN_frag, // TRANSFER_DARKEN
939 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
940 blend_DST_frag, // TRANSFER_DST
941 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
942 blend_DST_IN_frag, // TRANSFER_DST_IN
943 blend_DST_OUT_frag, // TRANSFER_DST_OUT
944 blend_DST_OVER_frag, // TRANSFER_DST_OVER
945 blend_SRC_frag, // TRANSFER_SRC
946 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
947 blend_SRC_IN_frag, // TRANSFER_SRC_IN
948 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
949 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
950 blend_AND_frag, // TRANSFER_AND
951 blend_OR_frag, // TRANSFER_OR
952 blend_XOR_frag, // TRANSFER_XOR
953 blend_OVERLAY_frag, // TRANSFER_OVERLAY
954 blend_SCREEN_frag, // TRANSFER_SCREEN
955 blend_BURN_frag, // TRANSFER_BURN
956 blend_DODGE_frag, // TRANSFER_DODGE
957 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
958 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
959 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
962 BC_WindowBase *window =
963 command->canvas->lock_canvas("Playback3D::overlay_sync");
965 // Make sure OpenGL is enabled first.
966 window->enable_opengl();
967 window->update_video_cursor();
969 glColor4f(1, 1, 1, 1);
973 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
974 switch( command->input->get_opengl_state() ) {
975 // Upload texture and composite to screen
977 command->input->to_texture();
979 // Just composite texture to screen
980 case VFrame::TEXTURE:
982 // read from PBuffer to texture, then composite texture to screen
984 command->input->enable_opengl();
985 command->input->screen_to_texture();
988 printf("Playback3D::overlay_sync unknown state\n");
994 command->frame->enable_opengl();
995 command->frame->set_opengl_state(VFrame::SCREEN);
996 canvas_w = command->frame->get_w();
997 canvas_h = command->frame->get_h();
1001 window->enable_opengl();
1002 canvas_w = window->get_w();
1003 canvas_h = window->get_h();
1007 const char *shader_stack[16];
1008 memset(shader_stack,0, sizeof(shader_stack));
1009 int total_shaders = 0, need_matrix = 0;
1011 VFrame::init_screen(canvas_w, canvas_h);
1014 command->input->bind_texture(0);
1016 // Convert colormodel to RGB if not nested.
1017 // The color model setting in the output frame is ignored.
1018 // if( command->is_nested <= 0 && // not nested
1019 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
1021 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
1025 #define add_shader(s) \
1026 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1027 shader_stack[total_shaders++] = s
1029 switch(command->mode) {
1030 case TRANSFER_REPLACE:
1031 // This requires overlaying an alpha multiplied image on a black screen.
1032 if( command->input->get_texture_components() != 4 ) break;
1033 add_shader(overlay_shaders[command->mode]);
1036 enable_overlay_texture(command);
1037 add_shader(overlay_shaders[command->mode]);
1041 // if to flatten alpha
1042 // if( command->is_nested < 0 ) {
1043 // switch(command->input->get_color_model()) {
1044 //// yuv has already been converted to rgb
1045 // case BC_YUVA8888:
1046 // case BC_RGBA_FLOAT:
1047 // case BC_RGBA8888:
1048 // add_shader(rgba_to_rgb_flatten);
1055 unsigned int shader = !shader_stack[0] ? 0 :
1056 VFrame::make_shader(shader_stack);
1058 glUseProgram(shader);
1059 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1060 // Set texture unit of the texture
1061 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1062 // Set texture unit of the temp texture
1063 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1065 int variable = glGetUniformLocation(shader, "alpha");
1066 glUniform1f(variable, command->alpha);
1067 // Set dimensions of the temp texture
1069 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1070 (float)temp_texture->get_texture_w(),
1071 (float)temp_texture->get_texture_h());
1077 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1078 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1079 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1081 command->input->draw_texture(
1082 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1083 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1084 !command->is_nested);
1087 // Delete temp texture
1089 delete temp_texture;
1091 glActiveTexture(GL_TEXTURE1);
1092 glDisable(GL_TEXTURE_2D);
1094 glActiveTexture(GL_TEXTURE0);
1095 glDisable(GL_TEXTURE_2D);
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);
1138 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1140 int x1 = iy1 < iy2 ? ix1 : ix2;
1141 int y1 = iy1 < iy2 ? iy1 : iy2;
1142 int x2 = iy1 < iy2 ? ix2 : ix1;
1143 int y2 = iy1 < iy2 ? iy2 : iy1;
1146 int dx = x2-x1, dy = y2-y1;
1147 int dx2 = 2*dx, dy2 = 2*dy;
1148 if( dx < 0 ) dx = -dx;
1149 int m = dx > dy ? dx : dy, n = m;
1151 if( dx2 >= 0 ) do { /* +Y, +X */
1152 spots.append(x, y++);
1153 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
1154 } while( --n >= 0 );
1155 else do { /* +Y, -X */
1156 spots.append(x, y++);
1157 if( (m += dx2) < 0 ) { m += dy2; --x; }
1158 } while( --n >= 0 );
1161 if( dx2 >= 0 ) do { /* +X, +Y */
1162 spots.append(x++, y);
1163 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
1164 } while( --n >= 0 );
1165 else do { /* -X, +Y */
1166 spots.append(x--, y);
1167 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
1168 } while( --n >= 0 );
1173 class fb_texture : public BC_Texture
1176 fb_texture(int w, int h, int colormodel);
1178 void bind(int texture_unit);
1179 void read_screen(int x, int y, int w, int h);
1180 void set_output_texture();
1184 fb_texture::fb_texture(int w, int h, int colormodel)
1185 : BC_Texture(w, h, colormodel)
1188 // glGenRenderbuffers(1, &rb);
1189 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1190 // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1191 glGenFramebuffers(1, &fb);
1192 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1193 // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1196 fb_texture::~fb_texture()
1198 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1199 glDeleteFramebuffers(1, (GLuint *)&fb);
1200 // glBindRenderbuffer(GL_RENDERBUFFER, 0);
1201 // glGenRenderbuffers(1, &rb);
1204 void fb_texture::bind(int texture_unit)
1206 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1207 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1208 BC_Texture::bind(texture_unit);
1211 void fb_texture::read_screen(int x, int y, int w, int h)
1213 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1214 glReadBuffer(GL_BACK);
1215 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1218 void fb_texture::set_output_texture()
1220 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, get_texture_id(), 0);
1221 GLenum dbo[1] = { GL_COLOR_ATTACHMENT0, }; // bind layout(location=0) out vec4 color;
1222 glDrawBuffers(1, dbo);
1223 int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1224 if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1225 printf("glDrawBuffer error 0x%04x\n", ret);
1230 static void combineData(GLdouble coords[3],
1231 GLdouble *vertex_data[4], GLfloat weight[4],
1232 GLdouble **outData, void *data)
1234 ArrayList<double *> *invented = (ArrayList<double *> *)data;
1235 GLdouble *vertex = new double[6];
1236 invented->append(vertex);
1237 vertex[0] = coords[0];
1238 vertex[1] = coords[1];
1239 vertex[2] = coords[2];
1240 for( int i=3; i<6; ++i ) {
1241 vertex[i] = weight[0] * vertex_data[0][i] +
1242 weight[1] * vertex_data[1][i] +
1243 weight[2] * vertex_data[2][i] +
1244 weight[3] * vertex_data[3][i];
1250 static void zglBegin(GLenum mode) { glBegin(mode); }
1251 static void zglEnd() { glEnd(); }
1252 static void zglVertex3dv(const GLdouble *v) { glVertex3dv(v); }
1256 void Playback3D::do_mask_sync(Playback3DCommand *command)
1259 BC_WindowBase *window =
1260 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1262 window->enable_opengl();
1264 switch( command->frame->get_opengl_state() ) {
1266 // upload frame to the texture
1267 command->frame->to_texture();
1270 case VFrame::SCREEN:
1271 // Read back from PBuffer
1272 // Bind context to pbuffer
1273 command->frame->enable_opengl();
1274 command->frame->screen_to_texture();
1278 // Initialize coordinate system
1279 command->frame->enable_opengl();
1280 command->frame->init_screen();
1281 int color_model = command->frame->get_color_model();
1282 int w = command->frame->get_w();
1283 int h = command->frame->get_h();
1285 float faders[SUBMASKS], feathers[SUBMASKS], bg = 1;
1286 MaskPointSet point_set[SUBMASKS];
1287 // Draw every submask as a new polygon
1288 int total_submasks = command->keyframe_set->total_submasks(
1289 command->start_position_project, PLAY_FORWARD);
1291 for(int k = 0; k < total_submasks; k++) {
1292 MaskPointSet &points = point_set[k];
1293 command->keyframe_set->get_points(&points,
1294 k, command->start_position_project, PLAY_FORWARD);
1295 float fader = command->keyframe_set->get_fader(
1296 command->start_position_project, k, PLAY_FORWARD);
1297 float v = fader/100.;
1299 if( v < 0 && (v+=1) < bg ) bg = v;
1300 float feather = command->keyframe_set->get_feather(
1301 command->start_position_project, k, PLAY_FORWARD);
1302 feathers[k] = feather;
1305 glDisable(GL_TEXTURE_2D);
1306 glClearColor(bg, bg, bg, bg);
1307 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1309 for(int k = 0; k < total_submasks; k++) {
1310 MaskPointSet &points = point_set[k];
1311 MaskEdge &edge = *edges.append(new MaskEdge());
1312 int first_point = 0;
1313 // Need to tabulate every vertex in persistent memory because
1314 // gluTessVertex doesn't copy them.
1315 for(int i = 0; i < points.total; i++) {
1316 MaskPoint *point1 = points.values[i];
1317 MaskPoint *point2 = (i >= points.total - 1) ?
1318 points.values[0] : points.values[i + 1];
1322 if( point1->control_x2 == 0 && point1->control_y2 == 0 &&
1323 point2->control_x1 == 0 && point2->control_y1 == 0 )
1326 float x0 = point1->x, y0 = point1->y;
1327 float x1 = point1->x + point1->control_x2;
1328 float y1 = point1->y + point1->control_y2;
1329 float x2 = point2->x + point2->control_x1;
1330 float y2 = point2->y + point2->control_y1;
1331 float x3 = point2->x, y3 = point2->y;
1333 // forward differencing bezier curves implementation taken from GPL code at
1334 // http://cvs.sourceforge.net/viewcvs.py/guliverkli/guliverkli/src/subtitles/Rasterizer.cpp?rev=1.3
1336 float cx3, cx2, cx1, cx0, cy3, cy2, cy1, cy0;
1343 cx3 = - x0 + 3*x1 - 3*x2 + x3;
1344 cx2 = 3*x0 - 6*x1 + 3*x2;
1348 cy3 = - y0 + 3*y1 - 3*y2 + y3;
1349 cy2 = 3*y0 - 6*y1 + 3*y2;
1353 // This equation is from Graphics Gems I.
1355 // The idea is that since we're approximating a cubic curve with lines,
1356 // any error we incur is due to the curvature of the line, which we can
1357 // estimate by calculating the maximum acceleration of the curve. For
1358 // a cubic, the acceleration (second derivative) is a line, meaning that
1359 // the absolute maximum acceleration must occur at either the beginning
1360 // (|c2|) or the end (|c2+c3|). Our bounds here are a little more
1361 // conservative than that, but that's okay.
1362 if (segments == 0) {
1363 float maxaccel1 = fabs(2*cy2) + fabs(6*cy3);
1364 float maxaccel2 = fabs(2*cx2) + fabs(6*cx3);
1366 float maxaccel = maxaccel1 > maxaccel2 ? maxaccel1 : maxaccel2;
1369 if(maxaccel > 8.0) h = sqrt((8.0) / maxaccel);
1370 segments = int(1/h);
1373 for(int j = 0; j <= segments; j++) {
1374 float t = (float)j / segments;
1375 x = cx0 + t*(cx1 + t*(cx2 + t*cx3));
1376 y = cy0 + t*(cy1 + t*(cy2 + t*cy3));
1378 if(j > 0 || first_point) {
1379 edge.append(x, y - h);
1384 if( edge.size() > 0 ) {
1386 float fader = faders[k];
1387 float v = fader < 0 ? 1 : 1-fader;
1388 glColor4f(v, v, v, v);
1389 int display_list = glGenLists(1);
1390 glNewList(display_list, GL_COMPILE);
1392 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1393 glBegin(GL_POLYGON);
1394 MaskCoord *c = &edge[0];
1395 for( int i=edge.size(); --i>=0; ++c )
1396 glVertex2f(c->x, c->y);
1399 GLUtesselator *tess = gluNewTess();
1400 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex3dv);
1401 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1402 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1403 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1404 ArrayList<double *> invented;
1405 invented.set_array_delete();
1407 gluTessBeginPolygon(tess, &invented);
1408 gluTessBeginContour(tess);
1409 MaskCoord *c = &edge[0];
1410 for( int i=edge.size(); --i>=0; ++c )
1411 gluTessVertex(tess, (GLdouble *)c, c);
1412 gluTessEndContour(tess);
1413 gluTessEndPolygon(tess);
1414 gluDeleteTess(tess);
1415 invented.remove_all_objects();
1418 glCallList(display_list);
1419 glDeleteLists(1, display_list);
1424 fb_texture *in = new fb_texture(w, h, color_model);
1426 in->read_screen(0,0, w,h);
1427 fb_texture *out = new fb_texture(w, h, color_model);
1429 unsigned int frag_shader =
1430 VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1431 if( frag_shader > 0 ) {
1433 glGenBuffers(1, points);
1434 for(int k = 0; k < total_submasks; k++) {
1435 MaskEdge &edge = *edges[k];
1436 if( !edge.size() ) continue;
1437 if( !faders[k] ) continue;
1438 if( !feathers[k] ) continue;
1440 for( int i=0; i<edge.size(); ++i ) {
1441 MaskCoord &a = edge[i];
1442 MaskCoord &b = i<edge.size()-1 ? edge[i+1] : edge[0];
1443 draw_spots(spots, a.x,a.y+h, b.x,b.y+h);
1445 int sz = spots.size() * sizeof(MaskSpot);
1446 glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, points[0], 0, sz);
1447 glBufferData(GL_SHADER_STORAGE_BUFFER, sz, &spots[0], GL_DYNAMIC_COPY);
1448 glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
1449 glUseProgram(frag_shader);
1450 float r = feathers[k], v = faders[k];
1451 glUniform1f(glGetUniformLocation(frag_shader, "r"), r);
1452 glUniform1f(glGetUniformLocation(frag_shader, "v"), v);
1454 glUniform1i(glGetUniformLocation(frag_shader, "tex"), 0);
1455 out->set_output_texture();
1456 glViewport(0,0, w,h);
1457 out->draw_texture(0,0, w,h, 0,0, w,h);
1459 fb_texture *t = in; in = out; out = t;
1461 glDeleteBuffers(1, points);
1464 glDrawBuffers(0, 0);
1465 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1467 unsigned int shader = VFrame::make_shader(0, alpha_frag, 0);
1468 glUseProgram(shader);
1470 command->frame->bind_texture(0);
1471 in->BC_Texture::bind(1);
1472 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1473 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1474 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1475 (float)in->get_texture_w(),
1476 (float)in->get_texture_h());
1477 // if( BC_CModels::components(color_model ) == 4) {
1478 // glEnable(GL_BLEND);
1479 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1482 command->frame->draw_texture();
1483 command->frame->set_opengl_state(VFrame::SCREEN);
1488 glDisable(GL_TEXTURE_2D);
1489 glColor4f(1, 1, 1, 1);
1490 glActiveTexture(GL_TEXTURE0);
1491 window->enable_opengl();
1493 command->canvas->unlock_canvas();
1498 void Playback3D::convert_cmodel(Canvas *canvas,
1502 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1503 int src_cmodel = output->get_color_model();
1505 (output->get_opengl_state() == VFrame::TEXTURE ||
1506 output->get_opengl_state() == VFrame::SCREEN) &&
1507 // OpenGL has no floating point.
1508 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1509 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1510 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1511 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1512 // OpenGL sets alpha to 1 on import
1513 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1514 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1515 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1520 Playback3DCommand command;
1521 command.command = Playback3DCommand::CONVERT_CMODEL;
1522 command.canvas = canvas;
1523 command.frame = output;
1524 command.dst_cmodel = dst_cmodel;
1525 send_command(&command);
1528 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1531 BC_WindowBase *window =
1532 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1534 window->enable_opengl();
1536 // Import into hardware
1537 command->frame->enable_opengl();
1538 command->frame->init_screen();
1539 command->frame->to_texture();
1541 // Colormodel permutation
1542 int src_cmodel = command->frame->get_color_model();
1543 int dst_cmodel = command->dst_cmodel;
1547 } cmodel_shader_table_t;
1548 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1550 static cmodel_shader_table_t cmodel_shader_table[] = {
1551 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1552 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1553 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1554 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1555 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1556 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1557 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1558 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1559 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1560 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1561 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1562 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1563 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1564 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1565 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1566 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1567 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1568 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1569 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1570 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1571 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1574 const char *shader = 0;
1575 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1576 for( int i=0; i<table_size; ++i ) {
1577 if( cmodel_shader_table[i].src == src_cmodel &&
1578 cmodel_shader_table[i].dst == dst_cmodel ) {
1579 shader = cmodel_shader_table[i].shader;
1580 type = cmodel_shader_table[i].typ;
1585 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1587 // command->frame->get_color_model(),
1588 // command->dst_cmodel,
1591 const char *shader_stack[9];
1592 memset(shader_stack,0, sizeof(shader_stack));
1593 int current_shader = 0;
1596 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1597 shader_stack[current_shader++] = shader;
1598 shader_stack[current_shader] = 0;
1599 unsigned int shader_id = VFrame::make_shader(shader_stack);
1601 command->frame->bind_texture(0);
1602 glUseProgram(shader_id);
1604 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1607 BC_GL_RGB_TO_YUV(shader_id);
1610 BC_GL_YUV_TO_RGB(shader_id);
1614 command->frame->draw_texture();
1615 if(shader) glUseProgram(0);
1616 command->frame->set_opengl_state(VFrame::SCREEN);
1620 command->canvas->unlock_canvas();
1624 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1626 Playback3DCommand command;
1627 command.command = Playback3DCommand::DO_FADE;
1628 command.canvas = canvas;
1629 command.frame = frame;
1630 command.alpha = fade;
1631 send_command(&command);
1634 void Playback3D::do_fade_sync(Playback3DCommand *command)
1637 BC_WindowBase *window =
1638 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1640 window->enable_opengl();
1641 switch( command->frame->get_opengl_state() ) {
1643 command->frame->to_texture();
1646 case VFrame::SCREEN:
1647 // Read back from PBuffer
1648 // Bind context to pbuffer
1649 command->frame->enable_opengl();
1650 command->frame->screen_to_texture();
1654 command->frame->enable_opengl();
1655 command->frame->init_screen();
1656 command->frame->bind_texture(0);
1658 // glClearColor(0.0, 0.0, 0.0, 0.0);
1659 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1660 glDisable(GL_BLEND);
1661 unsigned int frag_shader = 0;
1662 switch(command->frame->get_color_model())
1664 // For the alpha colormodels, the native function seems to multiply the
1665 // components by the alpha instead of just the alpha.
1669 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1674 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1675 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1680 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1686 glUseProgram(frag_shader);
1688 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1689 glUniform1i(variable, 0);
1690 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1691 glUniform1f(variable, command->alpha);
1694 command->frame->draw_texture();
1695 command->frame->set_opengl_state(VFrame::SCREEN);
1702 glColor4f(1, 1, 1, 1);
1703 glDisable(GL_BLEND);
1705 command->canvas->unlock_canvas();
1710 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1712 Playback3DCommand command;
1713 command.command = Playback3DCommand::PLUGIN;
1714 command.canvas = canvas;
1715 command.plugin_client = client;
1716 return send_command(&command);
1719 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1721 BC_WindowBase *window =
1722 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1724 window->enable_opengl();
1725 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1727 command->canvas->unlock_canvas();