Skip to content

Commit

Permalink
On second thought, remove GrColor4s
Browse files Browse the repository at this point in the history
We're going to use half-floats, which are far more future-proof.

Bug: skia:
Change-Id: I6e098017381256d6e750ac546c353072802282cb
Reviewed-on: https://skia-review.googlesource.com/c/165522
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
  • Loading branch information
brianosman authored and Skia Commit-Bot committed Oct 26, 2018
1 parent f91645d commit 3b79aa3
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 131 deletions.
25 changes: 1 addition & 24 deletions bench/VertexColorSpaceBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ enum Mode {
kFloat_Mode, // Transform colors on CPU, use float4 attributes.
kHalf_Mode, // Transform colors on CPU, use half4 attributes.
kShader_Mode, // Use ubyte4 attributes, transform colors on GPU (vertex shader).
kShort_Mode, // Transform on CPU, use short4 (4.12) attributes with a bit of shader math.
};

class GP : public GrGeometryProcessor {
Expand All @@ -52,9 +51,6 @@ class GP : public GrGeometryProcessor {
case kHalf_Mode:
fInColor = {"inColor", kHalf4_GrVertexAttribType, kHalf4_GrSLType};
break;
case kShort_Mode:
fInColor = {"inColor", kShort4_GrVertexAttribType, kFloat4_GrSLType};
break;
}
this->setVertexAttributeCnt(2);
}
Expand Down Expand Up @@ -84,8 +80,6 @@ class GP : public GrGeometryProcessor {
vertBuilder->appendColorGamutXform(&xformedColor, "color", &fColorSpaceHelper);
vertBuilder->codeAppendf("color = %s;", xformedColor.c_str());
vertBuilder->codeAppend("color = half4(color.rgb * color.a, color.a);");
} else if (kShort_Mode == gp.fMode) {
vertBuilder->codeAppend("color = color * (1 / 4095.0);");
}

vertBuilder->codeAppendf("%s = color;", varying.vsOut());
Expand Down Expand Up @@ -145,7 +139,7 @@ class Op : public GrMeshDrawOp {
: INHERITED(ClassID())
, fMode(mode)
, fColor4f(color4f) {
SkASSERT(kFloat_Mode == fMode || kHalf_Mode == mode || kShort_Mode == mode);
SkASSERT(kFloat_Mode == fMode || kHalf_Mode == mode);
this->setBounds(SkRect::MakeWH(100.f, 100.f), HasAABloat::kNo, IsZeroArea::kNo);
}

Expand Down Expand Up @@ -177,7 +171,6 @@ class Op : public GrMeshDrawOp {
vertexStride += sizeof(SkColor4f);
break;
case kHalf_Mode:
case kShort_Mode:
vertexStride += sizeof(uint64_t);
break;
default:
Expand Down Expand Up @@ -227,20 +220,6 @@ class Op : public GrMeshDrawOp {
v[i + 1].fPos.set(dx * i, 100.0f);
v[i + 1].fColor = color;
}
} else if (kShort_Mode == fMode) {
struct V {
SkPoint fPos;
GrColor4s fColor;
};
SkASSERT(sizeof(V) == vertexStride);
GrColor4s color = GrColor4s::FromFloat4(fColor4f.vec());
V* v = (V*)verts;
for (int i = 0; i < kVertexCount; i += 2) {
v[i + 0].fPos.set(dx * i, 0.0f);
v[i + 0].fColor = color;
v[i + 1].fPos.set(dx * i, 100.0f);
v[i + 1].fColor = color;
}
} else {
struct V {
SkPoint fPos;
Expand Down Expand Up @@ -320,7 +299,6 @@ class VertexColorSpaceBench : public Benchmark {
op = pool->allocate<Op>(SkColorToUnpremulGrColor(c), xform);
break;
case kHalf_Mode:
case kShort_Mode:
case kFloat_Mode: {
SkColor4f c4f = SkColor4f::FromColor(c);
c4f = xform->apply(c4f);
Expand All @@ -344,5 +322,4 @@ class VertexColorSpaceBench : public Benchmark {
DEF_BENCH(return new VertexColorSpaceBench(kBaseline_Mode, "baseline"));
DEF_BENCH(return new VertexColorSpaceBench(kFloat_Mode, "float"));
DEF_BENCH(return new VertexColorSpaceBench(kHalf_Mode, "half"));
DEF_BENCH(return new VertexColorSpaceBench(kShort_Mode, "short"));
DEF_BENCH(return new VertexColorSpaceBench(kShader_Mode, "shader"));
57 changes: 0 additions & 57 deletions include/private/GrColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,61 +152,4 @@ static inline GrColor GrUnpremulColor(GrColor color) {
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
50 changes: 0 additions & 50 deletions tests/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,53 +84,3 @@ DEF_TEST(Color, reporter) {
test_fast_interp(reporter);
//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);
}
}

0 comments on commit 3b79aa3

Please sign in to comment.