dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrAnalyticRectBatch.h" |
| 9 | |
| 10 | #include "GrBatchFlushState.h" |
| 11 | #include "GrBatchTest.h" |
| 12 | #include "GrGeometryProcessor.h" |
| 13 | #include "GrInvariantOutput.h" |
| 14 | #include "GrProcessor.h" |
| 15 | #include "GrResourceProvider.h" |
| 16 | #include "SkRRect.h" |
| 17 | #include "SkStrokeRec.h" |
| 18 | #include "batches/GrVertexBatch.h" |
| 19 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 20 | #include "glsl/GrGLSLGeometryProcessor.h" |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 21 | #include "glsl/GrGLSLGeometryProcessor.h" |
dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 22 | #include "glsl/GrGLSLProgramDataManager.h" |
| 23 | #include "glsl/GrGLSLVarying.h" |
| 24 | #include "glsl/GrGLSLVertexShaderBuilder.h" |
| 25 | #include "glsl/GrGLSLUniformHandler.h" |
| 26 | #include "glsl/GrGLSLUtil.h" |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | struct RectVertex { |
| 31 | SkPoint fPos; |
| 32 | GrColor fColor; |
| 33 | SkPoint fCenter; |
| 34 | SkVector fDownDir; |
| 35 | SkScalar fHalfWidth; |
| 36 | SkScalar fHalfHeight; |
| 37 | }; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | /** |
| 44 | * The output of this effect is the input color and coverage for an arbitrarily oriented rect. The |
| 45 | * rect is specified as: |
| 46 | * Center of the rect |
| 47 | * Unit vector point down the height of the rect |
| 48 | * Half width + 0.5 |
| 49 | * Half height + 0.5 |
| 50 | * The center and vector are stored in a vec4 varying ("RectEdge") with the |
| 51 | * center in the xy components and the vector in the zw components. |
| 52 | * The munged width and height are stored in a vec2 varying ("WidthHeight") |
| 53 | * with the width in x and the height in y. |
| 54 | */ |
| 55 | class RectGeometryProcessor : public GrGeometryProcessor { |
| 56 | public: |
| 57 | RectGeometryProcessor(const SkMatrix& localMatrix) : fLocalMatrix(localMatrix) { |
| 58 | this->initClassID<RectGeometryProcessor>(); |
bsalomon | 6cb807b | 2016-08-17 11:33:39 -0700 | [diff] [blame] | 59 | fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType, |
| 60 | kHigh_GrSLPrecision); |
| 61 | fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType); |
| 62 | fInRectEdge = &this->addVertexAttrib("inRectEdge", kVec4f_GrVertexAttribType); |
| 63 | fInWidthHeight = &this->addVertexAttrib("inWidthHeight", kVec2f_GrVertexAttribType); |
dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool implementsDistanceVector() const override { return true; }; |
| 67 | |
| 68 | const Attribute* inPosition() const { return fInPosition; } |
| 69 | const Attribute* inColor() const { return fInColor; } |
| 70 | const Attribute* inRectEdge() const { return fInRectEdge; } |
| 71 | const Attribute* inWidthHeight() const { return fInWidthHeight; } |
| 72 | |
| 73 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
| 74 | |
| 75 | virtual ~RectGeometryProcessor() {} |
| 76 | |
| 77 | const char* name() const override { return "RectEdge"; } |
| 78 | |
| 79 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 80 | public: |
| 81 | GLSLProcessor() {} |
| 82 | |
| 83 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override{ |
| 84 | const RectGeometryProcessor& rgp = args.fGP.cast<RectGeometryProcessor>(); |
| 85 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
| 86 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 87 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 88 | |
| 89 | // emit attributes |
| 90 | varyingHandler->emitAttributes(rgp); |
| 91 | |
| 92 | // setup the varying for the position |
| 93 | GrGLSLVertToFrag positionVary(kVec2f_GrSLType); |
| 94 | varyingHandler->addVarying("Position", &positionVary); |
| 95 | vertBuilder->codeAppendf("%s = %s;", positionVary.vsOut(), rgp.inPosition()->fName); |
| 96 | |
| 97 | // setup the varying for the center point and the unit vector that points down the |
| 98 | // height of the rect |
| 99 | GrGLSLVertToFrag rectEdgeVary(kVec4f_GrSLType); |
| 100 | varyingHandler->addVarying("RectEdge", &rectEdgeVary); |
| 101 | vertBuilder->codeAppendf("%s = %s;", rectEdgeVary.vsOut(), rgp.inRectEdge()->fName); |
| 102 | |
| 103 | // setup the varying for the width/2+.5 and height/2+.5 |
| 104 | GrGLSLVertToFrag widthHeightVary(kVec2f_GrSLType); |
| 105 | varyingHandler->addVarying("WidthHeight", &widthHeightVary); |
| 106 | vertBuilder->codeAppendf("%s = %s;", |
| 107 | widthHeightVary.vsOut(), rgp.inWidthHeight()->fName); |
| 108 | |
| 109 | GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 110 | |
| 111 | // setup pass through color |
| 112 | varyingHandler->addPassThroughAttribute(rgp.inColor(), args.fOutputColor); |
| 113 | |
| 114 | // Setup position |
| 115 | this->setupPosition(vertBuilder, gpArgs, rgp.inPosition()->fName); |
| 116 | |
| 117 | // emit transforms |
| 118 | this->emitTransforms(vertBuilder, |
| 119 | varyingHandler, |
| 120 | uniformHandler, |
| 121 | gpArgs->fPositionVar, |
| 122 | rgp.inPosition()->fName, |
| 123 | rgp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 124 | args.fFPCoordTransformHandler); |
dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 125 | |
| 126 | // TODO: compute all these offsets, spans, and scales in the VS |
| 127 | fragBuilder->codeAppendf("float insetW = min(1.0, %s.x) - 0.5;", |
| 128 | widthHeightVary.fsIn()); |
| 129 | fragBuilder->codeAppendf("float insetH = min(1.0, %s.y) - 0.5;", |
| 130 | widthHeightVary.fsIn()); |
| 131 | fragBuilder->codeAppend("float outset = 0.5;"); |
| 132 | // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0). For rects |
| 133 | // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a 0 .. 1 range. |
| 134 | fragBuilder->codeAppend("float spanW = insetW + outset;"); |
| 135 | fragBuilder->codeAppend("float spanH = insetH + outset;"); |
| 136 | // For rects < 1 pixel wide or tall, these scale factors are used to cap the maximum |
| 137 | // value of coverage that is used. In other words it is the coverage that is |
| 138 | // used in the interior of the rect after the ramp. |
| 139 | fragBuilder->codeAppend("float scaleW = min(1.0, 2.0*insetW/spanW);"); |
| 140 | fragBuilder->codeAppend("float scaleH = min(1.0, 2.0*insetH/spanH);"); |
| 141 | // Compute the coverage for the rect's width |
| 142 | fragBuilder->codeAppendf("vec2 offset = %s.xy - %s.xy;", |
| 143 | positionVary.fsIn(), rectEdgeVary.fsIn()); |
| 144 | fragBuilder->codeAppendf("float perpDot = abs(offset.x * %s.w - offset.y * %s.z);", |
| 145 | rectEdgeVary.fsIn(), rectEdgeVary.fsIn()); |
| 146 | |
| 147 | if (args.fDistanceVectorName) { |
| 148 | fragBuilder->codeAppendf("float widthDistance = %s.x - perpDot;", |
| 149 | widthHeightVary.fsIn()); |
| 150 | } |
| 151 | |
| 152 | fragBuilder->codeAppendf( |
| 153 | "float coverage = scaleW*clamp((%s.x-perpDot)/spanW, 0.0, 1.0);", |
| 154 | widthHeightVary.fsIn()); |
| 155 | // Compute the coverage for the rect's height and merge with the width |
| 156 | fragBuilder->codeAppendf("perpDot = abs(dot(offset, %s.zw));", |
| 157 | rectEdgeVary.fsIn()); |
| 158 | |
| 159 | if (args.fDistanceVectorName) { |
| 160 | fragBuilder->codeAppendf("float heightDistance = %s.y - perpDot;", |
| 161 | widthHeightVary.fsIn()); |
| 162 | } |
| 163 | |
| 164 | fragBuilder->codeAppendf( |
| 165 | "coverage = coverage*scaleH*clamp((%s.y-perpDot)/spanH, 0.0, 1.0);", |
| 166 | widthHeightVary.fsIn()); |
| 167 | |
| 168 | fragBuilder->codeAppendf("%s = vec4(coverage);", args.fOutputCoverage); |
| 169 | |
| 170 | if (args.fDistanceVectorName) { |
| 171 | fragBuilder->codeAppend( "// Calculating distance vector\n"); |
| 172 | fragBuilder->codeAppend( "vec2 dvAxis;"); |
| 173 | fragBuilder->codeAppend( "float dvLength;"); |
| 174 | |
| 175 | fragBuilder->codeAppend( "if (heightDistance < widthDistance) {"); |
| 176 | fragBuilder->codeAppendf(" dvAxis = %s.zw;", rectEdgeVary.fsIn()); |
| 177 | fragBuilder->codeAppend( " dvLength = heightDistance;"); |
| 178 | fragBuilder->codeAppend( "} else {"); |
| 179 | fragBuilder->codeAppendf(" dvAxis = vec2(-%s.w, %s.z);", |
| 180 | rectEdgeVary.fsIn(), rectEdgeVary.fsIn()); |
| 181 | fragBuilder->codeAppend( " dvLength = widthDistance;"); |
| 182 | fragBuilder->codeAppend( "}"); |
| 183 | |
| 184 | fragBuilder->codeAppend( "float dvSign = sign(dot(offset, dvAxis));"); |
jvanverth | 6c177a1 | 2016-08-17 07:59:41 -0700 | [diff] [blame] | 185 | fragBuilder->codeAppendf("%s = vec4(dvSign * dvAxis, dvLength, 0.0);", |
dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 186 | args.fDistanceVectorName); |
| 187 | |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static void GenKey(const GrGeometryProcessor& gp, |
| 192 | const GrGLSLCaps&, |
| 193 | GrProcessorKeyBuilder* b) { |
| 194 | b->add32(0x0); |
| 195 | } |
| 196 | |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 197 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 198 | FPCoordTransformIter&& transformIter) override { |
| 199 | const RectGeometryProcessor& rgp = primProc.cast<RectGeometryProcessor>(); |
| 200 | this->setTransformDataHelper(rgp.fLocalMatrix, pdman,&transformIter); |
dvonbeck | 09e12a6 | 2016-08-12 12:50:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private: |
| 204 | typedef GrGLSLGeometryProcessor INHERITED; |
| 205 | }; |
| 206 | |
| 207 | void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 208 | GLSLProcessor::GenKey(*this, caps, b); |
| 209 | } |
| 210 | |
| 211 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override { |
| 212 | return new GLSLProcessor(); |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | SkMatrix fLocalMatrix; |
| 217 | |
| 218 | const Attribute* fInPosition; |
| 219 | const Attribute* fInColor; |
| 220 | const Attribute* fInRectEdge; |
| 221 | const Attribute* fInWidthHeight; |
| 222 | |
| 223 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
| 224 | |
| 225 | typedef GrGeometryProcessor INHERITED; |
| 226 | }; |
| 227 | |
| 228 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(RectGeometryProcessor); |
| 229 | |
| 230 | sk_sp<GrGeometryProcessor> RectGeometryProcessor::TestCreate(GrProcessorTestData* d) { |
| 231 | return sk_sp<GrGeometryProcessor>( |
| 232 | new RectGeometryProcessor(GrTest::TestMatrix(d->fRandom))); |
| 233 | } |
| 234 | |
| 235 | /////////////////////////////////////////////////////////////////////////////// |
| 236 | |
| 237 | class AnalyticRectBatch : public GrVertexBatch { |
| 238 | public: |
| 239 | DEFINE_BATCH_CLASS_ID |
| 240 | |
| 241 | AnalyticRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 242 | const SkRect& croppedRect, const SkRect& bounds) |
| 243 | : INHERITED(ClassID()) |
| 244 | , fViewMatrixIfUsingLocalCoords(viewMatrix) { |
| 245 | SkPoint center = SkPoint::Make(rect.centerX(), rect.centerY()); |
| 246 | viewMatrix.mapPoints(¢er, 1); |
| 247 | SkScalar halfWidth = viewMatrix.mapRadius(SkScalarHalf(rect.width())); |
| 248 | SkScalar halfHeight = viewMatrix.mapRadius(SkScalarHalf(rect.height())); |
| 249 | SkVector downDir = viewMatrix.mapVector(0.0f, 1.0f); |
| 250 | downDir.normalize(); |
| 251 | |
| 252 | SkRect deviceSpaceCroppedRect = croppedRect; |
| 253 | viewMatrix.mapRect(&deviceSpaceCroppedRect); |
| 254 | |
| 255 | fGeoData.emplace_back(Geometry {color, center, downDir, halfWidth, halfHeight, |
| 256 | deviceSpaceCroppedRect}); |
| 257 | |
| 258 | this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo); |
| 259 | } |
| 260 | |
| 261 | const char* name() const override { return "AnalyticRectBatch"; } |
| 262 | |
| 263 | SkString dumpInfo() const override { |
| 264 | SkString string; |
| 265 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 266 | string.appendf("Color: 0x%08x Rect [C:(%.2f, %.2f) D:<%.2f,%.3f> W/2:%.2f H/2:%.2f]\n", |
| 267 | fGeoData[i].fColor, |
| 268 | fGeoData[i].fCenter.x(), fGeoData[i].fCenter.y(), |
| 269 | fGeoData[i].fDownDir.x(), fGeoData[i].fDownDir.y(), |
| 270 | fGeoData[i].fHalfWidth, |
| 271 | fGeoData[i].fHalfHeight); |
| 272 | } |
| 273 | string.append(INHERITED::dumpInfo()); |
| 274 | return string; |
| 275 | } |
| 276 | |
| 277 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 278 | GrInitInvariantOutput* coverage, |
| 279 | GrBatchToXPOverrides* overrides) const override { |
| 280 | // When this is called on a batch, there is only one geometry bundle |
| 281 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 282 | coverage->setUnknownSingleComponent(); |
| 283 | } |
| 284 | |
| 285 | private: |
| 286 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 287 | // Handle any overrides that affect our GP. |
| 288 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 289 | if (!overrides.readsLocalCoords()) { |
| 290 | fViewMatrixIfUsingLocalCoords.reset(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void onPrepareDraws(Target* target) const override { |
| 295 | SkMatrix localMatrix; |
| 296 | if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | // Setup geometry processor |
| 301 | SkAutoTUnref<GrGeometryProcessor> gp(new RectGeometryProcessor(localMatrix)); |
| 302 | |
| 303 | int instanceCount = fGeoData.count(); |
| 304 | size_t vertexStride = gp->getVertexStride(); |
| 305 | SkASSERT(vertexStride == sizeof(RectVertex)); |
| 306 | QuadHelper helper; |
| 307 | RectVertex* verts = reinterpret_cast<RectVertex*>(helper.init(target, vertexStride, |
| 308 | instanceCount)); |
| 309 | if (!verts) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | for (int i = 0; i < instanceCount; i++) { |
| 314 | const Geometry& geom = fGeoData[i]; |
| 315 | |
| 316 | GrColor color = geom.fColor; |
| 317 | SkPoint center = geom.fCenter; |
| 318 | SkVector downDir = geom.fDownDir; |
| 319 | SkScalar halfWidth = geom.fHalfWidth; |
| 320 | SkScalar halfHeight = geom.fHalfHeight; |
| 321 | SkRect croppedRect = geom.fCroppedRect; |
| 322 | |
| 323 | SkVector rightDir; |
| 324 | downDir.rotateCCW(&rightDir); |
| 325 | |
| 326 | verts[0].fPos = {croppedRect.fLeft, croppedRect.fTop}; |
| 327 | verts[0].fColor = color; |
| 328 | verts[0].fCenter = center; |
| 329 | verts[0].fDownDir = downDir; |
| 330 | verts[0].fHalfWidth = halfWidth; |
| 331 | verts[0].fHalfHeight = halfHeight; |
| 332 | |
| 333 | verts[1].fPos = {croppedRect.fRight, croppedRect.fTop}; |
| 334 | verts[1].fColor = color; |
| 335 | verts[1].fCenter = center; |
| 336 | verts[1].fDownDir = downDir; |
| 337 | verts[1].fHalfWidth = halfWidth; |
| 338 | verts[1].fHalfHeight = halfHeight; |
| 339 | |
| 340 | verts[2].fPos = {croppedRect.fRight, croppedRect.fBottom}; |
| 341 | verts[2].fColor = color; |
| 342 | verts[2].fCenter = center; |
| 343 | verts[2].fDownDir = downDir; |
| 344 | verts[2].fHalfWidth = halfWidth; |
| 345 | verts[2].fHalfHeight = halfHeight; |
| 346 | |
| 347 | verts[3].fPos = {croppedRect.fLeft, croppedRect.fBottom}; |
| 348 | verts[3].fColor = color; |
| 349 | verts[3].fCenter = center; |
| 350 | verts[3].fDownDir = downDir; |
| 351 | verts[3].fHalfWidth = halfWidth; |
| 352 | verts[3].fHalfHeight = halfHeight; |
| 353 | |
| 354 | verts += kVerticesPerQuad; |
| 355 | } |
| 356 | helper.recordDraw(target, gp); |
| 357 | } |
| 358 | |
| 359 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 360 | AnalyticRectBatch* that = t->cast<AnalyticRectBatch>(); |
| 361 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 362 | that->bounds(), caps)) { |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
| 371 | this->joinBounds(*that); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | struct Geometry { |
| 376 | GrColor fColor; |
| 377 | SkPoint fCenter; |
| 378 | SkVector fDownDir; |
| 379 | SkScalar fHalfWidth; |
| 380 | SkScalar fHalfHeight; |
| 381 | SkRect fCroppedRect; |
| 382 | }; |
| 383 | |
| 384 | SkMatrix fViewMatrixIfUsingLocalCoords; |
| 385 | SkSTArray<1, Geometry, true> fGeoData; |
| 386 | |
| 387 | typedef GrVertexBatch INHERITED; |
| 388 | }; |
| 389 | |
| 390 | GrDrawBatch* GrAnalyticRectBatch::CreateAnalyticRectBatch(GrColor color, |
| 391 | const SkMatrix& viewMatrix, |
| 392 | const SkRect& rect, |
| 393 | const SkRect& croppedRect, |
| 394 | const SkRect& bounds) { |
| 395 | return new AnalyticRectBatch(color, viewMatrix, rect, croppedRect, bounds); |
| 396 | } |
| 397 | |
| 398 | #ifdef GR_TEST_UTILS |
| 399 | |
| 400 | DRAW_BATCH_TEST_DEFINE(AnalyticRectBatch) { |
| 401 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 402 | GrColor color = GrRandomColor(random); |
| 403 | SkRect rect = GrTest::TestSquare(random); |
| 404 | SkRect croppedRect = GrTest::TestSquare(random); |
| 405 | SkRect bounds = GrTest::TestSquare(random); |
| 406 | return new AnalyticRectBatch(color, viewMatrix, rect, croppedRect, bounds); |
| 407 | } |
| 408 | |
| 409 | #endif |