bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 | |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 8 | // This is a GPU-backend specific test. It relies on static initializers to work |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 9 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkTypes.h" |
| 13 | #include "tests/Test.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkString.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 16 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkPointPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrGeometryProcessor.h" |
| 20 | #include "src/gpu/GrGpu.h" |
| 21 | #include "src/gpu/GrMemoryPool.h" |
| 22 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 26 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 27 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 28 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 29 | |
| 30 | namespace { |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 31 | class Op : public GrMeshDrawOp { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 32 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 33 | DEFINE_OP_CLASS_ID |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 34 | |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 35 | const char* name() const override { return "Test Op"; } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 36 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 37 | static GrOp::Owner Make(GrRecordingContext* rContext, int numAttribs) { |
| 38 | return GrOp::Make<Op>(rContext, numAttribs); |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 42 | return FixedFunctionFlags::kNone; |
| 43 | } |
| 44 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 45 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 46 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 47 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 48 | |
| 49 | private: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 50 | friend class ::GrOp; |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 51 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 52 | Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 53 | this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 56 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 57 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 58 | void onCreateProgramInfo(const GrCaps* caps, |
| 59 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 60 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 61 | bool usesMSAASurface, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 62 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 63 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 64 | GrXferBarrierFlags renderPassXferBarriers, |
| 65 | GrLoadOp colorLoadOp) override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 66 | class GP : public GrGeometryProcessor { |
| 67 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 68 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 69 | return arena->make([&](void* ptr) { |
| 70 | return new (ptr) GP(numAttribs); |
| 71 | }); |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 72 | } |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 73 | |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 74 | const char* name() const override { return "Test GP"; } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 75 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 76 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override { |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 77 | class Impl : public ProgramImpl { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 78 | public: |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 79 | void setData(const GrGLSLProgramDataManager&, |
| 80 | const GrShaderCaps&, |
| 81 | const GrGeometryProcessor&) override {} |
| 82 | |
| 83 | private: |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 84 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 85 | const GP& gp = args.fGeomProc.cast<GP>(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 86 | args.fVaryingHandler->emitAttributes(gp); |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 87 | WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fAttributes[0].name()); |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 88 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 89 | fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputColor); |
| 90 | fragBuilder->codeAppendf("const half4 %s = half4(1);", |
| 91 | args.fOutputCoverage); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 92 | } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 93 | }; |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 94 | |
| 95 | return std::make_unique<Impl>(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 96 | } |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 97 | void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* builder) const override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 98 | builder->add32(fNumAttribs); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 102 | GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) { |
| 103 | SkASSERT(numAttribs > 1); |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 104 | fAttribNames = std::make_unique<SkString[]>(numAttribs); |
| 105 | fAttributes = std::make_unique<Attribute[]>(numAttribs); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 106 | for (auto i = 0; i < numAttribs; ++i) { |
| 107 | fAttribNames[i].printf("attr%d", i); |
| 108 | // This gives us more of a mix of attribute types, and allows the |
| 109 | // component count to fit within the limits for iOS Metal. |
| 110 | if (i & 0x1) { |
| 111 | fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType, |
| 112 | kFloat_GrSLType}; |
| 113 | } else { |
| 114 | fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType, |
| 115 | kFloat2_GrSLType}; |
| 116 | } |
| 117 | } |
| 118 | this->setVertexAttributes(fAttributes.get(), numAttribs); |
| 119 | } |
| 120 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 121 | int fNumAttribs; |
| 122 | std::unique_ptr<SkString[]> fAttribNames; |
| 123 | std::unique_ptr<Attribute[]> fAttributes; |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 124 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 125 | using INHERITED = GrGeometryProcessor; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 126 | }; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 127 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 128 | GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs); |
| 129 | |
| 130 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, |
| 131 | arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 132 | writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame] | 133 | usesMSAASurface, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 134 | std::move(appliedClip), |
| 135 | dstProxyView, |
| 136 | gp, |
| 137 | GrProcessorSet::MakeEmptySet(), |
| 138 | GrPrimitiveType::kTriangles, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 139 | renderPassXferBarriers, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 140 | colorLoadOp, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 141 | GrPipeline::InputFlags::kNone); |
| 142 | } |
| 143 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 144 | void onPrepareDraws(GrMeshDrawTarget* target) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 145 | if (!fProgramInfo) { |
| 146 | this->createProgramInfo(target); |
| 147 | } |
| 148 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 149 | size_t vertexStride = fProgramInfo->geomProc().vertexStride(); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 150 | QuadHelper helper(target, vertexStride, 1); |
| 151 | SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices()); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 152 | SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 153 | fMesh = helper.mesh(); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 156 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 157 | if (!fProgramInfo || !fMesh) { |
| 158 | return; |
| 159 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 160 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 161 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 162 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 163 | flushState->drawMesh(*fMesh); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 166 | int fNumAttribs; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 167 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 168 | GrProgramInfo* fProgramInfo = nullptr; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 169 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 170 | using INHERITED = GrMeshDrawOp; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 171 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 172 | } // namespace |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 173 | |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 174 | DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 175 | auto dContext = ctxInfo.directContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 176 | #if GR_GPU_STATS |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 177 | GrGpu* gpu = dContext->priv().getGpu(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 178 | #endif |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 179 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 180 | auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, |
| 181 | GrColorType::kRGBA_8888, |
| 182 | nullptr, |
| 183 | SkBackingFit::kApprox, |
| 184 | {1, 1}, |
| 185 | SkSurfaceProps()); |
| 186 | if (!sdc) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 187 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 190 | int attribCnt = dContext->priv().caps()->maxVertexAttributes(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 191 | if (!attribCnt) { |
| 192 | ERRORF(reporter, "No attributes allowed?!"); |
| 193 | return; |
| 194 | } |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 195 | dContext->flushAndSubmit(); |
| 196 | dContext->priv().resetGpuStats(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 197 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 199 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 200 | #endif |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 201 | // Adding discard to appease vulkan validation warning about loading uninitialized data on draw |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 202 | sdc->discard(); |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 203 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 204 | GrPaint grPaint; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 205 | // This one should succeed. |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 206 | sdc->addDrawOp(Op::Make(dContext, attribCnt)); |
| 207 | dContext->flushAndSubmit(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 208 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 209 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1); |
| 210 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 211 | #endif |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 212 | dContext->priv().resetGpuStats(); |
| 213 | sdc->addDrawOp(Op::Make(dContext, attribCnt + 1)); |
| 214 | dContext->flushAndSubmit(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 215 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 216 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 217 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 218 | #endif |
| 219 | } |