GrColor4s, a fixed-point signed short type for wide color vertices

Bug: skia:
Change-Id: I91b9816aae74726762c123d9f3454c5961382b7b
Reviewed-on: https://skia-review.googlesource.com/c/164680
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
This commit is contained in:
Brian Osman 2018-10-24 13:52:20 -04:00 committed by Skia Commit-Bot
parent 0afef05056
commit 4f598e8c82
3 changed files with 110 additions and 9 deletions

View File

@ -85,7 +85,7 @@ public:
vertBuilder->codeAppendf("color = %s;", xformedColor.c_str()); vertBuilder->codeAppendf("color = %s;", xformedColor.c_str());
vertBuilder->codeAppend("color = half4(color.rgb * color.a, color.a);"); vertBuilder->codeAppend("color = half4(color.rgb * color.a, color.a);");
} else if (kShort_Mode == gp.fMode) { } else if (kShort_Mode == gp.fMode) {
vertBuilder->codeAppend("color = color * (1 / 4096.0);"); vertBuilder->codeAppend("color = color * (1 / 4095.0);");
} }
vertBuilder->codeAppendf("%s = color;", varying.vsOut()); vertBuilder->codeAppendf("%s = color;", varying.vsOut());
@ -228,18 +228,12 @@ private:
v[i + 1].fColor = color; v[i + 1].fColor = color;
} }
} else if (kShort_Mode == fMode) { } else if (kShort_Mode == fMode) {
struct ShortColor { int16_t fRGBA[4]; };
struct V { struct V {
SkPoint fPos; SkPoint fPos;
ShortColor fColor; GrColor4s fColor;
}; };
SkASSERT(sizeof(V) == vertexStride); SkASSERT(sizeof(V) == vertexStride);
Sk4i c = Sk4f_round(Sk4f::Load(&fColor4f) * 4096.0f); GrColor4s color = GrColor4s::FromFloat4(fColor4f.vec());
c = Sk4i::Max(-32768, Sk4i::Min(c, 32767));
ShortColor color;
for (int i = 0; i < 4; ++i) {
color.fRGBA[i] = c[i];
}
V* v = (V*)verts; V* v = (V*)verts;
for (int i = 0; i < kVertexCount; i += 2) { for (int i = 0; i < kVertexCount; i += 2) {
v[i + 0].fPos.set(dx * i, 0.0f); v[i + 0].fPos.set(dx * i, 0.0f);

View File

@ -152,4 +152,61 @@ static inline GrColor GrUnpremulColor(GrColor color) {
return GrColorPackRGBA(r, g, b, a); return GrColorPackRGBA(r, g, b, a);
} }
/**
* GrColor4s is 8 bytes (4 shorts) for for R, G, B, A, in that order. This is intended for storing
* wide-gamut (non-normalized) colors in vertex attributes. The shorts are fixed point (1.3.12),
* giving us a range of ~[-8,8], and plenty of precision.
*/
struct GrColor4s {
static constexpr float kScale = 4095.0f;
static GrColor4s FromFloat4(const float* c4f) {
auto convert = [](float x) {
return static_cast<uint16_t>(SkTPin(sk_float_round2int(x * kScale), -32768, 32767));
};
return { convert(c4f[0]), convert(c4f[1]), convert(c4f[2]), convert(c4f[3]) };
}
static GrColor4s FromGrColor(GrColor color) {
unsigned r = GrColorUnpackR(color);
unsigned g = GrColorUnpackG(color);
unsigned b = GrColorUnpackB(color);
unsigned a = GrColorUnpackA(color);
// GrColor4s has 12 fractional bits, so to map a [0-1] byte value, we need to shift up,
// and then replicate the top nibble into the bottom nibble.
return { static_cast<uint16_t>(r << 4 | r >> 4),
static_cast<uint16_t>(g << 4 | g >> 4),
static_cast<uint16_t>(b << 4 | b >> 4),
static_cast<uint16_t>(a << 4 | a >> 4) };
}
bool isNormalized() const {
// The smallest normalized value is 0x0000 == 0.0. Negative values set the top sign bit.
// The largest normalized value is 0x0fff == 1.0. Larger values set some of the next 3 bits.
// So a [0, 1] check is easy: Are the top four bits clear?
return !((fR | fG | fB | fA) & 0xF000);
}
SkColor4f toSkColor4f() const {
const float invScale = 1 / kScale;
return { static_cast<int16_t>(fR) * invScale,
static_cast<int16_t>(fG) * invScale,
static_cast<int16_t>(fB) * invScale,
static_cast<int16_t>(fA) * invScale };
}
GrColor toGrColor() const {
SkASSERT(isNormalized());
return GrColorPackRGBA(fR >> 4, fG >> 4, fB >> 4, fA >> 4);
}
// These values are actually signed shorts (as seen by the GPU), but we store them here as
// unsigned, so that we can safely/easily use bitwise operations to go to/from 8-bit, and to
// check for normalized values.
uint16_t fR;
uint16_t fG;
uint16_t fB;
uint16_t fA;
};
#endif #endif

View File

@ -84,3 +84,53 @@ DEF_TEST(Color, reporter) {
test_fast_interp(reporter); test_fast_interp(reporter);
//test_565blend(); //test_565blend();
} }
#include "GrColor.h"
DEF_GPUTEST(GrColor4s, reporter, /* options */) {
// Test that GrColor -> GrColor4s -> GrColor round-trips perfectly
for (unsigned i = 0; i <= 255; ++i) {
GrColor r = GrColorPackRGBA(i, 0, 0, 0);
GrColor g = GrColorPackRGBA(0, i, 0, 0);
GrColor b = GrColorPackRGBA(0, 0, i, 0);
GrColor a = GrColorPackRGBA(0, 0, 0, i);
REPORTER_ASSERT(reporter, r == GrColor4s::FromGrColor(r).toGrColor());
REPORTER_ASSERT(reporter, g == GrColor4s::FromGrColor(g).toGrColor());
REPORTER_ASSERT(reporter, b == GrColor4s::FromGrColor(b).toGrColor());
REPORTER_ASSERT(reporter, a == GrColor4s::FromGrColor(a).toGrColor());
REPORTER_ASSERT(reporter, GrColor4s::FromGrColor(r).isNormalized());
REPORTER_ASSERT(reporter, GrColor4s::FromGrColor(g).isNormalized());
REPORTER_ASSERT(reporter, GrColor4s::FromGrColor(b).isNormalized());
REPORTER_ASSERT(reporter, GrColor4s::FromGrColor(a).isNormalized());
}
// Test that floating point values are correctly detected as in/out of range, and that they
// round-trip to within the limits of the fixed point precision
float maxErr = 0, worstX = 0, worstRT = 0;
{
for (int i = -32768; i <= 32767; ++i) {
float x = i / 4095.0f;
float frgba[4] = { x, 0, 0, 0 };
GrColor4s c4s = GrColor4s::FromFloat4(frgba);
REPORTER_ASSERT(reporter, c4s.isNormalized() == (x >= 0.0f && x <= 1.0f));
SkColor4f c4f = c4s.toSkColor4f();
if (fabsf(c4f.fR - x) > maxErr) {
maxErr = fabsf(c4f.fR - x);
worstX = x;
worstRT = c4f.fR;
}
}
}
REPORTER_ASSERT(reporter, maxErr < 0.0001f, "maxErr: %f, %f != %f", maxErr, worstX, worstRT);
// Test clamping of unrepresentable values
{
float frgba[4] = { -8.5f, 9.0f, 0, 0 };
GrColor4s c4s = GrColor4s::FromFloat4(frgba);
REPORTER_ASSERT(reporter, !c4s.isNormalized());
SkColor4f c4f = c4s.toSkColor4f();
REPORTER_ASSERT(reporter, c4f.fR < -8.0f);
REPORTER_ASSERT(reporter, c4f.fG > 8.0f);
}
}