blob: 0e95d2eb84e86d9d40f6fd36635924ecf42a9e45 [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040014#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Robert Phillips6941f4a2020-03-12 09:41:54 -040021#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrRenderTargetContext.h"
23#include "src/gpu/GrRenderTargetContextPriv.h"
24#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
25#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
26#include "src/gpu/glsl/GrGLSLVarying.h"
27#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips3968fcb2019-12-05 16:40:31 -050028#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.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
Brian Salomon09d994e2016-12-21 11:14:46 -050035 const char* name() const override { return "Dummy Op"; }
bsalomon1d417a82016-03-23 11:50:26 -070036
Robert Phillips7c525e62018-06-12 10:11:12 -040037 static std::unique_ptr<GrDrawOp> Make(GrContext* context, int numAttribs) {
Robert Phillips9da87e02019-02-04 13:26:26 -050038 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040039
40 return pool->allocate<Op>(numAttribs);
Brian Salomonb4b8a462017-07-13 15:29:47 -040041 }
42
43 FixedFunctionFlags fixedFunctionFlags() const override {
44 return FixedFunctionFlags::kNone;
45 }
46
Chris Dalton6ce447a2019-06-23 18:07:38 -060047 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
48 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070049 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf8334782017-01-03 09:42:58 -050050 }
Brian Salomon09d994e2016-12-21 11:14:46 -050051
52private:
Robert Phillips7c525e62018-06-12 10:11:12 -040053 friend class ::GrOpMemoryPool;
54
Brian Salomon09d994e2016-12-21 11:14:46 -050055 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
Greg Daniel5faf4742019-10-01 15:14:44 -040056 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070057 }
58
Robert Phillips2669a7b2020-03-12 12:07:19 -040059 GrProgramInfo* programInfo() override { return fProgramInfo; }
60
Robert Phillips6941f4a2020-03-12 09:41:54 -040061 void onCreateProgramInfo(const GrCaps* caps,
62 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -040063 const GrSurfaceProxyView* writeView,
Robert Phillips6941f4a2020-03-12 09:41:54 -040064 GrAppliedClip&& appliedClip,
65 const GrXferProcessor::DstProxyView& dstProxyView) 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) {
69 return arena->make<GP>(numAttribs);
Mike Kleinfc6c37b2016-09-27 09:34:10 -040070 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050071
bsalomon1d417a82016-03-23 11:50:26 -070072 const char* name() const override { return "Dummy GP"; }
73
Brian Salomon94efbf52016-11-29 13:43:05 -050074 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070075 class GLSLGP : public GrGLSLGeometryProcessor {
76 public:
77 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
78 const GP& gp = args.fGP.cast<GP>();
79 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon70132d02018-05-29 15:33:06 -040080 this->writeOutputPosition(args.fVertBuilder, gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -040081 gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070082 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040083 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
84 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070085 }
86 void setData(const GrGLSLProgramDataManager& pdman,
Brian Osman609f1592020-07-01 15:14:39 -040087 const GrPrimitiveProcessor& primProc) override {}
bsalomon1d417a82016-03-23 11:50:26 -070088 };
89 return new GLSLGP();
90 }
Brian Salomon94efbf52016-11-29 13:43:05 -050091 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070092 GrProcessorKeyBuilder* builder) const override {
Brian Salomon92be2f72018-06-19 14:33:47 -040093 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -070094 }
95
96 private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050097 friend class ::SkArenaAlloc; // for access to ctor
98
99 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
100 SkASSERT(numAttribs > 1);
101 fAttribNames.reset(new SkString[numAttribs]);
102 fAttributes.reset(new Attribute[numAttribs]);
103 for (auto i = 0; i < numAttribs; ++i) {
104 fAttribNames[i].printf("attr%d", i);
105 // This gives us more of a mix of attribute types, and allows the
106 // component count to fit within the limits for iOS Metal.
107 if (i & 0x1) {
108 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
109 kFloat_GrSLType};
110 } else {
111 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
112 kFloat2_GrSLType};
113 }
114 }
115 this->setVertexAttributes(fAttributes.get(), numAttribs);
116 }
117
Brian Salomon92be2f72018-06-19 14:33:47 -0400118 int fNumAttribs;
119 std::unique_ptr<SkString[]> fAttribNames;
120 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400121
122 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700123 };
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500124
Robert Phillips6941f4a2020-03-12 09:41:54 -0400125 GrGeometryProcessor* gp = GP::Make(arena, fNumAttribs);
126
127 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps,
128 arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400129 writeView,
Robert Phillips6941f4a2020-03-12 09:41:54 -0400130 std::move(appliedClip),
131 dstProxyView,
132 gp,
133 GrProcessorSet::MakeEmptySet(),
134 GrPrimitiveType::kTriangles,
135 GrPipeline::InputFlags::kNone);
136 }
137
Robert Phillips6941f4a2020-03-12 09:41:54 -0400138 void onPrepareDraws(Target* target) override {
139 if (!fProgramInfo) {
140 this->createProgramInfo(target);
141 }
142
143 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000144 QuadHelper helper(target, vertexStride, 1);
145 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500146 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Robert Phillips6941f4a2020-03-12 09:41:54 -0400147 fMesh = helper.mesh();
Robert Phillips4133dc42020-03-11 15:55:55 -0400148 }
149
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700150 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips6941f4a2020-03-12 09:41:54 -0400151 if (!fProgramInfo || !fMesh) {
152 return;
153 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500154
Chris Dalton765ed362020-03-16 17:34:44 -0600155 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
156 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
157 flushState->drawMesh(*fMesh);
bsalomon1d417a82016-03-23 11:50:26 -0700158 }
159
Robert Phillips6941f4a2020-03-12 09:41:54 -0400160 int fNumAttribs;
Chris Daltoneb694b72020-03-16 09:25:50 -0600161 GrSimpleMesh* fMesh = nullptr;
Robert Phillips6941f4a2020-03-12 09:41:54 -0400162 GrProgramInfo* fProgramInfo = nullptr;
bsalomon1d417a82016-03-23 11:50:26 -0700163
Brian Salomonb4b8a462017-07-13 15:29:47 -0400164 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700165};
166}
167
egdanielb05df0f2016-06-27 07:15:20 -0700168DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400169 auto context = ctxInfo.directContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500170#if GR_GPU_STATS
Robert Phillips9da87e02019-02-04 13:26:26 -0500171 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500172#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700173
Greg Daniele20fcad2020-01-08 11:52:34 -0500174 auto renderTargetContext = GrRenderTargetContext::Make(
175 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1});
Brian Osman11052242016-10-27 14:47:55 -0400176 if (!renderTargetContext) {
177 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700178 return;
179 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500180 int attribCnt = context->priv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700181 if (!attribCnt) {
182 ERRORF(reporter, "No attributes allowed?!");
183 return;
184 }
Greg Daniel0a2464f2020-05-14 15:45:44 -0400185 context->flushAndSubmit();
Robert Phillips9da87e02019-02-04 13:26:26 -0500186 context->priv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700187#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500188 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
189 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700190#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500191 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
192 renderTargetContext->discard();
193
robertphillips28a838e2016-06-23 14:07:00 -0700194 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700195 // This one should succeed.
Robert Phillips7c525e62018-06-12 10:11:12 -0400196 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400197 context->flushAndSubmit();
bsalomon1d417a82016-03-23 11:50:26 -0700198#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500199 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
200 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700201#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500202 context->priv().resetGpuStats();
Robert Phillips7c525e62018-06-12 10:11:12 -0400203 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1));
Greg Daniel0a2464f2020-05-14 15:45:44 -0400204 context->flushAndSubmit();
bsalomon1d417a82016-03-23 11:50:26 -0700205#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500206 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
207 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700208#endif
209}