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 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 35 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override { |
| 36 | return new GLSLGP(); |
| 37 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 38 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 39 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 40 | GLSLGP::GenKey(*this, b); |
| 41 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 42 | |
| 43 | bool wideColor() const { return fInColor.cpuType() != kUByte4_norm_GrVertexAttribType; } |
| 44 | |
| 45 | private: |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 46 | class GLSLGP : public GrGLSLGeometryProcessor { |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 47 | public: |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 48 | void setData(const GrGLSLProgramDataManager& pdman, |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 49 | const GrPrimitiveProcessor& pp) override { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 50 | const auto& gp = pp.cast<GP>(); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 51 | this->setTransform(pdman, fLocalMatrixUni, gp.fLocalMatrix); |
| 52 | } |
| 53 | |
| 54 | static void GenKey(const GP& gp, GrProcessorKeyBuilder* b) { |
| 55 | b->add32(ComputeMatrixKey(gp.fLocalMatrix)); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private: |
| 59 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 60 | const auto& gp = args.fGP.cast<GP>(); |
| 61 | args.fVaryingHandler->emitAttributes(gp); |
| 62 | GrGLSLVarying colorVarying(kHalf4_GrSLType); |
| 63 | args.fVaryingHandler->addVarying("color", &colorVarying, |
| 64 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 65 | args.fVertBuilder->codeAppendf("%s = %s;", colorVarying.vsOut(), gp.fInColor.name()); |
| 66 | args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, colorVarying.fsIn()); |
| 67 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 68 | this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.fInPosition.name()); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 69 | this->writeLocalCoord(args.fVertBuilder, args.fUniformHandler, gpArgs, |
| 70 | gp.fInLocalCoords.asShaderVar(), gp.fLocalMatrix, |
| 71 | &fLocalMatrixUni); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 72 | } |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 73 | |
| 74 | UniformHandle fLocalMatrixUni; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 75 | }; |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 76 | |
| 77 | Attribute fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 78 | Attribute fInLocalCoords = {"inLocalCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 79 | Attribute fInColor; |
| 80 | SkMatrix fLocalMatrix; |
| 81 | }; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 82 | |
| 83 | class TestRectOp final : public GrMeshDrawOp { |
| 84 | public: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 85 | static GrOp::Owner Make(GrRecordingContext*, |
| 86 | GrPaint&&, |
| 87 | const SkRect& drawRect, |
| 88 | const SkRect& localRect, |
| 89 | const SkMatrix& localM); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 90 | |
| 91 | const char* name() const override { return "TestRectOp"; } |
| 92 | |
| 93 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 94 | |
| 95 | GrProcessorSet::Analysis finalize(const GrCaps&, |
| 96 | const GrAppliedClip*, |
| 97 | bool hasMixedSampledCoverage, |
| 98 | GrClampType) override; |
| 99 | |
| 100 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 101 | if (fProgramInfo) { |
| 102 | fProgramInfo->visitFPProxies(func); |
| 103 | } else { |
| 104 | fProcessorSet.visitProxies(func); |
| 105 | } |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | private: |
| 109 | DEFINE_OP_CLASS_ID |
| 110 | |
| 111 | TestRectOp(const GrCaps*, |
| 112 | GrPaint&&, |
| 113 | const SkRect& drawRect, |
| 114 | const SkRect& localRect, |
| 115 | const SkMatrix& localMatrix); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 116 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 117 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 118 | void onCreateProgramInfo(const GrCaps*, |
| 119 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 120 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 121 | GrAppliedClip&&, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 122 | const GrXferProcessor::DstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 123 | GrXferBarrierFlags renderPassXferBarriers, |
| 124 | GrLoadOp colorLoadOp) override; |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 125 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 126 | void onPrepareDraws(Target*) override; |
| 127 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
| 128 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 129 | SkRect fDrawRect; |
| 130 | SkRect fLocalRect; |
| 131 | SkPMColor4f fColor; |
| 132 | GP fGP; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 133 | GrProcessorSet fProcessorSet; |
| 134 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 135 | // 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] | 136 | // 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] | 137 | GrProgramInfo* fProgramInfo = nullptr; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 138 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 139 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 140 | friend class ::GrOp; |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 141 | }; |
| 142 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 143 | GrOp::Owner TestRectOp::Make(GrRecordingContext* context, |
| 144 | GrPaint&& paint, |
| 145 | const SkRect& drawRect, |
| 146 | const SkRect& localRect, |
| 147 | const SkMatrix& localM) { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 148 | const auto* caps = context->priv().caps(); |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 149 | return GrOp::Make<TestRectOp>(context, caps, std::move(paint), drawRect, localRect, localM); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | GrProcessorSet::Analysis TestRectOp::finalize(const GrCaps& caps, |
| 153 | const GrAppliedClip* clip, |
| 154 | bool hasMixedSampledCoverage, |
| 155 | GrClampType clampType) { |
| 156 | return fProcessorSet.finalize(GrProcessorAnalysisColor::Opaque::kYes, |
| 157 | GrProcessorAnalysisCoverage::kSingleChannel, clip, |
| 158 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, |
| 159 | clampType, &fColor); |
| 160 | } |
| 161 | |
| 162 | static bool use_wide_color(const GrPaint& paint, const GrCaps* caps) { |
| 163 | return !paint.getColor4f().fitsInBytes() && caps->halfFloatVertexAttributeSupport(); |
| 164 | } |
| 165 | TestRectOp::TestRectOp(const GrCaps* caps, |
| 166 | GrPaint&& paint, |
| 167 | const SkRect& drawRect, |
| 168 | const SkRect& localRect, |
| 169 | const SkMatrix& localMatrix) |
| 170 | : GrMeshDrawOp(ClassID()) |
| 171 | , fDrawRect(drawRect) |
| 172 | , fLocalRect(localRect) |
| 173 | , fColor(paint.getColor4f()) |
| 174 | , fGP(localMatrix, use_wide_color(paint, caps)) |
| 175 | , fProcessorSet(std::move(paint)) { |
| 176 | this->setBounds(drawRect.makeSorted(), HasAABloat::kNo, IsHairline::kNo); |
| 177 | } |
| 178 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 179 | void TestRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 180 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 181 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 182 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 183 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 184 | GrXferBarrierFlags renderPassXferBarriers, |
| 185 | GrLoadOp colorLoadOp) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 186 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, |
| 187 | arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 188 | writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 189 | std::move(appliedClip), |
| 190 | dstProxyView, |
| 191 | &fGP, |
| 192 | std::move(fProcessorSet), |
| 193 | GrPrimitiveType::kTriangles, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 194 | renderPassXferBarriers, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 195 | colorLoadOp, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 196 | GrPipeline::InputFlags::kNone); |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 199 | void TestRectOp::onPrepareDraws(Target* target) { |
| 200 | QuadHelper helper(target, fGP.vertexStride(), 1); |
| 201 | GrVertexWriter writer{helper.vertices()}; |
| 202 | auto pos = GrVertexWriter::TriStripFromRect(fDrawRect); |
| 203 | auto local = GrVertexWriter::TriStripFromRect(fLocalRect); |
| 204 | GrVertexColor color(fColor, fGP.wideColor()); |
| 205 | writer.writeQuad(pos, local, color); |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 206 | |
| 207 | fMesh = helper.mesh(); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void TestRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 211 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 212 | this->createProgramInfo(flushState); |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 213 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 214 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 215 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 216 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 217 | flushState->drawMesh(*fMesh); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } // anonymous namespace |
| 221 | |
| 222 | namespace sk_gpu_test::test_ops { |
| 223 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 224 | GrOp::Owner MakeRect(GrRecordingContext* context, |
| 225 | GrPaint&& paint, |
| 226 | const SkRect& drawRect, |
| 227 | const SkRect& localRect, |
| 228 | const SkMatrix& localM) { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 229 | return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM); |
| 230 | } |
| 231 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 232 | GrOp::Owner MakeRect(GrRecordingContext* context, |
| 233 | std::unique_ptr<GrFragmentProcessor> fp, |
| 234 | const SkRect& drawRect, |
| 235 | const SkRect& localRect, |
| 236 | const SkMatrix& localM) { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 237 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 238 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 239 | return TestRectOp::Make(context, std::move(paint), drawRect, localRect, localM); |
| 240 | } |
| 241 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 242 | GrOp::Owner MakeRect(GrRecordingContext* context, |
| 243 | GrPaint&& paint, |
| 244 | const SkRect& rect) { |
Brian Salomon | e21af50 | 2019-11-22 16:56:36 -0500 | [diff] [blame] | 245 | return TestRectOp::Make(context, std::move(paint), rect, rect, SkMatrix::I()); |
| 246 | } |
| 247 | |
| 248 | } // namespace sk_gpu_test::test_ops |