Interpreter: Lots of minor cleanup/refactoring

- Update the parameter lists to both run and runStriped so
  that they're in the same (sane) order, named consistently,
  and always take counts with pointer arguments.
- Add the same count-based safety checks to run that were
  already in runStriped.
- Remove the N parameter to run, it was only used to run
  things one-at-a-time (other than one spot in unit tests),
  and it simplifies the code quite a bit. If you want to run
  multiple times, use the striped version. I also moved that
  functions 'N' earlier in the parameter list, to make the
  pattern of the remaining parameters clearer.
- Remove an interpreter benchmark class that was never used.

Change-Id: Ibff0a47bdb2d29d095a0addd27e65ab13cb80fce
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/244716
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2019-09-27 10:25:57 -04:00 committed by Skia Commit-Bot
parent 3787f51c65
commit b23d66e10a
6 changed files with 171 additions and 250 deletions

View File

@ -12,11 +12,10 @@
// Benchmarks the interpreter with a function that has a color-filter style signature
class SkSLInterpreterCFBench : public Benchmark {
public:
SkSLInterpreterCFBench(SkSL::String name, int pixels, bool striped, const char* src)
: fName(SkStringPrintf("sksl_interp_cf_%d_%d_%s", pixels, striped ? 1 : 0, name.c_str()))
SkSLInterpreterCFBench(SkSL::String name, int pixels, const char* src)
: fName(SkStringPrintf("sksl_interp_cf_%d_%s", pixels, name.c_str()))
, fSrc(src)
, fCount(pixels)
, fStriped(striped) {}
, fCount(pixels) {}
protected:
const char* onGetName() override {
@ -45,19 +44,14 @@ protected:
void onDraw(int loops, SkCanvas*) override {
for (int i = 0; i < loops; i++) {
if (fStriped) {
float* args[] = {
fPixels.data() + 0 * fCount,
fPixels.data() + 1 * fCount,
fPixels.data() + 2 * fCount,
fPixels.data() + 3 * fCount,
};
float* args[] = {
fPixels.data() + 0 * fCount,
fPixels.data() + 1 * fCount,
fPixels.data() + 2 * fCount,
fPixels.data() + 3 * fCount,
};
SkAssertResult(fByteCode->runStriped(fMain, args, 4, fCount,
nullptr, 0, nullptr, 0));
} else {
SkAssertResult(fByteCode->run(fMain, fPixels.data(), nullptr, fCount, nullptr, 0));
}
SkAssertResult(fByteCode->runStriped(fMain, fCount, args, 4, nullptr, 0, nullptr, 0));
}
}
@ -68,7 +62,6 @@ private:
const SkSL::ByteCodeFunction* fMain;
int fCount;
bool fStriped;
std::vector<float> fPixels;
typedef Benchmark INHERITED;
@ -145,89 +138,5 @@ const char* kHighContrastFilterSrc = R"(
}
)";
DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, false, kLumaToAlphaSrc));
DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, true, kLumaToAlphaSrc));
DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, false, kHighContrastFilterSrc));
DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, true, kHighContrastFilterSrc));
class SkSLInterpreterSortBench : public Benchmark {
public:
SkSLInterpreterSortBench(int groups, int values, const char* src)
: fName(SkStringPrintf("sksl_interp_sort_%dx%d", groups, values))
, fCode(src)
, fGroups(groups)
, fValues(values) {
}
protected:
const char* onGetName() override {
return fName.c_str();
}
bool isSuitableFor(Backend backend) override {
return backend == kNonRendering_Backend;
}
void onDelayedSetup() override {
SkSL::Compiler compiler;
SkSL::Program::Settings settings;
auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, fCode, settings);
SkASSERT(compiler.errorCount() == 0);
fByteCode = compiler.toByteCode(*program);
SkASSERT(compiler.errorCount() == 0);
fMain = fByteCode->getFunction("main");
fSrc.resize(fGroups * fValues);
fDst.resize(fGroups * fValues);
SkRandom rnd;
for (float& x : fSrc) {
x = rnd.nextF();
}
// Trigger one run now to check correctness
SkAssertResult(fByteCode->run(fMain, fSrc.data(), fDst.data(), fGroups, nullptr, 0));
for (int i = 0; i < fGroups; ++i) {
for (int j = 1; j < fValues; ++j) {
SkASSERT(fDst[i * fValues + j] >= fDst[i * fValues + j - 1]);
}
}
}
void onDraw(int loops, SkCanvas*) override {
for (int i = 0; i < loops; i++) {
SkAssertResult(fByteCode->run(fMain, fSrc.data(), fDst.data(), fGroups, nullptr, 0));
}
}
private:
SkString fName;
SkSL::String fCode;
std::unique_ptr<SkSL::ByteCode> fByteCode;
const SkSL::ByteCodeFunction* fMain;
int fGroups;
int fValues;
std::vector<float> fSrc;
std::vector<float> fDst;
typedef Benchmark INHERITED;
};
// Currently, this exceeds the interpreter's stack. Consider it a test case for some eventual
// bounds checking.
#if 0
DEF_BENCH(return new SkSLInterpreterSortBench(1024, 32, R"(
float[32] main(float v[32]) {
for (int i = 1; i < 32; ++i) {
for (int j = i; j > 0 && v[j-1] > v[j]; --j) {
float t = v[j];
v[j] = v[j-1];
v[j-1] = t;
}
}
return v;
}
)"));
#endif
DEF_BENCH(return new SkSLInterpreterCFBench("lumaToAlpha", 256, kLumaToAlphaSrc));
DEF_BENCH(return new SkSLInterpreterCFBench("hcf", 256, kHighContrastFilterSrc));

View File

@ -235,7 +235,10 @@ int SkParticleEffect::runEffectScript(double now, const char* entry) {
value->setRandom(&fRandom);
value->setEffect(this);
}
SkAssertResult(byteCode->run(fun, &fState.fAge, nullptr, 1, &fState.fDeltaTime, 1));
// Size of the EffectState structure, minus deltaTime (which is uniform in effect code)
constexpr int EffectStructSize = 19;
SkAssertResult(byteCode->run(fun, &fState.fAge, EffectStructSize,
nullptr, 0, &fState.fDeltaTime, 1));
this->processEffectSpawnRequests(now);
}
}
@ -280,9 +283,9 @@ void SkParticleEffect::runParticleScript(double now, const char* entry, int star
value->setRandom(randomBase);
value->setEffect(this);
}
SkAssertResult(byteCode->runStriped(fun, args, SkParticles::kNumChannels, count,
&fState.fDeltaTime, sizeof(EffectState) / 4,
nullptr, 0));
SkAssertResult(byteCode->runStriped(fun, count, args, SkParticles::kNumChannels,
nullptr, 0,
&fState.fDeltaTime, sizeof(EffectState) / 4));
this->processParticleSpawnRequests(now, start);
}
}

View File

@ -2673,8 +2673,8 @@ STAGE(interpreter, SkRasterPipeline_InterpreterCtx* c) {
sk_unaligned_store(aa, a);
}
SkAssertResult(c->byteCode->runStriped(c->fn, in_args, in_count, tail ? tail : N,
(const float*)c->inputs, c->ninputs, nullptr, 0));
SkAssertResult(c->byteCode->runStriped(c->fn, tail ? tail : N, in_args, in_count,
nullptr, 0, (const float*)c->inputs, c->ninputs));
r = sk_unaligned_load<F>(rr);
g = sk_unaligned_load<F>(gg);

View File

@ -1574,7 +1574,9 @@ void ByteCodeFunction::preprocess(const void* labels[]) {
#endif
}
bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
bool ByteCode::run(const ByteCodeFunction* f,
float* args, int argCount,
float* outReturn, int returnCount,
const float* uniforms, int uniformCount) const {
#if defined(SK_ENABLE_SKSL_INTERPRETER)
Interpreter::VValue stack[128];
@ -1583,7 +1585,9 @@ bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int
return false;
}
if (uniformCount != (int)fInputSlots.size()) {
if (argCount != f->fParameterCount ||
returnCount != f->fReturnCount ||
uniformCount != (int)fInputSlots.size()) {
return false;
}
@ -1595,55 +1599,39 @@ bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int
globals[slot].fFloat = *uniforms++;
}
int baseIndex = 0;
while (N) {
int w = std::min(N, Interpreter::VecWidth);
// Transpose args into stack
{
float* src = args;
for (int i = 0; i < w; ++i) {
float* dst = (float*)stack + i;
for (int j = f->fParameterCount; j > 0; --j) {
*dst = *src++;
dst += Interpreter::VecWidth;
}
}
// Transpose args into stack
{
float* src = args;
float* dst = (float*)stack;
for (int i = 0; i < argCount; ++i) {
*dst = *src++;
dst += Interpreter::VecWidth;
}
bool stripedOutput = false;
float** outArray = outReturn ? &outReturn : nullptr;
if (!innerRun(this, f, stack, outArray, globals, stripedOutput, w, baseIndex)) {
return false;
}
// Transpose out parameters back
{
float* dst = args;
for (int i = 0; i < w; ++i) {
float* src = (float*)stack + i;
for (const auto& p : f->fParameters) {
if (p.fIsOutParameter) {
for (int j = p.fSlotCount; j > 0; --j) {
*dst++ = *src;
src += Interpreter::VecWidth;
}
} else {
dst += p.fSlotCount;
src += p.fSlotCount * Interpreter::VecWidth;
}
}
}
}
args += f->fParameterCount * w;
if (outReturn) {
outReturn += f->fReturnCount * w;
}
N -= w;
baseIndex += w;
}
bool stripedOutput = false;
float** outArray = outReturn ? &outReturn : nullptr;
if (!innerRun(this, f, stack, outArray, globals, stripedOutput, 1, 0)) {
return false;
}
// Transpose out parameters back
{
float* dst = args;
float* src = (float*)stack;
for (const auto& p : f->fParameters) {
if (p.fIsOutParameter) {
for (int i = p.fSlotCount; i > 0; --i) {
*dst++ = *src;
src += Interpreter::VecWidth;
}
} else {
dst += p.fSlotCount;
src += p.fSlotCount * Interpreter::VecWidth;
}
}
}
return true;
#else
SkDEBUGFAIL("ByteCode interpreter not enabled");
@ -1651,9 +1639,10 @@ bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int
#endif
}
bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
const float* uniforms, int uniformCount,
float* outArgs[], int outCount) const {
bool ByteCode::runStriped(const ByteCodeFunction* f, int N,
float* args[], int argCount,
float* outReturn[], int returnCount,
const float* uniforms, int uniformCount) const {
#if defined(SK_ENABLE_SKSL_INTERPRETER)
Interpreter::VValue stack[128];
int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
@ -1661,8 +1650,8 @@ bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, i
return false;
}
if (nargs != f->fParameterCount ||
outCount != f->fReturnCount ||
if (argCount != f->fParameterCount ||
returnCount != f->fReturnCount ||
uniformCount != (int)fInputSlots.size()) {
return false;
}
@ -1676,8 +1665,8 @@ bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, i
}
// innerRun just takes outArgs, so clear it if the count is zero
if (outCount == 0) {
outArgs = nullptr;
if (returnCount == 0) {
outReturn = nullptr;
}
int baseIndex = 0;
@ -1686,12 +1675,12 @@ bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, i
int w = std::min(N, Interpreter::VecWidth);
// Copy args into stack
for (int i = 0; i < nargs; ++i) {
for (int i = 0; i < argCount; ++i) {
memcpy(stack + i, args[i], w * sizeof(float));
}
bool stripedOutput = true;
if (!innerRun(this, f, stack, outArgs, globals, stripedOutput, w, baseIndex)) {
if (!innerRun(this, f, stack, outReturn, globals, stripedOutput, w, baseIndex)) {
return false;
}
@ -1707,7 +1696,7 @@ bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, i
}
// Step each argument pointer ahead
for (int i = 0; i < nargs; ++i) {
for (int i = 0; i < argCount; ++i) {
args[i] += w;
}
N -= w;

View File

@ -229,13 +229,15 @@ struct SK_API ByteCode {
* The return value is stored in 'outReturn' (may be null, to discard the return value).
* 'uniforms' are mapped to 'uniform' globals, in order.
*/
bool SKSL_WARN_UNUSED_RESULT run(const ByteCodeFunction*, float* args, float* outReturn, int N,
bool SKSL_WARN_UNUSED_RESULT run(const ByteCodeFunction*,
float* args, int argCount,
float* outReturn, int returnCount,
const float* uniforms, int uniformCount) const;
bool SKSL_WARN_UNUSED_RESULT runStriped(const ByteCodeFunction*,
float* args[], int nargs, int N,
const float* uniforms, int uniformCount,
float* outArgs[], int outArgCount) const;
bool SKSL_WARN_UNUSED_RESULT runStriped(const ByteCodeFunction*, int N,
float* args[], int argCount,
float* outReturn[], int returnCount,
const float* uniforms, int uniformCount) const;
};
}

View File

@ -22,7 +22,7 @@ static bool nearly_equal(const float a[], const float b[], int count) {
return true;
}
void test(skiatest::Reporter* r, const char* src, float* in, int expectedCount, float* expected,
void test(skiatest::Reporter* r, const char* src, float* in, float* expected,
bool exactCompare = true) {
SkSL::Compiler compiler;
SkSL::Program::Settings settings;
@ -39,21 +39,22 @@ void test(skiatest::Reporter* r, const char* src, float* in, int expectedCount,
return;
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[expectedCount]);
SkAssertResult(byteCode->run(main, in, out.get(), 1, nullptr, 0));
bool valid = exactCompare ? !memcmp(out.get(), expected, sizeof(float) * expectedCount)
: nearly_equal(out.get(), expected, expectedCount);
std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[main->fReturnCount]);
SkAssertResult(byteCode->run(main, in, main->fParameterCount, out.get(), main->fReturnCount,
nullptr, 0));
bool valid = exactCompare ? !memcmp(out.get(), expected, sizeof(float) * main->fReturnCount)
: nearly_equal(out.get(), expected, main->fReturnCount);
if (!valid) {
printf("for program: %s\n", src);
printf(" expected (");
const char* separator = "";
for (int i = 0; i < expectedCount; ++i) {
for (int i = 0; i < main->fReturnCount; ++i) {
printf("%s%f", separator, expected[i]);
separator = ", ";
}
printf("), but received (");
separator = "";
for (int i = 0; i < expectedCount; ++i) {
for (int i = 0; i < main->fReturnCount; ++i) {
printf("%s%f", separator, out.get()[i]);
separator = ", ";
}
@ -67,12 +68,6 @@ void test(skiatest::Reporter* r, const char* src, float* in, int expectedCount,
}
void vec_test(skiatest::Reporter* r, const char* src) {
// Test on four different vectors (with varying orderings to get divergent control flow)
const float input[16] = { 1, 2, 3, 4,
4, 3, 2, 1,
7, 5, 8, 6,
6, 8, 5, 7 };
SkSL::Compiler compiler;
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
SkSL::Program::kGeneric_Kind, SkSL::String(src), SkSL::Program::Settings());
@ -89,17 +84,38 @@ void vec_test(skiatest::Reporter* r, const char* src) {
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
// Test on four different vectors (with varying orderings to get divergent control flow)
const float input[16] = { 1, 2, 3, 4,
4, 3, 2, 1,
7, 5, 8, 6,
6, 8, 5, 7 };
float out_s[16], out_v[16];
memcpy(out_s, input, sizeof(out_s));
memcpy(out_v, input, sizeof(out_v));
// First run in scalar mode to determine the expected output
for (int i = 0; i < 4; ++i) {
SkAssertResult(byteCode->run(main, out_s + i * 4, nullptr, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, out_s + i * 4, 4, nullptr, 0, nullptr, 0));
}
// Need to transpose input vectors for striped execution
auto transpose = [](float* v) {
for (int r = 0; r < 4; ++r)
for (int c = 0; c < r; ++c)
std::swap(v[r*4 + c], v[c*4 + r]);
};
// Need to transpose input vectors for striped execution
transpose(out_v);
float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 };
// Now run in parallel and compare results
SkAssertResult(byteCode->run(main, out_v, nullptr, 4, nullptr, 0));
SkAssertResult(byteCode->runStriped(main, 4, args, 4, nullptr, 0, nullptr, 0));
// Transpose striped outputs back
transpose(out_v);
if (memcmp(out_s, out_v, sizeof(out_s)) != 0) {
printf("for program: %s\n", src);
for (int i = 0; i < 4; ++i) {
@ -131,7 +147,7 @@ void test(skiatest::Reporter* r, const char* src, float inR, float inG, float in
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
float inoutColor[4] = { inR, inG, inB, inA };
SkAssertResult(byteCode->run(main, inoutColor, nullptr, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, inoutColor, 4, nullptr, 0, nullptr, 0));
if (inoutColor[0] != expectedR || inoutColor[1] != expectedG ||
inoutColor[2] != expectedB || inoutColor[3] != expectedA) {
printf("for program: %s\n", src);
@ -261,13 +277,13 @@ DEF_TEST(SkSLInterpreterBitwise, r) {
unsigned out;
out = 0x00000088;
test(r, "int main(int x) { return x << 3; }", (float*)&in, 1, (float*)&out);
test(r, "int main(int x) { return x << 3; }", (float*)&in, (float*)&out);
out = 0xF0000002;
test(r, "int main(int x) { return x >> 3; }", (float*)&in, 1, (float*)&out);
test(r, "int main(int x) { return x >> 3; }", (float*)&in, (float*)&out);
out = 0x10000002;
test(r, "uint main(uint x) { return x >> 3; }", (float*)&in, 1, (float*)&out);
test(r, "uint main(uint x) { return x >> 3; }", (float*)&in, (float*)&out);
}
DEF_TEST(SkSLInterpreterMatrix, r) {
@ -278,18 +294,18 @@ DEF_TEST(SkSLInterpreterMatrix, r) {
in[0] = 1.0f;
expected[0] = 2.0f;
test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }",
in, 1, expected);
in, expected);
// With non-square matrix
test(r, "float main(float x) { float3x2 m = float3x2(x); return m[0][0] + m[1][1] + m[2][1]; }",
in, 1, expected);
in, expected);
// Constructing from a different-sized matrix fills the remaining space with the identity matrix
test(r, "float main(float x) {"
"float3x2 m = float3x2(x);"
"float4x4 m2 = float4x4(m);"
"return m2[0][0] + m2[3][3]; }",
in, 1, expected);
in, expected);
// Constructing a matrix from vectors or scalars fills in values in column-major order
in[0] = 1.0f;
@ -298,40 +314,40 @@ DEF_TEST(SkSLInterpreterMatrix, r) {
in[3] = 8.0f;
expected[0] = 6.0f;
test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }",
in, 1, expected);
in, expected);
expected[0] = 10.0f;
test(r, "float main(float4 v) {"
"float2x2 m = float2x2(v.x, v.y, v.w, v.z);"
"return m[0][1] + m[1][0]; }",
in, 1, expected);
in, expected);
// Initialize 16 values to be used as inputs to matrix tests
for (int i = 0; i < 16; ++i) { in[i] = (float)i; }
// M+M, M-S, S-M
for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); }
test(r, "float4x4 main(float4x4 m) { return m + m; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected);
for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); }
test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected);
test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected);
// M-M, M-S, S-M
for (int i = 0; i < 8; ++i) { expected[i] = 8.0f; }
test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", in, 8, expected);
test(r, "float4x2 main(float4x2 m1, float4x2 m2) { return m2 - m1; }", in, expected);
for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); }
test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected);
for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); }
test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected);
// M*S, S*M, M/S, S/M
for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); }
test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected);
test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected);
for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; }
test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected);
for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); }
test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, 16, expected);
test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected);
#if 0
// Matrix negation - legal in GLSL, not in SkSL?
@ -343,11 +359,11 @@ DEF_TEST(SkSLInterpreterMatrix, r) {
for (int i = 0; i < 4; ++i) {
expected[i] = 12.0f*i + 13.0f*(i+4) + 14.0f*(i+8);
}
test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, 4, expected);
test(r, "float4 main(float3x4 m, float3 v) { return m * v; }", in, expected);
for (int i = 0; i < 4; ++i) {
expected[i] = 12.0f*(3*i) + 13.0f*(3*i+1) + 14.0f*(3*i+2);
}
test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, 4, expected);
test(r, "float4 main(float4x3 m, float3 v) { return v * m; }", in, expected);
// M*M
{
@ -360,7 +376,7 @@ DEF_TEST(SkSLInterpreterMatrix, r) {
m.setConcat(m, m2);
// Rearrange the columns on the RHS so we detect left-hand/right-hand errors
test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }",
in, 16, (float*)&m);
in, (float*)&m);
}
}
@ -385,27 +401,27 @@ DEF_TEST(SkSLInterpreterCast, r) {
input[1].s = -5;
expected[0].f = 3.0f;
expected[1].f = -5.0f;
test(r, "float main(int x) { return float (x); }", (float*)input, 1, (float*)expected);
test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected);
test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected);
input[0].u = 3;
input[1].u = 5;
expected[0].f = 3.0f;
expected[1].f = 5.0f;
test(r, "float main(uint x) { return float (x); }", (float*)input, 1, (float*)expected);
test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, 2, (float*)expected);
test(r, "float main(uint x) { return float (x); }", (float*)input, (float*)expected);
test(r, "float2 main(uint2 x) { return float2(x); }", (float*)input, (float*)expected);
input[0].f = 3.0f;
input[1].f = -5.0f;
expected[0].s = 3;
expected[1].s = -5;
test(r, "int main(float x) { return int (x); }", (float*)input, 1, (float*)expected);
test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, 2, (float*)expected);
test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected);
test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected);
input[0].s = 3;
expected[0].f = 3.0f;
expected[1].f = 3.0f;
test(r, "float2 main(int x) { return float2(x); }", (float*)input, 2, (float*)expected);
test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected);
}
DEF_TEST(SkSLInterpreterIf, r) {
@ -548,10 +564,10 @@ DEF_TEST(SkSLInterpreterGlobal, r) {
DEF_TEST(SkSLInterpreterGeneric, r) {
float value1 = 5;
float expected1 = 25;
test(r, "float main(float x) { return x * x; }", &value1, 1, &expected1);
test(r, "float main(float x) { return x * x; }", &value1, &expected1);
float value2[2] = { 5, 25 };
float expected2[2] = { 25, 625 };
test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, 2, expected2);
test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2);
}
DEF_TEST(SkSLInterpreterCompound, r) {
@ -620,18 +636,19 @@ DEF_TEST(SkSLInterpreterCompound, r) {
fill_rects = byteCode->getFunction("fill_rects");
SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } };
const float* fRects = (const float*)gRects;
{
SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30);
int out = 0;
SkAssertResult(byteCode->run(rect_height, (float*)&in, (float*)&out, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(rect_height, (float*)&in, 4, (float*)&out, 1, fRects, 16));
REPORTER_ASSERT(r, out == 30);
}
{
int in[2] = { 15, 25 };
RectAndColor out;
SkAssertResult(byteCode->run(make_blue_rect, (float*)in, (float*)&out, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(make_blue_rect, (float*)in, 2, (float*)&out, 8, fRects, 16));
REPORTER_ASSERT(r, out.fRect.width() == 15);
REPORTER_ASSERT(r, out.fRect.height() == 25);
SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f };
@ -641,14 +658,14 @@ DEF_TEST(SkSLInterpreterCompound, r) {
{
int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int out = 0;
SkAssertResult(byteCode->run(median, (float*)in, (float*)&out, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(median, (float*)in, 15, (float*)&out, 1, fRects, 16));
REPORTER_ASSERT(r, out == 8);
}
{
float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
float out[8] = { 0 };
SkAssertResult(byteCode->run(sums, in, out, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(sums, in, 8, out, 8, fRects, 16));
for (int i = 0; i < 8; ++i) {
REPORTER_ASSERT(r, out[i] == static_cast<float>((i + 1) * (i + 2) / 2));
}
@ -657,7 +674,7 @@ DEF_TEST(SkSLInterpreterCompound, r) {
{
int in = 2;
SkIRect out = SkIRect::MakeEmpty();
SkAssertResult(byteCode->run(get_rect, (float*)&in, (float*)&out, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(get_rect, (float*)&in, 1, (float*)&out, 4, fRects, 16));
REPORTER_ASSERT(r, out == gRects[2]);
}
@ -665,7 +682,7 @@ DEF_TEST(SkSLInterpreterCompound, r) {
ManyRects in;
memset(&in, 0, sizeof(in));
in.fNumRects = 2;
SkAssertResult(byteCode->run(fill_rects, (float*)&in, nullptr, 1, (float*)gRects, 16));
SkAssertResult(byteCode->run(fill_rects, (float*)&in, 33, nullptr, 0, fRects, 16));
ManyRects expected;
memset(&expected, 0, sizeof(expected));
expected.fNumRects = 2;
@ -698,7 +715,8 @@ static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in
auto byteCode = compiler.toByteCode(*program);
REPORTER_ASSERT(r, byteCode);
bool result = byteCode->run(byteCode->getFunction("main"), in, nullptr, 1, nullptr, 0);
auto fun = byteCode->getFunction("main");
bool result = byteCode->run(fun, in, fun->fParameterCount, nullptr, 0, nullptr, 0);
REPORTER_ASSERT(r, !result);
}
@ -767,13 +785,13 @@ DEF_TEST(SkSLInterpreterFunctions, r) {
float out = 0.0f;
float in = 3.0f;
SkAssertResult(byteCode->run(main, &in, &out, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, &in, 1, &out, 1, nullptr, 0));
REPORTER_ASSERT(r, out = 6.0f);
SkAssertResult(byteCode->run(dot3, &in, &out, 1, nullptr, 0));
SkAssertResult(byteCode->run(dot3, &in, 1, &out, 1, nullptr, 0));
REPORTER_ASSERT(r, out = 9.0f);
SkAssertResult(byteCode->run(dot2, &in, &out, 1, nullptr, 0));
SkAssertResult(byteCode->run(dot2, &in, 1, &out, 1, nullptr, 0));
REPORTER_ASSERT(r, out = -1.0f);
}
@ -798,21 +816,21 @@ DEF_TEST(SkSLInterpreterMathFunctions, r) {
float value[4], expected[4];
value[0] = 0.0f; expected[0] = 0.0f;
test(r, "float main(float x) { return sin(x); }", value, 1, expected);
test(r, "float main(float x) { return tan(x); }", value, 1, expected);
test(r, "float main(float x) { return sin(x); }", value, expected);
test(r, "float main(float x) { return tan(x); }", value, expected);
value[0] = 0.0f; expected[0] = 1.0f;
test(r, "float main(float x) { return cos(x); }", value, 1, expected);
test(r, "float main(float x) { return cos(x); }", value, expected);
value[0] = 25.0f; expected[0] = 5.0f;
test(r, "float main(float x) { return sqrt(x); }", value, 1, expected);
test(r, "float main(float x) { return sqrt(x); }", value, expected);
value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]);
test(r, "float main(float x) { return radians(x); }", value, 1, expected);
test(r, "float main(float x) { return radians(x); }", value, expected);
value[0] = 1.0f; value[1] = -1.0f;
expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2;
test(r, "float2 main(float2 x) { return normalize(x); }", value, 2, expected);
test(r, "float2 main(float2 x) { return normalize(x); }", value, expected);
}
DEF_TEST(SkSLInterpreterVoidFunction, r) {
@ -826,15 +844,15 @@ DEF_TEST(SkSLInterpreterMix, r) {
float value, expected;
value = 0.5f; expected = 0.0f;
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
value = 0.75f; expected = 5.0f;
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
value = 2.0f; expected = 30.0f;
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, 1, &expected);
test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected);
float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f },
expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f };
test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors, 4,
test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors,
expectedVector);
}
@ -843,7 +861,7 @@ DEF_TEST(SkSLInterpreterCross, r) {
SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(args[0], args[1], args[2]),
SkPoint3::Make(args[3], args[4], args[5]));
float expected[] = { cross.fX, cross.fY, cross.fZ };
test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, 3, expected);
test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected);
}
DEF_TEST(SkSLInterpreterInverse, r) {
@ -853,7 +871,7 @@ DEF_TEST(SkSLInterpreterInverse, r) {
float args[4] = { m[0], m[3], m[1], m[4] };
SkAssertResult(m.invert(&m));
float expt[4] = { m[0], m[3], m[1], m[4] };
test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, 4, expt, false);
test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false);
}
{
SkMatrix m;
@ -861,7 +879,7 @@ DEF_TEST(SkSLInterpreterInverse, r) {
float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
SkAssertResult(m.invert(&m));
float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };
test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, 9, expt, false);
test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false);
}
{
float args[16], expt[16];
@ -871,7 +889,7 @@ DEF_TEST(SkSLInterpreterInverse, r) {
m.asColMajorf(args);
SkAssertResult(m.invert(&m));
m.asColMajorf(expt);
test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, 16, expt, false);
test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false);
}
}
@ -879,18 +897,18 @@ DEF_TEST(SkSLInterpreterDot, r) {
float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f };
float expected = args[0] * args[2] +
args[1] * args[3];
test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, 1, &expected);
test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected);
expected = args[0] * args[3] +
args[1] * args[4] +
args[2] * args[5];
test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, 1, &expected);
test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected);
expected = args[0] * args[4] +
args[1] * args[5] +
args[2] * args[6] +
args[3] * args[7];
test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, 1, &expected);
test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected);
}
static const SkSL::Type& type_of(const skjson::Value* value, SkSL::Compiler* compiler) {
@ -1010,7 +1028,7 @@ DEF_TEST(SkSLInterpreterExternalValues, r) {
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
float out;
SkAssertResult(byteCode->run(main, nullptr, &out, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
REPORTER_ASSERT(r, out == 66.0);
REPORTER_ASSERT(r, outValue == 152);
} else {
@ -1042,7 +1060,7 @@ DEF_TEST(SkSLInterpreterExternalValuesVector, r) {
return;
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
SkAssertResult(byteCode->run(main, nullptr, nullptr, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, nullptr, 0, nullptr, 0, nullptr, 0));
REPORTER_ASSERT(r, value[0] == 2);
REPORTER_ASSERT(r, value[1] == 4);
REPORTER_ASSERT(r, value[2] == 6);
@ -1108,7 +1126,7 @@ DEF_TEST(SkSLInterpreterExternalValuesCall, r) {
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
float out;
SkAssertResult(byteCode->run(main, nullptr, &out, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0));
REPORTER_ASSERT(r, out == 5.0);
} else {
printf("%s\n%s", src, compiler.errorText().c_str());
@ -1176,7 +1194,7 @@ DEF_TEST(SkSLInterpreterExternalValuesVectorCall, r) {
}
const SkSL::ByteCodeFunction* main = byteCode->getFunction("main");
float out[4];
SkAssertResult(byteCode->run(main, nullptr, out, 1, nullptr, 0));
SkAssertResult(byteCode->run(main, nullptr, 0, out, 4, nullptr, 0));
REPORTER_ASSERT(r, out[0] == 1.0);
REPORTER_ASSERT(r, out[1] == 2.0);
REPORTER_ASSERT(r, out[2] == 3.0);