4 * Copyright (C) 2008 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 #ifndef GAMMA_AGGREGATED
23 #define GAMMA_AGGREGATED
25 // Gamma sections performed by other plugins
27 // Functions to get pixel from either previous effect or texture
28 static const char *gamma_get_pixel1 =
29 "vec4 gamma_get_pixel()\n"
31 " return gl_FragColor;\n"
34 static const char *gamma_get_pixel2 =
35 "uniform sampler2D tex;\n"
36 "vec4 gamma_get_pixel()\n"
38 " return texture2D(tex, gl_TexCoord[0].st);\n"
41 static const char *gamma_pow_frag =
42 "float my_pow(float x, float y, float max)\n"
44 " return (x > 0.0) ? pow(x * 2.0 / max, y) : 0.0;\n"
47 static const char *gamma_rgb_frag =
48 "uniform float gamma_scale;\n"
49 "uniform float gamma_gamma;\n"
50 "uniform float gamma_max;\n"
53 " vec4 pixel = gamma_get_pixel();\n"
54 " pixel.r = pixel.r * gamma_scale * my_pow(pixel.r, gamma_gamma, gamma_max);\n"
55 " pixel.g = pixel.g * gamma_scale * my_pow(pixel.g, gamma_gamma, gamma_max);\n"
56 " pixel.b = pixel.b * gamma_scale * my_pow(pixel.b, gamma_gamma, gamma_max);\n"
57 " gl_FragColor = pixel;\n"
60 static const char *gamma_yuv_frag =
61 "uniform float gamma_scale;\n"
62 "uniform float gamma_gamma;\n"
63 "uniform float gamma_max;\n"
66 " vec4 pixel = gamma_get_pixel();\n"
67 YUV_TO_RGB_FRAG("pixel")
68 " pixel.r = pixel.r * gamma_scale * my_pow(pixel.r, gamma_gamma, gamma_max);\n"
69 " pixel.g = pixel.g * gamma_scale * my_pow(pixel.g, gamma_gamma, gamma_max);\n"
70 " pixel.b = pixel.b * gamma_scale * my_pow(pixel.b, gamma_gamma, gamma_max);\n"
71 RGB_TO_YUV_FRAG("pixel")
72 " gl_FragColor = pixel;\n"
75 #define GAMMA_COMPILE(shader_stack, current_shader, aggregate_interpolation) do { \
76 shader_stack[current_shader++] = \
77 (aggregate_interpolation) ? gamma_get_pixel1 : gamma_get_pixel2; \
78 shader_stack[current_shader++] = gamma_pow_frag; \
79 shader_stack[current_shader++] = \
80 !BC_CModels::is_yuv(get_output()->get_color_model()) ? \
81 gamma_rgb_frag : gamma_yuv_frag; \
84 #define GAMMA_UNIFORMS(frag) do { \
85 float gamma = get_output()->get_params()->get("GAMMA_GAMMA", (float)1); \
86 float max = get_output()->get_params()->get("GAMMA_MAX", (float)1) * gamma; \
88 float scale = 1.0 / max; \
89 glUniform1f(glGetUniformLocation(frag, "gamma_scale"), scale); \
90 glUniform1f(glGetUniformLocation(frag, "gamma_gamma"), gamma); \
91 glUniform1f(glGetUniformLocation(frag, "gamma_max"), max); \