blob: e78006344c44314ee117b3b967759f2d4e3663ba [file] [log] [blame]
csmartdalton0d28e572016-07-06 09:59:43 -07001/*
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 Salomon652ecb52017-01-17 12:39:53 -050015#include "GrAppliedClip.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070016#include "GrRenderTargetContext.h"
csmartdalton0d28e572016-07-06 09:59:43 -070017#include "GrRenderTargetPriv.h"
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050018#include "GrTypesPriv.h"
csmartdalton0d28e572016-07-06 09:59:43 -070019#include "gl/GrGLGpu.h"
20#include "gl/debug/DebugGLTestContext.h"
21
22typedef std::vector<SkPoint> SamplePattern;
23
24static 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};
85constexpr int numTestPatterns = SK_ARRAY_COUNT(kTestPatterns);
86
87class TestSampleLocationsInterface : public SkNoncopyable {
88public:
89 virtual void overrideSamplePattern(const SamplePattern&) = 0;
90 virtual ~TestSampleLocationsInterface() {}
91};
92
csmartdalton0d28e572016-07-06 09:59:43 -070093void 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) {
csmartdaltonc633abb2016-11-01 08:55:55 -070097 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong number of samples."));
csmartdalton0d28e572016-07-06 09:59:43 -070098 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) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700106 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong sample location."));
csmartdalton0d28e572016-07-06 09:59:43 -0700107 return;
108 }
109 }
110}
111
112void test_sampleLocations(skiatest::Reporter* reporter, TestSampleLocationsInterface* testInterface,
113 GrContext* ctx) {
114 SkRandom rand;
csmartdaltonc633abb2016-11-01 08:55:55 -0700115 sk_sp<GrRenderTargetContext> bottomUps[numTestPatterns];
116 sk_sp<GrRenderTargetContext> topDowns[numTestPatterns];
csmartdalton0d28e572016-07-06 09:59:43 -0700117 for (int i = 0; i < numTestPatterns; ++i) {
118 int numSamples = (int)kTestPatterns[i].size();
119 GrAlwaysAssert(numSamples > 1 && SkIsPow2(numSamples));
Robert Phillips09dfc472017-09-13 15:25:47 -0400120 bottomUps[i] = ctx->makeDeferredRenderTargetContext(
csmartdaltonc633abb2016-11-01 08:55:55 -0700121 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
Greg Daniel45d63032017-10-30 13:41:26 -0400122 rand.nextRangeU(1 + numSamples / 2, numSamples), GrMipMapped::kNo,
csmartdaltonc633abb2016-11-01 08:55:55 -0700123 kBottomLeft_GrSurfaceOrigin);
Robert Phillips09dfc472017-09-13 15:25:47 -0400124 topDowns[i] = ctx->makeDeferredRenderTargetContext(
csmartdaltonc633abb2016-11-01 08:55:55 -0700125 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
Greg Daniel45d63032017-10-30 13:41:26 -0400126 rand.nextRangeU(1 + numSamples / 2, numSamples), GrMipMapped::kNo,
csmartdaltonc633abb2016-11-01 08:55:55 -0700127 kTopLeft_GrSurfaceOrigin);
csmartdalton0d28e572016-07-06 09:59:43 -0700128 }
129
130 // Ensure all sample locations get queried and/or cached properly.
csmartdalton0d28e572016-07-06 09:59:43 -0700131 for (int repeat = 0; repeat < 2; ++repeat) {
132 for (int i = 0; i < numTestPatterns; ++i) {
133 testInterface->overrideSamplePattern(kTestPatterns[i]);
Chris Dalton46983b72017-06-06 12:27:16 -0600134 for (GrRenderTargetContext* rtc : {bottomUps[i].get(), topDowns[i].get()}) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400135 GrPipeline dummyPipeline(rtc->asRenderTargetProxy(),
Chris Dalton46983b72017-06-06 12:27:16 -0600136 GrPipeline::ScissorState::kDisabled,
137 SkBlendMode::kSrcOver);
138 GrRenderTarget* rt = rtc->accessRenderTarget();
csmartdaltonc633abb2016-11-01 08:55:55 -0700139 assert_equal(reporter, kTestPatterns[i],
Chris Dalton46983b72017-06-06 12:27:16 -0600140 rt->renderTargetPriv().getMultisampleSpecs(dummyPipeline),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400141 kBottomLeft_GrSurfaceOrigin == rtc->asSurfaceProxy()->origin());
csmartdaltonc633abb2016-11-01 08:55:55 -0700142 }
csmartdalton0d28e572016-07-06 09:59:43 -0700143 }
144 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700145
csmartdalton0d28e572016-07-06 09:59:43 -0700146}
147
148////////////////////////////////////////////////////////////////////////////////////////////////////
149
150class GLTestSampleLocationsInterface : public TestSampleLocationsInterface, public GrGLInterface {
151public:
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
182private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400183 std::unique_ptr<sk_gpu_test::GLTestContext> fTestContext;
csmartdalton0d28e572016-07-06 09:59:43 -0700184 SamplePattern fSamplePattern;
185};
186
Brian Salomondcfca432017-11-15 15:48:03 -0500187DEF_GPUTEST(GLSampleLocations, reporter, /* options */) {
csmartdalton0d28e572016-07-06 09:59:43 -0700188 GLTestSampleLocationsInterface testInterface;
Greg Daniel02611d92017-07-25 10:05:01 -0400189 sk_sp<GrContext> ctx(GrContext::MakeGL(&testInterface));
Eric Karl5c779752017-05-08 12:02:07 -0700190
191 // This test relies on at least 2 samples.
Greg Daniel81e7bf82017-07-19 14:47:42 -0400192 int supportedSample = ctx->caps()->getSampleCount(2, kRGBA_8888_GrPixelConfig);
193 if (supportedSample < 2) {
Eric Karl5c779752017-05-08 12:02:07 -0700194 return;
195 }
Hal Canary342b7ac2016-11-04 11:49:42 -0400196 test_sampleLocations(reporter, &testInterface, ctx.get());
csmartdalton0d28e572016-07-06 09:59:43 -0700197}
198
199#endif