Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_box.h
4561 views
#ifndef UTIL_BOX_INLINES_H1#define UTIL_BOX_INLINES_H23#include "pipe/p_state.h"4#include "util/u_math.h"56static inline void7u_box_1d(unsigned x, unsigned w, struct pipe_box *box)8{9box->x = x;10box->y = 0;11box->z = 0;12box->width = w;13box->height = 1;14box->depth = 1;15}1617static inline void18u_box_2d(unsigned x,unsigned y, unsigned w, unsigned h, struct pipe_box *box)19{20box->x = x;21box->y = y;22box->z = 0;23box->width = w;24box->height = h;25box->depth = 1;26}2728static inline void29u_box_origin_2d(unsigned w, unsigned h, struct pipe_box *box)30{31box->x = 0;32box->y = 0;33box->z = 0;34box->width = w;35box->height = h;36box->depth = 1;37}3839static inline void40u_box_2d_zslice(unsigned x, unsigned y, unsigned z,41unsigned w, unsigned h, struct pipe_box *box)42{43box->x = x;44box->y = y;45box->z = z;46box->width = w;47box->height = h;48box->depth = 1;49}5051static inline void52u_box_3d(unsigned x, unsigned y, unsigned z,53unsigned w, unsigned h, unsigned d,54struct pipe_box *box)55{56box->x = x;57box->y = y;58box->z = z;59box->width = w;60box->height = h;61box->depth = d;62}6364/* Clips @dst to width @w and height @h.65* Returns -1 if the resulting box would be empty (then @dst is left unchanged).66* 0 if nothing has been reduced.67* 1 if width has been reduced.68* 2 if height has been reduced.69* 3 if both width and height have been reduced.70* Aliasing permitted.71*/72static inline int73u_box_clip_2d(struct pipe_box *dst,74const struct pipe_box *box, int w, int h)75{76unsigned i;77int a[2], b[2], dim[2];78int *start, *end;79int res = 0;8081if (!box->width || !box->height)82return -1;83dim[0] = w;84dim[1] = h;85a[0] = box->x;86a[1] = box->y;87b[0] = box->x + box->width;88b[1] = box->y + box->height;8990for (i = 0; i < 2; ++i) {91start = (a[i] <= b[i]) ? &a[i] : &b[i];92end = (a[i] <= b[i]) ? &b[i] : &a[i];9394if (*end < 0 || *start >= dim[i])95return -1;96if (*start < 0) {97*start = 0;98res |= (1 << i);99}100if (*end > dim[i]) {101*end = dim[i];102res |= (1 << i);103}104}105106if (res) {107dst->x = a[0];108dst->y = a[1];109dst->width = b[0] - a[0];110dst->height = b[1] - a[1];111}112return res;113}114115static inline int64_t116u_box_volume_3d(const struct pipe_box *box)117{118return (int64_t)box->width * box->height * box->depth;119}120121/* Aliasing of @dst permitted. Supports empty width */122static inline void123u_box_union_1d(struct pipe_box *dst,124const struct pipe_box *a, const struct pipe_box *b)125{126int x, width;127128if (a->width == 0) {129x = b->x;130width = b->width;131} else if (b->width == 0) {132x = a->x;133width = a->width;134} else {135x = MIN2(a->x, b->x);136width = MAX2(a->x + a->width, b->x + b->width) - x;137}138139dst->x = x;140dst->width = width;141}142143/* Aliasing of @dst permitted. */144static inline void145u_box_intersect_1d(struct pipe_box *dst,146const struct pipe_box *a, const struct pipe_box *b)147{148int x;149150x = MAX2(a->x, b->x);151152dst->width = MIN2(a->x + a->width, b->x + b->width) - x;153dst->x = x;154if (dst->width <= 0) {155dst->x = 0;156dst->width = 0;157}158}159160/* Aliasing of @dst permitted. */161static inline void162u_box_union_2d(struct pipe_box *dst,163const struct pipe_box *a, const struct pipe_box *b)164{165int x, y;166167x = MIN2(a->x, b->x);168y = MIN2(a->y, b->y);169170dst->width = MAX2(a->x + a->width, b->x + b->width) - x;171dst->height = MAX2(a->y + a->height, b->y + b->height) - y;172dst->x = x;173dst->y = y;174}175176/* Aliasing of @dst permitted. */177static inline void178u_box_union_3d(struct pipe_box *dst,179const struct pipe_box *a, const struct pipe_box *b)180{181int x, y, z;182183x = MIN2(a->x, b->x);184y = MIN2(a->y, b->y);185z = MIN2(a->z, b->z);186187dst->width = MAX2(a->x + a->width, b->x + b->width) - x;188dst->height = MAX2(a->y + a->height, b->y + b->height) - y;189dst->depth = MAX2(a->z + a->depth, b->z + b->depth) - z;190dst->x = x;191dst->y = y;192dst->z = z;193}194195static inline boolean196u_box_test_intersection_2d(const struct pipe_box *a,197const struct pipe_box *b)198{199unsigned i;200int a_l[2], a_r[2], b_l[2], b_r[2];201202a_l[0] = MIN2(a->x, a->x + a->width);203a_r[0] = MAX2(a->x, a->x + a->width);204a_l[1] = MIN2(a->y, a->y + a->height);205a_r[1] = MAX2(a->y, a->y + a->height);206207b_l[0] = MIN2(b->x, b->x + b->width);208b_r[0] = MAX2(b->x, b->x + b->width);209b_l[1] = MIN2(b->y, b->y + b->height);210b_r[1] = MAX2(b->y, b->y + b->height);211212for (i = 0; i < 2; ++i) {213if (a_l[i] > b_r[i] || a_r[i] < b_l[i])214return FALSE;215}216return TRUE;217}218219static inline void220u_box_minify_2d(struct pipe_box *dst,221const struct pipe_box *src, unsigned l)222{223dst->x = src->x >> l;224dst->y = src->y >> l;225dst->width = MAX2(src->width >> l, 1);226dst->height = MAX2(src->height >> l, 1);227}228229static inline void230u_box_minify_3d(struct pipe_box *dst,231const struct pipe_box *src, unsigned l)232{233dst->x = src->x >> l;234dst->y = src->y >> l;235dst->z = src->z >> l;236dst->width = MAX2(src->width >> l, 1);237dst->height = MAX2(src->height >> l, 1);238dst->depth = MAX2(src->depth >> l, 1);239}240241#endif242243244