blob: d21ae5d4cd8f0d16ca4d13ec48a8b54a7ed364cb [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
joshualitt15988992014-10-09 15:04:05 -070043 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt65171342014-10-09 07:25:36 -070044 const GrFragmentProcessor& fp,
joshualitt65171342014-10-09 07:25:36 -070045 const char* outputColor,
46 const char* inputColor,
47 const TransformedCoordsArray&,
joshualitt6c891102015-05-13 08:51:49 -070048 const TextureSamplerArray&) {
49 // pass through
50 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
51 fsBuilder->codeAppendf("%s = %s;\n", outputColor, inputColor);
52 }
joshualitt65171342014-10-09 07:25:36 -070053
jvanverthcfc18862015-04-28 08:48:20 -070054 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070055 for (uint32_t i = 0; i < kMaxKeySize; i++) {
56 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070057 }
joshualittd9097592014-10-07 08:37:36 -070058 }
59
joshualitt65171342014-10-09 07:25:36 -070060private:
61 typedef GrGLFragmentProcessor INHERITED;
62};
63
joshualitteb2a6762014-12-04 11:35:33 -080064class BigKeyProcessor : public GrFragmentProcessor {
65public:
66 static GrFragmentProcessor* Create() {
67 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
68 return SkRef(gBigKeyProcessor);
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080072
jvanverthcfc18862015-04-28 08:48:20 -070073 virtual void getGLProcessorKey(const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -070074 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -080075 GLBigKeyProcessor::GenKey(*this, caps, b);
76 }
77
mtklein36352bf2015-03-25 18:17:31 -070078 GrGLFragmentProcessor* createGLInstance() const override {
joshualitteb2a6762014-12-04 11:35:33 -080079 return SkNEW_ARGS(GLBigKeyProcessor, (*this));
80 }
81
82private:
83 BigKeyProcessor() {
84 this->initClassID<BigKeyProcessor>();
85 }
mtklein36352bf2015-03-25 18:17:31 -070086 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
87 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080088
89 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
90
91 typedef GrFragmentProcessor INHERITED;
92};
93
94GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
95
joshualitt0067ff52015-07-08 14:26:19 -070096GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) {
joshualitteb2a6762014-12-04 11:35:33 -080097 return BigKeyProcessor::Create();
98}
99
joshualitt65171342014-10-09 07:25:36 -0700100/*
101 * Begin test code
102 */
103static const int kRenderTargetHeight = 1;
104static const int kRenderTargetWidth = 1;
105
bsalomon4061b122015-05-29 10:26:19 -0700106static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700107 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700108 // setup render target
109 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700110 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700111 texDesc.fWidth = kRenderTargetWidth;
112 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700113 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700114 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
115 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
116 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700117 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700118
bsalomon8718aaf2015-02-19 07:24:21 -0800119 GrUniqueKey key;
120 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700121 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800122 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700123 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800124 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700125
bsalomon4061b122015-05-29 10:26:19 -0700126 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700127 if (!texture) {
bsalomon4061b122015-05-29 10:26:19 -0700128 texture = textureProvider->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800129 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700130 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700131 }
joshualittd9097592014-10-07 08:37:36 -0700132 }
bsalomon37f9a262015-02-02 13:00:10 -0800133 return texture ? texture->asRenderTarget() : NULL;
bsalomon@google.com91207482013-02-12 21:45:24 +0000134}
135
joshualitt0067ff52015-07-08 14:26:19 -0700136static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) {
137 SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::CreateStage(d));
egdanielc2304142014-12-11 13:15:13 -0800138 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800139 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800140}
141
joshualitt0067ff52015-07-08 14:26:19 -0700142static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder,
143 GrProcessorTestData* d, int maxStages) {
144 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
145 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700146
joshualitt65171342014-10-09 07:25:36 -0700147 for (int s = 0; s < numProcs;) {
bsalomonae59b772014-11-19 08:23:49 -0800148 SkAutoTUnref<const GrFragmentProcessor> fp(
joshualitt0067ff52015-07-08 14:26:19 -0700149 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(d));
joshualitt65171342014-10-09 07:25:36 -0700150 SkASSERT(fp);
151
joshualitt65171342014-10-09 07:25:36 -0700152 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700153 if (s < numColorProcs) {
egdaniel8dd688b2015-01-22 10:16:09 -0800154 pipelineBuilder->addColorProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700155 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800156 pipelineBuilder->addCoverageProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700157 }
158 ++s;
159 }
160}
161
egdaniel8dd688b2015-01-22 10:16:09 -0800162static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700163 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700164 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700165 state |= random->nextBool() * i;
166 }
joshualitt6c891102015-05-13 08:51:49 -0700167
168 // If we don't have an MSAA rendertarget then we have to disable useHWAA
169 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
vbuzinovdded6962015-06-12 08:59:45 -0700170 !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) {
joshualitt6c891102015-05-13 08:51:49 -0700171 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
172 }
egdaniel8dd688b2015-01-22 10:16:09 -0800173 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700174}
175
joshualitt65171342014-10-09 07:25:36 -0700176// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800177static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700178 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
179 kReplace_StencilOp,
180 kReplace_StencilOp,
181 kAlways_StencilFunc,
182 0xffff,
183 0xffff,
184 0xffff);
185 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
186 kKeep_StencilOp,
187 kKeep_StencilOp,
188 kNever_StencilFunc,
189 0xffff,
190 0xffff,
191 0xffff);
192
193 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800194 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700195 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800196 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700197 }
198}
joshualitt249af152014-09-15 11:41:13 -0700199
bsalomon4061b122015-05-29 10:26:19 -0700200bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) {
joshualitt65171342014-10-09 07:25:36 -0700201 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700202 GrSurfaceDesc dummyDesc;
bsalomonbea01502015-07-16 10:00:28 -0700203 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000204 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000205 dummyDesc.fWidth = 34;
206 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700207 SkAutoTUnref<GrTexture> dummyTexture1(
208 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700209 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000210 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
211 dummyDesc.fWidth = 16;
212 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700213 SkAutoTUnref<GrTexture> dummyTexture2(
214 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000215
bsalomone904c092014-07-17 10:50:59 -0700216 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700217 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700218 return false;
219 }
220
joshualitt65171342014-10-09 07:25:36 -0700221 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
222
joshualitt54e0c122014-11-19 09:38:51 -0800223 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800224 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800225
joshualitt6c891102015-05-13 08:51:49 -0700226 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800227 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000228
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000229 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700230 static const int NUM_TESTS = 2048;
231 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700232 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700233 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
234 fResourceProvider, &random, this->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800235 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700236 SkDebugf("Could not allocate render target");
237 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000238 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000239
egdaniel8dd688b2015-01-22 10:16:09 -0800240 GrPipelineBuilder pipelineBuilder;
241 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800242 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000243
bsalomon4061b122015-05-29 10:26:19 -0700244 SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700245 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700246
joshualitt9cc17752015-07-09 06:28:14 -0700247 GrProcessorDataManager procDataManager;
248 GrProcessorTestData ptd(&random, context, &procDataManager, fGpu->caps(), dummyTextures);
joshualitt0067ff52015-07-08 14:26:19 -0700249 set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages);
250 set_random_xpf(&pipelineBuilder, &ptd);
egdaniel8dd688b2015-01-22 10:16:09 -0800251 set_random_state(&pipelineBuilder, &random);
252 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700253
joshualitt1c735482015-07-13 08:08:25 -0700254 this->drawBatch(pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000255 }
joshualitt6c891102015-05-13 08:51:49 -0700256
257 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
258 this->flush();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000259 return true;
260}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000261
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000262DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon3318ee72015-03-16 11:56:29 -0700263 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
264 // skbug 3330
265#ifdef SK_BUILD_FOR_WIN
266 GrAutoLocaleSetter als("sv-SE");
267#else
268 GrAutoLocaleSetter als("sv_SE.UTF-8");
269#endif
270
joshualitt6c891102015-05-13 08:51:49 -0700271 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700272 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700273 opts.fSuppressPrints = true;
274 GrContextFactory debugFactory(opts);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000275 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
joshualitt6c891102015-05-13 08:51:49 -0700276 GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700277 if (context) {
bsalomon861e1032014-12-16 07:33:49 -0800278 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700279
280 /*
281 * For the time being, we only support the test with desktop GL or for android on
282 * ARM platforms
283 * TODO When we run ES 3.00 GLSL in more places, test again
284 */
285 int maxStages;
286 if (kGL_GrGLStandard == gpu->glStandard() ||
287 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
288 maxStages = 6;
289 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
290 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
291 maxStages = 1;
292 } else {
293 return;
294 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000295#if SK_ANGLE
296 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
297 if (type == GrContextFactory::kANGLE_GLContextType) {
egdaniel305a8892015-02-17 11:18:38 -0800298 maxStages = 2;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000299 }
300#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800301 GrTestTarget target;
302 context->getTestTarget(&target);
bsalomon4061b122015-05-29 10:26:19 -0700303 REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000304 }
305 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000306}
307
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000308#endif