| 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 |
|
| 8 |
#include "src/gpu/ganesh/ops/FillRRectOp.h" |
| 9 |
|
| 10 |
#include "include/core/SkClipOp.h" |
| 11 |
#include "include/core/SkMatrix.h" |
| 12 |
#include "include/core/SkRRect.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 18 |
#include "include/private/base/SkAlignedStorage.h" |
| 19 |
#include "include/private/base/SkAssert.h" |
| 20 |
#include "include/private/base/SkDebug.h" |
| 21 |
#include "include/private/base/SkMacros.h" |
| 22 |
#include "include/private/base/SkOnce.h" |
| 23 |
#include "include/private/base/SkPoint_impl.h" |
| 24 |
#include "include/private/base/SkTArray.h" |
| 25 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 26 |
#include "src/base/SkArenaAlloc.h" |
| 27 |
#include "src/base/SkUtils.h" |
| 28 |
#include "src/base/SkVx.h" |
| 29 |
#include "src/core/SkColorData.h" |
| 30 |
#include "src/core/SkRRectPriv.h" |
| 31 |
#include "src/core/SkSLTypeShared.h" |
| 32 |
#include "src/gpu/BufferWriter.h" |
| 33 |
#include "src/gpu/KeyBuilder.h" |
| 34 |
#include "src/gpu/ResourceKey.h" |
| 35 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 36 |
#include "src/gpu/ganesh/GrBuffer.h" |
| 37 |
#include "src/gpu/ganesh/GrCaps.h" |
| 38 |
#include "src/gpu/ganesh/GrGeometryProcessor.h" |
| 39 |
#include "src/gpu/ganesh/GrMeshDrawTarget.h" |
| 40 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 41 |
#include "src/gpu/ganesh/GrPaint.h" |
| 42 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 43 |
#include "src/gpu/ganesh/GrProcessorSet.h" |
| 44 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 45 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 46 |
#include "src/gpu/ganesh/GrResourceProvider.h" |
| 47 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 48 |
#include "src/gpu/ganesh/GrShaderVar.h" |
| 49 |
#include "src/gpu/ganesh/geometry/GrShape.h" |
| 50 |
#include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h" |
| 51 |
#include "src/gpu/ganesh/glsl/GrGLSLVarying.h" |
| 52 |
#include "src/gpu/ganesh/glsl/GrGLSLVertexGeoBuilder.h" |
| 53 |
#include "src/gpu/ganesh/ops/GrDrawOp.h" |
| 54 |
#include "src/gpu/ganesh/ops/GrMeshDrawOp.h" |
| 55 |
#include "src/gpu/ganesh/ops/GrSimpleMeshDrawOpHelper.h" |
| 56 |
|
| 57 |
#if defined(GPU_TEST_UTILS) |
| 58 |
#include "src/base/SkRandom.h" |
| 59 |
#include "src/gpu/ganesh/GrDrawOpTest.h" |
| 60 |
#include "src/gpu/ganesh/GrTestUtils.h" |
| 61 |
#endif |
| 62 |
|
| 63 |
#include <algorithm> |
| 64 |
#include <array> |
| 65 |
#include <cstddef> |
| 66 |
#include <cstdint> |
| 67 |
#include <memory> |
| 68 |
#include <utility> |
| 69 |
|
| 70 |
class GrDstProxyView; |
| 71 |
class GrGLSLProgramDataManager; |
| 72 |
class GrSurfaceProxyView; |
| 73 |
enum class GrXferBarrierFlags; |
| 74 |
|
| 75 |
namespace skgpu::ganesh { |
| 76 |
class SurfaceDrawContext; |
| 77 |
} |
| 78 |
|
| 79 |
using namespace skia_private; |
| 80 |
|
| 81 |
namespace skgpu::ganesh::FillRRectOp { |
| 82 |
|
| 83 |
namespace { |
| 84 |
|
| 85 |
// Note: Just checking m.restStaysRect is not sufficient |
| 86 |
bool skews_are_relevant(const SkMatrix& m) { |
| 87 |
SkASSERT(!m.hasPerspective()); |
| 88 |
|
| 89 |
if (m[SkMatrix::kMSkewX] == 0.0f && m[SkMatrix::kMSkewY] == 0.0f) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
static constexpr float kTol = SK_ScalarNearlyZero; |
| 94 |
float absScaleX = SkScalarAbs(m[SkMatrix::kMScaleX]); |
| 95 |
float absSkewX = SkScalarAbs(m[SkMatrix::kMSkewX]); |
| 96 |
float absScaleY = SkScalarAbs(m[SkMatrix::kMScaleY]); |
| 97 |
float absSkewY = SkScalarAbs(m[SkMatrix::kMSkewY]); |
| 98 |
|
| 99 |
// The maximum absolute column sum norm of the upper left 2x2 |
| 100 |
float norm = std::max(absScaleX + absSkewY, absSkewX + absScaleY); |
| 101 |
|
| 102 |
return absSkewX > kTol * norm || absSkewY > kTol * norm; |
| 103 |
} |
| 104 |
|
| 105 |
class FillRRectOpImpl final : public GrMeshDrawOp { |
| 106 |
private: |
| 107 |
using Helper = GrSimpleMeshDrawOpHelper; |
| 108 |
|
| 109 |
public: |
| 110 |
DEFINE_OP_CLASS_ID |
| 111 |
|
| 112 |
struct LocalCoords { |
| 113 |
enum class Type : bool { kRect, kMatrix }; |
| 114 |
LocalCoords(const SkRect& localRect) |
| 115 |
: fType(Type::kRect) |
| 116 |
, fRect(localRect) {} |
| 117 |
LocalCoords(const SkMatrix& localMatrix) |
| 118 |
: fType(Type::kMatrix) |
| 119 |
, fMatrix(localMatrix) {} |
| 120 |
Type fType; |
| 121 |
union { |
| 122 |
SkRect fRect; |
| 123 |
SkMatrix fMatrix; |
| 124 |
}; |
| 125 |
}; |
| 126 |
|
| 127 |
static GrOp::Owner Make(GrRecordingContext*, |
| 128 |
SkArenaAlloc*, |
| 129 |
GrPaint&&, |
| 130 |
const SkMatrix& viewMatrix, |
| 131 |
const SkRRect&, |
| 132 |
const LocalCoords&, |
| 133 |
GrAA); |
| 134 |
|
| 135 |
const char* name() const override { return "FillRRectOp"; } |
| 136 |
|
| 137 |
FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 138 |
|
| 139 |
ClipResult clipToShape(skgpu::ganesh::SurfaceDrawContext*, |
| 140 |
SkClipOp, |
| 141 |
const SkMatrix& clipMatrix, |
| 142 |
const GrShape&, |
| 143 |
GrAA) override; |
| 144 |
|
| 145 |
GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override; |
| 146 |
CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) override; |
| 147 |
|
| 148 |
#if defined(GPU_TEST_UTILS) |
| 149 |
SkString onDumpInfo() const override; |
| 150 |
#endif |
| 151 |
|
| 152 |
void visitProxies(const GrVisitProxyFunc& func) const override { |
| 153 |
if (fProgramInfo) { |
| 154 |
fProgramInfo->visitFPProxies(func); |
| 155 |
} else { |
| 156 |
fHelper.visitProxies(func); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void onPrepareDraws(GrMeshDrawTarget*) override; |
| 161 |
|
| 162 |
void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
| 163 |
|
| 164 |
private: |
| 165 |
friend class ::GrSimpleMeshDrawOpHelper; // for access to ctor |
| 166 |
friend class ::GrOp; // for access to ctor |
| 167 |
|
| 168 |
enum class ProcessorFlags { |
| 169 |
kNone = 0, |
| 170 |
kUseHWDerivatives = 1 << 0, |
| 171 |
kHasLocalCoords = 1 << 1, |
| 172 |
kWideColor = 1 << 2, |
| 173 |
kMSAAEnabled = 1 << 3, |
| 174 |
kFakeNonAA = 1 << 4, |
| 175 |
}; |
| 176 |
constexpr static int kNumProcessorFlags = 5; |
| 177 |
|
| 178 |
SK_DECL_BITFIELD_CLASS_OPS_FRIENDS(ProcessorFlags); |
| 179 |
|
| 180 |
class Processor; |
| 181 |
|
| 182 |
FillRRectOpImpl(GrProcessorSet*, |
| 183 |
const SkPMColor4f& paintColor, |
| 184 |
SkArenaAlloc*, |
| 185 |
const SkMatrix& viewMatrix, |
| 186 |
const SkRRect&, |
| 187 |
const LocalCoords&, |
| 188 |
ProcessorFlags); |
| 189 |
|
| 190 |
GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 191 |
|
| 192 |
// Create a GrProgramInfo object in the provided arena |
| 193 |
void onCreateProgramInfo(const GrCaps*, |
| 194 |
SkArenaAlloc*, |
| 195 |
const GrSurfaceProxyView& writeView, |
| 196 |
bool usesMSAASurface, |
| 197 |
GrAppliedClip&&, |
| 198 |
const GrDstProxyView&, |
| 199 |
GrXferBarrierFlags renderPassXferBarriers, |
| 200 |
GrLoadOp colorLoadOp) override; |
| 201 |
|
| 202 |
Helper fHelper; |
| 203 |
ProcessorFlags fProcessorFlags; |
| 204 |
|
| 205 |
struct Instance { |
| 206 |
Instance(const SkMatrix& viewMatrix, |
| 207 |
const SkRRect& rrect, |
| 208 |
const LocalCoords& localCoords, |
| 209 |
const SkPMColor4f& color) |
| 210 |
: fViewMatrix(viewMatrix), fRRect(rrect), fLocalCoords(localCoords), fColor(color) { |
| 211 |
} |
| 212 |
SkMatrix fViewMatrix; |
| 213 |
SkRRect fRRect; |
| 214 |
LocalCoords fLocalCoords; |
| 215 |
SkPMColor4f fColor; |
| 216 |
Instance* fNext = nullptr; |
| 217 |
}; |
| 218 |
|
| 219 |
Instance* fHeadInstance; |
| 220 |
Instance** fTailInstance; |
| 221 |
int fInstanceCount = 1; |
| 222 |
|
| 223 |
sk_sp<const GrBuffer> fInstanceBuffer; |
| 224 |
sk_sp<const GrBuffer> fVertexBuffer; |
| 225 |
sk_sp<const GrBuffer> fIndexBuffer; |
| 226 |
int fBaseInstance = 0; |
| 227 |
|
| 228 |
// If this op is prePrepared the created programInfo will be stored here for use in |
| 229 |
// onExecute. In the prePrepared case it will have been stored in the record-time arena. |
| 230 |
GrProgramInfo* fProgramInfo = nullptr; |
| 231 |
}; |
| 232 |
|
| 233 |
SK_MAKE_BITFIELD_CLASS_OPS(FillRRectOpImpl::ProcessorFlags) |
| 234 |
|
| 235 |
// Hardware derivatives are not always accurate enough for highly elliptical corners. This method |
| 236 |
// checks to make sure the corners will still all look good if we use HW derivatives. |
| 237 |
bool can_use_hw_derivatives_with_coverage(const GrShaderCaps&, |
| 238 |
const SkMatrix&, |
| 239 |
const SkRRect&); |
| 240 |
|
| 241 |
GrOp::Owner FillRRectOpImpl::Make(GrRecordingContext* ctx, |
| 242 |
SkArenaAlloc* arena, |
| 243 |
GrPaint&& paint, |
| 244 |
const SkMatrix& viewMatrix, |
| 245 |
const SkRRect& rrect, |
| 246 |
const LocalCoords& localCoords, |
| 247 |
GrAA aa) { |
| 248 |
const GrCaps* caps = ctx->priv().caps(); |
| 249 |
|
| 250 |
if (!caps->drawInstancedSupport()) { |
| 251 |
return nullptr; |
| 252 |
} |
| 253 |
|
| 254 |
// We transform into a normalized -1..+1 space to draw the round rect. If the boundaries are too |
| 255 |
// large, the math can overflow. The caller can fall back on path rendering if this is the case. |
| 256 |
if (std::max(rrect.height(), rrect.width()) >= 1e6f) { |
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
|
| 260 |
ProcessorFlags flags = ProcessorFlags::kNone; |
| 261 |
// TODO: Support perspective in a follow-on CL. This shouldn't be difficult, since we already |
| 262 |
// use HW derivatives. The only trick will be adjusting the AA outset to account for |
| 263 |
// perspective. (i.e., outset = 0.5 * z.) |
| 264 |
if (viewMatrix.hasPerspective()) { |
| 265 |
return nullptr; |
| 266 |
} |
| 267 |
if (can_use_hw_derivatives_with_coverage(*caps->shaderCaps(), viewMatrix, rrect)) { |
| 268 |
// HW derivatives (more specifically, fwidth()) are consistently faster on all platforms in |
| 269 |
// coverage mode. We use them as long as the approximation will be accurate enough. |
| 270 |
flags |= ProcessorFlags::kUseHWDerivatives; |
| 271 |
} |
| 272 |
if (aa == GrAA::kNo) { |
| 273 |
flags |= ProcessorFlags::kFakeNonAA; |
| 274 |
} |
| 275 |
|
| 276 |
return Helper::FactoryHelper<FillRRectOpImpl>(ctx, std::move(paint), arena, viewMatrix, rrect, |
| 277 |
localCoords, flags); |
| 278 |
} |
| 279 |
|
| 280 |
FillRRectOpImpl::FillRRectOpImpl(GrProcessorSet* processorSet, |
| 281 |
const SkPMColor4f& paintColor, |
| 282 |
SkArenaAlloc* arena, |
| 283 |
const SkMatrix& viewMatrix, |
| 284 |
const SkRRect& rrect, |
| 285 |
const LocalCoords& localCoords, |
| 286 |
ProcessorFlags processorFlags) |
| 287 |
: GrMeshDrawOp(ClassID()) |
| 288 |
, fHelper(processorSet, |
| 289 |
(processorFlags & ProcessorFlags::kFakeNonAA) |
| 290 |
? GrAAType::kNone |
| 291 |
: GrAAType::kCoverage) // Use analytic AA even if the RT is MSAA. |
| 292 |
, fProcessorFlags(processorFlags & ~(ProcessorFlags::kHasLocalCoords | |
| 293 |
ProcessorFlags::kWideColor | |
| 294 |
ProcessorFlags::kMSAAEnabled)) |
| 295 |
, fHeadInstance(arena->make<Instance>(viewMatrix, rrect, localCoords, paintColor)) |
| 296 |
, fTailInstance(&fHeadInstance->fNext) { |
| 297 |
// FillRRectOp::Make fails if there is perspective. |
| 298 |
SkASSERT(!viewMatrix.hasPerspective()); |
| 299 |
this->setBounds(viewMatrix.mapRect(rrect.getBounds()), |
| 300 |
GrOp::HasAABloat(!(processorFlags & ProcessorFlags::kFakeNonAA)), |
| 301 |
GrOp::IsHairline::kNo); |
| 302 |
} |
| 303 |
|
| 304 |
GrDrawOp::ClipResult FillRRectOpImpl::clipToShape(skgpu::ganesh::SurfaceDrawContext* sdc, |
| 305 |
SkClipOp clipOp, |
| 306 |
const SkMatrix& clipMatrix, |
| 307 |
const GrShape& shape, |
| 308 |
GrAA aa) { |
| 309 |
SkASSERT(fInstanceCount == 1); // This needs to be called before combining. |
| 310 |
SkASSERT(fHeadInstance->fNext == nullptr); |
| 311 |
|
| 312 |
if ((shape.isRect() || shape.isRRect()) && |
| 313 |
clipOp == SkClipOp::kIntersect && |
| 314 |
(aa == GrAA::kNo) == (fProcessorFlags & ProcessorFlags::kFakeNonAA)) { |
| 315 |
// The clip shape is a round rect. Attempt to map it to a round rect in "viewMatrix" space. |
| 316 |
SkRRect clipRRect; |
| 317 |
if (clipMatrix == fHeadInstance->fViewMatrix) { |
| 318 |
if (shape.isRect()) { |
| 319 |
clipRRect.setRect(shape.rect()); |
| 320 |
} else { |
| 321 |
clipRRect = shape.rrect(); |
| 322 |
} |
| 323 |
} else { |
| 324 |
// Find a matrix that maps from "clipMatrix" space to "viewMatrix" space. |
| 325 |
SkASSERT(!fHeadInstance->fViewMatrix.hasPerspective()); |
| 326 |
if (clipMatrix.hasPerspective()) { |
| 327 |
return ClipResult::kFail; |
| 328 |
} |
| 329 |
SkMatrix clipToView; |
| 330 |
if (!fHeadInstance->fViewMatrix.invert(&clipToView)) { |
| 331 |
return ClipResult::kClippedOut; |
| 332 |
} |
| 333 |
clipToView.preConcat(clipMatrix); |
| 334 |
SkASSERT(!clipToView.hasPerspective()); |
| 335 |
|
| 336 |
if (skews_are_relevant(clipToView)) { |
| 337 |
// A rect in "clipMatrix" space is not a rect in "viewMatrix" space. |
| 338 |
return ClipResult::kFail; |
| 339 |
} |
| 340 |
clipToView.setSkewX(0); |
| 341 |
clipToView.setSkewY(0); |
| 342 |
SkASSERT(clipToView.rectStaysRect()); |
| 343 |
|
| 344 |
if (shape.isRect()) { |
| 345 |
clipRRect.setRect(clipToView.mapRect(shape.rect())); |
| 346 |
} else { |
| 347 |
if (auto rr = shape.rrect().transform(clipToView)) { |
| 348 |
clipRRect = *rr; |
| 349 |
} else { |
| 350 |
// Transforming the rrect failed. This shouldn't generally happen except in |
| 351 |
// cases of fp32 overflow. |
| 352 |
return ClipResult::kFail; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
// Intersect our round rect with the clip shape. |
| 358 |
SkRRect isectRRect; |
| 359 |
if (fHeadInstance->fRRect.isRect() && clipRRect.isRect()) { |
| 360 |
SkRect isectRect; |
| 361 |
if (!isectRect.intersect(fHeadInstance->fRRect.rect(), clipRRect.rect())) { |
| 362 |
return ClipResult::kClippedOut; |
| 363 |
} |
| 364 |
isectRRect.setRect(isectRect); |
| 365 |
} else { |
| 366 |
isectRRect = SkRRectPriv::ConservativeIntersect(fHeadInstance->fRRect, clipRRect); |
| 367 |
if (isectRRect.isEmpty()) { |
| 368 |
// The round rects did not intersect at all or the intersection was too complicated |
| 369 |
// to compute quickly. |
| 370 |
return ClipResult::kFail; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
// Don't apply the clip geometrically if it becomes subpixel, since then the hairline |
| 375 |
// rendering may outset beyond the original clip. |
| 376 |
SkRect devISectBounds = fHeadInstance->fViewMatrix.mapRect(isectRRect.rect()); |
| 377 |
if (devISectBounds.width() < 1.f || devISectBounds.height() < 1.f) { |
| 378 |
return ClipResult::kFail; |
| 379 |
} |
| 380 |
|
| 381 |
if (fHeadInstance->fLocalCoords.fType == LocalCoords::Type::kRect) { |
| 382 |
// Update the local rect. |
| 383 |
auto rect = sk_bit_cast<skvx::float4>(fHeadInstance->fRRect.rect()); |
| 384 |
auto local = sk_bit_cast<skvx::float4>(fHeadInstance->fLocalCoords.fRect); |
| 385 |
auto isect = sk_bit_cast<skvx::float4>(isectRRect.rect()); |
| 386 |
auto rectToLocalSize = (local - skvx::shuffle<2,3,0,1>(local)) / |
| 387 |
(rect - skvx::shuffle<2,3,0,1>(rect)); |
| 388 |
auto localCoordsRect = (isect - rect) * rectToLocalSize + local; |
| 389 |
fHeadInstance->fLocalCoords.fRect.setLTRB(localCoordsRect.x(), |
| 390 |
localCoordsRect.y(), |
| 391 |
localCoordsRect.z(), |
| 392 |
localCoordsRect.w()); |
| 393 |
} |
| 394 |
|
| 395 |
// Update the round rect. |
| 396 |
fHeadInstance->fRRect = isectRRect; |
| 397 |
return ClipResult::kClippedGeometrically; |
| 398 |
} |
| 399 |
|
| 400 |
return ClipResult::kFail; |
| 401 |
} |
| 402 |
|
| 403 |
GrProcessorSet::Analysis FillRRectOpImpl::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 404 |
GrClampType clampType) { |
| 405 |
SkASSERT(fInstanceCount == 1); |
| 406 |
SkASSERT(fHeadInstance->fNext == nullptr); |
| 407 |
|
| 408 |
bool isWideColor; |
| 409 |
auto analysis = fHelper.finalizeProcessors(caps, clip, clampType, |
| 410 |
GrProcessorAnalysisCoverage::kSingleChannel, |
| 411 |
&fHeadInstance->fColor, &isWideColor); |
| 412 |
if (isWideColor) { |
| 413 |
fProcessorFlags |= ProcessorFlags::kWideColor; |
| 414 |
} |
| 415 |
if (analysis.usesLocalCoords()) { |
| 416 |
fProcessorFlags |= ProcessorFlags::kHasLocalCoords; |
| 417 |
} |
| 418 |
return analysis; |
| 419 |
} |
| 420 |
|
| 421 |
GrOp::CombineResult FillRRectOpImpl::onCombineIfPossible(GrOp* op, |
| 422 |
SkArenaAlloc*, |
| 423 |
const GrCaps& caps) { |
| 424 |
auto that = op->cast<FillRRectOpImpl>(); |
| 425 |
if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds()) || |
| 426 |
fProcessorFlags != that->fProcessorFlags) { |
| 427 |
return CombineResult::kCannotCombine; |
| 428 |
} |
| 429 |
|
| 430 |
*fTailInstance = that->fHeadInstance; |
| 431 |
fTailInstance = that->fTailInstance; |
| 432 |
fInstanceCount += that->fInstanceCount; |
| 433 |
return CombineResult::kMerged; |
| 434 |
} |
| 435 |
|
| 436 |
#if defined(GPU_TEST_UTILS) |
| 437 |
SkString FillRRectOpImpl::onDumpInfo() const { |
| 438 |
SkString str = SkStringPrintf("# instances: %d\n", fInstanceCount); |
| 439 |
str += fHelper.dumpInfo(); |
| 440 |
int i = 0; |
| 441 |
for (Instance* tmp = fHeadInstance; tmp; tmp = tmp->fNext, ++i) { |
| 442 |
str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f] ", |
| 443 |
i, tmp->fColor.fR, tmp->fColor.fG, tmp->fColor.fB, tmp->fColor.fA); |
| 444 |
SkMatrix m = tmp->fViewMatrix; |
| 445 |
str.appendf("ViewMatrix: [%.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f] ", |
| 446 |
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]); |
| 447 |
SkRect r = tmp->fRRect.rect(); |
| 448 |
str.appendf("Rect: [%f %f %f %f]\n", r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 449 |
} |
| 450 |
return str; |
| 451 |
} |
| 452 |
#endif |
| 453 |
|
| 454 |
class FillRRectOpImpl::Processor final : public GrGeometryProcessor { |
| 455 |
public: |
| 456 |
static GrGeometryProcessor* Make(SkArenaAlloc* arena, GrAAType aaType, ProcessorFlags flags) { |
| 457 |
return arena->make([&](void* ptr) { |
| 458 |
return new (ptr) Processor(aaType, flags); |
| 459 |
}); |
| 460 |
} |
| 461 |
|
| 462 |
const char* name() const override { return "FillRRectOp::Processor"; } |
| 463 |
|
| 464 |
void addToKey(const GrShaderCaps& caps, KeyBuilder* b) const override { |
| 465 |
b->addBits(kNumProcessorFlags, (uint32_t)fFlags, "flags"); |
| 466 |
} |
| 467 |
|
| 468 |
std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override; |
| 469 |
|
| 470 |
private: |
| 471 |
class Impl; |
| 472 |
|
| 473 |
Processor(GrAAType aaType, ProcessorFlags flags) |
| 474 |
: GrGeometryProcessor(kGrFillRRectOp_Processor_ClassID) |
| 475 |
, fFlags(flags) { |
| 476 |
this->setVertexAttributesWithImplicitOffsets(kVertexAttribs, std::size(kVertexAttribs)); |
| 477 |
|
| 478 |
fInstanceAttribs.emplace_back("radii_x", kFloat4_GrVertexAttribType, SkSLType::kFloat4); |
| 479 |
fInstanceAttribs.emplace_back("radii_y", kFloat4_GrVertexAttribType, SkSLType::kFloat4); |
| 480 |
fInstanceAttribs.emplace_back("skew", kFloat4_GrVertexAttribType, SkSLType::kFloat4); |
| 481 |
if (fFlags & ProcessorFlags::kHasLocalCoords) { |
| 482 |
fInstanceAttribs.emplace_back("translate_and_localrotate", |
| 483 |
kFloat4_GrVertexAttribType, |
| 484 |
SkSLType::kFloat4); |
| 485 |
fInstanceAttribs.emplace_back( |
| 486 |
"localrect", kFloat4_GrVertexAttribType, SkSLType::kFloat4); |
| 487 |
} else { |
| 488 |
fInstanceAttribs.emplace_back("translate_and_localrotate", |
| 489 |
kFloat2_GrVertexAttribType, |
| 490 |
SkSLType::kFloat2); |
| 491 |
} |
| 492 |
fColorAttrib = &fInstanceAttribs.push_back( |
| 493 |
MakeColorAttribute("color", (fFlags & ProcessorFlags::kWideColor))); |
| 494 |
SkASSERT(fInstanceAttribs.size() <= kMaxInstanceAttribs); |
| 495 |
this->setInstanceAttributesWithImplicitOffsets(fInstanceAttribs.begin(), |
| 496 |
fInstanceAttribs.size()); |
| 497 |
} |
| 498 |
|
| 499 |
inline static constexpr Attribute kVertexAttribs[] = { |
| 500 |
{"radii_selector", kFloat4_GrVertexAttribType, SkSLType::kFloat4}, |
| 501 |
{"corner_and_radius_outsets", kFloat4_GrVertexAttribType, SkSLType::kFloat4}, |
| 502 |
// Coverage only. |
| 503 |
{"aa_bloat_and_coverage", kFloat4_GrVertexAttribType, SkSLType::kFloat4}}; |
| 504 |
|
| 505 |
const ProcessorFlags fFlags; |
| 506 |
|
| 507 |
constexpr static int kMaxInstanceAttribs = 6; |
| 508 |
STArray<kMaxInstanceAttribs, Attribute> fInstanceAttribs; |
| 509 |
const Attribute* fColorAttrib; |
| 510 |
}; |
| 511 |
|
| 512 |
// Our coverage geometry consists of an inset octagon with solid coverage, surrounded by linear |
| 513 |
// coverage ramps on the horizontal and vertical edges, and "arc coverage" pieces on the diagonal |
| 514 |
// edges. The Vertex struct tells the shader where to place its vertex within a normalized |
| 515 |
// ([l, t, r, b] = [-1, -1, +1, +1]) space, and how to calculate coverage. See onEmitCode. |
| 516 |
struct CoverageVertex { |
| 517 |
std::array<float, 4> fRadiiSelector; |
| 518 |
std::array<float, 2> fCorner; |
| 519 |
std::array<float, 2> fRadiusOutset; |
| 520 |
std::array<float, 2> fAABloatDirection; |
| 521 |
float fCoverage; |
| 522 |
float fIsLinearCoverage; |
| 523 |
}; |
| 524 |
|
| 525 |
// This is the offset (when multiplied by radii) from the corners of a bounding box to the vertices |
| 526 |
// of its inscribed octagon. We draw the outside portion of arcs with quarter-octagons rather than |
| 527 |
// rectangles. |
| 528 |
static constexpr float kOctoOffset = 1/(1 + SK_ScalarRoot2Over2); |
| 529 |
|
| 530 |
static constexpr CoverageVertex kVertexData[] = { |
| 531 |
// Left inset edge. |
| 532 |
{{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{+1,0}}, 1, 1}, |
| 533 |
{{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{+1,0}}, 1, 1}, |
| 534 |
|
| 535 |
// Top inset edge. |
| 536 |
{{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,+1}}, 1, 1}, |
| 537 |
{{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,+1}}, 1, 1}, |
| 538 |
|
| 539 |
// Right inset edge. |
| 540 |
{{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{-1,0}}, 1, 1}, |
| 541 |
{{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{-1,0}}, 1, 1}, |
| 542 |
|
| 543 |
// Bottom inset edge. |
| 544 |
{{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,-1}}, 1, 1}, |
| 545 |
{{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,-1}}, 1, 1}, |
| 546 |
|
| 547 |
|
| 548 |
// Left outset edge. |
| 549 |
{{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{-1,0}}, 0, 1}, |
| 550 |
{{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{-1,0}}, 0, 1}, |
| 551 |
|
| 552 |
// Top outset edge. |
| 553 |
{{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,-1}}, 0, 1}, |
| 554 |
{{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,-1}}, 0, 1}, |
| 555 |
|
| 556 |
// Right outset edge. |
| 557 |
{{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{+1,0}}, 0, 1}, |
| 558 |
{{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{+1,0}}, 0, 1}, |
| 559 |
|
| 560 |
// Bottom outset edge. |
| 561 |
{{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,+1}}, 0, 1}, |
| 562 |
{{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,+1}}, 0, 1}, |
| 563 |
|
| 564 |
|
| 565 |
// Top-left corner. |
| 566 |
{{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{-1, 0}}, 0, 0}, |
| 567 |
{{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{+1, 0}}, 1, 0}, |
| 568 |
{{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,+1}}, 1, 0}, |
| 569 |
{{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,-1}}, 0, 0}, |
| 570 |
{{{1,0,0,0}}, {{-1,-1}}, {{+kOctoOffset,0}}, {{-1,-1}}, 0, 0}, |
| 571 |
{{{1,0,0,0}}, {{-1,-1}}, {{0,+kOctoOffset}}, {{-1,-1}}, 0, 0}, |
| 572 |
|
| 573 |
// Top-right corner. |
| 574 |
{{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,-1}}, 0, 0}, |
| 575 |
{{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,+1}}, 1, 0}, |
| 576 |
{{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{-1, 0}}, 1, 0}, |
| 577 |
{{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{+1, 0}}, 0, 0}, |
| 578 |
{{{0,1,0,0}}, {{+1,-1}}, {{0,+kOctoOffset}}, {{+1,-1}}, 0, 0}, |
| 579 |
{{{0,1,0,0}}, {{+1,-1}}, {{-kOctoOffset,0}}, {{+1,-1}}, 0, 0}, |
| 580 |
|
| 581 |
// Bottom-right corner. |
| 582 |
{{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{+1, 0}}, 0, 0}, |
| 583 |
{{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{-1, 0}}, 1, 0}, |
| 584 |
{{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,-1}}, 1, 0}, |
| 585 |
{{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,+1}}, 0, 0}, |
| 586 |
{{{0,0,1,0}}, {{+1,+1}}, {{-kOctoOffset,0}}, {{+1,+1}}, 0, 0}, |
| 587 |
{{{0,0,1,0}}, {{+1,+1}}, {{0,-kOctoOffset}}, {{+1,+1}}, 0, 0}, |
| 588 |
|
| 589 |
// Bottom-left corner. |
| 590 |
{{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,+1}}, 0, 0}, |
| 591 |
{{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,-1}}, 1, 0}, |
| 592 |
{{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{+1, 0}}, 1, 0}, |
| 593 |
{{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{-1, 0}}, 0, 0}, |
| 594 |
{{{0,0,0,1}}, {{-1,+1}}, {{0,-kOctoOffset}}, {{-1,+1}}, 0, 0}, |
| 595 |
{{{0,0,0,1}}, {{-1,+1}}, {{+kOctoOffset,0}}, {{-1,+1}}, 0, 0}}; |
| 596 |
|
| 597 |
SKGPU_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 598 |
|
| 599 |
static constexpr uint16_t kIndexData[] = { |
| 600 |
// Inset octagon (solid coverage). |
| 601 |
0, 1, 7, |
| 602 |
1, 2, 7, |
| 603 |
7, 2, 6, |
| 604 |
2, 3, 6, |
| 605 |
6, 3, 5, |
| 606 |
3, 4, 5, |
| 607 |
|
| 608 |
// AA borders (linear coverage). |
| 609 |
0, 1, 8, 1, 9, 8, |
| 610 |
2, 3, 10, 3, 11, 10, |
| 611 |
4, 5, 12, 5, 13, 12, |
| 612 |
6, 7, 14, 7, 15, 14, |
| 613 |
|
| 614 |
// Top-left arc. |
| 615 |
16, 17, 21, |
| 616 |
17, 21, 18, |
| 617 |
21, 18, 20, |
| 618 |
18, 20, 19, |
| 619 |
|
| 620 |
// Top-right arc. |
| 621 |
22, 23, 27, |
| 622 |
23, 27, 24, |
| 623 |
27, 24, 26, |
| 624 |
24, 26, 25, |
| 625 |
|
| 626 |
// Bottom-right arc. |
| 627 |
28, 29, 33, |
| 628 |
29, 33, 30, |
| 629 |
33, 30, 32, |
| 630 |
30, 32, 31, |
| 631 |
|
| 632 |
// Bottom-left arc. |
| 633 |
34, 35, 39, |
| 634 |
35, 39, 36, |
| 635 |
39, 36, 38, |
| 636 |
36, 38, 37}; |
| 637 |
|
| 638 |
SKGPU_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 639 |
|
| 640 |
void FillRRectOpImpl::onPrepareDraws(GrMeshDrawTarget* target) { |
| 641 |
if (!fProgramInfo) { |
| 642 |
this->createProgramInfo(target); |
| 643 |
} |
| 644 |
|
| 645 |
size_t instanceStride = fProgramInfo->geomProc().instanceStride(); |
| 646 |
|
| 647 |
if (VertexWriter instanceWriter = target->makeVertexWriter(instanceStride, fInstanceCount, |
| 648 |
&fInstanceBuffer, &fBaseInstance)) SK_LIKELY { |
| 649 |
SkDEBUGCODE(auto end = instanceWriter.mark(instanceStride * fInstanceCount)); |
| 650 |
for (Instance* i = fHeadInstance; i; i = i->fNext) { |
| 651 |
auto [l, t, r, b] = i->fRRect.rect(); |
| 652 |
|
| 653 |
// Produce a matrix that draws the round rect from normalized [-1, -1, +1, +1] space. |
| 654 |
SkMatrix m; |
| 655 |
// Unmap the normalized rect [-1, -1, +1, +1] back to [l, t, r, b]. |
| 656 |
m.setScaleTranslate((r - l)/2, (b - t)/2, (l + r)/2, (t + b)/2); |
| 657 |
// Map to device space. |
| 658 |
m.postConcat(i->fViewMatrix); |
| 659 |
|
| 660 |
// Convert the radii to [-1, -1, +1, +1] space and write their attribs. |
| 661 |
skvx::float4 radiiX, radiiY; |
| 662 |
skvx::strided_load2(&SkRRectPriv::GetRadiiArray(i->fRRect)->fX, radiiX, radiiY); |
| 663 |
radiiX *= 2 / (r - l); |
| 664 |
radiiY *= 2 / (b - t); |
| 665 |
|
| 666 |
instanceWriter << radiiX << radiiY |
| 667 |
<< m.getScaleX() << m.getSkewX() << m.getSkewY() << m.getScaleY() |
| 668 |
<< m.getTranslateX() << m.getTranslateY(); |
| 669 |
|
| 670 |
if (fProcessorFlags & ProcessorFlags::kHasLocalCoords) { |
| 671 |
if (i->fLocalCoords.fType == LocalCoords::Type::kRect) { |
| 672 |
instanceWriter << 0.f << 0.f // localrotate |
| 673 |
<< i->fLocalCoords.fRect; // localrect |
| 674 |
} else { |
| 675 |
SkASSERT(i->fLocalCoords.fType == LocalCoords::Type::kMatrix); |
| 676 |
const SkRect& bounds = i->fRRect.rect(); |
| 677 |
const SkMatrix& localMatrix = i->fLocalCoords.fMatrix; |
| 678 |
SkVector u = localMatrix.mapVector(bounds.right() - bounds.left(), 0); |
| 679 |
SkVector v = localMatrix.mapVector(0, bounds.bottom() - bounds.top()); |
| 680 |
SkPoint l0 = localMatrix.mapPoint({bounds.left(), bounds.top()}); |
| 681 |
instanceWriter << v.x() << u.y() // localrotate |
| 682 |
<< l0 << (l0.x() + u.x()) << (l0.y() + v.y()); // localrect |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
instanceWriter << VertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor); |
| 687 |
} |
| 688 |
SkASSERT(instanceWriter.mark() == end); |
| 689 |
} |
| 690 |
|
| 691 |
SKGPU_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 692 |
|
| 693 |
fIndexBuffer = target->resourceProvider()->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 694 |
sizeof(kIndexData), |
| 695 |
kIndexData, gIndexBufferKey); |
| 696 |
|
| 697 |
SKGPU_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
| 698 |
|
| 699 |
fVertexBuffer = target->resourceProvider()->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, |
| 700 |
sizeof(kVertexData), |
| 701 |
kVertexData, |
| 702 |
gVertexBufferKey); |
| 703 |
} |
| 704 |
|
| 705 |
class FillRRectOpImpl::Processor::Impl : public ProgramImpl { |
| 706 |
public: |
| 707 |
void setData(const GrGLSLProgramDataManager&, |
| 708 |
const GrShaderCaps&, |
| 709 |
const GrGeometryProcessor&) override {} |
| 710 |
|
| 711 |
private: |
| 712 |
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 713 |
GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 714 |
GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 715 |
|
| 716 |
const auto& proc = args.fGeomProc.cast<Processor>(); |
| 717 |
bool useHWDerivatives = (proc.fFlags & ProcessorFlags::kUseHWDerivatives); |
| 718 |
|
| 719 |
SkASSERT(proc.vertexStride() == sizeof(CoverageVertex)); |
| 720 |
|
| 721 |
GrGLSLVaryingHandler* varyings = args.fVaryingHandler; |
| 722 |
varyings->emitAttributes(proc); |
| 723 |
f->codeAppendf("half4 %s;", args.fOutputColor); |
| 724 |
varyings->addPassThroughAttribute(proc.fColorAttrib->asShaderVar(), |
| 725 |
args.fOutputColor, |
| 726 |
GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 727 |
|
| 728 |
// Emit the vertex shader. |
| 729 |
// When MSAA is enabled, we need to make sure every sample gets lit up on pixels that have |
| 730 |
// fractional coverage. We do this by making the ramp wider. |
| 731 |
v->codeAppendf("float aa_bloat_multiplier = %i;", |
| 732 |
(proc.fFlags & ProcessorFlags::kMSAAEnabled) |
| 733 |
? 2 // Outset an entire pixel (2 radii). |
| 734 |
: (!(proc.fFlags & ProcessorFlags::kFakeNonAA)) |
| 735 |
? 1 // Outset one half pixel (1 radius). |
| 736 |
: 0); // No AA bloat. |
| 737 |
|
| 738 |
// Unpack vertex attribs. |
| 739 |
v->codeAppend("float2 corner = corner_and_radius_outsets.xy;"); |
| 740 |
v->codeAppend("float2 radius_outset = corner_and_radius_outsets.zw;"); |
| 741 |
v->codeAppend("float2 aa_bloat_direction = aa_bloat_and_coverage.xy;"); |
| 742 |
v->codeAppend("float is_linear_coverage = aa_bloat_and_coverage.w;"); |
| 743 |
|
| 744 |
// Find the amount to bloat each edge for AA (in source space). |
| 745 |
v->codeAppend("float2 pixellength = inversesqrt(" |
| 746 |
"float2(dot(skew.xz, skew.xz), dot(skew.yw, skew.yw)));"); |
| 747 |
v->codeAppend("float4 normalized_axis_dirs = skew * pixellength.xyxy;"); |
| 748 |
v->codeAppend("float2 axiswidths = (abs(normalized_axis_dirs.xy) + " |
| 749 |
"abs(normalized_axis_dirs.zw));"); |
| 750 |
v->codeAppend("float2 aa_bloatradius = axiswidths * pixellength * .5;"); |
| 751 |
|
| 752 |
// Identify our radii. |
| 753 |
v->codeAppend("float4 radii_and_neighbors = radii_selector" |
| 754 |
"* float4x4(radii_x, radii_y, radii_x.yxwz, radii_y.wzyx);"); |
| 755 |
v->codeAppend("float2 radii = radii_and_neighbors.xy;"); |
| 756 |
v->codeAppend("float2 neighbor_radii = radii_and_neighbors.zw;"); |
| 757 |
|
| 758 |
v->codeAppend("float coverage_multiplier = 1;"); |
| 759 |
v->codeAppend("if (any(greaterThan(aa_bloatradius, float2(1)))) {"); |
| 760 |
// The rrect is more narrow than a half-pixel AA coverage ramp. We can't |
| 761 |
// draw as-is or else opposite AA borders will overlap. Instead, fudge the |
| 762 |
// size up to the width of a coverage ramp, and then reduce total coverage |
| 763 |
// to make the rect appear more thin. |
| 764 |
v->codeAppend( "corner = max(abs(corner), aa_bloatradius) * sign(corner);"); |
| 765 |
v->codeAppend( "coverage_multiplier = 1 / (max(aa_bloatradius.x, 1) * " |
| 766 |
"max(aa_bloatradius.y, 1));"); |
| 767 |
// Set radii to zero to ensure we take the "linear coverage" codepath. |
| 768 |
// (The "coverage" variable only has effect in the linear codepath.) |
| 769 |
v->codeAppend( "radii = float2(0);"); |
| 770 |
v->codeAppend("}"); |
| 771 |
|
| 772 |
// Unpack coverage. |
| 773 |
v->codeAppend("float coverage = aa_bloat_and_coverage.z;"); |
| 774 |
if (proc.fFlags & ProcessorFlags::kMSAAEnabled) { |
| 775 |
// MSAA has a wider ramp that goes from -.5 to 1.5 instead of 0 to 1. |
| 776 |
v->codeAppendf("coverage = (coverage - .5) * aa_bloat_multiplier + .5;"); |
| 777 |
} |
| 778 |
|
| 779 |
v->codeAppend("if (any(lessThan(radii, aa_bloatradius * 1.5))) {"); |
| 780 |
// The radii are very small. Demote this arc to a sharp 90 degree corner. |
| 781 |
v->codeAppend( "radii = float2(0);"); |
| 782 |
// Convert to a standard picture frame for an AA rect instead of the round |
| 783 |
// rect geometry. |
| 784 |
v->codeAppend( "aa_bloat_direction = sign(corner);"); |
| 785 |
v->codeAppend( "if (coverage > .5) {"); // Are we an inset edge? |
| 786 |
v->codeAppend( "aa_bloat_direction = -aa_bloat_direction;"); |
| 787 |
v->codeAppend( "}"); |
| 788 |
v->codeAppend( "is_linear_coverage = 1;"); |
| 789 |
v->codeAppend("} else {"); |
| 790 |
// Don't let radii get smaller than a coverage ramp plus an extra half |
| 791 |
// pixel for MSAA. Always use the same amount so we don't pop when |
| 792 |
// switching between MSAA and coverage. |
| 793 |
v->codeAppend( "radii = clamp(radii, pixellength * 1.5, 2 - pixellength * 1.5);"); |
| 794 |
v->codeAppend( "neighbor_radii = clamp(neighbor_radii, pixellength * 1.5, " |
| 795 |
"2 - pixellength * 1.5);"); |
| 796 |
// Don't let neighboring radii get closer together than 1/16 pixel. |
| 797 |
v->codeAppend( "float2 spacing = 2 - radii - neighbor_radii;"); |
| 798 |
v->codeAppend( "float2 extra_pad = max(pixellength * .0625 - spacing, float2(0));"); |
| 799 |
v->codeAppend( "radii -= extra_pad * .5;"); |
| 800 |
v->codeAppend("}"); |
| 801 |
|
| 802 |
// Find our vertex position, adjusted for radii and bloated for AA. Our rect is drawn in |
| 803 |
// normalized [-1,-1,+1,+1] space. |
| 804 |
v->codeAppend("float2 aa_outset = " |
| 805 |
"aa_bloat_direction * aa_bloatradius * aa_bloat_multiplier;"); |
| 806 |
v->codeAppend("float2 vertexpos = corner + radius_outset * radii + aa_outset;"); |
| 807 |
|
| 808 |
v->codeAppend("if (coverage > .5) {"); // Are we an inset edge? |
| 809 |
// Don't allow the aa insets to overlap. i.e., Don't let them inset past |
| 810 |
// the center (x=y=0). Since we don't allow the rect to become thinner |
| 811 |
// than 1px, this should only happen when using MSAA, where we inset by an |
| 812 |
// entire pixel instead of half. |
| 813 |
v->codeAppend( "if (aa_bloat_direction.x != 0 && vertexpos.x * corner.x < 0) {"); |
| 814 |
v->codeAppend( "float backset = abs(vertexpos.x);"); |
| 815 |
v->codeAppend( "vertexpos.x = 0;"); |
| 816 |
v->codeAppend( "vertexpos.y += " |
| 817 |
"backset * sign(corner.y) * pixellength.y/pixellength.x;"); |
| 818 |
v->codeAppend( "coverage = (coverage - .5) * abs(corner.x) / " |
| 819 |
"(abs(corner.x) + backset) + .5;"); |
| 820 |
v->codeAppend( "}"); |
| 821 |
v->codeAppend( "if (aa_bloat_direction.y != 0 && vertexpos.y * corner.y < 0) {"); |
| 822 |
v->codeAppend( "float backset = abs(vertexpos.y);"); |
| 823 |
v->codeAppend( "vertexpos.y = 0;"); |
| 824 |
v->codeAppend( "vertexpos.x += " |
| 825 |
"backset * sign(corner.x) * pixellength.x/pixellength.y;"); |
| 826 |
v->codeAppend( "coverage = (coverage - .5) * abs(corner.y) / " |
| 827 |
"(abs(corner.y) + backset) + .5;"); |
| 828 |
v->codeAppend( "}"); |
| 829 |
v->codeAppend("}"); |
| 830 |
|
| 831 |
// Transform to device space. |
| 832 |
v->codeAppend("float2x2 skewmatrix = float2x2(skew.xy, skew.zw);"); |
| 833 |
v->codeAppend("float2 devcoord = vertexpos * skewmatrix + translate_and_localrotate.xy;"); |
| 834 |
gpArgs->fPositionVar.set(SkSLType::kFloat2, "devcoord"); |
| 835 |
|
| 836 |
// Output local coordinates. |
| 837 |
if (proc.fFlags & ProcessorFlags::kHasLocalCoords) { |
| 838 |
// Do math in a way that preserves exact local coord boundaries when there is no local |
| 839 |
// rotate and vertexpos is on an exact shape boundary. |
| 840 |
v->codeAppend("float2 T = vertexpos * .5 + .5;"); |
| 841 |
v->codeAppend("float2 localcoord = localrect.xy * (1 - T) + " |
| 842 |
"localrect.zw * T + " |
| 843 |
"translate_and_localrotate.zw * T.yx;"); |
| 844 |
gpArgs->fLocalCoordVar.set(SkSLType::kFloat2, "localcoord"); |
| 845 |
} |
| 846 |
|
| 847 |
// Setup interpolants for coverage. |
| 848 |
GrGLSLVarying arcCoord(useHWDerivatives ? SkSLType::kFloat2 : SkSLType::kFloat4); |
| 849 |
varyings->addVarying("arccoord", &arcCoord); |
| 850 |
v->codeAppend("if (0 != is_linear_coverage) {"); |
| 851 |
// We are a non-corner piece: Set x=0 to indicate built-in coverage, and |
| 852 |
// interpolate linear coverage across y. |
| 853 |
v->codeAppendf( "%s.xy = float2(0, coverage * coverage_multiplier);", |
| 854 |
arcCoord.vsOut()); |
| 855 |
v->codeAppend("} else {"); |
| 856 |
// Find the normalized arc coordinates for our corner ellipse. |
| 857 |
// (i.e., the coordinate system where x^2 + y^2 == 1). |
| 858 |
v->codeAppend( "float2 arccoord = 1 - abs(radius_outset) + aa_outset/radii * corner;"); |
| 859 |
// We are a corner piece: Interpolate the arc coordinates for coverage. |
| 860 |
// Emit x+1 to ensure no pixel in the arc has a x value of 0 (since x=0 |
| 861 |
// instructs the fragment shader to use linear coverage). |
| 862 |
v->codeAppendf( "%s.xy = float2(arccoord.x+1, arccoord.y);", arcCoord.vsOut()); |
| 863 |
if (!useHWDerivatives) { |
| 864 |
// The gradient is order-1: Interpolate it across arccoord.zw. |
| 865 |
v->codeAppendf("float2x2 derivatives = inverse(skewmatrix);"); |
| 866 |
v->codeAppendf("%s.zw = derivatives * (arccoord/radii * 2);", arcCoord.vsOut()); |
| 867 |
} |
| 868 |
v->codeAppend("}"); |
| 869 |
|
| 870 |
// Emit the fragment shader. |
| 871 |
f->codeAppendf("float x_plus_1=%s.x, y=%s.y;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 872 |
f->codeAppendf("half coverage;"); |
| 873 |
f->codeAppendf("if (0 == x_plus_1) {"); |
| 874 |
f->codeAppendf( "coverage = half(y);"); // We are a non-arc pixel (linear coverage). |
| 875 |
f->codeAppendf("} else {"); |
| 876 |
f->codeAppendf( "float fn = x_plus_1 * (x_plus_1 - 2);"); // fn = (x+1)*(x-1) = x^2-1 |
| 877 |
f->codeAppendf( "fn = fma(y,y, fn);"); // fn = x^2 + y^2 - 1 |
| 878 |
if (useHWDerivatives) { |
| 879 |
f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 880 |
} else { |
| 881 |
// The gradient is interpolated across arccoord.zw. |
| 882 |
f->codeAppendf("float gx=%s.z, gy=%s.w;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 883 |
f->codeAppendf("float fnwidth = abs(gx) + abs(gy);"); |
| 884 |
} |
| 885 |
f->codeAppendf( "coverage = .5 - half(fn/fnwidth);"); |
| 886 |
if (proc.fFlags & ProcessorFlags::kMSAAEnabled) { |
| 887 |
// MSAA uses ramps larger than 1px, so we need to clamp in both branches. |
| 888 |
f->codeAppendf("}"); |
| 889 |
} |
| 890 |
f->codeAppendf("coverage = clamp(coverage, 0, 1);"); |
| 891 |
if (!(proc.fFlags & ProcessorFlags::kMSAAEnabled)) { |
| 892 |
// When not using MSAA, we only need to clamp in the "arc" branch. |
| 893 |
f->codeAppendf("}"); |
| 894 |
} |
| 895 |
if (proc.fFlags & ProcessorFlags::kFakeNonAA) { |
| 896 |
f->codeAppendf("coverage = (coverage >= .5) ? 1 : 0;"); |
| 897 |
} |
| 898 |
f->codeAppendf("half4 %s = half4(coverage);", args.fOutputCoverage); |
| 899 |
} |
| 900 |
}; |
| 901 |
|
| 902 |
std::unique_ptr<GrGeometryProcessor::ProgramImpl> FillRRectOpImpl::Processor::makeProgramImpl( |
| 903 |
const GrShaderCaps&) const { |
| 904 |
return std::make_unique<Impl>(); |
| 905 |
} |
| 906 |
|
| 907 |
void FillRRectOpImpl::onCreateProgramInfo(const GrCaps* caps, |
| 908 |
SkArenaAlloc* arena, |
| 909 |
const GrSurfaceProxyView& writeView, |
| 910 |
bool usesMSAASurface, |
| 911 |
GrAppliedClip&& appliedClip, |
| 912 |
const GrDstProxyView& dstProxyView, |
| 913 |
GrXferBarrierFlags renderPassXferBarriers, |
| 914 |
GrLoadOp colorLoadOp) { |
| 915 |
if (usesMSAASurface) { |
| 916 |
fProcessorFlags |= ProcessorFlags::kMSAAEnabled; |
| 917 |
} |
| 918 |
GrGeometryProcessor* gp = Processor::Make(arena, fHelper.aaType(), fProcessorFlags); |
| 919 |
fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, usesMSAASurface, |
| 920 |
std::move(appliedClip), dstProxyView, gp, |
| 921 |
GrPrimitiveType::kTriangles, renderPassXferBarriers, |
| 922 |
colorLoadOp); |
| 923 |
} |
| 924 |
|
| 925 |
void FillRRectOpImpl::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
| 926 |
if (!fInstanceBuffer || !fIndexBuffer || !fVertexBuffer) { |
| 927 |
return; // Setup failed. |
| 928 |
} |
| 929 |
|
| 930 |
flushState->bindPipelineAndScissorClip(*fProgramInfo, this->bounds()); |
| 931 |
flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
| 932 |
flushState->bindBuffers(std::move(fIndexBuffer), std::move(fInstanceBuffer), |
| 933 |
std::move(fVertexBuffer)); |
| 934 |
flushState->drawIndexedInstanced(std::size(kIndexData), 0, fInstanceCount, fBaseInstance, 0); |
| 935 |
} |
| 936 |
|
| 937 |
// Will the given corner look good if we use HW derivatives? |
| 938 |
bool can_use_hw_derivatives_with_coverage(const skvx::float2& devScale, |
| 939 |
const skvx::float2& cornerRadii) { |
| 940 |
skvx::float2 devRadii = devScale * cornerRadii; |
| 941 |
if (devRadii[1] < devRadii[0]) { |
| 942 |
devRadii = skvx::shuffle<1,0>(devRadii); |
| 943 |
} |
| 944 |
float minDevRadius = std::max(devRadii[0], 1.f); // Shader clamps radius at a minimum of 1. |
| 945 |
// Is the gradient smooth enough for this corner look ok if we use hardware derivatives? |
| 946 |
// This threshold was arrived at subjevtively on an NVIDIA chip. |
| 947 |
return minDevRadius * minDevRadius * 5 > devRadii[1]; |
| 948 |
} |
| 949 |
|
| 950 |
bool can_use_hw_derivatives_with_coverage(const skvx::float2& devScale, |
| 951 |
const SkVector& cornerRadii) { |
| 952 |
return can_use_hw_derivatives_with_coverage(devScale, skvx::float2::Load(&cornerRadii)); |
| 953 |
} |
| 954 |
|
| 955 |
// Will the given round rect look good if we use HW derivatives? |
| 956 |
bool can_use_hw_derivatives_with_coverage(const GrShaderCaps& shaderCaps, |
| 957 |
const SkMatrix& viewMatrix, |
| 958 |
const SkRRect& rrect) { |
| 959 |
if (!shaderCaps.fShaderDerivativeSupport) { |
| 960 |
return false; |
| 961 |
} |
| 962 |
|
| 963 |
auto x = skvx::float2(viewMatrix.getScaleX(), viewMatrix.getSkewX()); |
| 964 |
auto y = skvx::float2(viewMatrix.getSkewY(), viewMatrix.getScaleY()); |
| 965 |
skvx::float2 devScale = sqrt(x*x + y*y); |
| 966 |
switch (rrect.getType()) { |
| 967 |
case SkRRect::kEmpty_Type: |
| 968 |
case SkRRect::kRect_Type: |
| 969 |
return true; |
| 970 |
|
| 971 |
case SkRRect::kOval_Type: |
| 972 |
case SkRRect::kSimple_Type: |
| 973 |
return can_use_hw_derivatives_with_coverage(devScale, rrect.getSimpleRadii()); |
| 974 |
|
| 975 |
case SkRRect::kNinePatch_Type: { |
| 976 |
skvx::float2 r0 = skvx::float2::Load(SkRRectPriv::GetRadiiArray(rrect)); |
| 977 |
skvx::float2 r1 = skvx::float2::Load(SkRRectPriv::GetRadiiArray(rrect) + 2); |
| 978 |
skvx::float2 minRadii = min(r0, r1); |
| 979 |
skvx::float2 maxRadii = max(r0, r1); |
| 980 |
return can_use_hw_derivatives_with_coverage(devScale, |
| 981 |
skvx::float2(minRadii[0], maxRadii[1])) && |
| 982 |
can_use_hw_derivatives_with_coverage(devScale, |
| 983 |
skvx::float2(maxRadii[0], minRadii[1])); |
| 984 |
} |
| 985 |
|
| 986 |
case SkRRect::kComplex_Type: { |
| 987 |
for (int i = 0; i < 4; ++i) { |
| 988 |
auto corner = static_cast<SkRRect::Corner>(i); |
| 989 |
if (!can_use_hw_derivatives_with_coverage(devScale, rrect.radii(corner))) { |
| 990 |
return false; |
| 991 |
} |
| 992 |
} |
| 993 |
return true; |
| 994 |
} |
| 995 |
} |
| 996 |
SK_ABORT("Invalid round rect type."); |
| 997 |
} |
| 998 |
|
| 999 |
} // anonymous namespace |
| 1000 |
|
| 1001 |
GrOp::Owner Make(GrRecordingContext* ctx, |
| 1002 |
SkArenaAlloc* arena, |
| 1003 |
GrPaint&& paint, |
| 1004 |
const SkMatrix& viewMatrix, |
| 1005 |
const SkRRect& rrect, |
| 1006 |
const SkRect& localRect, |
| 1007 |
GrAA aa) { |
| 1008 |
return FillRRectOpImpl::Make(ctx, arena, std::move(paint), viewMatrix, rrect, localRect, aa); |
| 1009 |
} |
| 1010 |
|
| 1011 |
GrOp::Owner Make(GrRecordingContext* ctx, |
| 1012 |
SkArenaAlloc* arena, |
| 1013 |
GrPaint&& paint, |
| 1014 |
const SkMatrix& viewMatrix, |
| 1015 |
const SkRRect& rrect, |
| 1016 |
const SkMatrix& localMatrix, |
| 1017 |
GrAA aa) { |
| 1018 |
return FillRRectOpImpl::Make(ctx, arena, std::move(paint), viewMatrix, rrect, localMatrix, aa); |
| 1019 |
} |
| 1020 |
|
| 1021 |
} // namespace skgpu::ganesh::FillRRectOp |
| 1022 |
|
| 1023 |
#if defined(GPU_TEST_UTILS) |
| 1024 |
|
| 1025 |
GR_DRAW_OP_TEST_DEFINE(FillRRectOp) { |
| 1026 |
SkArenaAlloc arena(64 * sizeof(float)); |
| 1027 |
SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 1028 |
GrAA aa = GrAA(random->nextBool()); |
| 1029 |
|
| 1030 |
SkRect rect = GrTest::TestRect(random); |
| 1031 |
float w = rect.width(); |
| 1032 |
float h = rect.height(); |
| 1033 |
|
| 1034 |
SkRRect rrect; |
| 1035 |
// TODO: test out other rrect configurations |
| 1036 |
rrect.setNinePatch(rect, w / 3.0f, h / 4.0f, w / 5.0f, h / 6.0); |
| 1037 |
|
| 1038 |
return skgpu::ganesh::FillRRectOp::Make( |
| 1039 |
context, &arena, std::move(paint), viewMatrix, rrect, rrect.rect(), aa); |
| 1040 |
} |
| 1041 |
|
| 1042 |
#endif |