blob: 75fa6fcf65da4d8693dcfe27e61a07b98fe43f56 [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"
16#include "GrPipelineBuilder.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070017#include "GrRenderTargetContext.h"
csmartdalton0d28e572016-07-06 09:59:43 -070018#include "GrRenderTargetPriv.h"
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050019#include "GrTypesPriv.h"
csmartdalton0d28e572016-07-06 09:59:43 -070020#include "gl/GrGLGpu.h"
21#include "gl/debug/DebugGLTestContext.h"
22
23typedef std::vector<SkPoint> SamplePattern;
24
25static 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};
86constexpr int numTestPatterns = SK_ARRAY_COUNT(kTestPatterns);
87
88class TestSampleLocationsInterface : public SkNoncopyable {
89public:
90 virtual void overrideSamplePattern(const SamplePattern&) = 0;
91 virtual ~TestSampleLocationsInterface() {}
92};
93
Brian Salomonb5cb6832017-02-24 11:01:15 -050094static void construct_dummy_pipeline(GrRenderTargetContext* dc, GrPipeline* pipeline) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050095 GrPipelineBuilder dummyBuilder(GrPaint(), GrAAType::kNone);
csmartdaltonc633abb2016-11-01 08:55:55 -070096 GrScissorState dummyScissor;
97 GrWindowRectsState dummyWindows;
csmartdaltonc633abb2016-11-01 08:55:55 -070098
Brian Salomonb5cb6832017-02-24 11:01:15 -050099 GrPipeline::InitArgs args;
100 dummyBuilder.getPipelineInitArgs(&args);
Brian Salomonb16e8ac2017-02-22 11:52:36 -0500101 args.fRenderTarget = dc->accessRenderTarget();
csmartdaltonc633abb2016-11-01 08:55:55 -0700102 args.fCaps = dc->caps();
csmartdaltonc633abb2016-11-01 08:55:55 -0700103 args.fDstTexture = GrXferProcessor::DstTexture();
Brian Salomonb5cb6832017-02-24 11:01:15 -0500104 pipeline->init(args);
csmartdalton0d28e572016-07-06 09:59:43 -0700105}
106
107void assert_equal(skiatest::Reporter* reporter, const SamplePattern& pattern,
108 const GrGpu::MultisampleSpecs& specs, bool flipY) {
109 GrAlwaysAssert(specs.fSampleLocations);
110 if ((int)pattern.size() != specs.fEffectiveSampleCnt) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700111 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong number of samples."));
csmartdalton0d28e572016-07-06 09:59:43 -0700112 return;
113 }
114 for (int i = 0; i < specs.fEffectiveSampleCnt; ++i) {
115 SkPoint expectedLocation = specs.fSampleLocations[i];
116 if (flipY) {
117 expectedLocation.fY = 1 - expectedLocation.fY;
118 }
119 if (pattern[i] != expectedLocation) {
csmartdaltonc633abb2016-11-01 08:55:55 -0700120 REPORT_FAILURE(reporter, "", SkString("Sample pattern has wrong sample location."));
csmartdalton0d28e572016-07-06 09:59:43 -0700121 return;
122 }
123 }
124}
125
126void test_sampleLocations(skiatest::Reporter* reporter, TestSampleLocationsInterface* testInterface,
127 GrContext* ctx) {
128 SkRandom rand;
csmartdaltonc633abb2016-11-01 08:55:55 -0700129 sk_sp<GrRenderTargetContext> bottomUps[numTestPatterns];
130 sk_sp<GrRenderTargetContext> topDowns[numTestPatterns];
csmartdalton0d28e572016-07-06 09:59:43 -0700131 for (int i = 0; i < numTestPatterns; ++i) {
132 int numSamples = (int)kTestPatterns[i].size();
133 GrAlwaysAssert(numSamples > 1 && SkIsPow2(numSamples));
csmartdaltonc633abb2016-11-01 08:55:55 -0700134 bottomUps[i] = ctx->makeRenderTargetContextWithFallback(
135 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
136 rand.nextRangeU(1 + numSamples / 2, numSamples),
137 kBottomLeft_GrSurfaceOrigin);
138 topDowns[i] = ctx->makeRenderTargetContextWithFallback(
139 SkBackingFit::kExact, 100, 100, kRGBA_8888_GrPixelConfig, nullptr,
140 rand.nextRangeU(1 + numSamples / 2, numSamples),
141 kTopLeft_GrSurfaceOrigin);
csmartdalton0d28e572016-07-06 09:59:43 -0700142 }
143
144 // Ensure all sample locations get queried and/or cached properly.
csmartdalton0d28e572016-07-06 09:59:43 -0700145 for (int repeat = 0; repeat < 2; ++repeat) {
146 for (int i = 0; i < numTestPatterns; ++i) {
147 testInterface->overrideSamplePattern(kTestPatterns[i]);
csmartdaltonc633abb2016-11-01 08:55:55 -0700148 for (GrRenderTargetContext* dc : {bottomUps[i].get(), topDowns[i].get()}) {
Brian Salomonb5cb6832017-02-24 11:01:15 -0500149 GrPipeline dummyPipeline;
150 construct_dummy_pipeline(dc, &dummyPipeline);
csmartdaltonc633abb2016-11-01 08:55:55 -0700151 GrRenderTarget* rt = dc->accessRenderTarget();
152 assert_equal(reporter, kTestPatterns[i],
Brian Salomonb5cb6832017-02-24 11:01:15 -0500153 rt->renderTargetPriv().getMultisampleSpecs(dummyPipeline),
csmartdaltonc633abb2016-11-01 08:55:55 -0700154 kBottomLeft_GrSurfaceOrigin == rt->origin());
csmartdaltonc633abb2016-11-01 08:55:55 -0700155 }
csmartdalton0d28e572016-07-06 09:59:43 -0700156 }
157 }
csmartdaltonc633abb2016-11-01 08:55:55 -0700158
csmartdalton0d28e572016-07-06 09:59:43 -0700159}
160
161////////////////////////////////////////////////////////////////////////////////////////////////////
162
163class GLTestSampleLocationsInterface : public TestSampleLocationsInterface, public GrGLInterface {
164public:
165 GLTestSampleLocationsInterface() : fTestContext(sk_gpu_test::CreateDebugGLTestContext()) {
166 fStandard = fTestContext->gl()->fStandard;
167 fExtensions = fTestContext->gl()->fExtensions;
168 fFunctions = fTestContext->gl()->fFunctions;
169
170 fFunctions.fGetIntegerv = [&](GrGLenum pname, GrGLint* params) {
171 GrAlwaysAssert(GR_GL_EFFECTIVE_RASTER_SAMPLES != pname);
172 if (GR_GL_SAMPLES == pname) {
173 GrAlwaysAssert(!fSamplePattern.empty());
174 *params = (int)fSamplePattern.size();
175 } else {
176 fTestContext->gl()->fFunctions.fGetIntegerv(pname, params);
177 }
178 };
179
180 fFunctions.fGetMultisamplefv = [&](GrGLenum pname, GrGLuint index, GrGLfloat* val) {
181 GrAlwaysAssert(GR_GL_SAMPLE_POSITION == pname);
182 val[0] = fSamplePattern[index].fX;
183 val[1] = fSamplePattern[index].fY;
184 };
185 }
186
187 operator GrBackendContext() {
188 return reinterpret_cast<GrBackendContext>(static_cast<GrGLInterface*>(this));
189 }
190
191 void overrideSamplePattern(const SamplePattern& newPattern) override {
192 fSamplePattern = newPattern;
193 }
194
195private:
Ben Wagner145dbcd2016-11-03 14:40:50 -0400196 std::unique_ptr<sk_gpu_test::GLTestContext> fTestContext;
csmartdalton0d28e572016-07-06 09:59:43 -0700197 SamplePattern fSamplePattern;
198};
199
200DEF_GPUTEST(GLSampleLocations, reporter, /*factory*/) {
201 GLTestSampleLocationsInterface testInterface;
Hal Canary342b7ac2016-11-04 11:49:42 -0400202 sk_sp<GrContext> ctx(GrContext::Create(kOpenGL_GrBackend, testInterface));
203 test_sampleLocations(reporter, &testInterface, ctx.get());
csmartdalton0d28e572016-07-06 09:59:43 -0700204}
205
206#endif