blob: c2dd57291c469932a65b1fcd6d8420a5cb6713da [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
bsalomon@google.comd4726202012-08-03 14:34:46 +00008// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000010#include "SkTypes.h"
11
Brian Osmanc7ad40f2018-05-31 14:27:17 -040012#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000013
bsalomon6f7f2012015-03-16 14:00:52 -070014#include "GrAutoLocaleSetter.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000015#include "GrContextFactory.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070016#include "GrContextPriv.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050017#include "GrDrawOpTest.h"
robertphillipsa13e2022015-11-11 12:01:09 -080018#include "GrDrawingManager.h"
egdaniel8dd688b2015-01-22 10:16:09 -080019#include "GrPipeline.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050020#include "GrRenderTargetContextPriv.h"
egdaniel95131432014-12-09 11:15:43 -080021#include "GrXferProcessor.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000022#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000023#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000024#include "Test.h"
joshualitt74417822015-08-07 11:42:16 -070025
Brian Salomon89527432016-12-16 09:52:16 -050026#include "ops/GrDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070027
joshualitt2c93efe2014-11-06 12:57:13 -080028#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080029#include "effects/GrPorterDuffXferProcessor.h"
wangyix059dffa2015-09-10 06:57:05 -070030#include "effects/GrXfermodeFragmentProcessor.h"
joshualitt74417822015-08-07 11:42:16 -070031
jvanverth39edf762014-12-22 11:44:19 -080032#include "gl/GrGLGpu.h"
egdaniel64c47282015-11-13 06:54:19 -080033#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080034#include "glsl/GrGLSLFragmentShaderBuilder.h"
35#include "glsl/GrGLSLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000036
joshualitt65171342014-10-09 07:25:36 -070037/*
bsalomon98b33eb2014-10-15 11:05:26 -070038 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070039 * whole thing correctly
40 */
41static const uint32_t kMaxKeySize = 1024;
42
egdaniel64c47282015-11-13 06:54:19 -080043class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070044public:
robertphillips9cdb9922016-02-03 12:25:40 -080045 void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070046 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080047 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070048 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080049 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070050 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080051 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070052 }
joshualitt6c891102015-05-13 08:51:49 -070053 }
joshualitt65171342014-10-09 07:25:36 -070054
Brian Salomon94efbf52016-11-29 13:43:05 -050055 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070056 for (uint32_t i = 0; i < kMaxKeySize; i++) {
57 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070058 }
joshualittd9097592014-10-07 08:37:36 -070059 }
60
joshualitt65171342014-10-09 07:25:36 -070061private:
egdaniel64c47282015-11-13 06:54:19 -080062 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070063};
64
joshualitteb2a6762014-12-04 11:35:33 -080065class BigKeyProcessor : public GrFragmentProcessor {
66public:
Brian Salomonaff329b2017-08-11 09:40:37 -040067 static std::unique_ptr<GrFragmentProcessor> Make() {
68 return std::unique_ptr<GrFragmentProcessor>(new BigKeyProcessor);
joshualitteb2a6762014-12-04 11:35:33 -080069 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080072
egdaniel57d3b032015-11-13 11:57:27 -080073 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080074 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080075 }
76
Brian Salomonaff329b2017-08-11 09:40:37 -040077 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
Brian Salomonb17e6392017-07-28 13:41:51 -040078
joshualitteb2a6762014-12-04 11:35:33 -080079private:
Ethan Nicholasabff9562017-10-09 10:54:08 -040080 BigKeyProcessor() : INHERITED(kBigKeyProcessor_ClassID, kNone_OptimizationFlags) { }
Brian Salomon94efbf52016-11-29 13:43:05 -050081 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080082 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070083 GLBigKeyProcessor::GenKey(*this, caps, b);
84 }
mtklein36352bf2015-03-25 18:17:31 -070085 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
joshualitteb2a6762014-12-04 11:35:33 -080086
Brian Salomon0c26a9d2017-07-06 10:09:38 -040087 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
joshualitteb2a6762014-12-04 11:35:33 -080088
89 typedef GrFragmentProcessor INHERITED;
90};
91
92GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
93
Hal Canary6f6961e2017-01-31 13:50:44 -050094#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -040095std::unique_ptr<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) {
bungeman06ca8ec2016-06-09 08:01:03 -070096 return BigKeyProcessor::Make();
joshualitteb2a6762014-12-04 11:35:33 -080097}
Hal Canary6f6961e2017-01-31 13:50:44 -050098#endif
joshualitteb2a6762014-12-04 11:35:33 -080099
bsalomonb5b60322015-09-14 12:26:33 -0700100//////////////////////////////////////////////////////////////////////////////
101
102class BlockInputFragmentProcessor : public GrFragmentProcessor {
103public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400104 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
105 return std::unique_ptr<GrFragmentProcessor>(new BlockInputFragmentProcessor(std::move(fp)));
bsalomonb5b60322015-09-14 12:26:33 -0700106 }
107
108 const char* name() const override { return "Block Input"; }
109
egdaniel57d3b032015-11-13 11:57:27 -0800110 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700111
Brian Salomonaff329b2017-08-11 09:40:37 -0400112 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400113 return Make(this->childProcessor(0).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400114 }
115
bsalomonb5b60322015-09-14 12:26:33 -0700116private:
egdaniel64c47282015-11-13 06:54:19 -0800117 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700118 public:
119 void emitCode(EmitArgs& args) override {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400120 this->emitChild(0, args);
bsalomonb5b60322015-09-14 12:26:33 -0700121 }
122
123 private:
egdaniel64c47282015-11-13 06:54:19 -0800124 typedef GrGLSLFragmentProcessor INHERITED;
bsalomonb5b60322015-09-14 12:26:33 -0700125 };
126
Brian Salomonaff329b2017-08-11 09:40:37 -0400127 BlockInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400128 : INHERITED(kBlockInputFragmentProcessor_ClassID, kNone_OptimizationFlags) {
bungeman06ca8ec2016-06-09 08:01:03 -0700129 this->registerChildProcessor(std::move(child));
bsalomonb5b60322015-09-14 12:26:33 -0700130 }
131
Brian Salomon94efbf52016-11-29 13:43:05 -0500132 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700133
134 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
135
bsalomonb5b60322015-09-14 12:26:33 -0700136 typedef GrFragmentProcessor INHERITED;
137};
138
139//////////////////////////////////////////////////////////////////////////////
140
joshualitt65171342014-10-09 07:25:36 -0700141/*
142 * Begin test code
143 */
144static const int kRenderTargetHeight = 1;
145static const int kRenderTargetWidth = 1;
146
Brian Osman11052242016-10-27 14:47:55 -0400147static sk_sp<GrRenderTargetContext> random_render_target_context(GrContext* context,
148 SkRandom* random,
149 const GrCaps* caps) {
robertphillips82ec6e52016-05-19 14:01:05 -0700150 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
151 : kBottomLeft_GrSurfaceOrigin;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500152 int sampleCnt =
153 random->nextBool() ? caps->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig) : 1;
154 // Above could be 0 if msaa isn't supported.
155 sampleCnt = SkTMax(1, sampleCnt);
joshualitt6c891102015-05-13 08:51:49 -0700156
Greg Daniel4065d452018-11-16 15:43:41 -0500157 const GrBackendFormat format = caps->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
158
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500159 sk_sp<GrRenderTargetContext> renderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500160 context->contextPriv().makeDeferredRenderTargetContext(format,
161 SkBackingFit::kExact,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500162 kRenderTargetWidth,
163 kRenderTargetHeight,
164 kRGBA_8888_GrPixelConfig,
165 nullptr,
166 sampleCnt,
167 GrMipMapped::kNo,
168 origin));
Brian Osman11052242016-10-27 14:47:55 -0400169 return renderTargetContext;
bsalomon@google.com91207482013-02-12 21:45:24 +0000170}
171
Hal Canary6f6961e2017-01-31 13:50:44 -0500172#if GR_TEST_UTILS
robertphillips28a838e2016-06-23 14:07:00 -0700173static void set_random_xpf(GrPaint* paint, GrProcessorTestData* d) {
Brian Salomona1633922017-01-09 11:46:10 -0500174 paint->setXPFactory(GrXPFactoryTestFactory::Get(d));
egdanielc2304142014-12-11 13:15:13 -0800175}
176
Brian Salomonaff329b2017-08-11 09:40:37 -0400177static std::unique_ptr<GrFragmentProcessor> create_random_proc_tree(GrProcessorTestData* d,
178 int minLevels, int maxLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700179 SkASSERT(1 <= minLevels);
180 SkASSERT(minLevels <= maxLevels);
181
182 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
183 // If returning a leaf node, make sure that it doesn't have children (e.g. another
184 // GrComposeEffect)
185 const float terminateProbability = 0.3f;
186 if (1 == minLevels) {
187 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
188 if (terminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400189 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700190 while (true) {
Brian Salomon1c053642017-07-24 10:16:19 -0400191 fp = GrFragmentProcessorTestFactory::Make(d);
wangyix059dffa2015-09-10 06:57:05 -0700192 SkASSERT(fp);
193 if (0 == fp->numChildProcessors()) {
194 break;
195 }
wangyix059dffa2015-09-10 06:57:05 -0700196 }
197 return fp;
198 }
199 }
200 // If we didn't terminate, choose either the left or right subtree to fulfill
201 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
Brian Salomona12c1532017-02-13 12:41:44 -0500202 // Also choose a random xfer mode.
wangyix059dffa2015-09-10 06:57:05 -0700203 if (minLevels > 1) {
204 --minLevels;
205 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400206 auto minLevelsChild = create_random_proc_tree(d, minLevels, maxLevels - 1);
207 std::unique_ptr<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1));
Mike Reed7d954ad2016-10-28 15:42:34 -0400208 SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0,
Brian Salomona12c1532017-02-13 12:41:44 -0500209 (int)SkBlendMode::kLastMode));
Brian Salomonaff329b2017-08-11 09:40:37 -0400210 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700211 if (d->fRandom->nextF() < 0.5f) {
bungeman06ca8ec2016-06-09 08:01:03 -0700212 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(minLevelsChild),
213 std::move(otherChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700214 SkASSERT(fp);
215 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700216 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(otherChild),
217 std::move(minLevelsChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700218 SkASSERT(fp);
219 }
220 return fp;
221}
222
robertphillips28a838e2016-06-23 14:07:00 -0700223static void set_random_color_coverage_stages(GrPaint* paint,
224 GrProcessorTestData* d,
Greg Daniel78325c12017-06-19 16:39:13 -0400225 int maxStages,
226 int maxTreeLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700227 // Randomly choose to either create a linear pipeline of procs or create one proc tree
228 const float procTreeProbability = 0.5f;
229 if (d->fRandom->nextF() < procTreeProbability) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400230 std::unique_ptr<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400231 if (fp) {
232 paint->addColorFragmentProcessor(std::move(fp));
233 }
wangyix059dffa2015-09-10 06:57:05 -0700234 } else {
235 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
236 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700237
wangyix059dffa2015-09-10 06:57:05 -0700238 for (int s = 0; s < numProcs;) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400239 std::unique_ptr<GrFragmentProcessor> fp(GrFragmentProcessorTestFactory::Make(d));
wangyix059dffa2015-09-10 06:57:05 -0700240 SkASSERT(fp);
241
242 // finally add the stage to the correct pipeline in the drawstate
243 if (s < numColorProcs) {
robertphillips28a838e2016-06-23 14:07:00 -0700244 paint->addColorFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700245 } else {
robertphillips28a838e2016-06-23 14:07:00 -0700246 paint->addCoverageFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700247 }
248 ++s;
joshualitt65171342014-10-09 07:25:36 -0700249 }
joshualitt65171342014-10-09 07:25:36 -0700250 }
251}
252
Hal Canary6f6961e2017-01-31 13:50:44 -0500253#endif
joshualitt249af152014-09-15 11:41:13 -0700254
Hal Canary6f6961e2017-01-31 13:50:44 -0500255#if !GR_TEST_UTILS
256bool GrDrawingManager::ProgramUnitTest(GrContext*, int) { return true; }
257#else
Greg Daniel78325c12017-06-19 16:39:13 -0400258bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages, int maxLevels) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700259 GrDrawingManager* drawingManager = context->contextPriv().drawingManager();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500260 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
robertphillipsa13e2022015-11-11 12:01:09 -0800261
Robert Phillipse78b7252017-04-06 07:59:41 -0400262 sk_sp<GrTextureProxy> proxies[2];
263
joshualitt65171342014-10-09 07:25:36 -0700264 // setup dummy textures
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500265 {
266 GrSurfaceDesc dummyDesc;
267 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500268 dummyDesc.fWidth = 34;
269 dummyDesc.fHeight = 18;
270 dummyDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500271 const GrBackendFormat format =
272 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
273 proxies[0] = proxyProvider->createProxy(format, dummyDesc, kBottomLeft_GrSurfaceOrigin,
Greg Daniel09c94002018-06-08 22:11:51 +0000274 GrMipMapped::kYes, SkBackingFit::kExact,
275 SkBudgeted::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500276 }
277 {
278 GrSurfaceDesc dummyDesc;
279 dummyDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500280 dummyDesc.fWidth = 16;
281 dummyDesc.fHeight = 22;
282 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500283 const GrBackendFormat format =
284 context->contextPriv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
285 proxies[1] = proxyProvider->createProxy(format, dummyDesc, kTopLeft_GrSurfaceOrigin,
Greg Daniel09c94002018-06-08 22:11:51 +0000286 GrMipMapped::kYes, SkBackingFit::kExact,
287 SkBudgeted::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500288 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000289
Robert Phillipse78b7252017-04-06 07:59:41 -0400290 if (!proxies[0] || !proxies[1]) {
joshualitt65171342014-10-09 07:25:36 -0700291 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700292 return false;
293 }
294
Brian Salomond818ebf2018-07-02 14:08:49 +0000295 // dummy scissor state
296 GrScissorState scissor;
297
Brian Osman9f772a42017-07-10 13:03:50 -0400298 SkRandom random;
bsalomonf3261af2016-04-05 10:57:13 -0700299 static const int NUM_TESTS = 1024;
joshualitt6c891102015-05-13 08:51:49 -0700300 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700301 // setup random render target(can fail)
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400302 sk_sp<GrRenderTargetContext> renderTargetContext(
303 random_render_target_context(context, &random, context->contextPriv().caps()));
Brian Osman11052242016-10-27 14:47:55 -0400304 if (!renderTargetContext) {
305 SkDebugf("Could not allocate renderTargetContext");
joshualittf5883a62016-01-13 07:47:38 -0800306 return false;
307 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800308
Brian Salomon17726632017-05-12 14:09:46 -0400309 GrPaint paint;
Robert Phillipse78b7252017-04-06 07:59:41 -0400310 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
Greg Daniel78325c12017-06-19 16:39:13 -0400311 set_random_color_coverage_stages(&paint, &ptd, maxStages, maxLevels);
Brian Salomon17726632017-05-12 14:09:46 -0400312 set_random_xpf(&paint, &ptd);
Brian Salomon17726632017-05-12 14:09:46 -0400313 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000314 }
joshualitt6c891102015-05-13 08:51:49 -0700315 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
Robert Phillips7ee385e2017-03-30 08:02:11 -0400316 drawingManager->flush(nullptr);
bsalomonb5b60322015-09-14 12:26:33 -0700317
Greg Daniel4065d452018-11-16 15:43:41 -0500318 const GrBackendFormat format =
319 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
bsalomonb5b60322015-09-14 12:26:33 -0700320 // Validate that GrFPs work correctly without an input.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500321 sk_sp<GrRenderTargetContext> renderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500322 context->contextPriv().makeDeferredRenderTargetContext(format,
323 SkBackingFit::kExact,
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500324 kRenderTargetWidth,
325 kRenderTargetHeight,
326 kRGBA_8888_GrPixelConfig,
327 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400328 if (!renderTargetContext) {
329 SkDebugf("Could not allocate a renderTargetContext");
robertphillips82ec6e52016-05-19 14:01:05 -0700330 return false;
331 }
332
Brian Salomon1c053642017-07-24 10:16:19 -0400333 int fpFactoryCnt = GrFragmentProcessorTestFactory::Count();
bsalomonb5b60322015-09-14 12:26:33 -0700334 for (int i = 0; i < fpFactoryCnt; ++i) {
335 // Since FP factories internally randomize, call each 10 times.
336 for (int j = 0; j < 10; ++j) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400337 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
bsalomonb5b60322015-09-14 12:26:33 -0700338
Brian Salomon17726632017-05-12 14:09:46 -0400339 GrPaint paint;
340 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400341 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &ptd);
342 auto blockFP = BlockInputFragmentProcessor::Make(std::move(fp));
Brian Salomon17726632017-05-12 14:09:46 -0400343 paint.addColorFragmentProcessor(std::move(blockFP));
344 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
Robert Phillips7ee385e2017-03-30 08:02:11 -0400345 drawingManager->flush(nullptr);
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
Greg Danield895ca62017-06-19 14:39:43 -0400353static int get_glprograms_max_stages(const sk_gpu_test::ContextInfo& ctxInfo) {
354 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500355 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
Brian Salomone334c592017-05-15 11:00:58 -0400356 int maxStages = 6;
357 if (kGLES_GrGLStandard == gpu->glStandard()) {
358 // We've had issues with driver crashes and HW limits being exceeded with many effects on
359 // Android devices. We have passes on ARM devices with the default number of stages.
360 // TODO When we run ES 3.00 GLSL in more places, test again
361#ifdef SK_BUILD_FOR_ANDROID
362 if (kARM_GrGLVendor != gpu->ctxInfo().vendor()) {
363 maxStages = 1;
364 }
kkinnunen3e980c32015-12-23 01:33:00 -0800365#endif
Brian Salomone334c592017-05-15 11:00:58 -0400366 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Brian Osman4e1868c2017-05-26 15:56:32 -0400367#ifdef SK_BUILD_FOR_IOS
Brian Salomone334c592017-05-15 11:00:58 -0400368 maxStages = 3;
369#endif
370 }
Greg Danield895ca62017-06-19 14:39:43 -0400371 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
372 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
373 // On Angle D3D we will hit a limit of out variables if we use too many stages.
374 maxStages = 3;
375 }
Brian Salomone334c592017-05-15 11:00:58 -0400376 return maxStages;
377}
378
Greg Daniel78325c12017-06-19 16:39:13 -0400379static int get_glprograms_max_levels(const sk_gpu_test::ContextInfo& ctxInfo) {
380 // A full tree with 5 levels (31 nodes) may cause a program that exceeds shader limits
381 // (e.g. uniform or varying limits); maxTreeLevels should be a number from 1 to 4 inclusive.
382 int maxTreeLevels = 4;
383 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
384#ifdef SK_BUILD_FOR_IOS
385 maxTreeLevels = 2;
386#endif
387 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
388 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
389 // On Angle D3D we will hit a limit of out variables if we use too many stages.
390 maxTreeLevels = 2;
391 }
392 return maxTreeLevels;
393}
394
Brian Salomone334c592017-05-15 11:00:58 -0400395static void test_glprograms(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Danield895ca62017-06-19 14:39:43 -0400396 int maxStages = get_glprograms_max_stages(ctxInfo);
kkinnunen3e980c32015-12-23 01:33:00 -0800397 if (maxStages == 0) {
398 return;
399 }
Greg Daniel78325c12017-06-19 16:39:13 -0400400 int maxLevels = get_glprograms_max_levels(ctxInfo);
401 if (maxLevels == 0) {
402 return;
403 }
Brian Osman9f772a42017-07-10 13:03:50 -0400404
Greg Daniel78325c12017-06-19 16:39:13 -0400405 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages,
406 maxLevels));
kkinnunen3e980c32015-12-23 01:33:00 -0800407}
408
Brian Salomondcfca432017-11-15 15:48:03 -0500409DEF_GPUTEST(GLPrograms, 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);
Brian Salomone334c592017-05-15 11:00:58 -0400422 skiatest::RunWithGPUTestContexts(test_glprograms, &skiatest::IsRenderingGLContextType, reporter,
Brian Salomondcfca432017-11-15 15:48:03 -0500423 opts);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000424}
425
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000426#endif