Add setInputs to SkSL::Interpreter

Allows updating the global inputs of a cached Interpreter object.

Change-Id: I626e63294a441a7f52ae484755c99f5d04bb614f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/212980
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2019-05-09 13:13:25 -04:00 committed by Skia Commit-Bot
parent 4f6eb15178
commit d369a5e9cf
3 changed files with 48 additions and 0 deletions

View File

@ -36,6 +36,10 @@ Interpreter::Interpreter(std::unique_ptr<Program> program, std::unique_ptr<ByteC
for (int i = 0; i < fByteCode->fGlobalCount; ++i) {
fGlobals.push_back(Value((int) UNINITIALIZED));
}
this->setInputs(inputs);
}
void Interpreter::setInputs(Interpreter::Value inputs[]) {
for (int i = fByteCode->fInputSlots.size() - 1; i >= 0; --i) {
fGlobals[fByteCode->fInputSlots[i]] = inputs[i];
}

View File

@ -64,6 +64,11 @@ public:
*/
void run(const ByteCodeFunction& f, Value args[], Value* outReturn);
/**
* Updates the global inputs.
*/
void setInputs(Value inputs[]);
private:
StackIndex stackAlloc(int count);

View File

@ -254,3 +254,42 @@ DEF_TEST(SkSLInterpreterGeneric, r) {
(SkSL::Interpreter::Value*) &value2, 2,
(SkSL::Interpreter::Value*) expected2);
}
DEF_TEST(SkSLInterpreterSetInputs, r) {
const char* src = R"(
layout(ctype=float) in uniform float x;
float main(float y) { return x + y; }
)";
SkSL::Compiler compiler;
SkSL::Program::Settings settings;
std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
SkSL::Program::kGeneric_Kind,
SkSL::String(src), settings);
REPORTER_ASSERT(r, program);
std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program);
REPORTER_ASSERT(r, !compiler.errorCount());
SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get();
float x = 1.0f;
SkSL::Interpreter interpreter(std::move(program), std::move(byteCode),
(SkSL::Interpreter::Value*)&x);
float out = 0.0f;
float in = 2.0f;
interpreter.run(*main, (SkSL::Interpreter::Value*)&in, (SkSL::Interpreter::Value*)&out);
REPORTER_ASSERT(r, out == 3.0f);
// External updates should be ignored
x = 3.0f;
out = 0.0f;
interpreter.run(*main, (SkSL::Interpreter::Value*)&in, (SkSL::Interpreter::Value*)&out);
REPORTER_ASSERT(r, out == 3.0f);
// Updating inputs should affect subsequent calls to run
out = 0.0f;
interpreter.setInputs((SkSL::Interpreter::Value*)&x);
interpreter.run(*main, (SkSL::Interpreter::Value*)&in, (SkSL::Interpreter::Value*)&out);
REPORTER_ASSERT(r, out == 5.0f);
}