blob: a6f24848c6ed7ca4775218c1096c0fc11487468f [file] [log] [blame]
bsalomon1d417a82016-03-23 11:50:26 -07001/*
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
26namespace {
27class Batch : public GrVertexBatch {
28public:
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) {
44 this->setBounds(SkRect::MakeWH(1.f, 1.f));
45 }
46
47private:
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) {
59 Attribute attribute;
60 attribute.fType = kVec2f_GrVertexAttribType;
61 attribute.fName = fAttribNames[i].c_str();
62 attribute.fOffset = 2 * sizeof(float) * i;
63 attribute.fPrecision = kDefault_GrSLPrecision;
64 this->addVertexAttrib(attribute);
65 }
66 };
67 const char* name() const override { return "Dummy GP"; }
68
69 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
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);
75 this->setupPosition(args.fVertBuilder, gpArgs, gp.fAttribs[0].fName);
76 }
77 void setData(const GrGLSLProgramDataManager& pdman,
78 const GrPrimitiveProcessor& primProc) override {}
79 };
80 return new GLSLGP();
81 }
82 void getGLSLProcessorKey(const GrGLSLCaps&,
83 GrProcessorKeyBuilder* builder) const override {
84 builder->add32(this->numAttribs());
85 }
86
87 private:
88 SkTArray<SkString> fAttribNames;
89 };
90 SkAutoTUnref<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070091 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);
bsalomon342bfc22016-04-01 06:06:20 -070095 helper.recordDraw(target, gp);
bsalomon1d417a82016-03-23 11:50:26 -070096 }
97
98 int fNumAttribs;
99
100 typedef GrVertexBatch INHERITED;
101};
102}
103
104DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, context) {
105 GrTextureDesc desc;
106 desc.fHeight = 1;
107 desc.fWidth = 1;
108 desc.fFlags = kRenderTarget_GrSurfaceFlag;
109 desc.fConfig = kRGBA_8888_GrPixelConfig;
110 SkAutoTUnref<GrTexture> target(context->textureProvider()->createTexture(desc,
111 SkBudgeted::kYes));
112 if (!target) {
113 ERRORF(reporter, "Could not create render target.");
114 return;
115 }
116 SkAutoTUnref<GrDrawContext> dc(context->drawContext(target->asRenderTarget()));
117 if (!dc) {
118 ERRORF(reporter, "Could not create draw context.");
119 return;
120 }
121 int attribCnt = context->caps()->maxVertexAttributes();
122 if (!attribCnt) {
123 ERRORF(reporter, "No attributes allowed?!");
124 return;
125 }
126 context->flush();
127 context->resetGpuStats();
128#if GR_GPU_STATS
129 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
130 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
131#endif
132 SkAutoTUnref<GrDrawBatch> batch;
133 GrPipelineBuilder pb;
134 pb.setRenderTarget(target->asRenderTarget());
135 // This one should succeed.
136 batch.reset(new Batch(attribCnt));
137 dc->drawContextPriv().testingOnly_drawBatch(pb, batch);
138 context->flush();
139#if GR_GPU_STATS
140 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1);
141 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
142#endif
143 context->resetGpuStats();
144 // This one should fail.
145 batch.reset(new Batch(attribCnt+1));
146 dc->drawContextPriv().testingOnly_drawBatch(pb, batch);
147 context->flush();
148#if GR_GPU_STATS
149 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
150 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1);
151#endif
152}
153#endif