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 | |
| 10 | #include "SkTypes.h" |
| 11 | #include "Test.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 14 | #include "GrContext.h" |
| 15 | #include "GrGeometryProcessor.h" |
| 16 | #include "GrGpu.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 17 | #include "GrOpFlushState.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 18 | #include "GrRenderTargetContext.h" |
| 19 | #include "GrRenderTargetContextPriv.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 20 | #include "SkString.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 21 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 22 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 23 | #include "glsl/GrGLSLVarying.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 24 | #include "ops/GrMeshDrawOp.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 25 | |
| 26 | namespace { |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 27 | class Op : public GrMeshDrawOp { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 28 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 29 | DEFINE_OP_CLASS_ID |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 30 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 31 | const char* name() const override { return "Dummy Op"; } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 32 | |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 33 | static std::unique_ptr<GrDrawOp> Make(int numAttribs) { |
| 34 | return std::unique_ptr<GrDrawOp>(new Op(numAttribs)); |
| 35 | } |
| 36 | |
| 37 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 38 | return FixedFunctionFlags::kNone; |
| 39 | } |
| 40 | |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 41 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,\ |
| 42 | GrPixelConfigIsClamped) override { |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 43 | return RequiresDstTexture::kNo; |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 44 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 48 | this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 51 | bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; } |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 52 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 53 | void onPrepareDraws(Target* target) override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 54 | class GP : public GrGeometryProcessor { |
| 55 | public: |
| 56 | GP(int numAttribs) { |
| 57 | this->initClassID<GP>(); |
| 58 | SkASSERT(numAttribs > 1); |
| 59 | for (auto i = 0; i < numAttribs; ++i) { |
| 60 | fAttribNames.push_back().printf("attr%d", i); |
| 61 | } |
| 62 | for (auto i = 0; i < numAttribs; ++i) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 63 | this->addVertexAttrib(fAttribNames[i].c_str(), kFloat2_GrVertexAttribType); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 64 | } |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 65 | } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 66 | const char* name() const override { return "Dummy GP"; } |
| 67 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 68 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 69 | class GLSLGP : public GrGLSLGeometryProcessor { |
| 70 | public: |
| 71 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 72 | const GP& gp = args.fGP.cast<GP>(); |
| 73 | args.fVaryingHandler->emitAttributes(gp); |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 74 | this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.getAttrib(0).fName); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 75 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 76 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor); |
| 77 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 78 | } |
| 79 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 80 | const GrPrimitiveProcessor& primProc, |
| 81 | FPCoordTransformIter&&) override {} |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 82 | }; |
| 83 | return new GLSLGP(); |
| 84 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 85 | void getGLSLProcessorKey(const GrShaderCaps&, |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 86 | GrProcessorKeyBuilder* builder) const override { |
| 87 | builder->add32(this->numAttribs()); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | SkTArray<SkString> fAttribNames; |
| 92 | }; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 93 | sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 94 | QuadHelper helper; |
| 95 | size_t vertexStride = gp->getVertexStride(); |
| 96 | SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1)); |
| 97 | vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride); |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 98 | helper.recordDraw(target, gp.get(), |
Brian Salomon | bfd18cd | 2017-08-09 16:27:09 -0400 | [diff] [blame] | 99 | target->makePipeline(0, GrProcessorSet::MakeEmptySet(), |
| 100 | target->detachAppliedClip())); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int fNumAttribs; |
| 104 | |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 105 | typedef GrMeshDrawOp INHERITED; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 106 | }; |
| 107 | } |
| 108 | |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 109 | DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 110 | GrContext* context = ctxInfo.grContext(); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 111 | |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 112 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 113 | SkBackingFit::kApprox, |
| 114 | 1, 1, kRGBA_8888_GrPixelConfig, |
| 115 | nullptr)); |
| 116 | if (!renderTargetContext) { |
| 117 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | int attribCnt = context->caps()->maxVertexAttributes(); |
| 121 | if (!attribCnt) { |
| 122 | ERRORF(reporter, "No attributes allowed?!"); |
| 123 | return; |
| 124 | } |
| 125 | context->flush(); |
| 126 | context->resetGpuStats(); |
| 127 | #if GR_GPU_STATS |
| 128 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0); |
| 129 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0); |
| 130 | #endif |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 131 | GrPaint grPaint; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 132 | // This one should succeed. |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 133 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 134 | context->flush(); |
| 135 | #if GR_GPU_STATS |
| 136 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1); |
| 137 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0); |
| 138 | #endif |
| 139 | context->resetGpuStats(); |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 140 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 141 | context->flush(); |
| 142 | #if GR_GPU_STATS |
| 143 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0); |
| 144 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1); |
| 145 | #endif |
| 146 | } |
| 147 | #endif |