blob: c3b64d0d46ec6727dd4402ec950d0c5c00bd729f [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
Kevin Lubickbe03ef12021-06-16 15:28:00 -04008// This is a GPU-backend specific test. It relies on static initializers to work
bsalomon1d417a82016-03-23 11:50:26 -07009
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#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"
Robert Phillips4dca8312021-07-28 15:13:20 -040028#include "src/gpu/v1/SurfaceDrawContext_v1.h"
bsalomon1d417a82016-03-23 11:50:26 -070029
30namespace {
Brian Salomonb4b8a462017-07-13 15:29:47 -040031class Op : public GrMeshDrawOp {
bsalomon1d417a82016-03-23 11:50:26 -070032public:
Brian Salomon25a88092016-12-01 09:36:50 -050033 DEFINE_OP_CLASS_ID
bsalomon1d417a82016-03-23 11:50:26 -070034
Kevin Lubickbe03ef12021-06-16 15:28:00 -040035 const char* name() const override { return "Test Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070036
Herb Derbyc76d4092020-10-07 16:46:15 -040037 static GrOp::Owner Make(GrRecordingContext* rContext, int numAttribs) {
38 return GrOp::Make<Op>(rContext, numAttribs);
Brian Salomonb4b8a462017-07-13 15:29:47 -040039 }
40
41 FixedFunctionFlags fixedFunctionFlags() const override {
42 return FixedFunctionFlags::kNone;
43 }
44
Chris Dalton57ab06c2021-04-22 12:57:28 -060045 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070046 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf8334782017-01-03 09:42:58 -050047 }
Brian Salomon09d994e2016-12-21 11:14:46 -050048
49private:
Herb Derbyc76d4092020-10-07 16:46:15 -040050 friend class ::GrOp;
Robert Phillips7c525e62018-06-12 10:11:12 -040051
Brian Salomon09d994e2016-12-21 11:14:46 -050052 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
Greg Daniel5faf4742019-10-01 15:14:44 -040053 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070054 }
55
Robert Phillips2669a7b2020-03-12 12:07:19 -040056 GrProgramInfo* programInfo() override { return fProgramInfo; }
57
Robert Phillips6941f4a2020-03-12 09:41:54 -040058 void onCreateProgramInfo(const GrCaps* caps,
59 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -050060 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -060061 bool usesMSAASurface,
Robert Phillips6941f4a2020-03-12 09:41:54 -040062 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -040063 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050064 GrXferBarrierFlags renderPassXferBarriers,
65 GrLoadOp colorLoadOp) override {
bsalomon1d417a82016-03-23 11:50:26 -070066 class GP : public GrGeometryProcessor {
67 public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050068 static GrGeometryProcessor* Make(SkArenaAlloc* arena, int numAttribs) {
Mike Kleinf1241082020-12-14 15:59:09 -060069 return arena->make([&](void* ptr) {
70 return new (ptr) GP(numAttribs);
71 });
Mike Kleinfc6c37b2016-09-27 09:34:10 -040072 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050073
Kevin Lubickbe03ef12021-06-16 15:28:00 -040074 const char* name() const override { return "Test GP"; }
bsalomon1d417a82016-03-23 11:50:26 -070075
Brian Salomonf95940b2021-08-09 15:56:24 -040076 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
Brian Salomonbab2d112021-08-11 09:59:56 -040077 class Impl : public ProgramImpl {
bsalomon1d417a82016-03-23 11:50:26 -070078 public:
Brian Salomonbab2d112021-08-11 09:59:56 -040079 void setData(const GrGLSLProgramDataManager&,
80 const GrShaderCaps&,
81 const GrGeometryProcessor&) override {}
82
83 private:
bsalomon1d417a82016-03-23 11:50:26 -070084 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -040085 const GP& gp = args.fGeomProc.cast<GP>();
bsalomon1d417a82016-03-23 11:50:26 -070086 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon5a328282021-04-14 10:32:25 -040087 WriteOutputPosition(args.fVertBuilder, gpArgs, gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070088 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
John Stiles4d7ac492021-03-09 20:16:43 -050089 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputColor);
90 fragBuilder->codeAppendf("const half4 %s = half4(1);",
91 args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070092 }
bsalomon1d417a82016-03-23 11:50:26 -070093 };
Brian Salomonbab2d112021-08-11 09:59:56 -040094
95 return std::make_unique<Impl>();
bsalomon1d417a82016-03-23 11:50:26 -070096 }
Brian Salomon13b28732021-08-06 15:33:58 -040097 void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* builder) const override {
Brian Salomon92be2f72018-06-19 14:33:47 -040098 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -070099 }
100
101 private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500102 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
103 SkASSERT(numAttribs > 1);
John Stilesfbd050b2020-08-03 13:21:46 -0400104 fAttribNames = std::make_unique<SkString[]>(numAttribs);
105 fAttributes = std::make_unique<Attribute[]>(numAttribs);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500106 for (auto i = 0; i < numAttribs; ++i) {
107 fAttribNames[i].printf("attr%d", i);
108 // This gives us more of a mix of attribute types, and allows the
109 // component count to fit within the limits for iOS Metal.
110 if (i & 0x1) {
111 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
112 kFloat_GrSLType};
113 } else {
114 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
115 kFloat2_GrSLType};
116 }
117 }
118 this->setVertexAttributes(fAttributes.get(), numAttribs);
119 }
120
Brian Salomon92be2f72018-06-19 14:33:47 -0400121 int fNumAttribs;
122 std::unique_ptr<SkString[]> fAttribNames;
123 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400124
John Stiles7571f9e2020-09-02 22:42:33 -0400125 using INHERITED = GrGeometryProcessor;
bsalomon1d417a82016-03-23 11:50:26 -0700126 };
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500127
Robert Phillips6941f4a2020-03-12 09:41:54 -0400128 GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs);
129
130 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
131 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400132 writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600133 usesMSAASurface,
Robert Phillips6941f4a2020-03-12 09:41:54 -0400134 std::move(appliedClip),
135 dstProxyView,
136 gp,
137 GrProcessorSet::MakeEmptySet(),
138 GrPrimitiveType::kTriangles,
Greg Danield358cbe2020-09-11 09:33:54 -0400139 renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500140 colorLoadOp,
Robert Phillips6941f4a2020-03-12 09:41:54 -0400141 GrPipeline::InputFlags::kNone);
142 }
143
Robert Phillips71143952021-06-17 14:55:07 -0400144 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips6941f4a2020-03-12 09:41:54 -0400145 if (!fProgramInfo) {
146 this->createProgramInfo(target);
147 }
148
Robert Phillips787fd9d2021-03-22 14:48:09 -0400149 size_t vertexStride = fProgramInfo->geomProc().vertexStride();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000150 QuadHelper helper(target, vertexStride, 1);
151 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500152 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Robert Phillips6941f4a2020-03-12 09:41:54 -0400153 fMesh = helper.mesh();
Robert Phillips4133dc42020-03-11 15:55:55 -0400154 }
155
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700156 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips6941f4a2020-03-12 09:41:54 -0400157 if (!fProgramInfo || !fMesh) {
158 return;
159 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500160
Chris Dalton765ed362020-03-16 17:34:44 -0600161 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400162 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600163 flushState->drawMesh(*fMesh);
bsalomon1d417a82016-03-23 11:50:26 -0700164 }
165
Robert Phillips6941f4a2020-03-12 09:41:54 -0400166 int fNumAttribs;
Chris Daltoneb694b72020-03-16 09:25:50 -0600167 GrSimpleMesh* fMesh = nullptr;
Robert Phillips6941f4a2020-03-12 09:41:54 -0400168 GrProgramInfo* fProgramInfo = nullptr;
bsalomon1d417a82016-03-23 11:50:26 -0700169
John Stiles7571f9e2020-09-02 22:42:33 -0400170 using INHERITED = GrMeshDrawOp;
bsalomon1d417a82016-03-23 11:50:26 -0700171};
John Stilesa6841be2020-08-06 14:11:56 -0400172} // namespace
bsalomon1d417a82016-03-23 11:50:26 -0700173
egdanielb05df0f2016-06-27 07:15:20 -0700174DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400175 auto dContext = ctxInfo.directContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500176#if GR_GPU_STATS
Robert Phillips4dca8312021-07-28 15:13:20 -0400177 GrGpu* gpu = dContext->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500178#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700179
Robert Phillips4dca8312021-07-28 15:13:20 -0400180 auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext,
181 GrColorType::kRGBA_8888,
182 nullptr,
183 SkBackingFit::kApprox,
184 {1, 1},
185 SkSurfaceProps());
186 if (!sdc) {
Brian Osman11052242016-10-27 14:47:55 -0400187 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700188 return;
189 }
Robert Phillips4dca8312021-07-28 15:13:20 -0400190 int attribCnt = dContext->priv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700191 if (!attribCnt) {
192 ERRORF(reporter, "No attributes allowed?!");
193 return;
194 }
Robert Phillips4dca8312021-07-28 15:13:20 -0400195 dContext->flushAndSubmit();
196 dContext->priv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700197#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500198 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
199 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700200#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500201 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
Robert Phillips4dca8312021-07-28 15:13:20 -0400202 sdc->discard();
Greg Danielf44cb482018-02-27 14:26:32 -0500203
robertphillips28a838e2016-06-23 14:07:00 -0700204 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700205 // This one should succeed.
Robert Phillips4dca8312021-07-28 15:13:20 -0400206 sdc->addDrawOp(Op::Make(dContext, attribCnt));
207 dContext->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() == 1);
210 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700211#endif
Robert Phillips4dca8312021-07-28 15:13:20 -0400212 dContext->priv().resetGpuStats();
213 sdc->addDrawOp(Op::Make(dContext, attribCnt + 1));
214 dContext->flushAndSubmit();
bsalomon1d417a82016-03-23 11:50:26 -0700215#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500216 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
217 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700218#endif
219}