2021-02-11 18:19:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 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/SkRuntimeEffect.h"
|
2021-07-21 19:39:51 +00:00
|
|
|
#include "src/core/SkCanvasPriv.h"
|
2021-08-04 17:27:43 +00:00
|
|
|
#include "src/gpu/SurfaceFillContext.h"
|
2021-02-11 18:19:38 +00:00
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "src/sksl/dsl/priv/DSLFPs.h"
|
2021-03-04 19:30:25 +00:00
|
|
|
#include "src/sksl/dsl/priv/DSLWriter.h"
|
|
|
|
#include "src/sksl/ir/SkSLVariable.h"
|
2021-02-11 18:19:38 +00:00
|
|
|
|
|
|
|
class SimpleDSLEffect : public GrFragmentProcessor {
|
|
|
|
public:
|
|
|
|
static constexpr GrProcessor::ClassID CLASS_ID = (GrProcessor::ClassID) 100;
|
|
|
|
|
|
|
|
SimpleDSLEffect() : GrFragmentProcessor(CLASS_ID, kNone_OptimizationFlags) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "DSLEffect"; }
|
|
|
|
void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
|
|
|
|
bool onIsEqual(const GrFragmentProcessor& that) const override { return this == &that; }
|
|
|
|
std::unique_ptr<GrFragmentProcessor> clone() const override { return nullptr; }
|
|
|
|
|
2021-02-23 15:07:05 +00:00
|
|
|
std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
|
2021-02-11 18:19:38 +00:00
|
|
|
class Impl : public GrGLSLFragmentProcessor {
|
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
using namespace SkSL::dsl;
|
|
|
|
StartFragmentProcessor(this, &args);
|
2021-03-02 20:38:19 +00:00
|
|
|
|
|
|
|
// Test for skbug.com/11384
|
2021-04-08 20:56:05 +00:00
|
|
|
Var x(kInt_Type, 1);
|
2021-03-05 19:23:48 +00:00
|
|
|
Declare(x);
|
2021-06-23 14:27:09 +00:00
|
|
|
SkASSERT(DSLWriter::Var(x)->initialValue()->description() == "1");
|
2021-03-02 20:38:19 +00:00
|
|
|
|
Broke DSLVar into separate subclasses
Previously, DSLVar represented local, global, and parameter variables.
This splits it into three separate subclasses.
In addition to just being a cleaner API in general, this also addresses
an issue we ran into with the upcoming DSLParser: previously, a global
DSLVar's storage was not set correctly until DeclareGlobal was called,
so an AddToSymbolTable call prior to DeclareGlobal would create the
SkSL variable with the wrong storage, causing spurious errors on
global-only modifiers. But holding off on the AddToSymbolTable tends to
break constructs like "int x = 0, y = x", so improving the API seemed
like the best way to address it.
Now that we have greater type safety around variables, we can
potentially avoid having to call AddToSymbolTable for DSLVar and
DSLGlobalVar altogether, since we know they are both supposed to end up
in the symbol table, but that isn't something I want to change in this
CL.
Change-Id: I5f390a7384ce0af6a2131d84f97fc5e5b318063f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/428576
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
2021-07-15 14:35:54 +00:00
|
|
|
GlobalVar blueAlpha(kUniform_Modifier, kHalf2_Type, "blueAlpha");
|
|
|
|
Declare(blueAlpha);
|
2021-02-11 20:56:27 +00:00
|
|
|
fBlueAlphaUniform = VarUniformHandle(blueAlpha);
|
2021-04-08 20:56:05 +00:00
|
|
|
Var coords(kFloat4_Type, sk_FragCoord());
|
2021-03-05 19:23:48 +00:00
|
|
|
Declare(coords);
|
2021-02-17 16:17:56 +00:00
|
|
|
Return(Half4(Swizzle(coords, X, Y) / 100, blueAlpha));
|
2021-02-11 18:19:38 +00:00
|
|
|
EndFragmentProcessor();
|
|
|
|
}
|
2021-02-11 20:56:27 +00:00
|
|
|
|
|
|
|
void onSetData(const GrGLSLProgramDataManager& pdman,
|
|
|
|
const GrFragmentProcessor& effect) override {
|
|
|
|
pdman.set2f(fBlueAlphaUniform, 0.0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
GrGLSLProgramDataManager::UniformHandle fBlueAlphaUniform;
|
2021-02-11 18:19:38 +00:00
|
|
|
};
|
2021-02-23 15:07:05 +00:00
|
|
|
return std::make_unique<Impl>();
|
2021-02-11 18:19:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-21 19:39:51 +00:00
|
|
|
DEF_SIMPLE_GPU_GM(simple_dsl_test, rContext, canvas, 100, 100) {
|
|
|
|
auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas);
|
2021-07-30 14:59:25 +00:00
|
|
|
if (!sfc) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 19:39:51 +00:00
|
|
|
|
|
|
|
sfc->fillWithFP(std::make_unique<SimpleDSLEffect>());
|
2021-02-11 18:19:38 +00:00
|
|
|
}
|