From: havoc Date: Wed, 11 Sep 2002 04:03:27 +0000 (+0000) Subject: BoxOnPlaneSideFunc code - gone, replaced by BoxOnPlaneSide X-Git-Tag: RELEASE_0_2_0_RC1~251 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=14b5a1213af9d37bf205793411ecdfb31135539f;p=xonotic%2Fdarkplaces.git BoxOnPlaneSideFunc code - gone, replaced by BoxOnPlaneSide BOX_ON_PLANE_SIDE macro - gone, no replacement necessary (use BoxOnPlaneSide) R_CullBox and R_CulledBox macros have been replaced by optimized functions this gets a minor speed gain overall git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2355 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/gl_rmain.c b/gl_rmain.c index 6a177423..8948594a 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -327,6 +327,100 @@ void GL_Init (void) qglEnable(GL_TEXTURE_2D); } +int R_CullBox(const vec3_t emins, const vec3_t emaxs) +{ + int i; + mplane_t *p; + for (i = 0;i < 4;i++) + { + p = frustum + i; + switch(p->signbits) + { + default: + case 0: + if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist) + return true; + break; + case 1: + if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist) + return true; + break; + case 2: + if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist) + return true; + break; + case 3: + if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist) + return true; + break; + case 4: + if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist) + return true; + break; + case 5: + if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist) + return true; + break; + case 6: + if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist) + return true; + break; + case 7: + if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist) + return true; + break; + } + } + return false; +} + +int R_NotCulledBox(const vec3_t emins, const vec3_t emaxs) +{ + int i; + mplane_t *p; + for (i = 0;i < 4;i++) + { + p = frustum + i; + switch(p->signbits) + { + default: + case 0: + if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist) + return false; + break; + case 1: + if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2] < p->dist) + return false; + break; + case 2: + if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist) + return false; + break; + case 3: + if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2] < p->dist) + return false; + break; + case 4: + if (p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist) + return false; + break; + case 5: + if (p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2] < p->dist) + return false; + break; + case 6: + if (p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist) + return false; + break; + case 7: + if (p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2] < p->dist) + return false; + break; + } + } + return true; +} + //================================================================================== diff --git a/mathlib.c b/mathlib.c index 7846af91..96090b50 100644 --- a/mathlib.c +++ b/mathlib.c @@ -276,79 +276,15 @@ void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, /*-----------------------------------------------------------------*/ -// LordHavoc note 1: -// BoxOnPlaneSide did a switch on a 'signbits' value and had optimized -// assembly in an attempt to accelerate it further, very inefficient -// considering that signbits of the frustum planes only changed each -// frame, and the world planes changed only at load time. -// So, to optimize it further I took the obvious route of storing a function -// pointer in the plane struct itself, and shrunk each of the individual -// cases to a single return statement. -// LordHavoc note 2: -// realized axial cases would be a nice speedup for world geometry, although -// never useful for the frustum planes. -int BoxOnPlaneSideX (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[0] ? 1 : (p->dist >= emaxs[0] ? 2 : 3);} -int BoxOnPlaneSideY (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[1] ? 1 : (p->dist >= emaxs[1] ? 2 : 3);} -int BoxOnPlaneSideZ (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[2] ? 1 : (p->dist >= emaxs[2] ? 2 : 3);} -int BoxOnPlaneSide0 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1));} -int BoxOnPlaneSide1 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1));} -int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1));} -int BoxOnPlaneSide3 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1));} -int BoxOnPlaneSide4 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));} -int BoxOnPlaneSide5 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));} -int BoxOnPlaneSide6 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));} -int BoxOnPlaneSide7 (vec3_t emins, vec3_t emaxs, mplane_t *p) {return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1));} - void BoxOnPlaneSideClassify(mplane_t *p) { - switch(p->type) - { - case 0: // x axis - p->BoxOnPlaneSideFunc = BoxOnPlaneSideX; - break; - case 1: // y axis - p->BoxOnPlaneSideFunc = BoxOnPlaneSideY; - break; - case 2: // z axis - p->BoxOnPlaneSideFunc = BoxOnPlaneSideZ; - break; - default: - if (p->normal[2] < 0) // 4 - { - if (p->normal[1] < 0) // 2 - { - if (p->normal[0] < 0) // 1 - p->BoxOnPlaneSideFunc = BoxOnPlaneSide7; - else - p->BoxOnPlaneSideFunc = BoxOnPlaneSide6; - } - else - { - if (p->normal[0] < 0) // 1 - p->BoxOnPlaneSideFunc = BoxOnPlaneSide5; - else - p->BoxOnPlaneSideFunc = BoxOnPlaneSide4; - } - } - else - { - if (p->normal[1] < 0) // 2 - { - if (p->normal[0] < 0) // 1 - p->BoxOnPlaneSideFunc = BoxOnPlaneSide3; - else - p->BoxOnPlaneSideFunc = BoxOnPlaneSide2; - } - else - { - if (p->normal[0] < 0) // 1 - p->BoxOnPlaneSideFunc = BoxOnPlaneSide1; - else - p->BoxOnPlaneSideFunc = BoxOnPlaneSide0; - } - } - break; - } + p->signbits = 0; + if (p->normal[0] < 0) // 1 + p->signbits |= 1; + if (p->normal[1] < 0) // 2 + p->signbits |= 2; + if (p->normal[2] < 0) // 4 + p->signbits |= 4; } void PlaneClassify(mplane_t *p) @@ -364,6 +300,32 @@ void PlaneClassify(mplane_t *p) BoxOnPlaneSideClassify(p); } +int BoxOnPlaneSide (const vec3_t emins, const vec3_t emaxs, const mplane_t *p) +{ + if (p->type < 3) + return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1)); + switch(p->signbits) + { + default: + case 0: + return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1)); + case 1: + return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) < p->dist) << 1)); + case 2: + return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1)); + case 3: + return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) < p->dist) << 1)); + case 4: + return (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1)); + case 5: + return (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2]) < p->dist) << 1)); + case 6: + return (((p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1)); + case 7: + return (((p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2]) >= p->dist) | (((p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2]) < p->dist) << 1)); + } +} + void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) { double angle, sr, sp, sy, cr, cp, cy; diff --git a/mathlib.h b/mathlib.h index afb24dde..cffbbbc8 100644 --- a/mathlib.h +++ b/mathlib.h @@ -156,22 +156,7 @@ void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up); void VectorVectorsDouble(const double *forward, double *right, double *up); void PlaneClassify(struct mplane_s *p); - -#define BOX_ON_PLANE_SIDE(emins, emaxs, p) \ - (((p)->type < 3)? \ - ( \ - ((p)->dist <= (emins)[(p)->type])? \ - 1 \ - : \ - ( \ - ((p)->dist >= (emaxs)[(p)->type])?\ - 2 \ - : \ - 3 \ - ) \ - ) \ - : \ - (p)->BoxOnPlaneSideFunc( (emins), (emaxs), (p))) +int BoxOnPlaneSide (const vec3_t emins, const vec3_t emaxs, const struct mplane_s *p); #define PlaneDist(point,plane) ((plane)->type < 3 ? (point)[(plane)->type] : DotProduct((point), (plane)->normal)) #define PlaneDiff(point,plane) (((plane)->type < 3 ? (point)[(plane)->type] : DotProduct((point), (plane)->normal)) - (plane)->dist) diff --git a/model_brush.h b/model_brush.h index 26c1c15f..ede0144e 100644 --- a/model_brush.h +++ b/model_brush.h @@ -51,8 +51,7 @@ typedef struct mplane_s float dist; // for texture axis selection and fast side tests int type; - // LordHavoc: faster than id's signbits system - int (*BoxOnPlaneSideFunc) (vec3_t emins, vec3_t emaxs, struct mplane_s *p); + int signbits; } mplane_t; diff --git a/render.h b/render.h index b4e7c3a3..ab437b95 100644 --- a/render.h +++ b/render.h @@ -128,9 +128,8 @@ void R_DrawSpriteModel (entity_render_t *ent); //#define PARANOID 1 -// LordHavoc: was a major time waster -#define R_CullBox(mins,maxs) (frustum[0].BoxOnPlaneSideFunc(mins, maxs, &frustum[0]) == 2 || frustum[1].BoxOnPlaneSideFunc(mins, maxs, &frustum[1]) == 2 || frustum[2].BoxOnPlaneSideFunc(mins, maxs, &frustum[2]) == 2 || frustum[3].BoxOnPlaneSideFunc(mins, maxs, &frustum[3]) == 2) -#define R_NotCulledBox(mins,maxs) (frustum[0].BoxOnPlaneSideFunc(mins, maxs, &frustum[0]) != 2 && frustum[1].BoxOnPlaneSideFunc(mins, maxs, &frustum[1]) != 2 && frustum[2].BoxOnPlaneSideFunc(mins, maxs, &frustum[2]) != 2 && frustum[3].BoxOnPlaneSideFunc(mins, maxs, &frustum[3]) != 2) +int R_CullBox(const vec3_t emins, const vec3_t emaxs); +int R_NotCulledBox(const vec3_t emins, const vec3_t emaxs); extern qboolean fogenabled; extern vec3_t fogcolor; diff --git a/sv_main.c b/sv_main.c index 0a5b5bdf..a65938f1 100644 --- a/sv_main.c +++ b/sv_main.c @@ -489,7 +489,7 @@ loc0: } // node - recurse down the BSP tree - switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane)) + switch (BoxOnPlaneSide(mins, maxs, node->plane)) { case 1: // front node = node->children[0];