4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define GL_GLEXT_PROTOTYPES
26 #include "bcsignals.h"
27 #include "bcwindowbase.h"
30 #include "condition.h"
32 #include "maskautos.h"
36 #include "overlayframe.inc"
37 #include "overlayframe.h"
38 #include "playback3d.h"
39 #include "pluginclient.h"
40 #include "pluginvclient.h"
41 #include "edlsession.h"
43 #include "transportque.inc"
61 // These should be passed to VFrame::make_shader to construct shaders.
62 // Can't hard code sampler2D
66 static const char *yuv_to_rgb_frag =
67 "uniform sampler2D tex;\n"
68 "uniform mat3 yuv_to_rgb_matrix;\n"
69 "uniform float yminf;\n"
72 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
73 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
74 " gl_FragColor = vec4(yuv_to_rgb_matrix * yuva.rgb, yuva.a);\n"
77 static const char *yuva_to_yuv_frag =
78 "uniform sampler2D tex;\n"
81 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
82 " float a = yuva.a;\n"
83 " float anti_a = 1.0 - a;\n"
85 " yuva.g = yuva.g * a + 0.5 * anti_a;\n"
86 " yuva.b = yuva.b * a + 0.5 * anti_a;\n"
88 " gl_FragColor = yuva;\n"
91 static const char *yuva_to_rgb_frag =
92 "uniform sampler2D tex;\n"
93 "uniform mat3 yuv_to_rgb_matrix;\n"
94 "uniform float yminf;\n"
97 " vec4 yuva = texture2D(tex, gl_TexCoord[0].st);\n"
98 " yuva.rgb -= vec3(yminf, 0.5, 0.5);\n"
99 " yuva.rgb = yuv_to_rgb_matrix * yuva.rgb;\n"
100 " yuva.rgb *= yuva.a;\n"
102 " gl_FragColor = yuva;\n"
105 static const char *rgb_to_yuv_frag =
106 "uniform sampler2D tex;\n"
107 "uniform mat3 rgb_to_yuv_matrix;\n"
108 "uniform float yminf;\n"
111 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
112 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
113 " rgba.rgb += vec3(yminf, 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"
130 "uniform mat3 rgb_to_yuv_matrix;\n"
131 "uniform float yminf;\n"
134 " vec4 rgba = texture2D(tex, gl_TexCoord[0].st);\n"
135 " rgba.rgb *= rgba.a;\n"
137 " rgba.rgb = rgb_to_yuv_matrix * rgba.rgb;\n"
138 " rgba.rgb += vec3(yminf, 0.5, 0.5);\n"
139 " gl_FragColor = rgba;\n"
142 //static const char *rgba_to_rgb_flatten =
144 // " gl_FragColor.rgb *= gl_FragColor.a;\n"
145 // " gl_FragColor.a = 1.0;\n"
148 #define GL_STD_BLEND(FN) \
149 static const char *blend_##FN##_frag = \
150 "uniform sampler2D tex2;\n" \
151 "uniform vec2 tex2_dimensions;\n" \
152 "uniform float alpha;\n" \
154 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
156 " result.rgb = " SS(COLOR_##FN(1.0, gl_FragColor.rgb, gl_FragColor.a, canvas.rgb, canvas.a)) ";\n" \
157 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
158 " gl_FragColor = mix(canvas, result, alpha);\n" \
161 #define GL_VEC_BLEND(FN) \
162 static const char *blend_##FN##_frag = \
163 "uniform sampler2D tex2;\n" \
164 "uniform vec2 tex2_dimensions;\n" \
165 "uniform float alpha;\n" \
167 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n" \
169 " result.r = " SS(COLOR_##FN(1.0, gl_FragColor.r, gl_FragColor.a, canvas.r, canvas.a)) ";\n" \
170 " result.g = " SS(COLOR_##FN(1.0, gl_FragColor.g, gl_FragColor.a, canvas.g, canvas.a)) ";\n" \
171 " result.b = " SS(COLOR_##FN(1.0, gl_FragColor.b, gl_FragColor.a, canvas.b, canvas.a)) ";\n" \
172 " result.a = " SS(ALPHA_##FN(1.0, gl_FragColor.a, canvas.a)) ";\n" \
173 " result = clamp(result, 0.0, 1.0);\n" \
174 " gl_FragColor = mix(canvas, result, alpha);\n" \
192 static const char *blend_NORMAL_frag =
193 "uniform sampler2D tex2;\n"
194 "uniform vec2 tex2_dimensions;\n"
195 "uniform float alpha;\n"
197 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
198 " vec4 result = mix(canvas, gl_FragColor, gl_FragColor.a);\n"
199 " gl_FragColor = mix(canvas, result, alpha);\n"
203 static const char *blend_REPLACE_frag =
204 "uniform float alpha;\n"
209 static const char *blend_ADDITION_frag =
210 "uniform sampler2D tex2;\n"
211 "uniform vec2 tex2_dimensions;\n"
212 "uniform float alpha;\n"
214 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
215 " vec4 result = clamp(gl_FragColor + canvas, 0.0, 1.0);\n"
216 " gl_FragColor = mix(canvas, result, alpha);\n"
220 static const char *blend_SUBTRACT_frag =
221 "uniform sampler2D tex2;\n"
222 "uniform vec2 tex2_dimensions;\n"
223 "uniform float alpha;\n"
225 " vec4 canvas = texture2D(tex2, gl_FragCoord.xy / tex2_dimensions);\n"
226 " vec4 result = clamp(gl_FragColor - canvas, 0.0, 1.0);\n"
227 " gl_FragColor = mix(canvas, result, alpha);\n"
230 GL_STD_BLEND(MULTIPLY);
231 GL_VEC_BLEND(DIVIDE);
234 GL_VEC_BLEND(DARKEN);
235 GL_VEC_BLEND(LIGHTEN);
237 GL_STD_BLEND(DST_ATOP);
238 GL_STD_BLEND(DST_IN);
239 GL_STD_BLEND(DST_OUT);
240 GL_STD_BLEND(DST_OVER);
242 GL_STD_BLEND(SRC_ATOP);
243 GL_STD_BLEND(SRC_IN);
244 GL_STD_BLEND(SRC_OUT);
245 GL_STD_BLEND(SRC_OVER);
249 GL_VEC_BLEND(OVERLAY);
250 GL_STD_BLEND(SCREEN);
253 GL_VEC_BLEND(HARDLIGHT);
254 GL_VEC_BLEND(SOFTLIGHT);
255 GL_VEC_BLEND(DIFFERENCE);
257 static const char *read_texture_frag =
258 "uniform sampler2D tex;\n"
261 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
264 static const char *in_vertex_frag =
265 "#version 430 // vertex shader\n"
268 " gl_Position = vec4(in_pos-vec3(0.5,0.5,0.), .5);\n"
271 static const char *feather_frag =
273 "layout(location=0) out vec4 color;\n"
274 "uniform sampler2D tex;\n"
275 "const int MAX = " SS(MAX_FEATHER) "+1;\n"
276 "uniform float psf[MAX];\n"
278 "uniform vec2 dxy;\n"
279 "uniform vec2 twh;\n"
282 " vec2 tc = gl_FragCoord.xy/textureSize(tex,0);\n"
283 " color = texture(tex, tc);\n"
284 " float c = color.r, f = c*psf[0];\n"
285 " for( int i=1; i<n; ++i ) {\n"
286 " vec2 dd = float(i)*dxy;\n"
287 " vec2 a = tc+dd, ac = min(max(vec2(0.),a), twh);\n"
288 " vec2 b = tc-dd, bc = min(max(vec2(0.),b), twh);\n"
289 " float fa = texture2D(tex, ac).r * psf[i];\n"
290 " float fb = texture2D(tex, bc).r * psf[i];\n"
291 " float m = max(fa, fb);\n"
292 " if( f < m ) f = m;\n"
294 " if( c < f ) color = vec4(f);\n"
297 static const char *max_frag =
299 "layout(location=0) out vec4 color;\n"
300 "uniform sampler2D tex;\n"
301 "uniform sampler2D tex1;\n"
306 " vec2 tc = gl_FragCoord.xy/textureSize(tex,0);\n"
307 " color = texture2D(tex1, tc);\n"
308 " float c = texture2D(tex, tc).r;\n"
309 " float b = r<0 ? 1. : 0.;\n"
310 " if( c == b ) return;\n"
311 " float iv = v>=0. ? 1. : -1.;\n"
312 " float rr = r!=0. ? r : 1.;\n"
313 " float rv = rr*v>=0. ? 1. : -1.;\n"
314 " float vv = v>=0. ? 1.-v : 1.+v;\n"
315 " float fg = rv>0. ? vv : 1.;\n"
316 " float bg = rv>0. ? 1. : vv;\n"
317 " float a = c*fg + (1.-c)*bg;\n"
318 " if( iv*(color.a-a) > 0. ) color = vec4(a);\n"
321 static const char *multiply_mask4_frag =
322 "uniform sampler2D tex;\n"
323 "uniform sampler2D tex1;\n"
326 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
327 " gl_FragColor.a *= texture2D(tex1, gl_TexCoord[0].st).r;\n"
330 static const char *multiply_mask3_frag =
331 "uniform sampler2D tex;\n"
332 "uniform sampler2D tex1;\n"
335 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
336 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
337 " gl_FragColor.rgb *= vec3(a, a, a);\n"
340 static const char *multiply_yuvmask3_frag =
341 "uniform sampler2D tex;\n"
342 "uniform sampler2D tex1;\n"
345 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
346 " float a = texture2D(tex1, gl_TexCoord[0].st).r;\n"
347 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
348 " gl_FragColor.rgb *= vec3(a, a, a);\n"
349 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
352 static const char *fade_rgba_frag =
353 "uniform sampler2D tex;\n"
354 "uniform float alpha;\n"
357 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
358 " gl_FragColor.a *= alpha;\n"
361 static const char *fade_yuv_frag =
362 "uniform sampler2D tex;\n"
363 "uniform float alpha;\n"
366 " gl_FragColor = texture2D(tex, gl_TexCoord[0].st);\n"
367 " gl_FragColor.r *= alpha;\n"
368 " gl_FragColor.gb -= vec2(0.5, 0.5);\n"
369 " gl_FragColor.g *= alpha;\n"
370 " gl_FragColor.b *= alpha;\n"
371 " gl_FragColor.gb += vec2(0.5, 0.5);\n"
377 Playback3DCommand::Playback3DCommand()
378 : BC_SynchronousCommand()
384 void Playback3DCommand::copy_from(BC_SynchronousCommand *command)
386 Playback3DCommand *ptr = (Playback3DCommand*)command;
387 this->canvas = ptr->canvas;
388 this->is_cleared = ptr->is_cleared;
390 this->in_x1 = ptr->in_x1;
391 this->in_y1 = ptr->in_y1;
392 this->in_x2 = ptr->in_x2;
393 this->in_y2 = ptr->in_y2;
394 this->out_x1 = ptr->out_x1;
395 this->out_y1 = ptr->out_y1;
396 this->out_x2 = ptr->out_x2;
397 this->out_y2 = ptr->out_y2;
398 this->alpha = ptr->alpha;
399 this->mode = ptr->mode;
400 this->interpolation_type = ptr->interpolation_type;
402 this->input = ptr->input;
403 this->start_position_project = ptr->start_position_project;
404 this->keyframe_set = ptr->keyframe_set;
405 this->keyframe = ptr->keyframe;
406 this->default_auto = ptr->default_auto;
407 this->plugin_client = ptr->plugin_client;
408 this->want_texture = ptr->want_texture;
409 this->is_nested = ptr->is_nested;
410 this->dst_cmodel = ptr->dst_cmodel;
412 BC_SynchronousCommand::copy_from(command);
415 Playback3D::Playback3D(MWindow *mwindow)
418 this->mwindow = mwindow;
422 Playback3D::~Playback3D()
429 BC_SynchronousCommand* Playback3D::new_command()
431 return new Playback3DCommand;
436 void Playback3D::handle_command(BC_SynchronousCommand *command)
438 //printf("Playback3D::handle_command 1 %d\n", command->command);
439 switch(command->command)
441 case Playback3DCommand::WRITE_BUFFER:
442 write_buffer_sync((Playback3DCommand*)command);
445 case Playback3DCommand::CLEAR_OUTPUT:
446 clear_output_sync((Playback3DCommand*)command);
449 case Playback3DCommand::CLEAR_INPUT:
450 clear_input_sync((Playback3DCommand*)command);
453 case Playback3DCommand::DO_CAMERA:
454 do_camera_sync((Playback3DCommand*)command);
457 case Playback3DCommand::OVERLAY:
458 overlay_sync((Playback3DCommand*)command);
461 case Playback3DCommand::DO_FADE:
462 do_fade_sync((Playback3DCommand*)command);
465 case Playback3DCommand::DO_MASK:
466 do_mask_sync((Playback3DCommand*)command);
469 case Playback3DCommand::PLUGIN:
470 run_plugin_sync((Playback3DCommand*)command);
473 case Playback3DCommand::COPY_FROM:
474 copy_from_sync((Playback3DCommand*)command);
477 case Playback3DCommand::CONVERT_CMODEL:
478 convert_cmodel_sync((Playback3DCommand*)command);
481 // case Playback3DCommand::DRAW_REFRESH:
482 // draw_refresh_sync((Playback3DCommand*)command);
485 //printf("Playback3D::handle_command 10\n");
491 void Playback3D::copy_from(Canvas *canvas,
496 Playback3DCommand command;
497 command.command = Playback3DCommand::COPY_FROM;
498 command.canvas = canvas;
501 command.want_texture = want_texture;
502 send_command(&command);
505 void Playback3D::copy_from_sync(Playback3DCommand *command)
508 BC_WindowBase *window =
509 command->canvas->lock_canvas("Playback3D::copy_from_sync");
511 window->enable_opengl();
514 int w = command->input->get_w();
515 int h = command->input->get_h();
516 if( command->input->get_opengl_state() == VFrame::SCREEN &&
517 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(), command->input->get_h(),
520 // command->frame->get_w(), command->frame->get_h(),
521 // command->frame->get_color_model());
523 // With NVidia at least
525 printf("Playback3D::copy_from_sync: w=%d not supported because it is not divisible by 4.\n", w);
530 if( command->want_texture ) {
531 //printf("Playback3D::copy_from_sync 1 dst=%p src=%p\n", command->frame, command->input);
532 // Screen_to_texture requires the source pbuffer enabled.
533 command->input->enable_opengl();
534 command->frame->screen_to_texture();
535 command->frame->set_opengl_state(VFrame::TEXTURE);
538 command->input->to_texture();
542 else if( command->input->get_opengl_state() == VFrame::TEXTURE &&
543 w == command->frame->get_w() && h == command->frame->get_h() ) {
547 printf("Playback3D::copy_from_sync: invalid formats opengl_state=%d %dx%d -> %dx%d\n",
548 command->input->get_opengl_state(), w, h,
549 command->frame->get_w(), command->frame->get_h());
553 command->input->bind_texture(0);
554 command->frame->enable_opengl();
555 command->frame->init_screen();
556 unsigned int shader = BC_CModels::is_yuv(command->input->get_color_model()) ?
557 VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
559 glUseProgram(shader);
560 int variable = glGetUniformLocation(shader, "tex");
561 glUniform1i(variable, 0);
562 BC_GL_YUV_TO_RGB(shader);
566 command->input->draw_texture(1);
567 command->frame->screen_to_ram();
571 command->canvas->unlock_canvas();
578 // void Playback3D::draw_refresh(Canvas *canvas,
589 // Playback3DCommand command;
590 // command.command = Playback3DCommand::DRAW_REFRESH;
591 // command.canvas = canvas;
592 // command.frame = frame;
593 // command.in_x1 = in_x1;
594 // command.in_y1 = in_y1;
595 // command.in_x2 = in_x2;
596 // command.in_y2 = in_y2;
597 // command.out_x1 = out_x1;
598 // command.out_y1 = out_y1;
599 // command.out_x2 = out_x2;
600 // command.out_y2 = out_y2;
601 // send_command(&command);
604 // void Playback3D::draw_refresh_sync(Playback3DCommand *command)
607 // BC_WindowBase *window =
608 // command->canvas->lock_canvas("Playback3D::draw_refresh_sync");
610 // window->enable_opengl();
612 // // Read output pbuffer back to RAM in project colormodel
613 // // RGB 8bit is fastest for OpenGL to read back.
614 // command->frame->reallocate(0,
618 // command->frame->get_w(),
619 // command->frame->get_h(),
622 // command->frame->screen_to_ram();
624 // window->clear_box(0,
628 // window->draw_vframe(command->frame,
629 // (int)command->out_x1,
630 // (int)command->out_y1,
631 // (int)(command->out_x2 - command->out_x1),
632 // (int)(command->out_y2 - command->out_y1),
633 // (int)command->in_x1,
634 // (int)command->in_y1,
635 // (int)(command->in_x2 - command->in_x1),
636 // (int)(command->in_y2 - command->in_y1),
640 // command->canvas->unlock_canvas();
648 void Playback3D::write_buffer(Canvas *canvas,
660 Playback3DCommand command;
661 command.command = Playback3DCommand::WRITE_BUFFER;
662 command.canvas = canvas;
663 command.frame = frame;
664 command.in_x1 = in_x1;
665 command.in_y1 = in_y1;
666 command.in_x2 = in_x2;
667 command.in_y2 = in_y2;
668 command.out_x1 = out_x1;
669 command.out_y1 = out_y1;
670 command.out_x2 = out_x2;
671 command.out_y2 = out_y2;
672 command.is_cleared = is_cleared;
673 send_command(&command);
677 void Playback3D::write_buffer_sync(Playback3DCommand *command)
680 BC_WindowBase *window =
681 command->canvas->lock_canvas("Playback3D::write_buffer_sync");
683 window->enable_opengl();
684 // Update hidden cursor
685 window->update_video_cursor();
686 command->frame->enable_opengl();
687 command->frame->init_screen();
688 //printf("Playback3D::write_buffer_sync 1 %d\n", window->get_id());
689 int frame_state = command->frame->get_opengl_state();
690 if( frame_state != VFrame::TEXTURE )
691 command->frame->to_texture();
692 if( frame_state != VFrame::RAM ) {
693 command->in_y1 = command->frame->get_h() - command->in_y1;
694 command->in_y2 = command->frame->get_h() - command->in_y2;
696 window->enable_opengl();
697 draw_output(command, 1);
698 command->frame->set_opengl_state(frame_state);
700 command->canvas->unlock_canvas();
706 void Playback3D::draw_output(Playback3DCommand *command, int flip_y)
709 int texture_id = command->frame->get_texture_id();
710 BC_WindowBase *window = command->canvas->get_canvas();
712 // printf("Playback3D::draw_output 1 texture_id=%d window=%p\n",
714 // command->canvas->get_canvas());
719 // If virtual console is being used, everything in this function has
720 // already been done except the page flip.
723 canvas_w = window->get_w();
724 canvas_h = window->get_h();
725 VFrame::init_screen(canvas_w, canvas_h);
726 int color_model = command->frame->get_color_model();
727 int is_yuv = BC_CModels::is_yuv(color_model);
729 if(!command->is_cleared)
731 // If we get here, the virtual console was not used.
732 int color = command->canvas->get_clear_color();
733 int r = (color>>16) & 0xff; // always rgb
734 int g = (color>>8) & 0xff;
735 int b = (color>>0) & 0xff;
736 color_frame(command, r/255.f, g/255.f, b/255.f, 0.f);
740 // Undo any previous shader settings
741 command->frame->bind_texture(0);
743 // Convert colormodel
744 unsigned int shader = is_yuv ? VFrame::make_shader(0, yuv_to_rgb_frag, 0) : 0;
746 glUseProgram(shader);
747 // Set texture unit of the texture
748 int variable = glGetUniformLocation(shader, "tex");
749 glUniform1i(variable, 0);
750 BC_GL_YUV_TO_RGB(shader);
753 // if(BC_CModels::components(color_model) == 4)
755 // glEnable(GL_BLEND);
756 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
759 command->frame->draw_texture(
760 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
761 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
765 //printf("Playback3D::draw_output 2 %f,%f %f,%f -> %f,%f %f,%f\n",
766 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
767 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
771 command->canvas->get_canvas()->flip_opengl();
778 void Playback3D::color_frame(Playback3DCommand *command,
779 float r, float g, float b, float a)
782 glClearColor(r, g, b, a);
783 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
788 void Playback3D::clear_output(Canvas *canvas, VFrame *output)
790 Playback3DCommand command;
791 command.command = Playback3DCommand::CLEAR_OUTPUT;
792 command.canvas = canvas;
793 command.frame = output;
794 send_command(&command);
797 void Playback3D::clear_output_sync(Playback3DCommand *command)
800 BC_WindowBase *window =
801 command->canvas->lock_canvas("Playback3D::clear_output_sync");
803 // If we get here, the virtual console is being used.
804 command->canvas->get_canvas()->enable_opengl();
806 int color = BLACK, alpha = 0;
807 // Using pbuffer for refresh frame.
808 if( command->frame ) {
809 command->frame->enable_opengl();
810 command->frame->set_opengl_state(VFrame::SCREEN);
811 color = command->frame->get_clear_color();
812 alpha = command->frame->get_clear_alpha();
813 int color_model = command->canvas->mwindow->edl->session->color_model;
814 is_yuv = BC_CModels::is_yuv(color_model);
817 color = command->canvas->get_clear_color();
819 int r = (color>>16) & 0xff;
820 int g = (color>>8) & 0xff;
821 int b = (color>>0) & 0xff;
822 if( is_yuv ) YUV::yuv.rgb_to_yuv_8(r, g, b);
823 color_frame(command, r/255.f, g/255.f, b/255.f, a/255.f);
825 command->canvas->unlock_canvas();
830 void Playback3D::clear_input(Canvas *canvas, VFrame *frame)
832 Playback3DCommand command;
833 command.command = Playback3DCommand::CLEAR_INPUT;
834 command.canvas = canvas;
835 command.frame = frame;
836 send_command(&command);
839 void Playback3D::clear_input_sync(Playback3DCommand *command)
842 BC_WindowBase *window =
843 command->canvas->lock_canvas("Playback3D::clear_input_sync");
845 command->canvas->get_canvas()->enable_opengl();
846 command->frame->enable_opengl();
847 command->frame->clear_pbuffer();
848 command->frame->set_opengl_state(VFrame::SCREEN);
850 command->canvas->unlock_canvas();
854 void Playback3D::do_camera(Canvas *canvas,
866 Playback3DCommand command;
867 command.command = Playback3DCommand::DO_CAMERA;
868 command.canvas = canvas;
869 command.input = input;
870 command.frame = output;
871 command.in_x1 = in_x1;
872 command.in_y1 = in_y1;
873 command.in_x2 = in_x2;
874 command.in_y2 = in_y2;
875 command.out_x1 = out_x1;
876 command.out_y1 = out_y1;
877 command.out_x2 = out_x2;
878 command.out_y2 = out_y2;
879 send_command(&command);
882 void Playback3D::do_camera_sync(Playback3DCommand *command)
885 BC_WindowBase *window =
886 command->canvas->lock_canvas("Playback3D::do_camera_sync");
888 command->canvas->get_canvas()->enable_opengl();
890 command->input->to_texture();
891 command->frame->enable_opengl();
892 command->frame->init_screen();
893 command->frame->clear_pbuffer();
895 command->input->bind_texture(0);
896 // Must call draw_texture in input frame to get the texture coordinates right.
898 // printf("Playback3D::do_camera_sync 1 %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n",
904 // (float)command->input->get_h() - command->out_y1,
906 // (float)command->input->get_h() - command->out_y2);
907 command->input->draw_texture(
908 command->in_x1, command->in_y2,
909 command->in_x2, command->in_y1,
911 (float)command->frame->get_h() - command->out_y1,
913 (float)command->frame->get_h() - command->out_y2);
916 command->frame->set_opengl_state(VFrame::SCREEN);
917 command->frame->screen_to_ram();
919 command->canvas->unlock_canvas();
923 void Playback3D::overlay(Canvas *canvas, VFrame *input,
924 float in_x1, float in_y1, float in_x2, float in_y2,
925 float out_x1, float out_y1, float out_x2, float out_y2,
926 float alpha, int mode, int interpolation_type,
927 VFrame *output, int is_nested)
929 Playback3DCommand command;
930 command.command = Playback3DCommand::OVERLAY;
931 command.canvas = canvas;
932 command.frame = output;
933 command.input = input;
934 command.in_x1 = in_x1;
935 command.in_y1 = in_y1;
936 command.in_x2 = in_x2;
937 command.in_y2 = in_y2;
938 command.out_x1 = out_x1;
939 command.out_y1 = out_y1;
940 command.out_x2 = out_x2;
941 command.out_y2 = out_y2;
942 command.alpha = alpha;
944 command.interpolation_type = interpolation_type;
945 command.is_nested = is_nested;
946 send_command(&command);
949 void Playback3D::overlay_sync(Playback3DCommand *command)
952 // To do these operations, we need to copy the input buffer to a texture
953 // and blend 2 textures in a shader
954 static const char * const overlay_shaders[TRANSFER_TYPES] = {
955 blend_NORMAL_frag, // TRANSFER_NORMAL
956 blend_ADDITION_frag, // TRANSFER_ADDITION
957 blend_SUBTRACT_frag, // TRANSFER_SUBTRACT
958 blend_MULTIPLY_frag, // TRANSFER_MULTIPLY
959 blend_DIVIDE_frag, // TRANSFER_DIVIDE
960 blend_REPLACE_frag, // TRANSFER_REPLACE
961 blend_MAX_frag, // TRANSFER_MAX
962 blend_MIN_frag, // TRANSFER_MIN
963 blend_DARKEN_frag, // TRANSFER_DARKEN
964 blend_LIGHTEN_frag, // TRANSFER_LIGHTEN
965 blend_DST_frag, // TRANSFER_DST
966 blend_DST_ATOP_frag, // TRANSFER_DST_ATOP
967 blend_DST_IN_frag, // TRANSFER_DST_IN
968 blend_DST_OUT_frag, // TRANSFER_DST_OUT
969 blend_DST_OVER_frag, // TRANSFER_DST_OVER
970 blend_SRC_frag, // TRANSFER_SRC
971 blend_SRC_ATOP_frag, // TRANSFER_SRC_ATOP
972 blend_SRC_IN_frag, // TRANSFER_SRC_IN
973 blend_SRC_OUT_frag, // TRANSFER_SRC_OUT
974 blend_SRC_OVER_frag, // TRANSFER_SRC_OVER
975 blend_AND_frag, // TRANSFER_AND
976 blend_OR_frag, // TRANSFER_OR
977 blend_XOR_frag, // TRANSFER_XOR
978 blend_OVERLAY_frag, // TRANSFER_OVERLAY
979 blend_SCREEN_frag, // TRANSFER_SCREEN
980 blend_BURN_frag, // TRANSFER_BURN
981 blend_DODGE_frag, // TRANSFER_DODGE
982 blend_HARDLIGHT_frag, // TRANSFER_HARDLIGHT
983 blend_SOFTLIGHT_frag, // TRANSFER_SOFTLIGHT
984 blend_DIFFERENCE_frag, // TRANSFER_DIFFERENCE
987 BC_WindowBase *window =
988 command->canvas->lock_canvas("Playback3D::overlay_sync");
990 // Make sure OpenGL is enabled first.
991 window->enable_opengl();
992 window->update_video_cursor();
994 glColor4f(1, 1, 1, 1);
998 //printf("Playback3D::overlay_sync 1 %d\n", command->input->get_opengl_state());
999 switch( command->input->get_opengl_state() ) {
1000 // Upload texture and composite to screen
1002 command->input->to_texture();
1004 // Just composite texture to screen
1005 case VFrame::TEXTURE:
1007 // read from PBuffer to texture, then composite texture to screen
1008 case VFrame::SCREEN:
1009 command->input->enable_opengl();
1010 command->input->screen_to_texture();
1013 printf("Playback3D::overlay_sync unknown state\n");
1017 if(command->frame) {
1018 // Render to PBuffer
1019 command->frame->enable_opengl();
1020 command->frame->set_opengl_state(VFrame::SCREEN);
1021 canvas_w = command->frame->get_w();
1022 canvas_h = command->frame->get_h();
1026 window->enable_opengl();
1027 canvas_w = window->get_w();
1028 canvas_h = window->get_h();
1032 const char *shader_stack[16];
1033 memset(shader_stack,0, sizeof(shader_stack));
1034 int total_shaders = 0, need_matrix = 0;
1036 VFrame::init_screen(canvas_w, canvas_h);
1039 command->input->bind_texture(0);
1041 // Convert colormodel to RGB if not nested.
1042 // The color model setting in the output frame is ignored.
1043 // if( command->is_nested <= 0 && // not nested
1044 // BC_CModels::is_yuv(command->input->get_color_model()) ) {
1046 // shader_stack[total_shaders++] = yuv_to_rgb_frag;
1050 #define add_shader(s) \
1051 if(!total_shaders) shader_stack[total_shaders++] = read_texture_frag; \
1052 shader_stack[total_shaders++] = s
1054 switch(command->mode) {
1055 case TRANSFER_REPLACE:
1056 // This requires overlaying an alpha multiplied image on a black screen.
1057 if( command->input->get_texture_components() != 4 ) break;
1058 add_shader(overlay_shaders[command->mode]);
1061 enable_overlay_texture(command);
1062 add_shader(overlay_shaders[command->mode]);
1066 // if to flatten alpha
1067 // if( command->is_nested < 0 ) {
1068 // switch(command->input->get_color_model()) {
1069 //// yuv has already been converted to rgb
1070 // case BC_YUVA8888:
1071 // case BC_RGBA_FLOAT:
1072 // case BC_RGBA8888:
1073 // add_shader(rgba_to_rgb_flatten);
1080 unsigned int shader = !shader_stack[0] ? 0 :
1081 VFrame::make_shader(shader_stack);
1083 glUseProgram(shader);
1084 if( need_matrix ) BC_GL_YUV_TO_RGB(shader);
1085 // Set texture unit of the texture
1086 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1087 // Set texture unit of the temp texture
1088 glUniform1i(glGetUniformLocation(shader, "tex2"), 1);
1090 int variable = glGetUniformLocation(shader, "alpha");
1091 glUniform1f(variable, command->alpha);
1092 // Set dimensions of the temp texture
1094 glUniform2f(glGetUniformLocation(shader, "tex2_dimensions"),
1095 (float)temp_texture->get_texture_w(),
1096 (float)temp_texture->get_texture_h());
1102 //printf("Playback3D::overlay_sync %f %f %f %f %f %f %f %f\n",
1103 // command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1104 // command->out_x1, command->out_y1, command->out_x2, command->out_y2);
1106 command->input->draw_texture(
1107 command->in_x1, command->in_y1, command->in_x2, command->in_y2,
1108 command->out_x1, command->out_y1, command->out_x2, command->out_y2,
1109 !command->is_nested);
1112 // Delete temp texture
1114 delete temp_texture;
1116 glActiveTexture(GL_TEXTURE1);
1117 glDisable(GL_TEXTURE_2D);
1119 glActiveTexture(GL_TEXTURE0);
1120 glDisable(GL_TEXTURE_2D);
1122 command->canvas->unlock_canvas();
1126 void Playback3D::enable_overlay_texture(Playback3DCommand *command)
1129 glDisable(GL_BLEND);
1131 glActiveTexture(GL_TEXTURE1);
1132 BC_Texture::new_texture(&temp_texture, canvas_w, canvas_h,
1133 command->input->get_color_model());
1134 temp_texture->bind(1);
1136 // Read canvas into texture
1137 glReadBuffer(GL_BACK);
1138 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, canvas_w, canvas_h);
1143 void Playback3D::do_mask(Canvas *canvas,
1145 int64_t start_position_project,
1146 MaskAutos *keyframe_set,
1148 MaskAuto *default_auto)
1150 Playback3DCommand command;
1151 command.command = Playback3DCommand::DO_MASK;
1152 command.canvas = canvas;
1153 command.frame = output;
1154 command.start_position_project = start_position_project;
1155 command.keyframe_set = keyframe_set;
1156 command.keyframe = keyframe;
1157 command.default_auto = default_auto;
1159 send_command(&command);
1163 void Playback3D::draw_spots(MaskSpots &spots, int ix1,int iy1, int ix2,int iy2)
1165 int x1 = iy1 < iy2 ? ix1 : ix2;
1166 int y1 = iy1 < iy2 ? iy1 : iy2;
1167 int x2 = iy1 < iy2 ? ix2 : ix1;
1168 int y2 = iy1 < iy2 ? iy2 : iy1;
1171 int dx = x2-x1, dy = y2-y1;
1172 int dx2 = 2*dx, dy2 = 2*dy;
1173 if( dx < 0 ) dx = -dx;
1174 int m = dx > dy ? dx : dy, n = m;
1176 if( dx2 >= 0 ) do { /* +Y, +X */
1177 spots.append(x, y++);
1178 if( (m -= dx2) < 0 ) { m += dy2; ++x; }
1179 } while( --n >= 0 );
1180 else do { /* +Y, -X */
1181 spots.append(x, y++);
1182 if( (m += dx2) < 0 ) { m += dy2; --x; }
1183 } while( --n >= 0 );
1186 if( dx2 >= 0 ) do { /* +X, +Y */
1187 spots.append(x++, y);
1188 if( (m -= dy2) < 0 ) { m += dx2; ++y; }
1189 } while( --n >= 0 );
1190 else do { /* -X, +Y */
1191 spots.append(x--, y);
1192 if( (m -= dy2) < 0 ) { m -= dx2; ++y; }
1193 } while( --n >= 0 );
1198 class fb_texture : public BC_Texture
1201 fb_texture(int w, int h, int colormodel);
1203 void bind(int texture_unit);
1204 void read_screen(int x, int y, int w, int h);
1205 void set_output_texture();
1206 void unset_output_texture();
1210 fb_texture::fb_texture(int w, int h, int colormodel)
1211 : BC_Texture(w, h, colormodel)
1214 // glGenRenderbuffers(1, &rb);
1215 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1216 // glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, get_texture_w(), get_texture_w());
1217 glGenFramebuffers(1, &fb);
1218 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1219 // glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);
1222 fb_texture::~fb_texture()
1224 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1225 glDeleteFramebuffers(1, (GLuint *)&fb);
1226 // glBindRenderbuffer(GL_RENDERBUFFER, 0);
1227 // glGenRenderbuffers(1, &rb);
1230 void fb_texture::bind(int texture_unit)
1232 glBindFramebuffer(GL_FRAMEBUFFER, fb);
1233 // glBindRenderbuffer(GL_RENDERBUFFER, rb);
1234 BC_Texture::bind(texture_unit);
1237 void fb_texture::read_screen(int x, int y, int w, int h)
1240 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1241 glReadBuffer(GL_BACK);
1242 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, x,y, w,h);
1245 void fb_texture::set_output_texture()
1247 GLenum at = GL_COLOR_ATTACHMENT0;
1248 glFramebufferTexture(GL_FRAMEBUFFER, at, get_texture_id(), 0);
1249 GLenum dbo[1] = { at, }; // bind layout(location=0) out vec4 color;
1250 glDrawBuffers(1, dbo);
1251 int ret = glCheckFramebufferStatus(GL_FRAMEBUFFER);
1252 if( ret != GL_FRAMEBUFFER_COMPLETE ) {
1253 printf("glDrawBuffer error 0x%04x\n", ret);
1257 void fb_texture::unset_output_texture()
1259 glDrawBuffers(0, 0);
1260 int at = GL_COLOR_ATTACHMENT0;
1261 glFramebufferTexture(GL_FRAMEBUFFER, at, 0, 0);
1262 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1263 glDisable(GL_TEXTURE_2D);
1267 class zglTessData : public ArrayList<double *>
1270 zglTessData() { set_array_delete(); }
1271 ~zglTessData() { remove_all_objects(); }
1274 static void combineData(GLdouble coords[3],
1275 GLdouble *vertex_data[4], GLfloat weight[4],
1276 GLdouble **outData, void *data)
1278 zglTessData *invented = (zglTessData *)data;
1279 GLdouble *vertex = new double[6];
1280 invented->append(vertex);
1281 vertex[0] = coords[0];
1282 vertex[1] = coords[1];
1283 vertex[2] = coords[2];
1284 for( int i=3; i<6; ++i ) {
1286 for( int k=0; k<4; ++k ) {
1287 if( !weight[k] || !vertex_data[k] ) continue;
1288 v += weight[k] * vertex_data[k][i];
1296 static void zglBegin(GLenum mode) { glBegin(mode); }
1297 static void zglEnd() { glEnd(); }
1298 static void zglVertex(const GLdouble *v) { glVertex3dv(v); }
1302 void Playback3D::do_mask_sync(Playback3DCommand *command)
1305 BC_WindowBase *window =
1306 command->canvas->lock_canvas("Playback3D::do_mask_sync");
1308 window->enable_opengl();
1310 switch( command->frame->get_opengl_state() ) {
1312 // upload frame to the texture
1313 command->frame->to_texture();
1316 case VFrame::SCREEN:
1317 // Read back from PBuffer
1318 // Bind context to pbuffer
1319 command->frame->enable_opengl();
1320 command->frame->screen_to_texture();
1324 // Initialize coordinate system
1325 command->frame->enable_opengl();
1326 command->frame->init_screen();
1327 int color_model = command->frame->get_color_model();
1328 int w = command->frame->get_w();
1329 int h = command->frame->get_h();
1331 float faders[SUBMASKS], feathers[SUBMASKS], cc = 1;
1332 MaskPoints point_set[SUBMASKS];
1333 // Draw every submask as a new polygon
1334 int total_submasks = command->keyframe_set->total_submasks(
1335 command->start_position_project, PLAY_FORWARD);
1336 int show_mask = command->keyframe_set->track->masks;
1338 for(int k = 0; k < total_submasks; k++) {
1339 MaskPoints &points = point_set[k];
1340 command->keyframe_set->get_points(&points,
1341 k, command->start_position_project, PLAY_FORWARD);
1342 float fader = command->keyframe_set->get_fader(
1343 command->start_position_project, k, PLAY_FORWARD);
1344 float v = fader/100.;
1346 float feather = command->keyframe_set->get_feather(
1347 command->start_position_project, k, PLAY_FORWARD);
1348 feathers[k] = feather;
1349 MaskEdge &edge = *edges.append(new MaskEdge());
1350 if( !v || !((show_mask>>k) & 1) || !points.size() ) continue;
1351 edge.load(point_set[k], h);
1352 if( v >= 0 ) continue;
1354 if( cc > vv ) cc = vv;
1357 fb_texture *mask = new fb_texture(w, h, color_model);
1358 mask->set_output_texture();
1359 glClearColor(cc, cc, cc, cc);
1360 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1361 mask->unset_output_texture();
1363 unsigned int feather_shader =
1364 VFrame::make_shader(0, in_vertex_frag, feather_frag, 0);
1365 unsigned int max_shader =
1366 VFrame::make_shader(0, in_vertex_frag, max_frag, 0);
1367 if( feather_shader && max_shader ) {
1368 fb_texture *in = new fb_texture(w, h, color_model);
1369 fb_texture *out = new fb_texture(w, h, color_model);
1370 float tw = 1./out->get_texture_w(), th = 1./out->get_texture_h();
1371 float tw1 = (w-1)*tw, th1 = (h-1)*th;
1372 for(int k = 0; k < total_submasks; k++) {
1373 MaskEdge &edge = *edges[k];
1374 if( edge.size() < 3 ) continue;
1375 float r = feathers[k], v = faders[k];
1376 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1377 glActiveTexture(GL_TEXTURE0);
1378 glDisable(GL_TEXTURE_2D);
1379 float b = r>=0 ? 0. : 1.;
1380 float f = r>=0 ? 1. : 0.;
1381 glClearColor(b, b, b, b);
1382 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1383 glColor4f(f, f, f, f);
1384 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1385 int display_list = glGenLists(1);
1387 glNewList(display_list, GL_COMPILE);
1388 glBegin(GL_POLYGON);
1389 MaskCoord *c = &edge[0];
1390 for( int i=edge.size(); --i>=0; ++c )
1391 glVertex2f(c->x, c->y);
1394 glCallList(display_list);
1396 { zglTessData invented;
1397 GLUtesselator *tess = gluNewTess();
1398 gluTessProperty(tess, GLU_TESS_TOLERANCE, 0.5);
1399 gluTessCallback(tess, GLU_TESS_VERTEX,(GLvoid (*)()) &zglVertex);
1400 gluTessCallback(tess, GLU_TESS_BEGIN,(GLvoid (*)()) &zglBegin);
1401 gluTessCallback(tess, GLU_TESS_END,(GLvoid (*)()) &zglEnd);
1402 gluTessCallback(tess, GLU_TESS_COMBINE_DATA,(GLvoid (*)()) &combineData);
1403 glNewList(display_list, GL_COMPILE);
1404 gluTessBeginPolygon(tess, &invented);
1405 gluTessBeginContour(tess);
1406 MaskCoord *c = &edge[0];
1407 for( int i=edge.size(); --i>=0; ++c )
1408 gluTessVertex(tess, (GLdouble *)c, c);
1409 gluTessEndContour(tess);
1410 gluTessEndPolygon(tess);
1412 glCallList(display_list);
1413 gluDeleteTess(tess); }
1415 glDeleteLists(1, display_list);
1416 in->read_screen(0,0, w,h);
1417 //in->write_tex("/tmp/in0.ppm");
1419 double sig2 = -log(255.0)/(r*r);
1420 int n = abs((int)r) + 1;
1421 if( n > MAX_FEATHER+1 ) n = MAX_FEATHER+1;
1422 float psf[n]; // point spot fn
1423 for( int i=0; i<n; ++i )
1424 psf[i] = exp(i*i * sig2);
1425 glUseProgram(feather_shader);
1426 glUniform1fv(glGetUniformLocation(feather_shader, "psf"), n, psf);
1427 glUniform1i(glGetUniformLocation(feather_shader, "n"), n);
1428 glUniform2f(glGetUniformLocation(feather_shader, "dxy"), tw, 0.);
1429 glUniform2f(glGetUniformLocation(feather_shader, "twh"), tw1, th1);
1430 glUniform1i(glGetUniformLocation(feather_shader, "tex"), 0);
1432 out->set_output_texture();
1433 out->draw_texture(0,0, w,h, 0,0, w,h);
1434 out->unset_output_texture();
1435 //out->write_tex("/tmp/out1.ppm");
1436 fb_texture *t = in; in = out; out = t;
1437 glUniform2f(glGetUniformLocation(feather_shader, "dxy"), 0., th);
1439 out->set_output_texture();
1440 out->draw_texture(0,0, w,h, 0,0, w,h);
1441 out->unset_output_texture();
1442 //out->write_tex("/tmp/out2.ppm");
1444 t = in; in = out; out = t;
1447 glUseProgram(max_shader);
1449 //in->write_tex("/tmp/in1.ppm");
1450 //mask->write_tex("/tmp/mask1.ppm");
1452 glUniform1i(glGetUniformLocation(max_shader, "tex"), 0);
1453 glUniform1i(glGetUniformLocation(max_shader, "tex1"), 1);
1454 glUniform1f(glGetUniformLocation(max_shader, "r"), r);
1455 glUniform1f(glGetUniformLocation(max_shader, "v"), v);
1456 glViewport(0,0, w,h);
1457 out->set_output_texture();
1458 out->draw_texture(0,0, w,h, 0,0, w,h);
1459 out->unset_output_texture();
1461 fb_texture *t = mask; mask = out; out = t;
1462 //mask->write_tex("/tmp/mask2.ppm");
1468 const char *alpha_shader = BC_CModels::has_alpha(color_model) ?
1469 multiply_mask4_frag :
1470 !BC_CModels::is_yuv(color_model) ?
1471 multiply_mask3_frag :
1472 multiply_yuvmask3_frag;
1473 unsigned int shader = VFrame::make_shader(0, alpha_shader, 0);
1474 glUseProgram(shader);
1476 command->frame->bind_texture(0);
1477 mask->BC_Texture::bind(1);
1478 glUniform1i(glGetUniformLocation(shader, "tex"), 0);
1479 glUniform1i(glGetUniformLocation(shader, "tex1"), 1);
1481 command->frame->draw_texture();
1482 command->frame->set_opengl_state(VFrame::SCREEN);
1485 glColor4f(1, 1, 1, 1);
1486 glActiveTexture(GL_TEXTURE0);
1487 glDisable(GL_TEXTURE_2D);
1488 window->enable_opengl();
1490 command->canvas->unlock_canvas();
1495 void Playback3D::convert_cmodel(Canvas *canvas,
1499 // Do nothing if colormodels are equivalent in OpenGL & the image is in hardware.
1500 int src_cmodel = output->get_color_model();
1502 (output->get_opengl_state() == VFrame::TEXTURE ||
1503 output->get_opengl_state() == VFrame::SCREEN) &&
1504 // OpenGL has no floating point.
1505 ( (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGB_FLOAT) ||
1506 (src_cmodel == BC_RGBA8888 && dst_cmodel == BC_RGBA_FLOAT) ||
1507 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGB888) ||
1508 (src_cmodel == BC_RGBA_FLOAT && dst_cmodel == BC_RGBA8888) ||
1509 // OpenGL sets alpha to 1 on import
1510 (src_cmodel == BC_RGB888 && dst_cmodel == BC_RGBA8888) ||
1511 (src_cmodel == BC_YUV888 && dst_cmodel == BC_YUVA8888) ||
1512 (src_cmodel == BC_RGB_FLOAT && dst_cmodel == BC_RGBA_FLOAT) )
1517 Playback3DCommand command;
1518 command.command = Playback3DCommand::CONVERT_CMODEL;
1519 command.canvas = canvas;
1520 command.frame = output;
1521 command.dst_cmodel = dst_cmodel;
1522 send_command(&command);
1525 void Playback3D::convert_cmodel_sync(Playback3DCommand *command)
1528 BC_WindowBase *window =
1529 command->canvas->lock_canvas("Playback3D::convert_cmodel_sync");
1531 window->enable_opengl();
1533 // Import into hardware
1534 command->frame->enable_opengl();
1535 command->frame->init_screen();
1536 command->frame->to_texture();
1538 // Colormodel permutation
1539 int src_cmodel = command->frame->get_color_model();
1540 int dst_cmodel = command->dst_cmodel;
1544 } cmodel_shader_table_t;
1545 enum { rgb_to_rgb, rgb_to_yuv, yuv_to_rgb, yuv_to_yuv, };
1547 static cmodel_shader_table_t cmodel_shader_table[] = {
1548 { BC_RGB888, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1549 { BC_RGB888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1550 { BC_RGBA8888, BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1551 { BC_RGBA8888, BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1552 { BC_RGBA8888, BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1553 { BC_RGBA8888, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1554 { BC_RGB_FLOAT, BC_YUV888, rgb_to_yuv, rgb_to_yuv_frag },
1555 { BC_RGB_FLOAT, BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1556 { BC_RGBA_FLOAT,BC_RGB888, rgb_to_rgb, rgba_to_rgb_frag },
1557 { BC_RGBA_FLOAT,BC_RGB_FLOAT, rgb_to_rgb, rgba_to_rgb_frag },
1558 { BC_RGBA_FLOAT,BC_YUV888, rgb_to_yuv, rgba_to_yuv_frag },
1559 { BC_RGBA_FLOAT,BC_YUVA8888, rgb_to_yuv, rgb_to_yuv_frag },
1560 { BC_YUV888, BC_RGB888, yuv_to_rgb, yuv_to_rgb_frag },
1561 { BC_YUV888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1562 { BC_YUV888, BC_RGB_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1563 { BC_YUV888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1564 { BC_YUVA8888, BC_RGB888, yuv_to_rgb, yuva_to_rgb_frag },
1565 { BC_YUVA8888, BC_RGBA8888, yuv_to_rgb, yuv_to_rgb_frag },
1566 { BC_YUVA8888, BC_RGB_FLOAT, yuv_to_rgb, yuva_to_rgb_frag },
1567 { BC_YUVA8888, BC_RGBA_FLOAT, yuv_to_rgb, yuv_to_rgb_frag },
1568 { BC_YUVA8888, BC_YUV888, yuv_to_yuv, yuva_to_yuv_frag },
1571 const char *shader = 0;
1572 int table_size = sizeof(cmodel_shader_table) / sizeof(cmodel_shader_table_t);
1573 for( int i=0; i<table_size; ++i ) {
1574 if( cmodel_shader_table[i].src == src_cmodel &&
1575 cmodel_shader_table[i].dst == dst_cmodel ) {
1576 shader = cmodel_shader_table[i].shader;
1577 type = cmodel_shader_table[i].typ;
1582 // printf("Playback3D::convert_cmodel_sync %d %d %d shader=\n%s",
1584 // command->frame->get_color_model(),
1585 // command->dst_cmodel,
1588 const char *shader_stack[9];
1589 memset(shader_stack,0, sizeof(shader_stack));
1590 int current_shader = 0;
1593 //printf("Playback3D::convert_cmodel_sync %d\n", __LINE__);
1594 shader_stack[current_shader++] = shader;
1595 shader_stack[current_shader] = 0;
1596 unsigned int shader_id = VFrame::make_shader(shader_stack);
1598 command->frame->bind_texture(0);
1599 glUseProgram(shader_id);
1601 glUniform1i(glGetUniformLocation(shader_id, "tex"), 0);
1604 BC_GL_RGB_TO_YUV(shader_id);
1607 BC_GL_YUV_TO_RGB(shader_id);
1611 command->frame->draw_texture();
1612 if(shader) glUseProgram(0);
1613 command->frame->set_opengl_state(VFrame::SCREEN);
1617 command->canvas->unlock_canvas();
1621 void Playback3D::do_fade(Canvas *canvas, VFrame *frame, float fade)
1623 Playback3DCommand command;
1624 command.command = Playback3DCommand::DO_FADE;
1625 command.canvas = canvas;
1626 command.frame = frame;
1627 command.alpha = fade;
1628 send_command(&command);
1631 void Playback3D::do_fade_sync(Playback3DCommand *command)
1634 BC_WindowBase *window =
1635 command->canvas->lock_canvas("Playback3D::do_fade_sync");
1637 window->enable_opengl();
1638 switch( command->frame->get_opengl_state() ) {
1640 command->frame->to_texture();
1643 case VFrame::SCREEN:
1644 // Read back from PBuffer
1645 // Bind context to pbuffer
1646 command->frame->enable_opengl();
1647 command->frame->screen_to_texture();
1651 command->frame->enable_opengl();
1652 command->frame->init_screen();
1653 command->frame->bind_texture(0);
1655 // glClearColor(0.0, 0.0, 0.0, 0.0);
1656 // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1657 glDisable(GL_BLEND);
1658 unsigned int frag_shader = 0;
1659 switch(command->frame->get_color_model())
1661 // For the alpha colormodels, the native function seems to multiply the
1662 // components by the alpha instead of just the alpha.
1666 frag_shader = VFrame::make_shader(0, fade_rgba_frag, 0);
1671 glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1672 glColor4f(command->alpha, command->alpha, command->alpha, 1);
1677 frag_shader = VFrame::make_shader(0, fade_yuv_frag, 0);
1683 glUseProgram(frag_shader);
1685 if((variable = glGetUniformLocation(frag_shader, "tex")) >= 0)
1686 glUniform1i(variable, 0);
1687 if((variable = glGetUniformLocation(frag_shader, "alpha")) >= 0)
1688 glUniform1f(variable, command->alpha);
1691 command->frame->draw_texture();
1692 command->frame->set_opengl_state(VFrame::SCREEN);
1699 glColor4f(1, 1, 1, 1);
1700 glDisable(GL_BLEND);
1702 command->canvas->unlock_canvas();
1707 int Playback3D::run_plugin(Canvas *canvas, PluginClient *client)
1709 Playback3DCommand command;
1710 command.command = Playback3DCommand::PLUGIN;
1711 command.canvas = canvas;
1712 command.plugin_client = client;
1713 return send_command(&command);
1716 void Playback3D::run_plugin_sync(Playback3DCommand *command)
1718 BC_WindowBase *window =
1719 command->canvas->lock_canvas("Playback3D::run_plugin_sync");
1721 window->enable_opengl();
1722 command->result = ((PluginVClient*)command->plugin_client)->handle_opengl();
1724 command->canvas->unlock_canvas();