namespace movit {
-EffectChain::EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool, GLenum intermediate_format)
+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),
- intermediate_format(intermediate_format),
+ intermediate_format(GL_RGBA16F),
+ intermediate_transformation(NO_FRAMEBUFFER_TRANSFORMATION),
num_dither_bits(0),
output_origin(OUTPUT_ORIGIN_BOTTOM_LEFT),
finalized(false),
frag_shader += string("uniform sampler2D tex_") + effect_id + ";\n";
frag_shader += string("vec4 ") + effect_id + "(vec2 tc) {\n";
- frag_shader += "\treturn tex2D(tex_" + string(effect_id) + ", tc);\n";
+ frag_shader += "\tvec4 tmp = tex2D(tex_" + string(effect_id) + ", tc);\n";
+
+ if (intermediate_transformation == SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION &&
+ phase->inputs[i]->output_node->output_gamma_curve == GAMMA_LINEAR) {
+ frag_shader += "\ttmp.rgb *= tmp.rgb;\n";
+ }
+
+ frag_shader += "\treturn tmp;\n";
frag_shader += "}\n";
frag_shader += "\n";
frag_shader_outputs.push_back("RGBA");
}
}
+
+ // If we're bouncing to a temporary texture, signal transformation if desired.
+ if (!phase->output_node->outgoing_links.empty()) {
+ if (intermediate_transformation == SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION &&
+ phase->output_node->output_gamma_curve == GAMMA_LINEAR) {
+ frag_shader += "#define SQUARE_ROOT_TRANSFORMATION 1\n";
+ }
+ }
+
frag_shader.append(read_file("footer.frag"));
// Collect uniforms from all effects and output them. Note that this needs
OUTPUT_ORIGIN_TOP_LEFT,
};
+// Transformation to apply (if any) to pixel data in temporary buffers.
+// See set_intermediate_format() below for more information.
+enum FramebufferTransformation {
+ // The default; just store the value. This is what you usually want.
+ NO_FRAMEBUFFER_TRANSFORMATION,
+
+ // If the values are in linear light, store sqrt(x) to the framebuffer
+ // instead of x itself, of course undoing it with x² on read. Useful as
+ // a rough approximation to the sRGB curve. (If the values are not in
+ // linear light, just store them as-is.)
+ SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION,
+};
+
// A node in the graph; basically an effect and some associated information.
class Node {
public:
// will create its own that is not shared with anything else. Does not take
// ownership of the passed-in ResourcePool, but will naturally take ownership
// of its own internal one if created.
- EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool = NULL, GLenum intermediate_format = GL_RGBA16F);
+ EffectChain(float aspect_nom, float aspect_denom, ResourcePool *resource_pool = NULL);
~EffectChain();
// User API:
this->output_origin = output_origin;
}
+ // Set intermediate format for framebuffers used when we need to bounce
+ // to a temporary texture. The default, GL_RGBA16F, is good for most uses;
+ // it is precise, has good range, and is relatively efficient. However,
+ // if you need even more speed and your chain can do with some loss of
+ // accuracy, you can change the format here (before calling finalize).
+ // Calculations between bounce buffers are still in 32-bit floating-point
+ // no matter what you specify.
+ //
+ // Of special interest is GL_SRGB8_ALPHA8, which stores sRGB-encoded RGB
+ // and linear alpha; this is half the memory bandwidth og GL_RGBA16F,
+ // while retaining reasonable precision for typical image data. It will,
+ // however, cause some gamut clipping if your colorspace is far from sRGB,
+ // as it cannot represent values outside [0,1]. NOTE: If you construct
+ // a chain where you end up bouncing pixels in non-linear light this
+ // will not do the wrong thing. However, it's hard to see how this
+ // could happen in a non-contrived chain; few effects ever need texture
+ // bounce or resizing without also combining multiple pixels, which
+ // really needs linear light and thus triggers a conversion before the
+ // bounce.
+ //
+ // If you don't need alpha (or can do with very little of it), GL_RGB10_A2
+ // is even better, as it has two more bits for each color component. There
+ // is no GL_SRGB10, unfortunately, so on its own, it is somewhat worse than
+ // GL_SRGB8, but you can set <transformation> to SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION,
+ // and sqrt(x) will be stored instead of x. This is a rough approximation to
+ // the sRGB curve, and reduces maximum error (in sRGB distance) by almost an
+ // order of magnitude, well below what you can get from 8-bit true sRGB.
+ // (Note that this strategy avoids the problem with bounced non-linear data
+ // above, since the square root is turned off in that case.)
+ void set_intermediate_format(
+ GLenum intermediate_format,
+ FramebufferTransformation transformation = NO_FRAMEBUFFER_TRANSFORMATION)
+ {
+ this->intermediate_format = intermediate_format;
+ this->intermediate_transformation = transformation;
+ }
+
void finalize();
// Measure the GPU time used for each actual phase during rendering.
std::vector<Phase *> phases;
GLenum intermediate_format;
+ FramebufferTransformation intermediate_transformation;
unsigned num_dither_bits;
OutputOrigin output_origin;
bool finalized;
0.0f, 0.5f, 0.0f, 1.0f,
};
float out_data[4];
- EffectChainTester tester(data, 1, 1, FORMAT_RGBA_PREMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA16F_ARB, GL_SRGB8);
+ EffectChainTester tester(data, 1, 1, FORMAT_RGBA_PREMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ tester.get_chain()->set_intermediate_format(GL_SRGB8);
tester.get_chain()->add_effect(new IdentityEffect());
tester.get_chain()->add_effect(new BouncingIdentityEffect());
tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
<< "Expected sRGB to be able to represent 0.5 approximately (got " << out_data[1] << ")";
}
+// An effect that is like IdentityEffect, but also does not require linear light.
+class PassThroughEffect : public IdentityEffect {
+public:
+ PassThroughEffect() {}
+ virtual string effect_type_id() const { return "PassThroughEffect"; }
+ virtual bool needs_linear_light() const { return false; }
+ AlphaHandling alpha_handling() const { return DONT_CARE_ALPHA_TYPE; }
+};
+
+// Same, just also bouncing.
+class BouncingPassThroughEffect : public BouncingIdentityEffect {
+public:
+ BouncingPassThroughEffect() {}
+ virtual string effect_type_id() const { return "BouncingPassThroughEffect"; }
+ virtual bool needs_linear_light() const { return false; }
+ bool needs_texture_bounce() const { return true; }
+ AlphaHandling alpha_handling() const { return DONT_CARE_ALPHA_TYPE; }
+};
+
+TEST(EffectChainTest, Linear10bitIntermediateAccuracy) {
+ // Note that we do the comparison in sRGB space, which is what we
+ // typically would want; however, we do the sRGB conversion ourself
+ // to avoid compounding errors from shader conversions into the
+ // analysis.
+ const int size = 4096; // 12-bit.
+ float linear_data[size], data[size], out_data[size];
+
+ for (int i = 0; i < size; ++i) {
+ linear_data[i] = i / double(size - 1);
+ data[i] = srgb_to_linear(linear_data[i]);
+ }
+
+ EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
+ tester.get_chain()->set_intermediate_format(GL_RGB10_A2);
+ tester.get_chain()->add_effect(new IdentityEffect());
+ tester.get_chain()->add_effect(new BouncingIdentityEffect());
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ for (int i = 0; i < size; ++i) {
+ out_data[i] = linear_to_srgb(out_data[i]);
+ }
+
+ // This maximum error is pretty bad; about 6.5 levels of a 10-bit sRGB
+ // framebuffer.
+ expect_equal(linear_data, out_data, size, 1, 7e-3, 2e-5);
+}
+
+TEST(EffectChainTest, SquareRoot10bitIntermediateAccuracy) {
+ // Note that we do the comparison in sRGB space, which is what we
+ // typically would want; however, we do the sRGB conversion ourself
+ // to avoid compounding errors from shader conversions into the
+ // analysis.
+ const int size = 4096; // 12-bit.
+ float linear_data[size], data[size], out_data[size];
+
+ for (int i = 0; i < size; ++i) {
+ linear_data[i] = i / double(size - 1);
+ data[i] = srgb_to_linear(linear_data[i]);
+ }
+
+ EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
+ tester.get_chain()->set_intermediate_format(GL_RGB10_A2, SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION);
+ tester.get_chain()->add_effect(new IdentityEffect());
+ tester.get_chain()->add_effect(new BouncingIdentityEffect());
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ for (int i = 0; i < size; ++i) {
+ out_data[i] = linear_to_srgb(out_data[i]);
+ }
+
+ // This maximum error is much better; about 0.7 levels of a 10-bit sRGB
+ // framebuffer (ideal would be 0.5). That is an order of magnitude better
+ // than in the linear test above. The RMS error is much better, too.
+ expect_equal(linear_data, out_data, size, 1, 7e-4, 5e-6);
+}
+
+TEST(EffectChainTest, SquareRootIntermediateIsTurnedOffForNonLinearData) {
+ const int size = 256; // 8-bit.
+ float data[size], out_data[size];
+
+ for (int i = 0; i < size; ++i) {
+ data[i] = i / double(size - 1);
+ }
+
+ EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_601, GL_RGBA32F);
+ tester.get_chain()->set_intermediate_format(GL_RGB8, SQUARE_ROOT_FRAMEBUFFER_TRANSFORMATION);
+ tester.get_chain()->add_effect(new PassThroughEffect());
+ tester.get_chain()->add_effect(new BouncingPassThroughEffect());
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_601);
+
+ // The data should be passed through nearly exactly, since there is no effect
+ // on the path that requires linear light. (Actually, it _is_ exact modulo
+ // fp32 errors, but the error bounds is strictly _less than_, not zero.)
+ expect_equal(data, out_data, size, 1, 1e-6, 1e-6);
+}
+
} // namespace movit
#define YCBCR_ALSO_OUTPUT_RGBA 0
#endif
+#ifndef SQUARE_ROOT_TRANSFORMATION
+#define SQUARE_ROOT_TRANSFORMATION 0
+#endif
+
#if YCBCR_OUTPUT_PLANAR
out vec4 Y;
out vec4 Cb;
vec4 color0 = INPUT(tc);
#endif
+#if SQUARE_ROOT_TRANSFORMATION
+ // Make sure we don't give negative values to sqrt.
+ color0.rgb = sqrt(max(color0.rgb, 0.0));
+#endif
+
#if YCBCR_OUTPUT_PLANAR
Y = color0.rrra;
Cb = color0.ggga;
EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
- GLenum framebuffer_format,
- GLenum intermediate_format)
- : chain(width, height, get_static_pool(), intermediate_format),
+ GLenum framebuffer_format)
+ : chain(width, height, get_static_pool()),
width(width),
height(height),
framebuffer_format(framebuffer_format),
MovitPixelFormat pixel_format = FORMAT_GRAYSCALE,
Colorspace color_space = COLORSPACE_sRGB,
GammaCurve gamma_curve = GAMMA_LINEAR,
- GLenum framebuffer_format = GL_RGBA16F_ARB,
- GLenum intermediate_format = GL_RGBA16F_ARB);
+ GLenum framebuffer_format = GL_RGBA16F_ARB);
~EffectChainTester();
EffectChain *get_chain() { return &chain; }
// changes, even within git versions. There is no specific version
// documentation outside the regular changelogs, though.
-#define MOVIT_VERSION 21
+#define MOVIT_VERSION 22
#endif // !defined(_MOVIT_VERSION_H)