blob: 94ec9a258383b86b31c6a97f9140c48c1334786f [file] [log] [blame]
Chris Daltond7291ba2019-03-07 14:17:03 -07001/*
2 * Copyright 2019 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColorSpace.h"
12#include "include/core/SkMatrix.h"
13#include "include/core/SkRect.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
17#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/private/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/GrTypesPriv.h"
22#include "include/private/SkColorData.h"
23#include "src/gpu/GrBuffer.h"
24#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrMemoryPool.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "src/gpu/GrMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040032#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrPaint.h"
34#include "src/gpu/GrPipeline.h"
35#include "src/gpu/GrPrimitiveProcessor.h"
36#include "src/gpu/GrProcessor.h"
37#include "src/gpu/GrProcessorSet.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrRecordingContextPriv.h"
39#include "src/gpu/GrRenderTargetContext.h"
40#include "src/gpu/GrRenderTargetContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040041#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "src/gpu/GrShaderCaps.h"
43#include "src/gpu/GrShaderVar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040044#include "src/gpu/GrSurfaceProxy.h"
45#include "src/gpu/GrTextureProxy.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040046#include "src/gpu/GrUserStencilSettings.h"
47#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
49#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050051#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
52#include "src/gpu/glsl/GrGLSLVarying.h"
53#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040054#include "src/gpu/ops/GrDrawOp.h"
55#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050056#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040057
58#include <memory>
59#include <utility>
60
61class GrAppliedClip;
62class GrGLSLProgramDataManager;
Chris Daltond7291ba2019-03-07 14:17:03 -070063
64namespace skiagm {
65
66enum class GradType : bool {
67 kHW,
68 kSW
69};
70
71/**
72 * This test ensures that the shaderBuilder's sample offsets and sample mask are correlated with
73 * actual HW sample locations. It does so by drawing pseudo-random subpixel boxes, and only turning
74 * off the samples whose locations fall inside the boxes.
75 */
76class SampleLocationsGM : public GpuGM {
77public:
78 SampleLocationsGM(GradType gradType, GrSurfaceOrigin origin)
79 : fGradType(gradType)
80 , fOrigin(origin) {}
81
82private:
Hal Canaryfa3305a2019-07-18 12:36:54 -040083 SkString onShortName() override {
84 return SkStringPrintf("samplelocations%s%s",
85 (GradType::kHW == fGradType) ? "_hwgrad" : "_swgrad",
86 (kTopLeft_GrSurfaceOrigin == fOrigin) ? "_topleft" : "_botleft");
87 }
88
Chris Daltond7291ba2019-03-07 14:17:03 -070089 SkISize onISize() override { return SkISize::Make(200, 200); }
90 DrawResult onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*, SkString* errorMsg) override;
91
92 const GradType fGradType;
93 const GrSurfaceOrigin fOrigin;
94};
95
96////////////////////////////////////////////////////////////////////////////////////////////////////
97// SkSL code.
98
99class SampleLocationsTestProcessor : public GrGeometryProcessor {
100public:
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500101 static GrGeometryProcessor* Make(SkArenaAlloc* arena, GradType gradType) {
102 return arena->make<SampleLocationsTestProcessor>(gradType);
103 }
104
105 const char* name() const override { return "SampleLocationsTestProcessor"; }
106
107 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
108 b->add32((uint32_t)fGradType);
109 }
110
111 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
112
113private:
114 friend class ::SkArenaAlloc; // for access to ctor
115
Chris Daltond7291ba2019-03-07 14:17:03 -0700116 SampleLocationsTestProcessor(GradType gradType)
117 : GrGeometryProcessor(kSampleLocationsTestProcessor_ClassID)
118 , fGradType(gradType) {
119 this->setWillUseCustomFeature(CustomFeatures::kSampleLocations);
120 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700121
Chris Daltond7291ba2019-03-07 14:17:03 -0700122 const GradType fGradType;
123
124 class Impl;
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500125
126 typedef GrGeometryProcessor INHERITED;
Chris Daltond7291ba2019-03-07 14:17:03 -0700127};
128
129class SampleLocationsTestProcessor::Impl : public GrGLSLGeometryProcessor {
130 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
131 const auto& proc = args.fGP.cast<SampleLocationsTestProcessor>();
132 auto* v = args.fVertBuilder;
133 auto* f = args.fFragBuilder;
134
135 GrGLSLVarying coord(kFloat2_GrSLType);
136 GrGLSLVarying grad(kFloat2_GrSLType);
137 args.fVaryingHandler->addVarying("coord", &coord);
138 if (GradType::kSW == proc.fGradType) {
139 args.fVaryingHandler->addVarying("grad", &grad);
140 }
141
142 // Pixel grid.
143 v->codeAppendf("int x = sk_InstanceID %% 200;");
144 v->codeAppendf("int y = sk_InstanceID / 200;");
145
146 // Create pseudo-random rectangles inside a 16x16 subpixel grid. This works out nicely
147 // because there are 17 positions on the grid (including both edges), and 17 is a great
148 // prime number for generating pseudo-random numbers.
149 v->codeAppendf("int ileft = (sk_InstanceID*929) %% 17;");
150 v->codeAppendf("int iright = ileft + 1 + ((sk_InstanceID*1637) %% (17 - ileft));");
151 v->codeAppendf("int itop = (sk_InstanceID*313) %% 17;");
152 v->codeAppendf("int ibot = itop + 1 + ((sk_InstanceID*1901) %% (17 - itop));");
153
154 // Outset (or inset) the rectangle, for the very likely scenario that samples fall on exact
155 // 16ths of a pixel. GL_SUBPIXEL_BITS is allowed to be as low as 4, so try not to let the
156 // outset value to get too small.
157 v->codeAppendf("float outset = 1/32.0;");
158 v->codeAppendf("outset = (0 == (x + y) %% 2) ? -outset : +outset;");
159 v->codeAppendf("float l = ileft/16.0 - outset;");
160 v->codeAppendf("float r = iright/16.0 + outset;");
161 v->codeAppendf("float t = itop/16.0 - outset;");
162 v->codeAppendf("float b = ibot/16.0 + outset;");
163
164 v->codeAppendf("float2 vertexpos;");
165 v->codeAppendf("vertexpos.x = float(x) + ((0 == (sk_VertexID %% 2)) ? l : r);");
166 v->codeAppendf("vertexpos.y = float(y) + ((0 == (sk_VertexID / 2)) ? t : b);");
167 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
168
169 v->codeAppendf("%s.x = (0 == (sk_VertexID %% 2)) ? -1 : +1;", coord.vsOut());
170 v->codeAppendf("%s.y = (0 == (sk_VertexID / 2)) ? -1 : +1;", coord.vsOut());
171 if (GradType::kSW == proc.fGradType) {
172 v->codeAppendf("%s = 2/float2(r - l, b - t);", grad.vsOut());
173 }
174
175 // Fragment shader: Output RED.
176 f->codeAppendf("%s = half4(1,0,0,1);", args.fOutputColor);
177 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
178
179 // Now turn off all the samples inside our sub-rectangle. As long as the shaderBuilder's
180 // sample offsets and sample mask are correlated with actual HW sample locations, no red
181 // will bleed through.
182 f->codeAppendf("for (int i = 0; i < %i; ++i) {",
183 f->getProgramBuilder()->effectiveSampleCnt());
184 if (GradType::kHW == proc.fGradType) {
185 f->codeAppendf("float2x2 grad = float2x2(dFdx(%s), dFdy(%s));",
186 coord.fsIn(), coord.fsIn());
187 } else {
188 f->codeAppendf("float2x2 grad = float2x2(%s.x, 0, 0, %s.y);", grad.fsIn(), grad.fsIn());
189 }
190 f->codeAppendf( "float2 samplecoord = %s[i] * grad + %s;",
191 f->sampleOffsets(), coord.fsIn());
192 f->codeAppendf( "if (all(lessThanEqual(abs(samplecoord), float2(1)))) {");
193 f->maskOffMultisampleCoverage(
Chris Dalton0dffbab2019-03-27 13:08:50 -0600194 "~(1 << i)", GrGLSLFPFragmentBuilder::ScopeFlags::kInsideLoop);
Chris Daltond7291ba2019-03-07 14:17:03 -0700195 f->codeAppendf( "}");
196 f->codeAppendf("}");
197 }
198
199 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -0500200 const CoordTransformRange&) override {}
Chris Daltond7291ba2019-03-07 14:17:03 -0700201};
202
203GrGLSLPrimitiveProcessor* SampleLocationsTestProcessor::createGLSLInstance(
204 const GrShaderCaps&) const {
205 return new Impl();
206}
207
208////////////////////////////////////////////////////////////////////////////////////////////////////
209// Draw Op.
210
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500211static constexpr GrUserStencilSettings gStencilWrite(
212 GrUserStencilSettings::StaticInit<
213 0x0001,
214 GrUserStencilTest::kAlways,
215 0xffff,
216 GrUserStencilOp::kReplace,
217 GrUserStencilOp::kKeep,
218 0xffff>()
219);
220
Chris Daltond7291ba2019-03-07 14:17:03 -0700221class SampleLocationsTestOp : public GrDrawOp {
222public:
223 DEFINE_OP_CLASS_ID
224
225 static std::unique_ptr<GrDrawOp> Make(
226 GrRecordingContext* ctx, const SkMatrix& viewMatrix, GradType gradType) {
227 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
228 return pool->allocate<SampleLocationsTestOp>(gradType);
229 }
230
231private:
232 SampleLocationsTestOp(GradType gradType) : GrDrawOp(ClassID()), fGradType(gradType) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400233 this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsHairline::kNo);
Chris Daltond7291ba2019-03-07 14:17:03 -0700234 }
235
236 const char* name() const override { return "SampleLocationsTestOp"; }
237 FixedFunctionFlags fixedFunctionFlags() const override {
238 return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil;
239 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600240 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
241 bool hasMixedSampledCoverage, GrClampType) override {
Chris Daltond7291ba2019-03-07 14:17:03 -0700242 return GrProcessorSet::EmptySetAnalysis();
243 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700244
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500245 void onPrePrepare(GrRecordingContext* context,
246 const GrSurfaceProxyView* dstView,
Robert Phillips34cea002019-11-21 16:02:34 -0500247 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500248 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500249 // We're going to create the GrProgramInfo (and the GrPipeline and geometry processor
250 // it relies on) in the DDL-record-time arena.
251 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Chris Daltonbaa1b352019-04-03 12:03:00 -0600252
Robert Phillips34cea002019-11-21 16:02:34 -0500253 // This is equivalent to a GrOpFlushState::detachAppliedClip
254 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500255
256 GrGeometryProcessor* geomProc = SampleLocationsTestProcessor::Make(arena, fGradType);
257
Robert Phillips34cea002019-11-21 16:02:34 -0500258 fProgramInfo = sk_gpu_test::CreateProgramInfo(context->priv().caps(), arena, dstView,
259 std::move(appliedClip), dstProxyView,
260 geomProc, SkBlendMode::kSrcOver,
261 GrPrimitiveType::kTriangleStrip,
262 GrPipeline::InputFlags::kHWAntialias,
263 &gStencilWrite);
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500264 }
265
266 void onPrepare(GrOpFlushState*) final {}
267
268 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
Robert Phillips901aff02019-10-08 12:32:56 -0400269
Robert Phillips34cea002019-11-21 16:02:34 -0500270 if (!fProgramInfo) {
271 auto geomProc = SampleLocationsTestProcessor::Make(flushState->allocator(),
272 fGradType);
273
274 fProgramInfo = sk_gpu_test::CreateProgramInfo(&flushState->caps(),
275 flushState->allocator(),
276 flushState->view(),
277 flushState->detachAppliedClip(),
278 flushState->dstProxyView(),
279 geomProc, SkBlendMode::kSrcOver,
280 GrPrimitiveType::kTriangleStrip,
281 GrPipeline::InputFlags::kHWAntialias,
282 &gStencilWrite);
283 }
284
Chris Daltond7291ba2019-03-07 14:17:03 -0700285 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
286 mesh.setInstanced(nullptr, 200*200, 0, 4);
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500287
Robert Phillips34cea002019-11-21 16:02:34 -0500288 flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1, SkRect::MakeIWH(200, 200));
Chris Daltond7291ba2019-03-07 14:17:03 -0700289 }
290
291 const GradType fGradType;
292
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500293 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500294 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
295 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
296 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
297 // guaranteed to have the same lifetime as the program info.
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500298 GrProgramInfo* fProgramInfo = nullptr;
299
Chris Daltond7291ba2019-03-07 14:17:03 -0700300 friend class ::GrOpMemoryPool; // for ctor
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500301
302 typedef GrDrawOp INHERITED;
Chris Daltond7291ba2019-03-07 14:17:03 -0700303};
304
305////////////////////////////////////////////////////////////////////////////////////////////////////
306// Test.
307
Chris Daltond7291ba2019-03-07 14:17:03 -0700308DrawResult SampleLocationsGM::onDraw(
309 GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas, SkString* errorMsg) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700310 if (!ctx->priv().caps()->sampleLocationsSupport()) {
311 *errorMsg = "Requires support for sample locations.";
312 return DrawResult::kSkip;
313 }
Chris Dalton8a64a442019-10-29 18:54:58 -0600314 if (!ctx->priv().caps()->shaderCaps()->sampleMaskSupport()) {
315 *errorMsg = "Requires support for sample mask.";
Chris Daltond7291ba2019-03-07 14:17:03 -0700316 return DrawResult::kSkip;
317 }
Chris Daltoneffee202019-07-01 22:28:03 -0600318 if (rtc->numSamples() <= 1 && !ctx->priv().caps()->mixedSamplesSupport()) {
319 *errorMsg = "MSAA and mixed samples only.";
320 return DrawResult::kSkip;
321 }
322
Greg Daniele20fcad2020-01-08 11:52:34 -0500323 auto offscreenRTC = GrRenderTargetContext::Make(
324 ctx, rtc->colorInfo().colorType(), nullptr, SkBackingFit::kExact, {200, 200},
325 rtc->numSamples(), GrMipMapped::kNo, GrProtected::kNo, fOrigin);
Chris Daltoneffee202019-07-01 22:28:03 -0600326 if (!offscreenRTC) {
327 *errorMsg = "Failed to create offscreen render target.";
328 return DrawResult::kFail;
329 }
330 if (offscreenRTC->numSamples() <= 1 &&
Greg Daniel46e366a2019-12-16 14:38:36 -0500331 !offscreenRTC->asRenderTargetProxy()->canUseMixedSamples(*ctx->priv().caps())) {
Chris Daltoneffee202019-07-01 22:28:03 -0600332 *errorMsg = "MSAA and mixed samples only.";
333 return DrawResult::kSkip;
334 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700335
336 static constexpr GrUserStencilSettings kStencilCover(
337 GrUserStencilSettings::StaticInit<
338 0x0000,
339 GrUserStencilTest::kNotEqual,
340 0xffff,
341 GrUserStencilOp::kZero,
342 GrUserStencilOp::kKeep,
343 0xffff>()
344 );
345
Chris Daltoneffee202019-07-01 22:28:03 -0600346 offscreenRTC->clear(nullptr, {0,1,0,1}, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Daltond7291ba2019-03-07 14:17:03 -0700347
Chris Daltoneffee202019-07-01 22:28:03 -0600348 // Stencil.
349 offscreenRTC->priv().testingOnly_addDrawOp(
350 SampleLocationsTestOp::Make(ctx, canvas->getTotalMatrix(), fGradType));
Chris Dalton6ce447a2019-06-23 18:07:38 -0600351
Chris Daltoneffee202019-07-01 22:28:03 -0600352 // Cover.
353 GrPaint coverPaint;
354 coverPaint.setColor4f({1,0,0,1});
355 coverPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrcOver));
356 rtc->priv().stencilRect(GrNoClip(), &kStencilCover, std::move(coverPaint), GrAA::kNo,
357 SkMatrix::I(), SkRect::MakeWH(200, 200));
Chris Daltond7291ba2019-03-07 14:17:03 -0700358
Chris Daltoneffee202019-07-01 22:28:03 -0600359 // Copy offscreen texture to canvas.
Greg Daniel40903af2020-01-30 14:55:05 -0500360 rtc->drawTexture(GrNoClip(), offscreenRTC->readSurfaceView(),
361 offscreenRTC->colorInfo().alphaType(),
Brian Salomonfc118442019-11-22 19:09:27 -0500362 GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, SK_PMColor4fWHITE,
363 {0,0,200,200}, {0,0,200,200}, GrAA::kNo, GrQuadAAFlags::kNone,
364 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
365 nullptr);
Chris Daltond7291ba2019-03-07 14:17:03 -0700366
367 return skiagm::DrawResult::kOk;
368}
369
370DEF_GM( return new SampleLocationsGM(GradType::kHW, kTopLeft_GrSurfaceOrigin); )
371DEF_GM( return new SampleLocationsGM(GradType::kHW, kBottomLeft_GrSurfaceOrigin); )
372DEF_GM( return new SampleLocationsGM(GradType::kSW, kTopLeft_GrSurfaceOrigin); )
373DEF_GM( return new SampleLocationsGM(GradType::kSW, kBottomLeft_GrSurfaceOrigin); )
374
375}