1 #ifndef UTIL_BOX_INLINES_H
2 #define UTIL_BOX_INLINES_H
4 #include "pipe/p_state.h"
5 #include "util/u_math.h"
8 u_box_1d(unsigned x
, unsigned w
, struct pipe_box
*box
)
19 u_box_2d(unsigned x
,unsigned y
, unsigned w
, unsigned h
, struct pipe_box
*box
)
30 u_box_origin_2d(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
)
53 u_box_3d(unsigned x
, unsigned y
, unsigned z
,
54 unsigned w
, unsigned h
, unsigned d
,
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.
74 u_box_clip_2d(struct pipe_box
*dst
,
75 const struct pipe_box
*box
, int w
, int h
)
78 int a
[2], b
[2], dim
[2];
82 if (!box
->width
|| !box
->height
)
88 b
[0] = box
->x
+ box
->width
;
89 b
[1] = box
->y
+ box
->height
;
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
];
95 if (*end
< 0 || *start
>= dim
[i
])
110 dst
->width
= b
[0] - a
[0];
111 dst
->height
= b
[1] - a
[1];
116 static inline int64_t
117 u_box_volume_3d(const struct pipe_box
*box
)
119 return (int64_t)box
->width
* box
->height
* box
->depth
;
122 /* Aliasing of @dst permitted. */
124 u_box_union_2d(struct pipe_box
*dst
,
125 const struct pipe_box
*a
, const struct pipe_box
*b
)
129 x
= MIN2(a
->x
, b
->x
);
130 y
= MIN2(a
->y
, b
->y
);
132 dst
->width
= MAX2(a
->x
+ a
->width
, b
->x
+ b
->width
) - x
;
133 dst
->height
= MAX2(a
->y
+ a
->height
, b
->y
+ b
->height
) - y
;
138 /* Aliasing of @dst permitted. */
140 u_box_union_3d(struct pipe_box
*dst
,
141 const struct pipe_box
*a
, const struct pipe_box
*b
)
145 x
= MIN2(a
->x
, b
->x
);
146 y
= MIN2(a
->y
, b
->y
);
147 z
= MIN2(a
->z
, b
->z
);
149 dst
->width
= MAX2(a
->x
+ a
->width
, b
->x
+ b
->width
) - x
;
150 dst
->height
= MAX2(a
->y
+ a
->height
, b
->y
+ b
->height
) - y
;
151 dst
->depth
= MAX2(a
->z
+ a
->depth
, b
->z
+ b
->depth
) - z
;
157 static inline boolean
158 u_box_test_intersection_2d(const struct pipe_box
*a
,
159 const struct pipe_box
*b
)
162 int a_l
[2], a_r
[2], b_l
[2], b_r
[2];
164 a_l
[0] = MIN2(a
->x
, a
->x
+ a
->width
);
165 a_r
[0] = MAX2(a
->x
, a
->x
+ a
->width
);
166 a_l
[1] = MIN2(a
->y
, a
->y
+ a
->height
);
167 a_r
[1] = MAX2(a
->y
, a
->y
+ a
->height
);
169 b_l
[0] = MIN2(b
->x
, b
->x
+ b
->width
);
170 b_r
[0] = MAX2(b
->x
, b
->x
+ b
->width
);
171 b_l
[1] = MIN2(b
->y
, b
->y
+ b
->height
);
172 b_r
[1] = MAX2(b
->y
, b
->y
+ b
->height
);
174 for (i
= 0; i
< 2; ++i
) {
175 if (a_l
[i
] > b_r
[i
] || a_r
[i
] < b_l
[i
])
182 u_box_minify_2d(struct pipe_box
*dst
,
183 const struct pipe_box
*src
, unsigned l
)
185 dst
->x
= src
->x
>> l
;
186 dst
->y
= src
->y
>> l
;
187 dst
->width
= MAX2(src
->width
>> l
, 1);
188 dst
->height
= MAX2(src
->height
>> l
, 1);
192 u_box_minify_3d(struct pipe_box
*dst
,
193 const struct pipe_box
*src
, unsigned l
)
195 dst
->x
= src
->x
>> l
;
196 dst
->y
= src
->y
>> l
;
197 dst
->z
= src
->z
>> l
;
198 dst
->width
= MAX2(src
->width
>> l
, 1);
199 dst
->height
= MAX2(src
->height
>> l
, 1);
200 dst
->depth
= MAX2(src
->depth
>> l
, 1);