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