4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define GL_GLEXT_PROTOTYPES
25 #include "bcsignals.h"
26 #include "bcwindowbase.h"
29 #include "condition.h"
31 #include "maskautos.h"
35 #include "overlayframe.inc"
36 #include "overlayframe.h"
37 #include "playback3d.h"
38 #include "pluginclient.h"
39 #include "pluginvclient.h"
40 #include "edlsession.h"
42 #include "transportque.inc"
60 // These should be passed to VFrame::make_shader to construct shaders.
61 // Can't hard code sampler2D
65 static const char *yuv_to_rgb_frag =
66 "uniform sampler2D tex;\n"
67 "uniform mat3 yuv_to_rgb_matrix;\n"
68 "uniform float yminf;\n"
71 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
72 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
73 " gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
76 static const char *yuva_to_yuv_frag =
77 "uniform sampler2D tex;\n"
80 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
81 " float a = yuva.a;\n"
82 " float anti_a = 1.0 - a;\n"
84 " yuva.g = yuva.g * a + 0.5 * anti_a;\n"
85 " yuva.b = yuva.b * a + 0.5 * anti_a;\n"
87 " gl_FragColor = yuva;\n"
90 static const char *yuva_to_rgb_frag =
91 "uniform sampler2D tex;\n"
92 "uniform mat3 yuv_to_rgb_matrix;\n"
93 "uniform float yminf;\n"
96 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
97 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
98 " yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
99 " yuva.rgb *= yuva.a;\n"
101 " gl_FragColor = yuva;\n"
104 static const char *rgb_to_yuv_frag =
105 "uniform sampler2D tex;\n"
106 "uniform mat3 rgb_to_yuv_matrix;\n"
107 "uniform float yminf;\n"
110 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
111 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
112 " rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
113 " gl_FragColor = rgba;\n"
117 static const char *rgba_to_rgb_frag =
118 "uniform sampler2D tex;\n"
121 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
122 " rgba.rgb *= rgba.a;\n"
124 " gl_FragColor = rgba;\n"
127 static const char *rgba_to_yuv_frag =
128 "uniform sampler2D tex;\n"
129 "uniform mat3 rgb_to_yuv_matrix;\n"
130 "uniform float yminf;\n"
133 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
134 " rgba.rgb *= rgba.a;\n"
136 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
137 " rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
138 " gl_FragColor = rgba;\n"
141 //static const char *rgba_to_rgb_flatten =
143 // " gl_FragColor.rgb *= gl_FragColor.a;\n"
144 // " gl_FragColor.a = 1.0;\n"
147 #define GL_STD_BLEND(FN) \
148 static const char *blend_##FN##_frag = \
149 "uniform sampler2D tex2;\n" \
150 "uniform vec2 tex2_dimensions;\n" \
151 "uniform float alpha;\n" \
153 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
155 " result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
156 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
157 " gl_FragColor = mix(canvas, result, alpha);\n" \
160 #define GL_VEC_BLEND(FN) \
161 static const char *blend_##FN##_frag = \
162 "uniform sampler2D tex2;\n" \
163 "uniform vec2 tex2_dimensions;\n" \
164 "uniform float alpha;\n" \
166 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
168 " result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
169 " result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
170 " result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
171 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
172 " result = clamp(result, 0.0, 1.0);\n" \
173 " gl_FragColor = mix(canvas, result, alpha);\n" \
191 static const char *blend_NORMAL_frag =
192 "uniform sampler2D tex2;\n"
193 "uniform vec2 tex2_dimensions;\n"
194 "uniform float alpha;\n"
196 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
197 " vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
198 " gl_FragColor = mix(canvas, result, alpha);\n"
202 static const char *blend_REPLACE_frag =
203 "uniform float alpha;\n"
208 static const char *blend_ADDITION_frag =
209 "uniform sampler2D tex2;\n"
210 "uniform vec2 tex2_dimensions;\n"
211 "uniform float alpha;\n"
213 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
214 " vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
215 " gl_FragColor = mix(canvas, result, alpha);\n"
219 static const char *blend_SUBTRACT_frag =
220 "uniform sampler2D tex2;\n"
221 "uniform vec2 tex2_dimensions;\n"
222 "uniform float alpha;\n"
224 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
225 " vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
226 " gl_FragColor = mix(canvas, result, alpha);\n"
229 GL_STD_BLEND(MULTIPLY);
230 GL_VEC_BLEND(DIVIDE);
233 GL_VEC_BLEND(DARKEN);
234 GL_VEC_BLEND(LIGHTEN);
236 GL_STD_BLEND(DST_ATOP);
237 GL_STD_BLEND(DST_IN);
238 GL_STD_BLEND(DST_OUT);
239 GL_STD_BLEND(DST_OVER);
241 GL_STD_BLEND(SRC_ATOP);
242 GL_STD_BLEND(SRC_IN);
243 GL_STD_BLEND(SRC_OUT);
244 GL_STD_BLEND(SRC_OVER);
248 GL_VEC_BLEND(OVERLAY);
249 GL_STD_BLEND(SCREEN);
252 GL_VEC_BLEND(HARDLIGHT);
253 GL_VEC_BLEND(SOFTLIGHT);
254 GL_VEC_BLEND(DIFFERENCE);
256 static const char *read_texture_frag =
257 "uniform sampler2D tex;\n"
260 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
263 static const char *in_vertex_frag =
264 "#version 430 // vertex shader\n"
267 " gl_Position = vec4(in_pos-vec3(0.5,0.5,0.), .5);\n"
270 static const char *feather_frag =
272 "layout(location=0) out vec4 color;\n"
273 "uniform sampler2D tex;\n"
274 "const int MAX = " SS(MAX_FEATHER) "+1;\n"
275 "uniform float psf[MAX];\n"
277 "uniform vec2 dxy;\n"
278 "uniform vec2 twh;\n"
281 " vec2 tc = gl_FragCoord.xy/textureSize(tex,0);\n"
282 " color = texture(tex, tc);\n"
283 " float c = color.r, f = c*psf[0];\n"
284 " for( int i=1; i<n; ++i ) {\n"
285 " vec2 dd = float(i)*dxy;\n"
286 " vec2 a = tc+dd, ac = min(max(vec2(0.),a), twh);\n"
287 " vec2 b = tc-dd, bc = min(max(vec2(0.),b), twh);\n"
288 " float fa = texture2D(tex, ac).r * psf[i];\n"
289 " float fb = texture2D(tex, bc).r * psf[i];\n"
290 " float m = max(fa, fb);\n"
291 " if( f < m ) f = m;\n"
293 " if( c < f ) color = vec4(f);\n"
296 static const char *max_frag =
298 "layout(location=0) out vec4 color;\n"
299 "uniform sampler2D tex;\n"
300 "uniform sampler2D tex1;\n"
305 " vec2 tc = gl_FragCoord.xy/textureSize(tex,0);\n"
306 " color = texture2D(tex1, tc);\n"
307 " float c = texture2D(tex, tc).r;\n"
308 " float b = r<0 ? 1. : 0.;\n"
309 " if( c == b ) return;\n"
310 " float iv = v>=0. ? 1. : -1.;\n"
311 " float rr = r!=0. ? r : 1.;\n"
312 " float rv = rr*v>=0. ? 1. : -1.;\n"
313 " float vv = v>=0. ? 1.-v : 1.+v;\n"
314 " float fg = rv>0. ? vv : 1.;\n"
315 " float bg = rv>0. ? 1. : vv;\n"
316 " float a = c*fg + (1.-c)*bg;\n"
317 " if( iv*(color.a-a) > 0. ) color = vec4(a);\n"
320 static const char *multiply_mask4_frag =
321 "uniform sampler2D tex;\n"
322 "uniform sampler2D tex1;\n"
325 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
326 " gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st).r;\n"
329 static const char *multiply_mask3_frag =
330 "uniform sampler2D tex;\n"
331 "uniform sampler2D tex1;\n"
334 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
335 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
336 " gl_FragColor.rgb *= vec3(a, a, a);\n"
339 static const char *multiply_yuvmask3_frag =
340 "uniform sampler2D tex;\n"
341 "uniform sampler2D tex1;\n"
344 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
345 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
346 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
347 " gl_FragColor.rgb *= vec3(a, a, a);\n"
348 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
351 static const char *fade_rgba_frag =
352 "uniform sampler2D tex;\n"
353 "uniform float alpha;\n"
356 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
357 " gl_FragColor.a *= alpha;\n"
360 static const char *fade_yuv_frag =
361 "uniform sampler2D tex;\n"
362 "uniform float alpha;\n"
365 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
366 " gl_FragColor.r *= alpha;\n"
367 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
368 " gl_FragColor.g *= alpha;\n"
369 " gl_FragColor.b *= alpha;\n"
370 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
376 Playback3DCommand::Playback3DCommand()
377 : BC_SynchronousCommand()
383 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
385 Playback3DCommand *ptr = (Playback3DCommand*)command;
386 this->canvas = ptr->canvas;
387 this->is_cleared = ptr->is_cleared;
389 this->in_x1 = ptr->in_x1;
390 this->in_y1 = ptr->in_y1;
391 this->in_x2 = ptr->in_x2;
392 this->in_y2 = ptr->in_y2;
393 this->out_x1 = ptr->out_x1;
394 this->out_y1 = ptr->out_y1;
395 this->out_x2 = ptr->out_x2;
396 this->out_y2 = ptr->out_y2;
397 this->alpha = ptr->alpha;
398 this->mode = ptr->mode;
399 this->interpolation_type = ptr->interpolation_type;
401 this->input = ptr->input;
402 this->start_position_project = ptr->start_position_project;
403 this->keyframe_set = ptr->keyframe_set;
404 this->keyframe = ptr->keyframe;
405 this->default_auto = ptr->default_auto;
406 this->plugin_client = ptr->plugin_client;
407 this->want_texture = ptr->want_texture;
408 this->is_nested = ptr->is_nested;
409 this->dst_cmodel = ptr->dst_cmodel;
411 BC_SynchronousCommand::copy_from(command);
414 Playback3D::Playback3D(MWindow *mwindow)
417 this->mwindow = mwindow;
421 Playback3D::~Playback3D()
428 BC_SynchronousCommand* Playback3D::new_command()
430 return new Playback3DCommand;
435 void Playback3D::handle_command(BC_SynchronousCommand *command)
437 //printf("Playback3D::handle_command 1 %d\n", command->command);
438 switch(command->command)
440 case Playback3DCommand::WRITE_BUFFER:
441 write_buffer_sync((Playback3DCommand*)command);
444 case Playback3DCommand::CLEAR_OUTPUT:
445 clear_output_sync((Playback3DCommand*)command);
448 case Playback3DCommand::CLEAR_INPUT:
449 clear_input_sync((Playback3DCommand*)command);
452 case Playback3DCommand::DO_CAMERA:
453 do_camera_sync((Playback3DCommand*)command);
456 case Playback3DCommand::OVERLAY:
457 overlay_sync((Playback3DCommand*)command);
460 case Playback3DCommand::DO_FADE:
461 do_fade_sync((Playback3DCommand*)command);
464 case Playback3DCommand::DO_MASK:
465 do_mask_sync((Playback3DCommand*)command);
468 case Playback3DCommand::PLUGIN:
469 run_plugin_sync((Playback3DCommand*)command);
472 case Playback3DCommand::COPY_FROM:
473 copy_from_sync((Playback3DCommand*)command);
476 case Playback3DCommand::CONVERT_CMODEL:
477 convert_cmodel_sync((Playback3DCommand*)command);
480 // case Playback3DCommand::DRAW_REFRESH:
481 // draw_refresh_sync((Playback3DCommand*)command);
484 //printf("Playback3D::handle_command 10\n");
490 void Playback3D::copy_from(Canvas *canvas,
495 Playback3DCommand command;
496 command.command = Playback3DCommand::COPY_FROM;
497 command.canvas = canvas;
500 command.want_texture = want_texture;
501 send_command(&command);
504 void Playback3D::copy_from_sync(Playback3DCommand *command)
507 BC_WindowBase *window =
508 command->canvas->lock_canvas("Playback3D::copy_from_sync");
510 window->enable_opengl();
512 int w = command->input->get_w();
513 int h = command->input->get_h();
515 if(command->input->get_opengl_state() == VFrame::SCREEN &&
516 w == command->frame->get_w() && h == command->frame->get_h())
518 // printf("Playback3D::copy_from_sync 1 %d %d %d %d %d\n",
519 // command->input->get_w(),
520 // command->input->get_h(),
521 // command->frame->get_w(),
522 // command->frame->get_h(),
523 // command->frame->get_color_model());
525 // With NVidia at least
528 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
533 if(command->want_texture)
535 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
536 // Screen_to_texture requires the source pbuffer enabled.
537 command->input->enable_opengl();
538 command->frame->screen_to_texture();
539 command->frame->set_opengl_state(VFrame::TEXTURE);
544 command->input->to_texture();
545 command->input->bind_texture(0);
546 command->frame->enable_opengl();
547 command->frame->init_screen();
548 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
549 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
551 glUseProgram(shader);
552 int variable = glGetUniformLocation(shader, "tex");
553 glUniform1i(variable, 0);
554 BC_GL_YUV_TO_RGB(shader);
558 command->input->draw_texture(1);
559 command->frame->screen_to_ram();
565 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
566 command->input->get_opengl_state(), w, h,
567 command->frame->get_w(), command->frame->get_h());
570 command->canvas->unlock_canvas();
577 // void Playback3D::draw_refresh(Canvas *canvas,
588 // Playback3DCommand command;
589 // command.command = Playback3DCommand::DRAW_REFRESH;
590 // command.canvas = canvas;
591 // command.frame = frame;
592 // command.in_x1 = in_x1;
593 // command.in_y1 = in_y1;
594 // command.in_x2 = in_x2;
595 // command.in_y2 = in_y2;
596 // command.out_x1 = out_x1;
597 // command.out_y1 = out_y1;
598 // command.out_x2 = out_x2;
599 // command.out_y2 = out_y2;
600 // send_command(&command);
603 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
606 // BC_WindowBase *window =
607 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
609 // window->enable_opengl();
611 // // Read output pbuffer back to RAM in project colormodel
612 // // RGB 8bit is fastest for OpenGL to read back.
613 // command->frame->reallocate(0,
617 // command->frame->get_w(),
618 // command->frame->get_h(),
621 // command->frame->screen_to_ram();
623 // window->clear_box(0,
627 // window->draw_vframe(command->frame,
628 // (int)command->out_x1,
629 // (int)command->out_y1,
630 // (int)(command->out_x2 - command->out_x1),
631 // (int)(command->out_y2 - command->out_y1),
632 // (int)command->in_x1,
633 // (int)command->in_y1,
634 // (int)(command->in_x2 - command->in_x1),
635 // (int)(command->in_y2 - command->in_y1),
639 // command->canvas->unlock_canvas();
647 void Playback3D::write_buffer(Canvas *canvas,
659 Playback3DCommand command;
660 command.command = Playback3DCommand::WRITE_BUFFER;
661 command.canvas = canvas;
662 command.frame = frame;
663 command.in_x1 = in_x1;
664 command.in_y1 = in_y1;
665 command.in_x2 = in_x2;
666 command.in_y2 = in_y2;
667 command.out_x1 = out_x1;
668 command.out_y1 = out_y1;
669 command.out_x2 = out_x2;
670 command.out_y2 = out_y2;
671 command.is_cleared = is_cleared;
672 send_command(&command);
676 void Playback3D::write_buffer_sync(Playback3DCommand *command)
679 BC_WindowBase *window =
680 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
682 window->enable_opengl();
683 // Update hidden cursor
684 window->update_video_cursor();
685 command->frame->enable_opengl();
686 command->frame->init_screen();
687 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
688 int frame_state = command->frame->get_opengl_state();
689 if( frame_state != VFrame::TEXTURE )
690 command->frame->to_texture();
691 if( frame_state != VFrame::RAM ) {
692 command->in_y1 = command->frame->get_h() - command->in_y1;
693 command->in_y2 = command->frame->get_h() - command->in_y2;
695 window->enable_opengl();
696 draw_output(command, 1);
697 command->frame->set_opengl_state(frame_state);
699 command->canvas->unlock_canvas();
705 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
708 int texture_id = command->frame->get_texture_id();
709 BC_WindowBase *window = command->canvas->get_canvas();
711 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
713 // command->canvas->get_canvas());
718 // If virtual console is being used, everything in this function has
719 // already been done except the page flip.
722 canvas_w = window->get_w();
723 canvas_h = window->get_h();
724 VFrame::init_screen(canvas_w, canvas_h);
725 int color_model = command->frame->get_color_model();
726 int is_yuv = BC_CModels::is_yuv(color_model);
728 if(!command->is_cleared)
730 // If we get here, the virtual console was not used.
731 color_frame(command, 0,0,0,0);
735 // Undo any previous shader settings
736 command->frame->bind_texture(0);
738 // Convert colormodel
739 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
741 glUseProgram(shader);
742 // Set texture unit of the texture
743 int variable = glGetUniformLocation(shader, "tex");
744 glUniform1i(variable, 0);
745 BC_GL_YUV_TO_RGB(shader);
748 // if(BC_CModels::components(color_model) == 4)
750 // glEnable(GL_BLEND);
751 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
754 command->frame->draw_texture(
755 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
756 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
760 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
761 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
762 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
766 command->canvas->get_canvas()->flip_opengl();
773 void Playback3D::color_frame(Playback3DCommand *command,
774 float r, float g, float b, float a)
777 glClearColor(r, g, b, a);
778 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
783 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
785 Playback3DCommand command;
786 command.command = Playback3DCommand::CLEAR_OUTPUT;
787 command.canvas = canvas;
788 command.frame = output;
789 send_command(&command);
792 void Playback3D::clear_output_sync(Playback3DCommand *command)
795 BC_WindowBase *window =
796 command->canvas->lock_canvas("Playback3D::clear_output_sync");
798 // If we get here, the virtual console is being used.
799 command->canvas->get_canvas()->enable_opengl();
801 int color = BLACK, alpha = 0;
802 // Using pbuffer for refresh frame.
803 if( command->frame ) {
804 command->frame->enable_opengl();
805 color = command->frame->get_clear_color();
806 alpha = command->frame->get_clear_alpha();
807 int color_model = command->canvas->mwindow->edl->session->color_model;
808 is_yuv = BC_CModels::is_yuv(color_model);
811 int r = (color>>16) & 0xff;
812 int g = (color>>8) & 0xff;
813 int b = (color>>0) & 0xff;
814 if( is_yuv ) YUV::yuv.rgb_to_yuv_8(r, g, b);
815 color_frame(command, r/255.f, g/255.f, b/255.f, a/255.f);
817 command->canvas->unlock_canvas();
822 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
824 Playback3DCommand command;
825 command.command = Playback3DCommand::CLEAR_INPUT;
826 command.canvas = canvas;
827 command.frame = frame;
828 send_command(&command);
831 void Playback3D::clear_input_sync(Playback3DCommand *command)
834 BC_WindowBase *window =
835 command->canvas->lock_canvas("Playback3D::clear_input_sync");
837 command->canvas->get_canvas()->enable_opengl();
838 command->frame->enable_opengl();
839 command->frame->clear_pbuffer();
840 command->frame->set_opengl_state(VFrame::SCREEN);
842 command->canvas->unlock_canvas();
846 void Playback3D::do_camera(Canvas *canvas,
858 Playback3DCommand command;
859 command.command = Playback3DCommand::DO_CAMERA;
860 command.canvas = canvas;
861 command.input = input;
862 command.frame = output;
863 command.in_x1 = in_x1;
864 command.in_y1 = in_y1;
865 command.in_x2 = in_x2;
866 command.in_y2 = in_y2;
867 command.out_x1 = out_x1;
868 command.out_y1 = out_y1;
869 command.out_x2 = out_x2;
870 command.out_y2 = out_y2;
871 send_command(&command);
874 void Playback3D::do_camera_sync(Playback3DCommand *command)
877 BC_WindowBase *window =
878 command->canvas->lock_canvas("Playback3D::do_camera_sync");
880 command->canvas->get_canvas()->enable_opengl();
882 command->input->to_texture();
883 command->frame->enable_opengl();
884 command->frame->init_screen();
885 command->frame->clear_pbuffer();
887 command->input->bind_texture(0);
888 // Must call draw_texture in input frame to get the texture coordinates right.
890 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
896 // (float)command->input->get_h() - command->out_y1,
898 // (float)command->input->get_h() - command->out_y2);
899 command->input->draw_texture(
900 command->in_x1, command->in_y2,
901 command->in_x2, command->in_y1,
903 (float)command->frame->get_h() - command->out_y1,
905 (float)command->frame->get_h() - command->out_y2);
908 command->frame->set_opengl_state(VFrame::SCREEN);
909 command->frame->screen_to_ram();
911 command->canvas->unlock_canvas();
915 void Playback3D::overlay(Canvas *canvas, VFrame *input,
916 float in_x1, float in_y1, float in_x2, float in_y2,
917 float out_x1, float out_y1, float out_x2, float out_y2,
918 float alpha, int mode, int interpolation_type,
919 VFrame *output, int is_nested)
921 Playback3DCommand command;
922 command.command = Playback3DCommand::OVERLAY;
923 command.canvas = canvas;
924 command.frame = output;
925 command.input = input;
926 command.in_x1 = in_x1;
927 command.in_y1 = in_y1;
928 command.in_x2 = in_x2;
929 command.in_y2 = in_y2;
930 command.out_x1 = out_x1;
931 command.out_y1 = out_y1;
932 command.out_x2 = out_x2;
933 command.out_y2 = out_y2;
934 command.alpha = alpha;
936 command.interpolation_type = interpolation_type;
937 command.is_nested = is_nested;
938 send_command(&command);
941 void Playback3D::overlay_sync(Playback3DCommand *command)
944 // To do these operations, we need to copy the input buffer to a texture
945 // and blend 2 textures in a shader
946 static const char * const overlay_shaders[TRANSFER_TYPES] = {
947 blend_NORMAL_frag, // TRANSFER_NORMAL
948 blend_ADDITION_frag, // TRANSFER_ADDITION
949 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
950 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
951 blend_DIVIDE_frag, // TRANSFER_DIVIDE
952 blend_REPLACE_frag, // TRANSFER_REPLACE
953 blend_MAX_frag, // TRANSFER_MAX
954 blend_MIN_frag, // TRANSFER_MIN
955 blend_DARKEN_frag, // TRANSFER_DARKEN
956 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
957 blend_DST_frag, // TRANSFER_DST
958 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
959 blend_DST_IN_frag, // TRANSFER_DST_IN
960 blend_DST_OUT_frag, // TRANSFER_DST_OUT
961 blend_DST_OVER_frag, // TRANSFER_DST_OVER
962 blend_SRC_frag, // TRANSFER_SRC
963 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
964 blend_SRC_IN_frag, // TRANSFER_SRC_IN
965 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
966 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
967 blend_AND_frag, // TRANSFER_AND
968 blend_OR_frag, // TRANSFER_OR
969 blend_XOR_frag, // TRANSFER_XOR
970 blend_OVERLAY_frag, // TRANSFER_OVERLAY
971 blend_SCREEN_frag, // TRANSFER_SCREEN
972 blend_BURN_frag, // TRANSFER_BURN
973 blend_DODGE_frag, // TRANSFER_DODGE
974 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
975 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
976 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
979 BC_WindowBase *window =
980 command->canvas->lock_canvas("Playback3D::overlay_sync");
982 // Make sure OpenGL is enabled first.
983 window->enable_opengl();
984 window->update_video_cursor();
986 glColor4f(1, 1, 1, 1);
990 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
991 switch( command->input->get_opengl_state() ) {
992 // Upload texture and composite to screen
994 command->input->to_texture();
996 // Just composite texture to screen
997 case VFrame::TEXTURE:
999 // read from PBuffer to texture, then composite texture to screen
1000 case VFrame::SCREEN:
1001 command->input->enable_opengl();
1002 command->input->screen_to_texture();
1005 printf("Playback3D::overlay_sync unknown state\n");
1009 if(command->frame) {
1010 // Render to PBuffer
1011 command->frame->enable_opengl();
1012 command->frame->set_opengl_state(VFrame::SCREEN);
1013 canvas_w = command->frame->get_w();
1014 canvas_h = command->frame->get_h();
1018 window->enable_opengl();
1019 canvas_w = window->get_w();
1020 canvas_h = window->get_h();
1024 const char *shader_stack[16];
1025 memset(shader_stack,0, sizeof(shader_stack));
1026 int total_shaders = 0, need_matrix = 0;
1028 VFrame::init_screen(canvas_w, canvas_h);
1031 command->input->bind_texture(0);
1033 // Convert colormodel to RGB if not nested.
1034 // The color model setting in the output frame is ignored.
1035 // if( command->is_nested <= 0 && // not nested
1036 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
1038 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
1042 #define add_shader(s) \
1043 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1044 shader_stack[total_shaders++] = s
1046 switch(command->mode) {
1047 case TRANSFER_REPLACE:
1048 // This requires overlaying an alpha multiplied image on a black screen.
1049 if( command->input->get_texture_components() != 4 ) break;
1050 add_shader(overlay_shaders[command->mode]);
1053 enable_overlay_texture(command);
1054 add_shader(overlay_shaders[command->mode]);
1058 // if to flatten alpha
1059 // if( command->is_nested < 0 ) {
1060 // switch(command->input->get_color_model()) {
1061 //// yuv has already been converted to rgb
1062 // case BC_YUVA8888:
1063 // case BC_RGBA_FLOAT:
1064 // case BC_RGBA8888:
1065 // add_shader(rgba_to_rgb_flatten);
1072 unsigned int shader = !shader_stack[0] ? 0 :
1073 VFrame::make_shader(shader_stack);
1075 glUseProgram(shader);
1076 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1077 // Set texture unit of the texture
1078 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1079 // Set texture unit of the temp texture
1080 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1082 int variable = glGetUniformLocation(shader, "alpha");
1083 glUniform1f(variable, command->alpha);
1084 // Set dimensions of the temp texture
1086 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1087 (float)temp_texture->get_texture_w(),
1088 (float)temp_texture->get_texture_h());
1094 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1095 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1096 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1098 command->input->draw_texture(
1099 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1100 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1101 !command->is_nested);
1104 // Delete temp texture
1106 delete temp_texture;
1108 glActiveTexture(GL_TEXTURE1);
1109 glDisable(GL_TEXTURE_2D);
1111 glActiveTexture(GL_TEXTURE0);
1112 glDisable(GL_TEXTURE_2D);
1114 command->canvas->unlock_canvas();
1118 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1121 glDisable(GL_BLEND);
1123 glActiveTexture(GL_TEXTURE1);
1124 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1125 command->input->get_color_model());
1126 temp_texture->bind(1);
1128 // Read canvas into texture
1129 glReadBuffer(GL_BACK);
1130 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1135 void Playback3D::do_mask(Canvas *canvas,
1137 int64_t start_position_project,
1138 MaskAutos *keyframe_set,
1140 MaskAuto *default_auto)
1142 Playback3DCommand command;
1143 command.command = Playback3DCommand::DO_MASK;
1144 command.canvas = canvas;
1145 command.frame = output;
1146 command.start_position_project = start_position_project;
1147 command.keyframe_set = keyframe_set;
1148 command.keyframe = keyframe;
1149 command.default_auto = default_auto;
1151 send_command(&command);
1155 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1157 int x1 = iy1 < iy2 ? ix1 : ix2;
1158 int y1 = iy1 < iy2 ? iy1 : iy2;
1159 int x2 = iy1 < iy2 ? ix2 : ix1;
1160 int y2 = iy1 < iy2 ? iy2 : iy1;
1163 int dx = x2-x1, dy = y2-y1;
1164 int dx2 = 2*dx, dy2 = 2*dy;
1165 if( dx < 0 ) dx = -dx;
1166 int m = dx > dy ? dx : dy, n = m;
1168 if( dx2 >= 0 ) do { /* +Y, +X */
1169 spots.append(x, y++);
1170 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
1171 } while( --n >= 0 );
1172 else do { /* +Y, -X */
1173 spots.append(x, y++);
1174 if( (m += dx2) < 0 ) { m += dy2; --x; }
1175 } while( --n >= 0 );
1178 if( dx2 >= 0 ) do { /* +X, +Y */
1179 spots.append(x++, y);
1180 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
1181 } while( --n >= 0 );
1182 else do { /* -X, +Y */
1183 spots.append(x--, y);
1184 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
1185 } while( --n >= 0 );
1190 class fb_texture : public BC_Texture
1193 fb_texture(int w, int h, int colormodel);
1195 void bind(int texture_unit);
1196 void read_screen(int x, int y, int w, int h);
1197 void set_output_texture();
1198 void unset_output_texture();
1202 fb_texture::fb_texture(int w, int h, int colormodel)
1203 : BC_Texture(w, h, colormodel)
1206 // glGenRenderbuffers(1, &rb);
1207 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1208 // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1209 glGenFramebuffers(1, &fb);
1210 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1211 // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1214 fb_texture::~fb_texture()
1216 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1217 glDeleteFramebuffers(1, (GLuint *)&fb);
1218 // glBindRenderbuffer(GL_RENDERBUFFER, 0);
1219 // glGenRenderbuffers(1, &rb);
1222 void fb_texture::bind(int texture_unit)
1224 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1225 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1226 BC_Texture::bind(texture_unit);
1229 void fb_texture::read_screen(int x, int y, int w, int h)
1232 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1233 glReadBuffer(GL_BACK);
1234 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1237 void fb_texture::set_output_texture()
1239 GLenum at = GL_COLOR_ATTACHMENT0;
1240 glFramebufferTexture(GL_FRAMEBUFFER, at, get_texture_id(), 0);
1241 GLenum dbo[1] = { at, }; // bind layout(location=0) out vec4 color;
1242 glDrawBuffers(1, dbo);
1243 int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1244 if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1245 printf("glDrawBuffer error 0x%04x\n", ret);
1249 void fb_texture::unset_output_texture()
1251 glDrawBuffers(0, 0);
1252 int at = GL_COLOR_ATTACHMENT0;
1253 glFramebufferTexture(GL_FRAMEBUFFER, at, 0, 0);
1254 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1255 glDisable(GL_TEXTURE_2D);
1259 class zglTessData : public ArrayList<double *>
1262 zglTessData() { set_array_delete(); }
1263 ~zglTessData() { remove_all_objects(); }
1266 static void combineData(GLdouble coords[3],
1267 GLdouble *vertex_data[4], GLfloat weight[4],
1268 GLdouble **outData, void *data)
1270 zglTessData *invented = (zglTessData *)data;
1271 GLdouble *vertex = new double[6];
1272 invented->append(vertex);
1273 vertex[0] = coords[0];
1274 vertex[1] = coords[1];
1275 vertex[2] = coords[2];
1276 for( int i=3; i<6; ++i ) {
1278 for( int k=0; k<4; ++k ) {
1279 if( !weight[k] || !vertex_data[k] ) continue;
1280 v += weight[k] * vertex_data[k][i];
1288 static void zglBegin(GLenum mode) { glBegin(mode); }
1289 static void zglEnd() { glEnd(); }
1290 static void zglVertex(const GLdouble *v) { glVertex3dv(v); }
1294 void Playback3D::do_mask_sync(Playback3DCommand *command)
1297 BC_WindowBase *window =
1298 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1300 window->enable_opengl();
1302 switch( command->frame->get_opengl_state() ) {
1304 // upload frame to the texture
1305 command->frame->to_texture();
1308 case VFrame::SCREEN:
1309 // Read back from PBuffer
1310 // Bind context to pbuffer
1311 command->frame->enable_opengl();
1312 command->frame->screen_to_texture();
1316 // Initialize coordinate system
1317 command->frame->enable_opengl();
1318 command->frame->init_screen();
1319 int color_model = command->frame->get_color_model();
1320 int w = command->frame->get_w();
1321 int h = command->frame->get_h();
1323 float faders[SUBMASKS], feathers[SUBMASKS], cc = 1;
1324 MaskPoints point_set[SUBMASKS];
1325 // Draw every submask as a new polygon
1326 int total_submasks = command->keyframe_set->total_submasks(
1327 command->start_position_project, PLAY_FORWARD);
1328 int show_mask = command->keyframe_set->track->masks;
1330 for(int k = 0; k < total_submasks; k++) {
1331 MaskPoints &points = point_set[k];
1332 command->keyframe_set->get_points(&points,
1333 k, command->start_position_project, PLAY_FORWARD);
1334 float fader = command->keyframe_set->get_fader(
1335 command->start_position_project, k, PLAY_FORWARD);
1336 float v = fader/100.;
1338 float feather = command->keyframe_set->get_feather(
1339 command->start_position_project, k, PLAY_FORWARD);
1340 feathers[k] = feather;
1341 MaskEdge &edge = *edges.append(new MaskEdge());
1342 if( !v || !((show_mask>>k) & 1) || !points.size() ) continue;
1343 edge.load(point_set[k], h);
1344 if( v >= 0 ) continue;
1346 if( cc > vv ) cc = vv;
1349 fb_texture *mask = new fb_texture(w, h, color_model);
1350 mask->set_output_texture();
1351 glClearColor(cc, cc, cc, cc);
1352 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1353 mask->unset_output_texture();
1355 unsigned int feather_shader =
1356 VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1357 unsigned int max_shader =
1358 VFrame::make_shader(0, in_vertex_frag, max_frag, 0);
1359 if( feather_shader && max_shader ) {
1360 fb_texture *in = new fb_texture(w, h, color_model);
1361 fb_texture *out = new fb_texture(w, h, color_model);
1362 float tw = 1./out->get_texture_w(), th = 1./out->get_texture_h();
1363 float tw1 = (w-1)*tw, th1 = (h-1)*th;
1364 for(int k = 0; k < total_submasks; k++) {
1365 MaskEdge &edge = *edges[k];
1366 if( edge.size() < 3 ) continue;
1367 float r = feathers[k], v = faders[k];
1368 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1369 glActiveTexture(GL_TEXTURE0);
1370 glDisable(GL_TEXTURE_2D);
1371 float b = r>=0 ? 0. : 1.;
1372 float f = r>=0 ? 1. : 0.;
1373 glClearColor(b, b, b, b);
1374 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1375 glColor4f(f, f, f, f);
1376 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1377 int display_list = glGenLists(1);
1379 glNewList(display_list, GL_COMPILE);
1380 glBegin(GL_POLYGON);
1381 MaskCoord *c = &edge[0];
1382 for( int i=edge.size(); --i>=0; ++c )
1383 glVertex2f(c->x, c->y);
1386 glCallList(display_list);
1388 { zglTessData invented;
1389 GLUtesselator *tess = gluNewTess();
1390 gluTessProperty(tess, GLU_TESS_TOLERANCE, 0.5);
1391 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex);
1392 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1393 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1394 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1395 glNewList(display_list, GL_COMPILE);
1396 gluTessBeginPolygon(tess, &invented);
1397 gluTessBeginContour(tess);
1398 MaskCoord *c = &edge[0];
1399 for( int i=edge.size(); --i>=0; ++c )
1400 gluTessVertex(tess, (GLdouble *)c, c);
1401 gluTessEndContour(tess);
1402 gluTessEndPolygon(tess);
1404 glCallList(display_list);
1405 gluDeleteTess(tess); }
1407 glDeleteLists(1, display_list);
1408 in->read_screen(0,0, w,h);
1409 //in->write_tex("/tmp/in0.ppm");
1411 double sig2 = -log(255.0)/(r*r);
1412 int n = abs((int)r) + 1;
1413 if( n > MAX_FEATHER+1 ) n = MAX_FEATHER+1;
1414 float psf[n]; // point spot fn
1415 for( int i=0; i<n; ++i )
1416 psf[i] = exp(i*i * sig2);
1417 glUseProgram(feather_shader);
1418 glUniform1fv(glGetUniformLocation(feather_shader, "psf"), n, psf);
1419 glUniform1i(glGetUniformLocation(feather_shader, "n"), n);
1420 glUniform2f(glGetUniformLocation(feather_shader, "dxy"), tw, 0.);
1421 glUniform2f(glGetUniformLocation(feather_shader, "twh"), tw1, th1);
1422 glUniform1i(glGetUniformLocation(feather_shader, "tex"), 0);
1424 out->set_output_texture();
1425 out->draw_texture(0,0, w,h, 0,0, w,h);
1426 out->unset_output_texture();
1427 //out->write_tex("/tmp/out1.ppm");
1428 fb_texture *t = in; in = out; out = t;
1429 glUniform2f(glGetUniformLocation(feather_shader, "dxy"), 0., th);
1431 out->set_output_texture();
1432 out->draw_texture(0,0, w,h, 0,0, w,h);
1433 out->unset_output_texture();
1434 //out->write_tex("/tmp/out2.ppm");
1436 t = in; in = out; out = t;
1439 glUseProgram(max_shader);
1441 //in->write_tex("/tmp/in1.ppm");
1442 //mask->write_tex("/tmp/mask1.ppm");
1444 glUniform1i(glGetUniformLocation(max_shader, "tex"), 0);
1445 glUniform1i(glGetUniformLocation(max_shader, "tex1"), 1);
1446 glUniform1f(glGetUniformLocation(max_shader, "r"), r);
1447 glUniform1f(glGetUniformLocation(max_shader, "v"), v);
1448 glViewport(0,0, w,h);
1449 out->set_output_texture();
1450 out->draw_texture(0,0, w,h, 0,0, w,h);
1451 out->unset_output_texture();
1453 fb_texture *t = mask; mask = out; out = t;
1454 //mask->write_tex("/tmp/mask2.ppm");
1460 const char *alpha_shader = BC_CModels::has_alpha(color_model) ?
1461 multiply_mask4_frag :
1462 !BC_CModels::is_yuv(color_model) ?
1463 multiply_mask3_frag :
1464 multiply_yuvmask3_frag;
1465 unsigned int shader = VFrame::make_shader(0, alpha_shader, 0);
1466 glUseProgram(shader);
1468 command->frame->bind_texture(0);
1469 mask->BC_Texture::bind(1);
1470 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1471 glUniform1i(glGetUniformLocation(shader, "tex1"), 1);
1473 command->frame->draw_texture();
1474 command->frame->set_opengl_state(VFrame::SCREEN);
1477 glColor4f(1, 1, 1, 1);
1478 glActiveTexture(GL_TEXTURE0);
1479 glDisable(GL_TEXTURE_2D);
1480 window->enable_opengl();
1482 command->canvas->unlock_canvas();
1487 void Playback3D::convert_cmodel(Canvas *canvas,
1491 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1492 int src_cmodel = output->get_color_model();
1494 (output->get_opengl_state() == VFrame::TEXTURE ||
1495 output->get_opengl_state() == VFrame::SCREEN) &&
1496 // OpenGL has no floating point.
1497 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1498 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1499 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1500 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1501 // OpenGL sets alpha to 1 on import
1502 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1503 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1504 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1509 Playback3DCommand command;
1510 command.command = Playback3DCommand::CONVERT_CMODEL;
1511 command.canvas = canvas;
1512 command.frame = output;
1513 command.dst_cmodel = dst_cmodel;
1514 send_command(&command);
1517 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1520 BC_WindowBase *window =
1521 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1523 window->enable_opengl();
1525 // Import into hardware
1526 command->frame->enable_opengl();
1527 command->frame->init_screen();
1528 command->frame->to_texture();
1530 // Colormodel permutation
1531 int src_cmodel = command->frame->get_color_model();
1532 int dst_cmodel = command->dst_cmodel;
1536 } cmodel_shader_table_t;
1537 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1539 static cmodel_shader_table_t cmodel_shader_table[] = {
1540 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1541 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1542 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1543 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1544 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1545 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1546 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1547 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1548 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1549 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1550 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1551 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1552 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1553 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1554 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1555 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1556 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1557 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1558 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1559 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1560 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1563 const char *shader = 0;
1564 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1565 for( int i=0; i<table_size; ++i ) {
1566 if( cmodel_shader_table[i].src == src_cmodel &&
1567 cmodel_shader_table[i].dst == dst_cmodel ) {
1568 shader = cmodel_shader_table[i].shader;
1569 type = cmodel_shader_table[i].typ;
1574 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1576 // command->frame->get_color_model(),
1577 // command->dst_cmodel,
1580 const char *shader_stack[9];
1581 memset(shader_stack,0, sizeof(shader_stack));
1582 int current_shader = 0;
1585 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1586 shader_stack[current_shader++] = shader;
1587 shader_stack[current_shader] = 0;
1588 unsigned int shader_id = VFrame::make_shader(shader_stack);
1590 command->frame->bind_texture(0);
1591 glUseProgram(shader_id);
1593 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1596 BC_GL_RGB_TO_YUV(shader_id);
1599 BC_GL_YUV_TO_RGB(shader_id);
1603 command->frame->draw_texture();
1604 if(shader) glUseProgram(0);
1605 command->frame->set_opengl_state(VFrame::SCREEN);
1609 command->canvas->unlock_canvas();
1613 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1615 Playback3DCommand command;
1616 command.command = Playback3DCommand::DO_FADE;
1617 command.canvas = canvas;
1618 command.frame = frame;
1619 command.alpha = fade;
1620 send_command(&command);
1623 void Playback3D::do_fade_sync(Playback3DCommand *command)
1626 BC_WindowBase *window =
1627 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1629 window->enable_opengl();
1630 switch( command->frame->get_opengl_state() ) {
1632 command->frame->to_texture();
1635 case VFrame::SCREEN:
1636 // Read back from PBuffer
1637 // Bind context to pbuffer
1638 command->frame->enable_opengl();
1639 command->frame->screen_to_texture();
1643 command->frame->enable_opengl();
1644 command->frame->init_screen();
1645 command->frame->bind_texture(0);
1647 // glClearColor(0.0, 0.0, 0.0, 0.0);
1648 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1649 glDisable(GL_BLEND);
1650 unsigned int frag_shader = 0;
1651 switch(command->frame->get_color_model())
1653 // For the alpha colormodels, the native function seems to multiply the
1654 // components by the alpha instead of just the alpha.
1658 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1663 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1664 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1669 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1675 glUseProgram(frag_shader);
1677 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1678 glUniform1i(variable, 0);
1679 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1680 glUniform1f(variable, command->alpha);
1683 command->frame->draw_texture();
1684 command->frame->set_opengl_state(VFrame::SCREEN);
1691 glColor4f(1, 1, 1, 1);
1692 glDisable(GL_BLEND);
1694 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 BC_WindowBase *window =
1711 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1713 window->enable_opengl();
1714 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1716 command->canvas->unlock_canvas();