blob: b8b9e6f3ce2ad37b0dd3e08d9eda0e9833410c57 [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);
ethannicholas22f939e2016-10-13 13:25:34 -070076 GrGLSLPPFragmentBuilder* 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();
robertphillipsd4c741e2016-04-28 09:55:15 -0700114
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400115 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400116 SkBackingFit::kApprox,
117 1, 1, kRGBA_8888_GrPixelConfig,
118 nullptr));
119 if (!renderTargetContext) {
120 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700121 return;
122 }
123 int attribCnt = context->caps()->maxVertexAttributes();
124 if (!attribCnt) {
125 ERRORF(reporter, "No attributes allowed?!");
126 return;
127 }
128 context->flush();
129 context->resetGpuStats();
130#if GR_GPU_STATS
131 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
132 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
133#endif
robertphillips28a838e2016-06-23 14:07:00 -0700134 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700135 // This one should succeed.
Brian Salomonb4b8a462017-07-13 15:29:47 -0400136 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700137 context->flush();
138#if GR_GPU_STATS
139 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 1);
140 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 0);
141#endif
142 context->resetGpuStats();
Brian Salomonb4b8a462017-07-13 15:29:47 -0400143 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700144 context->flush();
145#if GR_GPU_STATS
146 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numDraws() == 0);
147 REPORTER_ASSERT(reporter, context->getGpu()->stats()->numFailedDraws() == 1);
148#endif
149}
150#endif