blob: d36c48d82e4db7f2859bc1845a5341755623849b [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 "GrBatch.h"
17#include "GrBatchTest.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000018#include "GrContextFactory.h"
egdaniel605dd0f2014-11-12 08:35:25 -080019#include "GrInvariantOutput.h"
egdaniel8dd688b2015-01-22 10:16:09 -080020#include "GrPipeline.h"
bsalomon4061b122015-05-29 10:26:19 -070021#include "GrResourceProvider.h"
joshualitt2c93efe2014-11-06 12:57:13 -080022#include "GrTest.h"
egdaniel95131432014-12-09 11:15:43 -080023#include "GrXferProcessor.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000024#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000025#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000026#include "Test.h"
joshualitt2c93efe2014-11-06 12:57:13 -080027#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080028#include "effects/GrPorterDuffXferProcessor.h"
jvanverth39edf762014-12-22 11:44:19 -080029#include "gl/GrGLGpu.h"
joshualitt2c93efe2014-11-06 12:57:13 -080030#include "gl/GrGLPathRendering.h"
joshualitt2c93efe2014-11-06 12:57:13 -080031#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000032
joshualitt65171342014-10-09 07:25:36 -070033/*
bsalomon98b33eb2014-10-15 11:05:26 -070034 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070035 * whole thing correctly
36 */
37static const uint32_t kMaxKeySize = 1024;
38
joshualitt65171342014-10-09 07:25:36 -070039class GLBigKeyProcessor : public GrGLFragmentProcessor {
40public:
joshualitteb2a6762014-12-04 11:35:33 -080041 GLBigKeyProcessor(const GrProcessor&) {}
joshualittd9097592014-10-07 08:37:36 -070042
wangyix7c157a92015-07-22 15:08:53 -070043 virtual void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070044 // pass through
wangyix7c157a92015-07-22 15:08:53 -070045 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
46 fsBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
joshualitt6c891102015-05-13 08:51:49 -070047 }
joshualitt65171342014-10-09 07:25:36 -070048
jvanverthcfc18862015-04-28 08:48:20 -070049 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070050 for (uint32_t i = 0; i < kMaxKeySize; i++) {
51 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070052 }
joshualittd9097592014-10-07 08:37:36 -070053 }
54
joshualitt65171342014-10-09 07:25:36 -070055private:
56 typedef GrGLFragmentProcessor INHERITED;
57};
58
joshualitteb2a6762014-12-04 11:35:33 -080059class BigKeyProcessor : public GrFragmentProcessor {
60public:
61 static GrFragmentProcessor* Create() {
62 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
63 return SkRef(gBigKeyProcessor);
64 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080067
jvanverthcfc18862015-04-28 08:48:20 -070068 virtual void getGLProcessorKey(const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -070069 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -080070 GLBigKeyProcessor::GenKey(*this, caps, b);
71 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 GrGLFragmentProcessor* createGLInstance() const override {
joshualitteb2a6762014-12-04 11:35:33 -080074 return SkNEW_ARGS(GLBigKeyProcessor, (*this));
75 }
76
77private:
78 BigKeyProcessor() {
79 this->initClassID<BigKeyProcessor>();
80 }
mtklein36352bf2015-03-25 18:17:31 -070081 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
82 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080083
84 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
85
86 typedef GrFragmentProcessor INHERITED;
87};
88
89GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
90
joshualitt0067ff52015-07-08 14:26:19 -070091GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) {
joshualitteb2a6762014-12-04 11:35:33 -080092 return BigKeyProcessor::Create();
93}
94
joshualitt65171342014-10-09 07:25:36 -070095/*
96 * Begin test code
97 */
98static const int kRenderTargetHeight = 1;
99static const int kRenderTargetWidth = 1;
100
bsalomon4061b122015-05-29 10:26:19 -0700101static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700102 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700103 // setup render target
104 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700105 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700106 texDesc.fWidth = kRenderTargetWidth;
107 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700108 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700109 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
110 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
111 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700112 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700113
bsalomon8718aaf2015-02-19 07:24:21 -0800114 GrUniqueKey key;
115 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700116 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800117 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700118 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800119 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700120
bsalomon4061b122015-05-29 10:26:19 -0700121 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700122 if (!texture) {
bsalomon4061b122015-05-29 10:26:19 -0700123 texture = textureProvider->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800124 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700125 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700126 }
joshualittd9097592014-10-07 08:37:36 -0700127 }
bsalomon37f9a262015-02-02 13:00:10 -0800128 return texture ? texture->asRenderTarget() : NULL;
bsalomon@google.com91207482013-02-12 21:45:24 +0000129}
130
joshualitt0067ff52015-07-08 14:26:19 -0700131static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) {
132 SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::CreateStage(d));
egdanielc2304142014-12-11 13:15:13 -0800133 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800134 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800135}
136
joshualitt0067ff52015-07-08 14:26:19 -0700137static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder,
138 GrProcessorTestData* d, int maxStages) {
139 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
140 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700141
joshualitt65171342014-10-09 07:25:36 -0700142 for (int s = 0; s < numProcs;) {
bsalomonae59b772014-11-19 08:23:49 -0800143 SkAutoTUnref<const GrFragmentProcessor> fp(
joshualitt0067ff52015-07-08 14:26:19 -0700144 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(d));
joshualitt65171342014-10-09 07:25:36 -0700145 SkASSERT(fp);
146
joshualitt65171342014-10-09 07:25:36 -0700147 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700148 if (s < numColorProcs) {
egdaniel8dd688b2015-01-22 10:16:09 -0800149 pipelineBuilder->addColorProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700150 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800151 pipelineBuilder->addCoverageProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700152 }
153 ++s;
154 }
155}
156
egdaniel8dd688b2015-01-22 10:16:09 -0800157static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700158 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700159 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700160 state |= random->nextBool() * i;
161 }
joshualitt6c891102015-05-13 08:51:49 -0700162
163 // If we don't have an MSAA rendertarget then we have to disable useHWAA
164 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
vbuzinovdded6962015-06-12 08:59:45 -0700165 !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) {
joshualitt6c891102015-05-13 08:51:49 -0700166 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
167 }
egdaniel8dd688b2015-01-22 10:16:09 -0800168 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700169}
170
joshualitt65171342014-10-09 07:25:36 -0700171// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800172static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700173 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
174 kReplace_StencilOp,
175 kReplace_StencilOp,
176 kAlways_StencilFunc,
177 0xffff,
178 0xffff,
179 0xffff);
180 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
181 kKeep_StencilOp,
182 kKeep_StencilOp,
183 kNever_StencilFunc,
184 0xffff,
185 0xffff,
186 0xffff);
187
188 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800189 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700190 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800191 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700192 }
193}
joshualitt249af152014-09-15 11:41:13 -0700194
bsalomon4061b122015-05-29 10:26:19 -0700195bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) {
joshualitt65171342014-10-09 07:25:36 -0700196 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700197 GrSurfaceDesc dummyDesc;
bsalomonbea01502015-07-16 10:00:28 -0700198 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000199 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000200 dummyDesc.fWidth = 34;
201 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700202 SkAutoTUnref<GrTexture> dummyTexture1(
203 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700204 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000205 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
206 dummyDesc.fWidth = 16;
207 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700208 SkAutoTUnref<GrTexture> dummyTexture2(
209 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000210
bsalomone904c092014-07-17 10:50:59 -0700211 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700212 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700213 return false;
214 }
215
joshualitt65171342014-10-09 07:25:36 -0700216 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
217
joshualitt54e0c122014-11-19 09:38:51 -0800218 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800219 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800220
joshualitt6c891102015-05-13 08:51:49 -0700221 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800222 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000223
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000224 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700225 static const int NUM_TESTS = 2048;
226 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700227 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700228 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
229 fResourceProvider, &random, this->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800230 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700231 SkDebugf("Could not allocate render target");
232 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000233 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000234
egdaniel8dd688b2015-01-22 10:16:09 -0800235 GrPipelineBuilder pipelineBuilder;
236 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800237 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000238
bsalomon4061b122015-05-29 10:26:19 -0700239 SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700240 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700241
joshualitt9cc17752015-07-09 06:28:14 -0700242 GrProcessorDataManager procDataManager;
243 GrProcessorTestData ptd(&random, context, &procDataManager, fGpu->caps(), dummyTextures);
joshualitt0067ff52015-07-08 14:26:19 -0700244 set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages);
245 set_random_xpf(&pipelineBuilder, &ptd);
egdaniel8dd688b2015-01-22 10:16:09 -0800246 set_random_state(&pipelineBuilder, &random);
247 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700248
joshualitt1c735482015-07-13 08:08:25 -0700249 this->drawBatch(pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000250 }
joshualitt6c891102015-05-13 08:51:49 -0700251
252 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
253 this->flush();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000254 return true;
255}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000256
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000257DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon3318ee72015-03-16 11:56:29 -0700258 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
259 // skbug 3330
260#ifdef SK_BUILD_FOR_WIN
261 GrAutoLocaleSetter als("sv-SE");
262#else
263 GrAutoLocaleSetter als("sv_SE.UTF-8");
264#endif
265
joshualitt6c891102015-05-13 08:51:49 -0700266 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700267 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700268 opts.fSuppressPrints = true;
269 GrContextFactory debugFactory(opts);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000270 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
joshualitt6c891102015-05-13 08:51:49 -0700271 GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700272 if (context) {
bsalomon861e1032014-12-16 07:33:49 -0800273 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700274
275 /*
276 * For the time being, we only support the test with desktop GL or for android on
277 * ARM platforms
278 * TODO When we run ES 3.00 GLSL in more places, test again
279 */
280 int maxStages;
281 if (kGL_GrGLStandard == gpu->glStandard() ||
282 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
283 maxStages = 6;
284 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
285 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
286 maxStages = 1;
287 } else {
288 return;
289 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000290#if SK_ANGLE
291 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
292 if (type == GrContextFactory::kANGLE_GLContextType) {
egdaniel305a8892015-02-17 11:18:38 -0800293 maxStages = 2;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000294 }
295#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800296 GrTestTarget target;
297 context->getTestTarget(&target);
bsalomon4061b122015-05-29 10:26:19 -0700298 REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000299 }
300 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000301}
302
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000303#endif