blob: 6402020458fad79a2ff64644d4c2ad6ddcba487a [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
Brian Osman9a725dd2017-09-20 09:53:22 -040041 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,\
42 GrPixelConfigIsClamped) override {
Brian Salomonb4b8a462017-07-13 15:29:47 -040043 return RequiresDstTexture::kNo;
Brian Salomonf8334782017-01-03 09:42:58 -050044 }
Brian Salomon09d994e2016-12-21 11:14:46 -050045
46private:
47 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070048 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070049 }
50
Brian Salomon25a88092016-12-01 09:36:50 -050051 bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; }
Brian Salomonb4b8a462017-07-13 15:29:47 -040052
Brian Salomon91326c32017-08-09 16:02:19 -040053 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070054 class GP : public GrGeometryProcessor {
55 public:
56 GP(int numAttribs) {
57 this->initClassID<GP>();
58 SkASSERT(numAttribs > 1);
59 for (auto i = 0; i < numAttribs; ++i) {
60 fAttribNames.push_back().printf("attr%d", i);
61 }
62 for (auto i = 0; i < numAttribs; ++i) {
bsalomon6cb807b2016-08-17 11:33:39 -070063 this->addVertexAttrib(fAttribNames[i].c_str(), kVec2f_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070064 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040065 }
bsalomon1d417a82016-03-23 11:50:26 -070066 const char* name() const override { return "Dummy GP"; }
67
Brian Salomon94efbf52016-11-29 13:43:05 -050068 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070069 class GLSLGP : public GrGLSLGeometryProcessor {
70 public:
71 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
72 const GP& gp = args.fGP.cast<GP>();
73 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon7f235432017-08-16 09:41:48 -040074 this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.getAttrib(0).fName);
ethannicholas22f939e2016-10-13 13:25:34 -070075 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040076 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
77 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070078 }
79 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070080 const GrPrimitiveProcessor& primProc,
81 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070082 };
83 return new GLSLGP();
84 }
Brian Salomon94efbf52016-11-29 13:43:05 -050085 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070086 GrProcessorKeyBuilder* builder) const override {
87 builder->add32(this->numAttribs());
88 }
89
90 private:
91 SkTArray<SkString> fAttribNames;
92 };
Hal Canary342b7ac2016-11-04 11:49:42 -040093 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070094 QuadHelper helper;
95 size_t vertexStride = gp->getVertexStride();
96 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
97 vertices->setRectFan(0.f, 0.f, 1.f, 1.f, vertexStride);
Brian Salomon91326c32017-08-09 16:02:19 -040098 helper.recordDraw(target, gp.get(),
Brian Salomonbfd18cd2017-08-09 16:27:09 -040099 target->makePipeline(0, GrProcessorSet::MakeEmptySet(),
100 target->detachAppliedClip()));
bsalomon1d417a82016-03-23 11:50:26 -0700101 }
102
103 int fNumAttribs;
104
Brian Salomonb4b8a462017-07-13 15:29:47 -0400105 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700106};
107}
108
egdanielb05df0f2016-06-27 07:15:20 -0700109DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700110 GrContext* context = ctxInfo.grContext();
robertphillipsd4c741e2016-04-28 09:55:15 -0700111
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400112 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400113 SkBackingFit::kApprox,
114 1, 1, kRGBA_8888_GrPixelConfig,
115 nullptr));
116 if (!renderTargetContext) {
117 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700118 return;
119 }
120 int attribCnt = context->caps()->maxVertexAttributes();
121 if (!attribCnt) {
122 ERRORF(reporter, "No attributes allowed?!");
123 return;
124 }
125 context->flush();
126 context->resetGpuStats();
127#if GR_GPU_STATS
128 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
129 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
130#endif
robertphillips28a838e2016-06-23 14:07:00 -0700131 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700132 // This one should succeed.
Brian Salomonb4b8a462017-07-13 15:29:47 -0400133 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700134 context->flush();
135#if GR_GPU_STATS
136 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1);
137 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
138#endif
139 context->resetGpuStats();
Brian Salomonb4b8a462017-07-13 15:29:47 -0400140 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700141 context->flush();
142#if GR_GPU_STATS
143 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
144 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1);
145#endif
146}
147#endif