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 | |
| 8 | // This is a GPU-backend specific test. It relies on static intializers to work |
| 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/GrRenderTargetContext.h" |
| 25 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 26 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 27 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 28 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 29 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 30 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 31 | |
| 32 | namespace { |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 33 | class Op : public GrMeshDrawOp { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 34 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 35 | DEFINE_OP_CLASS_ID |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 36 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 37 | const char* name() const override { return "Dummy Op"; } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 38 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 39 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* rContext, int numAttribs) { |
| 40 | GrOpMemoryPool* pool = rContext->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 41 | |
| 42 | return pool->allocate<Op>(numAttribs); |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 46 | return FixedFunctionFlags::kNone; |
| 47 | } |
| 48 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 49 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 50 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 51 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 52 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 53 | |
| 54 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 55 | friend class ::GrOpMemoryPool; |
| 56 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 57 | Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 58 | this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 61 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 62 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 63 | void onCreateProgramInfo(const GrCaps* caps, |
| 64 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 65 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 66 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 67 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 68 | GrXferBarrierFlags renderPassXferBarriers) override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 69 | class GP : public GrGeometryProcessor { |
| 70 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 71 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) { |
| 72 | return arena->make<GP>(numAttribs); |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 73 | } |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 74 | |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 75 | const char* name() const override { return "Dummy GP"; } |
| 76 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 77 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 78 | class GLSLGP : public GrGLSLGeometryProcessor { |
| 79 | public: |
| 80 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 81 | const GP& gp = args.fGP.cast<GP>(); |
| 82 | args.fVaryingHandler->emitAttributes(gp); |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 83 | this->writeOutputPosition(args.fVertBuilder, gpArgs, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 84 | gp.fAttributes[0].name()); |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 85 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 86 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor); |
| 87 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 88 | } |
| 89 | void setData(const GrGLSLProgramDataManager& pdman, |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 90 | const GrPrimitiveProcessor& primProc) override {} |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 91 | }; |
| 92 | return new GLSLGP(); |
| 93 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 94 | void getGLSLProcessorKey(const GrShaderCaps&, |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 95 | GrProcessorKeyBuilder* builder) const override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 96 | builder->add32(fNumAttribs); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 100 | friend class ::SkArenaAlloc; // for access to ctor |
| 101 | |
| 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, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 133 | std::move(appliedClip), |
| 134 | dstProxyView, |
| 135 | gp, |
| 136 | GrProcessorSet::MakeEmptySet(), |
| 137 | GrPrimitiveType::kTriangles, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 138 | renderPassXferBarriers, |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 139 | GrPipeline::InputFlags::kNone); |
| 140 | } |
| 141 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 142 | void onPrepareDraws(Target* target) override { |
| 143 | if (!fProgramInfo) { |
| 144 | this->createProgramInfo(target); |
| 145 | } |
| 146 | |
| 147 | size_t vertexStride = fProgramInfo->primProc().vertexStride(); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 148 | QuadHelper helper(target, vertexStride, 1); |
| 149 | SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices()); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 150 | SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride); |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 151 | fMesh = helper.mesh(); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 154 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 155 | if (!fProgramInfo || !fMesh) { |
| 156 | return; |
| 157 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 158 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 159 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 160 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 161 | flushState->drawMesh(*fMesh); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 164 | int fNumAttribs; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 165 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | 6941f4a | 2020-03-12 09:41:54 -0400 | [diff] [blame] | 166 | GrProgramInfo* fProgramInfo = nullptr; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 167 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 168 | using INHERITED = GrMeshDrawOp; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 169 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 170 | } // namespace |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 171 | |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 172 | DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 173 | auto context = ctxInfo.directContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 174 | #if GR_GPU_STATS |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 175 | GrGpu* gpu = context->priv().getGpu(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 176 | #endif |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 177 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 178 | auto renderTargetContext = GrRenderTargetContext::Make( |
| 179 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1}); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 180 | if (!renderTargetContext) { |
| 181 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 182 | return; |
| 183 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 184 | int attribCnt = context->priv().caps()->maxVertexAttributes(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 185 | if (!attribCnt) { |
| 186 | ERRORF(reporter, "No attributes allowed?!"); |
| 187 | return; |
| 188 | } |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 189 | context->flushAndSubmit(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 190 | context->priv().resetGpuStats(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 191 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 192 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 193 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 194 | #endif |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 195 | // Adding discard to appease vulkan validation warning about loading uninitialized data on draw |
| 196 | renderTargetContext->discard(); |
| 197 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 198 | GrPaint grPaint; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 199 | // This one should succeed. |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 200 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt)); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 201 | context->flushAndSubmit(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 202 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1); |
| 204 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 205 | #endif |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 206 | context->priv().resetGpuStats(); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 207 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1)); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 208 | context->flushAndSubmit(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 209 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 210 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 211 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 212 | #endif |
| 213 | } |