Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "tools/gpu/TestOps.h" |
| 9 | |
| 10 | #include "src/core/SkPointPriv.h" |
| 11 | #include "src/gpu/GrCaps.h" |
| 12 | #include "src/gpu/GrGeometryProcessor.h" |
| 13 | #include "src/gpu/GrMemoryPool.h" |
| 14 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrProgramInfo.h" |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrVertexWriter.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 19 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 20 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 21 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | class GP : public GrGeometryProcessor { |
| 26 | public: |
| 27 | GP(const SkMatrix& localMatrix, bool wideColor) |
| 28 | : GrGeometryProcessor(kTestRectOp_ClassID), fLocalMatrix(localMatrix) { |
| 29 | fInColor = MakeColorAttribute("color", wideColor); |
| 30 | this->setVertexAttributes(&fInPosition, 3); |
| 31 | } |
| 32 | |
| 33 | const char* name() const override { return "TestRectOp::GP"; } |
| 34 | |
| 35 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override; |
| 36 | |
| 37 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
| 38 | |
| 39 | bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; } |
| 40 | |
| 41 | private: |
| 42 | Attribute fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 43 | Attribute fInLocalCoords = {"inLocalCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 44 | Attribute fInColor; |
| 45 | SkMatrix fLocalMatrix; |
| 46 | }; |
| 47 | |
| 48 | GrGLSLPrimitiveProcessor* GP::createGLSLInstance(const GrShaderCaps& caps) const { |
| 49 | class GLSLGP : public GrGLSLGeometryProcessor { |
| 50 | void setData(const GrGLSLProgramDataManager& pdman, |
| 51 | const GrPrimitiveProcessor& pp, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 52 | const CoordTransformRange& transformRange) override { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 53 | const auto& gp = pp.cast<GP>(); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 54 | this->setTransformDataHelper(gp.fLocalMatrix, pdman, transformRange); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
| 58 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 59 | const auto& gp = args.fGP.cast<GP>(); |
| 60 | args.fVaryingHandler->emitAttributes(gp); |
| 61 | GrGLSLVarying colorVarying(kHalf4_GrSLType); |
| 62 | args.fVaryingHandler->addVarying("color", &colorVarying, |
| 63 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 64 | args.fVertBuilder->codeAppendf("%s = %s;", colorVarying.vsOut(), gp.fInColor.name()); |
| 65 | args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, colorVarying.fsIn()); |
| 66 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 67 | this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name()); |
| 68 | this->emitTransforms(args.fVertBuilder, args.fVaryingHandler, args.fUniformHandler, |
| 69 | gp.fInLocalCoords.asShaderVar(), gp.fLocalMatrix, |
| 70 | args.fFPCoordTransformHandler); |
| 71 | } |
| 72 | }; |
| 73 | return new GLSLGP(); |
| 74 | } |
| 75 | |
| 76 | class TestRectOp final : public GrMeshDrawOp { |
| 77 | public: |
| 78 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext*, |
| 79 | GrPaint&&, |
| 80 | const SkRect& drawRect, |
| 81 | const SkRect& localRect, |
| 82 | const SkMatrix& localM); |
| 83 | |
| 84 | const char* name() const override { return "TestRectOp"; } |
| 85 | |
| 86 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 87 | |
| 88 | GrProcessorSet::Analysis finalize(const GrCaps&, |
| 89 | const GrAppliedClip*, |
| 90 | bool hasMixedSampledCoverage, |
| 91 | GrClampType) override; |
| 92 | |
| 93 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 94 | if (fProgramInfo) { |
| 95 | fProgramInfo->visitFPProxies(func); |
| 96 | } else { |
| 97 | fProcessorSet.visitProxies(func); |
| 98 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private: |
| 102 | DEFINE_OP_CLASS_ID |
| 103 | |
| 104 | TestRectOp(const GrCaps*, |
| 105 | GrPaint&&, |
| 106 | const SkRect& drawRect, |
| 107 | const SkRect& localRect, |
| 108 | const SkMatrix& localMatrix); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 109 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 110 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 111 | void onCreateProgramInfo(const GrCaps*, |
| 112 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 113 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 114 | GrAppliedClip&&, |
| 115 | const GrXferProcessor::DstProxyView&) override; |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 116 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 117 | void onPrepareDraws(Target*) override; |
| 118 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
| 119 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 120 | SkRect fDrawRect; |
| 121 | SkRect fLocalRect; |
| 122 | SkPMColor4f fColor; |
| 123 | GP fGP; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 124 | GrProcessorSet fProcessorSet; |
| 125 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 126 | // If this op is prePrepared the created programInfo will be stored here for use in |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 127 | // onExecute. In the prePrepared case it will have been stored in the record-time arena. |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 128 | GrProgramInfo* fProgramInfo = nullptr; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 129 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 130 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 131 | friend class ::GrOpMemoryPool; |
| 132 | }; |
| 133 | |
| 134 | std::unique_ptr<GrDrawOp> TestRectOp::Make(GrRecordingContext* context, |
| 135 | GrPaint&& paint, |
| 136 | const SkRect& drawRect, |
| 137 | const SkRect& localRect, |
| 138 | const SkMatrix& localM) { |
| 139 | auto* pool = context->priv().opMemoryPool(); |
| 140 | const auto* caps = context->priv().caps(); |
| 141 | return pool->allocate<TestRectOp>(caps, std::move(paint), drawRect, localRect, localM); |
| 142 | } |
| 143 | |
| 144 | GrProcessorSet::Analysis TestRectOp::finalize(const GrCaps& caps, |
| 145 | const GrAppliedClip* clip, |
| 146 | bool hasMixedSampledCoverage, |
| 147 | GrClampType clampType) { |
| 148 | return fProcessorSet.finalize(GrProcessorAnalysisColor::Opaque::kYes, |
| 149 | GrProcessorAnalysisCoverage::kSingleChannel, clip, |
| 150 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, |
| 151 | clampType, &fColor); |
| 152 | } |
| 153 | |
| 154 | static bool use_wide_color(const GrPaint& paint, const GrCaps* caps) { |
| 155 | return !paint.getColor4f().fitsInBytes() && caps->halfFloatVertexAttributeSupport(); |
| 156 | } |
| 157 | TestRectOp::TestRectOp(const GrCaps* caps, |
| 158 | GrPaint&& paint, |
| 159 | const SkRect& drawRect, |
| 160 | const SkRect& localRect, |
| 161 | const SkMatrix& localMatrix) |
| 162 | : GrMeshDrawOp(ClassID()) |
| 163 | , fDrawRect(drawRect) |
| 164 | , fLocalRect(localRect) |
| 165 | , fColor(paint.getColor4f()) |
| 166 | , fGP(localMatrix, use_wide_color(paint, caps)) |
| 167 | , fProcessorSet(std::move(paint)) { |
| 168 | this->setBounds(drawRect.makeSorted(), HasAABloat::kNo, IsHairline::kNo); |
| 169 | } |
| 170 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 171 | void TestRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 172 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 173 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 174 | GrAppliedClip&& appliedClip, |
| 175 | const GrXferProcessor::DstProxyView& dstProxyView) { |
| 176 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, |
| 177 | arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 178 | writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 179 | std::move(appliedClip), |
| 180 | dstProxyView, |
| 181 | &fGP, |
| 182 | std::move(fProcessorSet), |
| 183 | GrPrimitiveType::kTriangles, |
| 184 | GrPipeline::InputFlags::kNone); |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 187 | void TestRectOp::onPrepareDraws(Target* target) { |
| 188 | QuadHelper helper(target, fGP.vertexStride(), 1); |
| 189 | GrVertexWriter writer{helper.vertices()}; |
| 190 | auto pos = GrVertexWriter::TriStripFromRect(fDrawRect); |
| 191 | auto local = GrVertexWriter::TriStripFromRect(fLocalRect); |
| 192 | GrVertexColor color(fColor, fGP.wideColor()); |
| 193 | writer.writeQuad(pos, local, color); |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 194 | |
| 195 | fMesh = helper.mesh(); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void TestRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 199 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 200 | this->createProgramInfo(flushState); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 201 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 202 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 203 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 204 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 205 | flushState->drawMesh(*fMesh); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // anonymous namespace |
| 209 | |
| 210 | namespace sk_gpu_test::test_ops { |
| 211 | |
| 212 | std::unique_ptr<GrDrawOp> MakeRect(GrRecordingContext* context, |
| 213 | GrPaint&& paint, |
| 214 | const SkRect& drawRect, |
| 215 | const SkRect& localRect, |
| 216 | const SkMatrix& localM) { |
| 217 | return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM); |
| 218 | } |
| 219 | |
| 220 | std::unique_ptr<GrDrawOp> MakeRect(GrRecordingContext* context, |
| 221 | std::unique_ptr<GrFragmentProcessor> fp, |
| 222 | const SkRect& drawRect, |
| 223 | const SkRect& localRect, |
| 224 | const SkMatrix& localM) { |
| 225 | GrPaint paint; |
| 226 | paint.addColorFragmentProcessor(std::move(fp)); |
| 227 | return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM); |
| 228 | } |
| 229 | |
| 230 | std::unique_ptr<GrDrawOp> MakeRect(GrRecordingContext* context, |
| 231 | GrPaint&& paint, |
| 232 | const SkRect& rect) { |
| 233 | return TestRectOp::Make(context, std::move(paint), rect, rect, SkMatrix::I()); |
| 234 | } |
| 235 | |
| 236 | } // namespace sk_gpu_test::test_ops |