Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 9 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrClip.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrGpuResource.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrMemoryPool.h" |
| 16 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrResourceProvider.h" |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 18 | #include "src/gpu/SkGr.h" |
Robert Phillips | 550de7f | 2021-07-06 16:28:52 -0400 | [diff] [blame] | 19 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 21 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 23 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "tests/TestUtils.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 25 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 26 | #include <atomic> |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 27 | #include <random> |
| 28 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 29 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 30 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 31 | public: |
| 32 | DEFINE_OP_CLASS_ID |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 33 | static GrOp::Owner Make(GrRecordingContext* rContext, |
| 34 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 35 | return GrOp::Make<TestOp>(rContext, std::move(fp)); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 38 | const char* name() const override { return "TestOp"; } |
| 39 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 40 | void visitProxies(const GrVisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 41 | fProcessors.visitProxies(func); |
| 42 | } |
| 43 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 44 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 45 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 46 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 47 | GrClampType clampType) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 48 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 49 | SkPMColor4f overrideColor; |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 50 | return fProcessors.finalize( |
| 51 | kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 52 | &GrUserStencilSettings::kUnused, caps, clampType, &overrideColor); |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 53 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 54 | |
| 55 | private: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 56 | friend class ::GrOp; // for ctor |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 57 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 58 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 59 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 60 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 61 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 62 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 63 | GrProgramInfo* programInfo() override { return nullptr; } |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 64 | void onCreateProgramInfo(const GrCaps*, |
| 65 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 66 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 67 | bool usesMSAASurface, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 68 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 69 | const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 70 | GrXferBarrierFlags renderPassXferBarriers, |
| 71 | GrLoadOp colorLoadOp) override {} |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 72 | void onPrePrepareDraws(GrRecordingContext*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 73 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 74 | GrAppliedClip*, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 75 | const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 76 | GrXferBarrierFlags renderPassXferBarriers, |
| 77 | GrLoadOp colorLoadOp) override {} |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 78 | void onPrepareDraws(GrMeshDrawTarget*) override { return; } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 79 | void onExecute(GrOpFlushState*, const SkRect&) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 80 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 81 | GrProcessorSet fProcessors; |
| 82 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 83 | using INHERITED = GrMeshDrawOp; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /** |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 87 | * FP used to test ref counts on owned GrGpuResources. Can also be a parent FP to test counts |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 88 | * of resources owned by child FPs. |
| 89 | */ |
| 90 | class TestFP : public GrFragmentProcessor { |
| 91 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 92 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 93 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 94 | } |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 95 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<GrSurfaceProxyView>& views) { |
| 96 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(views)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | const char* name() const override { return "test"; } |
| 100 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 101 | void onAddToKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 102 | static std::atomic<int32_t> nextKey{0}; |
| 103 | b->add32(nextKey++); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 106 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 107 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 110 | private: |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 111 | TestFP(const SkTArray<GrSurfaceProxyView>& views) |
| 112 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) { |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 113 | for (const GrSurfaceProxyView& view : views) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 114 | this->registerChild(GrTextureEffect::Make(view, kUnknown_SkAlphaType)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 115 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 118 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 119 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 120 | this->registerChild(std::move(child)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 121 | } |
| 122 | |
John Stiles | 307f8f5 | 2021-08-09 15:36:59 -0400 | [diff] [blame^] | 123 | explicit TestFP(const TestFP& that) : INHERITED(that) {} |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 124 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 125 | std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
| 126 | class TestGLSLFP : public ProgramImpl { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 127 | public: |
| 128 | TestGLSLFP() {} |
| 129 | void emitCode(EmitArgs& args) override { |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 130 | args.fFragBuilder->codeAppendf("return half4(1);"); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 135 | return std::make_unique<TestGLSLFP>(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 139 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 140 | using INHERITED = GrFragmentProcessor; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 141 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 142 | } // namespace |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 143 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 144 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 145 | auto dContext = ctxInfo.directContext(); |
| 146 | GrProxyProvider* proxyProvider = dContext->priv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 147 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 148 | static constexpr SkISize kDims = {10, 10}; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 149 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 150 | const GrBackendFormat format = |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 151 | dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 152 | GrRenderable::kNo); |
| 153 | GrSwizzle swizzle = dContext->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 154 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 155 | for (bool makeClone : {false, true}) { |
| 156 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 157 | auto sdc = skgpu::v1::SurfaceDrawContext::Make( |
| 158 | dContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1}, |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 159 | SkSurfaceProps()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 160 | { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 161 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 162 | format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 163 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 164 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 165 | { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 166 | SkTArray<GrSurfaceProxyView> views; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 167 | views.push_back({proxy, kTopLeft_GrSurfaceOrigin, swizzle}); |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 168 | auto fp = TestFP::Make(std::move(views)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 169 | for (int i = 0; i < parentCnt; ++i) { |
| 170 | fp = TestFP::Make(std::move(fp)); |
| 171 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 172 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 173 | if (makeClone) { |
| 174 | clone = fp->clone(); |
| 175 | } |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 176 | GrOp::Owner op = TestOp::Make(dContext, std::move(fp)); |
| 177 | sdc->addDrawOp(std::move(op)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 178 | if (clone) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 179 | op = TestOp::Make(dContext, std::move(clone)); |
| 180 | sdc->addDrawOp(std::move(op)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 183 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 184 | // If the fp is cloned the number of refs should increase by one (for the clone) |
| 185 | int expectedProxyRefs = makeClone ? 3 : 2; |
| 186 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 187 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 188 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 189 | dContext->flushAndSubmit(); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 190 | |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 191 | // just one from the 'proxy' sk_sp |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 192 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 197 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 198 | #include "tools/flags/CommandLineFlags.h" |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 199 | static DEFINE_bool(randomProcessorTest, false, |
| 200 | "Use non-deterministic seed for random processor tests?"); |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 201 | static DEFINE_int(processorSeed, 0, |
| 202 | "Use specific seed for processor tests. Overridden by --randomProcessorTest."); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 203 | |
| 204 | #if GR_TEST_UTILS |
| 205 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 206 | static GrColor input_texel_color(int i, int j, SkScalar delta) { |
| 207 | // Delta must be less than 0.5 to prevent over/underflow issues with the input color |
| 208 | SkASSERT(delta <= 0.5); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 209 | |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 210 | SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF), |
| 211 | (uint8_t)(j & 0xFF), |
| 212 | (uint8_t)((i + j) & 0xFF), |
| 213 | (uint8_t)((2 * j - i) & 0xFF)); |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 214 | SkColor4f color4f = SkColor4f::FromColor(color); |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 215 | // We only apply delta to the r,g, and b channels. This is because we're using this |
| 216 | // to test the canTweakAlphaForCoverage() optimization. A processor is allowed |
| 217 | // to use the input color's alpha in its calculation and report this optimization. |
| 218 | for (int i = 0; i < 3; i++) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 219 | if (color4f[i] > 0.5) { |
| 220 | color4f[i] -= delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 221 | } else { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 222 | color4f[i] += delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 223 | } |
| 224 | } |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 225 | return color4f.premul().toBytes_RGBA(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 226 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 227 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 228 | // The output buffer must be the same size as the render-target context. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 229 | void render_fp(GrDirectContext* dContext, |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 230 | skgpu::v1::SurfaceDrawContext* sdc, |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 231 | std::unique_ptr<GrFragmentProcessor> fp, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 232 | GrColor* outBuffer) { |
Robert Phillips | ba70138 | 2021-07-07 14:15:12 -0400 | [diff] [blame] | 233 | sdc->fillWithFP(std::move(fp)); |
| 234 | std::fill_n(outBuffer, sdc->width() * sdc->height(), 0); |
| 235 | auto ii = SkImageInfo::Make(sdc->dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 236 | GrPixmap resultPM(ii, outBuffer, sdc->width()*sizeof(uint32_t)); |
| 237 | sdc->readPixels(dContext, resultPM, {0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 238 | } |
| 239 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 240 | // This class is responsible for reproducibly generating a random fragment processor. |
| 241 | // An identical randomly-designed FP can be generated as many times as needed. |
| 242 | class TestFPGenerator { |
| 243 | public: |
| 244 | TestFPGenerator() = delete; |
| 245 | TestFPGenerator(GrDirectContext* context, GrResourceProvider* resourceProvider) |
| 246 | : fContext(context) |
| 247 | , fResourceProvider(resourceProvider) |
| 248 | , fInitialSeed(synthesizeInitialSeed()) |
| 249 | , fRandomSeed(fInitialSeed) {} |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 250 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 251 | uint32_t initialSeed() { return fInitialSeed; } |
| 252 | |
| 253 | bool init() { |
| 254 | // Initializes the two test texture proxies that are available to the FP test factories. |
| 255 | SkRandom random{fRandomSeed}; |
| 256 | static constexpr int kTestTextureSize = 256; |
| 257 | |
| 258 | { |
| 259 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 260 | GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize]; |
| 261 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 262 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 263 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 264 | random.nextULessThan(256), random.nextULessThan(256), 0.0f); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 269 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 270 | SkBitmap bitmap; |
| 271 | bitmap.installPixels( |
| 272 | ii, rgbaData, ii.minRowBytes(), |
| 273 | [](void* addr, void* context) { delete[](GrColor*) addr; }, nullptr); |
| 274 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 275 | auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap)); |
| 276 | if (!view || !view.proxy()->instantiate(fResourceProvider)) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 277 | SkDebugf("Unable to instantiate RGBA8888 test texture."); |
| 278 | return false; |
| 279 | } |
| 280 | fTestViews[0] = GrProcessorTestData::ViewInfo{view, GrColorType::kRGBA_8888, |
| 281 | kPremul_SkAlphaType}; |
| 282 | } |
| 283 | |
| 284 | { |
| 285 | // Put random values into the alpha texture that the test FPs can optionally use. |
| 286 | uint8_t* alphaData = new uint8_t[kTestTextureSize * kTestTextureSize]; |
| 287 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 288 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 289 | alphaData[kTestTextureSize * y + x] = random.nextULessThan(256); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 294 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 295 | SkBitmap bitmap; |
| 296 | bitmap.installPixels( |
| 297 | ii, alphaData, ii.minRowBytes(), |
| 298 | [](void* addr, void* context) { delete[](uint8_t*) addr; }, nullptr); |
| 299 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 300 | auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap)); |
| 301 | if (!view || !view.proxy()->instantiate(fResourceProvider)) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 302 | SkDebugf("Unable to instantiate A8 test texture."); |
| 303 | return false; |
| 304 | } |
| 305 | fTestViews[1] = GrProcessorTestData::ViewInfo{view, GrColorType::kAlpha_8, |
| 306 | kPremul_SkAlphaType}; |
| 307 | } |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | void reroll() { |
| 313 | // Feed our current random seed into SkRandom to generate a new seed. |
| 314 | SkRandom random{fRandomSeed}; |
| 315 | fRandomSeed = random.nextU(); |
| 316 | } |
| 317 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 318 | std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 319 | std::unique_ptr<GrFragmentProcessor> inputFP) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 320 | // This will generate the exact same randomized FP (of each requested type) each time |
| 321 | // it's called. Call `reroll` to get a different FP. |
| 322 | SkRandom random{fRandomSeed}; |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 323 | GrProcessorTestData testData{&random, fContext, randomTreeDepth, |
| 324 | SK_ARRAY_COUNT(fTestViews), fTestViews, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 325 | std::move(inputFP)}; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 326 | return GrFragmentProcessorTestFactory::MakeIdx(type, &testData); |
| 327 | } |
| 328 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 329 | std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth, |
| 330 | GrSurfaceProxyView view, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 331 | SkAlphaType alpha = kPremul_SkAlphaType) { |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 332 | return make(type, randomTreeDepth, GrTextureEffect::Make(view, alpha)); |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 333 | } |
| 334 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 335 | private: |
| 336 | static uint32_t synthesizeInitialSeed() { |
| 337 | if (FLAGS_randomProcessorTest) { |
| 338 | std::random_device rd; |
| 339 | return rd(); |
| 340 | } else { |
| 341 | return FLAGS_processorSeed; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 342 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 343 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 344 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 345 | GrDirectContext* fContext; // owned by caller |
| 346 | GrResourceProvider* fResourceProvider; // owned by caller |
| 347 | const uint32_t fInitialSeed; |
| 348 | uint32_t fRandomSeed; |
| 349 | GrProcessorTestData::ViewInfo fTestViews[2]; |
| 350 | }; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 351 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 352 | // Creates an array of color values from input_texel_color(), to be used as an input texture. |
| 353 | std::vector<GrColor> make_input_pixels(int width, int height, SkScalar delta) { |
| 354 | std::vector<GrColor> pixel(width * height); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 355 | for (int y = 0; y < width; ++y) { |
| 356 | for (int x = 0; x < height; ++x) { |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 357 | pixel[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 358 | } |
| 359 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 360 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 361 | return pixel; |
| 362 | } |
| 363 | |
| 364 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 365 | // the fragment processor under test. An array of W*H colors are passed in as the texture data. |
| 366 | GrSurfaceProxyView make_input_texture(GrRecordingContext* context, |
| 367 | int width, int height, GrColor* pixel) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 368 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 369 | SkBitmap bitmap; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 370 | bitmap.installPixels(ii, pixel, ii.minRowBytes()); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 371 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 372 | return std::get<0>(GrMakeUncachedBitmapProxyView(context, bitmap)); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 373 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 374 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 375 | // We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 376 | // actually contains unpremul data. Also, even though we made the result data by rendering into |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 377 | // a "unpremul" SurfaceDrawContext, our input texture is unpremul and outside of the random |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 378 | // effect configuration, we didn't do anything to ensure the output is actually premul. We just |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 379 | // don't currently allow kUnpremul GrSurfaceDrawContexts. |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 380 | static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType; |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 381 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 382 | bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 383 | SkImageInfo info = |
| 384 | SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 385 | SkBitmap bmp; |
| 386 | bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 387 | return BipmapToBase64DataURI(bmp, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 390 | bool log_texture_view(GrDirectContext* dContext, GrSurfaceProxyView src, SkString* dst) { |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 391 | SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType, |
| 392 | kLogAlphaType); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 393 | |
Robert Phillips | 33bf2b5 | 2021-08-02 11:14:38 -0400 | [diff] [blame] | 394 | auto sContext = dContext->priv().makeSC(std::move(src), ii.colorInfo()); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 395 | SkBitmap bm; |
| 396 | SkAssertResult(bm.tryAllocPixels(ii)); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 397 | SkAssertResult(sContext->readPixels(dContext, bm.pixmap(), {0, 0})); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 398 | return BipmapToBase64DataURI(bm, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 399 | } |
| 400 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 401 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 402 | // With the loss of precision of rendering into 32-bit color, then estimating the FP's output |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 403 | // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01 |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 404 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 405 | // too unforgiving). |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 406 | static constexpr SkScalar kTolerance = 0.01f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 407 | for (int i = 0; i < 4; i++) { |
| 408 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | return true; |
| 413 | } |
| 414 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 415 | // Given three input colors (color preceding the FP being tested) provided to the FP at the same |
| 416 | // local coord and the three corresponding FP outputs, this ensures that either: |
| 417 | // out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a |
| 418 | // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't |
| 419 | // change), OR: |
| 420 | // out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2] |
| 421 | // (per-channel modulation instead of modulation by just the alpha channel) |
| 422 | // It does this by estimating the pre-modulated fp color from one of the input/output pairs and |
| 423 | // confirms the conditions hold for the other two pairs. |
| 424 | // It is required that the three input colors have the same alpha as fp is allowed to be a function |
| 425 | // of the input alpha (but not r, g, or b). |
| 426 | bool legal_modulation(const GrColor in[3], const GrColor out[3]) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 427 | // Convert to floating point, which is the number space the FP operates in (more or less) |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 428 | SkPMColor4f inf[3], outf[3]; |
| 429 | for (int i = 0; i < 3; ++i) { |
| 430 | inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]); |
| 431 | outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]); |
| 432 | } |
| 433 | // This test is only valid if all the input alphas are the same. |
| 434 | SkASSERT(inf[0].fA == inf[1].fA && inf[1].fA == inf[2].fA); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 435 | |
| 436 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 437 | // When the original input is very small, it may cause the final output color to round |
| 438 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 439 | // will then have a guaranteed larger channel value (since the offset will be added to it). |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 440 | SkPMColor4f fpPreColorModulation = {0,0,0,0}; |
| 441 | SkPMColor4f fpPreAlphaModulation = {0,0,0,0}; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 442 | for (int i = 0; i < 4; i++) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 443 | // Use the most stepped up frame |
| 444 | int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1; |
| 445 | maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2; |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 446 | const SkPMColor4f& in = inf[maxInIdx]; |
| 447 | const SkPMColor4f& out = outf[maxInIdx]; |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 448 | if (in[i] > 0) { |
| 449 | fpPreColorModulation[i] = out[i] / in[i]; |
| 450 | } |
| 451 | if (in[3] > 0) { |
| 452 | fpPreAlphaModulation[i] = out[i] / in[3]; |
| 453 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 457 | // of the transformed input colors. |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 458 | SkPMColor4f expectedForAlphaModulation[3]; |
| 459 | SkPMColor4f expectedForColorModulation[3]; |
| 460 | for (int i = 0; i < 3; ++i) { |
| 461 | expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA; |
| 462 | expectedForColorModulation[i] = fpPreColorModulation * inf[i]; |
| 463 | // If the input alpha is 0 then the other channels should also be zero |
| 464 | // since the color is assumed to be premul. Modulating zeros by anything |
| 465 | // should produce zeros. |
| 466 | if (inf[i].fA == 0) { |
| 467 | SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0); |
| 468 | expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0}; |
| 469 | } |
| 470 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 471 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 472 | bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) && |
| 473 | fuzzy_color_equals(outf[1], expectedForColorModulation[1]) && |
| 474 | fuzzy_color_equals(outf[2], expectedForColorModulation[2]); |
| 475 | |
| 476 | bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) && |
| 477 | fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) && |
| 478 | fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]); |
| 479 | |
| 480 | // This can be enabled to print the values that caused this check to fail. |
| 481 | if (0 && !isLegalColorModulation && !isLegalAlphaModulation) { |
| 482 | SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 483 | fpPreColorModulation[0], |
| 484 | fpPreColorModulation[1], |
| 485 | fpPreColorModulation[2], |
| 486 | fpPreColorModulation[3]); |
| 487 | for (int i = 0; i < 3; ++i) { |
| 488 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 489 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 490 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 491 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 492 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 493 | expectedForColorModulation[i].fR, expectedForColorModulation[i].fG, |
| 494 | expectedForColorModulation[i].fB, expectedForColorModulation[i].fA, |
| 495 | fuzzy_color_equals(outf[i], expectedForColorModulation[i])); |
| 496 | } |
| 497 | SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 498 | fpPreAlphaModulation[0], |
| 499 | fpPreAlphaModulation[1], |
| 500 | fpPreAlphaModulation[2], |
| 501 | fpPreAlphaModulation[3]); |
| 502 | for (int i = 0; i < 3; ++i) { |
| 503 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 504 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 505 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 506 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 507 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 508 | expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG, |
| 509 | expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA, |
| 510 | fuzzy_color_equals(outf[i], expectedForAlphaModulation[i])); |
| 511 | } |
| 512 | } |
| 513 | return isLegalColorModulation || isLegalAlphaModulation; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 516 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 517 | GrDirectContext* context = ctxInfo.directContext(); |
| 518 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 519 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 520 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 521 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 522 | if (!fpGenerator.init()) { |
| 523 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 524 | return; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 525 | } |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 526 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 527 | // Make the destination context for the test. |
| 528 | static constexpr int kRenderSize = 256; |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 529 | auto sdc = skgpu::v1::SurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 530 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 531 | {kRenderSize, kRenderSize}, SkSurfaceProps()); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 532 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 533 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 534 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 535 | // difference between the frame outputs if the FP is properly following the modulation |
| 536 | // requirements of the coverage optimization. |
| 537 | static constexpr SkScalar kInputDelta = 0.2f; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 538 | std::vector<GrColor> inputPixels1 = make_input_pixels(kRenderSize, kRenderSize, 0.0f); |
| 539 | std::vector<GrColor> inputPixels2 = |
| 540 | make_input_pixels(kRenderSize, kRenderSize, 1 * kInputDelta); |
| 541 | std::vector<GrColor> inputPixels3 = |
| 542 | make_input_pixels(kRenderSize, kRenderSize, 2 * kInputDelta); |
| 543 | GrSurfaceProxyView inputTexture1 = |
| 544 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels1.data()); |
| 545 | GrSurfaceProxyView inputTexture2 = |
| 546 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels2.data()); |
| 547 | GrSurfaceProxyView inputTexture3 = |
| 548 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels3.data()); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 549 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 550 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 551 | // first failure (subsequent failures have a reasonable chance of being related). |
| 552 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 553 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 554 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 555 | // Storage for the three frames required for coverage compatibility optimization testing. |
| 556 | // Each frame uses the correspondingly numbered inputTextureX. |
| 557 | std::vector<GrColor> readData1(kRenderSize * kRenderSize); |
| 558 | std::vector<GrColor> readData2(kRenderSize * kRenderSize); |
| 559 | std::vector<GrColor> readData3(kRenderSize * kRenderSize); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 560 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 561 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 562 | for (int i = 0; i < FPFactory::Count(); ++i) { |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 563 | int optimizedForOpaqueInput = 0; |
| 564 | int optimizedForCoverageAsAlpha = 0; |
| 565 | int optimizedForConstantOutputForInput = 0; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 566 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 567 | #ifdef __MSVC_RUNTIME_CHECKS |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 568 | // This test is infuriatingly slow with MSVC runtime checks enabled |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 569 | static constexpr int kMinimumTrials = 1; |
| 570 | static constexpr int kMaximumTrials = 1; |
| 571 | static constexpr int kExpectedSuccesses = 1; |
| 572 | #else |
| 573 | // We start by testing each fragment-processor 100 times, watching the optimization bits |
| 574 | // that appear. If we see an optimization bit appear in those first 100 trials, we keep |
| 575 | // running tests until we see at least five successful trials that have this optimization |
| 576 | // bit enabled. If we never see a particular optimization bit after 100 trials, we assume |
| 577 | // that this FP doesn't support that optimization at all. |
| 578 | static constexpr int kMinimumTrials = 100; |
| 579 | static constexpr int kMaximumTrials = 2000; |
| 580 | static constexpr int kExpectedSuccesses = 5; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 581 | #endif |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 582 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 583 | for (int trial = 0;; ++trial) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 584 | // Create a randomly-configured FP. |
| 585 | fpGenerator.reroll(); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 586 | std::unique_ptr<GrFragmentProcessor> fp = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 587 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 588 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 589 | // If we have iterated enough times and seen a sufficient number of successes on each |
| 590 | // optimization bit that can be returned, stop running trials. |
| 591 | if (trial >= kMinimumTrials) { |
| 592 | bool moreTrialsNeeded = (optimizedForOpaqueInput > 0 && |
| 593 | optimizedForOpaqueInput < kExpectedSuccesses) || |
| 594 | (optimizedForCoverageAsAlpha > 0 && |
| 595 | optimizedForCoverageAsAlpha < kExpectedSuccesses) || |
| 596 | (optimizedForConstantOutputForInput > 0 && |
| 597 | optimizedForConstantOutputForInput < kExpectedSuccesses); |
| 598 | if (!moreTrialsNeeded) break; |
| 599 | |
| 600 | if (trial >= kMaximumTrials) { |
| 601 | SkDebugf("Abandoning ProcessorOptimizationValidationTest after %d trials. " |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 602 | "Seed: 0x%08x, processor:\n%s", |
| 603 | kMaximumTrials, fpGenerator.initialSeed(), fp->dumpTreeInfo().c_str()); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | // Skip further testing if this trial has no optimization bits enabled. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 609 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 610 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 611 | continue; |
| 612 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 613 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 614 | // We can make identical copies of the test FP in order to test coverage-as-alpha. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 615 | if (fp->compatibleWithCoverageAsAlpha()) { |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 616 | // Create and render two identical versions of this FP, but using different input |
| 617 | // textures, to check coverage optimization. We don't need to do this step for |
| 618 | // constant-output or preserving-opacity tests. |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 619 | render_fp(context, sdc.get(), |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 620 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture2), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 621 | readData2.data()); |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 622 | render_fp(context, sdc.get(), |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 623 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture3), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 624 | readData3.data()); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 625 | ++optimizedForCoverageAsAlpha; |
| 626 | } |
| 627 | |
| 628 | if (fp->hasConstantOutputForConstantInput()) { |
| 629 | ++optimizedForConstantOutputForInput; |
| 630 | } |
| 631 | |
| 632 | if (fp->preservesOpaqueInput()) { |
| 633 | ++optimizedForOpaqueInput; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 634 | } |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 635 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 636 | // Draw base frame last so that rtc holds the original FP behavior if we need to dump |
| 637 | // the image to the log. |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 638 | render_fp(context, sdc.get(), fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 639 | readData1.data()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 640 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 641 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 642 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 643 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 644 | // of device errors and represents a separate problem. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 645 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 646 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 647 | #else |
| 648 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 649 | #endif |
| 650 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 651 | // Collect first optimization failure message, to be output later as a warning or an |
| 652 | // error depending on whether the rendering "passed" or failed. |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 653 | int failedPixelCount = 0; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 654 | SkString coverageMessage; |
| 655 | SkString opaqueMessage; |
| 656 | SkString constMessage; |
| 657 | for (int y = 0; y < kRenderSize; ++y) { |
| 658 | for (int x = 0; x < kRenderSize; ++x) { |
| 659 | bool passing = true; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 660 | GrColor input = inputPixels1[y * kRenderSize + x]; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 661 | GrColor output = readData1[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 662 | |
| 663 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 664 | GrColor ins[3]; |
| 665 | ins[0] = input; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 666 | ins[1] = inputPixels2[y * kRenderSize + x]; |
| 667 | ins[2] = inputPixels3[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 668 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 669 | GrColor outs[3]; |
| 670 | outs[0] = output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 671 | outs[1] = readData2[y * kRenderSize + x]; |
| 672 | outs[2] = readData3[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 673 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 674 | if (!legal_modulation(ins, outs)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 675 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 676 | if (coverageMessage.isEmpty()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 677 | coverageMessage.printf( |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 678 | "\"Modulating\" processor did not match alpha-modulation " |
| 679 | "nor color-modulation rules.\n" |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 680 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 681 | input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 682 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 683 | } |
| 684 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 685 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 686 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 687 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 688 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 689 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 690 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 691 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 692 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 693 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 694 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 695 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 696 | if (constMessage.isEmpty()) { |
| 697 | passing = false; |
| 698 | |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 699 | constMessage.printf( |
| 700 | "Processor claimed output for const input doesn't match " |
| 701 | "actual output.\n" |
| 702 | "Error: %f, Tolerance: %f, input: (%f, %f, %f, %f), " |
| 703 | "actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f).", |
| 704 | std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))), |
| 705 | kTol, input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 706 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 707 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 708 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 709 | } |
| 710 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 711 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 712 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 713 | |
| 714 | if (opaqueMessage.isEmpty()) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 715 | opaqueMessage.printf( |
| 716 | "Processor claimed opaqueness is preserved but " |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 717 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 718 | input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 719 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 720 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 721 | |
| 722 | if (!passing) { |
| 723 | // Regardless of how many optimizations the pixel violates, count it as a |
| 724 | // single bad pixel. |
| 725 | failedPixelCount++; |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 731 | // threshold for an FP violating the optimization requirements. |
| 732 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 733 | ERRORF(reporter, |
| 734 | "Processor violated %d of %d pixels, seed: 0x%08x.\n" |
| 735 | "Processor:\n%s\nFirst failing pixel details are below:", |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 736 | failedPixelCount, kRenderSize * kRenderSize, fpGenerator.initialSeed(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 737 | fp->dumpTreeInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 738 | |
| 739 | // Print first failing pixel's details. |
| 740 | if (!coverageMessage.isEmpty()) { |
| 741 | ERRORF(reporter, coverageMessage.c_str()); |
| 742 | } |
| 743 | if (!constMessage.isEmpty()) { |
| 744 | ERRORF(reporter, constMessage.c_str()); |
| 745 | } |
| 746 | if (!opaqueMessage.isEmpty()) { |
| 747 | ERRORF(reporter, opaqueMessage.c_str()); |
| 748 | } |
| 749 | |
| 750 | if (!loggedFirstFailure) { |
| 751 | // Print with ERRORF to make sure the encoded image is output |
| 752 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 753 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 754 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 755 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 756 | ERRORF(reporter, "Input image: %s\n\n" |
| 757 | "===========================================================\n\n" |
| 758 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 759 | loggedFirstFailure = true; |
| 760 | } |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 761 | } else if (failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 762 | // Don't trigger an error, but don't just hide the failures either. |
| 763 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 764 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 765 | fpGenerator.initialSeed(), fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 766 | if (!coverageMessage.isEmpty()) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 767 | INFOF(reporter, "%s", coverageMessage.c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 768 | } |
| 769 | if (!constMessage.isEmpty()) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 770 | INFOF(reporter, "%s", constMessage.c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 771 | } |
| 772 | if (!opaqueMessage.isEmpty()) { |
John Stiles | 7bf7999 | 2021-06-25 11:05:20 -0400 | [diff] [blame] | 773 | INFOF(reporter, "%s", opaqueMessage.c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 774 | } |
| 775 | if (!loggedFirstWarning) { |
| 776 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 777 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 778 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 779 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 780 | INFOF(reporter, "Input image: %s\n\n" |
| 781 | "===========================================================\n\n" |
| 782 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 783 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 789 | |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 790 | static void assert_processor_equality(skiatest::Reporter* reporter, |
| 791 | const GrFragmentProcessor& fp, |
| 792 | const GrFragmentProcessor& clone) { |
| 793 | REPORTER_ASSERT(reporter, !strcmp(fp.name(), clone.name()), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 794 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 795 | REPORTER_ASSERT(reporter, fp.compatibleWithCoverageAsAlpha() == |
| 796 | clone.compatibleWithCoverageAsAlpha(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 797 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 798 | REPORTER_ASSERT(reporter, fp.isEqual(clone), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 799 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 800 | REPORTER_ASSERT(reporter, fp.preservesOpaqueInput() == clone.preservesOpaqueInput(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 801 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 802 | REPORTER_ASSERT(reporter, fp.hasConstantOutputForConstantInput() == |
| 803 | clone.hasConstantOutputForConstantInput(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 804 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 805 | REPORTER_ASSERT(reporter, fp.numChildProcessors() == clone.numChildProcessors(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 806 | "\n%s", fp.dumpTreeInfo().c_str()); |
Brian Salomon | 66b500a | 2021-08-02 12:37:14 -0400 | [diff] [blame] | 807 | REPORTER_ASSERT(reporter, fp.sampleUsage() == clone.sampleUsage(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 808 | "\n%s", fp.dumpTreeInfo().c_str()); |
Brian Salomon | 66b500a | 2021-08-02 12:37:14 -0400 | [diff] [blame] | 809 | REPORTER_ASSERT(reporter, fp.usesSampleCoords() == clone.usesSampleCoords(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 810 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static bool verify_identical_render(skiatest::Reporter* reporter, int renderSize, |
| 814 | const char* processorType, |
| 815 | const GrColor readData1[], const GrColor readData2[]) { |
| 816 | // The ProcessorClone test has a history of being flaky on a number of devices. If an FP clone |
| 817 | // is logically wrong, it's reasonable to expect it produce a large number of pixel differences |
| 818 | // in the image. Sporadic pixel violations are more indicative device errors and represents a |
| 819 | // separate problem. |
| 820 | #if defined(SK_BUILD_FOR_SKQP) |
| 821 | const int maxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 822 | #else |
| 823 | const int maxAcceptableFailedPixels = 2 * renderSize; // ~0.002% of the pixels (size 1024*1024) |
| 824 | #endif |
| 825 | |
| 826 | int failedPixelCount = 0; |
| 827 | int firstWrongX = 0; |
| 828 | int firstWrongY = 0; |
| 829 | int idx = 0; |
| 830 | for (int y = 0; y < renderSize; ++y) { |
| 831 | for (int x = 0; x < renderSize; ++x, ++idx) { |
| 832 | if (readData1[idx] != readData2[idx]) { |
| 833 | if (!failedPixelCount) { |
| 834 | firstWrongX = x; |
| 835 | firstWrongY = y; |
| 836 | } |
| 837 | ++failedPixelCount; |
| 838 | } |
| 839 | if (failedPixelCount > maxAcceptableFailedPixels) { |
| 840 | idx = firstWrongY * renderSize + firstWrongX; |
| 841 | ERRORF(reporter, |
| 842 | "%s produced different output at (%d, %d). " |
| 843 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 844 | "Clone Output Color: 0x%08x.", |
| 845 | processorType, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f), |
| 846 | readData1[idx], readData2[idx]); |
| 847 | |
| 848 | return false; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | return true; |
| 854 | } |
| 855 | |
| 856 | static void log_clone_failure(skiatest::Reporter* reporter, int renderSize, |
| 857 | GrDirectContext* context, const GrSurfaceProxyView& inputTexture, |
| 858 | GrColor pixelsFP[], GrColor pixelsClone[], GrColor pixelsRegen[]) { |
| 859 | // Write the images out as data URLs for inspection. |
| 860 | SkString inputURL, origURL, cloneURL, regenURL; |
| 861 | if (log_texture_view(context, inputTexture, &inputURL) && |
| 862 | log_pixels(pixelsFP, renderSize, &origURL) && |
| 863 | log_pixels(pixelsClone, renderSize, &cloneURL) && |
| 864 | log_pixels(pixelsRegen, renderSize, ®enURL)) { |
| 865 | ERRORF(reporter, |
| 866 | "\nInput image:\n%s\n\n" |
| 867 | "===========================================================" |
| 868 | "\n\n" |
| 869 | "Orig output image:\n%s\n" |
| 870 | "===========================================================" |
| 871 | "\n\n" |
| 872 | "Clone output image:\n%s\n" |
| 873 | "===========================================================" |
| 874 | "\n\n" |
| 875 | "Regen output image:\n%s\n", |
| 876 | inputURL.c_str(), origURL.c_str(), cloneURL.c_str(), regenURL.c_str()); |
| 877 | } |
| 878 | } |
| 879 | |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 880 | // Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its |
| 881 | // progenitor. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 882 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 883 | GrDirectContext* context = ctxInfo.directContext(); |
| 884 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 885 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 886 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 887 | if (!fpGenerator.init()) { |
| 888 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 889 | return; |
| 890 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 891 | |
| 892 | // Make the destination context for the test. |
| 893 | static constexpr int kRenderSize = 1024; |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 894 | auto sdc = skgpu::v1::SurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 895 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 896 | {kRenderSize, kRenderSize}, SkSurfaceProps()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 897 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 898 | std::vector<GrColor> inputPixels = make_input_pixels(kRenderSize, kRenderSize, 0.0f); |
| 899 | GrSurfaceProxyView inputTexture = |
| 900 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels.data()); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 901 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 902 | // On failure we write out images, but just write the first failing set as the print is very |
| 903 | // large. |
| 904 | bool loggedFirstFailure = false; |
| 905 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 906 | // Storage for the original frame's readback and the readback of its clone. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 907 | std::vector<GrColor> readDataFP(kRenderSize * kRenderSize); |
| 908 | std::vector<GrColor> readDataClone(kRenderSize * kRenderSize); |
| 909 | std::vector<GrColor> readDataRegen(kRenderSize * kRenderSize); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 910 | |
| 911 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 912 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 913 | static constexpr int kTimesToInvokeFactory = 10; |
| 914 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 915 | fpGenerator.reroll(); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 916 | std::unique_ptr<GrFragmentProcessor> fp = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 917 | fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 918 | std::unique_ptr<GrFragmentProcessor> regen = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 919 | fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr); |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 920 | std::unique_ptr<GrFragmentProcessor> clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 921 | if (!clone) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 922 | ERRORF(reporter, "Clone of processor %s failed.", fp->dumpTreeInfo().c_str()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 923 | continue; |
| 924 | } |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 925 | assert_processor_equality(reporter, *fp, *clone); |
| 926 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 927 | // Draw with original and read back the results. |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 928 | render_fp(context, sdc.get(), std::move(fp), readDataFP.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 929 | |
| 930 | // Draw with clone and read back the results. |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 931 | render_fp(context, sdc.get(), std::move(clone), readDataClone.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 932 | |
| 933 | // Check that the results are the same. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 934 | if (!verify_identical_render(reporter, kRenderSize, "Processor clone", |
| 935 | readDataFP.data(), readDataClone.data())) { |
| 936 | // Dump a description from the regenerated processor (since the original FP has |
| 937 | // already been consumed). |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 938 | ERRORF(reporter, "FP hierarchy:\n%s", regen->dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 939 | |
| 940 | // Render and readback output from the regenerated FP. If this also mismatches, the |
| 941 | // FP itself doesn't generate consistent output. This could happen if: |
| 942 | // - the FP's TestCreate() does not always generate the same FP from a given seed |
| 943 | // - the FP's Make() does not always generate the same FP when given the same inputs |
| 944 | // - the FP itself generates inconsistent pixels (shader UB?) |
| 945 | // - the driver has a bug |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 946 | render_fp(context, sdc.get(), std::move(regen), readDataRegen.data()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 947 | |
| 948 | if (!verify_identical_render(reporter, kRenderSize, "Regenerated processor", |
| 949 | readDataFP.data(), readDataRegen.data())) { |
| 950 | ERRORF(reporter, "Output from regen did not match original!\n"); |
| 951 | } else { |
| 952 | ERRORF(reporter, "Regenerated processor output matches original results.\n"); |
| 953 | } |
| 954 | |
| 955 | // If this is the first time we've encountered a cloning failure, log the generated |
| 956 | // images to the reporter as data URLs. |
| 957 | if (!loggedFirstFailure) { |
| 958 | log_clone_failure(reporter, kRenderSize, context, inputTexture, |
| 959 | readDataFP.data(), readDataClone.data(), |
| 960 | readDataRegen.data()); |
| 961 | loggedFirstFailure = true; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 968 | #endif // GR_TEST_UTILS |