blob: 95855e0401c473f57f1b79bf77cc99da17134c40 [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"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "GrContextPriv.h"
bsalomon1d417a82016-03-23 11:50:26 -070016#include "GrGeometryProcessor.h"
17#include "GrGpu.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "GrOpFlushState.h"
Brian Salomondad29232016-12-01 16:40:24 -050019#include "GrRenderTargetContext.h"
20#include "GrRenderTargetContextPriv.h"
Cary Clark74f623d2017-11-06 20:02:02 -050021#include "SkPointPriv.h"
bsalomon1d417a82016-03-23 11:50:26 -070022#include "SkString.h"
Brian Salomondad29232016-12-01 16:40:24 -050023#include "glsl/GrGLSLFragmentShaderBuilder.h"
24#include "glsl/GrGLSLGeometryProcessor.h"
25#include "glsl/GrGLSLVarying.h"
Brian Salomon89527432016-12-16 09:52:16 -050026#include "ops/GrMeshDrawOp.h"
bsalomon1d417a82016-03-23 11:50:26 -070027
28namespace {
Brian Salomonb4b8a462017-07-13 15:29:47 -040029class Op : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070030public:
Brian Salomon25a88092016-12-01 09:36:50 -050031 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070032
Brian Salomon09d994e2016-12-21 11:14:46 -050033 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070034
Brian Salomonb4b8a462017-07-13 15:29:47 -040035 static std::unique_ptr<GrDrawOp> Make(int numAttribs) {
36 return std::unique_ptr<GrDrawOp>(new Op(numAttribs));
37 }
38
39 FixedFunctionFlags fixedFunctionFlags() const override {
40 return FixedFunctionFlags::kNone;
41 }
42
Brian Osman9a725dd2017-09-20 09:53:22 -040043 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,\
44 GrPixelConfigIsClamped) override {
Brian Salomonb4b8a462017-07-13 15:29:47 -040045 return RequiresDstTexture::kNo;
Brian Salomonf8334782017-01-03 09:42:58 -050046 }
Brian Salomon09d994e2016-12-21 11:14:46 -050047
48private:
49 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070050 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070051 }
52
Brian Salomon25a88092016-12-01 09:36:50 -050053 bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; }
Brian Salomonb4b8a462017-07-13 15:29:47 -040054
Brian Salomon91326c32017-08-09 16:02:19 -040055 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070056 class GP : public GrGeometryProcessor {
57 public:
Ethan Nicholasabff9562017-10-09 10:54:08 -040058 GP(int numAttribs)
59 : INHERITED(kGP_ClassID) {
bsalomon1d417a82016-03-23 11:50:26 -070060 SkASSERT(numAttribs > 1);
61 for (auto i = 0; i < numAttribs; ++i) {
62 fAttribNames.push_back().printf("attr%d", i);
63 }
64 for (auto i = 0; i < numAttribs; ++i) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -040065 this->addVertexAttrib(fAttribNames[i].c_str(), kFloat2_GrVertexAttribType);
bsalomon1d417a82016-03-23 11:50:26 -070066 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040067 }
bsalomon1d417a82016-03-23 11:50:26 -070068 const char* name() const override { return "Dummy GP"; }
69
Brian Salomon94efbf52016-11-29 13:43:05 -050070 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070071 class GLSLGP : public GrGLSLGeometryProcessor {
72 public:
73 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
74 const GP& gp = args.fGP.cast<GP>();
75 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon70132d02018-05-29 15:33:06 -040076 this->writeOutputPosition(args.fVertBuilder, gpArgs,
77 gp.getAttrib(0).name());
Chris Dalton60283612018-02-14 13:38:14 -070078 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040079 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
80 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070081 }
82 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070083 const GrPrimitiveProcessor& primProc,
84 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070085 };
86 return new GLSLGP();
87 }
Brian Salomon94efbf52016-11-29 13:43:05 -050088 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070089 GrProcessorKeyBuilder* builder) const override {
90 builder->add32(this->numAttribs());
91 }
92
93 private:
94 SkTArray<SkString> fAttribNames;
Ethan Nicholasabff9562017-10-09 10:54:08 -040095
96 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -070097 };
Hal Canary342b7ac2016-11-04 11:49:42 -040098 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
bsalomon1d417a82016-03-23 11:50:26 -070099 QuadHelper helper;
100 size_t vertexStride = gp->getVertexStride();
101 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.init(target, vertexStride, 1));
Cary Clark74f623d2017-11-06 20:02:02 -0500102 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Brian Salomon91326c32017-08-09 16:02:19 -0400103 helper.recordDraw(target, gp.get(),
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400104 target->makePipeline(0, GrProcessorSet::MakeEmptySet(),
105 target->detachAppliedClip()));
bsalomon1d417a82016-03-23 11:50:26 -0700106 }
107
108 int fNumAttribs;
109
Brian Salomonb4b8a462017-07-13 15:29:47 -0400110 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700111};
112}
113
egdanielb05df0f2016-06-27 07:15:20 -0700114DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700115 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500116#if GR_GPU_STATS
117 GrGpu* gpu = context->contextPriv().getGpu();
118#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700119
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500120 sk_sp<GrRenderTargetContext> renderTargetContext(
121 context->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kApprox,
122 1, 1, kRGBA_8888_GrPixelConfig,
123 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400124 if (!renderTargetContext) {
125 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700126 return;
127 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400128 int attribCnt = context->contextPriv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700129 if (!attribCnt) {
130 ERRORF(reporter, "No attributes allowed?!");
131 return;
132 }
133 context->flush();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500134 context->contextPriv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700135#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500136 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
137 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700138#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500139 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
140 renderTargetContext->discard();
141
robertphillips28a838e2016-06-23 14:07:00 -0700142 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700143 // This one should succeed.
Brian Salomonb4b8a462017-07-13 15:29:47 -0400144 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700145 context->flush();
146#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500147 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
148 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700149#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500150 context->contextPriv().resetGpuStats();
Brian Salomonb4b8a462017-07-13 15:29:47 -0400151 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700152 context->flush();
153#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500154 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
155 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700156#endif
157}
158#endif