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