From 3db2028126e12a9c1c1fb2f878d552a2de0e1452 Mon Sep 17 00:00:00 2001 From: mtklein Date: Tue, 7 Jun 2016 12:12:37 -0700 Subject: linear -> sRGB: use fast approximate sqrt() Since we're already approximating the sRGB gamma curve with a sqrt(), we might as well approximate with it a faster approximate sqrt(). On Intel, this .rsqrt().invert() version is 2-3x faster than .sqrt() (~3x faster on older machines, ~2x faster on newer machines). This should provide ~11 bits of precision, suspiciously exactly enough. Running dm --config srgb, there are diffs, but none perceptible. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2046063002 Review-Url: https://codereview.chromium.org/2046063002 --- src/core/SkPM4fPriv.h | 2 +- src/core/SkXfermode4f.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core') diff --git a/src/core/SkPM4fPriv.h b/src/core/SkPM4fPriv.h index 9650646d95..bf5ff5a1ad 100644 --- a/src/core/SkPM4fPriv.h +++ b/src/core/SkPM4fPriv.h @@ -41,7 +41,7 @@ static inline Sk4f srgb_to_linear(const Sk4f& s4) { } static inline Sk4f linear_to_srgb(const Sk4f& l4) { - return set_alpha(l4.sqrt(), get_alpha(l4)); + return set_alpha(l4.rsqrt().invert(), get_alpha(l4)); } static inline float srgb_to_linear(float x) { diff --git a/src/core/SkXfermode4f.cpp b/src/core/SkXfermode4f.cpp index 4d224ac0ad..8671df7b2f 100644 --- a/src/core/SkXfermode4f.cpp +++ b/src/core/SkXfermode4f.cpp @@ -68,10 +68,10 @@ static Sk4x4f load_4_srgb(const void* ptr) { // Store an Sk4x4f back to 4 interlaced 8888 sRGB pixels. static void store_4_srgb(void* ptr, const Sk4x4f& p) { // Convert back to sRGB and [0,255], again approximating sRGB as gamma == 2. - auto r = p.r.sqrt() * 255.0f + 0.5f, - g = p.g.sqrt() * 255.0f + 0.5f, - b = p.b.sqrt() * 255.0f + 0.5f, - a = p.a * 255.0f + 0.5f; + auto r = p.r.rsqrt().invert() * 255.0f + 0.5f, + g = p.g.rsqrt().invert() * 255.0f + 0.5f, + b = p.b.rsqrt().invert() * 255.0f + 0.5f, + a = p.a * 255.0f + 0.5f; Sk4x4f{r,g,b,a}.transpose((uint8_t*)ptr); } -- cgit v1.2.3