blob: ae61788b7ed44d61e735d22db9b81252d900d1c0 [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001/*
2 * Copyright 2011 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
Ben Wagnerf1344ac2019-05-10 15:01:53 -04008// This is a GPU-backend specific test.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkChecksum.h"
13#include "include/utils/SkRandom.h"
14#include "src/gpu/GrAutoLocaleSetter.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDrawOpTest.h"
17#include "src/gpu/GrDrawingManager.h"
18#include "src/gpu/GrPipeline.h"
19#include "src/gpu/GrRenderTargetContextPriv.h"
20#include "src/gpu/GrXferProcessor.h"
21#include "tests/Test.h"
22#include "tools/gpu/GrContextFactory.h"
joshualitt74417822015-08-07 11:42:16 -070023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/ops/GrDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070025
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
27#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
28#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
joshualitt74417822015-08-07 11:42:16 -070029
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/gl/GrGLGpu.h"
31#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
32#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
33#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000034
joshualitt65171342014-10-09 07:25:36 -070035/*
bsalomon98b33eb2014-10-15 11:05:26 -070036 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070037 * whole thing correctly
38 */
39static const uint32_t kMaxKeySize = 1024;
40
egdaniel64c47282015-11-13 06:54:19 -080041class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070042public:
robertphillips9cdb9922016-02-03 12:25:40 -080043 void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070044 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080045 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070046 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080047 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070048 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080049 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070050 }
joshualitt6c891102015-05-13 08:51:49 -070051 }
joshualitt65171342014-10-09 07:25:36 -070052
Brian Salomon94efbf52016-11-29 13:43:05 -050053 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070054 for (uint32_t i = 0; i < kMaxKeySize; i++) {
55 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070056 }
joshualittd9097592014-10-07 08:37:36 -070057 }
58
joshualitt65171342014-10-09 07:25:36 -070059private:
egdaniel64c47282015-11-13 06:54:19 -080060 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070061};
62
joshualitteb2a6762014-12-04 11:35:33 -080063class BigKeyProcessor : public GrFragmentProcessor {
64public:
Brian Salomonaff329b2017-08-11 09:40:37 -040065 static std::unique_ptr<GrFragmentProcessor> Make() {
66 return std::unique_ptr<GrFragmentProcessor>(new BigKeyProcessor);
joshualitteb2a6762014-12-04 11:35:33 -080067 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080070
egdaniel57d3b032015-11-13 11:57:27 -080071 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080072 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080073 }
74
Brian Salomonaff329b2017-08-11 09:40:37 -040075 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
Brian Salomonb17e6392017-07-28 13:41:51 -040076
joshualitteb2a6762014-12-04 11:35:33 -080077private:
Ethan Nicholasabff9562017-10-09 10:54:08 -040078 BigKeyProcessor() : INHERITED(kBigKeyProcessor_ClassID, kNone_OptimizationFlags) { }
Brian Salomon94efbf52016-11-29 13:43:05 -050079 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080080 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070081 GLBigKeyProcessor::GenKey(*this, caps, b);
82 }
mtklein36352bf2015-03-25 18:17:31 -070083 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
joshualitteb2a6762014-12-04 11:35:33 -080084
Brian Salomon0c26a9d2017-07-06 10:09:38 -040085 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
joshualitteb2a6762014-12-04 11:35:33 -080086
87 typedef GrFragmentProcessor INHERITED;
88};
89
90GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
91
Hal Canary6f6961e2017-01-31 13:50:44 -050092#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -040093std::unique_ptr<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) {
bungeman06ca8ec2016-06-09 08:01:03 -070094 return BigKeyProcessor::Make();
joshualitteb2a6762014-12-04 11:35:33 -080095}
Hal Canary6f6961e2017-01-31 13:50:44 -050096#endif
joshualitteb2a6762014-12-04 11:35:33 -080097
bsalomonb5b60322015-09-14 12:26:33 -070098//////////////////////////////////////////////////////////////////////////////
99
100class BlockInputFragmentProcessor : public GrFragmentProcessor {
101public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
103 return std::unique_ptr<GrFragmentProcessor>(new BlockInputFragmentProcessor(std::move(fp)));
bsalomonb5b60322015-09-14 12:26:33 -0700104 }
105
106 const char* name() const override { return "Block Input"; }
107
egdaniel57d3b032015-11-13 11:57:27 -0800108 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400111 return Make(this->childProcessor(0).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 }
113
bsalomonb5b60322015-09-14 12:26:33 -0700114private:
egdaniel64c47282015-11-13 06:54:19 -0800115 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700116 public:
117 void emitCode(EmitArgs& args) override {
Brian Osman978693c2020-01-24 14:52:10 -0500118 SkString temp = this->invokeChild(0, args);
Brian Osmancddfc5e2020-01-24 10:24:27 -0500119 args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, temp.c_str());
bsalomonb5b60322015-09-14 12:26:33 -0700120 }
121
122 private:
egdaniel64c47282015-11-13 06:54:19 -0800123 typedef GrGLSLFragmentProcessor INHERITED;
bsalomonb5b60322015-09-14 12:26:33 -0700124 };
125
Brian Salomonaff329b2017-08-11 09:40:37 -0400126 BlockInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400127 : INHERITED(kBlockInputFragmentProcessor_ClassID, kNone_OptimizationFlags) {
bungeman06ca8ec2016-06-09 08:01:03 -0700128 this->registerChildProcessor(std::move(child));
bsalomonb5b60322015-09-14 12:26:33 -0700129 }
130
Brian Salomon94efbf52016-11-29 13:43:05 -0500131 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700132
133 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
134
bsalomonb5b60322015-09-14 12:26:33 -0700135 typedef GrFragmentProcessor INHERITED;
136};
137
138//////////////////////////////////////////////////////////////////////////////
139
joshualitt65171342014-10-09 07:25:36 -0700140/*
141 * Begin test code
142 */
143static const int kRenderTargetHeight = 1;
144static const int kRenderTargetWidth = 1;
145
Brian Salomonbf6b9792019-08-21 09:38:10 -0400146static std::unique_ptr<GrRenderTargetContext> random_render_target_context(GrContext* context,
147 SkRandom* random,
148 const GrCaps* caps) {
robertphillips82ec6e52016-05-19 14:01:05 -0700149 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
150 : kBottomLeft_GrSurfaceOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700151
Greg Daniel5c96db82019-07-09 14:06:58 -0400152 GrColorType ct = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400153 const GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kYes);
Greg Daniel4065d452018-11-16 15:43:41 -0500154
Greg Daniel6fa62e22019-08-07 15:52:37 -0400155 int sampleCnt = random->nextBool() ? caps->getRenderTargetSampleCount(2, format) : 1;
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400156 // Above could be 0 if msaa isn't supported.
157 sampleCnt = SkTMax(1, sampleCnt);
158
Greg Daniele20fcad2020-01-08 11:52:34 -0500159 return GrRenderTargetContext::Make(
160 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
161 {kRenderTargetWidth, kRenderTargetHeight}, sampleCnt, GrMipMapped::kNo,
162 GrProtected::kNo, origin);
bsalomon@google.com91207482013-02-12 21:45:24 +0000163}
164
Hal Canary6f6961e2017-01-31 13:50:44 -0500165#if GR_TEST_UTILS
robertphillips28a838e2016-06-23 14:07:00 -0700166static void set_random_xpf(GrPaint* paint, GrProcessorTestData* d) {
Brian Salomona1633922017-01-09 11:46:10 -0500167 paint->setXPFactory(GrXPFactoryTestFactory::Get(d));
egdanielc2304142014-12-11 13:15:13 -0800168}
169
Brian Salomonaff329b2017-08-11 09:40:37 -0400170static std::unique_ptr<GrFragmentProcessor> create_random_proc_tree(GrProcessorTestData* d,
171 int minLevels, int maxLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700172 SkASSERT(1 <= minLevels);
173 SkASSERT(minLevels <= maxLevels);
174
175 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
176 // If returning a leaf node, make sure that it doesn't have children (e.g. another
177 // GrComposeEffect)
178 const float terminateProbability = 0.3f;
179 if (1 == minLevels) {
180 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
181 if (terminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400182 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700183 while (true) {
Brian Salomon1c053642017-07-24 10:16:19 -0400184 fp = GrFragmentProcessorTestFactory::Make(d);
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400185 if (!fp) {
186 return nullptr;
187 }
wangyix059dffa2015-09-10 06:57:05 -0700188 if (0 == fp->numChildProcessors()) {
189 break;
190 }
wangyix059dffa2015-09-10 06:57:05 -0700191 }
192 return fp;
193 }
194 }
195 // If we didn't terminate, choose either the left or right subtree to fulfill
196 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
Brian Salomona12c1532017-02-13 12:41:44 -0500197 // Also choose a random xfer mode.
wangyix059dffa2015-09-10 06:57:05 -0700198 if (minLevels > 1) {
199 --minLevels;
200 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400201 auto minLevelsChild = create_random_proc_tree(d, minLevels, maxLevels - 1);
202 std::unique_ptr<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1));
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400203 if (!minLevelsChild || !otherChild) {
204 return nullptr;
205 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400206 SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0,
Brian Salomona12c1532017-02-13 12:41:44 -0500207 (int)SkBlendMode::kLastMode));
Brian Salomonaff329b2017-08-11 09:40:37 -0400208 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700209 if (d->fRandom->nextF() < 0.5f) {
bungeman06ca8ec2016-06-09 08:01:03 -0700210 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(minLevelsChild),
211 std::move(otherChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700212 SkASSERT(fp);
213 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700214 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(otherChild),
215 std::move(minLevelsChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700216 SkASSERT(fp);
217 }
218 return fp;
219}
220
robertphillips28a838e2016-06-23 14:07:00 -0700221static void set_random_color_coverage_stages(GrPaint* paint,
222 GrProcessorTestData* d,
Greg Daniel78325c12017-06-19 16:39:13 -0400223 int maxStages,
224 int maxTreeLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700225 // Randomly choose to either create a linear pipeline of procs or create one proc tree
226 const float procTreeProbability = 0.5f;
227 if (d->fRandom->nextF() < procTreeProbability) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400228 std::unique_ptr<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400229 if (fp) {
230 paint->addColorFragmentProcessor(std::move(fp));
231 }
wangyix059dffa2015-09-10 06:57:05 -0700232 } else {
233 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
234 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700235
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400236 for (int s = 0; s < numProcs; ++s) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 std::unique_ptr<GrFragmentProcessor> fp(GrFragmentProcessorTestFactory::Make(d));
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400238 if (!fp) {
239 continue;
240 }
wangyix059dffa2015-09-10 06:57:05 -0700241 // finally add the stage to the correct pipeline in the drawstate
242 if (s < numColorProcs) {
robertphillips28a838e2016-06-23 14:07:00 -0700243 paint->addColorFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700244 } else {
robertphillips28a838e2016-06-23 14:07:00 -0700245 paint->addCoverageFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700246 }
joshualitt65171342014-10-09 07:25:36 -0700247 }
joshualitt65171342014-10-09 07:25:36 -0700248 }
249}
250
Hal Canary6f6961e2017-01-31 13:50:44 -0500251#endif
joshualitt249af152014-09-15 11:41:13 -0700252
Hal Canary6f6961e2017-01-31 13:50:44 -0500253#if !GR_TEST_UTILS
254bool GrDrawingManager::ProgramUnitTest(GrContext*, int) { return true; }
255#else
Greg Daniel78325c12017-06-19 16:39:13 -0400256bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages, int maxLevels) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500257 GrDrawingManager* drawingManager = context->priv().drawingManager();
258 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
robertphillipsa13e2022015-11-11 12:01:09 -0800259
Brian Salomon766098d2019-12-18 10:41:58 -0500260 GrProcessorTestData::ProxyInfo proxies[2];
Robert Phillipse78b7252017-04-06 07:59:41 -0400261
joshualitt65171342014-10-09 07:25:36 -0700262 // setup dummy textures
Brian Salomon4687bdd2019-05-09 16:28:04 -0400263 GrMipMapped mipMapped = GrMipMapped(context->priv().caps()->mipMapSupport());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500264 {
265 GrSurfaceDesc dummyDesc;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500266 dummyDesc.fWidth = 34;
267 dummyDesc.fHeight = 18;
Greg Daniel4065d452018-11-16 15:43:41 -0500268 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400269 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
270 GrRenderable::kYes);
Greg Daniel47c20e82020-01-21 14:29:57 -0500271 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
272 proxies[0] = {proxyProvider->createProxy(format, dummyDesc, swizzle, GrRenderable::kYes, 1,
Brian Salomon766098d2019-12-18 10:41:58 -0500273 kBottomLeft_GrSurfaceOrigin, mipMapped,
274 SkBackingFit::kExact, SkBudgeted::kNo,
275 GrProtected::kNo, GrInternalSurfaceFlags::kNone),
276 GrColorType::kRGBA_8888,
277 kPremul_SkAlphaType
278 };
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500279 }
280 {
281 GrSurfaceDesc dummyDesc;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500282 dummyDesc.fWidth = 16;
283 dummyDesc.fHeight = 22;
Greg Daniel4065d452018-11-16 15:43:41 -0500284 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400285 context->priv().caps()->getDefaultBackendFormat(GrColorType::kAlpha_8,
286 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500287 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kAlpha_8);
288 proxies[1] = {proxyProvider->createProxy(format, dummyDesc, swizzle, GrRenderable::kNo, 1,
Brian Salomon766098d2019-12-18 10:41:58 -0500289 kTopLeft_GrSurfaceOrigin, mipMapped,
290 SkBackingFit::kExact, SkBudgeted::kNo,
291 GrProtected::kNo, GrInternalSurfaceFlags::kNone),
292 GrColorType::kAlpha_8,
293 kPremul_SkAlphaType
294 };
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500295 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000296
Brian Salomon766098d2019-12-18 10:41:58 -0500297 if (!std::get<0>(proxies[0]) || !std::get<0>(proxies[1])) {
joshualitt65171342014-10-09 07:25:36 -0700298 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700299 return false;
300 }
301
Brian Osman9f772a42017-07-10 13:03:50 -0400302 SkRandom random;
bsalomonf3261af2016-04-05 10:57:13 -0700303 static const int NUM_TESTS = 1024;
joshualitt6c891102015-05-13 08:51:49 -0700304 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700305 // setup random render target(can fail)
Brian Salomonbf6b9792019-08-21 09:38:10 -0400306 auto renderTargetContext =
307 random_render_target_context(context, &random, context->priv().caps());
Brian Osman11052242016-10-27 14:47:55 -0400308 if (!renderTargetContext) {
309 SkDebugf("Could not allocate renderTargetContext");
joshualittf5883a62016-01-13 07:47:38 -0800310 return false;
311 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800312
Brian Salomon17726632017-05-12 14:09:46 -0400313 GrPaint paint;
Brian Salomon766098d2019-12-18 10:41:58 -0500314 GrProcessorTestData ptd(&random, context, 2, proxies);
Greg Daniel78325c12017-06-19 16:39:13 -0400315 set_random_color_coverage_stages(&paint, &ptd, maxStages, maxLevels);
Brian Salomon17726632017-05-12 14:09:46 -0400316 set_random_xpf(&paint, &ptd);
Brian Salomon17726632017-05-12 14:09:46 -0400317 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000318 }
joshualitt6c891102015-05-13 08:51:49 -0700319 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
Greg Daniel797efca2019-05-09 14:04:20 -0400320 drawingManager->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
321 GrPrepareForExternalIORequests());
bsalomonb5b60322015-09-14 12:26:33 -0700322
323 // Validate that GrFPs work correctly without an input.
Greg Daniele20fcad2020-01-08 11:52:34 -0500324 auto renderTargetContext = GrRenderTargetContext::Make(
325 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
326 {kRenderTargetWidth, kRenderTargetHeight});
Brian Osman11052242016-10-27 14:47:55 -0400327 if (!renderTargetContext) {
328 SkDebugf("Could not allocate a renderTargetContext");
robertphillips82ec6e52016-05-19 14:01:05 -0700329 return false;
330 }
331
Brian Salomon1c053642017-07-24 10:16:19 -0400332 int fpFactoryCnt = GrFragmentProcessorTestFactory::Count();
bsalomonb5b60322015-09-14 12:26:33 -0700333 for (int i = 0; i < fpFactoryCnt; ++i) {
334 // Since FP factories internally randomize, call each 10 times.
335 for (int j = 0; j < 10; ++j) {
Brian Salomon766098d2019-12-18 10:41:58 -0500336 GrProcessorTestData ptd(&random, context, 2, proxies);
bsalomonb5b60322015-09-14 12:26:33 -0700337
Brian Salomon17726632017-05-12 14:09:46 -0400338 GrPaint paint;
339 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400340 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &ptd);
341 auto blockFP = BlockInputFragmentProcessor::Make(std::move(fp));
Brian Salomon17726632017-05-12 14:09:46 -0400342 paint.addColorFragmentProcessor(std::move(blockFP));
343 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400344 drawingManager->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400345 GrFlushInfo(), GrPrepareForExternalIORequests());
bsalomonb5b60322015-09-14 12:26:33 -0700346 }
347 }
348
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000349 return true;
350}
Hal Canary6f6961e2017-01-31 13:50:44 -0500351#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000352
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400353static int get_programs_max_stages(const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Danield895ca62017-06-19 14:39:43 -0400354 GrContext* context = ctxInfo.grContext();
Brian Salomone334c592017-05-15 11:00:58 -0400355 int maxStages = 6;
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400356 if (skiatest::IsGLContextType(ctxInfo.type())) {
357 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
358 if (kGLES_GrGLStandard == gpu->glStandard()) {
359 // We've had issues with driver crashes and HW limits being exceeded with many effects on
360 // Android devices. We have passes on ARM devices with the default number of stages.
361 // TODO When we run ES 3.00 GLSL in more places, test again
Brian Salomone334c592017-05-15 11:00:58 -0400362#ifdef SK_BUILD_FOR_ANDROID
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400363 if (kARM_GrGLVendor != gpu->ctxInfo().vendor()) {
364 maxStages = 1;
365 }
kkinnunen3e980c32015-12-23 01:33:00 -0800366#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400367 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Brian Osman4e1868c2017-05-26 15:56:32 -0400368#ifdef SK_BUILD_FOR_IOS
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400369 maxStages = 3;
Brian Salomone334c592017-05-15 11:00:58 -0400370#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400371 }
372 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
373 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
374 // On Angle D3D we will hit a limit of out variables if we use too many stages.
375 maxStages = 3;
376 }
Greg Danield895ca62017-06-19 14:39:43 -0400377 }
Brian Salomone334c592017-05-15 11:00:58 -0400378 return maxStages;
379}
380
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400381static int get_programs_max_levels(const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Daniel78325c12017-06-19 16:39:13 -0400382 // A full tree with 5 levels (31 nodes) may cause a program that exceeds shader limits
383 // (e.g. uniform or varying limits); maxTreeLevels should be a number from 1 to 4 inclusive.
384 int maxTreeLevels = 4;
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400385 if (skiatest::IsGLContextType(ctxInfo.type())) {
386 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Greg Daniel78325c12017-06-19 16:39:13 -0400387#ifdef SK_BUILD_FOR_IOS
Michael Ludwig6387dc12019-10-22 15:39:41 +0000388 maxTreeLevels = 2;
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400389#endif
Brian Salomon9c4ee9c2019-12-20 16:20:54 -0500390#ifdef SK_BUILD_FOR_ANDROID
391 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctxInfo.grContext()->priv().getGpu());
392 // Tecno Spark 3 Pro with Power VR Rogue GE8300 will fail shader compiles with
393 // no message if the shader is particularly long.
394 if (gpu->ctxInfo().vendor() == kImagination_GrGLVendor) {
395 maxTreeLevels = 3;
396 }
397#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400398 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
399 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
400 // On Angle D3D we will hit a limit of out variables if we use too many stages.
401 maxTreeLevels = 2;
402 }
Greg Daniel78325c12017-06-19 16:39:13 -0400403 }
404 return maxTreeLevels;
405}
406
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400407static void test_programs(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo) {
408 int maxStages = get_programs_max_stages(ctxInfo);
kkinnunen3e980c32015-12-23 01:33:00 -0800409 if (maxStages == 0) {
410 return;
411 }
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400412 int maxLevels = get_programs_max_levels(ctxInfo);
Greg Daniel78325c12017-06-19 16:39:13 -0400413 if (maxLevels == 0) {
414 return;
415 }
Brian Osman9f772a42017-07-10 13:03:50 -0400416
Greg Daniel78325c12017-06-19 16:39:13 -0400417 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages,
418 maxLevels));
kkinnunen3e980c32015-12-23 01:33:00 -0800419}
420
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400421DEF_GPUTEST(Programs, reporter, options) {
bsalomon3318ee72015-03-16 11:56:29 -0700422 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
423 // skbug 3330
424#ifdef SK_BUILD_FOR_WIN
425 GrAutoLocaleSetter als("sv-SE");
426#else
427 GrAutoLocaleSetter als("sv_SE.UTF-8");
428#endif
429
joshualitt6c891102015-05-13 08:51:49 -0700430 // We suppress prints to avoid spew
Brian Salomondcfca432017-11-15 15:48:03 -0500431 GrContextOptions opts = options;
joshualitt6c891102015-05-13 08:51:49 -0700432 opts.fSuppressPrints = true;
bsalomon3724e572016-03-30 18:56:19 -0700433 sk_gpu_test::GrContextFactory debugFactory(opts);
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400434 skiatest::RunWithGPUTestContexts(test_programs, &skiatest::IsRenderingGLOrMetalContextType,
435 reporter, opts);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000436}