blob: d4e3335faf112b66e6754c1b9ef15245fe0623b3 [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
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkChecksum.h"
14#include "include/utils/SkRandom.h"
15#include "src/gpu/GrAutoLocaleSetter.h"
Adlai Hollera0693042020-10-14 11:23:11 -040016#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawOpTest.h"
18#include "src/gpu/GrDrawingManager.h"
19#include "src/gpu/GrPipeline.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040020#include "src/gpu/GrProxyProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrXferProcessor.h"
John Stilesf743d4e2020-07-23 11:35:08 -040022#include "src/gpu/effects/GrBlendFragmentProcessor.h"
Brian Salomon1171d312020-03-19 08:47:44 -040023#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Brian Salomon1171d312020-03-19 08:47:44 -040024#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
Brian Salomon1171d312020-03-19 08:47:44 -040025#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
26#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
27#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
Brian Salomonf4ba4ec2020-03-19 15:54:28 -040028#include "src/gpu/ops/GrDrawOp.h"
29#include "tests/Test.h"
30#include "tools/gpu/GrContextFactory.h"
31
32#ifdef SK_GL
33#include "src/gpu/gl/GrGLGpu.h"
34#endif
bsalomon@google.comc3841b92012-08-02 18:11:43 +000035
joshualitt65171342014-10-09 07:25:36 -070036/*
bsalomon98b33eb2014-10-15 11:05:26 -070037 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070038 * whole thing correctly
39 */
40static const uint32_t kMaxKeySize = 1024;
41
egdaniel64c47282015-11-13 06:54:19 -080042class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070043public:
robertphillips9cdb9922016-02-03 12:25:40 -080044 void emitCode(EmitArgs& args) override {
John Stiles02eb5dc2020-12-15 11:20:33 -050045 args.fFragBuilder->codeAppendf("return half4(1);\n");
joshualitt6c891102015-05-13 08:51:49 -070046 }
joshualitt65171342014-10-09 07:25:36 -070047
Brian Salomon94efbf52016-11-29 13:43:05 -050048 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070049 for (uint32_t i = 0; i < kMaxKeySize; i++) {
50 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070051 }
joshualittd9097592014-10-07 08:37:36 -070052 }
53
joshualitt65171342014-10-09 07:25:36 -070054private:
John Stiles7571f9e2020-09-02 22:42:33 -040055 using INHERITED = GrGLSLFragmentProcessor;
joshualitt65171342014-10-09 07:25:36 -070056};
57
joshualitteb2a6762014-12-04 11:35:33 -080058class BigKeyProcessor : public GrFragmentProcessor {
59public:
Brian Salomonaff329b2017-08-11 09:40:37 -040060 static std::unique_ptr<GrFragmentProcessor> Make() {
61 return std::unique_ptr<GrFragmentProcessor>(new BigKeyProcessor);
joshualitteb2a6762014-12-04 11:35:33 -080062 }
63
Ethan Nicholas7c752262020-04-01 14:18:28 -040064 const char* name() const override { return "Big_Ole_Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080065
John Stiles02eb5dc2020-12-15 11:20:33 -050066 bool usesExplicitReturn() const override { return true; }
67
egdaniel57d3b032015-11-13 11:57:27 -080068 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080069 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080070 }
71
Brian Salomonaff329b2017-08-11 09:40:37 -040072 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
Brian Salomonb17e6392017-07-28 13:41:51 -040073
joshualitteb2a6762014-12-04 11:35:33 -080074private:
Ethan Nicholasabff9562017-10-09 10:54:08 -040075 BigKeyProcessor() : INHERITED(kBigKeyProcessor_ClassID, kNone_OptimizationFlags) { }
John Stiles1cf2c8d2020-08-13 22:58:04 -040076 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070077 GLBigKeyProcessor::GenKey(*this, caps, b);
78 }
mtklein36352bf2015-03-25 18:17:31 -070079 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
joshualitteb2a6762014-12-04 11:35:33 -080080
Brian Salomon0c26a9d2017-07-06 10:09:38 -040081 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
joshualitteb2a6762014-12-04 11:35:33 -080082
John Stiles7571f9e2020-09-02 22:42:33 -040083 using INHERITED = GrFragmentProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080084};
85
86GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
87
Hal Canary6f6961e2017-01-31 13:50:44 -050088#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -040089std::unique_ptr<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) {
bungeman06ca8ec2016-06-09 08:01:03 -070090 return BigKeyProcessor::Make();
joshualitteb2a6762014-12-04 11:35:33 -080091}
Hal Canary6f6961e2017-01-31 13:50:44 -050092#endif
joshualitteb2a6762014-12-04 11:35:33 -080093
bsalomonb5b60322015-09-14 12:26:33 -070094//////////////////////////////////////////////////////////////////////////////
95
96class BlockInputFragmentProcessor : public GrFragmentProcessor {
97public:
Brian Salomonaff329b2017-08-11 09:40:37 -040098 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
99 return std::unique_ptr<GrFragmentProcessor>(new BlockInputFragmentProcessor(std::move(fp)));
bsalomonb5b60322015-09-14 12:26:33 -0700100 }
101
Brian Salomonf49debf2020-04-17 10:35:38 -0400102 const char* name() const override { return "Block_Input"; }
bsalomonb5b60322015-09-14 12:26:33 -0700103
John Stiles02eb5dc2020-12-15 11:20:33 -0500104 bool usesExplicitReturn() const override { return true; }
105
egdaniel57d3b032015-11-13 11:57:27 -0800106 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700107
Brian Salomonaff329b2017-08-11 09:40:37 -0400108 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Osman12c5d292020-07-13 16:11:35 -0400109 return Make(this->childProcessor(0)->clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400110 }
111
bsalomonb5b60322015-09-14 12:26:33 -0700112private:
egdaniel64c47282015-11-13 06:54:19 -0800113 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700114 public:
115 void emitCode(EmitArgs& args) override {
Brian Osman978693c2020-01-24 14:52:10 -0500116 SkString temp = this->invokeChild(0, args);
John Stiles02eb5dc2020-12-15 11:20:33 -0500117 args.fFragBuilder->codeAppendf("return %s;", temp.c_str());
bsalomonb5b60322015-09-14 12:26:33 -0700118 }
119
120 private:
John Stiles7571f9e2020-09-02 22:42:33 -0400121 using INHERITED = GrGLSLFragmentProcessor;
bsalomonb5b60322015-09-14 12:26:33 -0700122 };
123
Brian Salomonaff329b2017-08-11 09:40:37 -0400124 BlockInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400125 : INHERITED(kBlockInputFragmentProcessor_ClassID, kNone_OptimizationFlags) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400126 this->registerChild(std::move(child));
bsalomonb5b60322015-09-14 12:26:33 -0700127 }
128
Brian Salomon94efbf52016-11-29 13:43:05 -0500129 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700130
131 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
132
John Stiles7571f9e2020-09-02 22:42:33 -0400133 using INHERITED = GrFragmentProcessor;
bsalomonb5b60322015-09-14 12:26:33 -0700134};
135
136//////////////////////////////////////////////////////////////////////////////
137
joshualitt65171342014-10-09 07:25:36 -0700138/*
139 * Begin test code
140 */
141static const int kRenderTargetHeight = 1;
142static const int kRenderTargetWidth = 1;
143
Brian Salomoneebe7352020-12-09 16:37:04 -0500144static std::unique_ptr<GrSurfaceDrawContext> random_render_target_context(
Robert Phillips58adb342020-07-23 09:41:57 -0400145 GrRecordingContext* rContext,
146 SkRandom* random,
147 const GrCaps* caps) {
robertphillips82ec6e52016-05-19 14:01:05 -0700148 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
149 : kBottomLeft_GrSurfaceOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700150
Greg Daniel5c96db82019-07-09 14:06:58 -0400151 GrColorType ct = GrColorType::kRGBA_8888;
Robert Phillips0a15cc62019-07-30 12:49:10 -0400152 const GrBackendFormat format = caps->getDefaultBackendFormat(ct, GrRenderable::kYes);
Greg Daniel4065d452018-11-16 15:43:41 -0500153
Greg Daniel6fa62e22019-08-07 15:52:37 -0400154 int sampleCnt = random->nextBool() ? caps->getRenderTargetSampleCount(2, format) : 1;
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400155 // Above could be 0 if msaa isn't supported.
Brian Osman788b9162020-02-07 10:36:46 -0500156 sampleCnt = std::max(1, sampleCnt);
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400157
Brian Salomoneebe7352020-12-09 16:37:04 -0500158 return GrSurfaceDrawContext::Make(
Robert Phillips58adb342020-07-23 09:41:57 -0400159 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400160 {kRenderTargetWidth, kRenderTargetHeight}, sampleCnt, GrMipmapped::kNo,
Greg Daniele20fcad2020-01-08 11:52:34 -0500161 GrProtected::kNo, origin);
bsalomon@google.com91207482013-02-12 21:45:24 +0000162}
163
Hal Canary6f6961e2017-01-31 13:50:44 -0500164#if GR_TEST_UTILS
robertphillips28a838e2016-06-23 14:07:00 -0700165static void set_random_xpf(GrPaint* paint, GrProcessorTestData* d) {
Brian Salomona1633922017-01-09 11:46:10 -0500166 paint->setXPFactory(GrXPFactoryTestFactory::Get(d));
egdanielc2304142014-12-11 13:15:13 -0800167}
168
Brian Salomonaff329b2017-08-11 09:40:37 -0400169static std::unique_ptr<GrFragmentProcessor> create_random_proc_tree(GrProcessorTestData* d,
170 int minLevels, int maxLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700171 SkASSERT(1 <= minLevels);
172 SkASSERT(minLevels <= maxLevels);
173
174 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
175 // If returning a leaf node, make sure that it doesn't have children (e.g. another
176 // GrComposeEffect)
177 const float terminateProbability = 0.3f;
178 if (1 == minLevels) {
179 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
180 if (terminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400181 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700182 while (true) {
Brian Salomon1c053642017-07-24 10:16:19 -0400183 fp = GrFragmentProcessorTestFactory::Make(d);
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400184 if (!fp) {
185 return nullptr;
186 }
Brian Osman12c5d292020-07-13 16:11:35 -0400187 if (0 == fp->numNonNullChildProcessors()) {
wangyix059dffa2015-09-10 06:57:05 -0700188 break;
189 }
wangyix059dffa2015-09-10 06:57:05 -0700190 }
191 return fp;
192 }
193 }
194 // If we didn't terminate, choose either the left or right subtree to fulfill
195 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
Brian Salomona12c1532017-02-13 12:41:44 -0500196 // Also choose a random xfer mode.
wangyix059dffa2015-09-10 06:57:05 -0700197 if (minLevels > 1) {
198 --minLevels;
199 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400200 auto minLevelsChild = create_random_proc_tree(d, minLevels, maxLevels - 1);
201 std::unique_ptr<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1));
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400202 if (!minLevelsChild || !otherChild) {
203 return nullptr;
204 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400205 SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0,
Brian Salomona12c1532017-02-13 12:41:44 -0500206 (int)SkBlendMode::kLastMode));
Brian Salomonaff329b2017-08-11 09:40:37 -0400207 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700208 if (d->fRandom->nextF() < 0.5f) {
John Stilesf743d4e2020-07-23 11:35:08 -0400209 fp = GrBlendFragmentProcessor::Make(std::move(minLevelsChild), std::move(otherChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700210 SkASSERT(fp);
211 } else {
John Stilesf743d4e2020-07-23 11:35:08 -0400212 fp = GrBlendFragmentProcessor::Make(std::move(otherChild), std::move(minLevelsChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700213 SkASSERT(fp);
214 }
215 return fp;
216}
217
robertphillips28a838e2016-06-23 14:07:00 -0700218static void set_random_color_coverage_stages(GrPaint* paint,
219 GrProcessorTestData* d,
Greg Daniel78325c12017-06-19 16:39:13 -0400220 int maxStages,
221 int maxTreeLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700222 // Randomly choose to either create a linear pipeline of procs or create one proc tree
223 const float procTreeProbability = 0.5f;
224 if (d->fRandom->nextF() < procTreeProbability) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400225 std::unique_ptr<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400226 if (fp) {
John Stiles5933d7d2020-07-21 12:28:35 -0400227 paint->setColorFragmentProcessor(std::move(fp));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400228 }
wangyix059dffa2015-09-10 06:57:05 -0700229 } else {
John Stiles41d91b62020-07-21 14:39:40 -0400230 if (maxStages >= 1) {
231 if (std::unique_ptr<GrFragmentProcessor> fp = GrFragmentProcessorTestFactory::Make(d)) {
John Stiles5933d7d2020-07-21 12:28:35 -0400232 paint->setColorFragmentProcessor(std::move(fp));
John Stiles41d91b62020-07-21 14:39:40 -0400233 }
234 }
235 if (maxStages >= 2) {
236 if (std::unique_ptr<GrFragmentProcessor> fp = GrFragmentProcessorTestFactory::Make(d)) {
237 paint->setCoverageFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700238 }
joshualitt65171342014-10-09 07:25:36 -0700239 }
joshualitt65171342014-10-09 07:25:36 -0700240 }
241}
242
Hal Canary6f6961e2017-01-31 13:50:44 -0500243#endif
joshualitt249af152014-09-15 11:41:13 -0700244
Hal Canary6f6961e2017-01-31 13:50:44 -0500245#if !GR_TEST_UTILS
Robert Phillips4e105e22020-07-16 09:18:50 -0400246bool GrDrawingManager::ProgramUnitTest(GrDirectContext*, int) { return true; }
Hal Canary6f6961e2017-01-31 13:50:44 -0500247#else
Robert Phillips4e105e22020-07-16 09:18:50 -0400248bool GrDrawingManager::ProgramUnitTest(GrDirectContext* direct, int maxStages, int maxLevels) {
249 GrProxyProvider* proxyProvider = direct->priv().proxyProvider();
250 const GrCaps* caps = direct->priv().caps();
robertphillipsa13e2022015-11-11 12:01:09 -0800251
Greg Daniel026a60c2020-02-12 10:53:51 -0500252 GrProcessorTestData::ViewInfo views[2];
Robert Phillipse78b7252017-04-06 07:59:41 -0400253
joshualitt65171342014-10-09 07:25:36 -0700254 // setup dummy textures
Brian Salomon69100f02020-07-21 10:49:25 -0400255 GrMipmapped mipMapped = GrMipmapped(caps->mipmapSupport());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500256 {
Brian Salomona56a7462020-02-07 14:17:25 -0500257 static constexpr SkISize kDummyDims = {34, 18};
Robert Phillips4e105e22020-07-16 09:18:50 -0400258 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
259 GrRenderable::kYes);
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400260 auto proxy = proxyProvider->createProxy(format, kDummyDims, GrRenderable::kYes, 1,
Greg Daniel3a365112020-02-14 10:47:18 -0500261 mipMapped, SkBackingFit::kExact, SkBudgeted::kNo,
Greg Daniel026a60c2020-02-12 10:53:51 -0500262 GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips4e105e22020-07-16 09:18:50 -0400263 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888);
Greg Daniel026a60c2020-02-12 10:53:51 -0500264 views[0] = {{std::move(proxy), kBottomLeft_GrSurfaceOrigin, swizzle},
265 GrColorType::kRGBA_8888, kPremul_SkAlphaType};
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500266 }
267 {
Brian Salomona56a7462020-02-07 14:17:25 -0500268 static constexpr SkISize kDummyDims = {16, 22};
Robert Phillips4e105e22020-07-16 09:18:50 -0400269 const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
270 GrRenderable::kNo);
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400271 auto proxy = proxyProvider->createProxy(format, kDummyDims, GrRenderable::kNo, 1, mipMapped,
272 SkBackingFit::kExact, SkBudgeted::kNo,
Greg Daniel026a60c2020-02-12 10:53:51 -0500273 GrProtected::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips4e105e22020-07-16 09:18:50 -0400274 GrSwizzle swizzle = caps->getReadSwizzle(format, GrColorType::kAlpha_8);
Greg Daniel026a60c2020-02-12 10:53:51 -0500275 views[1] = {{std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle},
Brian Salomona56a7462020-02-07 14:17:25 -0500276 GrColorType::kAlpha_8, kPremul_SkAlphaType};
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500277 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000278
Greg Daniel026a60c2020-02-12 10:53:51 -0500279 if (!std::get<0>(views[0]) || !std::get<0>(views[1])) {
joshualitt65171342014-10-09 07:25:36 -0700280 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700281 return false;
282 }
283
Brian Osman9f772a42017-07-10 13:03:50 -0400284 SkRandom random;
bsalomonf3261af2016-04-05 10:57:13 -0700285 static const int NUM_TESTS = 1024;
joshualitt6c891102015-05-13 08:51:49 -0700286 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700287 // setup random render target(can fail)
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500288 auto surfaceDrawContext = random_render_target_context(direct, &random, caps);
289 if (!surfaceDrawContext) {
290 SkDebugf("Could not allocate surfaceDrawContext");
joshualittf5883a62016-01-13 07:47:38 -0800291 return false;
292 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800293
Brian Salomon17726632017-05-12 14:09:46 -0400294 GrPaint paint;
John Stiles87d0a2f2020-08-10 13:12:41 -0400295 GrProcessorTestData ptd(&random, direct, /*maxTreeDepth=*/1, SK_ARRAY_COUNT(views), views);
Greg Daniel78325c12017-06-19 16:39:13 -0400296 set_random_color_coverage_stages(&paint, &ptd, maxStages, maxLevels);
Brian Salomon17726632017-05-12 14:09:46 -0400297 set_random_xpf(&paint, &ptd);
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500298 GrDrawRandomOp(&random, surfaceDrawContext.get(), std::move(paint));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000299 }
joshualitt6c891102015-05-13 08:51:49 -0700300 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
Robert Phillips4e105e22020-07-16 09:18:50 -0400301 direct->flush(GrFlushInfo());
302 direct->submit(false);
bsalomonb5b60322015-09-14 12:26:33 -0700303
304 // Validate that GrFPs work correctly without an input.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500305 auto surfaceDrawContext = GrSurfaceDrawContext::Make(
Robert Phillips4e105e22020-07-16 09:18:50 -0400306 direct, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
Greg Daniele20fcad2020-01-08 11:52:34 -0500307 {kRenderTargetWidth, kRenderTargetHeight});
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500308 if (!surfaceDrawContext) {
309 SkDebugf("Could not allocate a surfaceDrawContext");
robertphillips82ec6e52016-05-19 14:01:05 -0700310 return false;
311 }
312
Brian Salomon1c053642017-07-24 10:16:19 -0400313 int fpFactoryCnt = GrFragmentProcessorTestFactory::Count();
bsalomonb5b60322015-09-14 12:26:33 -0700314 for (int i = 0; i < fpFactoryCnt; ++i) {
315 // Since FP factories internally randomize, call each 10 times.
316 for (int j = 0; j < 10; ++j) {
John Stiles87d0a2f2020-08-10 13:12:41 -0400317 GrProcessorTestData ptd(&random, direct, /*maxTreeDepth=*/1, SK_ARRAY_COUNT(views),
318 views);
bsalomonb5b60322015-09-14 12:26:33 -0700319
Brian Salomon17726632017-05-12 14:09:46 -0400320 GrPaint paint;
321 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400322 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &ptd);
323 auto blockFP = BlockInputFragmentProcessor::Make(std::move(fp));
John Stiles5933d7d2020-07-21 12:28:35 -0400324 paint.setColorFragmentProcessor(std::move(blockFP));
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500325 GrDrawRandomOp(&random, surfaceDrawContext.get(), std::move(paint));
Robert Phillips4e105e22020-07-16 09:18:50 -0400326
327 direct->flush(GrFlushInfo());
328 direct->submit(false);
bsalomonb5b60322015-09-14 12:26:33 -0700329 }
330 }
331
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000332 return true;
333}
Hal Canary6f6961e2017-01-31 13:50:44 -0500334#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000335
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400336static int get_programs_max_stages(const sk_gpu_test::ContextInfo& ctxInfo) {
Brian Salomon1171d312020-03-19 08:47:44 -0400337 int maxStages = 6;
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400338#ifdef SK_GL
Robert Phillips6d344c32020-07-06 10:56:46 -0400339 auto context = ctxInfo.directContext();
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400340 if (skiatest::IsGLContextType(ctxInfo.type())) {
341 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
342 if (kGLES_GrGLStandard == gpu->glStandard()) {
343 // We've had issues with driver crashes and HW limits being exceeded with many effects on
344 // Android devices. We have passes on ARM devices with the default number of stages.
345 // TODO When we run ES 3.00 GLSL in more places, test again
Brian Salomone334c592017-05-15 11:00:58 -0400346#ifdef SK_BUILD_FOR_ANDROID
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400347 if (kARM_GrGLVendor != gpu->ctxInfo().vendor()) {
348 maxStages = 1;
349 }
kkinnunen3e980c32015-12-23 01:33:00 -0800350#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400351 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Brian Osman4e1868c2017-05-26 15:56:32 -0400352#ifdef SK_BUILD_FOR_IOS
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400353 maxStages = 3;
Brian Salomone334c592017-05-15 11:00:58 -0400354#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400355 }
Brian Salomonb0aab2c2020-04-16 16:01:25 -0400356 // On Angle D3D we will hit a limit of out variables if we use too many stages. This is
357 // particularly true on D3D9 with a low limit on varyings and the fact that every varying is
358 // packed as though it has 4 components.
359 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType) {
360 maxStages = 2;
361 } else if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400362 maxStages = 3;
363 }
Greg Danield895ca62017-06-19 14:39:43 -0400364 }
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400365#endif
Brian Salomone334c592017-05-15 11:00:58 -0400366 return maxStages;
367}
368
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400369static int get_programs_max_levels(const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Daniel78325c12017-06-19 16:39:13 -0400370 // A full tree with 5 levels (31 nodes) may cause a program that exceeds shader limits
371 // (e.g. uniform or varying limits); maxTreeLevels should be a number from 1 to 4 inclusive.
372 int maxTreeLevels = 4;
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400373 if (skiatest::IsGLContextType(ctxInfo.type())) {
374 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Greg Daniel78325c12017-06-19 16:39:13 -0400375#ifdef SK_BUILD_FOR_IOS
Michael Ludwig6387dc12019-10-22 15:39:41 +0000376 maxTreeLevels = 2;
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400377#endif
Brian Salomon32d41b72020-09-21 10:22:49 -0400378#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_GL)
Robert Phillips6d344c32020-07-06 10:56:46 -0400379 GrGLGpu* gpu = static_cast<GrGLGpu*>(ctxInfo.directContext()->priv().getGpu());
Brian Salomon9c4ee9c2019-12-20 16:20:54 -0500380 // Tecno Spark 3 Pro with Power VR Rogue GE8300 will fail shader compiles with
381 // no message if the shader is particularly long.
382 if (gpu->ctxInfo().vendor() == kImagination_GrGLVendor) {
383 maxTreeLevels = 3;
384 }
385#endif
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400386 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
387 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
388 // On Angle D3D we will hit a limit of out variables if we use too many stages.
389 maxTreeLevels = 2;
390 }
Greg Daniel78325c12017-06-19 16:39:13 -0400391 }
392 return maxTreeLevels;
393}
394
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400395static void test_programs(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo) {
396 int maxStages = get_programs_max_stages(ctxInfo);
kkinnunen3e980c32015-12-23 01:33:00 -0800397 if (maxStages == 0) {
398 return;
399 }
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400400 int maxLevels = get_programs_max_levels(ctxInfo);
Greg Daniel78325c12017-06-19 16:39:13 -0400401 if (maxLevels == 0) {
402 return;
403 }
Brian Osman9f772a42017-07-10 13:03:50 -0400404
Robert Phillips6d344c32020-07-06 10:56:46 -0400405 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.directContext(), maxStages,
Greg Daniel78325c12017-06-19 16:39:13 -0400406 maxLevels));
kkinnunen3e980c32015-12-23 01:33:00 -0800407}
408
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400409DEF_GPUTEST(Programs, reporter, options) {
bsalomon3318ee72015-03-16 11:56:29 -0700410 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
411 // skbug 3330
412#ifdef SK_BUILD_FOR_WIN
413 GrAutoLocaleSetter als("sv-SE");
414#else
415 GrAutoLocaleSetter als("sv_SE.UTF-8");
416#endif
417
joshualitt6c891102015-05-13 08:51:49 -0700418 // We suppress prints to avoid spew
Brian Salomondcfca432017-11-15 15:48:03 -0500419 GrContextOptions opts = options;
joshualitt6c891102015-05-13 08:51:49 -0700420 opts.fSuppressPrints = true;
bsalomon3724e572016-03-30 18:56:19 -0700421 sk_gpu_test::GrContextFactory debugFactory(opts);
Jim Van Verth5a8f3e42019-10-24 10:23:26 -0400422 skiatest::RunWithGPUTestContexts(test_programs, &skiatest::IsRenderingGLOrMetalContextType,
423 reporter, opts);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000424}