Enable local coordinates in runtime effects.

At present, we calculate the coordinates, but we don't yet use them
anywhere, because the runtime effect code isn't being included in the
finished program.

Change-Id: I6eb6b1b43ce78da31884b7a8b1bac2aa9ba5407b
Bug: skia:13405
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/549101
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
This commit is contained in:
John Stiles 2022-06-13 17:44:10 -04:00 committed by SkCQ
parent f4087cb7c4
commit b0912790c6
6 changed files with 1201 additions and 1176 deletions

View File

@ -570,8 +570,10 @@ RuntimeShaderBlock::ShaderData::ShaderData(sk_sp<const SkRuntimeEffect> effect)
: fEffect(std::move(effect)) {}
RuntimeShaderBlock::ShaderData::ShaderData(sk_sp<const SkRuntimeEffect> effect,
const SkMatrix& localMatrix,
sk_sp<const SkData> uniforms)
: fEffect(std::move(effect))
, fLocalMatrix(localMatrix)
, fUniforms(std::move(uniforms)) {}
static bool skdata_matches(const SkData* a, const SkData* b) {
@ -582,6 +584,7 @@ static bool skdata_matches(const SkData* a, const SkData* b) {
bool RuntimeShaderBlock::ShaderData::operator==(const ShaderData& rhs) const {
return fEffect == rhs.fEffect &&
fLocalMatrix == rhs.fLocalMatrix &&
skdata_matches(fUniforms.get(), rhs.fUniforms.get());
}
@ -600,6 +603,13 @@ void RuntimeShaderBlock::BeginBlock(const SkKeyContext& keyContext,
[[maybe_unused]] const SkShaderCodeDictionary* dict = keyContext.dict();
VALIDATE_UNIFORMS(gatherer, dict, kCodeSnippetID)
gatherer->addFlags(dict->getSnippetRequirementFlags(kCodeSnippetID));
// Pass the local matrix inverse so we can use local coordinates.
SkMatrix inverseLocalMatrix;
if (!shaderData.fLocalMatrix.invert(&inverseLocalMatrix)) {
inverseLocalMatrix.setIdentity();
}
gatherer->write(SkM44(inverseLocalMatrix));
}
// Use the combination of {SkSL program hash, uniform size} as our key.
// In the unfortunate event of a hash collision, at least we'll have the right amount of
@ -717,7 +727,7 @@ SkUniquePaintParamsID CreateKey(const SkKeyContext& keyContext,
{
sk_sp<SkRuntimeEffect> effect = TestingOnly_GetCommonRuntimeEffect();
RuntimeShaderBlock::BeginBlock(keyContext, builder, nullptr,
{effect, /*uniforms=*/nullptr});
{effect, SkMatrix::I(), /*uniforms=*/nullptr});
builder->endBlock();
}
break;

View File

@ -176,13 +176,16 @@ struct RuntimeShaderBlock {
ShaderData(sk_sp<const SkRuntimeEffect> effect);
// This ctor is used when extracting information from PaintParams.
ShaderData(sk_sp<const SkRuntimeEffect> effect, sk_sp<const SkData> uniforms);
ShaderData(sk_sp<const SkRuntimeEffect> effect,
const SkMatrix& localMatrix,
sk_sp<const SkData> uniforms);
bool operator==(const ShaderData& rhs) const;
bool operator!=(const ShaderData& rhs) const { return !(*this == rhs); }
// Runtime shader data.
sk_sp<const SkRuntimeEffect> fEffect;
SkMatrix fLocalMatrix;
sk_sp<const SkData> fUniforms;
};

View File

@ -1156,7 +1156,8 @@ public:
void addToKey(const SkKeyContext& keyContext,
SkPaintParamsKeyBuilder* builder,
SkPipelineDataGatherer* gatherer) const override {
RuntimeShaderBlock::BeginBlock(keyContext, builder, gatherer, {fEffect, fUniforms});
RuntimeShaderBlock::BeginBlock(keyContext, builder, gatherer,
{fEffect, this->getLocalMatrix(), fUniforms});
builder->endBlock();
}

View File

@ -542,6 +542,10 @@ static constexpr char kBlendShaderName[] = "sk_blend_shader";
//--------------------------------------------------------------------------------------------------
static constexpr char kRuntimeShaderName[] = "sk_runtime_placeholder";
static constexpr SkUniform kRuntimeShaderUniforms[] = {
{"localMatrix", SkSLType::kFloat4x4},
};
static constexpr DataPayloadField kRuntimeShaderDataPayload[] = {
{"runtime effect hash", DataPayloadType::kByte, 4},
{"uniform data size (bytes)", DataPayloadType::kByte, 4},
@ -824,8 +828,8 @@ SkShaderCodeDictionary::SkShaderCodeDictionary() {
};
fBuiltInCodeSnippets[(int) SkBuiltInCodeSnippetID::kRuntimeShader] = {
"RuntimeShader",
{ }, // no uniforms
SnippetRequirementFlags::kNone,
SkMakeSpan(kRuntimeShaderUniforms),
SnippetRequirementFlags::kLocalCoords,
{ }, // no samplers
kRuntimeShaderName,
GenerateDefaultGlueCode,

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,8 @@ half4 sk_error() {
return half4(1.0, 0.0, 1.0, 1.0);
}
half4 sk_runtime_placeholder() {
// TODO(skia:13405): remove me when RuntimeShader is implemented
half4 sk_runtime_placeholder(float4x4 dev2Local) {
float2 pos = (dev2Local * sk_FragCoord).xy;
return half4(0.75, 0.0, 1.0, 1.0);
}