blob: aeaa36f3e567ce37acd525f2680ba304f5595094 [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"
ethannicholas22f939e2016-10-13 13:25:34 -070022#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon1d417a82016-03-23 11:50:26 -070023#include "glsl/GrGLSLVarying.h"
24#include "batches/GrVertexBatch.h"
25#include "SkString.h"
26
27namespace {
28class Batch : public GrVertexBatch {
29public:
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 override {
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) {
bsalomon88cf17d2016-07-08 06:40:56 -070045 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070046 }
47
48private:
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) {
bsalomon6cb807b2016-08-17 11:33:39 -070060 this->addVertexAttrib(fAttribNames[i].c_str(), kVec2f_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070061 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040062 }
bsalomon1d417a82016-03-23 11:50:26 -070063 const char* name() const override { return "Dummy GP"; }
64
65 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override {
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);
ethannicholas22f939e2016-10-13 13:25:34 -070072 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
73 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputColor);
74 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070075 }
76 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070077 const GrPrimitiveProcessor& primProc,
78 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070079 };
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
egdanielb05df0f2016-06-27 07:15:20 -0700104DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700105 GrContext* context = ctxInfo.grContext();
robertphillipsd4c741e2016-04-28 09:55:15 -0700106
robertphillips6738c702016-07-27 12:13:51 -0700107 sk_sp<GrDrawContext> drawContext(context->makeDrawContext(SkBackingFit::kApprox,
108 1, 1, kRGBA_8888_GrPixelConfig,
109 nullptr));
robertphillips55fdccc2016-06-06 06:16:20 -0700110 if (!drawContext) {
bsalomon1d417a82016-03-23 11:50:26 -0700111 ERRORF(reporter, "Could not create draw context.");
112 return;
113 }
114 int attribCnt = context->caps()->maxVertexAttributes();
115 if (!attribCnt) {
116 ERRORF(reporter, "No attributes allowed?!");
117 return;
118 }
119 context->flush();
120 context->resetGpuStats();
121#if GR_GPU_STATS
122 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
123 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
124#endif
125 SkAutoTUnref<GrDrawBatch> batch;
robertphillips28a838e2016-06-23 14:07:00 -0700126 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700127 // This one should succeed.
128 batch.reset(new Batch(attribCnt));
robertphillips28a838e2016-06-23 14:07:00 -0700129 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
bsalomon1d417a82016-03-23 11:50:26 -0700130 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();
136 // This one should fail.
137 batch.reset(new Batch(attribCnt+1));
robertphillips28a838e2016-06-23 14:07:00 -0700138 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
bsalomon1d417a82016-03-23 11:50:26 -0700139 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