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