csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [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 | |
| 8 | #include "SkTypes.h" |
| 9 | #include "SkPoint.h" |
| 10 | #include "Test.h" |
| 11 | #include <vector> |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
Brian Salomon | 652ecb5 | 2017-01-17 12:39:53 -0500 | [diff] [blame] | 15 | #include "GrAppliedClip.h" |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 17 | #include "GrRenderTargetPriv.h" |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 18 | #include "GrTypesPriv.h" |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 19 | #include "gl/GrGLGpu.h" |
| 20 | #include "gl/debug/DebugGLTestContext.h" |
| 21 | |
| 22 | typedef std::vector<SkPoint> SamplePattern; |
| 23 | |
| 24 | static const SamplePattern kTestPatterns[] = { |
| 25 | SamplePattern{ // Intel on mac, msaa8, offscreen. |
| 26 | {0.562500, 0.312500}, |
| 27 | {0.437500, 0.687500}, |
| 28 | {0.812500, 0.562500}, |
| 29 | {0.312500, 0.187500}, |
| 30 | {0.187500, 0.812500}, |
| 31 | {0.062500, 0.437500}, |
| 32 | {0.687500, 0.937500}, |
| 33 | {0.937500, 0.062500} |
| 34 | }, |
| 35 | |
| 36 | SamplePattern{ // Intel on mac, msaa8, on-screen. |
| 37 | {0.562500, 0.687500}, |
| 38 | {0.437500, 0.312500}, |
| 39 | {0.812500, 0.437500}, |
| 40 | {0.312500, 0.812500}, |
| 41 | {0.187500, 0.187500}, |
| 42 | {0.062500, 0.562500}, |
| 43 | {0.687500, 0.062500}, |
| 44 | {0.937500, 0.937500} |
| 45 | }, |
| 46 | |
| 47 | SamplePattern{ // NVIDIA, msaa16. |
| 48 | {0.062500, 0.000000}, |
| 49 | {0.250000, 0.125000}, |
| 50 | {0.187500, 0.375000}, |
| 51 | {0.437500, 0.312500}, |
| 52 | {0.500000, 0.062500}, |
| 53 | {0.687500, 0.187500}, |
| 54 | {0.750000, 0.437500}, |
| 55 | {0.937500, 0.250000}, |
| 56 | {0.000000, 0.500000}, |
| 57 | {0.312500, 0.625000}, |
| 58 | {0.125000, 0.750000}, |
| 59 | {0.375000, 0.875000}, |
| 60 | {0.562500, 0.562500}, |
| 61 | {0.812500, 0.687500}, |
| 62 | {0.625000, 0.812500}, |
| 63 | {0.875000, 0.937500} |
| 64 | }, |
| 65 | |
| 66 | SamplePattern{ // NVIDIA, mixed samples, 16:1. |
| 67 | {0.250000, 0.125000}, |
| 68 | {0.625000, 0.812500}, |
| 69 | {0.500000, 0.062500}, |
| 70 | {0.812500, 0.687500}, |
| 71 | {0.187500, 0.375000}, |
| 72 | {0.875000, 0.937500}, |
| 73 | {0.125000, 0.750000}, |
| 74 | {0.750000, 0.437500}, |
| 75 | {0.937500, 0.250000}, |
| 76 | {0.312500, 0.625000}, |
| 77 | {0.437500, 0.312500}, |
| 78 | {0.000000, 0.500000}, |
| 79 | {0.375000, 0.875000}, |
| 80 | {0.687500, 0.187500}, |
| 81 | {0.062500, 0.000000}, |
| 82 | {0.562500, 0.562500} |
| 83 | } |
| 84 | }; |
| 85 | constexpr int numTestPatterns = SK_ARRAY_COUNT(kTestPatterns); |
| 86 | |
| 87 | class TestSampleLocationsInterface : public SkNoncopyable { |
| 88 | public: |
| 89 | virtual void overrideSamplePattern(const SamplePattern&) = 0; |
| 90 | virtual ~TestSampleLocationsInterface() {} |
| 91 | }; |
| 92 | |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 93 | void assert_equal(skiatest::Reporter* reporter, const SamplePattern& pattern, |
| 94 | const GrGpu::MultisampleSpecs& specs, bool flipY) { |
| 95 | GrAlwaysAssert(specs.fSampleLocations); |
| 96 | if ((int)pattern.size() != specs.fEffectiveSampleCnt) { |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 97 | REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong number of samples.")); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) { |
| 101 | SkPoint expectedLocation = specs.fSampleLocations[i]; |
| 102 | if (flipY) { |
| 103 | expectedLocation.fY = 1 - expectedLocation.fY; |
| 104 | } |
| 105 | if (pattern[i] != expectedLocation) { |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 106 | REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong sample location.")); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void test_sampleLocations(skiatest::Reporter* reporter, TestSampleLocationsInterface* testInterface, |
| 113 | GrContext* ctx) { |
| 114 | SkRandom rand; |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 115 | sk_sp<GrRenderTargetContext> bottomUps[numTestPatterns]; |
| 116 | sk_sp<GrRenderTargetContext> topDowns[numTestPatterns]; |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 117 | for (int i = 0; i < numTestPatterns; ++i) { |
| 118 | int numSamples = (int)kTestPatterns[i].size(); |
| 119 | GrAlwaysAssert(numSamples > 1 && SkIsPow2(numSamples)); |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 120 | bottomUps[i] = ctx->makeDeferredRenderTargetContext( |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 121 | SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 122 | rand.nextRangeU(1 + numSamples / 2, numSamples), GrMipMapped::kNo, |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 123 | kBottomLeft_GrSurfaceOrigin); |
Robert Phillips | 09dfc47 | 2017-09-13 15:25:47 -0400 | [diff] [blame] | 124 | topDowns[i] = ctx->makeDeferredRenderTargetContext( |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 125 | SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr, |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 126 | rand.nextRangeU(1 + numSamples / 2, numSamples), GrMipMapped::kNo, |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 127 | kTopLeft_GrSurfaceOrigin); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Ensure all sample locations get queried and/or cached properly. |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 131 | for (int repeat = 0; repeat < 2; ++repeat) { |
| 132 | for (int i = 0; i < numTestPatterns; ++i) { |
| 133 | testInterface->overrideSamplePattern(kTestPatterns[i]); |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 134 | for (GrRenderTargetContext* rtc : {bottomUps[i].get(), topDowns[i].get()}) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 135 | GrPipeline dummyPipeline(rtc->asRenderTargetProxy(), |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 136 | GrPipeline::ScissorState::kDisabled, |
| 137 | SkBlendMode::kSrcOver); |
| 138 | GrRenderTarget* rt = rtc->accessRenderTarget(); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 139 | assert_equal(reporter, kTestPatterns[i], |
Chris Dalton | 46983b7 | 2017-06-06 12:27:16 -0600 | [diff] [blame] | 140 | rt->renderTargetPriv().getMultisampleSpecs(dummyPipeline), |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 141 | kBottomLeft_GrSurfaceOrigin == rtc->asSurfaceProxy()->origin()); |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 142 | } |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
csmartdalton | c633abb | 2016-11-01 08:55:55 -0700 | [diff] [blame] | 145 | |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | class GLTestSampleLocationsInterface : public TestSampleLocationsInterface, public GrGLInterface { |
| 151 | public: |
| 152 | GLTestSampleLocationsInterface() : fTestContext(sk_gpu_test::CreateDebugGLTestContext()) { |
| 153 | fStandard = fTestContext->gl()->fStandard; |
| 154 | fExtensions = fTestContext->gl()->fExtensions; |
| 155 | fFunctions = fTestContext->gl()->fFunctions; |
| 156 | |
| 157 | fFunctions.fGetIntegerv = [&](GrGLenum pname, GrGLint* params) { |
| 158 | GrAlwaysAssert(GR_GL_EFFECTIVE_RASTER_SAMPLES != pname); |
| 159 | if (GR_GL_SAMPLES == pname) { |
| 160 | GrAlwaysAssert(!fSamplePattern.empty()); |
| 161 | *params = (int)fSamplePattern.size(); |
| 162 | } else { |
| 163 | fTestContext->gl()->fFunctions.fGetIntegerv(pname, params); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | fFunctions.fGetMultisamplefv = [&](GrGLenum pname, GrGLuint index, GrGLfloat* val) { |
| 168 | GrAlwaysAssert(GR_GL_SAMPLE_POSITION == pname); |
| 169 | val[0] = fSamplePattern[index].fX; |
| 170 | val[1] = fSamplePattern[index].fY; |
| 171 | }; |
| 172 | } |
| 173 | |
| 174 | operator GrBackendContext() { |
| 175 | return reinterpret_cast<GrBackendContext>(static_cast<GrGLInterface*>(this)); |
| 176 | } |
| 177 | |
| 178 | void overrideSamplePattern(const SamplePattern& newPattern) override { |
| 179 | fSamplePattern = newPattern; |
| 180 | } |
| 181 | |
| 182 | private: |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 183 | std::unique_ptr<sk_gpu_test::GLTestContext> fTestContext; |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 184 | SamplePattern fSamplePattern; |
| 185 | }; |
| 186 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 187 | DEF_GPUTEST(GLSampleLocations, reporter, /* options */) { |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 188 | GLTestSampleLocationsInterface testInterface; |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 189 | sk_sp<GrContext> ctx(GrContext::MakeGL(&testInterface)); |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 190 | |
| 191 | // This test relies on at least 2 samples. |
Greg Daniel | 81e7bf8 | 2017-07-19 14:47:42 -0400 | [diff] [blame] | 192 | int supportedSample = ctx->caps()->getSampleCount(2, kRGBA_8888_GrPixelConfig); |
| 193 | if (supportedSample < 2) { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 194 | return; |
| 195 | } |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 196 | test_sampleLocations(reporter, &testInterface, ctx.get()); |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | #endif |