1 /* 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" 10 #include "include/core/SkData.h" 11 #include "include/core/SkPaint.h" 12 #include "include/core/SkShader.h" 13 #include "include/core/SkSize.h" 14 #include "include/core/SkString.h" 15 #include "include/effects/SkRuntimeEffect.h" 16 17 static const char* RUNTIME_FUNCTIONS_SRC = R"( 18 // Source: @notargs https://twitter.com/notargs/status/1250468645030858753 19 uniform half4 iResolution; 20 const float iTime = 0; 21 22 float 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 } 28 29 half4 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; 34 } 35 return ((sin(p) + vec3(2, 5, 9)) / length(p)).xyz1; 36 } 37 )"; 38 39 class RuntimeFunctions : public skiagm::GM { runAsBench() const40 bool runAsBench() const override { return true; } 41 getName() const42 SkString getName() const override { return SkString("runtimefunctions"); } 43 getISize()44 SkISize getISize() override { return {256, 256}; } 45 onDraw(SkCanvas * canvas)46 void onDraw(SkCanvas* canvas) override { 47 SkRuntimeEffect::Result result = 48 SkRuntimeEffect::MakeForShader(SkString(RUNTIME_FUNCTIONS_SRC)); 49 SkASSERTF(result.effect, "%s", result.errorText.c_str()); 50 51 SkMatrix localM; 52 localM.setRotate(90, 128, 128); 53 54 SkV4 iResolution = { 255, 255, 0, 0 }; 55 auto shader = result.effect->makeShader( 56 SkData::MakeWithCopy(&iResolution, sizeof(iResolution)), nullptr, 0, &localM); 57 SkPaint p; 58 p.setShader(std::move(shader)); 59 canvas->drawRect({0, 0, 256, 256}, p); 60 } 61 }; 62 63 DEF_GM(return new RuntimeFunctions;) 64