• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #ifndef tessellate_CullTest_DEFINED
9 #define tessellate_CullTest_DEFINED
10 
11 #include "include/core/SkMatrix.h"
12 #include "src/gpu/tessellate/Tessellation.h"
13 
14 namespace skgpu {
15 
16 // This class determines whether the given local-space points will be contained in the cull bounds
17 // post transform. For the versions that take >1 point, it returns whether any region of their
18 // device-space bounding box will be in the cull bounds.
19 //
20 // NOTE: Our view matrix is not a normal matrix. M*p maps to the float4 [x, y, -x, -y] in device
21 // space. We do this to aid in quick bounds calculations. The matrix also does not have a
22 // translation element. Instead we unapply the translation to the cull bounds ahead of time.
23 class CullTest {
24 public:
25     CullTest() = default;
26 
CullTest(const SkRect & devCullBounds,const SkMatrix & m)27     CullTest(const SkRect& devCullBounds, const SkMatrix& m) {
28         this->set(devCullBounds, m);
29     }
30 
set(const SkRect & devCullBounds,const SkMatrix & m)31     void set(const SkRect& devCullBounds, const SkMatrix& m) {
32         SkASSERT(!m.hasPerspective());
33         // [fMatX, fMatY] maps path coordinates to the float4 [x, y, -x, -y] in device space.
34         fMatX = {m.getScaleX(), m.getSkewY(), -m.getScaleX(), -m.getSkewY()};
35         fMatY = {m.getSkewX(), m.getScaleY(), -m.getSkewX(), -m.getScaleY()};
36         // Store the cull bounds as [l, t, -r, -b] for faster math.
37         // Also subtract the matrix translate from the cull bounds ahead of time, rather than adding
38         // it to every point every time we test.
39         fCullBounds = {devCullBounds.fLeft - m.getTranslateX(),
40                        devCullBounds.fTop - m.getTranslateY(),
41                        m.getTranslateX() - devCullBounds.fRight,
42                        m.getTranslateY() - devCullBounds.fBottom};
43     }
44 
45     // Returns whether M*p will be in the viewport.
isVisible(SkPoint p)46     bool isVisible(SkPoint p) const {
47         // devPt = [x, y, -x, -y] in device space.
48         auto devPt = fMatX*p.fX + fMatY*p.fY;
49         // i.e., l < x && t < y && r > x && b > y.
50         return skvx::all(fCullBounds < devPt);
51     }
52 
53     // Returns whether any region of the bounding box of M * p0..2 will be in the viewport.
areVisible3(const SkPoint p[3])54     bool areVisible3(const SkPoint p[3]) const {
55         // Transform p0..2 to device space.
56         auto val0 = fMatY * p[0].fY;
57         auto val1 = fMatY * p[1].fY;
58         auto val2 = fMatY * p[2].fY;
59         val0 = fMatX*p[0].fX + val0;
60         val1 = fMatX*p[1].fX + val1;
61         val2 = fMatX*p[2].fX + val2;
62         // At this point: valN = {xN, yN, -xN, -yN} in device space.
63 
64         // Find the device-space bounding box of p0..2.
65         val0 = skvx::max(val0, val1);
66         val0 = skvx::max(val0, val2);
67         // At this point: val0 = [r, b, -l, -t] of the device-space bounding box of p0..2.
68 
69         // Does fCullBounds intersect the device-space bounding box of p0..2?
70         // i.e., l0 < r1 && t0 < b1 && r0 > l1 && b0 > t1.
71         return skvx::all(fCullBounds < val0);
72     }
73 
74     // Returns whether any region of the bounding box of M * p0..3 will be in the viewport.
areVisible4(const SkPoint p[4])75     bool areVisible4(const SkPoint p[4]) const {
76         // Transform p0..3 to device space.
77         auto val0 = fMatY * p[0].fY;
78         auto val1 = fMatY * p[1].fY;
79         auto val2 = fMatY * p[2].fY;
80         auto val3 = fMatY * p[3].fY;
81         val0 = fMatX*p[0].fX + val0;
82         val1 = fMatX*p[1].fX + val1;
83         val2 = fMatX*p[2].fX + val2;
84         val3 = fMatX*p[3].fX + val3;
85         // At this point: valN = {xN, yN, -xN, -yN} in device space.
86 
87         // Find the device-space bounding box of p0..3.
88         val0 = skvx::max(val0, val1);
89         val2 = skvx::max(val2, val3);
90         val0 = skvx::max(val0, val2);
91         // At this point: val0 = [r, b, -l, -t] of the device-space bounding box of p0..3.
92 
93         // Does fCullBounds intersect the device-space bounding box of p0..3?
94         // i.e., l0 < r1 && t0 < b1 && r0 > l1 && b0 > t1.
95         return skvx::all(fCullBounds < val0);
96     }
97 
98 private:
99     // [fMatX, fMatY] maps path coordinates to the float4 [x, y, -x, -y] in device space.
100     float4 fMatX;
101     float4 fMatY;
102     float4 fCullBounds;  // [l, t, -r, -b]
103 };
104 
105 }  // namespace skgpu
106 
107 #endif  // tessellate_CullTest_DEFINED
108