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/GrGLSLGeometryProcessor.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"; }
|
|
|
|
|
2020-06-19 14:47:30 +00:00
|
|
|
GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
|
|
|
|
return new GLSLGP();
|
|
|
|
}
|
2019-11-22 21:56:36 +00:00
|
|
|
|
2020-06-19 14:47:30 +00:00
|
|
|
void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
|
|
|
|
GLSLGP::GenKey(*this, b);
|
|
|
|
}
|
2019-11-22 21:56:36 +00:00
|
|
|
|
|
|
|
bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
class GLSLGP : public GrGLSLGeometryProcessor {
|
2020-06-19 14:47:30 +00:00
|
|
|
public:
|
2019-11-22 21:56:36 +00:00
|
|
|
void setData(const GrGLSLProgramDataManager& pdman,
|
2020-07-01 19:14:39 +00:00
|
|
|
const GrPrimitiveProcessor& pp) override {
|
2019-11-22 21:56:36 +00:00
|
|
|
const auto& gp = pp.cast<GP>();
|
2020-06-19 14:47:30 +00:00
|
|
|
this->setTransform(pdman, fLocalMatrixUni, gp.fLocalMatrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GenKey(const GP& gp, GrProcessorKeyBuilder* b) {
|
|
|
|
b->add32(ComputeMatrixKey(gp.fLocalMatrix));
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
|
|
|
|
const auto& gp = args.fGP.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("%s = %s;", args.fOutputColor, colorVarying.fsIn());
|
|
|
|
args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
|
|
|
|
this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name());
|
2020-06-19 14:47:30 +00:00
|
|
|
this->writeLocalCoord(args.fVertBuilder, args.fUniformHandler, gpArgs,
|
|
|
|
gp.fInLocalCoords.asShaderVar(), gp.fLocalMatrix,
|
|
|
|
&fLocalMatrixUni);
|
2019-11-22 21:56:36 +00:00
|
|
|
}
|
2020-06-19 14:47:30 +00:00
|
|
|
|
|
|
|
UniformHandle fLocalMatrixUni;
|
2019-11-22 21:56:36 +00:00
|
|
|
};
|
2020-06-19 14:47:30 +00:00
|
|
|
|
|
|
|
Attribute fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
|
|
|
|
Attribute fInLocalCoords = {"inLocalCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
|
|
|
|
Attribute fInColor;
|
|
|
|
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*,
|
|
|
|
bool hasMixedSampledCoverage,
|
|
|
|
GrClampType) override;
|
|
|
|
|
|
|
|
void visitProxies(const VisitProxyFunc& 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,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrAppliedClip&&,
|
2020-09-11 13:33:54 +00:00
|
|
|
const GrXferProcessor::DstProxyView&,
|
2020-11-20 15:22:43 +00:00
|
|
|
GrXferBarrierFlags renderPassXferBarriers,
|
|
|
|
GrLoadOp colorLoadOp) override;
|
2020-02-28 21:02:40 +00:00
|
|
|
|
2019-11-22 21:56:36 +00:00
|
|
|
void onPrepareDraws(Target*) override;
|
|
|
|
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,
|
|
|
|
bool hasMixedSampledCoverage,
|
|
|
|
GrClampType clampType) {
|
|
|
|
return fProcessorSet.finalize(GrProcessorAnalysisColor::Opaque::kYes,
|
|
|
|
GrProcessorAnalysisCoverage::kSingleChannel, clip,
|
|
|
|
&GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps,
|
|
|
|
clampType, &fColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-03-11 19:55:55 +00:00
|
|
|
GrAppliedClip&& appliedClip,
|
2020-09-11 13:33:54 +00:00
|
|
|
const GrXferProcessor::DstProxyView& 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,
|
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
|
|
|
}
|
|
|
|
|
2019-11-22 21:56:36 +00:00
|
|
|
void TestRectOp::onPrepareDraws(Target* target) {
|
|
|
|
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);
|
|
|
|
flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
|
|
|
|
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
|