Flutter Engine
The Flutter Engine
SkSL_Uniforms.cpp
Go to the documentation of this file.
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.
4REG_FIDDLE(SkSL_Uniforms, 128, 64, false, 0) {
5void draw(SkCanvas* canvas) {
6 // Make a very wide-gamut offscreen surface:
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":
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:
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}} // END FIDDLE
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static sk_sp< SkColorSpace > rec2020()
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
REG_FIDDLE(SkSL_Uniforms, 128, 64, false, 0)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
static Result MakeForShader(SkString sksl, const Options &)
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
static constexpr skcms_Matrix3x3 kRec2020
Definition: SkColorSpace.h:93
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:51
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
Definition: SkM44.h:98