st/mesa: add a fallback for clear_with_quad when no vs_layer
[mesa.git] / src / gallium / auxiliary / util / u_box.h
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
8 void u_box_1d( unsigned x,
9 unsigned w,
10 struct pipe_box *box )
11 {
12 box->x = x;
13 box->y = 0;
14 box->z = 0;
15 box->width = w;
16 box->height = 1;
17 box->depth = 1;
18 }
19
20 static INLINE
21 void u_box_2d( unsigned x,
22 unsigned y,
23 unsigned w,
24 unsigned h,
25 struct pipe_box *box )
26 {
27 box->x = x;
28 box->y = y;
29 box->z = 0;
30 box->width = w;
31 box->height = h;
32 box->depth = 1;
33 }
34
35 static INLINE
36 void u_box_origin_2d( unsigned w,
37 unsigned h,
38 struct pipe_box *box )
39 {
40 box->x = 0;
41 box->y = 0;
42 box->z = 0;
43 box->width = w;
44 box->height = h;
45 box->depth = 1;
46 }
47
48 static INLINE
49 void u_box_2d_zslice( unsigned x,
50 unsigned y,
51 unsigned z,
52 unsigned w,
53 unsigned h,
54 struct pipe_box *box )
55 {
56 box->x = x;
57 box->y = y;
58 box->z = z;
59 box->width = w;
60 box->height = h;
61 box->depth = 1;
62 }
63
64 static INLINE
65 void u_box_3d( unsigned x,
66 unsigned y,
67 unsigned z,
68 unsigned w,
69 unsigned h,
70 unsigned d,
71 struct pipe_box *box )
72 {
73 box->x = x;
74 box->y = y;
75 box->z = z;
76 box->width = w;
77 box->height = h;
78 box->depth = d;
79 }
80
81 /* Clips @dst to width @w and height @h.
82 * Returns -1 if the resulting box would be empty (then @dst is left unchanged).
83 * 0 if nothing has been reduced.
84 * 1 if width has been reduced.
85 * 2 if height has been reduced.
86 * 3 if both width and height have been reduced.
87 * Aliasing permitted.
88 */
89 static INLINE int
90 u_box_clip_2d(struct pipe_box *dst,
91 const struct pipe_box *box, int w, int h)
92 {
93 unsigned i;
94 int a[2], b[2], dim[2];
95 int *start, *end;
96 int res = 0;
97
98 if (!box->width || !box->height)
99 return -1;
100 dim[0] = w;
101 dim[1] = h;
102 a[0] = box->x;
103 a[1] = box->y;
104 b[0] = box->x + box->width;
105 b[1] = box->y + box->height;
106
107 for (i = 0; i < 2; ++i) {
108 start = (a[i] <= b[i]) ? &a[i] : &b[i];
109 end = (a[i] <= b[i]) ? &b[i] : &a[i];
110
111 if (*end < 0 || *start >= dim[i])
112 return -1;
113 if (*start < 0) {
114 *start = 0;
115 res |= (1 << i);
116 }
117 if (*end > dim[i]) {
118 *end = dim[i];
119 res |= (1 << i);
120 }
121 }
122
123 if (res) {
124 dst->x = a[0];
125 dst->y = a[1];
126 dst->width = b[0] - a[0];
127 dst->height = b[1] - a[1];
128 }
129 return res;
130 }
131
132 static INLINE int64_t
133 u_box_volume_3d(const struct pipe_box *box)
134 {
135 return (int64_t)box->width * box->height * box->depth;
136 }
137
138 /* Aliasing of @dst permitted. */
139 static INLINE void
140 u_box_union_2d(struct pipe_box *dst,
141 const struct pipe_box *a, const struct pipe_box *b)
142 {
143 dst->x = MIN2(a->x, b->x);
144 dst->y = MIN2(a->y, b->y);
145
146 dst->width = MAX2(a->x + a->width, b->x + b->width) - dst->x;
147 dst->height = MAX2(a->y + a->height, b->y + b->height) - dst->y;
148 }
149
150 /* Aliasing of @dst permitted. */
151 static INLINE void
152 u_box_union_3d(struct pipe_box *dst,
153 const struct pipe_box *a, const struct pipe_box *b)
154 {
155 dst->x = MIN2(a->x, b->x);
156 dst->y = MIN2(a->y, b->y);
157 dst->z = MIN2(a->z, b->z);
158
159 dst->width = MAX2(a->x + a->width, b->x + b->width) - dst->x;
160 dst->height = MAX2(a->y + a->height, b->y + b->height) - dst->y;
161 dst->depth = MAX2(a->z + a->depth, b->z + b->depth) - dst->z;
162 }
163
164 static INLINE boolean
165 u_box_test_intersection_2d(const struct pipe_box *a,
166 const struct pipe_box *b)
167 {
168 unsigned i;
169 int a_l[2], a_r[2], b_l[2], b_r[2];
170
171 a_l[0] = MIN2(a->x, a->x + a->width);
172 a_r[0] = MAX2(a->x, a->x + a->width);
173 a_l[1] = MIN2(a->y, a->y + a->height);
174 a_r[1] = MAX2(a->y, a->y + a->height);
175
176 b_l[0] = MIN2(b->x, b->x + b->width);
177 b_r[0] = MAX2(b->x, b->x + b->width);
178 b_l[1] = MIN2(b->y, b->y + b->height);
179 b_r[1] = MAX2(b->y, b->y + b->height);
180
181 for (i = 0; i < 2; ++i) {
182 if (a_l[i] > b_r[i] || a_r[i] < b_l[i])
183 return FALSE;
184 }
185 return TRUE;
186 }
187
188 static INLINE void
189 u_box_minify_2d(struct pipe_box *dst,
190 const struct pipe_box *src, unsigned l)
191 {
192 dst->x = src->x >> l;
193 dst->y = src->y >> l;
194 dst->width = MAX2(src->width >> l, 1);
195 dst->height = MAX2(src->height >> l, 1);
196 }
197
198 #endif