blob: d329124a07e3a1e7ba9d44d65aa40b05ba624c45 [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
96GrFragmentProcessor* BigKeyProcessor::TestCreate(SkRandom*,
97 GrContext*,
bsalomon4b91f762015-05-19 09:29:46 -070098 const GrCaps&,
joshualitteb2a6762014-12-04 11:35:33 -080099 GrTexture*[]) {
100 return BigKeyProcessor::Create();
101}
102
joshualitt65171342014-10-09 07:25:36 -0700103/*
104 * Begin test code
105 */
106static const int kRenderTargetHeight = 1;
107static const int kRenderTargetWidth = 1;
108
bsalomon4061b122015-05-29 10:26:19 -0700109static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700110 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700111 // setup render target
112 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700113 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700114 texDesc.fWidth = kRenderTargetWidth;
115 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700116 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700117 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
118 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
119 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700120 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700121
bsalomon8718aaf2015-02-19 07:24:21 -0800122 GrUniqueKey key;
123 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700124 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800125 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700126 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800127 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700128
bsalomon4061b122015-05-29 10:26:19 -0700129 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700130 if (!texture) {
bsalomon4061b122015-05-29 10:26:19 -0700131 texture = textureProvider->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800132 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700133 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700134 }
joshualittd9097592014-10-07 08:37:36 -0700135 }
bsalomon37f9a262015-02-02 13:00:10 -0800136 return texture ? texture->asRenderTarget() : NULL;
bsalomon@google.com91207482013-02-12 21:45:24 +0000137}
138
bsalomon4b91f762015-05-19 09:29:46 -0700139static void set_random_xpf(GrContext* context, const GrCaps& caps,
egdaniel8dd688b2015-01-22 10:16:09 -0800140 GrPipelineBuilder* pipelineBuilder, SkRandom* random,
141 GrTexture* dummyTextures[]) {
egdanielc2304142014-12-11 13:15:13 -0800142 SkAutoTUnref<const GrXPFactory> xpf(
143 GrProcessorTestFactory<GrXPFactory>::CreateStage(random, context, caps, dummyTextures));
144 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800145 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800146}
147
bsalomon4061b122015-05-29 10:26:19 -0700148static void set_random_color_coverage_stages(GrGpu* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -0800149 GrPipelineBuilder* pipelineBuilder,
joshualitt65171342014-10-09 07:25:36 -0700150 int maxStages,
joshualitt65171342014-10-09 07:25:36 -0700151 SkRandom* random,
152 GrTexture* dummyTextures[]) {
153 int numProcs = random->nextULessThan(maxStages + 1);
154 int numColorProcs = random->nextULessThan(numProcs + 1);
155
joshualitt65171342014-10-09 07:25:36 -0700156 for (int s = 0; s < numProcs;) {
bsalomonae59b772014-11-19 08:23:49 -0800157 SkAutoTUnref<const GrFragmentProcessor> fp(
joshualitt65171342014-10-09 07:25:36 -0700158 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(random,
159 gpu->getContext(),
160 *gpu->caps(),
161 dummyTextures));
162 SkASSERT(fp);
163
joshualitt65171342014-10-09 07:25:36 -0700164 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700165 if (s < numColorProcs) {
egdaniel8dd688b2015-01-22 10:16:09 -0800166 pipelineBuilder->addColorProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700167 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800168 pipelineBuilder->addCoverageProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700169 }
170 ++s;
171 }
172}
173
egdaniel8dd688b2015-01-22 10:16:09 -0800174static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700175 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700176 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700177 state |= random->nextBool() * i;
178 }
joshualitt6c891102015-05-13 08:51:49 -0700179
180 // If we don't have an MSAA rendertarget then we have to disable useHWAA
181 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
182 !pipelineBuilder->getRenderTarget()->isMultisampled()) {
183 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
184 }
egdaniel8dd688b2015-01-22 10:16:09 -0800185 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700186}
187
joshualitt65171342014-10-09 07:25:36 -0700188// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800189static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700190 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
191 kReplace_StencilOp,
192 kReplace_StencilOp,
193 kAlways_StencilFunc,
194 0xffff,
195 0xffff,
196 0xffff);
197 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
198 kKeep_StencilOp,
199 kKeep_StencilOp,
200 kNever_StencilFunc,
201 0xffff,
202 0xffff,
203 0xffff);
204
205 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800206 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700207 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800208 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700209 }
210}
joshualitt249af152014-09-15 11:41:13 -0700211
bsalomon4061b122015-05-29 10:26:19 -0700212bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) {
joshualitt65171342014-10-09 07:25:36 -0700213 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700214 GrSurfaceDesc dummyDesc;
215 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000216 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000217 dummyDesc.fWidth = 34;
218 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700219 SkAutoTUnref<GrTexture> dummyTexture1(
220 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700221 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000222 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
223 dummyDesc.fWidth = 16;
224 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700225 SkAutoTUnref<GrTexture> dummyTexture2(
226 fResourceProvider->createTexture(dummyDesc, false, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000227
bsalomone904c092014-07-17 10:50:59 -0700228 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700229 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700230 return false;
231 }
232
joshualitt65171342014-10-09 07:25:36 -0700233 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
234
joshualitt54e0c122014-11-19 09:38:51 -0800235 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800236 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800237
joshualitt6c891102015-05-13 08:51:49 -0700238 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800239 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000240
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000241 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700242 static const int NUM_TESTS = 2048;
243 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700244 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700245 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
246 fResourceProvider, &random, this->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800247 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700248 SkDebugf("Could not allocate render target");
249 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000250 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000251
egdaniel8dd688b2015-01-22 10:16:09 -0800252 GrPipelineBuilder pipelineBuilder;
253 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800254 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000255
bsalomon4061b122015-05-29 10:26:19 -0700256 SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700257 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700258
bsalomon4061b122015-05-29 10:26:19 -0700259 set_random_color_coverage_stages(fGpu,
egdaniel8dd688b2015-01-22 10:16:09 -0800260 &pipelineBuilder,
joshualitt6c891102015-05-13 08:51:49 -0700261 maxStages,
joshualitt2c93efe2014-11-06 12:57:13 -0800262 &random,
263 dummyTextures);
egdanielc2304142014-12-11 13:15:13 -0800264
265 // creates a random xfer processor factory on the draw state
bsalomon4061b122015-05-29 10:26:19 -0700266 set_random_xpf(context, *fGpu->caps(), &pipelineBuilder, &random, dummyTextures);
egdanielc2304142014-12-11 13:15:13 -0800267
egdaniel8dd688b2015-01-22 10:16:09 -0800268 set_random_state(&pipelineBuilder, &random);
269 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700270
joshualitt6c891102015-05-13 08:51:49 -0700271 this->drawBatch(&pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000272 }
joshualitt6c891102015-05-13 08:51:49 -0700273
274 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
275 this->flush();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000276 return true;
277}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000278
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000279DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon3318ee72015-03-16 11:56:29 -0700280 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
281 // skbug 3330
282#ifdef SK_BUILD_FOR_WIN
283 GrAutoLocaleSetter als("sv-SE");
284#else
285 GrAutoLocaleSetter als("sv_SE.UTF-8");
286#endif
287
joshualitt6c891102015-05-13 08:51:49 -0700288 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700289 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700290 opts.fSuppressPrints = true;
291 GrContextFactory debugFactory(opts);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000292 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
joshualitt6c891102015-05-13 08:51:49 -0700293 GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700294 if (context) {
bsalomon861e1032014-12-16 07:33:49 -0800295 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700296
297 /*
298 * For the time being, we only support the test with desktop GL or for android on
299 * ARM platforms
300 * TODO When we run ES 3.00 GLSL in more places, test again
301 */
302 int maxStages;
303 if (kGL_GrGLStandard == gpu->glStandard() ||
304 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
305 maxStages = 6;
306 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
307 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
308 maxStages = 1;
309 } else {
310 return;
311 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000312#if SK_ANGLE
313 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
314 if (type == GrContextFactory::kANGLE_GLContextType) {
egdaniel305a8892015-02-17 11:18:38 -0800315 maxStages = 2;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000316 }
317#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800318 GrTestTarget target;
319 context->getTestTarget(&target);
bsalomon4061b122015-05-29 10:26:19 -0700320 REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000321 }
322 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000323}
324
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000325#endif