Add shadertoy style 'iTime' to SkSLSlide

Change-Id: I1eaffcce43921632df483e40aa365c645663093a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/343418
Auto-Submit: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2020-12-11 10:09:03 -05:00 committed by Skia Commit-Bot
parent ad0571f54a
commit 0af9e5a15c
2 changed files with 15 additions and 2 deletions

View File

@ -80,9 +80,11 @@ void SkSLSlide::unload() {
}
bool SkSLSlide::rebuild() {
auto [effect, errorText] = SkRuntimeEffect::Make(fSkSL);
SkString sksl("uniform float iTime;\n");
sksl.append(fSkSL);
auto [effect, errorText] = SkRuntimeEffect::Make(sksl);
if (!effect) {
Viewer::ShaderErrorHandler()->compileError(fSkSL.c_str(), errorText.c_str());
Viewer::ShaderErrorHandler()->compileError(sksl.c_str(), errorText.c_str());
return false;
}
@ -121,6 +123,10 @@ void SkSLSlide::draw(SkCanvas* canvas) {
}
for (const auto& v : fEffect->uniforms()) {
if (v.fName.equals("iTime")) {
*(float*)(fInputs.get() + v.fOffset) = fSeconds;
continue;
}
switch (v.fType) {
case SkRuntimeEffect::Uniform::Type::kFloat:
case SkRuntimeEffect::Uniform::Type::kFloat2:
@ -185,3 +191,8 @@ void SkSLSlide::draw(SkCanvas* canvas) {
p.setShader(std::move(shader));
canvas->drawRect({ 0, 0, 256, 256 }, p);
}
bool SkSLSlide::animate(double nanos) {
fSeconds = static_cast<float>(nanos * 1E-9);
return true;
}

View File

@ -20,6 +20,7 @@ public:
SkISize getDimensions() const override { return SkISize::MakeEmpty(); }
void draw(SkCanvas* canvas) override;
bool animate(double nanos) override;
void load(SkScalar winWidth, SkScalar winHeight) override;
void unload() override;
@ -32,6 +33,7 @@ private:
sk_sp<SkRuntimeEffect> fEffect;
SkAutoTMalloc<char> fInputs;
SkTArray<sk_sp<SkShader>> fChildren;
float fSeconds = 0.0f;
// Named shaders that can be selected as inputs
SkTArray<std::pair<const char*, sk_sp<SkShader>>> fShaders;