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" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/private/GrTypesPriv.h" |
| 22 | #include "include/private/SkColorData.h" |
| 23 | #include "src/gpu/GrBuffer.h" |
| 24 | #include "src/gpu/GrCaps.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrColorSpaceXform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "src/gpu/GrGeometryProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/GrMemoryPool.h" |
| 29 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 30 | #include "src/gpu/GrOpsRenderPass.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include "src/gpu/GrPaint.h" |
| 32 | #include "src/gpu/GrPipeline.h" |
| 33 | #include "src/gpu/GrPrimitiveProcessor.h" |
| 34 | #include "src/gpu/GrProcessor.h" |
| 35 | #include "src/gpu/GrProcessorSet.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/gpu/GrRecordingContextPriv.h" |
| 37 | #include "src/gpu/GrRenderTargetContext.h" |
| 38 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 39 | #include "src/gpu/GrSamplerState.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 40 | #include "src/gpu/GrShaderCaps.h" |
| 41 | #include "src/gpu/GrShaderVar.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 42 | #include "src/gpu/GrSurfaceProxy.h" |
| 43 | #include "src/gpu/GrTextureProxy.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 44 | #include "src/gpu/GrUserStencilSettings.h" |
| 45 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 47 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 48 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 49 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 50 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 51 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 52 | #include "src/gpu/ops/GrDrawOp.h" |
| 53 | #include "src/gpu/ops/GrOp.h" |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 54 | #include "tools/gpu/ProxyUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 55 | |
| 56 | #include <memory> |
| 57 | #include <utility> |
| 58 | |
| 59 | class GrAppliedClip; |
| 60 | class GrGLSLProgramDataManager; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 61 | |
| 62 | namespace skiagm { |
| 63 | |
| 64 | enum class GradType : bool { |
| 65 | kHW, |
| 66 | kSW |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * This test ensures that the shaderBuilder's sample offsets and sample mask are correlated with |
| 71 | * actual HW sample locations. It does so by drawing pseudo-random subpixel boxes, and only turning |
| 72 | * off the samples whose locations fall inside the boxes. |
| 73 | */ |
| 74 | class SampleLocationsGM : public GpuGM { |
| 75 | public: |
| 76 | SampleLocationsGM(GradType gradType, GrSurfaceOrigin origin) |
| 77 | : fGradType(gradType) |
| 78 | , fOrigin(origin) {} |
| 79 | |
| 80 | private: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 81 | SkString onShortName() override { |
| 82 | return SkStringPrintf("samplelocations%s%s", |
| 83 | (GradType::kHW == fGradType) ? "_hwgrad" : "_swgrad", |
| 84 | (kTopLeft_GrSurfaceOrigin == fOrigin) ? "_topleft" : "_botleft"); |
| 85 | } |
| 86 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 87 | SkISize onISize() override { return SkISize::Make(200, 200); } |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 88 | DrawResult onDraw(GrRecordingContext*, GrRenderTargetContext*, |
| 89 | SkCanvas*, SkString* errorMsg) override; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 90 | |
| 91 | const GradType fGradType; |
| 92 | const GrSurfaceOrigin fOrigin; |
| 93 | }; |
| 94 | |
| 95 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 96 | // SkSL code. |
| 97 | |
| 98 | class SampleLocationsTestProcessor : public GrGeometryProcessor { |
| 99 | public: |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 100 | 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 | |
| 112 | private: |
| 113 | friend class ::SkArenaAlloc; // for access to ctor |
| 114 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 115 | SampleLocationsTestProcessor(GradType gradType) |
| 116 | : GrGeometryProcessor(kSampleLocationsTestProcessor_ClassID) |
| 117 | , fGradType(gradType) { |
| 118 | this->setWillUseCustomFeature(CustomFeatures::kSampleLocations); |
| 119 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 120 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 121 | const GradType fGradType; |
| 122 | |
| 123 | class Impl; |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 124 | |
| 125 | typedef GrGeometryProcessor INHERITED; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | class 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 Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 193 | "~(1 << i)", GrGLSLFPFragmentBuilder::ScopeFlags::kInsideLoop); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 194 | f->codeAppendf( "}"); |
| 195 | f->codeAppendf("}"); |
| 196 | } |
| 197 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 198 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override {} |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | GrGLSLPrimitiveProcessor* SampleLocationsTestProcessor::createGLSLInstance( |
| 202 | const GrShaderCaps&) const { |
| 203 | return new Impl(); |
| 204 | } |
| 205 | |
| 206 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 207 | // Draw Op. |
| 208 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 209 | static constexpr GrUserStencilSettings gStencilWrite( |
| 210 | GrUserStencilSettings::StaticInit< |
| 211 | 0x0001, |
| 212 | GrUserStencilTest::kAlways, |
| 213 | 0xffff, |
| 214 | GrUserStencilOp::kReplace, |
| 215 | GrUserStencilOp::kKeep, |
| 216 | 0xffff>() |
| 217 | ); |
| 218 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 219 | class SampleLocationsTestOp : public GrDrawOp { |
| 220 | public: |
| 221 | DEFINE_OP_CLASS_ID |
| 222 | |
| 223 | static std::unique_ptr<GrDrawOp> Make( |
| 224 | GrRecordingContext* ctx, const SkMatrix& viewMatrix, GradType gradType) { |
| 225 | GrOpMemoryPool* pool = ctx->priv().opMemoryPool(); |
| 226 | return pool->allocate<SampleLocationsTestOp>(gradType); |
| 227 | } |
| 228 | |
| 229 | private: |
| 230 | SampleLocationsTestOp(GradType gradType) : GrDrawOp(ClassID()), fGradType(gradType) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 231 | this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | const char* name() const override { return "SampleLocationsTestOp"; } |
| 235 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 236 | return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil; |
| 237 | } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 238 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 239 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 240 | return GrProcessorSet::EmptySetAnalysis(); |
| 241 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 242 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 243 | |
| 244 | GrProgramInfo* createProgramInfo(const GrCaps* caps, |
| 245 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 246 | const GrSurfaceProxyView* writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 247 | GrAppliedClip&& appliedClip, |
| 248 | const GrXferProcessor::DstProxyView& dstProxyView) const { |
| 249 | GrGeometryProcessor* geomProc = SampleLocationsTestProcessor::Make(arena, fGradType); |
| 250 | |
| 251 | GrPipeline::InputFlags flags = GrPipeline::InputFlags::kHWAntialias; |
| 252 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 253 | return sk_gpu_test::CreateProgramInfo(caps, arena, writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 254 | std::move(appliedClip), dstProxyView, |
| 255 | geomProc, SkBlendMode::kSrcOver, |
| 256 | GrPrimitiveType::kTriangleStrip, |
| 257 | flags, &gStencilWrite); |
| 258 | } |
| 259 | |
| 260 | GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const { |
| 261 | return this->createProgramInfo(&flushState->caps(), |
| 262 | flushState->allocator(), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 263 | flushState->writeView(), |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 264 | flushState->detachAppliedClip(), |
| 265 | flushState->dstProxyView()); |
| 266 | } |
| 267 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 268 | void onPrePrepare(GrRecordingContext* context, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 269 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 270 | GrAppliedClip* clip, |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 271 | const GrXferProcessor::DstProxyView& dstProxyView) final { |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 272 | // We're going to create the GrProgramInfo (and the GrPipeline and geometry processor |
| 273 | // it relies on) in the DDL-record-time arena. |
| 274 | SkArenaAlloc* arena = context->priv().recordTimeAllocator(); |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 275 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 276 | // This is equivalent to a GrOpFlushState::detachAppliedClip |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 277 | GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled(); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 278 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 279 | fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 280 | std::move(appliedClip), dstProxyView); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 281 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 282 | context->priv().recordProgramInfo(fProgramInfo); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void onPrepare(GrOpFlushState*) final {} |
| 286 | |
| 287 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final { |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 288 | if (!fProgramInfo) { |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 289 | fProgramInfo = this->createProgramInfo(flushState); |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 292 | flushState->bindPipelineAndScissorClip(*fProgramInfo, SkRect::MakeIWH(200, 200)); |
| 293 | flushState->bindBuffers(nullptr, nullptr, nullptr); |
| 294 | flushState->drawInstanced(200*200, 0, 4, 0); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | const GradType fGradType; |
| 298 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 299 | // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 300 | // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the |
| 301 | // arena's job to free up their memory so we just have a bare programInfo pointer here. We |
| 302 | // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are |
| 303 | // guaranteed to have the same lifetime as the program info. |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 304 | GrProgramInfo* fProgramInfo = nullptr; |
| 305 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 306 | friend class ::GrOpMemoryPool; // for ctor |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 307 | |
| 308 | typedef GrDrawOp INHERITED; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 312 | // Test. |
| 313 | |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 314 | DrawResult SampleLocationsGM::onDraw(GrRecordingContext* ctx, GrRenderTargetContext* rtc, |
| 315 | SkCanvas* canvas, SkString* errorMsg) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 316 | if (!ctx->priv().caps()->sampleLocationsSupport()) { |
| 317 | *errorMsg = "Requires support for sample locations."; |
| 318 | return DrawResult::kSkip; |
| 319 | } |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 320 | if (!ctx->priv().caps()->shaderCaps()->sampleMaskSupport()) { |
| 321 | *errorMsg = "Requires support for sample mask."; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 322 | return DrawResult::kSkip; |
| 323 | } |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 324 | if (!ctx->priv().caps()->drawInstancedSupport()) { |
| 325 | *errorMsg = "Requires support for instanced rendering."; |
| 326 | return DrawResult::kSkip; |
| 327 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 328 | if (rtc->numSamples() <= 1 && !ctx->priv().caps()->mixedSamplesSupport()) { |
| 329 | *errorMsg = "MSAA and mixed samples only."; |
| 330 | return DrawResult::kSkip; |
| 331 | } |
| 332 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 333 | auto offscreenRTC = GrRenderTargetContext::Make( |
| 334 | ctx, rtc->colorInfo().colorType(), nullptr, SkBackingFit::kExact, {200, 200}, |
| 335 | rtc->numSamples(), GrMipMapped::kNo, GrProtected::kNo, fOrigin); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 336 | if (!offscreenRTC) { |
| 337 | *errorMsg = "Failed to create offscreen render target."; |
| 338 | return DrawResult::kFail; |
| 339 | } |
| 340 | if (offscreenRTC->numSamples() <= 1 && |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 341 | !offscreenRTC->asRenderTargetProxy()->canUseMixedSamples(*ctx->priv().caps())) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 342 | *errorMsg = "MSAA and mixed samples only."; |
| 343 | return DrawResult::kSkip; |
| 344 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 345 | |
| 346 | static constexpr GrUserStencilSettings kStencilCover( |
| 347 | GrUserStencilSettings::StaticInit< |
| 348 | 0x0000, |
| 349 | GrUserStencilTest::kNotEqual, |
| 350 | 0xffff, |
| 351 | GrUserStencilOp::kZero, |
| 352 | GrUserStencilOp::kKeep, |
| 353 | 0xffff>() |
| 354 | ); |
| 355 | |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 356 | offscreenRTC->clear({0,1,0,1}); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 357 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 358 | // Stencil. |
| 359 | offscreenRTC->priv().testingOnly_addDrawOp( |
| 360 | SampleLocationsTestOp::Make(ctx, canvas->getTotalMatrix(), fGradType)); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 361 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 362 | // Cover. |
| 363 | GrPaint coverPaint; |
| 364 | coverPaint.setColor4f({1,0,0,1}); |
| 365 | coverPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrcOver)); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 366 | rtc->priv().stencilRect(nullptr, &kStencilCover, std::move(coverPaint), GrAA::kNo, |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 367 | SkMatrix::I(), SkRect::MakeWH(200, 200)); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 368 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 369 | // Copy offscreen texture to canvas. |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 370 | rtc->drawTexture(nullptr, offscreenRTC->readSurfaceView(), |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 371 | offscreenRTC->colorInfo().alphaType(), |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 372 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrc, SK_PMColor4fWHITE, |
| 373 | {0,0,200,200}, {0,0,200,200}, GrAA::kNo, GrQuadAAFlags::kNone, |
| 374 | SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(), |
| 375 | nullptr); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 376 | |
| 377 | return skiagm::DrawResult::kOk; |
| 378 | } |
| 379 | |
| 380 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kTopLeft_GrSurfaceOrigin); ) |
| 381 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kBottomLeft_GrSurfaceOrigin); ) |
| 382 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kTopLeft_GrSurfaceOrigin); ) |
| 383 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kBottomLeft_GrSurfaceOrigin); ) |
| 384 | |
| 385 | } |