bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 1 | |
| 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.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 9 | // This is a GPU-backend specific test. It relies on static intializers to work |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 11 | #include "SkTypes.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 14 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 15 | #include "GrAutoLocaleSetter.h" |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 16 | #include "GrBatchTest.h" |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 17 | #include "GrContextFactory.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 18 | #include "GrInvariantOutput.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 19 | #include "GrPipeline.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 20 | #include "GrResourceProvider.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 21 | #include "GrTest.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 22 | #include "GrXferProcessor.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 23 | #include "SkChecksum.h" |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 24 | #include "SkRandom.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 25 | #include "Test.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 26 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 27 | #include "batches/GrDrawBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 28 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 29 | #include "effects/GrConfigConversionEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 30 | #include "effects/GrPorterDuffXferProcessor.h" |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 31 | #include "effects/GrXfermodeFragmentProcessor.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 32 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 33 | #include "gl/GrGLGpu.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 34 | #include "gl/GrGLPathRendering.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 35 | #include "gl/builders/GrGLProgramBuilder.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 36 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 37 | /* |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 38 | * A dummy processor which just tries to insert a massive key and verify that it can retrieve the |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 39 | * whole thing correctly |
| 40 | */ |
| 41 | static const uint32_t kMaxKeySize = 1024; |
| 42 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 43 | class GLBigKeyProcessor : public GrGLFragmentProcessor { |
| 44 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 45 | GLBigKeyProcessor(const GrProcessor&) {} |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 46 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 47 | virtual void emitCode(EmitArgs& args) override { |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 48 | // pass through |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 49 | GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 50 | if (args.fInputColor) { |
| 51 | fsBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor); |
| 52 | } else { |
| 53 | fsBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor); |
| 54 | } |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 55 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 56 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 57 | static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 58 | for (uint32_t i = 0; i < kMaxKeySize; i++) { |
| 59 | b->add32(i); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 60 | } |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 63 | private: |
| 64 | typedef GrGLFragmentProcessor INHERITED; |
| 65 | }; |
| 66 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 67 | class BigKeyProcessor : public GrFragmentProcessor { |
| 68 | public: |
| 69 | static GrFragmentProcessor* Create() { |
mdempsky | 38f1f6f | 2015-08-27 12:57:01 -0700 | [diff] [blame] | 70 | static BigKeyProcessor gBigKeyProcessor; |
| 71 | return SkRef(&gBigKeyProcessor); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 72 | } |
| 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | const char* name() const override { return "Big Ole Key"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 75 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 76 | GrGLFragmentProcessor* onCreateGLInstance() const override { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 77 | return new GLBigKeyProcessor(*this); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
| 81 | BigKeyProcessor() { |
| 82 | this->initClassID<BigKeyProcessor>(); |
| 83 | } |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 84 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 85 | GrProcessorKeyBuilder* b) const override { |
| 86 | GLBigKeyProcessor::GenKey(*this, caps, b); |
| 87 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 89 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 90 | |
| 91 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 92 | |
| 93 | typedef GrFragmentProcessor INHERITED; |
| 94 | }; |
| 95 | |
| 96 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor); |
| 97 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 98 | const GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 99 | return BigKeyProcessor::Create(); |
| 100 | } |
| 101 | |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | class BlockInputFragmentProcessor : public GrFragmentProcessor { |
| 105 | public: |
| 106 | static GrFragmentProcessor* Create(const GrFragmentProcessor* fp) { |
| 107 | return new BlockInputFragmentProcessor(fp); |
| 108 | } |
| 109 | |
| 110 | const char* name() const override { return "Block Input"; } |
| 111 | |
| 112 | GrGLFragmentProcessor* onCreateGLInstance() const override { return new GLFP; } |
| 113 | |
| 114 | private: |
| 115 | class GLFP : public GrGLFragmentProcessor { |
| 116 | public: |
| 117 | void emitCode(EmitArgs& args) override { |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 118 | this->emitChild(0, nullptr, args); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private: |
| 122 | typedef GrGLFragmentProcessor INHERITED; |
| 123 | }; |
| 124 | |
| 125 | BlockInputFragmentProcessor(const GrFragmentProcessor* child) { |
| 126 | this->initClassID<BlockInputFragmentProcessor>(); |
| 127 | this->registerChildProcessor(child); |
| 128 | } |
| 129 | |
| 130 | void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {} |
| 131 | |
| 132 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 133 | |
| 134 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 135 | inout->setToOther(kRGBA_GrColorComponentFlags, GrColor_WHITE, |
| 136 | GrInvariantOutput::kWillNot_ReadInput); |
| 137 | this->childProcessor(0).computeInvariantOutput(inout); |
| 138 | } |
| 139 | |
| 140 | typedef GrFragmentProcessor INHERITED; |
| 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Begin test code |
| 147 | */ |
| 148 | static const int kRenderTargetHeight = 1; |
| 149 | static const int kRenderTargetWidth = 1; |
| 150 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 151 | static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 152 | const GrCaps* caps) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 153 | // setup render target |
| 154 | GrTextureParams params; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 155 | GrSurfaceDesc texDesc; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 156 | texDesc.fWidth = kRenderTargetWidth; |
| 157 | texDesc.fHeight = kRenderTargetHeight; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 158 | texDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 159 | texDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 160 | texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin : |
| 161 | kBottomLeft_GrSurfaceOrigin; |
joshualitt | 1573206 | 2015-05-13 12:15:14 -0700 | [diff] [blame] | 162 | texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 163 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 164 | GrUniqueKey key; |
| 165 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 166 | GrUniqueKey::Builder builder(&key, kDomain, 2); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 167 | builder[0] = texDesc.fOrigin; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 168 | builder[1] = texDesc.fSampleCnt; |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 169 | builder.finish(); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 170 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 171 | GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key); |
bsalomon | d27726e | 2014-10-12 05:40:00 -0700 | [diff] [blame] | 172 | if (!texture) { |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 173 | texture = textureProvider->createTexture(texDesc, true); |
bsalomon | 37f9a26 | 2015-02-02 13:00:10 -0800 | [diff] [blame] | 174 | if (texture) { |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 175 | textureProvider->assignUniqueKeyToTexture(key, texture); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 176 | } |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 177 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | return texture ? texture->asRenderTarget() : nullptr; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 181 | static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) { |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 182 | SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::Create(d)); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 183 | SkASSERT(xpf); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 184 | pipelineBuilder->setXPFactory(xpf.get()); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 185 | } |
| 186 | |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 187 | static const GrFragmentProcessor* create_random_proc_tree(GrProcessorTestData* d, |
| 188 | int minLevels, int maxLevels) { |
| 189 | SkASSERT(1 <= minLevels); |
| 190 | SkASSERT(minLevels <= maxLevels); |
| 191 | |
| 192 | // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate. |
| 193 | // If returning a leaf node, make sure that it doesn't have children (e.g. another |
| 194 | // GrComposeEffect) |
| 195 | const float terminateProbability = 0.3f; |
| 196 | if (1 == minLevels) { |
| 197 | bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability); |
| 198 | if (terminate) { |
| 199 | const GrFragmentProcessor* fp; |
| 200 | while (true) { |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 201 | fp = GrProcessorTestFactory<GrFragmentProcessor>::Create(d); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 202 | SkASSERT(fp); |
| 203 | if (0 == fp->numChildProcessors()) { |
| 204 | break; |
| 205 | } |
| 206 | fp->unref(); |
| 207 | } |
| 208 | return fp; |
| 209 | } |
| 210 | } |
| 211 | // If we didn't terminate, choose either the left or right subtree to fulfill |
| 212 | // the minLevels requirement of this tree; the other child can have as few levels as it wants. |
| 213 | // Also choose a random xfer mode that's supported by CreateFrom2Procs(). |
| 214 | if (minLevels > 1) { |
| 215 | --minLevels; |
| 216 | } |
| 217 | SkAutoTUnref<const GrFragmentProcessor> minLevelsChild(create_random_proc_tree(d, minLevels, |
| 218 | maxLevels - 1)); |
| 219 | SkAutoTUnref<const GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, |
| 220 | maxLevels - 1)); |
| 221 | SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(d->fRandom->nextRangeU(0, |
| 222 | SkXfermode::kLastCoeffMode)); |
| 223 | const GrFragmentProcessor* fp; |
| 224 | if (d->fRandom->nextF() < 0.5f) { |
| 225 | fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(minLevelsChild, otherChild, mode); |
| 226 | SkASSERT(fp); |
| 227 | } else { |
| 228 | fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(otherChild, minLevelsChild, mode); |
| 229 | SkASSERT(fp); |
| 230 | } |
| 231 | return fp; |
| 232 | } |
| 233 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 234 | static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder, |
| 235 | GrProcessorTestData* d, int maxStages) { |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 236 | // Randomly choose to either create a linear pipeline of procs or create one proc tree |
| 237 | const float procTreeProbability = 0.5f; |
| 238 | if (d->fRandom->nextF() < procTreeProbability) { |
| 239 | // A full tree with 5 levels (31 nodes) may exceed the max allowed length of the gl |
| 240 | // processor key; maxTreeLevels should be a number from 1 to 4 inclusive. |
| 241 | const int maxTreeLevels = 4; |
bsalomon | ae59b77 | 2014-11-19 08:23:49 -0800 | [diff] [blame] | 242 | SkAutoTUnref<const GrFragmentProcessor> fp( |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 243 | create_random_proc_tree(d, 2, maxTreeLevels)); |
| 244 | pipelineBuilder->addColorFragmentProcessor(fp); |
| 245 | } else { |
| 246 | int numProcs = d->fRandom->nextULessThan(maxStages + 1); |
| 247 | int numColorProcs = d->fRandom->nextULessThan(numProcs + 1); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 248 | |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 249 | for (int s = 0; s < numProcs;) { |
| 250 | SkAutoTUnref<const GrFragmentProcessor> fp( |
bsalomon | ae4738f | 2015-09-15 15:33:27 -0700 | [diff] [blame] | 251 | GrProcessorTestFactory<GrFragmentProcessor>::Create(d)); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 252 | SkASSERT(fp); |
| 253 | |
| 254 | // finally add the stage to the correct pipeline in the drawstate |
| 255 | if (s < numColorProcs) { |
| 256 | pipelineBuilder->addColorFragmentProcessor(fp); |
| 257 | } else { |
| 258 | pipelineBuilder->addCoverageFragmentProcessor(fp); |
| 259 | } |
| 260 | ++s; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 261 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 265 | static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 266 | int state = 0; |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 267 | for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 268 | state |= random->nextBool() * i; |
| 269 | } |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 270 | |
| 271 | // If we don't have an MSAA rendertarget then we have to disable useHWAA |
| 272 | if ((state | GrPipelineBuilder::kHWAntialias_Flag) && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 273 | !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) { |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 274 | state &= ~GrPipelineBuilder::kHWAntialias_Flag; |
| 275 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 276 | pipelineBuilder->enableState(state); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 279 | // right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()' |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 280 | static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 281 | GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil, |
| 282 | kReplace_StencilOp, |
| 283 | kReplace_StencilOp, |
| 284 | kAlways_StencilFunc, |
| 285 | 0xffff, |
| 286 | 0xffff, |
| 287 | 0xffff); |
| 288 | GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil, |
| 289 | kKeep_StencilOp, |
| 290 | kKeep_StencilOp, |
| 291 | kNever_StencilFunc, |
| 292 | 0xffff, |
| 293 | 0xffff, |
| 294 | 0xffff); |
| 295 | |
| 296 | if (random->nextBool()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 297 | pipelineBuilder->setStencil(kDoesWriteStencil); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 298 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 299 | pipelineBuilder->setStencil(kDoesNotWriteStencil); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 302 | |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 303 | bool GrDrawTarget::programUnitTest(GrContext* context, int maxStages) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 304 | // setup dummy textures |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 305 | GrSurfaceDesc dummyDesc; |
bsalomon | bea0150 | 2015-07-16 10:00:28 -0700 | [diff] [blame] | 306 | dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 307 | dummyDesc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 308 | dummyDesc.fWidth = 34; |
| 309 | dummyDesc.fHeight = 18; |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 310 | SkAutoTUnref<GrTexture> dummyTexture1( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 311 | context->textureProvider()->createTexture(dummyDesc, false, nullptr, 0)); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 312 | dummyDesc.fFlags = kNone_GrSurfaceFlags; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 313 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 314 | dummyDesc.fWidth = 16; |
| 315 | dummyDesc.fHeight = 22; |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 316 | SkAutoTUnref<GrTexture> dummyTexture2( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 317 | context->textureProvider()->createTexture(dummyDesc, false, nullptr, 0)); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 318 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 319 | if (!dummyTexture1 || ! dummyTexture2) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 320 | SkDebugf("Could not allocate dummy textures"); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 324 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
| 325 | |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 326 | // dummy scissor state |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 327 | GrScissorState scissor; |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 328 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 329 | // wide open clip |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 330 | GrClip clip; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 331 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 332 | SkRandom random; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 333 | static const int NUM_TESTS = 2048; |
| 334 | for (int t = 0; t < NUM_TESTS; t++) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 335 | // setup random render target(can fail) |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 336 | SkAutoTUnref<GrRenderTarget> rt(random_render_target( |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 337 | context->textureProvider(), &random, this->caps())); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 338 | if (!rt.get()) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 339 | SkDebugf("Could not allocate render target"); |
| 340 | return false; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 341 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 342 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 343 | GrPipelineBuilder pipelineBuilder; |
| 344 | pipelineBuilder.setRenderTarget(rt.get()); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 345 | pipelineBuilder.setClip(clip); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 346 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 347 | SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context)); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 348 | SkASSERT(batch); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 349 | |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 350 | GrProcessorDataManager procDataManager; |
| 351 | GrProcessorTestData ptd(&random, context, &procDataManager, fGpu->caps(), dummyTextures); |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 352 | set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages); |
| 353 | set_random_xpf(&pipelineBuilder, &ptd); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 354 | set_random_state(&pipelineBuilder, &random); |
| 355 | set_random_stencil(&pipelineBuilder, &random); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 356 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 357 | this->drawBatch(pipelineBuilder, batch); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 358 | } |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 359 | // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes) |
| 360 | this->flush(); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 361 | |
| 362 | // Validate that GrFPs work correctly without an input. |
| 363 | GrSurfaceDesc rtDesc; |
| 364 | rtDesc.fWidth = kRenderTargetWidth; |
| 365 | rtDesc.fHeight = kRenderTargetHeight; |
| 366 | rtDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 367 | rtDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 368 | SkAutoTUnref<GrRenderTarget> rt( |
| 369 | fContext->textureProvider()->createTexture(rtDesc, false)->asRenderTarget()); |
| 370 | int fpFactoryCnt = GrProcessorTestFactory<GrFragmentProcessor>::Count(); |
| 371 | for (int i = 0; i < fpFactoryCnt; ++i) { |
| 372 | // Since FP factories internally randomize, call each 10 times. |
| 373 | for (int j = 0; j < 10; ++j) { |
| 374 | SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context)); |
| 375 | SkASSERT(batch); |
| 376 | GrProcessorDataManager procDataManager; |
| 377 | GrProcessorTestData ptd(&random, context, &procDataManager, this->caps(), |
| 378 | dummyTextures); |
| 379 | GrPipelineBuilder builder; |
| 380 | builder.setXPFactory(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref(); |
| 381 | builder.setRenderTarget(rt); |
| 382 | builder.setClip(clip); |
| 383 | |
| 384 | SkAutoTUnref<const GrFragmentProcessor> fp( |
| 385 | GrProcessorTestFactory<GrFragmentProcessor>::CreateIdx(i, &ptd)); |
| 386 | SkAutoTUnref<const GrFragmentProcessor> blockFP( |
| 387 | BlockInputFragmentProcessor::Create(fp)); |
| 388 | builder.addColorFragmentProcessor(blockFP); |
| 389 | |
| 390 | this->drawBatch(builder, batch); |
| 391 | this->flush(); |
| 392 | } |
| 393 | } |
| 394 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 397 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 398 | DEF_GPUTEST(GLPrograms, reporter, factory) { |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 399 | // Set a locale that would cause shader compilation to fail because of , as decimal separator. |
| 400 | // skbug 3330 |
| 401 | #ifdef SK_BUILD_FOR_WIN |
| 402 | GrAutoLocaleSetter als("sv-SE"); |
| 403 | #else |
| 404 | GrAutoLocaleSetter als("sv_SE.UTF-8"); |
| 405 | #endif |
| 406 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 407 | // We suppress prints to avoid spew |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 408 | GrContextOptions opts; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 409 | opts.fSuppressPrints = true; |
| 410 | GrContextFactory debugFactory(opts); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 411 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 412 | GrContext* context = debugFactory.get(static_cast<GrContextFactory::GLContextType>(type)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 413 | if (context) { |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 414 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * For the time being, we only support the test with desktop GL or for android on |
| 418 | * ARM platforms |
| 419 | * TODO When we run ES 3.00 GLSL in more places, test again |
| 420 | */ |
| 421 | int maxStages; |
| 422 | if (kGL_GrGLStandard == gpu->glStandard() || |
| 423 | kARM_GrGLVendor == gpu->ctxInfo().vendor()) { |
| 424 | maxStages = 6; |
| 425 | } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() || |
| 426 | kOther_GrGLRenderer == gpu->ctxInfo().renderer()) { |
| 427 | maxStages = 1; |
| 428 | } else { |
| 429 | return; |
| 430 | } |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 431 | #if SK_ANGLE |
| 432 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE. |
| 433 | if (type == GrContextFactory::kANGLE_GLContextType) { |
egdaniel | 305a889 | 2015-02-17 11:18:38 -0800 | [diff] [blame] | 434 | maxStages = 2; |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 435 | } |
| 436 | #endif |
hendrikw | 885bf09 | 2015-08-27 10:38:39 -0700 | [diff] [blame] | 437 | #if SK_COMMAND_BUFFER |
| 438 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE. |
| 439 | // TODO(hendrikw): This only needs to happen with the ANGLE comand buffer backend. |
| 440 | if (type == GrContextFactory::kCommandBuffer_GLContextType) { |
| 441 | maxStages = 2; |
| 442 | } |
| 443 | #endif |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 444 | GrTestTarget target; |
| 445 | context->getTestTarget(&target); |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 446 | REPORTER_ASSERT(reporter, target.target()->programUnitTest(context, maxStages)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 449 | } |
| 450 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 451 | #endif |