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