blob: 7f1f4ec946acd4a62a796e842a1277d7bce22f24 [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"
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"
joshualitt2c93efe2014-11-06 12:57:13 -080026#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080027#include "effects/GrPorterDuffXferProcessor.h"
jvanverth39edf762014-12-22 11:44:19 -080028#include "gl/GrGLGpu.h"
joshualitt2c93efe2014-11-06 12:57:13 -080029#include "gl/GrGLPathRendering.h"
joshualitt2c93efe2014-11-06 12:57:13 -080030#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000031
joshualitt65171342014-10-09 07:25:36 -070032/*
bsalomon98b33eb2014-10-15 11:05:26 -070033 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070034 * whole thing correctly
35 */
36static const uint32_t kMaxKeySize = 1024;
37
joshualitt65171342014-10-09 07:25:36 -070038class GLBigKeyProcessor : public GrGLFragmentProcessor {
39public:
joshualitteb2a6762014-12-04 11:35:33 -080040 GLBigKeyProcessor(const GrProcessor&) {}
joshualittd9097592014-10-07 08:37:36 -070041
joshualitt15988992014-10-09 15:04:05 -070042 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt65171342014-10-09 07:25:36 -070043 const GrFragmentProcessor& fp,
joshualitt65171342014-10-09 07:25:36 -070044 const char* outputColor,
45 const char* inputColor,
46 const TransformedCoordsArray&,
joshualitt6c891102015-05-13 08:51:49 -070047 const TextureSamplerArray&) {
48 // pass through
49 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
50 fsBuilder->codeAppendf("%s = %s;\n", outputColor, inputColor);
51 }
joshualitt65171342014-10-09 07:25:36 -070052
jvanverthcfc18862015-04-28 08:48:20 -070053 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070054 for (uint32_t i = 0; i < kMaxKeySize; i++) {
55 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070056 }
joshualittd9097592014-10-07 08:37:36 -070057 }
58
joshualitt65171342014-10-09 07:25:36 -070059private:
60 typedef GrGLFragmentProcessor INHERITED;
61};
62
joshualitteb2a6762014-12-04 11:35:33 -080063class BigKeyProcessor : public GrFragmentProcessor {
64public:
65 static GrFragmentProcessor* Create() {
66 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
67 return SkRef(gBigKeyProcessor);
68 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080071
jvanverthcfc18862015-04-28 08:48:20 -070072 virtual void getGLProcessorKey(const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -070073 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -080074 GLBigKeyProcessor::GenKey(*this, caps, b);
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 GrGLFragmentProcessor* createGLInstance() const override {
joshualitteb2a6762014-12-04 11:35:33 -080078 return SkNEW_ARGS(GLBigKeyProcessor, (*this));
79 }
80
81private:
82 BigKeyProcessor() {
83 this->initClassID<BigKeyProcessor>();
84 }
mtklein36352bf2015-03-25 18:17:31 -070085 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
86 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080087
88 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
89
90 typedef GrFragmentProcessor INHERITED;
91};
92
93GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
94
95GrFragmentProcessor* BigKeyProcessor::TestCreate(SkRandom*,
96 GrContext*,
97 const GrDrawTargetCaps&,
98 GrTexture*[]) {
99 return BigKeyProcessor::Create();
100}
101
joshualitt65171342014-10-09 07:25:36 -0700102/*
103 * Begin test code
104 */
105static const int kRenderTargetHeight = 1;
106static const int kRenderTargetWidth = 1;
107
joshualitt15732062015-05-13 12:15:14 -0700108static GrRenderTarget* random_render_target(GrContext* context, SkRandom* random,
109 const GrDrawTargetCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700110 // setup render target
111 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700112 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700113 texDesc.fWidth = kRenderTargetWidth;
114 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700115 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700116 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
117 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
118 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700119 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700120
bsalomon8718aaf2015-02-19 07:24:21 -0800121 GrUniqueKey key;
122 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700123 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800124 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700125 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800126 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700127
bsalomond309e7a2015-04-30 14:18:54 -0700128 GrTexture* texture = context->textureProvider()->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700129 if (!texture) {
bsalomond309e7a2015-04-30 14:18:54 -0700130 texture = context->textureProvider()->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800131 if (texture) {
bsalomond309e7a2015-04-30 14:18:54 -0700132 context->textureProvider()->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700133 }
joshualittd9097592014-10-07 08:37:36 -0700134 }
bsalomon37f9a262015-02-02 13:00:10 -0800135 return texture ? texture->asRenderTarget() : NULL;
bsalomon@google.com91207482013-02-12 21:45:24 +0000136}
137
egdaniel8dd688b2015-01-22 10:16:09 -0800138static void set_random_xpf(GrContext* context, const GrDrawTargetCaps& caps,
139 GrPipelineBuilder* pipelineBuilder, SkRandom* random,
140 GrTexture* dummyTextures[]) {
egdanielc2304142014-12-11 13:15:13 -0800141 SkAutoTUnref<const GrXPFactory> xpf(
142 GrProcessorTestFactory<GrXPFactory>::CreateStage(random, context, caps, dummyTextures));
143 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800144 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800145}
146
bsalomon861e1032014-12-16 07:33:49 -0800147static void set_random_color_coverage_stages(GrGLGpu* gpu,
egdaniel8dd688b2015-01-22 10:16:09 -0800148 GrPipelineBuilder* pipelineBuilder,
joshualitt65171342014-10-09 07:25:36 -0700149 int maxStages,
joshualitt65171342014-10-09 07:25:36 -0700150 SkRandom* random,
151 GrTexture* dummyTextures[]) {
152 int numProcs = random->nextULessThan(maxStages + 1);
153 int numColorProcs = random->nextULessThan(numProcs + 1);
154
joshualitt65171342014-10-09 07:25:36 -0700155 for (int s = 0; s < numProcs;) {
bsalomonae59b772014-11-19 08:23:49 -0800156 SkAutoTUnref<const GrFragmentProcessor> fp(
joshualitt65171342014-10-09 07:25:36 -0700157 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(random,
158 gpu->getContext(),
159 *gpu->caps(),
160 dummyTextures));
161 SkASSERT(fp);
162
joshualitt65171342014-10-09 07:25:36 -0700163 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700164 if (s < numColorProcs) {
egdaniel8dd688b2015-01-22 10:16:09 -0800165 pipelineBuilder->addColorProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700166 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800167 pipelineBuilder->addCoverageProcessor(fp);
joshualitt65171342014-10-09 07:25:36 -0700168 }
169 ++s;
170 }
171}
172
egdaniel8dd688b2015-01-22 10:16:09 -0800173static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700174 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700175 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700176 state |= random->nextBool() * i;
177 }
joshualitt6c891102015-05-13 08:51:49 -0700178
179 // If we don't have an MSAA rendertarget then we have to disable useHWAA
180 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
181 !pipelineBuilder->getRenderTarget()->isMultisampled()) {
182 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
183 }
egdaniel8dd688b2015-01-22 10:16:09 -0800184 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700185}
186
joshualitt65171342014-10-09 07:25:36 -0700187// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800188static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700189 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
190 kReplace_StencilOp,
191 kReplace_StencilOp,
192 kAlways_StencilFunc,
193 0xffff,
194 0xffff,
195 0xffff);
196 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
197 kKeep_StencilOp,
198 kKeep_StencilOp,
199 kNever_StencilFunc,
200 0xffff,
201 0xffff,
202 0xffff);
203
204 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800205 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700206 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800207 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700208 }
209}
joshualitt249af152014-09-15 11:41:13 -0700210
joshualitt2c93efe2014-11-06 12:57:13 -0800211bool GrDrawTarget::programUnitTest(int maxStages) {
bsalomon861e1032014-12-16 07:33:49 -0800212 GrGLGpu* gpu = static_cast<GrGLGpu*>(fContext->getGpu());
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;
bsalomon5236cf42015-01-14 10:42:08 -0800219 SkAutoTUnref<GrTexture> dummyTexture1(gpu->createTexture(dummyDesc, false, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700220 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000221 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
222 dummyDesc.fWidth = 16;
223 dummyDesc.fHeight = 22;
bsalomon5236cf42015-01-14 10:42:08 -0800224 SkAutoTUnref<GrTexture> dummyTexture2(gpu->createTexture(dummyDesc, false, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000225
bsalomone904c092014-07-17 10:50:59 -0700226 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700227 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700228 return false;
229 }
230
joshualitt65171342014-10-09 07:25:36 -0700231 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
232
joshualitt54e0c122014-11-19 09:38:51 -0800233 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800234 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800235
joshualitt6c891102015-05-13 08:51:49 -0700236 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800237 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000238
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000239 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700240 static const int NUM_TESTS = 2048;
241 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700242 // setup random render target(can fail)
joshualitt15732062015-05-13 12:15:14 -0700243 SkAutoTUnref<GrRenderTarget> rt(random_render_target(fContext, &random, this->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800244 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700245 SkDebugf("Could not allocate render target");
246 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000247 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000248
egdaniel8dd688b2015-01-22 10:16:09 -0800249 GrPipelineBuilder pipelineBuilder;
250 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800251 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000252
joshualitt6c891102015-05-13 08:51:49 -0700253 SkAutoTUnref<GrBatch> batch(GrRandomBatch(&random, fContext));
254 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700255
joshualitt2c93efe2014-11-06 12:57:13 -0800256 set_random_color_coverage_stages(gpu,
egdaniel8dd688b2015-01-22 10:16:09 -0800257 &pipelineBuilder,
joshualitt6c891102015-05-13 08:51:49 -0700258 maxStages,
joshualitt2c93efe2014-11-06 12:57:13 -0800259 &random,
260 dummyTextures);
egdanielc2304142014-12-11 13:15:13 -0800261
262 // creates a random xfer processor factory on the draw state
egdaniel8dd688b2015-01-22 10:16:09 -0800263 set_random_xpf(fContext, gpu->glCaps(), &pipelineBuilder, &random, dummyTextures);
egdanielc2304142014-12-11 13:15:13 -0800264
egdaniel8dd688b2015-01-22 10:16:09 -0800265 set_random_state(&pipelineBuilder, &random);
266 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700267
joshualitt6c891102015-05-13 08:51:49 -0700268 this->drawBatch(&pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000269 }
joshualitt6c891102015-05-13 08:51:49 -0700270
271 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
272 this->flush();
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000273 return true;
274}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000275
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000276DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon3318ee72015-03-16 11:56:29 -0700277 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
278 // skbug 3330
279#ifdef SK_BUILD_FOR_WIN
280 GrAutoLocaleSetter als("sv-SE");
281#else
282 GrAutoLocaleSetter als("sv_SE.UTF-8");
283#endif
284
joshualitt6c891102015-05-13 08:51:49 -0700285 // We suppress prints to avoid spew
286 GrContext::Options opts;
287 opts.fSuppressPrints = true;
288 GrContextFactory debugFactory(opts);
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000289 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
joshualitt6c891102015-05-13 08:51:49 -0700290 GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700291 if (context) {
bsalomon861e1032014-12-16 07:33:49 -0800292 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700293
294 /*
295 * For the time being, we only support the test with desktop GL or for android on
296 * ARM platforms
297 * TODO When we run ES 3.00 GLSL in more places, test again
298 */
299 int maxStages;
300 if (kGL_GrGLStandard == gpu->glStandard() ||
301 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
302 maxStages = 6;
303 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
304 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
305 maxStages = 1;
306 } else {
307 return;
308 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000309#if SK_ANGLE
310 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
311 if (type == GrContextFactory::kANGLE_GLContextType) {
egdaniel305a8892015-02-17 11:18:38 -0800312 maxStages = 2;
bsalomon@google.com042a2862013-02-04 18:39:24 +0000313 }
314#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800315 GrTestTarget target;
316 context->getTestTarget(&target);
317 REPORTER_ASSERT(reporter, target.target()->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000318 }
319 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000320}
321
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000322#endif