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