2019-11-22 21:56:36 +00:00
|
|
|
/*
|
|
|
|
* 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 "tools/gpu/TestOps.h"
|
|
|
|
|
|
|
|
#include "src/core/SkPointPriv.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
|
|
|
#include "src/gpu/GrGeometryProcessor.h"
|
|
|
|
#include "src/gpu/GrMemoryPool.h"
|
|
|
|
#include "src/gpu/GrOpFlushState.h"
|
2020-03-18 12:13:53 +00:00
|
|
|
#include "src/gpu/GrProgramInfo.h"
|
2019-11-22 21:56:36 +00:00
|
|
|
#include "src/gpu/GrVertexWriter.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLVarying.h"
|
|
|
|
#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
|
2019-12-05 21:40:31 +00:00
|
|
|
#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
|
2019-11-22 21:56:36 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class GP : public GrGeometryProcessor {
|
|
|
|
public:
|
|
|
|
GP(const SkMatrix& localMatrix, bool wideColor)
|
|
|
|
: GrGeometryProcessor(kTestRectOp_ClassID), fLocalMatrix(localMatrix) {
|
|
|
|
fInColor = MakeColorAttribute("color", wideColor);
|
|
|
|
this->setVertexAttributes(&fInPosition, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name() const override { return "TestRectOp::GP"; }
|
|
|
|
|
2021-08-09 19:56:24 +00:00
|
|
|
std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
|
2021-08-11 13:59:56 +00:00
|
|
|
class Impl : public ProgramImpl {
|
|
|
|
public:
|
|
|
|
void setData(const GrGLSLProgramDataManager& pdman,
|
|
|
|
const GrShaderCaps& shaderCaps,
|
|
|
|
const GrGeometryProcessor& geomProc) override {
|
|
|
|
const auto& gp = geomProc.cast<GP>();
|
|
|
|
SetTransform(pdman, shaderCaps, fLocalMatrixUni, gp.fLocalMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
|
|
|
|
const auto& gp = args.fGeomProc.cast<GP>();
|
|
|
|
args.fVaryingHandler->emitAttributes(gp);
|
|
|
|
GrGLSLVarying colorVarying(kHalf4_GrSLType);
|
|
|
|
args.fVaryingHandler->addVarying("color", &colorVarying,
|
|
|
|
GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
|
|
|
|
args.fVertBuilder->codeAppendf("%s = %s;", colorVarying.vsOut(), gp.fInColor.name());
|
|
|
|
args.fFragBuilder->codeAppendf("half4 %s = %s;",
|
|
|
|
args.fOutputColor, colorVarying.fsIn());
|
|
|
|
args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
|
|
|
|
WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name());
|
|
|
|
WriteLocalCoord(args.fVertBuilder,
|
|
|
|
args.fUniformHandler,
|
|
|
|
*args.fShaderCaps,
|
|
|
|
gpArgs,
|
|
|
|
gp.fInLocalCoords.asShaderVar(),
|
|
|
|
gp.fLocalMatrix,
|
|
|
|
&fLocalMatrixUni);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniformHandle fLocalMatrixUni;
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::make_unique<Impl>();
|
2020-06-19 14:47:30 +00:00
|
|
|
}
|
2019-11-22 21:56:36 +00:00
|
|
|
|
2021-08-06 19:33:58 +00:00
|
|
|
void addToKey(const GrShaderCaps& shaderCaps, GrProcessorKeyBuilder* b) const override {
|
2021-08-11 13:59:56 +00:00
|
|
|
b->add32(ProgramImpl::ComputeMatrixKey(shaderCaps, fLocalMatrix));
|
2020-06-19 14:47:30 +00:00
|
|
|
}
|
2019-11-22 21:56:36 +00:00
|
|
|
|
|
|
|
bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; }
|
|
|
|
|
|
|
|
private:
|
2021-08-11 13:59:56 +00:00
|
|
|
Attribute fInPosition = { "inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
|
2020-06-19 14:47:30 +00:00
|
|
|
Attribute fInLocalCoords = {"inLocalCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
|
|
|
|
Attribute fInColor;
|
2021-08-11 13:59:56 +00:00
|
|
|
|
2020-06-19 14:47:30 +00:00
|
|
|
SkMatrix fLocalMatrix;
|
|
|
|
};
|
2019-11-22 21:56:36 +00:00
|
|
|
|
|
|
|
class TestRectOp final : public GrMeshDrawOp {
|
|
|
|
public:
|
2020-10-07 20:46:15 +00:00
|
|
|
static GrOp::Owner Make(GrRecordingContext*,
|
|
|
|
GrPaint&&,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localM);
|
2019-11-22 21:56:36 +00:00
|
|
|
|
|
|
|
const char* name() const override { return "TestRectOp"; }
|
|
|
|
|
|
|
|
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
|
|
|
|
|
|
|
|
GrProcessorSet::Analysis finalize(const GrCaps&,
|
|
|
|
const GrAppliedClip*,
|
|
|
|
GrClampType) override;
|
|
|
|
|
2021-06-17 13:23:58 +00:00
|
|
|
void visitProxies(const GrVisitProxyFunc& func) const override {
|
2020-03-18 12:13:53 +00:00
|
|
|
if (fProgramInfo) {
|
|
|
|
fProgramInfo->visitFPProxies(func);
|
|
|
|
} else {
|
|
|
|
fProcessorSet.visitProxies(func);
|
|
|
|
}
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DEFINE_OP_CLASS_ID
|
|
|
|
|
|
|
|
TestRectOp(const GrCaps*,
|
|
|
|
GrPaint&&,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localMatrix);
|
2020-02-27 14:30:37 +00:00
|
|
|
|
2020-03-12 16:07:19 +00:00
|
|
|
GrProgramInfo* programInfo() override { return fProgramInfo; }
|
2020-03-11 19:55:55 +00:00
|
|
|
void onCreateProgramInfo(const GrCaps*,
|
|
|
|
SkArenaAlloc*,
|
2020-11-19 18:41:26 +00:00
|
|
|
const GrSurfaceProxyView& writeView,
|
2021-07-13 19:26:39 +00:00
|
|
|
bool usesMSAASurface,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrAppliedClip&&,
|
2021-06-02 15:58:05 +00:00
|
|
|
const GrDstProxyView&,
|
2020-11-20 15:22:43 +00:00
|
|
|
GrXferBarrierFlags renderPassXferBarriers,
|
|
|
|
GrLoadOp colorLoadOp) override;
|
2020-02-28 21:02:40 +00:00
|
|
|
|
2021-06-17 18:55:07 +00:00
|
|
|
void onPrepareDraws(GrMeshDrawTarget*) override;
|
2019-11-22 21:56:36 +00:00
|
|
|
void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
|
|
|
|
|
2020-02-28 21:02:40 +00:00
|
|
|
SkRect fDrawRect;
|
|
|
|
SkRect fLocalRect;
|
|
|
|
SkPMColor4f fColor;
|
|
|
|
GP fGP;
|
2019-11-22 21:56:36 +00:00
|
|
|
GrProcessorSet fProcessorSet;
|
|
|
|
|
2020-02-28 21:02:40 +00:00
|
|
|
// If this op is prePrepared the created programInfo will be stored here for use in
|
2020-02-27 14:30:37 +00:00
|
|
|
// onExecute. In the prePrepared case it will have been stored in the record-time arena.
|
2020-02-28 21:02:40 +00:00
|
|
|
GrProgramInfo* fProgramInfo = nullptr;
|
2020-03-16 15:25:50 +00:00
|
|
|
GrSimpleMesh* fMesh = nullptr;
|
2020-02-27 14:30:37 +00:00
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
friend class ::GrOp;
|
2019-11-22 21:56:36 +00:00
|
|
|
};
|
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
GrOp::Owner TestRectOp::Make(GrRecordingContext* context,
|
|
|
|
GrPaint&& paint,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localM) {
|
2019-11-22 21:56:36 +00:00
|
|
|
const auto* caps = context->priv().caps();
|
2020-10-07 20:46:15 +00:00
|
|
|
return GrOp::Make<TestRectOp>(context, caps, std::move(paint), drawRect, localRect, localM);
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GrProcessorSet::Analysis TestRectOp::finalize(const GrCaps& caps,
|
|
|
|
const GrAppliedClip* clip,
|
|
|
|
GrClampType clampType) {
|
|
|
|
return fProcessorSet.finalize(GrProcessorAnalysisColor::Opaque::kYes,
|
|
|
|
GrProcessorAnalysisCoverage::kSingleChannel, clip,
|
2021-04-22 18:57:28 +00:00
|
|
|
&GrUserStencilSettings::kUnused, caps, clampType, &fColor);
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool use_wide_color(const GrPaint& paint, const GrCaps* caps) {
|
|
|
|
return !paint.getColor4f().fitsInBytes() && caps->halfFloatVertexAttributeSupport();
|
|
|
|
}
|
|
|
|
TestRectOp::TestRectOp(const GrCaps* caps,
|
|
|
|
GrPaint&& paint,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localMatrix)
|
|
|
|
: GrMeshDrawOp(ClassID())
|
|
|
|
, fDrawRect(drawRect)
|
|
|
|
, fLocalRect(localRect)
|
|
|
|
, fColor(paint.getColor4f())
|
|
|
|
, fGP(localMatrix, use_wide_color(paint, caps))
|
|
|
|
, fProcessorSet(std::move(paint)) {
|
|
|
|
this->setBounds(drawRect.makeSorted(), HasAABloat::kNo, IsHairline::kNo);
|
|
|
|
}
|
|
|
|
|
2020-03-11 19:55:55 +00:00
|
|
|
void TestRectOp::onCreateProgramInfo(const GrCaps* caps,
|
|
|
|
SkArenaAlloc* arena,
|
2020-11-19 18:41:26 +00:00
|
|
|
const GrSurfaceProxyView& writeView,
|
2021-07-13 19:26:39 +00:00
|
|
|
bool usesMSAASurface,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrAppliedClip&& appliedClip,
|
2021-06-02 15:58:05 +00:00
|
|
|
const GrDstProxyView& dstProxyView,
|
2020-11-20 15:22:43 +00:00
|
|
|
GrXferBarrierFlags renderPassXferBarriers,
|
|
|
|
GrLoadOp colorLoadOp) {
|
2020-03-11 19:55:55 +00:00
|
|
|
fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
|
|
|
|
arena,
|
2020-04-01 20:22:00 +00:00
|
|
|
writeView,
|
2021-08-26 16:05:11 +00:00
|
|
|
usesMSAASurface,
|
2020-03-11 19:55:55 +00:00
|
|
|
std::move(appliedClip),
|
|
|
|
dstProxyView,
|
|
|
|
&fGP,
|
|
|
|
std::move(fProcessorSet),
|
|
|
|
GrPrimitiveType::kTriangles,
|
2020-09-11 13:33:54 +00:00
|
|
|
renderPassXferBarriers,
|
2020-11-20 15:22:43 +00:00
|
|
|
colorLoadOp,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrPipeline::InputFlags::kNone);
|
2020-02-28 21:02:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 18:55:07 +00:00
|
|
|
void TestRectOp::onPrepareDraws(GrMeshDrawTarget* target) {
|
2019-11-22 21:56:36 +00:00
|
|
|
QuadHelper helper(target, fGP.vertexStride(), 1);
|
|
|
|
GrVertexWriter writer{helper.vertices()};
|
|
|
|
auto pos = GrVertexWriter::TriStripFromRect(fDrawRect);
|
|
|
|
auto local = GrVertexWriter::TriStripFromRect(fLocalRect);
|
|
|
|
GrVertexColor color(fColor, fGP.wideColor());
|
|
|
|
writer.writeQuad(pos, local, color);
|
2020-02-28 21:02:40 +00:00
|
|
|
|
|
|
|
fMesh = helper.mesh();
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
|
2020-02-28 21:02:40 +00:00
|
|
|
if (!fProgramInfo) {
|
2020-03-11 19:55:55 +00:00
|
|
|
this->createProgramInfo(flushState);
|
2020-02-27 14:30:37 +00:00
|
|
|
}
|
2019-12-05 21:40:31 +00:00
|
|
|
|
2020-03-16 23:34:44 +00:00
|
|
|
flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
|
2021-03-22 18:48:09 +00:00
|
|
|
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
|
2020-03-16 23:34:44 +00:00
|
|
|
flushState->drawMesh(*fMesh);
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace sk_gpu_test::test_ops {
|
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
GrOp::Owner MakeRect(GrRecordingContext* context,
|
|
|
|
GrPaint&& paint,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localM) {
|
2019-11-22 21:56:36 +00:00
|
|
|
return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
GrOp::Owner MakeRect(GrRecordingContext* context,
|
|
|
|
std::unique_ptr<GrFragmentProcessor> fp,
|
|
|
|
const SkRect& drawRect,
|
|
|
|
const SkRect& localRect,
|
|
|
|
const SkMatrix& localM) {
|
2019-11-22 21:56:36 +00:00
|
|
|
GrPaint paint;
|
2020-07-21 16:28:35 +00:00
|
|
|
paint.setColorFragmentProcessor(std::move(fp));
|
2019-11-22 21:56:36 +00:00
|
|
|
return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM);
|
|
|
|
}
|
|
|
|
|
2020-10-07 20:46:15 +00:00
|
|
|
GrOp::Owner MakeRect(GrRecordingContext* context,
|
|
|
|
GrPaint&& paint,
|
|
|
|
const SkRect& rect) {
|
2019-11-22 21:56:36 +00:00
|
|
|
return TestRectOp::Make(context, std::move(paint), rect, rect, SkMatrix::I());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sk_gpu_test::test_ops
|