Skip to content

Commit

Permalink
Optimize|Client: Defer initialization of Bias surface and vertex data
Browse files Browse the repository at this point in the history
If the Bias lighting model is not enabled it is a waste of time and
resources to initialize the surface and vertex illumination data for
the map. Now this data is prepared JIT for rendering if 'rend-bias'
is enabled.
  • Loading branch information
danij-deng committed Jul 27, 2013
1 parent 024406d commit 817da96
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 140 deletions.
25 changes: 4 additions & 21 deletions doomsday/client/include/world/bspleaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,11 @@ class BspLeaf : public de::MapElement
/// Required sector attribution is missing. @ingroup errors
DENG2_ERROR(MissingSectorError);

#ifdef __CLIENT__
/// The referenced geometry group does not exist. @ingroup errors
DENG2_ERROR(UnknownGeometryGroupError);
#endif

/*
* Linked-element lists/sets:
*/
typedef QSet<polyobj_s *> Polyobjs;
typedef QList<Segment *> Segments;
#ifdef __CLIENT__
typedef QMap<int, BiasSurface *> BiasSurfaces;
#endif

public: /// @todo Make private:
#ifdef __CLIENT__
Expand Down Expand Up @@ -281,26 +273,17 @@ class BspLeaf : public de::MapElement
int numFanVertices() const;

/**
* Retrieve the bias surface for specified geometry @a group.
* Retrieve the bias surface for specified geometry @a group. If no bias
* surface has yet been initialized for the group it will be at this time.
*
* @param group Geometry group identifier for the bias surface.
*/
BiasSurface &biasSurface(int group);

/**
* Assign a new bias surface to the specified geometry @a group.
*
* @param group Geometry group identifier for the surface.
* @param newBiasSurface New BiasSurface for the identified @a group. Any
* existing bias surface will be replaced (destroyed).
* Ownership is given to the BSP leaf.
*/
void setBiasSurface(int group, BiasSurface *newBiasSurface);

/**
* Provides access to the bias surfaces for the BSP leaf, for efficent traversal.
* @param changes
*/
BiasSurfaces const &biasSurfaces() const;
void updateBiasAffection(BiasTracker &changes);

/**
* Returns a pointer to the first ShadowLink; otherwise @c 0.
Expand Down
28 changes: 4 additions & 24 deletions doomsday/client/include/world/segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ class Segment : public de::MapElement
/// Required line attribution is missing. @ingroup errors
DENG2_ERROR(MissingLineSideError);

#ifdef __CLIENT__

/// The referenced geometry group does not exist. @ingroup errors
DENG2_ERROR(UnknownGeometryGroupError);

/*
* Linked-element lists/sets:
*/
typedef QMap<int, BiasSurface *> BiasSurfaces;
#endif

enum Flag
{
FacingFront = 0x1
Expand Down Expand Up @@ -242,26 +231,17 @@ class Segment : public de::MapElement
#ifdef __CLIENT__

/**
* Retrieve the bias surface for specified geometry @a group.
* Retrieve the bias surface for specified geometry @a group. If no bias
* surface has yet been initialized for the group it will be at this time.
*
* @param group Geometry group identifier for the bias surface.
*/
BiasSurface &biasSurface(int group);

/**
* Assign a new bias surface to the specified geometry @a group.
*
* @param group Geometry group identifier for the surface.
* @param newBiasSurface New BiasSurface for the identified @a group. Any
* existing bias surface will be replaced (destroyed).
* Ownership is given to the segment.
*/
void setBiasSurface(int group, BiasSurface *newBiasSurface);

/**
* Provides access to the bias surfaces for the segment, for efficent traversal.
* @param allChanges
*/
BiasSurfaces const &biasSurfaces() const;
void updateBiasAffection(BiasTracker &changes);

#endif // __CLIENT__

Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/render/rend_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ static void writeWallSection(Segment &segment, int section,
parm.flags = RPF_DEFAULT | (skyMasked? RPF_SKYMASK : 0);
parm.forceOpaque = wallSpec.flags.testFlag(WallSpec::ForceOpaque);
parm.alpha = parm.forceOpaque? 1 : opacity;
parm.bsuf = &segment.biasSurface(wallSpec.section);
parm.bsuf = useBias? &segment.biasSurface(wallSpec.section) : 0;
parm.texTL = &texQuad[0];
parm.texBR = &texQuad[1];

Expand Down Expand Up @@ -1676,7 +1676,7 @@ static void writeLeafPlane(Plane &plane)

parm.flags = RPF_DEFAULT;
parm.isWall = false;
parm.bsuf = &leaf->biasSurface(plane.indexInSector());
parm.bsuf = useBias? &leaf->biasSurface(plane.indexInSector()) : 0;
parm.texTL = &texTL;
parm.texBR = &texBR;
parm.surfaceLightLevelDL = parm.surfaceLightLevelDR = 0;
Expand Down
41 changes: 17 additions & 24 deletions doomsday/client/src/world/bspleaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ ddouble triangleArea(Vector2d const &v1, Vector2d const &v2, Vector2d const &v3)
return (a.x * b.y - b.x * a.y) / 2;
}

#ifdef __CLIENT__
typedef QMap<int, BiasSurface *> BiasSurfaces;
#endif

DENG2_PIMPL(BspLeaf)
{
typedef QSet<Mesh *> Meshes;
Expand Down Expand Up @@ -478,42 +482,31 @@ int BspLeaf::numFanVertices() const
}

BiasSurface &BspLeaf::biasSurface(int group)
{
BiasSurfaces::iterator found = d->biasSurfaces.find(group);
if(found != d->biasSurfaces.end())
{
return **found;
}
/// @throw InvalidGeometryGroupError Attempted with an invalid geometry group.
throw UnknownGeometryGroupError("BspLeaf::biasSurface", QString("Invalid group %1").arg(group));
}

void BspLeaf::setBiasSurface(int group, BiasSurface *newBiasSurface)
{
if(!d->sector)
/// @throw MissingSectorError Attempted with no sector attributed.
throw MissingSectorError("BspLeaf::setBiasSurface", "No sector is attributed");

// Sanity check.
DENG_ASSERT(group >= 0 && group < d->sector->planeCount());
DENG_ASSERT(group >= 0 && group < d->sector->planeCount()); // sanity check
DENG_ASSERT(!isDegenerate()); // sanity check

if(d->biasSurfaces.contains(group))
BiasSurfaces::iterator foundAt = d->biasSurfaces.find(group);
if(foundAt != d->biasSurfaces.end())
{
delete d->biasSurfaces.take(group);
return **foundAt;
}

if(newBiasSurface)
{
if(isDegenerate())
LOG_TRACE("Adding a BiasSurface to a degenerate BSP leaf??");

d->biasSurfaces.insert(group, newBiasSurface);
}
BiasSurface *bsuf = new BiasSurface(*this, group, numFanVertices());
d->biasSurfaces.insert(group, bsuf);
return *bsuf;
}

BspLeaf::BiasSurfaces const &BspLeaf::biasSurfaces() const
void BspLeaf::updateBiasAffection(BiasTracker &changes)
{
return d->biasSurfaces;
foreach(BiasSurface *biasSurface, d->biasSurfaces)
{
biasSurface->updateAffection(changes);
}
}

ShadowLink *BspLeaf::firstShadowLink() const
Expand Down
50 changes: 4 additions & 46 deletions doomsday/client/src/world/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,24 +1198,18 @@ DENG2_OBSERVES(bsp::Partitioner, UnclosedSectorFound)
/*
* Apply changes to all surfaces:
*/
foreach(Segment *segment, segments)
foreach(BiasSurface *biasSurface, segment->biasSurfaces())
foreach(Segment *seg, segments)
{
biasSurface->updateAffection(allChanges);
seg->updateBiasAffection(allChanges);
}
foreach(Polyobj *polyobj, polyobjs)
foreach(Line *line, polyobj->lines())
{
Segment *segment = line->front().leftSegment();
foreach(BiasSurface *biasSurface, segment->biasSurfaces())
{
biasSurface->updateAffection(allChanges);
}
line->front().leftSegment()->updateBiasAffection(allChanges);
}
foreach(BspLeaf *bspLeaf, bspLeafs)
foreach(BiasSurface *biasSurface, bspLeaf->biasSurfaces())
{
biasSurface->updateAffection(allChanges);
bspLeaf->updateBiasAffection(allChanges);
}
}

Expand Down Expand Up @@ -1406,42 +1400,6 @@ void Map::initBias()
VertexIllum *illums = (VertexIllum *) Z_Calloc(sizeof(*illums) * numVertIllums, PU_MAP, 0);
*/

// Allocate bias surfaces.
foreach(Segment *segment, d->segments)
{
if(!segment->hasLineSide()) continue;

for(int i = 0; i < 3; ++i)
{
BiasSurface *bsuf = new BiasSurface(*segment, i, 4);
segment->setBiasSurface(i, bsuf);
}
}

foreach(BspLeaf *bspLeaf, d->bspLeafs)
{
if(!bspLeaf->hasSector()) continue;
if(bspLeaf->isDegenerate()) continue;

for(int i = 0; i < bspLeaf->sector().planeCount(); ++i)
{
BiasSurface *bsuf = new BiasSurface(*bspLeaf, i, bspLeaf->numFanVertices());
bspLeaf->setBiasSurface(i, bsuf);
}
}

foreach(Polyobj *polyobj, d->polyobjs)
foreach(Line *line, polyobj->lines())
{
Segment *segment = line->front().leftSegment();

for(int i = 0; i < 3; ++i)
{
BiasSurface *bsuf = new BiasSurface(*segment, i, 4);
segment->setBiasSurface(i, bsuf);
}
}

LOG_INFO(String("Completed in %1 seconds.").arg(begunAt.since(), 0, 'g', 2));
}

Expand Down
6 changes: 5 additions & 1 deletion doomsday/client/src/world/polyobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "world/map.h"
#include "Segment"

#ifdef __CLIENT__
# include "render/rend_main.h" // useBias
#endif

#include "render/r_main.h" // validCount

#include "world/polyobj.h"
Expand All @@ -44,7 +48,7 @@ typedef QVector<Vector2d> VertexCoords;
static void notifyGeometryChanged(Polyobj &po)
{
#ifdef __CLIENT__
if(!ddMapSetup)
if(!ddMapSetup && useBias)
{
// Shadow bias must be informed when surfaces move/deform.
foreach(Line *line, po.lines())
Expand Down
6 changes: 5 additions & 1 deletion doomsday/client/src/world/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "world/p_object.h"
#include "world/p_players.h"

#ifdef __CLIENT__
# include "render/rend_main.h" // useBias
#endif

#include "world/sector.h"

using namespace de;
Expand Down Expand Up @@ -163,7 +167,7 @@ DENG2_OBSERVES(Plane, HeightChange)
}

#ifdef __CLIENT__
if(!ddMapSetup)
if(!ddMapSetup && useBias)
{
// Inform the shadow bias of changed geometry.
foreach(BspLeaf *bspLeaf, bspLeafs)
Expand Down
38 changes: 17 additions & 21 deletions doomsday/client/src/world/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

using namespace de;

#ifdef __CLIENT__
typedef QMap<int, BiasSurface *> BiasSurfaces;
#endif

DENG2_PIMPL(Segment)
{
/// Segment on the back side of this (if any). @todo remove me
Expand Down Expand Up @@ -191,34 +195,26 @@ void Segment::setFlags(Flags flagsToChange, FlagOp operation)

BiasSurface &Segment::biasSurface(int group)
{
BiasSurfaces::iterator found = d->biasSurfaces.find(group);
if(found != d->biasSurfaces.end())
{
return **found;
}
/// @throw InvalidGeometryGroupError Attempted with an invalid geometry group.
throw UnknownGeometryGroupError("Segment::biasSurface", QString("Invalid group %1").arg(group));
}

void Segment::setBiasSurface(int group, BiasSurface *newBiasSurface)
{
// Sanity check.
DENG_ASSERT(group >= 0 && group < 3);
DENG_ASSERT(group >= 0 && group < 3); // sanity check
DENG_ASSERT(hasLineSide()); // sanity check

if(d->biasSurfaces.contains(group))
BiasSurfaces::iterator foundAt = d->biasSurfaces.find(group);
if(foundAt != d->biasSurfaces.end())
{
delete d->biasSurfaces.take(group);
return **foundAt;
}

if(newBiasSurface)
{
d->biasSurfaces.insert(group, newBiasSurface);
}
BiasSurface *bsuf = new BiasSurface(*this, group, 4);
d->biasSurfaces.insert(group, bsuf);
return *bsuf;
}

Segment::BiasSurfaces const &Segment::biasSurfaces() const
void Segment::updateBiasAffection(BiasTracker &changes)
{
return d->biasSurfaces;
foreach(BiasSurface *biasSurface, d->biasSurfaces)
{
biasSurface->updateAffection(changes);
}
}

#endif // __CLIENT__
Expand Down

0 comments on commit 817da96

Please sign in to comment.