blob: 3a59e9db5f89e888536b47273b4940e9b88395d7 [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
csmartdaltonc633abb2016-11-01 08:55:55 -070015#include "GrRenderTargetContext.h"
csmartdalton0d28e572016-07-06 09:59:43 -070016#include "GrRenderTargetPriv.h"
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050017#include "GrTypesPriv.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070018#include "GrPipelineBuilder.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
csmartdaltonc633abb2016-11-01 08:55:55 -070093static GrPipeline* construct_dummy_pipeline(GrRenderTargetContext* dc, void* storage) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050094 GrPipelineBuilder dummyBuilder(GrPaint(), GrAAType::kNone);
csmartdaltonc633abb2016-11-01 08:55:55 -070095 GrScissorState dummyScissor;
96 GrWindowRectsState dummyWindows;
97 GrXPOverridesForBatch dummyOverrides;
98
99 GrPipeline::CreateArgs args;
100 args.fPipelineBuilder = &dummyBuilder;
101 args.fRenderTargetContext = dc;
102 args.fCaps = dc->caps();
103 args.fOpts = GrPipelineOptimizations();
104 args.fScissor = &dummyScissor;
105 args.fWindowRectsState = &dummyWindows;
106 args.fHasStencilClip = false;
107 args.fDstTexture = GrXferProcessor::DstTexture();
108
109 GrPipeline::CreateAt(storage, args, &dummyOverrides);
110 return reinterpret_cast<GrPipeline*>(storage);
csmartdalton0d28e572016-07-06 09:59:43 -0700111}
112
113void assert_equal(skiatest::Reporter* reporter, const SamplePattern& pattern,
114 const GrGpu::MultisampleSpecs& specs, bool flipY) {
115 GrAlwaysAssert(specs.fSampleLocations);
116 if ((int)pattern.size() != specs.fEffectiveSampleCnt) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700117 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong number of samples."));
csmartdalton0d28e572016-07-06 09:59:43 -0700118 return;
119 }
120 for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
121 SkPoint expectedLocation = specs.fSampleLocations[i];
122 if (flipY) {
123 expectedLocation.fY = 1 - expectedLocation.fY;
124 }
125 if (pattern[i] != expectedLocation) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700126 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong sample location."));
csmartdalton0d28e572016-07-06 09:59:43 -0700127 return;
128 }
129 }
130}
131
132void test_sampleLocations(skiatest::Reporter* reporter, TestSampleLocationsInterface* testInterface,
133 GrContext* ctx) {
134 SkRandom rand;
csmartdaltonc633abb2016-11-01 08:55:55 -0700135 sk_sp<GrRenderTargetContext> bottomUps[numTestPatterns];
136 sk_sp<GrRenderTargetContext> topDowns[numTestPatterns];
csmartdalton0d28e572016-07-06 09:59:43 -0700137 for (int i = 0; i < numTestPatterns; ++i) {
138 int numSamples = (int)kTestPatterns[i].size();
139 GrAlwaysAssert(numSamples > 1 && SkIsPow2(numSamples));
csmartdaltonc633abb2016-11-01 08:55:55 -0700140 bottomUps[i] = ctx->makeRenderTargetContextWithFallback(
141 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
142 rand.nextRangeU(1 + numSamples / 2, numSamples),
143 kBottomLeft_GrSurfaceOrigin);
144 topDowns[i] = ctx->makeRenderTargetContextWithFallback(
145 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
146 rand.nextRangeU(1 + numSamples / 2, numSamples),
147 kTopLeft_GrSurfaceOrigin);
csmartdalton0d28e572016-07-06 09:59:43 -0700148 }
149
150 // Ensure all sample locations get queried and/or cached properly.
csmartdaltonc633abb2016-11-01 08:55:55 -0700151 SkAlignedSTStorage<1, GrPipeline> pipelineStorage;
csmartdalton0d28e572016-07-06 09:59:43 -0700152 for (int repeat = 0; repeat < 2; ++repeat) {
153 for (int i = 0; i < numTestPatterns; ++i) {
154 testInterface->overrideSamplePattern(kTestPatterns[i]);
csmartdaltonc633abb2016-11-01 08:55:55 -0700155 for (GrRenderTargetContext* dc : {bottomUps[i].get(), topDowns[i].get()}) {
156 GrPipeline* dummyPipe = construct_dummy_pipeline(dc, pipelineStorage.get());
157 GrRenderTarget* rt = dc->accessRenderTarget();
158 assert_equal(reporter, kTestPatterns[i],
159 rt->renderTargetPriv().getMultisampleSpecs(*dummyPipe),
160 kBottomLeft_GrSurfaceOrigin == rt->origin());
161 dummyPipe->~GrPipeline();
162 }
csmartdalton0d28e572016-07-06 09:59:43 -0700163 }
164 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700165
csmartdalton0d28e572016-07-06 09:59:43 -0700166}
167
168////////////////////////////////////////////////////////////////////////////////////////////////////
169
170class GLTestSampleLocationsInterface : public TestSampleLocationsInterface, public GrGLInterface {
171public:
172 GLTestSampleLocationsInterface() : fTestContext(sk_gpu_test::CreateDebugGLTestContext()) {
173 fStandard = fTestContext->gl()->fStandard;
174 fExtensions = fTestContext->gl()->fExtensions;
175 fFunctions = fTestContext->gl()->fFunctions;
176
177 fFunctions.fGetIntegerv = [&](GrGLenum pname, GrGLint* params) {
178 GrAlwaysAssert(GR_GL_EFFECTIVE_RASTER_SAMPLES != pname);
179 if (GR_GL_SAMPLES == pname) {
180 GrAlwaysAssert(!fSamplePattern.empty());
181 *params = (int)fSamplePattern.size();
182 } else {
183 fTestContext->gl()->fFunctions.fGetIntegerv(pname, params);
184 }
185 };
186
187 fFunctions.fGetMultisamplefv = [&](GrGLenum pname, GrGLuint index, GrGLfloat* val) {
188 GrAlwaysAssert(GR_GL_SAMPLE_POSITION == pname);
189 val[0] = fSamplePattern[index].fX;
190 val[1] = fSamplePattern[index].fY;
191 };
192 }
193
194 operator GrBackendContext() {
195 return reinterpret_cast<GrBackendContext>(static_cast<GrGLInterface*>(this));
196 }
197
198 void overrideSamplePattern(const SamplePattern& newPattern) override {
199 fSamplePattern = newPattern;
200 }
201
202private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400203 std::unique_ptr<sk_gpu_test::GLTestContext> fTestContext;
csmartdalton0d28e572016-07-06 09:59:43 -0700204 SamplePattern fSamplePattern;
205};
206
207DEF_GPUTEST(GLSampleLocations, reporter, /*factory*/) {
208 GLTestSampleLocationsInterface testInterface;
Hal Canary342b7ac2016-11-04 11:49:42 -0400209 sk_sp<GrContext> ctx(GrContext::Create(kOpenGL_GrBackend, testInterface));
210 test_sampleLocations(reporter, &testInterface, ctx.get());
csmartdalton0d28e572016-07-06 09:59:43 -0700211}
212
213#endif