blob: 02fc02b75cd23a7c858ee24ec0b724216c1987de [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"
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
Robert Phillips7c525e62018-06-12 10:11:12 -040035 static std::unique_ptr<GrDrawOp> Make(GrContext* context, int numAttribs) {
Robert Phillips9da87e02019-02-04 13:26:26 -050036 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040037
38 return pool->allocate<Op>(numAttribs);
Brian Salomonb4b8a462017-07-13 15:29:47 -040039 }
40
41 FixedFunctionFlags fixedFunctionFlags() const override {
42 return FixedFunctionFlags::kNone;
43 }
44
Chris Dalton6ce447a2019-06-23 18:07:38 -060045 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
46 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -070047 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf8334782017-01-03 09:42:58 -050048 }
Brian Salomon09d994e2016-12-21 11:14:46 -050049
50private:
Robert Phillips7c525e62018-06-12 10:11:12 -040051 friend class ::GrOpMemoryPool;
52
Brian Salomon09d994e2016-12-21 11:14:46 -050053 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
Greg Daniel5faf4742019-10-01 15:14:44 -040054 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsHairline::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070055 }
56
Brian Salomon91326c32017-08-09 16:02:19 -040057 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070058 class GP : public GrGeometryProcessor {
59 public:
Brian Salomon92be2f72018-06-19 14:33:47 -040060 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
bsalomon1d417a82016-03-23 11:50:26 -070061 SkASSERT(numAttribs > 1);
Brian Salomon92be2f72018-06-19 14:33:47 -040062 fAttribNames.reset(new SkString[numAttribs]);
63 fAttributes.reset(new Attribute[numAttribs]);
bsalomon1d417a82016-03-23 11:50:26 -070064 for (auto i = 0; i < numAttribs; ++i) {
Brian Salomon92be2f72018-06-19 14:33:47 -040065 fAttribNames[i].printf("attr%d", i);
Jim Van Vertha3407ab2019-03-15 15:22:39 -040066 // This gives us more of a mix of attribute types, and allows the
67 // component count to fit within the limits for iOS Metal.
68 if (i & 0x1) {
69 fAttributes[i] = {fAttribNames[i].c_str(), kFloat_GrVertexAttribType,
70 kFloat_GrSLType};
71 } else {
72 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType,
73 kFloat2_GrSLType};
74 }
bsalomon1d417a82016-03-23 11:50:26 -070075 }
Brian Osmanf04fb3c2018-11-12 15:34:00 -050076 this->setVertexAttributes(fAttributes.get(), numAttribs);
Mike Kleinfc6c37b2016-09-27 09:34:10 -040077 }
bsalomon1d417a82016-03-23 11:50:26 -070078 const char* name() const override { return "Dummy GP"; }
79
Brian Salomon94efbf52016-11-29 13:43:05 -050080 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070081 class GLSLGP : public GrGLSLGeometryProcessor {
82 public:
83 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
84 const GP& gp = args.fGP.cast<GP>();
85 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon70132d02018-05-29 15:33:06 -040086 this->writeOutputPosition(args.fVertBuilder, gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -040087 gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070088 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040089 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
90 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070091 }
92 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070093 const GrPrimitiveProcessor& primProc,
94 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070095 };
96 return new GLSLGP();
97 }
Brian Salomon94efbf52016-11-29 13:43:05 -050098 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070099 GrProcessorKeyBuilder* builder) const override {
Brian Salomon92be2f72018-06-19 14:33:47 -0400100 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -0700101 }
102
103 private:
Brian Salomon92be2f72018-06-19 14:33:47 -0400104 int fNumAttribs;
105 std::unique_ptr<SkString[]> fAttribNames;
106 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400107
108 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700109 };
Hal Canary342b7ac2016-11-04 11:49:42 -0400110 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500111 size_t vertexStride = gp->vertexStride();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000112 QuadHelper helper(target, vertexStride, 1);
113 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500114 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700115 helper.recordDraw(target, std::move(gp));
116 }
117
118 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
119 flushState->executeDrawsAndUploadsForMeshDrawOp(
120 this, chainBounds, GrProcessorSet::MakeEmptySet());
bsalomon1d417a82016-03-23 11:50:26 -0700121 }
122
123 int fNumAttribs;
124
Brian Salomonb4b8a462017-07-13 15:29:47 -0400125 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700126};
127}
128
egdanielb05df0f2016-06-27 07:15:20 -0700129DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700130 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500131#if GR_GPU_STATS
Robert Phillips9da87e02019-02-04 13:26:26 -0500132 GrGpu* gpu = context->priv().getGpu();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500133#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700134
Brian Salomonbf6b9792019-08-21 09:38:10 -0400135 auto renderTargetContext = context->priv().makeDeferredRenderTargetContext(
136 SkBackingFit::kApprox, 1, 1, GrColorType::kRGBA_8888, nullptr);
Brian Osman11052242016-10-27 14:47:55 -0400137 if (!renderTargetContext) {
138 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700139 return;
140 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500141 int attribCnt = context->priv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700142 if (!attribCnt) {
143 ERRORF(reporter, "No attributes allowed?!");
144 return;
145 }
146 context->flush();
Robert Phillips9da87e02019-02-04 13:26:26 -0500147 context->priv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700148#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500149 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
150 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700151#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500152 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
153 renderTargetContext->discard();
154
robertphillips28a838e2016-06-23 14:07:00 -0700155 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700156 // This one should succeed.
Robert Phillips7c525e62018-06-12 10:11:12 -0400157 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700158 context->flush();
159#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500160 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
161 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700162#endif
Robert Phillips9da87e02019-02-04 13:26:26 -0500163 context->priv().resetGpuStats();
Robert Phillips7c525e62018-06-12 10:11:12 -0400164 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700165 context->flush();
166#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500167 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
168 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700169#endif
170}