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