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, 0, YUV::mat)
47 #define BC_GL_YMINF(shader,mat) \
48 glUniform1f(glGetUniformLocation(shader, "yminf"), YUV::mat[9])
50 #define BC_GL_RGB_TO_YUV(shader) do { \
51 BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
52 BC_GL_YMINF(shader, rgb_to_yuv_matrix); \
55 #define BC_GL_YUV_TO_RGB(shader) do { \
56 BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
57 BC_GL_YMINF(shader, yuv_to_rgb_matrix); \
60 #define BC_GL_COLORS(shader) do { \
61 BC_GL_MATRIX(shader, yuv_to_rgb_matrix); \
62 BC_GL_MATRIX(shader, rgb_to_yuv_matrix); \
63 BC_GL_YMINF(shader, rgb_to_yuv_matrix); \
67 #define bc_gl_yuv_to_rgb "uniform mat3 yuv_to_rgb_matrix;\n"
68 #define bc_gl_rgb_to_yuv "uniform mat3 rgb_to_yuv_matrix;\n"
69 #define bc_gl_yminf "uniform float yminf;\n"
70 #define bc_gl_colors bc_gl_yuv_to_rgb bc_gl_rgb_to_yuv bc_gl_yminf
72 // Macros for useful fragment shaders
73 #define YUV_TO_RGB_FRAG(PIXEL) \
74 PIXEL ".rgb -= vec3(yminf, 0.5, 0.5);\n" \
75 PIXEL ".rgb = yuv_to_rgb_matrix * " PIXEL ".rgb;\n"
77 #define RGB_TO_YUV_FRAG(PIXEL) \
78 PIXEL ".rgb = rgb_to_yuv_matrix * " PIXEL ".rgb;\n" \
79 PIXEL ".rgb += vec3(yminf, 0.5, 0.5);\n"
81 #define RGB_TO_HSV_FRAG(PIXEL) \
85 "float min, max, delta;\n" \
86 "float f, p, q, t;\n" \
87 "r = " PIXEL ".r;\n" \
88 "g = " PIXEL ".g;\n" \
89 "b = " PIXEL ".b;\n" \
90 "min = ((r < g) ? r : g) < b ? ((r < g) ? r : g) : b;\n" \
91 "max = ((r > g) ? r : g) > b ? ((r > g) ? r : g) : b;\n" \
93 "delta = max - min;\n" \
94 "if(max != 0.0 && delta != 0.0)\n" \
96 " s = delta / max;\n" \
98 " h = (g - b) / delta;\n" \
101 " h = 2.0 + (b - r) / delta;\n" \
103 " h = 4.0 + (r - g) / delta;\n" \
114 "" PIXEL ".r = h;\n" \
115 "" PIXEL ".g = s;\n" \
116 "" PIXEL ".b = v;\n" \
119 #define HSV_TO_RGB_FRAG(PIXEL) \
124 "float min, max, delta;\n" \
125 "float f, p, q, t;\n" \
126 "h = " PIXEL ".r;\n" \
127 "s = " PIXEL ".g;\n" \
128 "v = " PIXEL ".b;\n" \
131 " r = g = b = v;\n" \
137 " f = h - float(i);\n" \
138 " p = v * (1.0 - s);\n" \
139 " q = v * (1.0 - s * f);\n" \
140 " t = v * (1.0 - s * (1.0 - f));\n" \
184 "" PIXEL ".r = r;\n" \
185 "" PIXEL ".g = g;\n" \
186 "" PIXEL ".b = b;\n" \
189 class Playback3DCommand : public BC_SynchronousCommand
193 void copy_from(BC_SynchronousCommand *command);
199 WRITE_BUFFER = LAST_COMMAND,
214 // Parameters for overlay command
226 int interpolation_type;
232 int64_t start_position_project;
233 MaskAutos *keyframe_set;
235 MaskAuto *default_auto;
236 PluginClient *plugin_client;
240 class Playback3D : public BC_Synchronous
243 Playback3D(MWindow *mwindow);
246 BC_SynchronousCommand* new_command();
247 void handle_command(BC_SynchronousCommand *command);
249 // Called by VDeviceX11::write_buffer during video playback
250 void write_buffer(Canvas *canvas,
262 // Reads from pbuffer to either RAM or texture and updates the dst state
263 // want_texture - causes read into texture if 1
264 void copy_from(Canvas *canvas,
267 int want_texture = 0);
269 // Clear framebuffer before composing virtual console
270 // output - passed when rendering refresh frame. If 0, the canvas is cleared.
271 void clear_output(Canvas *canvas, VFrame *output);
273 void do_fade(Canvas *canvas, VFrame *frame, float fade);
274 void convert_cmodel(Canvas *canvas, VFrame *output, int dst_cmodel);
276 void do_mask(Canvas *canvas,
278 int64_t start_position_project,
279 MaskAutos *keyframe_set,
281 MaskAuto *default_auto);
284 // Overlay a virtual node on the framebuffer
285 void overlay(Canvas *canvas,
295 float alpha, // 0 - 1
297 int interpolation_type,
298 // supplied if rendering single frame to PBuffer.
303 int run_plugin(Canvas *canvas, PluginClient *client);
305 void clear_input(Canvas *canvas, VFrame *frame);
306 void do_camera(Canvas *canvas,
319 // Called by write_buffer and clear_frame to initialize OpenGL flags
320 void init_frame(Playback3DCommand *command);
321 void write_buffer_sync(Playback3DCommand *command);
322 void draw_output(Playback3DCommand *command);
323 void clear_output_sync(Playback3DCommand *command);
324 void clear_input_sync(Playback3DCommand *command);
325 void overlay_sync(Playback3DCommand *command);
326 // Read frame buffer back into texture for overlay operation
327 void enable_overlay_texture(Playback3DCommand *command);
328 void do_fade_sync(Playback3DCommand *command);
329 void do_mask_sync(Playback3DCommand *command);
330 void run_plugin_sync(Playback3DCommand *command);
331 void do_camera_sync(Playback3DCommand *command);
332 // void draw_refresh_sync(Playback3DCommand *command);
333 void copy_from_sync(Playback3DCommand *command);
334 void convert_cmodel_sync(Playback3DCommand *command);
336 // Print errors from shader compilation
337 void print_error(unsigned int object, int is_program);
339 // This quits the program when it's 1.
341 // Temporaries for render to texture
342 BC_Texture *temp_texture;
343 // This is set by clear_output and used in compositing directly
344 // to the output framebuffer.