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