• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef UTIL_BOX_INLINES_H
2 #define UTIL_BOX_INLINES_H
3 
4 #include "pipe/p_state.h"
5 #include "util/u_math.h"
6 
7 static inline void
u_box_1d(unsigned x,unsigned w,struct pipe_box * box)8 u_box_1d(unsigned x, unsigned w, struct pipe_box *box)
9 {
10    box->x = x;
11    box->y = 0;
12    box->z = 0;
13    box->width = w;
14    box->height = 1;
15    box->depth = 1;
16 }
17 
18 static inline void
u_box_2d(unsigned x,unsigned y,unsigned w,unsigned h,struct pipe_box * box)19 u_box_2d(unsigned x,unsigned y, unsigned w, unsigned h, struct pipe_box *box)
20 {
21    box->x = x;
22    box->y = y;
23    box->z = 0;
24    box->width = w;
25    box->height = h;
26    box->depth = 1;
27 }
28 
29 static inline void
u_box_origin_2d(unsigned w,unsigned h,struct pipe_box * box)30 u_box_origin_2d(unsigned w, unsigned h, struct pipe_box *box)
31 {
32    box->x = 0;
33    box->y = 0;
34    box->z = 0;
35    box->width = w;
36    box->height = h;
37    box->depth = 1;
38 }
39 
40 static inline void
u_box_2d_zslice(unsigned x,unsigned y,unsigned z,unsigned w,unsigned h,struct pipe_box * box)41 u_box_2d_zslice(unsigned x, unsigned y, unsigned z,
42                 unsigned w, unsigned h, struct pipe_box *box)
43 {
44    box->x = x;
45    box->y = y;
46    box->z = z;
47    box->width = w;
48    box->height = h;
49    box->depth = 1;
50 }
51 
52 static inline void
u_box_3d(unsigned x,unsigned y,unsigned z,unsigned w,unsigned h,unsigned d,struct pipe_box * box)53 u_box_3d(unsigned x, unsigned y, unsigned z,
54          unsigned w, unsigned h, unsigned d,
55          struct pipe_box *box)
56 {
57    box->x = x;
58    box->y = y;
59    box->z = z;
60    box->width = w;
61    box->height = h;
62    box->depth = d;
63 }
64 
65 /* Clips @dst to width @w and height @h.
66  * Returns -1 if the resulting box would be empty (then @dst is left unchanged).
67  *          0 if nothing has been reduced.
68  *          1 if width has been reduced.
69  *          2 if height has been reduced.
70  *          3 if both width and height have been reduced.
71  * Aliasing permitted.
72  */
73 static inline int
u_box_clip_2d(struct pipe_box * dst,const struct pipe_box * box,int w,int h)74 u_box_clip_2d(struct pipe_box *dst,
75               const struct pipe_box *box, int w, int h)
76 {
77    unsigned i;
78    int a[2], b[2], dim[2];
79    int *start, *end;
80    int res = 0;
81 
82    if (!box->width || !box->height)
83       return -1;
84    dim[0] = w;
85    dim[1] = h;
86    a[0] = box->x;
87    a[1] = box->y;
88    b[0] = box->x + box->width;
89    b[1] = box->y + box->height;
90 
91    for (i = 0; i < 2; ++i) {
92       start = (a[i] <= b[i]) ? &a[i] : &b[i];
93       end = (a[i] <= b[i]) ? &b[i] : &a[i];
94 
95       if (*end < 0 || *start >= dim[i])
96          return -1;
97       if (*start < 0) {
98          *start = 0;
99          res |= (1 << i);
100       }
101       if (*end > dim[i]) {
102          *end = dim[i];
103          res |= (1 << i);
104       }
105    }
106 
107    if (res) {
108       dst->x = a[0];
109       dst->y = a[1];
110       dst->width = b[0] - a[0];
111       dst->height = b[1] - a[1];
112    }
113    return res;
114 }
115 
116 static inline int64_t
u_box_volume_3d(const struct pipe_box * box)117 u_box_volume_3d(const struct pipe_box *box)
118 {
119    return (int64_t)box->width * box->height * box->depth;
120 }
121 
122 /* Aliasing of @dst permitted. Supports empty width */
123 static inline void
u_box_union_1d(struct pipe_box * dst,const struct pipe_box * a,const struct pipe_box * b)124 u_box_union_1d(struct pipe_box *dst,
125                const struct pipe_box *a, const struct pipe_box *b)
126 {
127    int x, width;
128 
129    if (a->width == 0) {
130        x = b->x;
131        width = b->width;
132    } else if (b->width == 0) {
133        x = a->x;
134        width = a->width;
135    } else {
136        x = MIN2(a->x, b->x);
137        width = MAX2(a->x + a->width, b->x + b->width) - x;
138    }
139 
140    dst->x = x;
141    dst->width = width;
142 }
143 
144 /* Aliasing of @dst permitted. */
145 static inline void
u_box_intersect_1d(struct pipe_box * dst,const struct pipe_box * a,const struct pipe_box * b)146 u_box_intersect_1d(struct pipe_box *dst,
147                const struct pipe_box *a, const struct pipe_box *b)
148 {
149    int x;
150 
151    x = MAX2(a->x, b->x);
152 
153    dst->width = MIN2(a->x + a->width, b->x + b->width) - x;
154    dst->x = x;
155    if (dst->width <= 0) {
156       dst->x = 0;
157       dst->width = 0;
158    }
159 }
160 
161 /* Aliasing of @dst permitted. */
162 static inline void
u_box_union_2d(struct pipe_box * dst,const struct pipe_box * a,const struct pipe_box * b)163 u_box_union_2d(struct pipe_box *dst,
164                const struct pipe_box *a, const struct pipe_box *b)
165 {
166    int x, y;
167 
168    x = MIN2(a->x, b->x);
169    y = MIN2(a->y, b->y);
170 
171    dst->width = MAX2(a->x + a->width, b->x + b->width) - x;
172    dst->height = MAX2(a->y + a->height, b->y + b->height) - y;
173    dst->x = x;
174    dst->y = y;
175 }
176 
177 /* Aliasing of @dst permitted. */
178 static inline void
u_box_union_3d(struct pipe_box * dst,const struct pipe_box * a,const struct pipe_box * b)179 u_box_union_3d(struct pipe_box *dst,
180                const struct pipe_box *a, const struct pipe_box *b)
181 {
182    int x, y, z;
183 
184    x = MIN2(a->x, b->x);
185    y = MIN2(a->y, b->y);
186    z = MIN2(a->z, b->z);
187 
188    dst->width = MAX2(a->x + a->width, b->x + b->width) - x;
189    dst->height = MAX2(a->y + a->height, b->y + b->height) - y;
190    dst->depth = MAX2(a->z + a->depth, b->z + b->depth) - z;
191    dst->x = x;
192    dst->y = y;
193    dst->z = z;
194 }
195 
196 static inline boolean
u_box_test_intersection_2d(const struct pipe_box * a,const struct pipe_box * b)197 u_box_test_intersection_2d(const struct pipe_box *a,
198                            const struct pipe_box *b)
199 {
200    unsigned i;
201    int a_l[2], a_r[2], b_l[2], b_r[2];
202 
203    a_l[0] = MIN2(a->x, a->x + a->width);
204    a_r[0] = MAX2(a->x, a->x + a->width);
205    a_l[1] = MIN2(a->y, a->y + a->height);
206    a_r[1] = MAX2(a->y, a->y + a->height);
207 
208    b_l[0] = MIN2(b->x, b->x + b->width);
209    b_r[0] = MAX2(b->x, b->x + b->width);
210    b_l[1] = MIN2(b->y, b->y + b->height);
211    b_r[1] = MAX2(b->y, b->y + b->height);
212 
213    for (i = 0; i < 2; ++i) {
214       if (a_l[i] > b_r[i] || a_r[i] < b_l[i])
215          return FALSE;
216    }
217    return TRUE;
218 }
219 
220 static inline void
u_box_minify_2d(struct pipe_box * dst,const struct pipe_box * src,unsigned l)221 u_box_minify_2d(struct pipe_box *dst,
222                 const struct pipe_box *src, unsigned l)
223 {
224    dst->x = src->x >> l;
225    dst->y = src->y >> l;
226    dst->width = MAX2(src->width >> l, 1);
227    dst->height = MAX2(src->height >> l, 1);
228 }
229 
230 static inline void
u_box_minify_3d(struct pipe_box * dst,const struct pipe_box * src,unsigned l)231 u_box_minify_3d(struct pipe_box *dst,
232                 const struct pipe_box *src, unsigned l)
233 {
234    dst->x = src->x >> l;
235    dst->y = src->y >> l;
236    dst->z = src->z >> l;
237    dst->width = MAX2(src->width >> l, 1);
238    dst->height = MAX2(src->height >> l, 1);
239    dst->depth = MAX2(src->depth >> l, 1);
240 }
241 
242 #endif
243