Flutter Engine
The Flutter Engine
Functions
SkSL_LinearSRGB.cpp File Reference
#include "tools/fiddle/examples.h"

Go to the source code of this file.

Functions

 REG_FIDDLE (SkSL_LinearSRGB, 256, 128, false, 0)
 

Function Documentation

◆ REG_FIDDLE()

REG_FIDDLE ( SkSL_LinearSRGB  ,
256  ,
128  ,
false  ,
 
)

Definition at line 4 of file SkSL_LinearSRGB.cpp.

4 {
5void draw(SkCanvas* canvas) {
6 // Make a simple lighting effect:
8 layout(color) uniform vec3 surfaceColor;
9 uniform int doLinearLighting;
10
11 vec3 normal_at(vec2 p) {
12 p = (p / 128) * 2 - 1;
13 float len2 = dot(p, p);
14 vec3 n = (len2 > 1) ? vec3(0, 0, 1) : vec3(p, sqrt(1 - len2));
15 return normalize(n);
16 }
17
18 vec4 main(vec2 p) {
19 vec3 n = normal_at(p);
20 vec3 l = normalize(vec3(-1, -1, 0.5));
21 vec3 C = surfaceColor;
22
23 if (doLinearLighting != 0) { C = toLinearSrgb(C); }
24 C *= saturate(dot(n, l));
25 if (doLinearLighting != 0) { C = fromLinearSrgb(C); }
26
27 return C.rgb1;
28 })")).effect;
30 builder.uniform("surfaceColor") = SkV3{0.8, 0.8, 0.8};
32
33 // FIRST: Draw the lit sphere without converting to linear sRGB.
34 // This produces INCORRECT light falloff.
35 builder.uniform("doLinearLighting") = 0;
36 paint.setShader(builder.makeShader());
37 canvas->drawRect({0,0,128,128}, paint);
38
39 // SECOND: Draw the lit sphere with math done in linear sRGB.
40 // This produces sharper falloff, which is CORRECT.
41 builder.uniform("doLinearLighting") = 1;
42 paint.setShader(builder.makeShader());
43 canvas->translate(128, 0);
44 canvas->drawRect({0,0,128,128}, paint);
45}} // END FIDDLE
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
static Result MakeForShader(SkString sksl, const Options &)
const Paint & paint
Definition: color_source.cc:38
sk_sp< SkRuntimeEffect > effect
Definition: SkM44.h:56