[graphite] Add radial gradient shader

Bug: skia:13302
Change-Id: I6c8e8e9e6741862977a3d1b89fb58bff9cb76ee7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/537981
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: James Godfrey-Kittle <jamesgk@google.com>
This commit is contained in:
James Godfrey-Kittle 2022-05-06 15:18:15 -04:00 committed by SkCQ
parent aac8e4545a
commit eba022af78
5 changed files with 680 additions and 561 deletions

View File

@ -159,10 +159,8 @@ void add_radial_gradient_uniform_data(const SkShaderCodeDictionary* dict,
gatherer->write(gradData.fColor4fs, GradientData::kMaxStops);
gatherer->write(gradData.fOffsets, GradientData::kMaxStops);
gatherer->write(gradData.fPoints[0]);
gatherer->write(gradData.fPoints[1]); // unused
gatherer->write(gradData.fRadii[0]);
gatherer->write(gradData.fRadii[1]); // unused
gatherer->write(SkPoint::Make(0.0f, 0.0f)); // padding
gatherer->write(0.0f); // padding
gatherer->addFlags(
dict->getSnippetRequirementFlags(SkBuiltInCodeSnippetID::kRadialGradientShader));

View File

@ -46,7 +46,6 @@ namespace SolidColorShaderBlock {
} // namespace SolidColorShaderBlock
// TODO: move this functionality to the SkLinearGradient, SkRadialGradient, etc classes
namespace GradientShaderBlocks {
struct GradientData {

View File

@ -357,7 +357,18 @@ static constexpr SkUniform kGradientUniforms[kNumGradientUniforms] = {
{ "padding", SkSLType::kFloat2 } // TODO: add automatic uniform padding
};
static constexpr int kNumRadialGradientUniforms = 6;
static constexpr SkUniform kRadialGradientUniforms[kNumRadialGradientUniforms] = {
{ "localMatrix", SkSLType::kFloat4x4 },
{ "colors", SkSLType::kFloat4, kFourStopGradient },
{ "offsets", SkSLType::kFloat, kFourStopGradient },
{ "center", SkSLType::kFloat2 },
{ "radius", SkSLType::kFloat },
{ "padding", SkSLType::kFloat } // TODO: add automatic uniform padding
};
static constexpr char kLinearGradient4Name[] = "sk_linear_grad_4_shader";
static constexpr char kRadialGradient4Name[] = "sk_radial_grad_4_shader";
//--------------------------------------------------------------------------------------------------
static constexpr int kNumSolidShaderUniforms = 1;
@ -605,10 +616,10 @@ SkShaderCodeDictionary::SkShaderCodeDictionary() {
};
fBuiltInCodeSnippets[(int) SkBuiltInCodeSnippetID::kRadialGradientShader] = {
"RadialGradient4",
SkMakeSpan(kGradientUniforms, kNumGradientUniforms),
SkMakeSpan(kRadialGradientUniforms, kNumRadialGradientUniforms),
SnippetRequirementFlags::kLocalCoords,
{ }, // no samplers
kLinearGradient4Name,
kRadialGradient4Name,
GenerateDefaultGlueCode,
kNoChildren,
{ }

File diff suppressed because it is too large Load Diff

View File

@ -48,6 +48,20 @@ float2 sk_compute_coords(float4x4 dev2Local,
return coords;
}
half4 sk_clamp_grad_4(float4 colorsParam[4], float offsetsParam[4], float t) {
float4 result = colorsParam[0];
result = mix(result, colorsParam[1],
clamp((t-offsetsParam[0])/(offsetsParam[1]-offsetsParam[0]),
0, 1));
result = mix(result, colorsParam[2],
clamp((t-offsetsParam[1])/(offsetsParam[2]-offsetsParam[1]),
0, 1));
result = mix(result, colorsParam[3],
clamp((t-offsetsParam[2])/(offsetsParam[3]-offsetsParam[2]),
0, 1));
return half4(result);
}
half4 sk_linear_grad_4_shader(float4x4 dev2Local,
float4 colorsParam[4],
float offsetsParam[4],
@ -60,17 +74,19 @@ half4 sk_linear_grad_4_shader(float4x4 dev2Local,
float2 delta = point1Param - point0Param;
float2 pt = pos - point0Param;
float t = dot(pt, delta) / dot(delta, delta);
float4 result = colorsParam[0];
result = mix(result, colorsParam[1],
clamp((t-offsetsParam[0])/(offsetsParam[1]-offsetsParam[0]),
0, 1));
result = mix(result, colorsParam[2],
clamp((t-offsetsParam[1])/(offsetsParam[2]-offsetsParam[1]),
0, 1));
result = mix(result, colorsParam[3],
clamp((t-offsetsParam[2])/(offsetsParam[3]-offsetsParam[2]),
0, 1));
return half4(result);
return sk_clamp_grad_4(colorsParam, offsetsParam, t);
}
half4 sk_radial_grad_4_shader(float4x4 dev2Local,
float4 colorsParam[4],
float offsetsParam[4],
float2 centerParam,
float radiusParam,
float padding) {
float2 pos = (dev2Local * sk_FragCoord).xy;
float2 pt = pos - centerParam;
float t = length(pt) / radiusParam;
return sk_clamp_grad_4(colorsParam, offsetsParam, t);
}
half4 sk_blend(int blendMode, half4 src, half4 dst) {