blob: b2b00b119c4feb9596b430e7cd524123f2b3ed21 [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
John Stilesfbd050b2020-08-03 13:21:46 -040010#include <memory>
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkTypes.h"
13#include "tests/Test.h"
bsalomon1d417a82016-03-23 11:50:26 -070014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkString.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040016#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkPointPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040018#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrGeometryProcessor.h"
20#include "src/gpu/GrGpu.h"
21#include "src/gpu/GrMemoryPool.h"
22#include "src/gpu/GrOpFlushState.h"
Robert Phillips6941f4a2020-03-12 09:41:54 -040023#include "src/gpu/GrProgramInfo.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050024#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
26#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
27#include "src/gpu/glsl/GrGLSLVarying.h"
28#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050029#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
bsalomon1d417a82016-03-23 11:50:26 -070030
31namespace {
Brian Salomonb4b8a462017-07-13 15:29:47 -040032class Op : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070033public:
Brian Salomon25a88092016-12-01 09:36:50 -050034 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070035
Brian Salomon09d994e2016-12-21 11:14:46 -050036 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070037
Herb Derbyc76d4092020-10-07 16:46:15 -040038 static GrOp::Owner Make(GrRecordingContext* rContext, int numAttribs) {
39 return GrOp::Make<Op>(rContext, 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:
Herb Derbyc76d4092020-10-07 16:46:15 -040052 friend class ::GrOp;
Robert Phillips7c525e62018-06-12 10:11:12 -040053
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
Robert Phillips2669a7b2020-03-12 12:07:19 -040058 GrProgramInfo* programInfo() override { return fProgramInfo; }
59
Robert Phillips6941f4a2020-03-12 09:41:54 -040060 void onCreateProgramInfo(const GrCaps* caps,
61 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -050062 const GrSurfaceProxyView& writeView,
Robert Phillips6941f4a2020-03-12 09:41:54 -040063 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -040064 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050065 GrXferBarrierFlags renderPassXferBarriers,
66 GrLoadOp colorLoadOp) override {
bsalomon1d417a82016-03-23 11:50:26 -070067 class GP : public GrGeometryProcessor {
68 public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050069 static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) {
Mike Kleinf1241082020-12-14 15:59:09 -060070 return arena->make([&](void* ptr) {
71 return new (ptr) GP(numAttribs);
72 });
Mike Kleinfc6c37b2016-09-27 09:34:10 -040073 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050074
bsalomon1d417a82016-03-23 11:50:26 -070075 const char* name() const override { return "Dummy GP"; }
76
Brian Salomon94efbf52016-11-29 13:43:05 -050077 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070078 class GLSLGP : public GrGLSLGeometryProcessor {
79 public:
80 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
81 const GP& gp = args.fGP.cast<GP>();
82 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon70132d02018-05-29 15:33:06 -040083 this->writeOutputPosition(args.fVertBuilder, gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -040084 gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070085 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040086 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
87 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070088 }
89 void setData(const GrGLSLProgramDataManager& pdman,
Brian Osman609f1592020-07-01 15:14:39 -040090 const GrPrimitiveProcessor& primProc) override {}
bsalomon1d417a82016-03-23 11:50:26 -070091 };
92 return new GLSLGP();
93 }
Brian Salomon94efbf52016-11-29 13:43:05 -050094 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070095 GrProcessorKeyBuilder* builder) const override {
Brian Salomon92be2f72018-06-19 14:33:47 -040096 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -070097 }
98
99 private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500100 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
101 SkASSERT(numAttribs > 1);
John Stilesfbd050b2020-08-03 13:21:46 -0400102 fAttribNames = std::make_unique<SkString[]>(numAttribs);
103 fAttributes = std::make_unique<Attribute[]>(numAttribs);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500104 for (auto i = 0; i < numAttribs; ++i) {
105 fAttribNames[i].printf("attr%d", i);
106 // This gives us more of a mix of attribute types, and allows the
107 // component count to fit within the limits for iOS Metal.
108 if (i & 0x1) {
109 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
110 kFloat_GrSLType};
111 } else {
112 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
113 kFloat2_GrSLType};
114 }
115 }
116 this->setVertexAttributes(fAttributes.get(), numAttribs);
117 }
118
Brian Salomon92be2f72018-06-19 14:33:47 -0400119 int fNumAttribs;
120 std::unique_ptr<SkString[]> fAttribNames;
121 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400122
John Stiles7571f9e2020-09-02 22:42:33 -0400123 using INHERITED = GrGeometryProcessor;
bsalomon1d417a82016-03-23 11:50:26 -0700124 };
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500125
Robert Phillips6941f4a2020-03-12 09:41:54 -0400126 GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs);
127
128 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
129 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400130 writeView,
Robert Phillips6941f4a2020-03-12 09:41:54 -0400131 std::move(appliedClip),
132 dstProxyView,
133 gp,
134 GrProcessorSet::MakeEmptySet(),
135 GrPrimitiveType::kTriangles,
Greg Danield358cbe2020-09-11 09:33:54 -0400136 renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500137 colorLoadOp,
Robert Phillips6941f4a2020-03-12 09:41:54 -0400138 GrPipeline::InputFlags::kNone);
139 }
140
Robert Phillips6941f4a2020-03-12 09:41:54 -0400141 void onPrepareDraws(Target* target) override {
142 if (!fProgramInfo) {
143 this->createProgramInfo(target);
144 }
145
146 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000147 QuadHelper helper(target, vertexStride, 1);
148 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500149 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Robert Phillips6941f4a2020-03-12 09:41:54 -0400150 fMesh = helper.mesh();
Robert Phillips4133dc42020-03-11 15:55:55 -0400151 }
152
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700153 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips6941f4a2020-03-12 09:41:54 -0400154 if (!fProgramInfo || !fMesh) {
155 return;
156 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500157
Chris Dalton765ed362020-03-16 17:34:44 -0600158 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
159 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
160 flushState->drawMesh(*fMesh);
bsalomon1d417a82016-03-23 11:50:26 -0700161 }
162
Robert Phillips6941f4a2020-03-12 09:41:54 -0400163 int fNumAttribs;
Chris Daltoneb694b72020-03-16 09:25:50 -0600164 GrSimpleMesh* fMesh = nullptr;
Robert Phillips6941f4a2020-03-12 09:41:54 -0400165 GrProgramInfo* fProgramInfo = nullptr;
bsalomon1d417a82016-03-23 11:50:26 -0700166
John Stiles7571f9e2020-09-02 22:42:33 -0400167 using INHERITED = GrMeshDrawOp;
bsalomon1d417a82016-03-23 11:50:26 -0700168};
John Stilesa6841be2020-08-06 14:11:56 -0400169} // namespace
bsalomon1d417a82016-03-23 11:50:26 -0700170
egdanielb05df0f2016-06-27 07:15:20 -0700171DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400172 auto context = ctxInfo.directContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500173#if GR_GPU_STATS
Robert Phillips9da87e02019-02-04 13:26:26 -0500174 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500175#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700176
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500177 auto surfaceDrawContext = GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500178 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1});
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500179 if (!surfaceDrawContext) {
Brian Osman11052242016-10-27 14:47:55 -0400180 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700181 return;
182 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500183 int attribCnt = context->priv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700184 if (!attribCnt) {
185 ERRORF(reporter, "No attributes allowed?!");
186 return;
187 }
Greg Daniel0a2464f2020-05-14 15:45:44 -0400188 context->flushAndSubmit();
Robert Phillips9da87e02019-02-04 13:26:26 -0500189 context->priv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700190#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500191 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
192 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700193#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500194 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500195 surfaceDrawContext->discard();
Greg Danielf44cb482018-02-27 14:26:32 -0500196
robertphillips28a838e2016-06-23 14:07:00 -0700197 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700198 // This one should succeed.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500199 surfaceDrawContext->addDrawOp(Op::Make(context, attribCnt));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400200 context->flushAndSubmit();
bsalomon1d417a82016-03-23 11:50:26 -0700201#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500202 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
203 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700204#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500205 context->priv().resetGpuStats();
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500206 surfaceDrawContext->addDrawOp(Op::Make(context, attribCnt + 1));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400207 context->flushAndSubmit();
bsalomon1d417a82016-03-23 11:50:26 -0700208#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500209 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
210 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700211#endif
212}