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 |
| 14 | #include "GrBatchFlushState.h" |
| 15 | #include "GrDrawContext.h" |
| 16 | #include "GrDrawContextPriv.h" |
| 17 | #include "GrContext.h" |
| 18 | #include "GrGeometryProcessor.h" |
| 19 | #include "GrGpu.h" |
| 20 | #include "GrTextureProvider.h" |
| 21 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 22 | #include "glsl/GrGLSLVarying.h" |
| 23 | #include "batches/GrVertexBatch.h" |
| 24 | #include "SkString.h" |
| 25 | |
| 26 | namespace { |
| 27 | class Batch : public GrVertexBatch { |
| 28 | public: |
| 29 | DEFINE_BATCH_CLASS_ID |
| 30 | |
| 31 | const char* name() const override { return "Dummy Batch"; } |
| 32 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 33 | GrInitInvariantOutput* coverage, |
| 34 | GrBatchToXPOverrides* overrides) const override { |
| 35 | color->setUnknownFourComponents(); |
| 36 | coverage->setUnknownSingleComponent(); |
| 37 | } |
| 38 | |
| 39 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override {} |
| 40 | |
| 41 | Batch(int numAttribs) |
| 42 | : INHERITED(ClassID()) |
| 43 | , fNumAttribs(numAttribs) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 44 | this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private: |
| 48 | bool onCombineIfPossible(GrBatch*, const GrCaps&) override { return false; } |
| 49 | void onPrepareDraws(Target* target) const override { |
| 50 | class GP : public GrGeometryProcessor { |
| 51 | public: |
| 52 | GP(int numAttribs) { |
| 53 | this->initClassID<GP>(); |
| 54 | SkASSERT(numAttribs > 1); |
| 55 | for (auto i = 0; i < numAttribs; ++i) { |
| 56 | fAttribNames.push_back().printf("attr%d", i); |
| 57 | } |
| 58 | for (auto i = 0; i < numAttribs; ++i) { |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 59 | this->addVertexAttrib(fAttribNames[i].c_str(), kVec2f_GrVertexAttribType); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | const char* name() const override { return "Dummy GP"; } |
| 63 | |
| 64 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 65 | class GLSLGP : public GrGLSLGeometryProcessor { |
| 66 | public: |
| 67 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 68 | const GP& gp = args.fGP.cast<GP>(); |
| 69 | args.fVaryingHandler->emitAttributes(gp); |
| 70 | this->setupPosition(args.fVertBuilder, gpArgs, gp.fAttribs[0].fName); |
| 71 | } |
| 72 | void setData(const GrGLSLProgramDataManager& pdman, |
| 73 | const GrPrimitiveProcessor& primProc) override {} |
| 74 | }; |
| 75 | return new GLSLGP(); |
| 76 | } |
| 77 | void getGLSLProcessorKey(const GrGLSLCaps&, |
| 78 | GrProcessorKeyBuilder* builder) const override { |
| 79 | builder->add32(this->numAttribs()); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | SkTArray<SkString> fAttribNames; |
| 84 | }; |
| 85 | SkAutoTUnref<GrGeometryProcessor> gp(new GP(fNumAttribs)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 86 | QuadHelper helper; |
| 87 | size_t vertexStride = gp->getVertexStride(); |
| 88 | SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1)); |
| 89 | vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 90 | helper.recordDraw(target, gp); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | int fNumAttribs; |
| 94 | |
| 95 | typedef GrVertexBatch INHERITED; |
| 96 | }; |
| 97 | } |
| 98 | |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 99 | DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 100 | GrContext* context = ctxInfo.grContext(); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 101 | |
robertphillips | 6738c70 | 2016-07-27 12:13:51 -0700 | [diff] [blame] | 102 | sk_sp<GrDrawContext> drawContext(context->makeDrawContext(SkBackingFit::kApprox, |
| 103 | 1, 1, kRGBA_8888_GrPixelConfig, |
| 104 | nullptr)); |
robertphillips | 55fdccc | 2016-06-06 06:16:20 -0700 | [diff] [blame] | 105 | if (!drawContext) { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 106 | ERRORF(reporter, "Could not create draw context."); |
| 107 | return; |
| 108 | } |
| 109 | int attribCnt = context->caps()->maxVertexAttributes(); |
| 110 | if (!attribCnt) { |
| 111 | ERRORF(reporter, "No attributes allowed?!"); |
| 112 | return; |
| 113 | } |
| 114 | context->flush(); |
| 115 | context->resetGpuStats(); |
| 116 | #if GR_GPU_STATS |
| 117 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0); |
| 118 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0); |
| 119 | #endif |
| 120 | SkAutoTUnref<GrDrawBatch> batch; |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 121 | GrPaint grPaint; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 122 | // This one should succeed. |
| 123 | batch.reset(new Batch(attribCnt)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 124 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 125 | context->flush(); |
| 126 | #if GR_GPU_STATS |
| 127 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1); |
| 128 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0); |
| 129 | #endif |
| 130 | context->resetGpuStats(); |
| 131 | // This one should fail. |
| 132 | batch.reset(new Batch(attribCnt+1)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 133 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
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() == 0); |
| 137 | REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1); |
| 138 | #endif |
| 139 | } |
| 140 | #endif |