blob: d402e65f1cc3d45fa849397e9619762f73532d5a [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"
Cary Clark74f623d2017-11-06 20:02:02 -050020#include "SkPointPriv.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 Salomonb4b8a462017-07-13 15:29:47 -040028class Op : 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
Brian Salomon09d994e2016-12-21 11:14:46 -050032 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070033
Brian Salomonb4b8a462017-07-13 15:29:47 -040034 static std::unique_ptr<GrDrawOp> Make(int numAttribs) {
35 return std::unique_ptr<GrDrawOp>(new Op(numAttribs));
36 }
37
38 FixedFunctionFlags fixedFunctionFlags() const override {
39 return FixedFunctionFlags::kNone;
40 }
41
Brian Osman9a725dd2017-09-20 09:53:22 -040042 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,\
43 GrPixelConfigIsClamped) override {
Brian Salomonb4b8a462017-07-13 15:29:47 -040044 return RequiresDstTexture::kNo;
Brian Salomonf8334782017-01-03 09:42:58 -050045 }
Brian Salomon09d994e2016-12-21 11:14:46 -050046
47private:
48 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070049 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070050 }
51
Brian Salomon25a88092016-12-01 09:36:50 -050052 bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; }
Brian Salomonb4b8a462017-07-13 15:29:47 -040053
Brian Salomon91326c32017-08-09 16:02:19 -040054 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070055 class GP : public GrGeometryProcessor {
56 public:
Ethan Nicholasabff9562017-10-09 10:54:08 -040057 GP(int numAttribs)
58 : INHERITED(kGP_ClassID) {
bsalomon1d417a82016-03-23 11:50:26 -070059 SkASSERT(numAttribs > 1);
60 for (auto i = 0; i < numAttribs; ++i) {
61 fAttribNames.push_back().printf("attr%d", i);
62 }
63 for (auto i = 0; i < numAttribs; ++i) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040064 this->addVertexAttrib(fAttribNames[i].c_str(), kFloat2_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070065 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040066 }
bsalomon1d417a82016-03-23 11:50:26 -070067 const char* name() const override { return "Dummy GP"; }
68
Brian Salomon94efbf52016-11-29 13:43:05 -050069 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070070 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);
Brian Salomon7f235432017-08-16 09:41:48 -040075 this->writeOutputPosition(args.fVertBuilder, gpArgs, gp.getAttrib(0).fName);
Chris Dalton60283612018-02-14 13:38:14 -070076 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040077 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
78 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070079 }
80 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070081 const GrPrimitiveProcessor& primProc,
82 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070083 };
84 return new GLSLGP();
85 }
Brian Salomon94efbf52016-11-29 13:43:05 -050086 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070087 GrProcessorKeyBuilder* builder) const override {
88 builder->add32(this->numAttribs());
89 }
90
91 private:
92 SkTArray<SkString> fAttribNames;
Ethan Nicholasabff9562017-10-09 10:54:08 -040093
94 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -070095 };
Hal Canary342b7ac2016-11-04 11:49:42 -040096 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070097 QuadHelper helper;
98 size_t vertexStride = gp->getVertexStride();
99 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
Cary Clark74f623d2017-11-06 20:02:02 -0500100 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Brian Salomon91326c32017-08-09 16:02:19 -0400101 helper.recordDraw(target, gp.get(),
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400102 target->makePipeline(0, GrProcessorSet::MakeEmptySet(),
103 target->detachAppliedClip()));
bsalomon1d417a82016-03-23 11:50:26 -0700104 }
105
106 int fNumAttribs;
107
Brian Salomonb4b8a462017-07-13 15:29:47 -0400108 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700109};
110}
111
egdanielb05df0f2016-06-27 07:15:20 -0700112DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700113 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500114#if GR_GPU_STATS
115 GrGpu* gpu = context->contextPriv().getGpu();
116#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700117
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400118 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400119 SkBackingFit::kApprox,
120 1, 1, kRGBA_8888_GrPixelConfig,
121 nullptr));
122 if (!renderTargetContext) {
123 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700124 return;
125 }
126 int attribCnt = context->caps()->maxVertexAttributes();
127 if (!attribCnt) {
128 ERRORF(reporter, "No attributes allowed?!");
129 return;
130 }
131 context->flush();
132 context->resetGpuStats();
133#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500134 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
135 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700136#endif
robertphillips28a838e2016-06-23 14:07:00 -0700137 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700138 // This one should succeed.
Brian Salomonb4b8a462017-07-13 15:29:47 -0400139 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700140 context->flush();
141#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500142 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
143 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700144#endif
145 context->resetGpuStats();
Brian Salomonb4b8a462017-07-13 15:29:47 -0400146 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700147 context->flush();
148#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500149 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
150 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700151#endif
152}
153#endif