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