blob: be4b88515a61ef5543013231f9fab74085405eef [file] [log] [blame]
Brian Osman2e29ab52019-09-20 12:19:11 -04001/*
2 * Copyright 2019 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040010#include "include/core/SkData.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040011#include "include/core/SkPaint.h"
Brian Osmana443dc32021-02-18 20:51:20 +000012#include "include/core/SkShader.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040013#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
Brian Osmanee426f22020-01-02 11:55:24 -050015#include "include/effects/SkRuntimeEffect.h"
Brian Osman2e29ab52019-09-20 12:19:11 -040016
17static const char* RUNTIME_FUNCTIONS_SRC = R"(
John Stilese9ae96c2022-05-11 12:16:17 -040018// Source: @notargs https://twitter.com/notargs/status/1250468645030858753
19uniform half4 iResolution;
20const float iTime = 0;
Brian Osman2e29ab52019-09-20 12:19:11 -040021
John Stilese9ae96c2022-05-11 12:16:17 -040022float f(vec3 p) {
23 p.z -= iTime * 10.;
24 float a = p.z * .1;
25 p.xy *= mat2(cos(a), sin(a), -sin(a), cos(a));
26 return .1 - length(cos(p.xy) + sin(p.yz));
27}
Brian Osman2e29ab52019-09-20 12:19:11 -040028
John Stilese9ae96c2022-05-11 12:16:17 -040029half4 main(vec2 fragcoord) {
30 vec3 d = .5 - fragcoord.xy1 / iResolution.y;
31 vec3 p=vec3(0);
32 for (int i = 0; i < 32; i++) {
33 p += f(p) * d;
Brian Osman2e29ab52019-09-20 12:19:11 -040034 }
John Stilese9ae96c2022-05-11 12:16:17 -040035 return ((sin(p) + vec3(2, 5, 9)) / length(p)).xyz1;
36}
Brian Osman2e29ab52019-09-20 12:19:11 -040037)";
38
Brian Osman2e29ab52019-09-20 12:19:11 -040039class RuntimeFunctions : public skiagm::GM {
Brian Osman2e29ab52019-09-20 12:19:11 -040040 bool runAsBench() const override { return true; }
41
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000042 SkString getName() const override { return SkString("runtimefunctions"); }
Brian Osman2e29ab52019-09-20 12:19:11 -040043
Leandro Lovisolo8f023882023-08-15 21:13:52 +000044 SkISize getISize() override { return {256, 256}; }
Brian Osman2e29ab52019-09-20 12:19:11 -040045
Brian Osman2e29ab52019-09-20 12:19:11 -040046 void onDraw(SkCanvas* canvas) override {
John Stilese9ae96c2022-05-11 12:16:17 -040047 SkRuntimeEffect::Result result =
48 SkRuntimeEffect::MakeForShader(SkString(RUNTIME_FUNCTIONS_SRC));
49 SkASSERTF(result.effect, "%s", result.errorText.c_str());
Brian Osman93de1622019-12-26 08:43:05 -050050
51 SkMatrix localM;
52 localM.setRotate(90, 128, 128);
53
John Stilese9ae96c2022-05-11 12:16:17 -040054 SkV4 iResolution = { 255, 255, 0, 0 };
55 auto shader = result.effect->makeShader(
56 SkData::MakeWithCopy(&iResolution, sizeof(iResolution)), nullptr, 0, &localM);
Brian Osman2e29ab52019-09-20 12:19:11 -040057 SkPaint p;
Brian Osman93de1622019-12-26 08:43:05 -050058 p.setShader(std::move(shader));
Brian Osman2e29ab52019-09-20 12:19:11 -040059 canvas->drawRect({0, 0, 256, 256}, p);
60 }
61};
Brian Osman93de1622019-12-26 08:43:05 -050062
Brian Osman2e29ab52019-09-20 12:19:11 -040063DEF_GM(return new RuntimeFunctions;)