Remove unnecessary casting from SkSL in GMs
Converts examples to use more idiomatic "user" SkSL, without extra casts, and taking advantage of swizzle. Change-Id: I4ad4e7b6563b4f09402855cb125546b015622ced Reviewed-on: https://skia-review.googlesource.com/c/skia/+/317388 Reviewed-by: Mike Klein <mtklein@google.com> Reviewed-by: Michael Ludwig <michaelludwig@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
db4a9fbc65
commit
b25e6e15c6
@ -17,12 +17,12 @@ static const char* RUNTIME_FUNCTIONS_SRC = R"(
|
||||
uniform half4 gColor;
|
||||
|
||||
half scale(float x) {
|
||||
return half(x) / 255;
|
||||
return x / 255;
|
||||
}
|
||||
|
||||
half4 blackAndWhite(half4 raw) {
|
||||
half value = raw.r * 0.22 + raw.g * 0.67 + raw.b * 0.11;
|
||||
return half4(half3(value), raw.a);
|
||||
return half4(value.xxx, raw.a);
|
||||
}
|
||||
|
||||
half4 main(float2 p) {
|
||||
|
@ -61,7 +61,7 @@ public:
|
||||
uniform half4 gColor;
|
||||
|
||||
half4 main(float2 p) {
|
||||
return half4(half2(p)*(1.0/255), gColor.b, 1);
|
||||
return half4(p*(1.0/255), gColor.b, 1);
|
||||
}
|
||||
)", kBench_RTFlag) {}
|
||||
|
||||
@ -137,7 +137,7 @@ public:
|
||||
half4 after = sample(after_map);
|
||||
|
||||
float m = smooth_cutoff(sample(threshold_map).a);
|
||||
return mix(before, after, half(m));
|
||||
return mix(before, after, m);
|
||||
}
|
||||
)", kAnimate_RTFlag | kBench_RTFlag) {}
|
||||
|
||||
@ -196,8 +196,7 @@ public:
|
||||
float t = (angle + 3.1415926/2) / (3.1415926);
|
||||
t += radius * rad_scale;
|
||||
t = fract(t);
|
||||
float4 m = in_colors0 * (1-t) + in_colors1 * t;
|
||||
return half4(m);
|
||||
return in_colors0 * (1-t) + in_colors1 * t;
|
||||
}
|
||||
)", kAnimate_RTFlag | kBench_RTFlag) {}
|
||||
|
||||
@ -228,7 +227,7 @@ public:
|
||||
uniform float inv_size;
|
||||
|
||||
half4 main(float2 xy) {
|
||||
float4 c = float4(unpremul(sample(input)));
|
||||
float4 c = unpremul(sample(input));
|
||||
|
||||
// Map to cube coords:
|
||||
float3 cubeCoords = float3(c.rg * rg_scale + rg_bias, c.b * b_scale);
|
||||
@ -239,7 +238,7 @@ public:
|
||||
|
||||
// Two bilinear fetches, plus a manual lerp for the third axis:
|
||||
half4 color = mix(sample(color_cube, coords1), sample(color_cube, coords2),
|
||||
half(fract(cubeCoords.b)));
|
||||
fract(cubeCoords.b));
|
||||
|
||||
// Premul again
|
||||
color.rgb *= color.a;
|
||||
|
@ -302,7 +302,7 @@ DEF_SIMPLE_GM(vertices_data, canvas, 512, 256) {
|
||||
const char* gProg = R"(
|
||||
varying float4 vtx_color;
|
||||
half4 main(float2 p) {
|
||||
return half4(vtx_color);
|
||||
return vtx_color;
|
||||
}
|
||||
)";
|
||||
auto[effect, errorText] = SkRuntimeEffect::Make(SkString(gProg));
|
||||
@ -383,7 +383,7 @@ DEF_SIMPLE_GM(vertices_data_lerp, canvas, 256, 256) {
|
||||
half4 main(float2 p) {
|
||||
half4 col0 = sample(c0, p);
|
||||
half4 col1 = sample(c1, p);
|
||||
return mix(col0, col1, half(vtx_lerp));
|
||||
return mix(col0, col1, vtx_lerp);
|
||||
}
|
||||
)";
|
||||
auto [effect, errorText] = SkRuntimeEffect::Make(SkString(gProg));
|
||||
@ -584,7 +584,7 @@ DEF_SIMPLE_GM(vertices_custom_matrices, canvas, 400, 400) {
|
||||
const char* vectorProg = R"(
|
||||
varying float3 vtx_vec;
|
||||
half4 main(float2 p) {
|
||||
return (half3(vtx_vec) * 0.5 + 0.5).rgb1;
|
||||
return (vtx_vec * 0.5 + 0.5).rgb1;
|
||||
})";
|
||||
|
||||
// raw, local vectors, normals, and positions should all look the same (no real transform)
|
||||
@ -614,7 +614,7 @@ DEF_SIMPLE_GM(vertices_custom_matrices, canvas, 400, 400) {
|
||||
const char* ctmPositionProg250 = R"(
|
||||
varying float3 vtx_pos;
|
||||
half4 main(float2 p) {
|
||||
return ((half3(vtx_pos) - half3(250, 350, 0)) / 50 + 0.5).rgb1;
|
||||
return ((vtx_pos - float3(250, 350, 0)) / 50 + 0.5).rgb1;
|
||||
}
|
||||
)";
|
||||
draw(250, 350, make_cone(Attr::Usage::kPosition, nullptr), ctmPositionProg250, 0.5f);
|
||||
@ -622,7 +622,7 @@ DEF_SIMPLE_GM(vertices_custom_matrices, canvas, 400, 400) {
|
||||
const char* ctmPositionProg350 = R"(
|
||||
varying float3 vtx_pos;
|
||||
half4 main(float2 p) {
|
||||
return ((half3(vtx_pos) - half3(350, 350, 0)) / 50 + 0.5).rgb1;
|
||||
return ((vtx_pos - float3(350, 350, 0)) / 50 + 0.5).rgb1;
|
||||
}
|
||||
)";
|
||||
canvas->saveLayer({ 300, 300, 400, 400 }, nullptr);
|
||||
|
@ -383,16 +383,16 @@ public:
|
||||
|
||||
half4 main(float2 p) {
|
||||
float3 norm = convert_normal_sample(sample(normal_map, p));
|
||||
float3 plane_norm = normalize(localToWorldAdjInv * float4(norm, 0)).xyz;
|
||||
float3 plane_norm = normalize(localToWorldAdjInv * norm.xyz0).xyz;
|
||||
|
||||
float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz;
|
||||
float3 plane_pos = (localToWorld * p.xy01).xyz;
|
||||
float3 light_dir = normalize(lightPos - plane_pos);
|
||||
|
||||
float ambient = 0.2;
|
||||
float dp = dot(plane_norm, light_dir);
|
||||
float scale = min(ambient + max(dp, 0), 1);
|
||||
|
||||
return sample(color_map, p) * half4(float4(scale, scale, scale, 1));
|
||||
return sample(color_map, p) * scale.xxx1;
|
||||
}
|
||||
)";
|
||||
auto [effect, error] = SkRuntimeEffect::Make(SkString(code));
|
||||
@ -468,16 +468,16 @@ public:
|
||||
|
||||
half4 main(float2 p) {
|
||||
float3 norm = normalize(vtx_normal);
|
||||
float3 plane_norm = normalize(localToWorldAdjInv * float4(norm, 0)).xyz;
|
||||
float3 plane_norm = normalize(localToWorldAdjInv * norm.xyz0).xyz;
|
||||
|
||||
float3 plane_pos = (localToWorld * float4(p, 0, 1)).xyz;
|
||||
float3 plane_pos = (localToWorld * p.xy01).xyz;
|
||||
float3 light_dir = normalize(lightPos - plane_pos);
|
||||
|
||||
float ambient = 0.2;
|
||||
float dp = dot(plane_norm, light_dir);
|
||||
float scale = min(ambient + max(dp, 0), 1);
|
||||
|
||||
return half4(0.7, 0.9, 0.3, 1) * half4(float4(scale, scale, scale, 1));
|
||||
return half4(0.7, 0.9, 0.3, 1) * scale.xxx1;
|
||||
}
|
||||
)";
|
||||
auto [effect, error] = SkRuntimeEffect::Make(SkString(code));
|
||||
|
Loading…
Reference in New Issue
Block a user