blob: 305202d9a2d9ce8d9d88cc6faac6999bdaf9bf59 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
11#include "tests/Test.h"
bsalomon1d417a82016-03-23 11:50:26 -070012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkString.h"
14#include "include/gpu/GrContext.h"
15#include "src/core/SkPointPriv.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGeometryProcessor.h"
18#include "src/gpu/GrGpu.h"
19#include "src/gpu/GrMemoryPool.h"
20#include "src/gpu/GrOpFlushState.h"
21#include "src/gpu/GrRenderTargetContext.h"
22#include "src/gpu/GrRenderTargetContextPriv.h"
23#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
24#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
25#include "src/gpu/glsl/GrGLSLVarying.h"
26#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050027#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
bsalomon1d417a82016-03-23 11:50:26 -070028
29namespace {
Brian Salomonb4b8a462017-07-13 15:29:47 -040030class Op : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070031public:
Brian Salomon25a88092016-12-01 09:36:50 -050032 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070033
Brian Salomon09d994e2016-12-21 11:14:46 -050034 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070035
Robert Phillips7c525e62018-06-12 10:11:12 -040036 static std::unique_ptr<GrDrawOp> Make(GrContext* context, int numAttribs) {
Robert Phillips9da87e02019-02-04 13:26:26 -050037 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040038
39 return pool->allocate<Op>(numAttribs);
Brian Salomonb4b8a462017-07-13 15:29:47 -040040 }
41
42 FixedFunctionFlags fixedFunctionFlags() const override {
43 return FixedFunctionFlags::kNone;
44 }
45
Chris Dalton6ce447a2019-06-23 18:07:38 -060046 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
47 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070048 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf8334782017-01-03 09:42:58 -050049 }
Brian Salomon09d994e2016-12-21 11:14:46 -050050
51private:
Robert Phillips7c525e62018-06-12 10:11:12 -040052 friend class ::GrOpMemoryPool;
53
Brian Salomon09d994e2016-12-21 11:14:46 -050054 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
Greg Daniel5faf4742019-10-01 15:14:44 -040055 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070056 }
57
Brian Salomon91326c32017-08-09 16:02:19 -040058 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070059 class GP : public GrGeometryProcessor {
60 public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050061 static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) {
62 return arena->make<GP>(numAttribs);
Mike Kleinfc6c37b2016-09-27 09:34:10 -040063 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050064
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);
Brian Salomon70132d02018-05-29 15:33:06 -040073 this->writeOutputPosition(args.fVertBuilder, gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -040074 gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070075 GrGLSLFPFragmentBuilder* 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,
Brian Salomonc241b582019-11-27 08:57:17 -050081 const CoordTransformRange&) 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 {
Brian Salomon92be2f72018-06-19 14:33:47 -040087 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -070088 }
89
90 private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050091 friend class ::SkArenaAlloc; // for access to ctor
92
93 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
94 SkASSERT(numAttribs > 1);
95 fAttribNames.reset(new SkString[numAttribs]);
96 fAttributes.reset(new Attribute[numAttribs]);
97 for (auto i = 0; i < numAttribs; ++i) {
98 fAttribNames[i].printf("attr%d", i);
99 // This gives us more of a mix of attribute types, and allows the
100 // component count to fit within the limits for iOS Metal.
101 if (i & 0x1) {
102 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
103 kFloat_GrSLType};
104 } else {
105 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
106 kFloat2_GrSLType};
107 }
108 }
109 this->setVertexAttributes(fAttributes.get(), numAttribs);
110 }
111
Brian Salomon92be2f72018-06-19 14:33:47 -0400112 int fNumAttribs;
113 std::unique_ptr<SkString[]> fAttribNames;
114 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400115
116 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700117 };
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500118
119 GrGeometryProcessor* gp = GP::Make(target->allocator(), fNumAttribs);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500120 size_t vertexStride = gp->vertexStride();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000121 QuadHelper helper(target, vertexStride, 1);
122 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500123 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500124 helper.recordDraw(target, gp);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700125 }
126
127 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500128 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
129 GrProcessorSet::MakeEmptySet(),
130 GrPipeline::InputFlags::kNone);
131
132 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
bsalomon1d417a82016-03-23 11:50:26 -0700133 }
134
135 int fNumAttribs;
136
Brian Salomonb4b8a462017-07-13 15:29:47 -0400137 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700138};
139}
140
egdanielb05df0f2016-06-27 07:15:20 -0700141DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700142 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500143#if GR_GPU_STATS
Robert Phillips9da87e02019-02-04 13:26:26 -0500144 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500145#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700146
Greg Daniele20fcad2020-01-08 11:52:34 -0500147 auto renderTargetContext = GrRenderTargetContext::Make(
148 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1});
Brian Osman11052242016-10-27 14:47:55 -0400149 if (!renderTargetContext) {
150 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700151 return;
152 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500153 int attribCnt = context->priv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700154 if (!attribCnt) {
155 ERRORF(reporter, "No attributes allowed?!");
156 return;
157 }
158 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500159 context->priv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700160#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500161 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
162 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700163#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500164 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
165 renderTargetContext->discard();
166
robertphillips28a838e2016-06-23 14:07:00 -0700167 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700168 // This one should succeed.
Robert Phillips7c525e62018-06-12 10:11:12 -0400169 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700170 context->flush();
171#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500172 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
173 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700174#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500175 context->priv().resetGpuStats();
Robert Phillips7c525e62018-06-12 10:11:12 -0400176 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700177 context->flush();
178#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500179 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
180 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700181#endif
182}