blob: 75bdb834fdb6ebd8baa240c6208bc206a0907f48 [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 "SkString.h"
Brian Salomondad29232016-12-01 16:40:24 -050021#include "glsl/GrGLSLFragmentShaderBuilder.h"
22#include "glsl/GrGLSLGeometryProcessor.h"
23#include "glsl/GrGLSLVarying.h"
Brian Salomon89527432016-12-16 09:52:16 -050024#include "ops/GrMeshDrawOp.h"
bsalomon1d417a82016-03-23 11:50:26 -070025
26namespace {
Brian Salomonb4b8a462017-07-13 15:29:47 -040027class Op : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070028public:
Brian Salomon25a88092016-12-01 09:36:50 -050029 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070030
Brian Salomon09d994e2016-12-21 11:14:46 -050031 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070032
Brian Salomonb4b8a462017-07-13 15:29:47 -040033 static std::unique_ptr<GrDrawOp> Make(int numAttribs) {
34 return std::unique_ptr<GrDrawOp>(new Op(numAttribs));
35 }
36
37 FixedFunctionFlags fixedFunctionFlags() const override {
38 return FixedFunctionFlags::kNone;
39 }
40
41 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
42 return RequiresDstTexture::kNo;
Brian Salomonf8334782017-01-03 09:42:58 -050043 }
Brian Salomon09d994e2016-12-21 11:14:46 -050044
45private:
46 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070047 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070048 }
49
Brian Salomon25a88092016-12-01 09:36:50 -050050 bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; }
Brian Salomonb4b8a462017-07-13 15:29:47 -040051
bsalomon1d417a82016-03-23 11:50:26 -070052 void onPrepareDraws(Target* target) const override {
53 class GP : public GrGeometryProcessor {
54 public:
55 GP(int numAttribs) {
56 this->initClassID<GP>();
57 SkASSERT(numAttribs > 1);
58 for (auto i = 0; i < numAttribs; ++i) {
59 fAttribNames.push_back().printf("attr%d", i);
60 }
61 for (auto i = 0; i < numAttribs; ++i) {
bsalomon6cb807b2016-08-17 11:33:39 -070062 this->addVertexAttrib(fAttribNames[i].c_str(), kVec2f_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070063 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040064 }
bsalomon1d417a82016-03-23 11:50:26 -070065 const char* name() const override { return "Dummy GP"; }
66
Brian Salomon94efbf52016-11-29 13:43:05 -050067 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070068 class GLSLGP : public GrGLSLGeometryProcessor {
69 public:
70 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
71 const GP& gp = args.fGP.cast<GP>();
72 args.fVaryingHandler->emitAttributes(gp);
Chris Dalton1d616352017-05-31 12:51:23 -060073 this->setupPosition(args.fVertBuilder, gpArgs, gp.getAttrib(0).fName);
ethannicholas22f939e2016-10-13 13:25:34 -070074 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
75 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputColor);
76 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070077 }
78 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070079 const GrPrimitiveProcessor& primProc,
80 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070081 };
82 return new GLSLGP();
83 }
Brian Salomon94efbf52016-11-29 13:43:05 -050084 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070085 GrProcessorKeyBuilder* builder) const override {
86 builder->add32(this->numAttribs());
87 }
88
89 private:
90 SkTArray<SkString> fAttribNames;
91 };
Hal Canary342b7ac2016-11-04 11:49:42 -040092 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070093 QuadHelper helper;
94 size_t vertexStride = gp->getVertexStride();
95 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
96 vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride);
Brian Salomonb4b8a462017-07-13 15:29:47 -040097 helper.recordDraw(target, gp.get(), target->makePipeline(0, &GrProcessorSet::EmptySet()));
bsalomon1d417a82016-03-23 11:50:26 -070098 }
99
100 int fNumAttribs;
101
Brian Salomonb4b8a462017-07-13 15:29:47 -0400102 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700103};
104}
105
egdanielb05df0f2016-06-27 07:15:20 -0700106DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700107 GrContext* context = ctxInfo.grContext();
robertphillipsd4c741e2016-04-28 09:55:15 -0700108
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400109 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400110 SkBackingFit::kApprox,
111 1, 1, kRGBA_8888_GrPixelConfig,
112 nullptr));
113 if (!renderTargetContext) {
114 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700115 return;
116 }
117 int attribCnt = context->caps()->maxVertexAttributes();
118 if (!attribCnt) {
119 ERRORF(reporter, "No attributes allowed?!");
120 return;
121 }
122 context->flush();
123 context->resetGpuStats();
124#if GR_GPU_STATS
125 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
126 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
127#endif
robertphillips28a838e2016-06-23 14:07:00 -0700128 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700129 // This one should succeed.
Brian Salomonb4b8a462017-07-13 15:29:47 -0400130 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700131 context->flush();
132#if GR_GPU_STATS
133 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1);
134 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
135#endif
136 context->resetGpuStats();
Brian Salomonb4b8a462017-07-13 15:29:47 -0400137 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1));
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