blob: 4b3bcdffbe66d31ff2467a35e79a0681f1387f72 [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
bsalomon@google.comd4726202012-08-03 14:34:46 +00009// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000011#include "SkTypes.h"
12
13#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
14
bsalomon6f7f2012015-03-16 14:00:52 -070015#include "GrAutoLocaleSetter.h"
joshualitt6c891102015-05-13 08:51:49 -070016#include "GrBatchTest.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017#include "GrContextFactory.h"
egdaniel605dd0f2014-11-12 08:35:25 -080018#include "GrInvariantOutput.h"
egdaniel8dd688b2015-01-22 10:16:09 -080019#include "GrPipeline.h"
bsalomon4061b122015-05-29 10:26:19 -070020#include "GrResourceProvider.h"
joshualitt2c93efe2014-11-06 12:57:13 -080021#include "GrTest.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "GrXferProcessor.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000023#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000024#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000025#include "Test.h"
joshualitt74417822015-08-07 11:42:16 -070026
bsalomon16b99132015-08-13 14:55:50 -070027#include "batches/GrDrawBatch.h"
joshualitt74417822015-08-07 11:42:16 -070028
joshualitt2c93efe2014-11-06 12:57:13 -080029#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080030#include "effects/GrPorterDuffXferProcessor.h"
joshualitt74417822015-08-07 11:42:16 -070031
jvanverth39edf762014-12-22 11:44:19 -080032#include "gl/GrGLGpu.h"
joshualitt2c93efe2014-11-06 12:57:13 -080033#include "gl/GrGLPathRendering.h"
joshualitt2c93efe2014-11-06 12:57:13 -080034#include "gl/builders/GrGLProgramBuilder.h"
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
joshualitt65171342014-10-09 07:25:36 -070042class GLBigKeyProcessor : public GrGLFragmentProcessor {
43public:
joshualitteb2a6762014-12-04 11:35:33 -080044 GLBigKeyProcessor(const GrProcessor&) {}
joshualittd9097592014-10-07 08:37:36 -070045
wangyix7c157a92015-07-22 15:08:53 -070046 virtual void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070047 // pass through
wangyix7c157a92015-07-22 15:08:53 -070048 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
49 fsBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
joshualitt6c891102015-05-13 08:51:49 -070050 }
joshualitt65171342014-10-09 07:25:36 -070051
jvanverthcfc18862015-04-28 08:48:20 -070052 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070053 for (uint32_t i = 0; i < kMaxKeySize; i++) {
54 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070055 }
joshualittd9097592014-10-07 08:37:36 -070056 }
57
joshualitt65171342014-10-09 07:25:36 -070058private:
59 typedef GrGLFragmentProcessor INHERITED;
60};
61
joshualitteb2a6762014-12-04 11:35:33 -080062class BigKeyProcessor : public GrFragmentProcessor {
63public:
64 static GrFragmentProcessor* Create() {
65 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
66 return SkRef(gBigKeyProcessor);
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080070
wangyixb1daa862015-08-18 11:29:31 -070071 GrGLFragmentProcessor* onCreateGLInstance() const override {
halcanary385fe4d2015-08-26 13:07:48 -070072 return new GLBigKeyProcessor(*this);
joshualitteb2a6762014-12-04 11:35:33 -080073 }
74
75private:
76 BigKeyProcessor() {
77 this->initClassID<BigKeyProcessor>();
78 }
wangyix4b3050b2015-08-04 07:59:37 -070079 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
80 GrProcessorKeyBuilder* b) const override {
81 GLBigKeyProcessor::GenKey(*this, caps, b);
82 }
mtklein36352bf2015-03-25 18:17:31 -070083 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
84 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080085
86 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
87
88 typedef GrFragmentProcessor INHERITED;
89};
90
91GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
92
joshualitt0067ff52015-07-08 14:26:19 -070093GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) {
joshualitteb2a6762014-12-04 11:35:33 -080094 return BigKeyProcessor::Create();
95}
96
joshualitt65171342014-10-09 07:25:36 -070097/*
98 * Begin test code
99 */
100static const int kRenderTargetHeight = 1;
101static const int kRenderTargetWidth = 1;
102
bsalomon4061b122015-05-29 10:26:19 -0700103static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700104 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700105 // setup render target
106 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700107 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700108 texDesc.fWidth = kRenderTargetWidth;
109 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700110 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700111 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
112 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
113 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700114 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700115
bsalomon8718aaf2015-02-19 07:24:21 -0800116 GrUniqueKey key;
117 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700118 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800119 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700120 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800121 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700122
bsalomon4061b122015-05-29 10:26:19 -0700123 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700124 if (!texture) {
bsalomon4061b122015-05-29 10:26:19 -0700125 texture = textureProvider->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800126 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700127 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700128 }
joshualittd9097592014-10-07 08:37:36 -0700129 }
bsalomon37f9a262015-02-02 13:00:10 -0800130 return texture ? texture->asRenderTarget() : NULL;
bsalomon@google.com91207482013-02-12 21:45:24 +0000131}
132
joshualitt0067ff52015-07-08 14:26:19 -0700133static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) {
134 SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::CreateStage(d));
egdanielc2304142014-12-11 13:15:13 -0800135 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800136 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800137}
138
joshualitt0067ff52015-07-08 14:26:19 -0700139static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder,
140 GrProcessorTestData* d, int maxStages) {
141 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
142 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700143
joshualitt65171342014-10-09 07:25:36 -0700144 for (int s = 0; s < numProcs;) {
bsalomonae59b772014-11-19 08:23:49 -0800145 SkAutoTUnref<const GrFragmentProcessor> fp(
joshualitt0067ff52015-07-08 14:26:19 -0700146 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(d));
joshualitt65171342014-10-09 07:25:36 -0700147 SkASSERT(fp);
148
joshualitt65171342014-10-09 07:25:36 -0700149 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700150 if (s < numColorProcs) {
rmistry4bf69f7b2015-08-26 06:48:27 -0700151 pipelineBuilder->addColorProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700152 } else {
rmistry4bf69f7b2015-08-26 06:48:27 -0700153 pipelineBuilder->addCoverageProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700154 }
155 ++s;
156 }
157}
158
egdaniel8dd688b2015-01-22 10:16:09 -0800159static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700160 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700161 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700162 state |= random->nextBool() * i;
163 }
joshualitt6c891102015-05-13 08:51:49 -0700164
165 // If we don't have an MSAA rendertarget then we have to disable useHWAA
166 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
vbuzinovdded6962015-06-12 08:59:45 -0700167 !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) {
joshualitt6c891102015-05-13 08:51:49 -0700168 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
169 }
egdaniel8dd688b2015-01-22 10:16:09 -0800170 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700171}
172
joshualitt65171342014-10-09 07:25:36 -0700173// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800174static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700175 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
176 kReplace_StencilOp,
177 kReplace_StencilOp,
178 kAlways_StencilFunc,
179 0xffff,
180 0xffff,
181 0xffff);
182 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
183 kKeep_StencilOp,
184 kKeep_StencilOp,
185 kNever_StencilFunc,
186 0xffff,
187 0xffff,
188 0xffff);
189
190 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800191 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700192 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800193 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700194 }
195}
joshualitt249af152014-09-15 11:41:13 -0700196
bsalomon4061b122015-05-29 10:26:19 -0700197bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) {
joshualitt65171342014-10-09 07:25:36 -0700198 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700199 GrSurfaceDesc dummyDesc;
bsalomonbea01502015-07-16 10:00:28 -0700200 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000201 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000202 dummyDesc.fWidth = 34;
203 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700204 SkAutoTUnref<GrTexture> dummyTexture1(
bsalomoneae62002015-07-31 13:59:30 -0700205 context->textureProvider()->createTexture(dummyDesc, false, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700206 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000207 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
208 dummyDesc.fWidth = 16;
209 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700210 SkAutoTUnref<GrTexture> dummyTexture2(
bsalomoneae62002015-07-31 13:59:30 -0700211 context->textureProvider()->createTexture(dummyDesc, false, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000212
bsalomone904c092014-07-17 10:50:59 -0700213 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700214 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700215 return false;
216 }
217
joshualitt65171342014-10-09 07:25:36 -0700218 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
219
joshualitt54e0c122014-11-19 09:38:51 -0800220 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800221 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800222
joshualitt6c891102015-05-13 08:51:49 -0700223 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800224 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000225
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000226 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700227 static const int NUM_TESTS = 2048;
228 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700229 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700230 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
bsalomoneae62002015-07-31 13:59:30 -0700231 context->textureProvider(), &random, this->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800232 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700233 SkDebugf("Could not allocate render target");
234 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000235 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000236
egdaniel8dd688b2015-01-22 10:16:09 -0800237 GrPipelineBuilder pipelineBuilder;
238 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800239 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000240
bsalomonabd30f52015-08-13 13:34:48 -0700241 SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700242 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700243
joshualitt9cc17752015-07-09 06:28:14 -0700244 GrProcessorDataManager procDataManager;
245 GrProcessorTestData ptd(&random, context, &procDataManager, fGpu->caps(), dummyTextures);
joshualitt0067ff52015-07-08 14:26:19 -0700246 set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages);
247 set_random_xpf(&pipelineBuilder, &ptd);
egdaniel8dd688b2015-01-22 10:16:09 -0800248 set_random_state(&pipelineBuilder, &random);
249 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700250
joshualitt1c735482015-07-13 08:08:25 -0700251 this->drawBatch(pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000252 }
joshualitt6c891102015-05-13 08:51:49 -0700253
254 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
255 this->flush();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000256 return true;
257}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000258
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000259DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon3318ee72015-03-16 11:56:29 -0700260 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
261 // skbug 3330
262#ifdef SK_BUILD_FOR_WIN
263 GrAutoLocaleSetter als("sv-SE");
264#else
265 GrAutoLocaleSetter als("sv_SE.UTF-8");
266#endif
267
joshualitt6c891102015-05-13 08:51:49 -0700268 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700269 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700270 opts.fSuppressPrints = true;
271 GrContextFactory debugFactory(opts);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000272 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
joshualitt6c891102015-05-13 08:51:49 -0700273 GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700274 if (context) {
bsalomon861e1032014-12-16 07:33:49 -0800275 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700276
277 /*
278 * For the time being, we only support the test with desktop GL or for android on
279 * ARM platforms
280 * TODO When we run ES 3.00 GLSL in more places, test again
281 */
282 int maxStages;
283 if (kGL_GrGLStandard == gpu->glStandard() ||
284 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
285 maxStages = 6;
286 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
287 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
288 maxStages = 1;
289 } else {
290 return;
291 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000292#if SK_ANGLE
293 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
294 if (type == GrContextFactory::kANGLE_GLContextType) {
egdaniel305a8892015-02-17 11:18:38 -0800295 maxStages = 2;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000296 }
297#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800298 GrTestTarget target;
299 context->getTestTarget(&target);
bsalomon4061b122015-05-29 10:26:19 -0700300 REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000301 }
302 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000303}
304
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000305#endif