blob: fce38d20d250a5c32f9ad04de8708e36797e2c87 [file] [log] [blame]
Michael Ludwig0f809022019-06-04 09:14:37 -04001/*
2 * Copyright 2019 Google LLC
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/geometry/GrQuadUtils.h"
9
Michael Ludwig61328202019-06-19 14:48:58 +000010#include "include/core/SkRect.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040011#include "include/private/GrTypesPriv.h"
Michael Ludwig61328202019-06-19 14:48:58 +000012#include "include/private/SkVx.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040013#include "src/gpu/geometry/GrQuad.h"
14
Michael Ludwig61328202019-06-19 14:48:58 +000015using V4f = skvx::Vec<4, float>;
16using M4f = skvx::Vec<4, int32_t>;
17
18// Since the local quad may not be type kRect, this uses the opposites for each vertex when
19// interpolating, and calculates new ws in addition to new xs, ys.
20static void interpolate_local(float alpha, int v0, int v1, int v2, int v3,
21 float lx[4], float ly[4], float lw[4]) {
22 SkASSERT(v0 >= 0 && v0 < 4);
23 SkASSERT(v1 >= 0 && v1 < 4);
24 SkASSERT(v2 >= 0 && v2 < 4);
25 SkASSERT(v3 >= 0 && v3 < 4);
26
27 float beta = 1.f - alpha;
28 lx[v0] = alpha * lx[v0] + beta * lx[v2];
29 ly[v0] = alpha * ly[v0] + beta * ly[v2];
30 lw[v0] = alpha * lw[v0] + beta * lw[v2];
31
32 lx[v1] = alpha * lx[v1] + beta * lx[v3];
33 ly[v1] = alpha * ly[v1] + beta * ly[v3];
34 lw[v1] = alpha * lw[v1] + beta * lw[v3];
35}
36
37// Crops v0 to v1 based on the clipDevRect. v2 is opposite of v0, v3 is opposite of v1.
38// It is written to not modify coordinates if there's no intersection along the edge.
39// Ideally this would have been detected earlier and the entire draw is skipped.
40static bool crop_rect_edge(const SkRect& clipDevRect, int v0, int v1, int v2, int v3,
41 float x[4], float y[4], float lx[4], float ly[4], float lw[4]) {
42 SkASSERT(v0 >= 0 && v0 < 4);
43 SkASSERT(v1 >= 0 && v1 < 4);
44 SkASSERT(v2 >= 0 && v2 < 4);
45 SkASSERT(v3 >= 0 && v3 < 4);
46
47 if (SkScalarNearlyEqual(x[v0], x[v1])) {
48 // A vertical edge
49 if (x[v0] < clipDevRect.fLeft && x[v2] >= clipDevRect.fLeft) {
50 // Overlapping with left edge of clipDevRect
51 if (lx) {
52 float alpha = (x[v2] - clipDevRect.fLeft) / (x[v2] - x[v0]);
53 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
54 }
55 x[v0] = clipDevRect.fLeft;
56 x[v1] = clipDevRect.fLeft;
57 return true;
58 } else if (x[v0] > clipDevRect.fRight && x[v2] <= clipDevRect.fRight) {
59 // Overlapping with right edge of clipDevRect
60 if (lx) {
61 float alpha = (clipDevRect.fRight - x[v2]) / (x[v0] - x[v2]);
62 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
63 }
64 x[v0] = clipDevRect.fRight;
65 x[v1] = clipDevRect.fRight;
66 return true;
67 }
68 } else {
69 // A horizontal edge
70 SkASSERT(SkScalarNearlyEqual(y[v0], y[v1]));
71 if (y[v0] < clipDevRect.fTop && y[v2] >= clipDevRect.fTop) {
72 // Overlapping with top edge of clipDevRect
73 if (lx) {
74 float alpha = (y[v2] - clipDevRect.fTop) / (y[v2] - y[v0]);
75 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
76 }
77 y[v0] = clipDevRect.fTop;
78 y[v1] = clipDevRect.fTop;
79 return true;
80 } else if (y[v0] > clipDevRect.fBottom && y[v2] <= clipDevRect.fBottom) {
81 // Overlapping with bottom edge of clipDevRect
82 if (lx) {
83 float alpha = (clipDevRect.fBottom - y[v2]) / (y[v0] - y[v2]);
84 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
85 }
86 y[v0] = clipDevRect.fBottom;
87 y[v1] = clipDevRect.fBottom;
88 return true;
89 }
90 }
91
92 // No overlap so don't crop it
93 return false;
94}
95
96// Updates x and y to intersect with clipDevRect, and applies clipAA policy to edgeFlags for each
97// intersected edge. lx, ly, and lw are updated appropriately and may be null to skip calculations.
98static void crop_rect(const SkRect& clipDevRect, GrAA clipAA, GrQuadAAFlags* edgeFlags,
99 float x[4], float y[4], float lx[4], float ly[4], float lw[4]) {
100 // Filled in as if clipAA were true, will be inverted at the end if needed.
101 GrQuadAAFlags clipEdgeFlags = GrQuadAAFlags::kNone;
102
103 // However, the quad's left edge may not align with the SkRect notion of left due to 90 degree
104 // rotations or mirrors. So, this processes the logical edges of the quad and clamps it to the 4
105 // sides of clipDevRect.
106
107 // Quad's left is v0 to v1 (op. v2 and v3)
108 if (crop_rect_edge(clipDevRect, 0, 1, 2, 3, x, y, lx, ly, lw)) {
109 clipEdgeFlags |= GrQuadAAFlags::kLeft;
110 }
111 // Quad's top edge is v0 to v2 (op. v1 and v3)
112 if (crop_rect_edge(clipDevRect, 0, 2, 1, 3, x, y, lx, ly, lw)) {
113 clipEdgeFlags |= GrQuadAAFlags::kTop;
114 }
115 // Quad's right edge is v2 to v3 (op. v0 and v1)
116 if (crop_rect_edge(clipDevRect, 2, 3, 0, 1, x, y, lx, ly, lw)) {
117 clipEdgeFlags |= GrQuadAAFlags::kRight;
118 }
119 // Quad's bottom edge is v1 to v3 (op. v0 and v2)
120 if (crop_rect_edge(clipDevRect, 1, 3, 0, 2, x, y, lx, ly, lw)) {
121 clipEdgeFlags |= GrQuadAAFlags::kBottom;
122 }
123
124 if (clipAA == GrAA::kYes) {
125 // Turn on all edges that were clipped
126 *edgeFlags |= clipEdgeFlags;
127 } else {
128 // Turn off all edges that were clipped
129 *edgeFlags &= ~clipEdgeFlags;
130 }
131}
132
133// Calculates barycentric coordinates for each point in (testX, testY) in the triangle formed by
134// (x0,y0) - (x1,y1) - (x2, y2) and stores them in u, v, w.
135static void barycentric_coords(float x0, float y0, float x1, float y1, float x2, float y2,
136 const V4f& testX, const V4f& testY,
137 V4f* u, V4f* v, V4f* w) {
138 // Modeled after SkPathOpsQuad::pointInTriangle() but uses float instead of double, is
139 // vectorized and outputs normalized barycentric coordinates instead of inside/outside test
140 float v0x = x2 - x0;
141 float v0y = y2 - y0;
142 float v1x = x1 - x0;
143 float v1y = y1 - y0;
144 V4f v2x = testX - x0;
145 V4f v2y = testY - y0;
146
147 float dot00 = v0x * v0x + v0y * v0y;
148 float dot01 = v0x * v1x + v0y * v1y;
149 V4f dot02 = v0x * v2x + v0y * v2y;
150 float dot11 = v1x * v1x + v1y * v1y;
151 V4f dot12 = v1x * v2x + v1y * v2y;
152 float invDenom = sk_ieee_float_divide(1.f, dot00 * dot11 - dot01 * dot01);
153 *u = (dot11 * dot02 - dot01 * dot12) * invDenom;
154 *v = (dot00 * dot12 - dot01 * dot02) * invDenom;
155 *w = 1.f - *u - *v;
156}
157
158static M4f inside_triangle(const V4f& u, const V4f& v, const V4f& w) {
159 return ((u >= 0.f) & (u <= 1.f)) & ((v >= 0.f) & (v <= 1.f)) & ((w >= 0.f) & (w <= 1.f));
160}
161
Michael Ludwig0f809022019-06-04 09:14:37 -0400162namespace GrQuadUtils {
163
164void ResolveAAType(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, const GrQuad& quad,
165 GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) {
166 // Most cases will keep the requested types unchanged
167 *outAAType = requestedAAType;
168 *outEdgeFlags = requestedEdgeFlags;
169
170 switch (requestedAAType) {
171 // When aa type is coverage, disable AA if the edge configuration doesn't actually need it
172 case GrAAType::kCoverage:
173 if (requestedEdgeFlags == GrQuadAAFlags::kNone) {
174 // Turn off anti-aliasing
175 *outAAType = GrAAType::kNone;
176 } else {
177 // For coverage AA, if the quad is a rect and it lines up with pixel boundaries
178 // then overall aa and per-edge aa can be completely disabled
179 if (quad.quadType() == GrQuad::Type::kAxisAligned && !quad.aaHasEffectOnRect()) {
180 *outAAType = GrAAType::kNone;
181 *outEdgeFlags = GrQuadAAFlags::kNone;
182 }
183 }
184 break;
185 // For no or msaa anti aliasing, override the edge flags since edge flags only make sense
186 // when coverage aa is being used.
187 case GrAAType::kNone:
188 *outEdgeFlags = GrQuadAAFlags::kNone;
189 break;
190 case GrAAType::kMSAA:
191 *outEdgeFlags = GrQuadAAFlags::kAll;
192 break;
193 case GrAAType::kMixedSamples:
194 SK_ABORT("Should not use mixed sample AA with edge AA flags");
195 break;
196 }
Michael Ludwig61328202019-06-19 14:48:58 +0000197}
198
199bool CropToRect(const SkRect& cropRect, GrAA cropAA, GrQuadAAFlags* edgeFlags, GrQuad* quad,
200 GrQuad* local) {
201 if (quad->quadType() == GrQuad::Type::kAxisAligned) {
202 // crop_rect keeps the rectangles as rectangles, so there's no need to modify types
203 if (local) {
204 crop_rect(cropRect, cropAA, edgeFlags, quad->xs(), quad->ys(),
205 local->xs(), local->ys(), local->ws());
206 } else {
207 crop_rect(cropRect, cropAA, edgeFlags, quad->xs(), quad->ys(),
208 nullptr, nullptr, nullptr);
209 }
210 return true;
211 }
212
213 if (local) {
214 // FIXME (michaelludwig) Calculate cropped local coordinates when not kAxisAligned
215 return false;
216 }
217
218 V4f devX = quad->x4f();
219 V4f devY = quad->y4f();
220 V4f devIW = quad->iw4f();
221 // Project the 3D coordinates to 2D
222 if (quad->quadType() == GrQuad::Type::kPerspective) {
223 devX *= devIW;
224 devY *= devIW;
225 }
226
227 V4f clipX = {cropRect.fLeft, cropRect.fLeft, cropRect.fRight, cropRect.fRight};
228 V4f clipY = {cropRect.fTop, cropRect.fBottom, cropRect.fTop, cropRect.fBottom};
229
230 // Calculate barycentric coordinates for the 4 rect corners in the 2 triangles that the quad
231 // is tessellated into when drawn.
232 V4f u1, v1, w1;
233 barycentric_coords(devX[0], devY[0], devX[1], devY[1], devX[2], devY[2], clipX, clipY,
234 &u1, &v1, &w1);
235 V4f u2, v2, w2;
236 barycentric_coords(devX[1], devY[1], devX[3], devY[3], devX[2], devY[2], clipX, clipY,
237 &u2, &v2, &w2);
238
239 // clipDevRect is completely inside this quad if each corner is in at least one of two triangles
240 M4f inTri1 = inside_triangle(u1, v1, w1);
241 M4f inTri2 = inside_triangle(u2, v2, w2);
242 if (all(inTri1 | inTri2)) {
243 // We can crop to exactly the clipDevRect.
244 // FIXME (michaelludwig) - there are other ways to have determined quad covering the clip
245 // rect, but the barycentric coords will be useful to derive local coordinates in the future
246
247 // Since we are cropped to exactly clipDevRect, we have discarded any perspective and the
248 // type becomes kRect. If updated locals were requested, they will incorporate perspective.
249 // FIXME (michaelludwig) - once we have local coordinates handled, it may be desirable to
250 // keep the draw as perspective so that the hardware does perspective interpolation instead
251 // of pushing it into a local coord w and having the shader do an extra divide.
252 clipX.store(quad->xs());
253 clipY.store(quad->ys());
254 quad->ws()[0] = 1.f;
255 quad->ws()[1] = 1.f;
256 quad->ws()[2] = 1.f;
257 quad->ws()[3] = 1.f;
258 quad->setQuadType(GrQuad::Type::kAxisAligned);
259
260 // Update the edge flags to match the clip setting since all 4 edges have been clipped
261 *edgeFlags = cropAA == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
262
263 return true;
264 }
265
266 // FIXME (michaelludwig) - use the GrQuadPerEdgeAA tessellation inset/outset math to move
267 // edges to the closest clip corner they are outside of
268
269 return false;
270}
Michael Ludwig0f809022019-06-04 09:14:37 -0400271
272}; // namespace GrQuadUtils