skia2/gm/sample_matrix_variable.cpp

106 lines
4.3 KiB
C++
Raw Normal View History

/*
* Copyright 2019 Google LLC.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/effects/SkGradientShader.h"
#include "src/core/SkMatrixProvider.h"
#include "src/gpu/GrBitmapTextureMaker.h"
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrRenderTargetContextPriv.h"
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
#include "src/gpu/ops/GrFillRectOp.h"
#include "tools/Resources.h"
class SampleMatrixVariableEffect : public GrFragmentProcessor {
public:
static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 2;
SampleMatrixVariableEffect(std::unique_ptr<GrFragmentProcessor> child,
float xOffset,
float yOffset)
: INHERITED(CLASS_ID, kNone_OptimizationFlags)
, fXOffset(xOffset)
, fYOffset(yOffset) {
this->registerChild(std::move(child), SkSL::SampleMatrix::MakeVariable());
}
const char* name() const override { return "SampleMatrixVariableEffect"; }
std::unique_ptr<GrFragmentProcessor> clone() const override {
SkASSERT(false);
return nullptr;
}
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
private:
GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
float fXOffset;
float fYOffset;
typedef GrFragmentProcessor INHERITED;
friend class GLSLSampleMatrixVariableEffect;
};
class GLSLSampleMatrixVariableEffect : public GrGLSLFragmentProcessor {
void emitCode(EmitArgs& args) override {
auto& smve = args.fFp.cast<SampleMatrixVariableEffect>();
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
SkString sample1 = this->invokeChildWithMatrix(0, args, "float3x3(1)");
SkString sample2 = this->invokeChildWithMatrix(0, args,
SkStringPrintf("float3x3(1, -1, 0, 1, 0, 0, "
"%g, %g, 1)",
smve.fXOffset,
smve.fYOffset).c_str());
fragBuilder->codeAppendf("%s = (%s + %s) / 2;\n", args.fOutputColor, sample1.c_str(),
sample2.c_str());
}
};
GrGLSLFragmentProcessor* SampleMatrixVariableEffect::onCreateGLSLInstance() const {
return new GLSLSampleMatrixVariableEffect();
}
DEF_SIMPLE_GPU_GM(sample_matrix_variable, ctx, rtCtx, canvas, 512, 256) {
auto draw = [rtCtx](std::unique_ptr<GrFragmentProcessor> baseFP, float ofsX, float ofsY,
int tx, int ty) {
auto fp = std::unique_ptr<GrFragmentProcessor>(
new SampleMatrixVariableEffect(std::move(baseFP), ofsX, ofsY));
GrPaint paint;
paint.addColorFragmentProcessor(std::move(fp));
rtCtx->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::Translate(tx, ty),
SkRect::MakeIWH(256, 256));
};
{
SkBitmap bmp;
GetResourceAsBitmap("images/mandrill_256.png", &bmp);
GrBitmapTextureMaker maker(ctx, bmp, GrImageTexGenPolicy::kDraw);
auto view = maker.view(GrMipMapped::kNo);
std::unique_ptr<GrFragmentProcessor> imgFP =
GrTextureEffect::Make(std::move(view), bmp.alphaType(), SkMatrix());
draw(std::move(imgFP), -128, 256, 0, 0);
}
{
static constexpr SkColor colors[] = { 0xff00ff00, 0xffff00ff };
const SkPoint pts[] = {{ 256, 0 }, { 512, 0 }};
auto shader = SkGradientShader::MakeLinear(pts, colors, nullptr,
SK_ARRAY_COUNT(colors),
SkTileMode::kRepeat);
SkMatrix matrix;
SkSimpleMatrixProvider matrixProvider(matrix);
GrColorInfo colorInfo;
GrFPArgs args(ctx, matrixProvider, kHigh_SkFilterQuality, &colorInfo);
std::unique_ptr<GrFragmentProcessor> gradientFP = as_SB(shader)->asFragmentProcessor(args);
Update how sample(matrix) calls are invoked in SkSL This removes the kMixed type of SkSL::SampleMatrix. All analysis of FP sampling due to parent-child relationships is tracked in flags on GrFragmentProcessor now. The sample strategy is tracked as follows: - An FP marks itself as using the local coordinate builtin directly (automatically done for .fp code based on reference to sk_TransformedCoords2D[0]). - This state propagates up the parent towards the root, marking FPs as using coordinates indirectly. We stop the propagation when we hit a parent FP that explicitly samples the child because it becomes the source of the child's coordinates. - If that parent references its local coordinates directly, that kicks off its own upwards propagation. - Being sampled explicitly propagates down to all children, and effectively disables vertex-shader evaluation of transforms. - A variable matrix automatically marks this flag as well, since it's essentially a shortcut to (matrix expression) * coords. - The matrix type also propagates down, but right now that's only for whether or not there's perspective. - This doesn't affect FS coord evaluation since each FP applies its action independently. - But for VS-promoted transforms, the child's varying may inherit perspective (or other more general matrix types) from the parent and switch from a float2 to a float3. - A SampleMatrix no longer tracks a base or owner, GrFragmentProcessor exposes its parent FP. An FP's sample matrix is always owned by its immediate parent. - This means that you can have a hierarchy from root to leaf like: [uniform, none, none, uses local coords], and that leaf will have a SampleMatrix of kNone type. However, because of parent tracking, the coordinate generation can walk up to the root and detect the proper transform expression it needs to produce, and automatically de-duplicate across children. Currently, all FP's that are explicitly sampled have a signature of (color, float2 coord). FP's that don't use local coords, or whose coords are promoted to a varying have a signature of (color). - In this case, the shader builder either updates args.fLocalCoords to point to the varying directly, or adds a float2 local to the function body that includes the perspective divide. GrFragmentProcessor automatically pretends it has an identity coord transform if the FP is marked as referencing the local coord builtin. This allows these FPs to still be processed as part of GrGLSLGeometryProcessor::collectTransforms, but removes the need for FP implementations to declare an identity GrCoordTransform. - To test this theory, GrTextureEffect and GrSkSLFP no longer have coord transforms explicitly. - Later CLs can trivially remove them from a lot of the other effects. - The coord generation should not change because it detects in both cases that the coord transform matrices were identity. GrGLSLGeometryProcessor's collectTransforms and emitTransformCode has been completely overhauled to recurse up an FP's parent pointers and collect the expressions that affect the result. It de-duplicates expressions between siblings, and is able to produce a single varying for the base local coord (either when there are no intervening transforms, or the root FP needs an explicit coordinate to start off with). This also adds the fp_sample_chaining GM from Brian, with a few more configurations to fill out the cells. Bug: skia:10396 Change-Id: I86acc0c34c9f29d6371b34370bee9a18c2acf1c1 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/297868 Commit-Queue: Michael Ludwig <michaelludwig@google.com> Reviewed-by: Brian Salomon <bsalomon@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
2020-06-24 13:04:56 +00:00
draw(std::move(gradientFP), -128, 256, 256, 0);
}
}