bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkTypes.h" |
| 11 | #include "tests/Test.h" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #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" |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 27 | |
| 28 | namespace { |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 29 | class Op : public GrMeshDrawOp { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 30 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 31 | DEFINE_OP_CLASS_ID |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 32 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 33 | const char* name() const override { return "Dummy Op"; } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 34 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 35 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, int numAttribs) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 36 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 37 | |
| 38 | return pool->allocate<Op>(numAttribs); |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 42 | return FixedFunctionFlags::kNone; |
| 43 | } |
| 44 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 45 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 46 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 47 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 48 | } |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 49 | |
| 50 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 51 | friend class ::GrOpMemoryPool; |
| 52 | |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 53 | Op(int numAttribs) : INHERITED(ClassID()), fNumAttribs(numAttribs) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 54 | this->setBounds(SkRect::MakeWH(1.f, 1.f), HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 57 | void onPrepareDraws(Target* target) override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 58 | class GP : public GrGeometryProcessor { |
| 59 | public: |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 60 | GP(int numAttribs) : INHERITED(kGP_ClassID), fNumAttribs(numAttribs) { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 61 | SkASSERT(numAttribs > 1); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 62 | fAttribNames.reset(new SkString[numAttribs]); |
| 63 | fAttributes.reset(new Attribute[numAttribs]); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 64 | for (auto i = 0; i < numAttribs; ++i) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 65 | fAttribNames[i].printf("attr%d", i); |
Jim Van Verth | a3407ab | 2019-03-15 15:22:39 -0400 | [diff] [blame] | 66 | // 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 | } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 75 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 76 | this->setVertexAttributes(fAttributes.get(), numAttribs); |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 77 | } |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 78 | const char* name() const override { return "Dummy GP"; } |
| 79 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 80 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 81 | 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 Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 86 | this->writeOutputPosition(args.fVertBuilder, gpArgs, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 87 | gp.fAttributes[0].name()); |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 88 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 89 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor); |
| 90 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 91 | } |
| 92 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 93 | const GrPrimitiveProcessor& primProc, |
| 94 | FPCoordTransformIter&&) override {} |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 95 | }; |
| 96 | return new GLSLGP(); |
| 97 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 98 | void getGLSLProcessorKey(const GrShaderCaps&, |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 99 | GrProcessorKeyBuilder* builder) const override { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 100 | builder->add32(fNumAttribs); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 104 | int fNumAttribs; |
| 105 | std::unique_ptr<SkString[]> fAttribNames; |
| 106 | std::unique_ptr<Attribute[]> fAttributes; |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 107 | |
| 108 | typedef GrGeometryProcessor INHERITED; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 109 | }; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 110 | sk_sp<GrGeometryProcessor> gp(new GP(fNumAttribs)); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 111 | size_t vertexStride = gp->vertexStride(); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 112 | QuadHelper helper(target, vertexStride, 1); |
| 113 | SkPoint* vertices = reinterpret_cast<SkPoint*>(helper.vertices()); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 114 | SkPointPriv::SetRectTriStrip(vertices, 0.f, 0.f, 1.f, 1.f, vertexStride); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 115 | 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()); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int fNumAttribs; |
| 124 | |
Brian Salomon | b4b8a46 | 2017-07-13 15:29:47 -0400 | [diff] [blame] | 125 | typedef GrMeshDrawOp INHERITED; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 126 | }; |
| 127 | } |
| 128 | |
egdaniel | b05df0f | 2016-06-27 07:15:20 -0700 | [diff] [blame] | 129 | DEF_GPUTEST_FOR_ALL_CONTEXTS(VertexAttributeCount, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 130 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 131 | #if GR_GPU_STATS |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 132 | GrGpu* gpu = context->priv().getGpu(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 133 | #endif |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 134 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 135 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 136 | context->priv().makeDeferredRenderTargetContext(SkBackingFit::kApprox, 1, 1, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 137 | GrColorType::kRGBA_8888, nullptr)); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 138 | if (!renderTargetContext) { |
| 139 | ERRORF(reporter, "Could not create render target context."); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 142 | int attribCnt = context->priv().caps()->maxVertexAttributes(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 143 | if (!attribCnt) { |
| 144 | ERRORF(reporter, "No attributes allowed?!"); |
| 145 | return; |
| 146 | } |
| 147 | context->flush(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 148 | context->priv().resetGpuStats(); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 149 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 151 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 152 | #endif |
Greg Daniel | f44cb48 | 2018-02-27 14:26:32 -0500 | [diff] [blame] | 153 | // Adding discard to appease vulkan validation warning about loading uninitialized data on draw |
| 154 | renderTargetContext->discard(); |
| 155 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 156 | GrPaint grPaint; |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 157 | // This one should succeed. |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 158 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 159 | context->flush(); |
| 160 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 161 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 1); |
| 162 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 0); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 163 | #endif |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 164 | context->priv().resetGpuStats(); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 165 | renderTargetContext->priv().testingOnly_addDrawOp(Op::Make(context, attribCnt + 1)); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 166 | context->flush(); |
| 167 | #if GR_GPU_STATS |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, gpu->stats()->numDraws() == 0); |
| 169 | REPORTER_ASSERT(reporter, gpu->stats()->numFailedDraws() == 1); |
bsalomon | 1d417a8 | 2016-03-23 11:50:26 -0700 | [diff] [blame] | 170 | #endif |
| 171 | } |