// uniform vec2 PREFIX(tc_scale);
// uniform float PREFIX(round_fac), PREFIX(inv_round_fac);
+#if YCBCR_ALSO_OUTPUT_RGBA
+
+// There are two values to dither; otherwise, exactly the same as the algorithm below
+// (so comments are not duplicated).
+
+vec4[2] FUNCNAME(vec2 tc) {
+ vec4[2] result = INPUT(tc);
+ float d = tex2D(PREFIX(dither_tex), tc * PREFIX(tc_scale)).x;
+ result[0].rgb += vec3(d);
+ result[1].rgb += vec3(d);
+
+#if NEED_EXPLICIT_ROUND
+ result[0] = round(result[0] * vec4(PREFIX(round_fac))) * vec4(PREFIX(inv_round_fac));
+ result[1] = round(result[1] * vec4(PREFIX(round_fac))) * vec4(PREFIX(inv_round_fac));
+#endif
+
+ return result;
+}
+
+#else
+
vec4 FUNCNAME(vec2 tc) {
vec4 result = INPUT(tc);
+ float d = tex2D(PREFIX(dither_tex), tc * PREFIX(tc_scale)).x;
// Don't dither alpha; the case of alpha=255 (1.0) is very important to us,
// and if there's any inaccuracy earlier in the chain so that it becomes e.g.
// 254.8, it's better to just get it rounded off than to dither and have it
// possibly get down to 254. This is not the case for the color components.
- result.rgb += tex2D(PREFIX(dither_tex), tc * PREFIX(tc_scale)).xxx;
+ result.rgb += vec3(d);
// NEED_EXPLICIT_ROUND will be #defined to 1 if the GPU has inaccurate
// fp32 -> int8 framebuffer rounding, and 0 otherwise.
return result;
}
+
+#endif
// Unit tests for DitherEffect.
+//
+// Note: Dithering of multiple outputs is tested (somewhat weakly)
+// in YCbCrConversionEffectTest.
#include <epoxy/gl.h>
#include <math.h>
EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool)
: aspect_nom(aspect_nom),
aspect_denom(aspect_denom),
+ output_color_rgba(false),
+ output_color_ycbcr(false),
dither_effect(NULL),
num_dither_bits(0),
output_origin(OUTPUT_ORIGIN_BOTTOM_LEFT),
void EffectChain::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
{
assert(!finalized);
+ assert(!output_color_rgba);
output_format = format;
output_alpha_format = alpha_format;
- output_color_type = OUTPUT_COLOR_RGB;
+ output_color_rgba = true;
}
void EffectChain::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format,
const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
{
assert(!finalized);
+ assert(!output_color_ycbcr);
output_format = format;
output_alpha_format = alpha_format;
- output_color_type = OUTPUT_COLOR_YCBCR;
+ output_color_ycbcr = true;
output_ycbcr_format = ycbcr_format;
output_ycbcr_splitting = output_splitting;
frag_shader += string("#define INPUT ") + phase->effect_ids[phase->effects.back()] + "\n";
// If we're the last phase, add the right #defines for Y'CbCr multi-output as needed.
- if (phase->output_node->outgoing_links.empty() && output_color_type == OUTPUT_COLOR_YCBCR) {
+ if (phase->output_node->outgoing_links.empty() && output_color_ycbcr) {
switch (output_ycbcr_splitting) {
case YCBCR_OUTPUT_INTERLEAVED:
// No #defines set.
default:
assert(false);
}
+
+ if (output_color_rgba) {
+ // Note: Needs to come in the header, because not only the
+ // output needs to see it (YCbCrConversionEffect and DitherEffect
+ // do, too).
+ frag_shader_header += "#define YCBCR_ALSO_OUTPUT_RGBA 1\n";
+ }
}
frag_shader.append(read_version_dependent_file("footer", "frag"));
// gamma-encoded data.
void EffectChain::add_ycbcr_conversion_if_needed()
{
- assert(output_color_type == OUTPUT_COLOR_RGB || output_color_type == OUTPUT_COLOR_YCBCR);
- if (output_color_type != OUTPUT_COLOR_YCBCR) {
+ assert(output_color_rgba || output_color_ycbcr);
+ if (!output_color_ycbcr) {
return;
}
Node *output = find_output_node();
}
Effect *add_effect(Effect *effect, const std::vector<Effect *> &inputs);
- // Adds an RGB output. Note that you can only have one output.
+ // Adds an RGBA output. Note that you can have at most one RGBA output and one
+ // Y'CbCr output (see below for details).
void add_output(const ImageFormat &format, OutputAlphaFormat alpha_format);
// Adds an YCbCr output. Note that you can only have one output.
// Currently, only chunked packed output is supported, and only 4:4:4
// (so chroma_subsampling_x and chroma_subsampling_y must both be 1).
+ //
+ // If you have both RGBA and Y'CbCr output, the RGBA output will come
+ // in the last draw buffer. Also, <format> and <alpha_format> must be
+ // identical between the two.
void add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format,
const YCbCrFormat &ycbcr_format,
YCbCrOutputSplitting output_splitting = YCBCR_OUTPUT_INTERLEAVED);
ImageFormat output_format;
OutputAlphaFormat output_alpha_format;
- enum OutputColorType { OUTPUT_COLOR_RGB, OUTPUT_COLOR_YCBCR };
- OutputColorType output_color_type;
- YCbCrFormat output_ycbcr_format; // If output_color_type == OUTPUT_COLOR_YCBCR.
- YCbCrOutputSplitting output_ycbcr_splitting; // If output_color_type == OUTPUT_COLOR_YCBCR.
+ bool output_color_rgba, output_color_ycbcr;
+ YCbCrFormat output_ycbcr_format; // If output_color_ycbcr is true.
+ YCbCrOutputSplitting output_ycbcr_splitting; // If output_color_ycbcr is true.
std::vector<Node *> nodes;
std::map<Effect *, Node *> node_map;
out vec4 FragColor;
#endif
+#if YCBCR_ALSO_OUTPUT_RGBA
+out vec4 RGBA;
+#endif
+
void main()
{
- vec4 color = INPUT(tc);
+#if YCBCR_ALSO_OUTPUT_RGBA
+ vec4 color[2] = INPUT(tc);
+ vec4 color0 = color[0];
+ vec4 color1 = color[1];
+#else
+ vec4 color0 = INPUT(tc);
+#endif
+
#if YCBCR_OUTPUT_PLANAR
- Y = color.rrra;
- Cb = color.ggga;
- Cr = color.bbba;
+ Y = color0.rrra;
+ Cb = color0.ggga;
+ Cr = color0.bbba;
#elif YCBCR_OUTPUT_SPLIT_Y_AND_CBCR
- Y = color.rrra;
- Chroma = color.gbba;
+ Y = color0.rrra;
+ Chroma = color0.gbba;
#else
- FragColor = color;
+ FragColor = color0;
+#endif
+
+#if YCBCR_ALSO_OUTPUT_RGBA
+ RGBA = color1;
#endif
}
out vec4 FragColor;
#endif
+#if YCBCR_ALSO_OUTPUT_RGBA
+out vec4 RGBA;
+#endif
+
void main()
{
- vec4 color = INPUT(tc);
+#if YCBCR_ALSO_OUTPUT_RGBA
+ vec4 color[2] = INPUT(tc);
+ vec4 color0 = color[0];
+ vec4 color1 = color[1];
+#else
+ vec4 color0 = INPUT(tc);
+#endif
+
#if YCBCR_OUTPUT_PLANAR
- Y = color.rrra;
- Cb = color.ggga;
- Cr = color.bbba;
+ Y = color0.rrra;
+ Cb = color0.ggga;
+ Cr = color0.bbba;
#elif YCBCR_OUTPUT_SPLIT_Y_AND_CBCR
- Y = color.rrra;
- Chroma = color.gbba;
+ Y = color0.rrra;
+ Chroma = color0.gbba;
#else
- FragColor = color;
+ FragColor = color0;
+#endif
+
+#if YCBCR_ALSO_OUTPUT_RGBA
+ RGBA = color1;
#endif
}
// changes, even within git versions. There is no specific version
// documentation outside the regular changelogs, though.
-#define MOVIT_VERSION 7
+#define MOVIT_VERSION 8
#endif // !defined(_MOVIT_VERSION_H)
uniform sampler2D PREFIX(tex_cb);
uniform sampler2D PREFIX(tex_cr);
+#if YCBCR_ALSO_OUTPUT_RGBA
+vec4[2] FUNCNAME(vec2 tc) {
+#else
vec4 FUNCNAME(vec2 tc) {
+#endif
vec4 rgba = INPUT(tc);
vec4 ycbcr_a;
#endif
ycbcr_a.a = rgba.a;
+
+#if YCBCR_ALSO_OUTPUT_RGBA
+ return vec4[2](ycbcr_a, rgba);
+#else
return ycbcr_a;
+#endif
}
expect_equal(expected_cbcr, out_cbcr, width * 4, height);
}
+TEST(YCbCrConversionEffectTest, OutputChunkyAndRGBA) {
+ const int width = 1;
+ const int height = 5;
+
+ // Pure-color test inputs, calculated with the formulas in Rec. 601
+ // section 2.5.4.
+ unsigned char y[width * height] = {
+ 16, 235, 81, 145, 41,
+ };
+ unsigned char cb[width * height] = {
+ 128, 128, 90, 54, 240,
+ };
+ unsigned char cr[width * height] = {
+ 128, 128, 240, 34, 110,
+ };
+ unsigned char expected_ycbcr[width * height * 4] = {
+ // The same data, just rearranged.
+ 16, 128, 128, 255,
+ 235, 128, 128, 255,
+ 81, 90, 240, 255,
+ 145, 54, 34, 255,
+ 41, 240, 110, 255
+ };
+ unsigned char expected_rgba[width * height * 4] = {
+ 0, 0, 0, 255,
+ 255, 255, 255, 255,
+ 255, 0, 0, 255,
+ 0, 255, 0, 255,
+ 0, 0, 255, 255,
+ };
+
+ unsigned char out_ycbcr[width * height * 4];
+ unsigned char out_rgba[width * height * 4];
+
+ EffectChainTester tester(NULL, width, height, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
+
+ ImageFormat format;
+ format.color_space = COLORSPACE_sRGB;
+ format.gamma_curve = GAMMA_sRGB;
+
+ YCbCrFormat ycbcr_format;
+ ycbcr_format.luma_coefficients = YCBCR_REC_601;
+ ycbcr_format.full_range = false;
+ ycbcr_format.num_levels = 256;
+ ycbcr_format.chroma_subsampling_x = 1;
+ ycbcr_format.chroma_subsampling_y = 1;
+ ycbcr_format.cb_x_position = 0.5f;
+ ycbcr_format.cb_y_position = 0.5f;
+ ycbcr_format.cr_x_position = 0.5f;
+ ycbcr_format.cr_y_position = 0.5f;
+
+ tester.add_output(format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
+ tester.add_ycbcr_output(format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, ycbcr_format);
+
+ YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
+ input->set_pixel_data(0, y);
+ input->set_pixel_data(1, cb);
+ input->set_pixel_data(2, cr);
+ tester.get_chain()->add_input(input);
+
+ // Note: We don't test that the values actually get dithered,
+ // just that the shader compiles and doesn't mess up badly.
+ tester.get_chain()->set_dither_bits(8);
+
+ tester.run(out_ycbcr, out_rgba, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
+ expect_equal(expected_ycbcr, out_ycbcr, width * 4, height);
+
+ // Y'CbCr isn't 100% accurate (the input values are rounded),
+ // so we need some leeway.
+ expect_equal(expected_rgba, out_rgba, 4 * width, height, 7, 255 * 0.002);
+}
+
} // namespace movit