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
25 #include "arraylist.h"
27 #include "bcpixmap.inc"
28 #include "bcsynchronous.h"
29 #include "bcwindowbase.inc"
31 #include "condition.inc"
32 #include "maskauto.inc"
33 #include "maskautos.inc"
35 #include "mwindow.inc"
36 #include "pluginclient.inc"
43 // use static presets YUV in bccolors.h
44 #define BC_GL_MATRIX(shader, mat) \
45 glUniformMatrix3fv(glGetUniformLocation(shader, #mat), 1, 1, YUV::mat)
46 #define BC_GL_VECTOR(shader, vec) \
47 glUniform3fv(glGetUniformLocation(shader, #vec), 1, YUV::vec)
49 #define BC_GL_YMINF(shader) \
50 glUniform1f(glGetUniformLocation(shader, "yminf"), YUV::yuv.get_yminf())
52 #define BC_GL_RGB_TO_YUV(shader) do { \
53 BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
54 BC_GL_YMINF(shader); \
57 #define BC_GL_RGB_TO_Y(shader) do { \
58 BC_GL_VECTOR(shader, rgb_to_y_vector); \
59 BC_GL_YMINF(shader); \
62 #define BC_GL_YUV_TO_RGB(shader) do { \
63 BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
64 BC_GL_YMINF(shader); \
67 #define BC_GL_COLORS(shader) do { \
68 BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
69 BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
70 BC_GL_YMINF(shader); \
74 #define bc_gl_yuv_to_rgb "uniform mat3 yuv_to_rgb_matrix;\n"
75 #define bc_gl_rgb_to_yuv "uniform mat3 rgb_to_yuv_matrix;\n"
76 #define bc_gl_rgb_to_y "uniform vec3 rgb_to_y_vector;\n"
77 #define bc_gl_yminf "uniform float yminf;\n"
78 #define bc_gl_colors bc_gl_yuv_to_rgb bc_gl_rgb_to_yuv bc_gl_yminf
80 // Macros for useful fragment shaders
81 #define YUV_TO_RGB_FRAG(PIXEL) \
82 PIXEL ".rgb -= vec3(yminf, 0.5, 0.5);\n" \
83 PIXEL ".rgb = yuv_to_rgb_matrix * " PIXEL ".rgb;\n"
85 #define RGB_TO_YUV_FRAG(PIXEL) \
86 PIXEL ".rgb = rgb_to_yuv_matrix * " PIXEL ".rgb;\n" \
87 PIXEL ".rgb += vec3(yminf, 0.5, 0.5);\n"
89 #define RGB_TO_HSV_FRAG(PIXEL) \
93 "float min, max, delta;\n" \
94 "float f, p, q, t;\n" \
95 "r = " PIXEL ".r;\n" \
96 "g = " PIXEL ".g;\n" \
97 "b = " PIXEL ".b;\n" \
98 "min = ((r < g) ? r : g) < b ? ((r < g) ? r : g) : b;\n" \
99 "max = ((r > g) ? r : g) > b ? ((r > g) ? r : g) : b;\n" \
101 "delta = max - min;\n" \
102 "if(max != 0.0 && delta != 0.0)\n" \
104 " s = delta / max;\n" \
106 " h = (g - b) / delta;\n" \
109 " h = 2.0 + (b - r) / delta;\n" \
111 " h = 4.0 + (r - g) / delta;\n" \
122 "" PIXEL ".r = h;\n" \
123 "" PIXEL ".g = s;\n" \
124 "" PIXEL ".b = v;\n" \
127 #define HSV_TO_RGB_FRAG(PIXEL) \
132 "float min, max, delta;\n" \
133 "float f, p, q, t;\n" \
134 "h = " PIXEL ".r;\n" \
135 "s = " PIXEL ".g;\n" \
136 "v = " PIXEL ".b;\n" \
139 " r = g = b = v;\n" \
145 " f = h - float(i);\n" \
146 " p = v * (1.0 - s);\n" \
147 " q = v * (1.0 - s * f);\n" \
148 " t = v * (1.0 - s * (1.0 - f));\n" \
192 "" PIXEL ".r = r;\n" \
193 "" PIXEL ".g = g;\n" \
194 "" PIXEL ".b = b;\n" \
197 class Playback3DCommand : public BC_SynchronousCommand
201 void copy_from(BC_SynchronousCommand *command);
207 WRITE_BUFFER = LAST_COMMAND,
222 // Parameters for overlay command
234 int interpolation_type;
240 int64_t start_position_project;
241 MaskAutos *keyframe_set;
243 MaskAuto *default_auto;
244 PluginClient *plugin_client;
248 class Playback3D : public BC_Synchronous
251 Playback3D(MWindow *mwindow);
254 BC_SynchronousCommand* new_command();
255 void handle_command(BC_SynchronousCommand *command);
257 // Called by VDeviceX11::write_buffer during video playback
258 void write_buffer(Canvas *canvas,
270 // Reads from pbuffer to either RAM or texture and updates the dst state
271 // want_texture - causes read into texture if 1
272 void copy_from(Canvas *canvas,
275 int want_texture = 0);
277 // Clear framebuffer before composing virtual console
278 // output - passed when rendering refresh frame. If 0, the canvas is cleared.
279 void clear_output(Canvas *canvas, VFrame *output);
281 void do_fade(Canvas *canvas, VFrame *frame, float fade);
282 void convert_cmodel(Canvas *canvas, VFrame *output, int dst_cmodel);
284 void do_mask(Canvas *canvas,
286 int64_t start_position_project,
287 MaskAutos *keyframe_set,
289 MaskAuto *default_auto);
292 // Overlay a virtual node on the framebuffer
293 void overlay(Canvas *canvas,
303 float alpha, // 0 - 1
305 int interpolation_type,
306 // supplied if rendering single frame to PBuffer.
311 int run_plugin(Canvas *canvas, PluginClient *client);
313 void clear_input(Canvas *canvas, VFrame *frame);
314 void do_camera(Canvas *canvas,
327 // Called by write_buffer and clear_frame to initialize OpenGL flags
328 void init_frame(Playback3DCommand *command);
329 void write_buffer_sync(Playback3DCommand *command);
330 void draw_output(Playback3DCommand *command);
331 void clear_output_sync(Playback3DCommand *command);
332 void clear_input_sync(Playback3DCommand *command);
333 void overlay_sync(Playback3DCommand *command);
334 // Read frame buffer back into texture for overlay operation
335 void enable_overlay_texture(Playback3DCommand *command);
336 void do_fade_sync(Playback3DCommand *command);
337 void do_mask_sync(Playback3DCommand *command);
338 void run_plugin_sync(Playback3DCommand *command);
339 void do_camera_sync(Playback3DCommand *command);
340 // void draw_refresh_sync(Playback3DCommand *command);
341 void copy_from_sync(Playback3DCommand *command);
342 void convert_cmodel_sync(Playback3DCommand *command);
344 // Print errors from shader compilation
345 void print_error(unsigned int object, int is_program);
347 // This quits the program when it's 1.
349 // Temporaries for render to texture
350 BC_Texture *temp_texture;
351 // This is set by clear_output and used in compositing directly
352 // to the output framebuffer.