| 1 |
// Copyright 2024 Google LLC. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 |
#include "tools/fiddle/examples.h" |
| 4 |
REG_FIDDLE(SkSL_Uniforms, 128, 64, false, 0) { |
| 5 |
void draw(SkCanvas* canvas) { |
| 6 |
// Make a very wide-gamut offscreen surface: |
| 7 |
auto rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, |
| 8 |
SkNamedGamut::kRec2020); |
| 9 |
auto info = SkImageInfo::Make(128, 64, kRGBA_F16_SkColorType, |
| 10 |
kPremul_SkAlphaType, rec2020); |
| 11 |
auto surface = SkSurfaces::Raster(info); |
| 12 |
|
| 13 |
// Effect draws horizontal gradients. Top half uses the simple uniform, |
| 14 |
// bottom half uses a uniform tagged as a color: |
| 15 |
const char* sksl = R"( |
| 16 |
uniform vec4 not_a_color; |
| 17 |
layout(color) uniform vec4 color; |
| 18 |
|
| 19 |
vec4 main(vec2 xy) { |
| 20 |
vec4 c = xy.y < 32 ? not_a_color : color; |
| 21 |
return (c * (xy.x / 128)).rgb1; |
| 22 |
})"; |
| 23 |
|
| 24 |
auto [effect, err] = SkRuntimeEffect::MakeForShader(SkString(sksl)); |
| 25 |
|
| 26 |
// Set both uniforms to be "red": |
| 27 |
SkRuntimeShaderBuilder builder(effect); |
| 28 |
builder.uniform("not_a_color") = SkV4{ 1, 0, 0, 1 }; // Red? |
| 29 |
builder.uniform("color") = SkV4{ 1, 0, 0, 1 }; // sRGB Red |
| 30 |
|
| 31 |
// Fill the offscreen surface: |
| 32 |
SkPaint paint; |
| 33 |
paint.setShader(builder.makeShader()); |
| 34 |
surface->getCanvas()->drawPaint(paint); |
| 35 |
|
| 36 |
// Draw our offscreen image back to the original canvas: |
| 37 |
canvas->drawImage(surface->makeImageSnapshot(), 0, 0); |
| 38 |
} |
| 39 |
} // END FIDDLE |