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" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 18 | #include "include/gpu/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/gpu/GrTypes.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/private/GrTypesPriv.h" |
| 21 | #include "include/private/SkColorData.h" |
| 22 | #include "src/gpu/GrBuffer.h" |
| 23 | #include "src/gpu/GrCaps.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrColorSpaceXform.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 25 | #include "src/gpu/GrDirectContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrGeometryProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrMemoryPool.h" |
| 28 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrOpsRenderPass.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "src/gpu/GrPaint.h" |
| 31 | #include "src/gpu/GrPipeline.h" |
| 32 | #include "src/gpu/GrPrimitiveProcessor.h" |
| 33 | #include "src/gpu/GrProcessor.h" |
| 34 | #include "src/gpu/GrProcessorSet.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 36 | #include "src/gpu/GrSamplerState.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "src/gpu/GrShaderCaps.h" |
| 38 | #include "src/gpu/GrShaderVar.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 39 | #include "src/gpu/GrSurfaceDrawContext.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 40 | #include "src/gpu/GrSurfaceProxy.h" |
| 41 | #include "src/gpu/GrTextureProxy.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 42 | #include "src/gpu/GrUserStencilSettings.h" |
| 43 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 44 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 45 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 46 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 47 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 48 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 49 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 50 | #include "src/gpu/ops/GrDrawOp.h" |
| 51 | #include "src/gpu/ops/GrOp.h" |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 52 | #include "tools/gpu/ProxyUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 53 | |
| 54 | #include <memory> |
| 55 | #include <utility> |
| 56 | |
| 57 | class GrAppliedClip; |
| 58 | class GrGLSLProgramDataManager; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 59 | |
| 60 | namespace skiagm { |
| 61 | |
| 62 | enum class GradType : bool { |
| 63 | kHW, |
| 64 | kSW |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * This test ensures that the shaderBuilder's sample offsets and sample mask are correlated with |
| 69 | * actual HW sample locations. It does so by drawing pseudo-random subpixel boxes, and only turning |
| 70 | * off the samples whose locations fall inside the boxes. |
| 71 | */ |
| 72 | class SampleLocationsGM : public GpuGM { |
| 73 | public: |
| 74 | SampleLocationsGM(GradType gradType, GrSurfaceOrigin origin) |
| 75 | : fGradType(gradType) |
| 76 | , fOrigin(origin) {} |
| 77 | |
| 78 | private: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 79 | SkString onShortName() override { |
| 80 | return SkStringPrintf("samplelocations%s%s", |
| 81 | (GradType::kHW == fGradType) ? "_hwgrad" : "_swgrad", |
| 82 | (kTopLeft_GrSurfaceOrigin == fOrigin) ? "_topleft" : "_botleft"); |
| 83 | } |
| 84 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 85 | SkISize onISize() override { return SkISize::Make(200, 200); } |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 86 | DrawResult onDraw(GrRecordingContext*, GrSurfaceDrawContext*, |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 87 | SkCanvas*, SkString* errorMsg) override; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 88 | |
| 89 | const GradType fGradType; |
| 90 | const GrSurfaceOrigin fOrigin; |
| 91 | }; |
| 92 | |
| 93 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 94 | // SkSL code. |
| 95 | |
| 96 | class SampleLocationsTestProcessor : public GrGeometryProcessor { |
| 97 | public: |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 98 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, GradType gradType) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 99 | return arena->make([&](void* ptr) { |
| 100 | return new (ptr) SampleLocationsTestProcessor(gradType); |
| 101 | }); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 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: |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 113 | SampleLocationsTestProcessor(GradType gradType) |
| 114 | : GrGeometryProcessor(kSampleLocationsTestProcessor_ClassID) |
| 115 | , fGradType(gradType) { |
| 116 | this->setWillUseCustomFeature(CustomFeatures::kSampleLocations); |
| 117 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 118 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 119 | const GradType fGradType; |
| 120 | |
| 121 | class Impl; |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 122 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 123 | using INHERITED = GrGeometryProcessor; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | class SampleLocationsTestProcessor::Impl : public GrGLSLGeometryProcessor { |
| 127 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 128 | const auto& proc = args.fGP.cast<SampleLocationsTestProcessor>(); |
| 129 | auto* v = args.fVertBuilder; |
| 130 | auto* f = args.fFragBuilder; |
| 131 | |
| 132 | GrGLSLVarying coord(kFloat2_GrSLType); |
| 133 | GrGLSLVarying grad(kFloat2_GrSLType); |
| 134 | args.fVaryingHandler->addVarying("coord", &coord); |
| 135 | if (GradType::kSW == proc.fGradType) { |
| 136 | args.fVaryingHandler->addVarying("grad", &grad); |
| 137 | } |
| 138 | |
| 139 | // Pixel grid. |
| 140 | v->codeAppendf("int x = sk_InstanceID %% 200;"); |
| 141 | v->codeAppendf("int y = sk_InstanceID / 200;"); |
| 142 | |
| 143 | // Create pseudo-random rectangles inside a 16x16 subpixel grid. This works out nicely |
| 144 | // because there are 17 positions on the grid (including both edges), and 17 is a great |
| 145 | // prime number for generating pseudo-random numbers. |
| 146 | v->codeAppendf("int ileft = (sk_InstanceID*929) %% 17;"); |
| 147 | v->codeAppendf("int iright = ileft + 1 + ((sk_InstanceID*1637) %% (17 - ileft));"); |
| 148 | v->codeAppendf("int itop = (sk_InstanceID*313) %% 17;"); |
| 149 | v->codeAppendf("int ibot = itop + 1 + ((sk_InstanceID*1901) %% (17 - itop));"); |
| 150 | |
| 151 | // Outset (or inset) the rectangle, for the very likely scenario that samples fall on exact |
| 152 | // 16ths of a pixel. GL_SUBPIXEL_BITS is allowed to be as low as 4, so try not to let the |
| 153 | // outset value to get too small. |
| 154 | v->codeAppendf("float outset = 1/32.0;"); |
| 155 | v->codeAppendf("outset = (0 == (x + y) %% 2) ? -outset : +outset;"); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 156 | v->codeAppendf("float l = float(ileft)/16.0 - outset;"); |
| 157 | v->codeAppendf("float r = float(iright)/16.0 + outset;"); |
| 158 | v->codeAppendf("float t = float(itop)/16.0 - outset;"); |
| 159 | v->codeAppendf("float b = float(ibot)/16.0 + outset;"); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 160 | |
| 161 | v->codeAppendf("float2 vertexpos;"); |
| 162 | v->codeAppendf("vertexpos.x = float(x) + ((0 == (sk_VertexID %% 2)) ? l : r);"); |
| 163 | v->codeAppendf("vertexpos.y = float(y) + ((0 == (sk_VertexID / 2)) ? t : b);"); |
| 164 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
| 165 | |
| 166 | v->codeAppendf("%s.x = (0 == (sk_VertexID %% 2)) ? -1 : +1;", coord.vsOut()); |
| 167 | v->codeAppendf("%s.y = (0 == (sk_VertexID / 2)) ? -1 : +1;", coord.vsOut()); |
| 168 | if (GradType::kSW == proc.fGradType) { |
| 169 | v->codeAppendf("%s = 2/float2(r - l, b - t);", grad.vsOut()); |
| 170 | } |
| 171 | |
| 172 | // Fragment shader: Output RED. |
| 173 | f->codeAppendf("%s = half4(1,0,0,1);", args.fOutputColor); |
| 174 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 175 | |
| 176 | // Now turn off all the samples inside our sub-rectangle. As long as the shaderBuilder's |
| 177 | // sample offsets and sample mask are correlated with actual HW sample locations, no red |
| 178 | // will bleed through. |
| 179 | f->codeAppendf("for (int i = 0; i < %i; ++i) {", |
| 180 | f->getProgramBuilder()->effectiveSampleCnt()); |
| 181 | if (GradType::kHW == proc.fGradType) { |
| 182 | f->codeAppendf("float2x2 grad = float2x2(dFdx(%s), dFdy(%s));", |
| 183 | coord.fsIn(), coord.fsIn()); |
| 184 | } else { |
| 185 | f->codeAppendf("float2x2 grad = float2x2(%s.x, 0, 0, %s.y);", grad.fsIn(), grad.fsIn()); |
| 186 | } |
| 187 | f->codeAppendf( "float2 samplecoord = %s[i] * grad + %s;", |
| 188 | f->sampleOffsets(), coord.fsIn()); |
| 189 | f->codeAppendf( "if (all(lessThanEqual(abs(samplecoord), float2(1)))) {"); |
| 190 | f->maskOffMultisampleCoverage( |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 191 | "~(1 << i)", GrGLSLFPFragmentBuilder::ScopeFlags::kInsideLoop); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 192 | f->codeAppendf( "}"); |
| 193 | f->codeAppendf("}"); |
| 194 | } |
| 195 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 196 | void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override {} |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | GrGLSLPrimitiveProcessor* SampleLocationsTestProcessor::createGLSLInstance( |
| 200 | const GrShaderCaps&) const { |
| 201 | return new Impl(); |
| 202 | } |
| 203 | |
| 204 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 205 | // Draw Op. |
| 206 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 207 | static constexpr GrUserStencilSettings gStencilWrite( |
| 208 | GrUserStencilSettings::StaticInit< |
| 209 | 0x0001, |
| 210 | GrUserStencilTest::kAlways, |
| 211 | 0xffff, |
| 212 | GrUserStencilOp::kReplace, |
| 213 | GrUserStencilOp::kKeep, |
| 214 | 0xffff>() |
| 215 | ); |
| 216 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 217 | class SampleLocationsTestOp : public GrDrawOp { |
| 218 | public: |
| 219 | DEFINE_OP_CLASS_ID |
| 220 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 221 | static GrOp::Owner Make( |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 222 | GrRecordingContext* ctx, const SkMatrix& viewMatrix, GradType gradType) { |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 223 | return GrOp::Make<SampleLocationsTestOp>(ctx, gradType); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private: |
| 227 | SampleLocationsTestOp(GradType gradType) : GrDrawOp(ClassID()), fGradType(gradType) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 228 | this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | const char* name() const override { return "SampleLocationsTestOp"; } |
| 232 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 233 | return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil; |
| 234 | } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 235 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 236 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 237 | return GrProcessorSet::EmptySetAnalysis(); |
| 238 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 239 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 240 | |
| 241 | GrProgramInfo* createProgramInfo(const GrCaps* caps, |
| 242 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 243 | const GrSurfaceProxyView& writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 244 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 245 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 246 | GrXferBarrierFlags renderPassXferBarriers, |
| 247 | GrLoadOp colorLoadOp) const { |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 248 | GrGeometryProcessor* geomProc = SampleLocationsTestProcessor::Make(arena, fGradType); |
| 249 | |
| 250 | GrPipeline::InputFlags flags = GrPipeline::InputFlags::kHWAntialias; |
| 251 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 252 | return sk_gpu_test::CreateProgramInfo(caps, arena, writeView, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 253 | std::move(appliedClip), dstProxyView, |
| 254 | geomProc, SkBlendMode::kSrcOver, |
| 255 | GrPrimitiveType::kTriangleStrip, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 256 | renderPassXferBarriers, colorLoadOp, |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 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(), |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 265 | flushState->dstProxyView(), |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 266 | flushState->renderPassBarriers(), |
| 267 | flushState->colorLoadOp()); |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 270 | void onPrePrepare(GrRecordingContext* context, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 271 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 272 | GrAppliedClip* clip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 273 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 274 | GrXferBarrierFlags renderPassXferBarriers, |
| 275 | GrLoadOp colorLoadOp) final { |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 276 | // We're going to create the GrProgramInfo (and the GrPipeline and geometry processor |
| 277 | // it relies on) in the DDL-record-time arena. |
| 278 | SkArenaAlloc* arena = context->priv().recordTimeAllocator(); |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 279 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 280 | // This is equivalent to a GrOpFlushState::detachAppliedClip |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 281 | GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled(); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 282 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 283 | fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 284 | std::move(appliedClip), dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 285 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 286 | |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 287 | context->priv().recordProgramInfo(fProgramInfo); |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void onPrepare(GrOpFlushState*) final {} |
| 291 | |
| 292 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final { |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 293 | if (!fProgramInfo) { |
Robert Phillips | ac6156c | 2020-02-28 16:02:40 -0500 | [diff] [blame] | 294 | fProgramInfo = this->createProgramInfo(flushState); |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 297 | flushState->bindPipelineAndScissorClip(*fProgramInfo, SkRect::MakeIWH(200, 200)); |
| 298 | flushState->bindBuffers(nullptr, nullptr, nullptr); |
| 299 | flushState->drawInstanced(200*200, 0, 4, 0); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | const GradType fGradType; |
| 303 | |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 304 | // 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] | 305 | // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the |
| 306 | // arena's job to free up their memory so we just have a bare programInfo pointer here. We |
| 307 | // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are |
| 308 | // guaranteed to have the same lifetime as the program info. |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 309 | GrProgramInfo* fProgramInfo = nullptr; |
| 310 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 311 | friend class ::GrOp; // for ctor |
Robert Phillips | fbfc355 | 2019-11-18 11:50:54 -0500 | [diff] [blame] | 312 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 313 | using INHERITED = GrDrawOp; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 317 | // Test. |
| 318 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 319 | DrawResult SampleLocationsGM::onDraw(GrRecordingContext* ctx, GrSurfaceDrawContext* rtc, |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 320 | SkCanvas* canvas, SkString* errorMsg) { |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 321 | if (!ctx->priv().caps()->sampleLocationsSupport()) { |
| 322 | *errorMsg = "Requires support for sample locations."; |
| 323 | return DrawResult::kSkip; |
| 324 | } |
Chris Dalton | 8a64a44 | 2019-10-29 18:54:58 -0600 | [diff] [blame] | 325 | if (!ctx->priv().caps()->shaderCaps()->sampleMaskSupport()) { |
| 326 | *errorMsg = "Requires support for sample mask."; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 327 | return DrawResult::kSkip; |
| 328 | } |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 329 | if (!ctx->priv().caps()->drawInstancedSupport()) { |
| 330 | *errorMsg = "Requires support for instanced rendering."; |
| 331 | return DrawResult::kSkip; |
| 332 | } |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 333 | if (rtc->numSamples() <= 1 && !ctx->priv().caps()->mixedSamplesSupport()) { |
| 334 | *errorMsg = "MSAA and mixed samples only."; |
| 335 | return DrawResult::kSkip; |
| 336 | } |
| 337 | |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 338 | auto offscreenRTC = GrSurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 339 | ctx, rtc->colorInfo().colorType(), nullptr, SkBackingFit::kExact, {200, 200}, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 340 | rtc->numSamples(), GrMipmapped::kNo, GrProtected::kNo, fOrigin); |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 341 | if (!offscreenRTC) { |
| 342 | *errorMsg = "Failed to create offscreen render target."; |
| 343 | return DrawResult::kFail; |
| 344 | } |
| 345 | if (offscreenRTC->numSamples() <= 1 && |
Greg Daniel | 46e366a | 2019-12-16 14:38:36 -0500 | [diff] [blame] | 346 | !offscreenRTC->asRenderTargetProxy()->canUseMixedSamples(*ctx->priv().caps())) { |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 347 | *errorMsg = "MSAA and mixed samples only."; |
| 348 | return DrawResult::kSkip; |
| 349 | } |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 350 | |
| 351 | static constexpr GrUserStencilSettings kStencilCover( |
| 352 | GrUserStencilSettings::StaticInit< |
| 353 | 0x0000, |
| 354 | GrUserStencilTest::kNotEqual, |
| 355 | 0xffff, |
| 356 | GrUserStencilOp::kZero, |
| 357 | GrUserStencilOp::kKeep, |
| 358 | 0xffff>() |
| 359 | ); |
| 360 | |
Brian Salomon | 590f567 | 2020-12-16 11:44:47 -0500 | [diff] [blame] | 361 | offscreenRTC->clear(SkPMColor4f{0, 1, 0, 1}); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 362 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 363 | // Stencil. |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 364 | offscreenRTC->addDrawOp(SampleLocationsTestOp::Make(ctx, canvas->getTotalMatrix(), fGradType)); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 365 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 366 | // Cover. |
| 367 | GrPaint coverPaint; |
| 368 | coverPaint.setColor4f({1,0,0,1}); |
| 369 | coverPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrcOver)); |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 370 | rtc->stencilRect(nullptr, &kStencilCover, std::move(coverPaint), GrAA::kNo, SkMatrix::I(), |
| 371 | SkRect::MakeWH(200, 200)); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 372 | |
Chris Dalton | effee20 | 2019-07-01 22:28:03 -0600 | [diff] [blame] | 373 | // Copy offscreen texture to canvas. |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 374 | rtc->drawTexture(nullptr, |
| 375 | offscreenRTC->readSurfaceView(), |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 376 | offscreenRTC->colorInfo().alphaType(), |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 377 | GrSamplerState::Filter::kNearest, |
| 378 | GrSamplerState::MipmapMode::kNone, |
| 379 | SkBlendMode::kSrc, |
| 380 | SK_PMColor4fWHITE, |
| 381 | {0, 0, 200, 200}, |
| 382 | {0, 0, 200, 200}, |
| 383 | GrAA::kNo, |
| 384 | GrQuadAAFlags::kNone, |
| 385 | SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, |
| 386 | SkMatrix::I(), |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 387 | nullptr); |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 388 | |
| 389 | return skiagm::DrawResult::kOk; |
| 390 | } |
| 391 | |
| 392 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kTopLeft_GrSurfaceOrigin); ) |
| 393 | DEF_GM( return new SampleLocationsGM(GradType::kHW, kBottomLeft_GrSurfaceOrigin); ) |
| 394 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kTopLeft_GrSurfaceOrigin); ) |
| 395 | DEF_GM( return new SampleLocationsGM(GradType::kSW, kBottomLeft_GrSurfaceOrigin); ) |
| 396 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 397 | } // namespace skiagm |