blob: 7c4bff9b559019517dce57b68a269bfd990f53a3 [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"
56
57#include <memory>
58#include <utility>
59
60class GrAppliedClip;
61class GrGLSLProgramDataManager;
Chris Daltond7291ba2019-03-07 14:17:03 -070062
63namespace skiagm {
64
65enum class GradType : bool {
66 kHW,
67 kSW
68};
69
70/**
71 * This test ensures that the shaderBuilder's sample offsets and sample mask are correlated with
72 * actual HW sample locations. It does so by drawing pseudo-random subpixel boxes, and only turning
73 * off the samples whose locations fall inside the boxes.
74 */
75class SampleLocationsGM : public GpuGM {
76public:
77 SampleLocationsGM(GradType gradType, GrSurfaceOrigin origin)
78 : fGradType(gradType)
79 , fOrigin(origin) {}
80
81private:
Hal Canaryfa3305a2019-07-18 12:36:54 -040082 SkString onShortName() override {
83 return SkStringPrintf("samplelocations%s%s",
84 (GradType::kHW == fGradType) ? "_hwgrad" : "_swgrad",
85 (kTopLeft_GrSurfaceOrigin == fOrigin) ? "_topleft" : "_botleft");
86 }
87
Chris Daltond7291ba2019-03-07 14:17:03 -070088 SkISize onISize() override { return SkISize::Make(200, 200); }
89 DrawResult onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*, SkString* errorMsg) override;
90
91 const GradType fGradType;
92 const GrSurfaceOrigin fOrigin;
93};
94
95////////////////////////////////////////////////////////////////////////////////////////////////////
96// SkSL code.
97
98class SampleLocationsTestProcessor : public GrGeometryProcessor {
99public:
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500100 static GrGeometryProcessor* Make(SkArenaAlloc* arena, GradType gradType) {
101 return arena->make<SampleLocationsTestProcessor>(gradType);
102 }
103
104 const char* name() const override { return "SampleLocationsTestProcessor"; }
105
106 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
107 b->add32((uint32_t)fGradType);
108 }
109
110 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
111
112private:
113 friend class ::SkArenaAlloc; // for access to ctor
114
Chris Daltond7291ba2019-03-07 14:17:03 -0700115 SampleLocationsTestProcessor(GradType gradType)
116 : GrGeometryProcessor(kSampleLocationsTestProcessor_ClassID)
117 , fGradType(gradType) {
118 this->setWillUseCustomFeature(CustomFeatures::kSampleLocations);
119 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700120
Chris Daltond7291ba2019-03-07 14:17:03 -0700121 const GradType fGradType;
122
123 class Impl;
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500124
125 typedef GrGeometryProcessor INHERITED;
Chris Daltond7291ba2019-03-07 14:17:03 -0700126};
127
128class SampleLocationsTestProcessor::Impl : public GrGLSLGeometryProcessor {
129 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
130 const auto& proc = args.fGP.cast<SampleLocationsTestProcessor>();
131 auto* v = args.fVertBuilder;
132 auto* f = args.fFragBuilder;
133
134 GrGLSLVarying coord(kFloat2_GrSLType);
135 GrGLSLVarying grad(kFloat2_GrSLType);
136 args.fVaryingHandler->addVarying("coord", &coord);
137 if (GradType::kSW == proc.fGradType) {
138 args.fVaryingHandler->addVarying("grad", &grad);
139 }
140
141 // Pixel grid.
142 v->codeAppendf("int x = sk_InstanceID %% 200;");
143 v->codeAppendf("int y = sk_InstanceID / 200;");
144
145 // Create pseudo-random rectangles inside a 16x16 subpixel grid. This works out nicely
146 // because there are 17 positions on the grid (including both edges), and 17 is a great
147 // prime number for generating pseudo-random numbers.
148 v->codeAppendf("int ileft = (sk_InstanceID*929) %% 17;");
149 v->codeAppendf("int iright = ileft + 1 + ((sk_InstanceID*1637) %% (17 - ileft));");
150 v->codeAppendf("int itop = (sk_InstanceID*313) %% 17;");
151 v->codeAppendf("int ibot = itop + 1 + ((sk_InstanceID*1901) %% (17 - itop));");
152
153 // Outset (or inset) the rectangle, for the very likely scenario that samples fall on exact
154 // 16ths of a pixel. GL_SUBPIXEL_BITS is allowed to be as low as 4, so try not to let the
155 // outset value to get too small.
156 v->codeAppendf("float outset = 1/32.0;");
157 v->codeAppendf("outset = (0 == (x + y) %% 2) ? -outset : +outset;");
158 v->codeAppendf("float l = ileft/16.0 - outset;");
159 v->codeAppendf("float r = iright/16.0 + outset;");
160 v->codeAppendf("float t = itop/16.0 - outset;");
161 v->codeAppendf("float b = ibot/16.0 + outset;");
162
163 v->codeAppendf("float2 vertexpos;");
164 v->codeAppendf("vertexpos.x = float(x) + ((0 == (sk_VertexID %% 2)) ? l : r);");
165 v->codeAppendf("vertexpos.y = float(y) + ((0 == (sk_VertexID / 2)) ? t : b);");
166 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
167
168 v->codeAppendf("%s.x = (0 == (sk_VertexID %% 2)) ? -1 : +1;", coord.vsOut());
169 v->codeAppendf("%s.y = (0 == (sk_VertexID / 2)) ? -1 : +1;", coord.vsOut());
170 if (GradType::kSW == proc.fGradType) {
171 v->codeAppendf("%s = 2/float2(r - l, b - t);", grad.vsOut());
172 }
173
174 // Fragment shader: Output RED.
175 f->codeAppendf("%s = half4(1,0,0,1);", args.fOutputColor);
176 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
177
178 // Now turn off all the samples inside our sub-rectangle. As long as the shaderBuilder's
179 // sample offsets and sample mask are correlated with actual HW sample locations, no red
180 // will bleed through.
181 f->codeAppendf("for (int i = 0; i < %i; ++i) {",
182 f->getProgramBuilder()->effectiveSampleCnt());
183 if (GradType::kHW == proc.fGradType) {
184 f->codeAppendf("float2x2 grad = float2x2(dFdx(%s), dFdy(%s));",
185 coord.fsIn(), coord.fsIn());
186 } else {
187 f->codeAppendf("float2x2 grad = float2x2(%s.x, 0, 0, %s.y);", grad.fsIn(), grad.fsIn());
188 }
189 f->codeAppendf( "float2 samplecoord = %s[i] * grad + %s;",
190 f->sampleOffsets(), coord.fsIn());
191 f->codeAppendf( "if (all(lessThanEqual(abs(samplecoord), float2(1)))) {");
192 f->maskOffMultisampleCoverage(
Chris Dalton0dffbab2019-03-27 13:08:50 -0600193 "~(1 << i)", GrGLSLFPFragmentBuilder::ScopeFlags::kInsideLoop);
Chris Daltond7291ba2019-03-07 14:17:03 -0700194 f->codeAppendf( "}");
195 f->codeAppendf("}");
196 }
197
198 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&,
199 FPCoordTransformIter&&) override {}
200};
201
202GrGLSLPrimitiveProcessor* SampleLocationsTestProcessor::createGLSLInstance(
203 const GrShaderCaps&) const {
204 return new Impl();
205}
206
207////////////////////////////////////////////////////////////////////////////////////////////////////
208// Draw Op.
209
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500210static constexpr GrUserStencilSettings gStencilWrite(
211 GrUserStencilSettings::StaticInit<
212 0x0001,
213 GrUserStencilTest::kAlways,
214 0xffff,
215 GrUserStencilOp::kReplace,
216 GrUserStencilOp::kKeep,
217 0xffff>()
218);
219
Chris Daltond7291ba2019-03-07 14:17:03 -0700220class SampleLocationsTestOp : public GrDrawOp {
221public:
222 DEFINE_OP_CLASS_ID
223
224 static std::unique_ptr<GrDrawOp> Make(
225 GrRecordingContext* ctx, const SkMatrix& viewMatrix, GradType gradType) {
226 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
227 return pool->allocate<SampleLocationsTestOp>(gradType);
228 }
229
230private:
231 SampleLocationsTestOp(GradType gradType) : GrDrawOp(ClassID()), fGradType(gradType) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400232 this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsHairline::kNo);
Chris Daltond7291ba2019-03-07 14:17:03 -0700233 }
234
235 const char* name() const override { return "SampleLocationsTestOp"; }
236 FixedFunctionFlags fixedFunctionFlags() const override {
237 return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil;
238 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600239 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
240 bool hasMixedSampledCoverage, GrClampType) override {
Chris Daltond7291ba2019-03-07 14:17:03 -0700241 return GrProcessorSet::EmptySetAnalysis();
242 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700243
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500244 void onPrePrepare(GrRecordingContext* context,
245 const GrSurfaceProxyView* dstView,
Robert Phillips8053c972019-11-21 10:44:53 -0500246 GrAppliedClip* appliedClip,
247 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500248 // We're going to create the GrProgramInfo (and the GrPipeline and geometry processor
249 // it relies on) in the DDL-record-time arena.
250 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Chris Daltonbaa1b352019-04-03 12:03:00 -0600251
Robert Phillips901aff02019-10-08 12:32:56 -0400252
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500253 // Not POD! It has some sk_sp's buried inside it!
254 GrPipeline* pipeline = arena->make<GrPipeline>(GrScissorTest::kDisabled,
255 SkBlendMode::kSrcOver,
256 dstView->swizzle(),
257 GrPipeline::InputFlags::kHWAntialias,
258 &gStencilWrite);
259
260 GrGeometryProcessor* geomProc = SampleLocationsTestProcessor::Make(arena, fGradType);
261
262 // The programInfo is POD
263 GrRenderTargetProxy* dstProxy = dstView->asRenderTargetProxy();
264 fProgramInfo = arena->make<GrProgramInfo>(dstProxy->numSamples(),
265 dstProxy->numStencilSamples(),
266 dstView->origin(),
267 pipeline,
268 geomProc,
269 nullptr, nullptr, 0,
270 GrPrimitiveType::kTriangleStrip);
271 }
272
273 void onPrepare(GrOpFlushState*) final {}
274
275 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
Robert Phillips901aff02019-10-08 12:32:56 -0400276
Chris Daltond7291ba2019-03-07 14:17:03 -0700277 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
278 mesh.setInstanced(nullptr, 200*200, 0, 4);
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500279
280 if (fProgramInfo) {
281 flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1,
282 SkRect::MakeIWH(200, 200));
283 } else {
284 const GrSurfaceProxyView* dstView = flushState->view();
285
286 GrPipeline pipeline(GrScissorTest::kDisabled,
287 SkBlendMode::kSrcOver,
288 dstView->swizzle(),
289 GrPipeline::InputFlags::kHWAntialias,
290 &gStencilWrite);
291
292 GrGeometryProcessor* gp = SampleLocationsTestProcessor::Make(flushState->allocator(),
293 fGradType);
294
295 GrRenderTargetProxy* dstProxy = dstView->asRenderTargetProxy();
296 GrProgramInfo programInfo(dstProxy->numSamples(),
297 dstProxy->numStencilSamples(),
298 dstView->origin(),
299 &pipeline,
300 gp,
301 nullptr, nullptr, 0,
302 GrPrimitiveType::kTriangleStrip);
303
304 flushState->opsRenderPass()->draw(programInfo, &mesh, 1,
305 SkRect::MakeIWH(200, 200));
306 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700307 }
308
309 const GradType fGradType;
310
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500311 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
312 // allocated, are allocated in the ddl-record-time arena. It is the arena's job to free up
313 // their memory so we just have a bare programInfo pointer here. We don't even store the
314 // GrPipeline and GrPrimitiveProcessor pointers here bc they are guaranteed to have the
315 // same lifetime as the program info.
316 GrProgramInfo* fProgramInfo = nullptr;
317
Chris Daltond7291ba2019-03-07 14:17:03 -0700318 friend class ::GrOpMemoryPool; // for ctor
Robert Phillipsfbfc3552019-11-18 11:50:54 -0500319
320 typedef GrDrawOp INHERITED;
Chris Daltond7291ba2019-03-07 14:17:03 -0700321};
322
323////////////////////////////////////////////////////////////////////////////////////////////////////
324// Test.
325
Chris Daltond7291ba2019-03-07 14:17:03 -0700326DrawResult SampleLocationsGM::onDraw(
327 GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas, SkString* errorMsg) {
Chris Daltond7291ba2019-03-07 14:17:03 -0700328 if (!ctx->priv().caps()->sampleLocationsSupport()) {
329 *errorMsg = "Requires support for sample locations.";
330 return DrawResult::kSkip;
331 }
Chris Dalton8a64a442019-10-29 18:54:58 -0600332 if (!ctx->priv().caps()->shaderCaps()->sampleMaskSupport()) {
333 *errorMsg = "Requires support for sample mask.";
Chris Daltond7291ba2019-03-07 14:17:03 -0700334 return DrawResult::kSkip;
335 }
Chris Daltoneffee202019-07-01 22:28:03 -0600336 if (rtc->numSamples() <= 1 && !ctx->priv().caps()->mixedSamplesSupport()) {
337 *errorMsg = "MSAA and mixed samples only.";
338 return DrawResult::kSkip;
339 }
340
341 auto offscreenRTC = ctx->priv().makeDeferredRenderTargetContext(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400342 SkBackingFit::kExact, 200, 200, rtc->colorInfo().colorType(), nullptr,
Chris Daltoneffee202019-07-01 22:28:03 -0600343 rtc->numSamples(), GrMipMapped::kNo, fOrigin);
344 if (!offscreenRTC) {
345 *errorMsg = "Failed to create offscreen render target.";
346 return DrawResult::kFail;
347 }
348 if (offscreenRTC->numSamples() <= 1 &&
349 !offscreenRTC->proxy()->canUseMixedSamples(*ctx->priv().caps())) {
350 *errorMsg = "MSAA and mixed samples only.";
351 return DrawResult::kSkip;
352 }
Chris Daltond7291ba2019-03-07 14:17:03 -0700353
354 static constexpr GrUserStencilSettings kStencilCover(
355 GrUserStencilSettings::StaticInit<
356 0x0000,
357 GrUserStencilTest::kNotEqual,
358 0xffff,
359 GrUserStencilOp::kZero,
360 GrUserStencilOp::kKeep,
361 0xffff>()
362 );
363
Chris Daltoneffee202019-07-01 22:28:03 -0600364 offscreenRTC->clear(nullptr, {0,1,0,1}, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Daltond7291ba2019-03-07 14:17:03 -0700365
Chris Daltoneffee202019-07-01 22:28:03 -0600366 // Stencil.
367 offscreenRTC->priv().testingOnly_addDrawOp(
368 SampleLocationsTestOp::Make(ctx, canvas->getTotalMatrix(), fGradType));
Chris Dalton6ce447a2019-06-23 18:07:38 -0600369
Chris Daltoneffee202019-07-01 22:28:03 -0600370 // Cover.
371 GrPaint coverPaint;
372 coverPaint.setColor4f({1,0,0,1});
373 coverPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrcOver));
374 rtc->priv().stencilRect(GrNoClip(), &kStencilCover, std::move(coverPaint), GrAA::kNo,
375 SkMatrix::I(), SkRect::MakeWH(200, 200));
Chris Daltond7291ba2019-03-07 14:17:03 -0700376
Chris Daltoneffee202019-07-01 22:28:03 -0600377 // Copy offscreen texture to canvas.
Brian Salomon078e8fa2019-11-22 04:10:18 +0000378 rtc->drawTexture(
379 GrNoClip(), sk_ref_sp(offscreenRTC->asTextureProxy()),
380 offscreenRTC->colorInfo().colorType(), GrSamplerState::Filter::kNearest,
381 SkBlendMode::kSrc, SK_PMColor4fWHITE, {0,0,200,200}, {0,0,200,200}, GrAA::kNo,
382 GrQuadAAFlags::kNone, SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
383 SkMatrix::I(), nullptr);
Chris Daltond7291ba2019-03-07 14:17:03 -0700384
385 return skiagm::DrawResult::kOk;
386}
387
388DEF_GM( return new SampleLocationsGM(GradType::kHW, kTopLeft_GrSurfaceOrigin); )
389DEF_GM( return new SampleLocationsGM(GradType::kHW, kBottomLeft_GrSurfaceOrigin); )
390DEF_GM( return new SampleLocationsGM(GradType::kSW, kTopLeft_GrSurfaceOrigin); )
391DEF_GM( return new SampleLocationsGM(GradType::kSW, kBottomLeft_GrSurfaceOrigin); )
392
393}