OLD | NEW |
(Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2016 Google Inc. |
| 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. |
| 7 */ |
| 8 |
| 9 // This is a GPU-backend specific test. It relies on static intializers to work |
| 10 |
| 11 #include "SkTypes.h" |
| 12 #include "Test.h" |
| 13 |
| 14 #if SK_SUPPORT_GPU |
| 15 #include "GrBatchFlushState.h" |
| 16 #include "GrDrawContext.h" |
| 17 #include "GrDrawContextPriv.h" |
| 18 #include "GrContext.h" |
| 19 #include "GrGeometryProcessor.h" |
| 20 #include "GrGpu.h" |
| 21 #include "GrTextureProvider.h" |
| 22 #include "glsl/GrGLSLGeometryProcessor.h" |
| 23 #include "glsl/GrGLSLVarying.h" |
| 24 #include "batches/GrVertexBatch.h" |
| 25 #include "SkString.h" |
| 26 |
| 27 namespace { |
| 28 class Batch : public GrVertexBatch { |
| 29 public: |
| 30 DEFINE_BATCH_CLASS_ID |
| 31 |
| 32 const char* name() const override { return "Dummy Batch"; } |
| 33 void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 34 GrInitInvariantOutput* coverage, |
| 35 GrBatchToXPOverrides* overrides) const ove
rride { |
| 36 color->setUnknownFourComponents(); |
| 37 coverage->setUnknownSingleComponent(); |
| 38 } |
| 39 |
| 40 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {} |
| 41 |
| 42 Batch(int numAttribs) |
| 43 : INHERITED(ClassID()) |
| 44 , fNumAttribs(numAttribs) { |
| 45 this->setBounds(SkRect::MakeWH(1.f, 1.f)); |
| 46 } |
| 47 |
| 48 private: |
| 49 bool onCombineIfPossible(GrBatch*, const GrCaps&) override { return false; } |
| 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) { |
| 60 Attribute attribute; |
| 61 attribute.fType = kVec2f_GrVertexAttribType; |
| 62 attribute.fName = fAttribNames[i].c_str(); |
| 63 attribute.fOffset = 2 * sizeof(float) * i; |
| 64 attribute.fPrecision = kDefault_GrSLPrecision; |
| 65 this->addVertexAttrib(attribute); |
| 66 } |
| 67 }; |
| 68 const char* name() const override { return "Dummy GP"; } |
| 69 |
| 70 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) cons
t override { |
| 71 class GLSLGP : public GrGLSLGeometryProcessor { |
| 72 public: |
| 73 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 74 const GP& gp = args.fGP.cast<GP>(); |
| 75 args.fVaryingHandler->emitAttributes(gp); |
| 76 this->setupPosition(args.fVertBuilder, gpArgs, gp.fAttri
bs[0].fName); |
| 77 } |
| 78 void setData(const GrGLSLProgramDataManager& pdman, |
| 79 const GrPrimitiveProcessor& primProc) override
{} |
| 80 }; |
| 81 return new GLSLGP(); |
| 82 } |
| 83 void getGLSLProcessorKey(const GrGLSLCaps&, |
| 84 GrProcessorKeyBuilder* builder) const overr
ide { |
| 85 builder->add32(this->numAttribs()); |
| 86 } |
| 87 |
| 88 private: |
| 89 SkTArray<SkString> fAttribNames; |
| 90 }; |
| 91 SkAutoTUnref<GrGeometryProcessor> gp(new GP(fNumAttribs)); |
| 92 target->initDraw(gp); |
| 93 QuadHelper helper; |
| 94 size_t vertexStride = gp->getVertexStride(); |
| 95 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, verte
xStride, 1)); |
| 96 vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride); |
| 97 helper.recordDraw(target); |
| 98 } |
| 99 |
| 100 int fNumAttribs; |
| 101 |
| 102 typedef GrVertexBatch INHERITED; |
| 103 }; |
| 104 } |
| 105 |
| 106 DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, context) { |
| 107 GrTextureDesc desc; |
| 108 desc.fHeight = 1; |
| 109 desc.fWidth = 1; |
| 110 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 111 desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 112 SkAutoTUnref<GrTexture> target(context->textureProvider()->createTexture(des
c, |
| 113 SkB
udgeted::kYes)); |
| 114 if (!target) { |
| 115 ERRORF(reporter, "Could not create render target."); |
| 116 return; |
| 117 } |
| 118 SkAutoTUnref<GrDrawContext> dc(context->drawContext(target->asRenderTarget()
)); |
| 119 if (!dc) { |
| 120 ERRORF(reporter, "Could not create draw context."); |
| 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 |
| 134 SkAutoTUnref<GrDrawBatch> batch; |
| 135 GrPipelineBuilder pb; |
| 136 pb.setRenderTarget(target->asRenderTarget()); |
| 137 // This one should succeed. |
| 138 batch.reset(new Batch(attribCnt)); |
| 139 dc->drawContextPriv().testingOnly_drawBatch(pb, batch); |
| 140 context->flush(); |
| 141 #if GR_GPU_STATS |
| 142 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1); |
| 143 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0)
; |
| 144 #endif |
| 145 context->resetGpuStats(); |
| 146 // This one should fail. |
| 147 batch.reset(new Batch(attribCnt+1)); |
| 148 dc->drawContextPriv().testingOnly_drawBatch(pb, batch); |
| 149 context->flush(); |
| 150 #if GR_GPU_STATS |
| 151 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0); |
| 152 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1)
; |
| 153 #endif |
| 154 } |
| 155 #endif |
OLD | NEW |