Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 "src/gpu/ops/GrFillRRectOp.h" |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkRRectPriv.h" |
| 12 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrMemoryPool.h" |
| 14 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrOpsRenderPass.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
| 18 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 19 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 20 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 21 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 22 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 23 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 27 | class FillRRectOp : public GrMeshDrawOp { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 28 | private: |
| 29 | using Helper = GrSimpleMeshDrawOpHelper; |
| 30 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 31 | public: |
| 32 | DEFINE_OP_CLASS_ID |
| 33 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 34 | static GrOp::Owner Make(GrRecordingContext*, |
| 35 | GrPaint&&, |
| 36 | const SkMatrix& viewMatrix, |
| 37 | const SkRRect&, |
| 38 | GrAAType); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 39 | |
| 40 | const char* name() const final { return "GrFillRRectOp"; } |
| 41 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 42 | FixedFunctionFlags fixedFunctionFlags() const final { return fHelper.fixedFunctionFlags(); } |
| 43 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 44 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 45 | bool hasMixedSampledCoverage, GrClampType) final; |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 46 | CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) final; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 47 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 48 | void visitProxies(const VisitProxyFunc& fn) const override { |
| 49 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 50 | fProgramInfo->visitFPProxies(fn); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 51 | } else { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 52 | fHelper.visitProxies(fn); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 56 | void onPrepareDraws(Target*) final; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 57 | |
| 58 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) final; |
| 59 | |
| 60 | private: |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 61 | friend class ::GrSimpleMeshDrawOpHelper; // for access to ctor |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 62 | friend class ::GrOp; // for access to ctor |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 63 | |
| 64 | enum class ProcessorFlags { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 65 | kNone = 0, |
| 66 | kUseHWDerivatives = 1 << 0, |
| 67 | kHasPerspective = 1 << 1, |
| 68 | kHasLocalCoords = 1 << 2, |
| 69 | kWideColor = 1 << 3 |
| 70 | }; |
| 71 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 72 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(ProcessorFlags); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 73 | |
| 74 | class Processor; |
| 75 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 76 | FillRRectOp(GrProcessorSet*, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 77 | const SkPMColor4f& paintColor, |
| 78 | const SkMatrix& totalShapeMatrix, |
| 79 | const SkRRect&, |
| 80 | GrAAType, |
| 81 | ProcessorFlags, |
| 82 | const SkRect& devBounds); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 83 | |
| 84 | // These methods are used to append data of various POD types to our internal array of instance |
| 85 | // data. The actual layout of the instance buffer can vary from Op to Op. |
| 86 | template <typename T> inline T* appendInstanceData(int count) { |
| 87 | static_assert(std::is_pod<T>::value, ""); |
| 88 | static_assert(4 == alignof(T), ""); |
| 89 | return reinterpret_cast<T*>(fInstanceData.push_back_n(sizeof(T) * count)); |
| 90 | } |
| 91 | |
| 92 | template <typename T, typename... Args> |
| 93 | inline void writeInstanceData(const T& val, const Args&... remainder) { |
| 94 | memcpy(this->appendInstanceData<T>(1), &val, sizeof(T)); |
| 95 | this->writeInstanceData(remainder...); |
| 96 | } |
| 97 | |
| 98 | void writeInstanceData() {} // Halt condition. |
| 99 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 100 | GrProgramInfo* programInfo() final { return fProgramInfo; } |
| 101 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 102 | // Create a GrProgramInfo object in the provided arena |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 103 | void onCreateProgramInfo(const GrCaps*, |
| 104 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 105 | const GrSurfaceProxyView& writeView, |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 106 | GrAppliedClip&&, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 107 | const GrXferProcessor::DstProxyView&, |
| 108 | GrXferBarrierFlags renderPassXferBarriers) final; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 109 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 110 | Helper fHelper; |
| 111 | SkPMColor4f fColor; |
| 112 | const SkRect fLocalRect; |
| 113 | ProcessorFlags fProcessorFlags; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 114 | |
| 115 | SkSTArray<sizeof(float) * 16 * 4, char, /*MEM_MOVE=*/ true> fInstanceData; |
| 116 | int fInstanceCount = 1; |
| 117 | int fInstanceStride = 0; |
| 118 | |
| 119 | sk_sp<const GrBuffer> fInstanceBuffer; |
| 120 | sk_sp<const GrBuffer> fVertexBuffer; |
| 121 | sk_sp<const GrBuffer> fIndexBuffer; |
| 122 | int fBaseInstance = 0; |
| 123 | int fIndexCount = 0; |
| 124 | |
| 125 | // If this op is prePrepared the created programInfo will be stored here for use in |
| 126 | // onExecute. In the prePrepared case it will have been stored in the record-time arena. |
| 127 | GrProgramInfo* fProgramInfo = nullptr; |
| 128 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 129 | using INHERITED = GrMeshDrawOp; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 132 | GR_MAKE_BITFIELD_CLASS_OPS(FillRRectOp::ProcessorFlags) |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 133 | |
| 134 | // Hardware derivatives are not always accurate enough for highly elliptical corners. This method |
| 135 | // checks to make sure the corners will still all look good if we use HW derivatives. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 136 | static bool can_use_hw_derivatives_with_coverage(const GrShaderCaps&, |
| 137 | const SkMatrix&, |
| 138 | const SkRRect&); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 139 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 140 | GrOp::Owner FillRRectOp::Make(GrRecordingContext* ctx, |
| 141 | GrPaint&& paint, |
| 142 | const SkMatrix& viewMatrix, |
| 143 | const SkRRect& rrect, |
| 144 | GrAAType aaType) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 145 | using Helper = GrSimpleMeshDrawOpHelper; |
| 146 | |
| 147 | const GrCaps* caps = ctx->priv().caps(); |
| 148 | |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 149 | if (!caps->drawInstancedSupport()) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 150 | return nullptr; |
| 151 | } |
| 152 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 153 | ProcessorFlags flags = ProcessorFlags::kNone; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 154 | if (GrAAType::kCoverage == aaType) { |
| 155 | // TODO: Support perspective in a follow-on CL. This shouldn't be difficult, since we |
| 156 | // already use HW derivatives. The only trick will be adjusting the AA outset to account for |
| 157 | // perspective. (i.e., outset = 0.5 * z.) |
| 158 | if (viewMatrix.hasPerspective()) { |
| 159 | return nullptr; |
| 160 | } |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 161 | if (can_use_hw_derivatives_with_coverage(*caps->shaderCaps(), viewMatrix, rrect)) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 162 | // HW derivatives (more specifically, fwidth()) are consistently faster on all platforms |
| 163 | // in coverage mode. We use them as long as the approximation will be accurate enough. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 164 | flags |= ProcessorFlags::kUseHWDerivatives; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 165 | } |
| 166 | } else { |
| 167 | if (GrAAType::kMSAA == aaType) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 168 | if (!caps->sampleLocationsSupport() || !caps->shaderCaps()->sampleMaskSupport() || |
| 169 | caps->shaderCaps()->canOnlyUseSampleMaskWithStencil()) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 170 | return nullptr; |
| 171 | } |
| 172 | } |
| 173 | if (viewMatrix.hasPerspective()) { |
| 174 | // HW derivatives are consistently slower on all platforms in sample mask mode. We |
| 175 | // therefore only use them when there is perspective, since then we can't interpolate |
| 176 | // the symbolic screen-space gradient. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 177 | flags |= ProcessorFlags::kUseHWDerivatives | ProcessorFlags::kHasPerspective; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 178 | } |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Produce a matrix that draws the round rect from normalized [-1, -1, +1, +1] space. |
| 182 | float l = rrect.rect().left(), r = rrect.rect().right(), |
| 183 | t = rrect.rect().top(), b = rrect.rect().bottom(); |
| 184 | SkMatrix m; |
| 185 | // Unmap the normalized rect [-1, -1, +1, +1] back to [l, t, r, b]. |
| 186 | m.setScaleTranslate((r - l)/2, (b - t)/2, (l + r)/2, (t + b)/2); |
| 187 | // Map to device space. |
| 188 | m.postConcat(viewMatrix); |
| 189 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 190 | SkRect devBounds; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 191 | if (!(flags & ProcessorFlags::kHasPerspective)) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 192 | // Since m is an affine matrix that maps the rect [-1, -1, +1, +1] into the shape's |
| 193 | // device-space quad, it's quite simple to find the bounding rectangle: |
| 194 | devBounds = SkRect::MakeXYWH(m.getTranslateX(), m.getTranslateY(), 0, 0); |
| 195 | devBounds.outset(SkScalarAbs(m.getScaleX()) + SkScalarAbs(m.getSkewX()), |
| 196 | SkScalarAbs(m.getSkewY()) + SkScalarAbs(m.getScaleY())); |
| 197 | } else { |
| 198 | viewMatrix.mapRect(&devBounds, rrect.rect()); |
| 199 | } |
| 200 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 201 | if (GrAAType::kMSAA == aaType && caps->preferTrianglesOverSampleMask()) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 202 | // We are on a platform that prefers fine triangles instead of using the sample mask. See if |
| 203 | // the round rect is large enough that it will be faster for us to send it off to the |
| 204 | // default path renderer instead. The 200x200 threshold was arrived at using the |
| 205 | // "shapes_rrect" benchmark on an ARM Galaxy S9. |
| 206 | if (devBounds.height() * devBounds.width() > 200 * 200) { |
| 207 | return nullptr; |
| 208 | } |
| 209 | } |
| 210 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 211 | return Helper::FactoryHelper<FillRRectOp>(ctx, std::move(paint), m, rrect, aaType, |
| 212 | flags, devBounds); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 213 | } |
| 214 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 215 | FillRRectOp::FillRRectOp(GrProcessorSet* processorSet, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 216 | const SkPMColor4f& paintColor, |
| 217 | const SkMatrix& totalShapeMatrix, |
| 218 | const SkRRect& rrect, |
| 219 | GrAAType aaType, |
| 220 | ProcessorFlags processorFlags, |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 221 | const SkRect& devBounds) |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 222 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 223 | , fHelper(processorSet, aaType) |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 224 | , fColor(paintColor) |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 225 | , fLocalRect(rrect.rect()) |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 226 | , fProcessorFlags(processorFlags & ~(ProcessorFlags::kHasLocalCoords | |
| 227 | ProcessorFlags::kWideColor)) { |
| 228 | SkASSERT((fProcessorFlags & ProcessorFlags::kHasPerspective) == |
| 229 | totalShapeMatrix.hasPerspective()); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 230 | this->setBounds(devBounds, GrOp::HasAABloat::kYes, GrOp::IsHairline::kNo); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 231 | |
| 232 | // Write the matrix attribs. |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 233 | const SkMatrix& m = totalShapeMatrix; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 234 | if (!(fProcessorFlags & ProcessorFlags::kHasPerspective)) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 235 | // Affine 2D transformation (float2x2 plus float2 translate). |
| 236 | SkASSERT(!m.hasPerspective()); |
| 237 | this->writeInstanceData(m.getScaleX(), m.getSkewX(), m.getSkewY(), m.getScaleY()); |
| 238 | this->writeInstanceData(m.getTranslateX(), m.getTranslateY()); |
| 239 | } else { |
| 240 | // Perspective float3x3 transformation matrix. |
| 241 | SkASSERT(m.hasPerspective()); |
| 242 | m.get9(this->appendInstanceData<float>(9)); |
| 243 | } |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 244 | |
| 245 | // Convert the radii to [-1, -1, +1, +1] space and write their attribs. |
| 246 | Sk4f radiiX, radiiY; |
| 247 | Sk4f::Load2(SkRRectPriv::GetRadiiArray(rrect), &radiiX, &radiiY); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 248 | (radiiX * (2/rrect.width())).store(this->appendInstanceData<float>(4)); |
| 249 | (radiiY * (2/rrect.height())).store(this->appendInstanceData<float>(4)); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 250 | |
| 251 | // We will write the color and local rect attribs during finalize(). |
| 252 | } |
| 253 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 254 | GrProcessorSet::Analysis FillRRectOp::finalize( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 255 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 256 | GrClampType clampType) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 257 | SkASSERT(1 == fInstanceCount); |
| 258 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 259 | bool isWideColor; |
| 260 | auto analysis = fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType, |
| 261 | GrProcessorAnalysisCoverage::kSingleChannel, |
| 262 | &fColor, &isWideColor); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 263 | |
| 264 | // Finish writing the instance attribs. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 265 | if (isWideColor) { |
| 266 | fProcessorFlags |= ProcessorFlags::kWideColor; |
| 267 | this->writeInstanceData(fColor); |
Brian Osman | 5105d68 | 2019-02-13 16:06:14 -0500 | [diff] [blame] | 268 | } else { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 269 | this->writeInstanceData(fColor.toBytes_RGBA()); |
Brian Osman | 5105d68 | 2019-02-13 16:06:14 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 272 | if (analysis.usesLocalCoords()) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 273 | fProcessorFlags |= ProcessorFlags::kHasLocalCoords; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 274 | this->writeInstanceData(fLocalRect); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 275 | } |
| 276 | fInstanceStride = fInstanceData.count(); |
| 277 | |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 278 | return analysis; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 281 | GrOp::CombineResult FillRRectOp::onCombineIfPossible(GrOp* op, SkArenaAlloc*, const GrCaps& caps) { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 282 | const auto& that = *op->cast<FillRRectOp>(); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 283 | if (!fHelper.isCompatible(that.fHelper, caps, this->bounds(), that.bounds())) { |
| 284 | return CombineResult::kCannotCombine; |
| 285 | } |
| 286 | |
| 287 | if (fProcessorFlags != that.fProcessorFlags || |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 288 | fInstanceData.count() > std::numeric_limits<int>::max() - that.fInstanceData.count()) { |
| 289 | return CombineResult::kCannotCombine; |
| 290 | } |
| 291 | |
| 292 | fInstanceData.push_back_n(that.fInstanceData.count(), that.fInstanceData.begin()); |
| 293 | fInstanceCount += that.fInstanceCount; |
| 294 | SkASSERT(fInstanceStride == that.fInstanceStride); |
| 295 | return CombineResult::kMerged; |
| 296 | } |
| 297 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 298 | class FillRRectOp::Processor : public GrGeometryProcessor { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 299 | public: |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 300 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, GrAAType aaType, ProcessorFlags flags) { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 301 | return arena->make<Processor>(aaType, flags); |
| 302 | } |
| 303 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 304 | const char* name() const final { return "GrFillRRectOp::Processor"; } |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 305 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 306 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const final { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 307 | b->add32(((uint32_t)fFlags << 16) | (uint32_t)fAAType); |
| 308 | } |
| 309 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 310 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 311 | |
| 312 | private: |
| 313 | friend class ::SkArenaAlloc; // for access to ctor |
| 314 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 315 | Processor(GrAAType aaType, ProcessorFlags flags) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 316 | : INHERITED(kGrFillRRectOp_Processor_ClassID) |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 317 | , fAAType(aaType) |
| 318 | , fFlags(flags) { |
| 319 | int numVertexAttribs = (GrAAType::kCoverage == fAAType) ? 3 : 2; |
| 320 | this->setVertexAttributes(kVertexAttribs, numVertexAttribs); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 321 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 322 | if (!(fFlags & ProcessorFlags::kHasPerspective)) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 323 | // Affine 2D transformation (float2x2 plus float2 translate). |
| 324 | fInstanceAttribs.emplace_back("skew", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 325 | fInstanceAttribs.emplace_back( |
| 326 | "translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType); |
| 327 | } else { |
| 328 | // Perspective float3x3 transformation matrix. |
| 329 | fInstanceAttribs.emplace_back("persp_x", kFloat3_GrVertexAttribType, kFloat3_GrSLType); |
| 330 | fInstanceAttribs.emplace_back("persp_y", kFloat3_GrVertexAttribType, kFloat3_GrSLType); |
| 331 | fInstanceAttribs.emplace_back("persp_z", kFloat3_GrVertexAttribType, kFloat3_GrSLType); |
| 332 | } |
| 333 | fInstanceAttribs.emplace_back("radii_x", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 334 | fInstanceAttribs.emplace_back("radii_y", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 335 | fColorAttrib = &fInstanceAttribs.push_back( |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 336 | MakeColorAttribute("color", (fFlags & ProcessorFlags::kWideColor))); |
| 337 | if (fFlags & ProcessorFlags::kHasLocalCoords) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 338 | fInstanceAttribs.emplace_back( |
| 339 | "local_rect", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 340 | } |
| 341 | this->setInstanceAttributes(fInstanceAttribs.begin(), fInstanceAttribs.count()); |
| 342 | |
| 343 | if (GrAAType::kMSAA == fAAType) { |
| 344 | this->setWillUseCustomFeature(CustomFeatures::kSampleLocations); |
| 345 | } |
| 346 | } |
| 347 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 348 | static constexpr Attribute kVertexAttribs[] = { |
| 349 | {"radii_selector", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 350 | {"corner_and_radius_outsets", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 351 | // Coverage only. |
| 352 | {"aa_bloat_and_coverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType}}; |
| 353 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 354 | const GrAAType fAAType; |
| 355 | const ProcessorFlags fFlags; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 356 | |
| 357 | SkSTArray<6, Attribute> fInstanceAttribs; |
| 358 | const Attribute* fColorAttrib; |
| 359 | |
| 360 | class CoverageImpl; |
| 361 | class MSAAImpl; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 362 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 363 | using INHERITED = GrGeometryProcessor; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 364 | }; |
| 365 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 366 | constexpr GrPrimitiveProcessor::Attribute FillRRectOp::Processor::kVertexAttribs[]; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 367 | |
| 368 | // Our coverage geometry consists of an inset octagon with solid coverage, surrounded by linear |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 369 | // coverage ramps on the horizontal and vertical edges, and "arc coverage" pieces on the diagonal |
| 370 | // edges. The Vertex struct tells the shader where to place its vertex within a normalized |
| 371 | // ([l, t, r, b] = [-1, -1, +1, +1]) space, and how to calculate coverage. See onEmitCode. |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 372 | struct CoverageVertex { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 373 | std::array<float, 4> fRadiiSelector; |
| 374 | std::array<float, 2> fCorner; |
| 375 | std::array<float, 2> fRadiusOutset; |
| 376 | std::array<float, 2> fAABloatDirection; |
| 377 | float fCoverage; |
| 378 | float fIsLinearCoverage; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | // This is the offset (when multiplied by radii) from the corners of a bounding box to the vertices |
| 382 | // of its inscribed octagon. We draw the outside portion of arcs with quarter-octagons rather than |
| 383 | // rectangles. |
| 384 | static constexpr float kOctoOffset = 1/(1 + SK_ScalarRoot2Over2); |
| 385 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 386 | static constexpr CoverageVertex kCoverageVertexData[] = { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 387 | // Left inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 388 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{+1,0}}, 1, 1}, |
| 389 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{+1,0}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 390 | |
| 391 | // Top inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 392 | {{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,+1}}, 1, 1}, |
| 393 | {{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,+1}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 394 | |
| 395 | // Right inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 396 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{-1,0}}, 1, 1}, |
| 397 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{-1,0}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 398 | |
| 399 | // Bottom inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 400 | {{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,-1}}, 1, 1}, |
| 401 | {{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,-1}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 402 | |
| 403 | |
| 404 | // Left outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 405 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{-1,0}}, 0, 1}, |
| 406 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{-1,0}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 407 | |
| 408 | // Top outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 409 | {{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,-1}}, 0, 1}, |
| 410 | {{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,-1}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 411 | |
| 412 | // Right outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 413 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{+1,0}}, 0, 1}, |
| 414 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{+1,0}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 415 | |
| 416 | // Bottom outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 417 | {{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,+1}}, 0, 1}, |
| 418 | {{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,+1}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 419 | |
| 420 | |
| 421 | // Top-left corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 422 | {{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{-1, 0}}, 0, 0}, |
| 423 | {{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{+1, 0}}, 1, 0}, |
| 424 | {{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,+1}}, 1, 0}, |
| 425 | {{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,-1}}, 0, 0}, |
| 426 | {{{1,0,0,0}}, {{-1,-1}}, {{+kOctoOffset,0}}, {{-1,-1}}, 0, 0}, |
| 427 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+kOctoOffset}}, {{-1,-1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 428 | |
| 429 | // Top-right corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 430 | {{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,-1}}, 0, 0}, |
| 431 | {{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,+1}}, 1, 0}, |
| 432 | {{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{-1, 0}}, 1, 0}, |
| 433 | {{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{+1, 0}}, 0, 0}, |
| 434 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+kOctoOffset}}, {{+1,-1}}, 0, 0}, |
| 435 | {{{0,1,0,0}}, {{+1,-1}}, {{-kOctoOffset,0}}, {{+1,-1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 436 | |
| 437 | // Bottom-right corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 438 | {{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{+1, 0}}, 0, 0}, |
| 439 | {{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{-1, 0}}, 1, 0}, |
| 440 | {{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,-1}}, 1, 0}, |
| 441 | {{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,+1}}, 0, 0}, |
| 442 | {{{0,0,1,0}}, {{+1,+1}}, {{-kOctoOffset,0}}, {{+1,+1}}, 0, 0}, |
| 443 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-kOctoOffset}}, {{+1,+1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 444 | |
| 445 | // Bottom-left corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 446 | {{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,+1}}, 0, 0}, |
| 447 | {{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,-1}}, 1, 0}, |
| 448 | {{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{+1, 0}}, 1, 0}, |
| 449 | {{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{-1, 0}}, 0, 0}, |
Chris Dalton | 2d07e86 | 2018-11-26 12:30:47 -0700 | [diff] [blame] | 450 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-kOctoOffset}}, {{-1,+1}}, 0, 0}, |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 451 | {{{0,0,0,1}}, {{-1,+1}}, {{+kOctoOffset,0}}, {{-1,+1}}, 0, 0}}; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 452 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 453 | GR_DECLARE_STATIC_UNIQUE_KEY(gCoverageVertexBufferKey); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 454 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 455 | static constexpr uint16_t kCoverageIndexData[] = { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 456 | // Inset octagon (solid coverage). |
| 457 | 0, 1, 7, |
| 458 | 1, 2, 7, |
| 459 | 7, 2, 6, |
| 460 | 2, 3, 6, |
| 461 | 6, 3, 5, |
| 462 | 3, 4, 5, |
| 463 | |
| 464 | // AA borders (linear coverage). |
| 465 | 0, 1, 8, 1, 9, 8, |
| 466 | 2, 3, 10, 3, 11, 10, |
| 467 | 4, 5, 12, 5, 13, 12, |
| 468 | 6, 7, 14, 7, 15, 14, |
| 469 | |
| 470 | // Top-left arc. |
| 471 | 16, 17, 21, |
| 472 | 17, 21, 18, |
| 473 | 21, 18, 20, |
| 474 | 18, 20, 19, |
| 475 | |
| 476 | // Top-right arc. |
| 477 | 22, 23, 27, |
| 478 | 23, 27, 24, |
| 479 | 27, 24, 26, |
| 480 | 24, 26, 25, |
| 481 | |
| 482 | // Bottom-right arc. |
| 483 | 28, 29, 33, |
| 484 | 29, 33, 30, |
| 485 | 33, 30, 32, |
| 486 | 30, 32, 31, |
| 487 | |
| 488 | // Bottom-left arc. |
| 489 | 34, 35, 39, |
| 490 | 35, 39, 36, |
| 491 | 39, 36, 38, |
| 492 | 36, 38, 37}; |
| 493 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 494 | GR_DECLARE_STATIC_UNIQUE_KEY(gCoverageIndexBufferKey); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 495 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 496 | |
| 497 | // Our MSAA geometry consists of an inset octagon with full sample mask coverage, circumscribed |
| 498 | // by a larger octagon that modifies the sample mask for the arc at each corresponding corner. |
| 499 | struct MSAAVertex { |
| 500 | std::array<float, 4> fRadiiSelector; |
| 501 | std::array<float, 2> fCorner; |
| 502 | std::array<float, 2> fRadiusOutset; |
| 503 | }; |
| 504 | |
| 505 | static constexpr MSAAVertex kMSAAVertexData[] = { |
| 506 | // Left edge. (Negative radii selector indicates this is not an arc section.) |
| 507 | {{{0,0,0,-1}}, {{-1,+1}}, {{0,-1}}}, |
| 508 | {{{-1,0,0,0}}, {{-1,-1}}, {{0,+1}}}, |
| 509 | |
| 510 | // Top edge. |
| 511 | {{{-1,0,0,0}}, {{-1,-1}}, {{+1,0}}}, |
| 512 | {{{0,-1,0,0}}, {{+1,-1}}, {{-1,0}}}, |
| 513 | |
| 514 | // Right edge. |
| 515 | {{{0,-1,0,0}}, {{+1,-1}}, {{0,+1}}}, |
| 516 | {{{0,0,-1,0}}, {{+1,+1}}, {{0,-1}}}, |
| 517 | |
| 518 | // Bottom edge. |
| 519 | {{{0,0,-1,0}}, {{+1,+1}}, {{-1,0}}}, |
| 520 | {{{0,0,0,-1}}, {{-1,+1}}, {{+1,0}}}, |
| 521 | |
| 522 | // Top-left corner. |
| 523 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}}, |
| 524 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+kOctoOffset}}}, |
| 525 | {{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}}, |
| 526 | {{{1,0,0,0}}, {{-1,-1}}, {{+kOctoOffset,0}}}, |
| 527 | |
| 528 | // Top-right corner. |
| 529 | {{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}}, |
| 530 | {{{0,1,0,0}}, {{+1,-1}}, {{-kOctoOffset,0}}}, |
| 531 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}}, |
| 532 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+kOctoOffset}}}, |
| 533 | |
| 534 | // Bottom-right corner. |
| 535 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}}, |
| 536 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-kOctoOffset}}}, |
| 537 | {{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}}, |
| 538 | {{{0,0,1,0}}, {{+1,+1}}, {{-kOctoOffset,0}}}, |
| 539 | |
| 540 | // Bottom-left corner. |
| 541 | {{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}}, |
| 542 | {{{0,0,0,1}}, {{-1,+1}}, {{+kOctoOffset,0}}}, |
| 543 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}}, |
| 544 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-kOctoOffset}}}}; |
| 545 | |
| 546 | GR_DECLARE_STATIC_UNIQUE_KEY(gMSAAVertexBufferKey); |
| 547 | |
| 548 | static constexpr uint16_t kMSAAIndexData[] = { |
| 549 | // Inset octagon. (Full sample mask.) |
| 550 | 0, 1, 2, |
| 551 | 0, 2, 3, |
| 552 | 0, 3, 6, |
| 553 | 3, 4, 5, |
| 554 | 3, 5, 6, |
| 555 | 6, 7, 0, |
| 556 | |
| 557 | // Top-left arc. (Sample mask is set to the arc.) |
| 558 | 8, 9, 10, |
| 559 | 9, 11, 10, |
| 560 | |
| 561 | // Top-right arc. |
| 562 | 12, 13, 14, |
| 563 | 13, 15, 14, |
| 564 | |
| 565 | // Bottom-right arc. |
| 566 | 16, 17, 18, |
| 567 | 17, 19, 18, |
| 568 | |
| 569 | // Bottom-left arc. |
| 570 | 20, 21, 22, |
| 571 | 21, 23, 22}; |
| 572 | |
| 573 | GR_DECLARE_STATIC_UNIQUE_KEY(gMSAAIndexBufferKey); |
| 574 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 575 | void FillRRectOp::onPrepareDraws(Target* target) { |
| 576 | if (void* instanceData = target->makeVertexSpace(fInstanceStride, fInstanceCount, |
| 577 | &fInstanceBuffer, &fBaseInstance)) { |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 578 | SkASSERT(fInstanceStride * fInstanceCount == fInstanceData.count()); |
| 579 | memcpy(instanceData, fInstanceData.begin(), fInstanceData.count()); |
| 580 | } |
| 581 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 582 | if (GrAAType::kCoverage == fHelper.aaType()) { |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 583 | GR_DEFINE_STATIC_UNIQUE_KEY(gCoverageIndexBufferKey); |
| 584 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 585 | fIndexBuffer = target->resourceProvider()->findOrMakeStaticBuffer( |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 586 | GrGpuBufferType::kIndex, sizeof(kCoverageIndexData), kCoverageIndexData, |
| 587 | gCoverageIndexBufferKey); |
| 588 | |
| 589 | GR_DEFINE_STATIC_UNIQUE_KEY(gCoverageVertexBufferKey); |
| 590 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 591 | fVertexBuffer = target->resourceProvider()->findOrMakeStaticBuffer( |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 592 | GrGpuBufferType::kVertex, sizeof(kCoverageVertexData), kCoverageVertexData, |
| 593 | gCoverageVertexBufferKey); |
| 594 | |
| 595 | fIndexCount = SK_ARRAY_COUNT(kCoverageIndexData); |
| 596 | } else { |
| 597 | GR_DEFINE_STATIC_UNIQUE_KEY(gMSAAIndexBufferKey); |
| 598 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 599 | fIndexBuffer = target->resourceProvider()->findOrMakeStaticBuffer( |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 600 | GrGpuBufferType::kIndex, sizeof(kMSAAIndexData), kMSAAIndexData, |
| 601 | gMSAAIndexBufferKey); |
| 602 | |
| 603 | GR_DEFINE_STATIC_UNIQUE_KEY(gMSAAVertexBufferKey); |
| 604 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 605 | fVertexBuffer = target->resourceProvider()->findOrMakeStaticBuffer( |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 606 | GrGpuBufferType::kVertex, sizeof(kMSAAVertexData), kMSAAVertexData, |
| 607 | gMSAAVertexBufferKey); |
| 608 | |
| 609 | fIndexCount = SK_ARRAY_COUNT(kMSAAIndexData); |
| 610 | } |
| 611 | } |
| 612 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 613 | class FillRRectOp::Processor::CoverageImpl : public GrGLSLGeometryProcessor { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 614 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 615 | const auto& proc = args.fGP.cast<Processor>(); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 616 | bool useHWDerivatives = (proc.fFlags & ProcessorFlags::kUseHWDerivatives); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 617 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 618 | SkASSERT(proc.vertexStride() == sizeof(CoverageVertex)); |
| 619 | |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 620 | GrGLSLVaryingHandler* varyings = args.fVaryingHandler; |
| 621 | varyings->emitAttributes(proc); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 622 | varyings->addPassThroughAttribute(*proc.fColorAttrib, args.fOutputColor, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 623 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 624 | |
| 625 | // Emit the vertex shader. |
| 626 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 627 | |
| 628 | // Unpack vertex attribs. |
| 629 | v->codeAppend("float2 corner = corner_and_radius_outsets.xy;"); |
| 630 | v->codeAppend("float2 radius_outset = corner_and_radius_outsets.zw;"); |
| 631 | v->codeAppend("float2 aa_bloat_direction = aa_bloat_and_coverage.xy;"); |
| 632 | v->codeAppend("float coverage = aa_bloat_and_coverage.z;"); |
| 633 | v->codeAppend("float is_linear_coverage = aa_bloat_and_coverage.w;"); |
| 634 | |
| 635 | // Find the amount to bloat each edge for AA (in source space). |
| 636 | v->codeAppend("float2 pixellength = inversesqrt(" |
| 637 | "float2(dot(skew.xz, skew.xz), dot(skew.yw, skew.yw)));"); |
| 638 | v->codeAppend("float4 normalized_axis_dirs = skew * pixellength.xyxy;"); |
| 639 | v->codeAppend("float2 axiswidths = (abs(normalized_axis_dirs.xy) + " |
| 640 | "abs(normalized_axis_dirs.zw));"); |
| 641 | v->codeAppend("float2 aa_bloatradius = axiswidths * pixellength * .5;"); |
| 642 | |
| 643 | // Identify our radii. |
Mike Reed | d3efa99 | 2018-11-28 13:13:15 +0000 | [diff] [blame] | 644 | v->codeAppend("float4 radii_and_neighbors = radii_selector" |
| 645 | "* float4x4(radii_x, radii_y, radii_x.yxwz, radii_y.wzyx);"); |
| 646 | v->codeAppend("float2 radii = radii_and_neighbors.xy;"); |
| 647 | v->codeAppend("float2 neighbor_radii = radii_and_neighbors.zw;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 648 | |
| 649 | v->codeAppend("if (any(greaterThan(aa_bloatradius, float2(1)))) {"); |
| 650 | // The rrect is more narrow than an AA coverage ramp. We can't draw as-is |
| 651 | // or else opposite AA borders will overlap. Instead, fudge the size up to |
| 652 | // the width of a coverage ramp, and then reduce total coverage to make |
| 653 | // the rect appear more thin. |
| 654 | v->codeAppend( "corner = max(abs(corner), aa_bloatradius) * sign(corner);"); |
| 655 | v->codeAppend( "coverage /= max(aa_bloatradius.x, 1) * max(aa_bloatradius.y, 1);"); |
| 656 | // Set radii to zero to ensure we take the "linear coverage" codepath. |
| 657 | // (The "coverage" variable only has effect in the linear codepath.) |
| 658 | v->codeAppend( "radii = float2(0);"); |
| 659 | v->codeAppend("}"); |
| 660 | |
| 661 | v->codeAppend("if (any(lessThan(radii, aa_bloatradius * 1.25))) {"); |
| 662 | // The radii are very small. Demote this arc to a sharp 90 degree corner. |
| 663 | v->codeAppend( "radii = aa_bloatradius;"); |
| 664 | // Snap octagon vertices to the corner of the bounding box. |
| 665 | v->codeAppend( "radius_outset = floor(abs(radius_outset)) * radius_outset;"); |
| 666 | v->codeAppend( "is_linear_coverage = 1;"); |
| 667 | v->codeAppend("} else {"); |
Mike Reed | d3efa99 | 2018-11-28 13:13:15 +0000 | [diff] [blame] | 668 | // Don't let radii get smaller than a pixel. |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 669 | v->codeAppend( "radii = clamp(radii, pixellength, 2 - pixellength);"); |
Mike Reed | d3efa99 | 2018-11-28 13:13:15 +0000 | [diff] [blame] | 670 | v->codeAppend( "neighbor_radii = clamp(neighbor_radii, pixellength, 2 - pixellength);"); |
| 671 | // Don't let neighboring radii get closer together than 1/16 pixel. |
| 672 | v->codeAppend( "float2 spacing = 2 - radii - neighbor_radii;"); |
| 673 | v->codeAppend( "float2 extra_pad = max(pixellength * .0625 - spacing, float2(0));"); |
| 674 | v->codeAppend( "radii -= extra_pad * .5;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 675 | v->codeAppend("}"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 676 | |
| 677 | // Find our vertex position, adjusted for radii and bloated for AA. Our rect is drawn in |
| 678 | // normalized [-1,-1,+1,+1] space. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 679 | v->codeAppend("float2 aa_outset = aa_bloat_direction.xy * aa_bloatradius;"); |
| 680 | v->codeAppend("float2 vertexpos = corner + radius_outset * radii + aa_outset;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 681 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 682 | // Write positions |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 683 | GrShaderVar localCoord("", kFloat2_GrSLType); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 684 | if (proc.fFlags & ProcessorFlags::kHasLocalCoords) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 685 | v->codeAppend("float2 localcoord = (local_rect.xy * (1 - vertexpos) + " |
| 686 | "local_rect.zw * (1 + vertexpos)) * .5;"); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 687 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 688 | } |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 689 | |
| 690 | // Transform to device space. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 691 | SkASSERT(!(proc.fFlags & ProcessorFlags::kHasPerspective)); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 692 | v->codeAppend("float2x2 skewmatrix = float2x2(skew.xy, skew.zw);"); |
| 693 | v->codeAppend("float2 devcoord = vertexpos * skewmatrix + translate;"); |
| 694 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord"); |
| 695 | |
| 696 | // Setup interpolants for coverage. |
| 697 | GrGLSLVarying arcCoord(useHWDerivatives ? kFloat2_GrSLType : kFloat4_GrSLType); |
| 698 | varyings->addVarying("arccoord", &arcCoord); |
| 699 | v->codeAppend("if (0 != is_linear_coverage) {"); |
| 700 | // We are a non-corner piece: Set x=0 to indicate built-in coverage, and |
| 701 | // interpolate linear coverage across y. |
| 702 | v->codeAppendf( "%s.xy = float2(0, coverage);", arcCoord.vsOut()); |
| 703 | v->codeAppend("} else {"); |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 704 | // Find the normalized arc coordinates for our corner ellipse. |
| 705 | // (i.e., the coordinate system where x^2 + y^2 == 1). |
| 706 | v->codeAppend( "float2 arccoord = 1 - abs(radius_outset) + aa_outset/radii * corner;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 707 | // We are a corner piece: Interpolate the arc coordinates for coverage. |
| 708 | // Emit x+1 to ensure no pixel in the arc has a x value of 0 (since x=0 |
| 709 | // instructs the fragment shader to use linear coverage). |
| 710 | v->codeAppendf( "%s.xy = float2(arccoord.x+1, arccoord.y);", arcCoord.vsOut()); |
| 711 | if (!useHWDerivatives) { |
| 712 | // The gradient is order-1: Interpolate it across arccoord.zw. |
| 713 | v->codeAppendf("float2x2 derivatives = inverse(skewmatrix);"); |
| 714 | v->codeAppendf("%s.zw = derivatives * (arccoord/radii * 2);", arcCoord.vsOut()); |
| 715 | } |
| 716 | v->codeAppend("}"); |
| 717 | |
| 718 | // Emit the fragment shader. |
| 719 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 720 | |
| 721 | f->codeAppendf("float x_plus_1=%s.x, y=%s.y;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 722 | f->codeAppendf("half coverage;"); |
| 723 | f->codeAppendf("if (0 == x_plus_1) {"); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 724 | f->codeAppendf( "coverage = half(y);"); // We are a non-arc pixel (linear coverage). |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 725 | f->codeAppendf("} else {"); |
| 726 | f->codeAppendf( "float fn = x_plus_1 * (x_plus_1 - 2);"); // fn = (x+1)*(x-1) = x^2-1 |
| 727 | f->codeAppendf( "fn = fma(y,y, fn);"); // fn = x^2 + y^2 - 1 |
| 728 | if (useHWDerivatives) { |
| 729 | f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 730 | } else { |
| 731 | // The gradient is interpolated across arccoord.zw. |
| 732 | f->codeAppendf("float gx=%s.z, gy=%s.w;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 733 | f->codeAppendf("float fnwidth = abs(gx) + abs(gy);"); |
| 734 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 735 | f->codeAppendf( "half d = half(fn/fnwidth);"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 736 | f->codeAppendf( "coverage = clamp(.5 - d, 0, 1);"); |
| 737 | f->codeAppendf("}"); |
| 738 | f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); |
| 739 | } |
| 740 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 741 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&) override {} |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 742 | }; |
| 743 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 744 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 745 | class FillRRectOp::Processor::MSAAImpl : public GrGLSLGeometryProcessor { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 746 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 747 | const auto& proc = args.fGP.cast<Processor>(); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 748 | bool useHWDerivatives = (proc.fFlags & ProcessorFlags::kUseHWDerivatives); |
| 749 | bool hasPerspective = (proc.fFlags & ProcessorFlags::kHasPerspective); |
| 750 | bool hasLocalCoords = (proc.fFlags & ProcessorFlags::kHasLocalCoords); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 751 | SkASSERT(useHWDerivatives == hasPerspective); |
| 752 | |
| 753 | SkASSERT(proc.vertexStride() == sizeof(MSAAVertex)); |
| 754 | |
| 755 | // Emit the vertex shader. |
| 756 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 757 | |
| 758 | GrGLSLVaryingHandler* varyings = args.fVaryingHandler; |
| 759 | varyings->emitAttributes(proc); |
| 760 | varyings->addPassThroughAttribute(*proc.fColorAttrib, args.fOutputColor, |
| 761 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 762 | |
| 763 | // Unpack vertex attribs. |
| 764 | v->codeAppendf("float2 corner = corner_and_radius_outsets.xy;"); |
| 765 | v->codeAppendf("float2 radius_outset = corner_and_radius_outsets.zw;"); |
| 766 | |
| 767 | // Identify our radii. |
| 768 | v->codeAppend("float2 radii;"); |
| 769 | v->codeAppend("radii.x = dot(radii_selector, radii_x);"); |
| 770 | v->codeAppend("radii.y = dot(radii_selector, radii_y);"); |
| 771 | v->codeAppendf("bool is_arc_section = (radii.x > 0);"); |
| 772 | v->codeAppendf("radii = abs(radii);"); |
| 773 | |
| 774 | // Find our vertex position, adjusted for radii. Our rect is drawn in normalized |
| 775 | // [-1,-1,+1,+1] space. |
| 776 | v->codeAppend("float2 vertexpos = corner + radius_outset * radii;"); |
| 777 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 778 | // Write positions |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 779 | GrShaderVar localCoord("", kFloat2_GrSLType); |
| 780 | if (hasLocalCoords) { |
| 781 | v->codeAppend("float2 localcoord = (local_rect.xy * (1 - vertexpos) + " |
| 782 | "local_rect.zw * (1 + vertexpos)) * .5;"); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 783 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 784 | } |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 785 | |
| 786 | // Transform to device space. |
| 787 | if (!hasPerspective) { |
| 788 | v->codeAppend("float2x2 skewmatrix = float2x2(skew.xy, skew.zw);"); |
| 789 | v->codeAppend("float2 devcoord = vertexpos * skewmatrix + translate;"); |
| 790 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord"); |
| 791 | } else { |
| 792 | v->codeAppend("float3x3 persp_matrix = float3x3(persp_x, persp_y, persp_z);"); |
| 793 | v->codeAppend("float3 devcoord = float3(vertexpos, 1) * persp_matrix;"); |
| 794 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "devcoord"); |
| 795 | } |
| 796 | |
| 797 | // Determine normalized arc coordinates for the implicit function. |
| 798 | GrGLSLVarying arcCoord((useHWDerivatives) ? kFloat2_GrSLType : kFloat4_GrSLType); |
| 799 | varyings->addVarying("arccoord", &arcCoord); |
| 800 | v->codeAppendf("if (is_arc_section) {"); |
| 801 | v->codeAppendf( "%s.xy = 1 - abs(radius_outset);", arcCoord.vsOut()); |
| 802 | if (!useHWDerivatives) { |
| 803 | // The gradient is order-1: Interpolate it across arccoord.zw. |
| 804 | // This doesn't work with perspective. |
| 805 | SkASSERT(!hasPerspective); |
| 806 | v->codeAppendf("float2x2 derivatives = inverse(skewmatrix);"); |
| 807 | v->codeAppendf("%s.zw = derivatives * (%s.xy/radii * corner * 2);", |
| 808 | arcCoord.vsOut(), arcCoord.vsOut()); |
| 809 | } |
| 810 | v->codeAppendf("} else {"); |
| 811 | if (useHWDerivatives) { |
| 812 | v->codeAppendf("%s = float2(0);", arcCoord.vsOut()); |
| 813 | } else { |
| 814 | v->codeAppendf("%s = float4(0);", arcCoord.vsOut()); |
| 815 | } |
| 816 | v->codeAppendf("}"); |
| 817 | |
| 818 | // Emit the fragment shader. |
| 819 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 820 | |
| 821 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 822 | |
| 823 | // If x,y == 0, then we are drawing a triangle that does not track an arc. |
| 824 | f->codeAppendf("if (float2(0) != %s.xy) {", arcCoord.fsIn()); |
| 825 | f->codeAppendf( "float fn = dot(%s.xy, %s.xy) - 1;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 826 | if (GrAAType::kMSAA == proc.fAAType) { |
| 827 | using ScopeFlags = GrGLSLFPFragmentBuilder::ScopeFlags; |
| 828 | if (!useHWDerivatives) { |
| 829 | f->codeAppendf("float2 grad = %s.zw;", arcCoord.fsIn()); |
| 830 | f->applyFnToMultisampleMask("fn", "grad", ScopeFlags::kInsidePerPrimitiveBranch); |
| 831 | } else { |
| 832 | f->applyFnToMultisampleMask("fn", nullptr, ScopeFlags::kInsidePerPrimitiveBranch); |
| 833 | } |
| 834 | } else { |
| 835 | f->codeAppendf("if (fn > 0) {"); |
| 836 | f->codeAppendf( "%s = half4(0);", args.fOutputCoverage); |
| 837 | f->codeAppendf("}"); |
| 838 | } |
| 839 | f->codeAppendf("}"); |
| 840 | } |
| 841 | |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 842 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&) override {} |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 843 | }; |
| 844 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 845 | GrGLSLPrimitiveProcessor* FillRRectOp::Processor::createGLSLInstance( |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 846 | const GrShaderCaps&) const { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 847 | if (GrAAType::kCoverage != fAAType) { |
| 848 | return new MSAAImpl(); |
| 849 | } |
| 850 | return new CoverageImpl(); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 851 | } |
| 852 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 853 | void FillRRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 854 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 855 | const GrSurfaceProxyView& writeView, |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 856 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 857 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 858 | GrXferBarrierFlags renderPassXferBarriers) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 859 | GrGeometryProcessor* gp = Processor::Make(arena, fHelper.aaType(), fProcessorFlags); |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 860 | SkASSERT(gp->instanceStride() == (size_t)fInstanceStride); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 861 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 862 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(appliedClip), |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 863 | dstProxyView, gp, GrPrimitiveType::kTriangles, |
| 864 | renderPassXferBarriers); |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 865 | } |
| 866 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 867 | void FillRRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 868 | if (!fInstanceBuffer || !fIndexBuffer || !fVertexBuffer) { |
| 869 | return; // Setup failed. |
| 870 | } |
| 871 | |
| 872 | if (!fProgramInfo) { |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 873 | this->createProgramInfo(flushState); |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 874 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 875 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 876 | flushState->bindPipelineAndScissorClip(*fProgramInfo, this->bounds()); |
| 877 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 878 | flushState->bindBuffers(std::move(fIndexBuffer), std::move(fInstanceBuffer), |
| 879 | std::move(fVertexBuffer)); |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 880 | flushState->drawIndexedInstanced(fIndexCount, 0, fInstanceCount, fBaseInstance, 0); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | // Will the given corner look good if we use HW derivatives? |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 884 | static bool can_use_hw_derivatives_with_coverage(const Sk2f& devScale, const Sk2f& cornerRadii) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 885 | Sk2f devRadii = devScale * cornerRadii; |
| 886 | if (devRadii[1] < devRadii[0]) { |
| 887 | devRadii = SkNx_shuffle<1,0>(devRadii); |
| 888 | } |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 889 | float minDevRadius = std::max(devRadii[0], 1.f); // Shader clamps radius at a minimum of 1. |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 890 | // Is the gradient smooth enough for this corner look ok if we use hardware derivatives? |
| 891 | // This threshold was arrived at subjevtively on an NVIDIA chip. |
| 892 | return minDevRadius * minDevRadius * 5 > devRadii[1]; |
| 893 | } |
| 894 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 895 | static bool can_use_hw_derivatives_with_coverage( |
| 896 | const Sk2f& devScale, const SkVector& cornerRadii) { |
| 897 | return can_use_hw_derivatives_with_coverage(devScale, Sk2f::Load(&cornerRadii)); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Will the given round rect look good if we use HW derivatives? |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 901 | static bool can_use_hw_derivatives_with_coverage( |
| 902 | const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix, const SkRRect& rrect) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 903 | if (!shaderCaps.shaderDerivativeSupport()) { |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | Sk2f x = Sk2f(viewMatrix.getScaleX(), viewMatrix.getSkewX()); |
| 908 | Sk2f y = Sk2f(viewMatrix.getSkewY(), viewMatrix.getScaleY()); |
| 909 | Sk2f devScale = (x*x + y*y).sqrt(); |
| 910 | switch (rrect.getType()) { |
| 911 | case SkRRect::kEmpty_Type: |
| 912 | case SkRRect::kRect_Type: |
| 913 | return true; |
| 914 | |
| 915 | case SkRRect::kOval_Type: |
| 916 | case SkRRect::kSimple_Type: |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 917 | return can_use_hw_derivatives_with_coverage(devScale, rrect.getSimpleRadii()); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 918 | |
| 919 | case SkRRect::kNinePatch_Type: { |
| 920 | Sk2f r0 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect)); |
| 921 | Sk2f r1 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect) + 2); |
| 922 | Sk2f minRadii = Sk2f::Min(r0, r1); |
| 923 | Sk2f maxRadii = Sk2f::Max(r0, r1); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 924 | return can_use_hw_derivatives_with_coverage(devScale, Sk2f(minRadii[0], maxRadii[1])) && |
| 925 | can_use_hw_derivatives_with_coverage(devScale, Sk2f(maxRadii[0], minRadii[1])); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | case SkRRect::kComplex_Type: { |
| 929 | for (int i = 0; i < 4; ++i) { |
| 930 | auto corner = static_cast<SkRRect::Corner>(i); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 931 | if (!can_use_hw_derivatives_with_coverage(devScale, rrect.radii(corner))) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 932 | return false; |
| 933 | } |
| 934 | } |
| 935 | return true; |
| 936 | } |
| 937 | } |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 938 | SK_ABORT("Invalid round rect type."); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 939 | } |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 940 | |
| 941 | } // anonymous namespace |
| 942 | |
| 943 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 944 | GrOp::Owner GrFillRRectOp::Make(GrRecordingContext* ctx, |
| 945 | GrPaint&& paint, |
| 946 | const SkMatrix& viewMatrix, |
| 947 | const SkRRect& rrect, |
| 948 | GrAAType aaType) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 949 | return FillRRectOp::Make(ctx, std::move(paint), viewMatrix, rrect, aaType); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | #if GR_TEST_UTILS |
| 953 | |
| 954 | #include "src/gpu/GrDrawOpTest.h" |
| 955 | |
| 956 | GR_DRAW_OP_TEST_DEFINE(FillRRectOp) { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 957 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 958 | GrAAType aaType = GrAAType::kNone; |
| 959 | if (random->nextBool()) { |
| 960 | aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage; |
| 961 | } |
| 962 | |
| 963 | SkRect rect = GrTest::TestRect(random); |
| 964 | float w = rect.width(); |
| 965 | float h = rect.height(); |
| 966 | |
| 967 | SkRRect rrect; |
| 968 | // TODO: test out other rrect configurations |
| 969 | rrect.setNinePatch(rect, w / 3.0f, h / 4.0f, w / 5.0f, h / 6.0); |
| 970 | |
| 971 | return GrFillRRectOp::Make(context, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 972 | std::move(paint), |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 973 | viewMatrix, |
| 974 | rrect, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 975 | aaType); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | #endif |