blob: 0c7c142151913d750dc90f5aff56f3bf88037d53 [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
bsalomon1d417a82016-03-23 11:50:26 -070014#include "GrContext.h"
15#include "GrGeometryProcessor.h"
16#include "GrGpu.h"
Brian Salomon89527432016-12-16 09:52:16 -050017#include "GrOpFlushState.h"
Brian Salomondad29232016-12-01 16:40:24 -050018#include "GrRenderTargetContext.h"
19#include "GrRenderTargetContextPriv.h"
bsalomon1d417a82016-03-23 11:50:26 -070020#include "GrTextureProvider.h"
bsalomon1d417a82016-03-23 11:50:26 -070021#include "SkString.h"
Brian Salomondad29232016-12-01 16:40:24 -050022#include "glsl/GrGLSLFragmentShaderBuilder.h"
23#include "glsl/GrGLSLGeometryProcessor.h"
24#include "glsl/GrGLSLVarying.h"
Brian Salomon89527432016-12-16 09:52:16 -050025#include "ops/GrMeshDrawOp.h"
bsalomon1d417a82016-03-23 11:50:26 -070026
27namespace {
Brian Salomondad29232016-12-01 16:40:24 -050028class Batch : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070029public:
Brian Salomon25a88092016-12-01 09:36:50 -050030 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070031
32 const char* name() const override { return "Dummy Batch"; }
bsalomon1d417a82016-03-23 11:50:26 -070033
34 Batch(int numAttribs)
35 : INHERITED(ClassID())
36 , fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070037 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070038 }
39
40private:
Brian Salomon92aee3d2016-12-21 09:20:25 -050041 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
42 input->pipelineColorInput()->setUnknownFourComponents();
43 input->pipelineCoverageInput()->setUnknownSingleComponent();
44 }
45
46 void applyPipelineOptimizations(const GrPipelineOptimizations&) override {}
Brian Salomon25a88092016-12-01 09:36:50 -050047 bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; }
bsalomon1d417a82016-03-23 11:50:26 -070048 void onPrepareDraws(Target* target) const override {
49 class GP : public GrGeometryProcessor {
50 public:
51 GP(int numAttribs) {
52 this->initClassID<GP>();
53 SkASSERT(numAttribs > 1);
54 for (auto i = 0; i < numAttribs; ++i) {
55 fAttribNames.push_back().printf("attr%d", i);
56 }
57 for (auto i = 0; i < numAttribs; ++i) {
bsalomon6cb807b2016-08-17 11:33:39 -070058 this->addVertexAttrib(fAttribNames[i].c_str(), kVec2f_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070059 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040060 }
bsalomon1d417a82016-03-23 11:50:26 -070061 const char* name() const override { return "Dummy GP"; }
62
Brian Salomon94efbf52016-11-29 13:43:05 -050063 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070064 class GLSLGP : public GrGLSLGeometryProcessor {
65 public:
66 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
67 const GP& gp = args.fGP.cast<GP>();
68 args.fVaryingHandler->emitAttributes(gp);
69 this->setupPosition(args.fVertBuilder, gpArgs, gp.fAttribs[0].fName);
ethannicholas22f939e2016-10-13 13:25:34 -070070 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
71 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputColor);
72 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070073 }
74 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070075 const GrPrimitiveProcessor& primProc,
76 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070077 };
78 return new GLSLGP();
79 }
Brian Salomon94efbf52016-11-29 13:43:05 -050080 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070081 GrProcessorKeyBuilder* builder) const override {
82 builder->add32(this->numAttribs());
83 }
84
85 private:
86 SkTArray<SkString> fAttribNames;
87 };
Hal Canary342b7ac2016-11-04 11:49:42 -040088 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070089 QuadHelper helper;
90 size_t vertexStride = gp->getVertexStride();
91 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
92 vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride);
Hal Canary342b7ac2016-11-04 11:49:42 -040093 helper.recordDraw(target, gp.get());
bsalomon1d417a82016-03-23 11:50:26 -070094 }
95
96 int fNumAttribs;
97
Brian Salomondad29232016-12-01 16:40:24 -050098 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -070099};
100}
101
egdanielb05df0f2016-06-27 07:15:20 -0700102DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700103 GrContext* context = ctxInfo.grContext();
robertphillipsd4c741e2016-04-28 09:55:15 -0700104
Brian Osman11052242016-10-27 14:47:55 -0400105 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext(
106 SkBackingFit::kApprox,
107 1, 1, kRGBA_8888_GrPixelConfig,
108 nullptr));
109 if (!renderTargetContext) {
110 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700111 return;
112 }
113 int attribCnt = context->caps()->maxVertexAttributes();
114 if (!attribCnt) {
115 ERRORF(reporter, "No attributes allowed?!");
116 return;
117 }
118 context->flush();
119 context->resetGpuStats();
120#if GR_GPU_STATS
121 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
122 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
123#endif
Brian Salomon1951f3d2016-12-09 16:07:12 -0500124 sk_sp<GrDrawOp> op;
robertphillips28a838e2016-06-23 14:07:00 -0700125 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700126 // This one should succeed.
Brian Salomon1951f3d2016-12-09 16:07:12 -0500127 op.reset(new Batch(attribCnt));
128 renderTargetContext->priv().testingOnly_addDrawOp(grPaint, GrAAType::kNone, std::move(op));
bsalomon1d417a82016-03-23 11:50:26 -0700129 context->flush();
130#if GR_GPU_STATS
131 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1);
132 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
133#endif
134 context->resetGpuStats();
135 // This one should fail.
Brian Salomon1951f3d2016-12-09 16:07:12 -0500136 op.reset(new Batch(attribCnt+1));
137 renderTargetContext->priv().testingOnly_addDrawOp(grPaint, GrAAType::kNone, std::move(op));
bsalomon1d417a82016-03-23 11:50:26 -0700138 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