blob: 5f9fbe522382b0cd6b4d11eb93a78635515e32b9 [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
10#include "SkTypes.h"
11#include "Test.h"
12
bsalomon1d417a82016-03-23 11:50:26 -070013#include "GrContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040014#include "GrContextPriv.h"
bsalomon1d417a82016-03-23 11:50:26 -070015#include "GrGeometryProcessor.h"
16#include "GrGpu.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040017#include "GrMemoryPool.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "GrOpFlushState.h"
Brian Salomondad29232016-12-01 16:40:24 -050019#include "GrRenderTargetContext.h"
20#include "GrRenderTargetContextPriv.h"
Cary Clark74f623d2017-11-06 20:02:02 -050021#include "SkPointPriv.h"
bsalomon1d417a82016-03-23 11:50:26 -070022#include "SkString.h"
Brian Salomondad29232016-12-01 16:40:24 -050023#include "glsl/GrGLSLFragmentShaderBuilder.h"
24#include "glsl/GrGLSLGeometryProcessor.h"
25#include "glsl/GrGLSLVarying.h"
Brian Salomon89527432016-12-16 09:52:16 -050026#include "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 Phillipsc994a932018-06-19 13:09:54 -040036 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
37
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
Brian Osman532b3f92018-07-11 10:02:07 -040045 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Brian Salomonb4b8a462017-07-13 15:29:47 -040046 return RequiresDstTexture::kNo;
Brian Salomonf8334782017-01-03 09:42:58 -050047 }
Brian Salomon09d994e2016-12-21 11:14:46 -050048
49private:
Robert Phillips7c525e62018-06-12 10:11:12 -040050 friend class ::GrOpMemoryPool;
51
Brian Salomon09d994e2016-12-21 11:14:46 -050052 Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) {
bsalomon88cf17d2016-07-08 06:40:56 -070053 this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo);
bsalomon1d417a82016-03-23 11:50:26 -070054 }
55
Brian Salomon91326c32017-08-09 16:02:19 -040056 void onPrepareDraws(Target* target) override {
bsalomon1d417a82016-03-23 11:50:26 -070057 class GP : public GrGeometryProcessor {
58 public:
Brian Salomon92be2f72018-06-19 14:33:47 -040059 GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) {
bsalomon1d417a82016-03-23 11:50:26 -070060 SkASSERT(numAttribs > 1);
Brian Salomon92be2f72018-06-19 14:33:47 -040061 fAttribNames.reset(new SkString[numAttribs]);
62 fAttributes.reset(new Attribute[numAttribs]);
bsalomon1d417a82016-03-23 11:50:26 -070063 for (auto i = 0; i < numAttribs; ++i) {
Brian Salomon92be2f72018-06-19 14:33:47 -040064 fAttribNames[i].printf("attr%d", i);
65 fAttributes[i] = {fAttribNames[i].c_str(), kFloat2_GrVertexAttribType};
bsalomon1d417a82016-03-23 11:50:26 -070066 }
Brian Salomon92be2f72018-06-19 14:33:47 -040067 this->setVertexAttributeCnt(numAttribs);
Mike Kleinfc6c37b2016-09-27 09:34:10 -040068 }
bsalomon1d417a82016-03-23 11:50:26 -070069 const char* name() const override { return "Dummy GP"; }
70
Brian Salomon94efbf52016-11-29 13:43:05 -050071 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
bsalomon1d417a82016-03-23 11:50:26 -070072 class GLSLGP : public GrGLSLGeometryProcessor {
73 public:
74 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
75 const GP& gp = args.fGP.cast<GP>();
76 args.fVaryingHandler->emitAttributes(gp);
Brian Salomon70132d02018-05-29 15:33:06 -040077 this->writeOutputPosition(args.fVertBuilder, gpArgs,
Brian Salomon92be2f72018-06-19 14:33:47 -040078 gp.fAttributes[0].name());
Chris Dalton60283612018-02-14 13:38:14 -070079 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -040080 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
81 fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
bsalomon1d417a82016-03-23 11:50:26 -070082 }
83 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -070084 const GrPrimitiveProcessor& primProc,
85 FPCoordTransformIter&&) override {}
bsalomon1d417a82016-03-23 11:50:26 -070086 };
87 return new GLSLGP();
88 }
Brian Salomon94efbf52016-11-29 13:43:05 -050089 void getGLSLProcessorKey(const GrShaderCaps&,
bsalomon1d417a82016-03-23 11:50:26 -070090 GrProcessorKeyBuilder* builder) const override {
Brian Salomon92be2f72018-06-19 14:33:47 -040091 builder->add32(fNumAttribs);
bsalomon1d417a82016-03-23 11:50:26 -070092 }
93
94 private:
Brian Salomon92be2f72018-06-19 14:33:47 -040095 const GrPrimitiveProcessor::Attribute& onVertexAttribute(int i) const override {
96 return fAttributes[i];
97 }
98
99 int fNumAttribs;
100 std::unique_ptr<SkString[]> fAttribNames;
101 std::unique_ptr<Attribute[]> fAttributes;
Ethan Nicholasabff9562017-10-09 10:54:08 -0400102
103 typedef GrGeometryProcessor INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700104 };
Hal Canary342b7ac2016-11-04 11:49:42 -0400105 sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs));
Brian Salomon92be2f72018-06-19 14:33:47 -0400106 size_t vertexStride = fNumAttribs * GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
107 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000108 QuadHelper helper(target, vertexStride, 1);
109 SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices());
Cary Clark74f623d2017-11-06 20:02:02 -0500110 SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride);
Brian Salomon49348902018-06-26 09:12:38 -0400111 auto pipe = target->makePipeline(0, GrProcessorSet::MakeEmptySet(),
112 target->detachAppliedClip());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000113 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
bsalomon1d417a82016-03-23 11:50:26 -0700114 }
115
116 int fNumAttribs;
117
Brian Salomonb4b8a462017-07-13 15:29:47 -0400118 typedef GrMeshDrawOp INHERITED;
bsalomon1d417a82016-03-23 11:50:26 -0700119};
120}
121
egdanielb05df0f2016-06-27 07:15:20 -0700122DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700123 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500124#if GR_GPU_STATS
125 GrGpu* gpu = context->contextPriv().getGpu();
126#endif
robertphillipsd4c741e2016-04-28 09:55:15 -0700127
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500128 sk_sp<GrRenderTargetContext> renderTargetContext(
129 context->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kApprox,
130 1, 1, kRGBA_8888_GrPixelConfig,
131 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400132 if (!renderTargetContext) {
133 ERRORF(reporter, "Could not create render target context.");
bsalomon1d417a82016-03-23 11:50:26 -0700134 return;
135 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400136 int attribCnt = context->contextPriv().caps()->maxVertexAttributes();
bsalomon1d417a82016-03-23 11:50:26 -0700137 if (!attribCnt) {
138 ERRORF(reporter, "No attributes allowed?!");
139 return;
140 }
141 context->flush();
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500142 context->contextPriv().resetGpuStats();
bsalomon1d417a82016-03-23 11:50:26 -0700143#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500144 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
145 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700146#endif
Greg Danielf44cb482018-02-27 14:26:32 -0500147 // Adding discard to appease vulkan validation warning about loading uninitialized data on draw
148 renderTargetContext->discard();
149
robertphillips28a838e2016-06-23 14:07:00 -0700150 GrPaint grPaint;
bsalomon1d417a82016-03-23 11:50:26 -0700151 // This one should succeed.
Robert Phillips7c525e62018-06-12 10:11:12 -0400152 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt));
bsalomon1d417a82016-03-23 11:50:26 -0700153 context->flush();
154#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500155 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1);
156 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0);
bsalomon1d417a82016-03-23 11:50:26 -0700157#endif
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500158 context->contextPriv().resetGpuStats();
Robert Phillips7c525e62018-06-12 10:11:12 -0400159 renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1));
bsalomon1d417a82016-03-23 11:50:26 -0700160 context->flush();
161#if GR_GPU_STATS
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500162 REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0);
163 REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1);
bsalomon1d417a82016-03-23 11:50:26 -0700164#endif
165}