Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrSamplerState.h" |
| 20 | #include "include/gpu/GrTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "include/private/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 22 | #include "include/private/GrSurfaceProxy.h" |
| 23 | #include "include/private/GrTextureProxy.h" |
| 24 | #include "include/private/GrTypesPriv.h" |
| 25 | #include "include/private/SkColorData.h" |
| 26 | #include "src/gpu/GrBuffer.h" |
| 27 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrClip.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrColorSpaceXform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include "src/gpu/GrGeometryProcessor.h" |
| 32 | #include "src/gpu/GrGpuCommandBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "src/gpu/GrMemoryPool.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 34 | #include "src/gpu/GrMesh.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/gpu/GrOpFlushState.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 36 | #include "src/gpu/GrPaint.h" |
| 37 | #include "src/gpu/GrPipeline.h" |
| 38 | #include "src/gpu/GrPrimitiveProcessor.h" |
| 39 | #include "src/gpu/GrProcessor.h" |
| 40 | #include "src/gpu/GrProcessorSet.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 41 | #include "src/gpu/GrRecordingContextPriv.h" |
| 42 | #include "src/gpu/GrRenderTargetContext.h" |
| 43 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 44 | #include "src/gpu/GrShaderCaps.h" |
| 45 | #include "src/gpu/GrShaderVar.h" |
| 46 | #include "src/gpu/GrUserStencilSettings.h" |
| 47 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 48 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 49 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 50 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 51 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 52 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 53 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 54 | #include "src/gpu/ops/GrDrawOp.h" |
| 55 | #include "src/gpu/ops/GrOp.h" |
| 56 | |
| 57 | #include <memory> |
| 58 | #include <utility> |
| 59 | |
| 60 | class GrAppliedClip; |
| 61 | class GrGLSLProgramDataManager; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 62 | |
| 63 | namespace skiagm { |
| 64 | |
| 65 | enum 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 | */ |
| 75 | class SampleLocationsGM : public GpuGM { |
| 76 | public: |
| 77 | SampleLocationsGM(GradType gradType, GrSurfaceOrigin origin) |
| 78 | : fGradType(gradType) |
| 79 | , fOrigin(origin) {} |
| 80 | |
| 81 | private: |
| 82 | SkString onShortName() override; |
| 83 | SkISize onISize() override { return SkISize::Make(200, 200); } |
| 84 | DrawResult onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*, SkString* errorMsg) override; |
| 85 | |
| 86 | const GradType fGradType; |
| 87 | const GrSurfaceOrigin fOrigin; |
| 88 | }; |
| 89 | |
| 90 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 91 | // SkSL code. |
| 92 | |
| 93 | class SampleLocationsTestProcessor : public GrGeometryProcessor { |
| 94 | public: |
| 95 | SampleLocationsTestProcessor(GradType gradType) |
| 96 | : GrGeometryProcessor(kSampleLocationsTestProcessor_ClassID) |
| 97 | , fGradType(gradType) { |
| 98 | this->setWillUseCustomFeature(CustomFeatures::kSampleLocations); |
| 99 | } |
| 100 | const char* name() const override { return "SampleLocationsTestProcessor"; } |
| 101 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 102 | b->add32((uint32_t)fGradType); |
| 103 | } |
| 104 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 105 | |
| 106 | private: |
| 107 | const GradType fGradType; |
| 108 | |
| 109 | class Impl; |
| 110 | }; |
| 111 | |
| 112 | class SampleLocationsTestProcessor::Impl : public GrGLSLGeometryProcessor { |
| 113 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 114 | const auto& proc = args.fGP.cast<SampleLocationsTestProcessor>(); |
| 115 | auto* v = args.fVertBuilder; |
| 116 | auto* f = args.fFragBuilder; |
| 117 | |
| 118 | GrGLSLVarying coord(kFloat2_GrSLType); |
| 119 | GrGLSLVarying grad(kFloat2_GrSLType); |
| 120 | args.fVaryingHandler->addVarying("coord", &coord); |
| 121 | if (GradType::kSW == proc.fGradType) { |
| 122 | args.fVaryingHandler->addVarying("grad", &grad); |
| 123 | } |
| 124 | |
| 125 | // Pixel grid. |
| 126 | v->codeAppendf("int x = sk_InstanceID %% 200;"); |
| 127 | v->codeAppendf("int y = sk_InstanceID / 200;"); |
| 128 | |
| 129 | // Create pseudo-random rectangles inside a 16x16 subpixel grid. This works out nicely |
| 130 | // because there are 17 positions on the grid (including both edges), and 17 is a great |
| 131 | // prime number for generating pseudo-random numbers. |
| 132 | v->codeAppendf("int ileft = (sk_InstanceID*929) %% 17;"); |
| 133 | v->codeAppendf("int iright = ileft + 1 + ((sk_InstanceID*1637) %% (17 - ileft));"); |
| 134 | v->codeAppendf("int itop = (sk_InstanceID*313) %% 17;"); |
| 135 | v->codeAppendf("int ibot = itop + 1 + ((sk_InstanceID*1901) %% (17 - itop));"); |
| 136 | |
| 137 | // Outset (or inset) the rectangle, for the very likely scenario that samples fall on exact |
| 138 | // 16ths of a pixel. GL_SUBPIXEL_BITS is allowed to be as low as 4, so try not to let the |
| 139 | // outset value to get too small. |
| 140 | v->codeAppendf("float outset = 1/32.0;"); |
| 141 | v->codeAppendf("outset = (0 == (x + y) %% 2) ? -outset : +outset;"); |
| 142 | v->codeAppendf("float l = ileft/16.0 - outset;"); |
| 143 | v->codeAppendf("float r = iright/16.0 + outset;"); |
| 144 | v->codeAppendf("float t = itop/16.0 - outset;"); |
| 145 | v->codeAppendf("float b = ibot/16.0 + outset;"); |
| 146 | |
| 147 | v->codeAppendf("float2 vertexpos;"); |
| 148 | v->codeAppendf("vertexpos.x = float(x) + ((0 == (sk_VertexID %% 2)) ? l : r);"); |
| 149 | v->codeAppendf("vertexpos.y = float(y) + ((0 == (sk_VertexID / 2)) ? t : b);"); |
| 150 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
| 151 | |
| 152 | v->codeAppendf("%s.x = (0 == (sk_VertexID %% 2)) ? -1 : +1;", coord.vsOut()); |
| 153 | v->codeAppendf("%s.y = (0 == (sk_VertexID / 2)) ? -1 : +1;", coord.vsOut()); |
| 154 | if (GradType::kSW == proc.fGradType) { |
| 155 | v->codeAppendf("%s = 2/float2(r - l, b - t);", grad.vsOut()); |
| 156 | } |
| 157 | |
| 158 | // Fragment shader: Output RED. |
| 159 | f->codeAppendf("%s = half4(1,0,0,1);", args.fOutputColor); |
| 160 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 161 | |
| 162 | // Now turn off all the samples inside our sub-rectangle. As long as the shaderBuilder's |
| 163 | // sample offsets and sample mask are correlated with actual HW sample locations, no red |
| 164 | // will bleed through. |
| 165 | f->codeAppendf("for (int i = 0; i < %i; ++i) {", |
| 166 | f->getProgramBuilder()->effectiveSampleCnt()); |
| 167 | if (GradType::kHW == proc.fGradType) { |
| 168 | f->codeAppendf("float2x2 grad = float2x2(dFdx(%s), dFdy(%s));", |
| 169 | coord.fsIn(), coord.fsIn()); |
| 170 | } else { |
| 171 | f->codeAppendf("float2x2 grad = float2x2(%s.x, 0, 0, %s.y);", grad.fsIn(), grad.fsIn()); |
| 172 | } |
| 173 | f->codeAppendf( "float2 samplecoord = %s[i] * grad + %s;", |
| 174 | f->sampleOffsets(), coord.fsIn()); |
| 175 | f->codeAppendf( "if (all(lessThanEqual(abs(samplecoord), float2(1)))) {"); |
| 176 | f->maskOffMultisampleCoverage( |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 177 | "~(1 << i)", GrGLSLFPFragmentBuilder::ScopeFlags::kInsideLoop); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 178 | f->codeAppendf( "}"); |
| 179 | f->codeAppendf("}"); |
| 180 | } |
| 181 | |
| 182 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&, |
| 183 | FPCoordTransformIter&&) override {} |
| 184 | }; |
| 185 | |
| 186 | GrGLSLPrimitiveProcessor* SampleLocationsTestProcessor::createGLSLInstance( |
| 187 | const GrShaderCaps&) const { |
| 188 | return new Impl(); |
| 189 | } |
| 190 | |
| 191 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 192 | // Draw Op. |
| 193 | |
| 194 | class SampleLocationsTestOp : public GrDrawOp { |
| 195 | public: |
| 196 | DEFINE_OP_CLASS_ID |
| 197 | |
| 198 | static std::unique_ptr<GrDrawOp> Make( |
| 199 | GrRecordingContext* ctx, const SkMatrix& viewMatrix, GradType gradType) { |
| 200 | GrOpMemoryPool* pool = ctx->priv().opMemoryPool(); |
| 201 | return pool->allocate<SampleLocationsTestOp>(gradType); |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | SampleLocationsTestOp(GradType gradType) : GrDrawOp(ClassID()), fGradType(gradType) { |
| 206 | this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsZeroArea::kNo); |
| 207 | } |
| 208 | |
| 209 | const char* name() const override { return "SampleLocationsTestOp"; } |
| 210 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 211 | return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil; |
| 212 | } |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 213 | GrProcessorSet::Analysis finalize( |
| 214 | const GrCaps&, const GrAppliedClip*, GrFSAAType, GrClampType) override { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 215 | return GrProcessorSet::EmptySetAnalysis(); |
| 216 | } |
| 217 | void onPrepare(GrOpFlushState*) override {} |
| 218 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 219 | static constexpr GrUserStencilSettings kStencilWrite( |
| 220 | GrUserStencilSettings::StaticInit< |
| 221 | 0x0001, |
| 222 | GrUserStencilTest::kAlways, |
| 223 | 0xffff, |
| 224 | GrUserStencilOp::kReplace, |
| 225 | GrUserStencilOp::kKeep, |
| 226 | 0xffff>() |
| 227 | ); |
| 228 | |
| 229 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver, |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 230 | GrPipeline::InputFlags::kHWAntialias, &kStencilWrite); |
| 231 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 232 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 233 | mesh.setInstanced(nullptr, 200*200, 0, 4); |
| 234 | flushState->rtCommandBuffer()->draw( |
| 235 | SampleLocationsTestProcessor(fGradType), pipeline, nullptr, nullptr, &mesh, 1, |
| 236 | SkRect::MakeIWH(200, 200)); |
| 237 | } |
| 238 | |
| 239 | const GradType fGradType; |
| 240 | |
| 241 | friend class ::GrOpMemoryPool; // for ctor |
| 242 | }; |
| 243 | |
| 244 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 245 | // Test. |
| 246 | |
| 247 | SkString SampleLocationsGM::onShortName() { |
| 248 | SkString name("samplelocations"); |
| 249 | name.append((GradType::kHW == fGradType) ? "_hwgrad" : "_swgrad"); |
| 250 | name.append((kTopLeft_GrSurfaceOrigin == fOrigin) ? "_topleft" : "_botleft"); |
| 251 | return name; |
| 252 | } |
| 253 | |
| 254 | DrawResult SampleLocationsGM::onDraw( |
| 255 | GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas, SkString* errorMsg) { |
| 256 | if (rtc->numStencilSamples() <= 1) { |
| 257 | *errorMsg = "MSAA only."; |
| 258 | return DrawResult::kSkip; |
| 259 | } |
| 260 | if (!ctx->priv().caps()->sampleLocationsSupport()) { |
| 261 | *errorMsg = "Requires support for sample locations."; |
| 262 | return DrawResult::kSkip; |
| 263 | } |
| 264 | if (!ctx->priv().caps()->shaderCaps()->sampleVariablesSupport()) { |
| 265 | *errorMsg = "Requires support for sample variables."; |
| 266 | return DrawResult::kSkip; |
| 267 | } |
| 268 | |
| 269 | static constexpr GrUserStencilSettings kStencilCover( |
| 270 | GrUserStencilSettings::StaticInit< |
| 271 | 0x0000, |
| 272 | GrUserStencilTest::kNotEqual, |
| 273 | 0xffff, |
| 274 | GrUserStencilOp::kZero, |
| 275 | GrUserStencilOp::kKeep, |
| 276 | 0xffff>() |
| 277 | ); |
| 278 | |
| 279 | if (auto offscreenRTC = ctx->priv().makeDeferredRenderTargetContext( |
| 280 | rtc->asSurfaceProxy()->backendFormat(), SkBackingFit::kExact, 200, 200, |
| 281 | rtc->asSurfaceProxy()->config(), nullptr, rtc->numStencilSamples(), GrMipMapped::kNo, |
| 282 | fOrigin)) { |
| 283 | offscreenRTC->clear(nullptr, {0,1,0,1}, GrRenderTargetContext::CanClearFullscreen::kYes); |
| 284 | |
| 285 | // Stencil. |
| 286 | offscreenRTC->priv().testingOnly_addDrawOp( |
| 287 | SampleLocationsTestOp::Make(ctx, canvas->getTotalMatrix(), fGradType)); |
| 288 | |
| 289 | // Cover. |
| 290 | GrPaint coverPaint; |
| 291 | coverPaint.setColor4f({1,0,0,1}); |
| 292 | coverPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrcOver)); |
| 293 | rtc->priv().drawFilledRect( |
| 294 | GrNoClip(), std::move(coverPaint), GrAA::kNo, SkMatrix::I(), |
| 295 | SkRect::MakeWH(200, 200), &kStencilCover); |
| 296 | |
| 297 | // Copy offscreen texture to canvas. |
| 298 | rtc->drawTexture( |
| 299 | GrNoClip(), sk_ref_sp(offscreenRTC->asTextureProxy()), |
| 300 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, SK_PMColor4fWHITE, |
| 301 | {0,0,200,200}, {0,0,200,200}, GrAA::kNo, GrQuadAAFlags::kNone, |
| 302 | SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(), nullptr); |
| 303 | } |
| 304 | |
| 305 | return skiagm::DrawResult::kOk; |
| 306 | } |
| 307 | |
| 308 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kTopLeft_GrSurfaceOrigin); ) |
| 309 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kBottomLeft_GrSurfaceOrigin); ) |
| 310 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kTopLeft_GrSurfaceOrigin); ) |
| 311 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kBottomLeft_GrSurfaceOrigin); ) |
| 312 | |
| 313 | } |