Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 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" |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 10 | #include "include/core/SkData.h" |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 11 | #include "include/core/SkPaint.h" |
Brian Osman | a443dc3 | 2021-02-18 20:51:20 +0000 | [diff] [blame] | 12 | #include "include/core/SkShader.h" |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 13 | #include "include/core/SkSize.h" |
| 14 | #include "include/core/SkString.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 15 | #include "include/effects/SkRuntimeEffect.h" |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 16 | |
| 17 | static const char* RUNTIME_FUNCTIONS_SRC = R"( |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 18 | // Source: @notargs https://twitter.com/notargs/status/1250468645030858753 |
| 19 | uniform half4 iResolution; |
| 20 | const float iTime = 0; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 21 | |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 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 | } |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 28 | |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 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; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 34 | } |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 35 | return ((sin(p) + vec3(2, 5, 9)) / length(p)).xyz1; |
| 36 | } |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 37 | )"; |
| 38 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 39 | class RuntimeFunctions : public skiagm::GM { |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 40 | bool runAsBench() const override { return true; } |
| 41 | |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 42 | SkString getName() const override { return SkString("runtimefunctions"); } |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 43 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 44 | SkISize getISize() override { return {256, 256}; } |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 45 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 46 | void onDraw(SkCanvas* canvas) override { |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 47 | SkRuntimeEffect::Result result = |
| 48 | SkRuntimeEffect::MakeForShader(SkString(RUNTIME_FUNCTIONS_SRC)); |
| 49 | SkASSERTF(result.effect, "%s", result.errorText.c_str()); |
Brian Osman | 93de162 | 2019-12-26 08:43:05 -0500 | [diff] [blame] | 50 | |
| 51 | SkMatrix localM; |
| 52 | localM.setRotate(90, 128, 128); |
| 53 | |
John Stiles | e9ae96c | 2022-05-11 12:16:17 -0400 | [diff] [blame] | 54 | SkV4 iResolution = { 255, 255, 0, 0 }; |
| 55 | auto shader = result.effect->makeShader( |
| 56 | SkData::MakeWithCopy(&iResolution, sizeof(iResolution)), nullptr, 0, &localM); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 57 | SkPaint p; |
Brian Osman | 93de162 | 2019-12-26 08:43:05 -0500 | [diff] [blame] | 58 | p.setShader(std::move(shader)); |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 59 | canvas->drawRect({0, 0, 256, 256}, p); |
| 60 | } |
| 61 | }; |
Brian Osman | 93de162 | 2019-12-26 08:43:05 -0500 | [diff] [blame] | 62 | |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 63 | DEF_GM(return new RuntimeFunctions;) |