From 74d8c929cc0b08fd2424ba261dfd7459047367fb Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 19 Jun 2018 09:37:49 +0200 Subject: [PATCH] Give the clipper a reference to the current viewpoint This is both for efficiency and encapsulation. At last on MSVC in 64 bit, accessing global variables is very inefficient and the clipper was doing it repeatedly in its worker functions. It is also one less place where the global viewpoint gets checked. --- src/gl/scene/gl_drawinfo.cpp | 1 + src/gl/scene/gl_portal.cpp | 4 +--- src/hwrenderer/scene/hw_clipper.cpp | 15 ++++++++------- src/hwrenderer/scene/hw_clipper.h | 25 ++++++++++++++++--------- src/r_defs.h | 2 -- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/gl/scene/gl_drawinfo.cpp b/src/gl/scene/gl_drawinfo.cpp index abcdbe70b2..c7e02314cf 100644 --- a/src/gl/scene/gl_drawinfo.cpp +++ b/src/gl/scene/gl_drawinfo.cpp @@ -201,6 +201,7 @@ FDrawInfo *FDrawInfo::StartDrawInfo(GLSceneDrawer *drawer) di->mDrawer = drawer; di->mVBO = GLRenderer->mVBO; di->mClipper = &staticClipper; + di->mClipper->SetViewpoint(r_viewpoint); staticClipper.Clear(); di->FixedColormap = drawer->FixedColormap; di->StartScene(); diff --git a/src/gl/scene/gl_portal.cpp b/src/gl/scene/gl_portal.cpp index 3f8ede410a..bb0d09adb3 100644 --- a/src/gl/scene/gl_portal.cpp +++ b/src/gl/scene/gl_portal.cpp @@ -941,9 +941,7 @@ void GLMirrorPortal::DrawContents(FDrawInfo *di) angle_t af = drawer->FrustumAngle(); if (afmClipper->SafeAddClipRangeRealAngles(r_viewpoint.Angles.Yaw.BAMs()+af, r_viewpoint.Angles.Yaw.BAMs()-af); - angle_t a2 = linedef->v1->GetClipAngle(); - angle_t a1 = linedef->v2->GetClipAngle(); - di->mClipper->SafeAddClipRange(a1,a2); + di->mClipper->SafeAddClipRange(linedef->v1, linedef->v2); gl_RenderState.SetClipLine(linedef); gl_RenderState.EnableClipLine(true); diff --git a/src/hwrenderer/scene/hw_clipper.cpp b/src/hwrenderer/scene/hw_clipper.cpp index e1811dd8c4..85646f84c1 100644 --- a/src/hwrenderer/scene/hw_clipper.cpp +++ b/src/hwrenderer/scene/hw_clipper.cpp @@ -373,10 +373,10 @@ angle_t Clipper::AngleToPseudo(angle_t ang) // //----------------------------------------------------------------------------- -angle_t R_PointToPseudoAngle(double x, double y) +angle_t Clipper::PointToPseudoAngle(double x, double y) { - double vecx = x - r_viewpoint.Pos.X; - double vecy = y - r_viewpoint.Pos.Y; + double vecx = x - viewpoint->Pos.X; + double vecy = y - viewpoint->Pos.Y; if (vecx == 0 && vecy == 0) { @@ -427,14 +427,15 @@ bool Clipper::CheckBox(const float *bspcoord) // Find the corners of the box // that define the edges from current viewpoint. - boxpos = (r_viewpoint.Pos.X <= bspcoord[BOXLEFT] ? 0 : r_viewpoint.Pos.X < bspcoord[BOXRIGHT ] ? 1 : 2) + - (r_viewpoint.Pos.Y >= bspcoord[BOXTOP ] ? 0 : r_viewpoint.Pos.Y > bspcoord[BOXBOTTOM] ? 4 : 8); + auto &vp = viewpoint; + boxpos = (vp->Pos.X <= bspcoord[BOXLEFT] ? 0 : vp->Pos.X < bspcoord[BOXRIGHT ] ? 1 : 2) + + (vp->Pos.Y >= bspcoord[BOXTOP ] ? 0 : vp->Pos.Y > bspcoord[BOXBOTTOM] ? 4 : 8); if (boxpos == 5) return true; check = checkcoord[boxpos]; - angle1 = R_PointToPseudoAngle (bspcoord[check[0]], bspcoord[check[1]]); - angle2 = R_PointToPseudoAngle (bspcoord[check[2]], bspcoord[check[3]]); + angle1 = PointToPseudoAngle (bspcoord[check[0]], bspcoord[check[1]]); + angle2 = PointToPseudoAngle (bspcoord[check[2]], bspcoord[check[3]]); return SafeCheckRange(angle2, angle1); } diff --git a/src/hwrenderer/scene/hw_clipper.h b/src/hwrenderer/scene/hw_clipper.h index c3897bd56f..e8da8172cf 100644 --- a/src/hwrenderer/scene/hw_clipper.h +++ b/src/hwrenderer/scene/hw_clipper.h @@ -6,14 +6,6 @@ #include "r_utility.h" #include "memarena.h" -angle_t R_PointToPseudoAngle(double x, double y); - -// Used to speed up angle calculations during clipping -inline angle_t vertex_t::GetClipAngle() -{ - return R_PointToPseudoAngle(p.X, p.Y); -} - class ClipNode { friend class Clipper; @@ -37,6 +29,7 @@ class Clipper ClipNode * clipnodes = nullptr; ClipNode * cliphead = nullptr; ClipNode * silhouette = nullptr; // will be preserved even when RemoveClipRange is called + const FRenderViewpoint *viewpoint = nullptr; bool blocked = false; static angle_t AngleToPseudo(angle_t ang); @@ -78,6 +71,11 @@ public: c->next = c->prev = NULL; return c; } + + void SetViewpoint(const FRenderViewpoint &vp) + { + viewpoint = &vp; + } void SetSilhouette(); @@ -105,6 +103,13 @@ public: AddClipRange(startangle, endangle); } } + + void SafeAddClipRange(const vertex_t *v1, const vertex_t *v2) + { + angle_t a2 = PointToPseudoAngle(v1->p.X, v1->p.Y); + angle_t a1 = PointToPseudoAngle(v2->p.X, v2->p.Y); + SafeAddClipRange(a1,a2); + } void SafeAddClipRangeRealAngles(angle_t startangle, angle_t endangle) { @@ -141,13 +146,15 @@ public: { return blocked; } + + angle_t PointToPseudoAngle(double x, double y); bool CheckBox(const float *bspcoord); // Used to speed up angle calculations during clipping inline angle_t GetClipAngle(vertex_t *v) { - return unsigned(v->angletime) == starttime ? v->viewangle : (v->angletime = starttime, v->viewangle = R_PointToPseudoAngle(v->p.X, v->p.Y)); + return unsigned(v->angletime) == starttime ? v->viewangle : (v->angletime = starttime, v->viewangle = PointToPseudoAngle(v->p.X, v->p.Y)); } }; diff --git a/src/r_defs.h b/src/r_defs.h index 4ba0d45d35..4422fbf8dd 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -180,8 +180,6 @@ struct vertex_t { p.Zero(); } - - angle_t GetClipAngle(); }; // Forward of LineDefs, for Sectors.