Skip to content

Commit

Permalink
Shadow Bias: Integrated BiasSurface, optimized linking mechanism
Browse files Browse the repository at this point in the history
The previous implementation maintained a global linked list of all
the bias surfaces in the map. This meant that the unlink algorithm
(which had to support dynamic unlinking) was of the order O(n) and
consequently unlinking often took far longer than it should.

The revised mechanism dispenses with the global list entirely and
instead a simple traversal of the Map element LUTs is used when
performing bias lighting updates.
  • Loading branch information
danij-deng committed Jun 25, 2013
1 parent 89a5612 commit b0a311d
Show file tree
Hide file tree
Showing 12 changed files with 588 additions and 270 deletions.
27 changes: 19 additions & 8 deletions doomsday/client/include/world/bspleaf.h
Expand Up @@ -31,12 +31,15 @@
#include "Line"
#include "Mesh"

#ifdef __CLIENT__
# include "BiasSurface"
#endif

class Sector;
class Segment;
struct polyobj_s;

#ifdef __CLIENT__
struct BiasSurface;
struct ShadowLink;
#endif

Expand Down Expand Up @@ -78,8 +81,11 @@ class BspLeaf : public de::MapElement
/*
* Linked-element lists/sets:
*/
typedef QSet<polyobj_s *> Polyobjs;
typedef QList<Segment *> Segments;
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 @@ -284,12 +290,17 @@ class BspLeaf : public de::MapElement
/**
* Assign a new bias surface to the specified geometry @a group.
*
* @param group Geometry group identifier for the surface.
* @param biasSurface New BiasSurface for the identified @a group. Any
* existing bias surface will be replaced (destroyed).
* Ownership is given to the BSP leaf.
* @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.
*/
void setBiasSurface(int group, BiasSurface *biasSurface);
BiasSurfaces const &biasSurfaces() const;

/**
* Returns a pointer to the first ShadowLink; otherwise @c 0.
Expand Down
113 changes: 95 additions & 18 deletions doomsday/client/include/world/map.h
Expand Up @@ -32,6 +32,8 @@

#ifdef __CLIENT__
# include "world/world.h"

# include "BiasSource"
#endif

class BspLeaf;
Expand All @@ -43,6 +45,7 @@ class Segment;
class Vertex;

#ifdef __CLIENT__
class BiasSurface;

struct clmoinfo_s;

Expand Down Expand Up @@ -112,6 +115,9 @@ class Map
#ifdef __CLIENT__
/// Required light grid is missing. @ingroup errors
DENG2_ERROR(MissingLightGridError);

/// Attempted to add a new element when already full. @ingroup errors
DENG2_ERROR(FullError);
#endif

/*
Expand Down Expand Up @@ -146,6 +152,8 @@ class Map
#ifdef __CLIENT__
typedef QSet<Plane *> PlaneSet;
typedef QSet<Surface *> SurfaceSet;

typedef QList<BiasSource *> BiasSources;
#endif

public: /// @todo make private:
Expand Down Expand Up @@ -185,6 +193,11 @@ class Map
*/
static void initDummies();

/**
* To be called following an engine reset to update the map state.
*/
void update();

/**
* Returns the universal resource identifier (URI) attributed to the map.
*/
Expand Down Expand Up @@ -565,6 +578,71 @@ class Map
*/
Generators &generators();

/**
* Attempt to add a new bias light source to the map (a copy is made).
*
* @note At most @ref MAX_BIAS_SOURCES are supported for technical reasons.
*
* @return Reference to the newly added bias source.
*
* @see biasSourceCount()
* @throws FullError Once capacity is reached.
*/
BiasSource &addBiasSource(BiasSource const &biasSource = BiasSource());

/**
* Removes the specified bias light source from the map.
*
* @see removeAllBiasSources()
*/
void removeBiasSource(int which);

/**
* Remove all bias sources from the map.
*
* @see removeBiasSource()
*/
void removeAllBiasSources();

/**
* Provides a list of all the bias sources in the map.
*/
BiasSources const &biasSources() const;

/**
* Returns the total number of bias sources in the map.
*/
inline int biasSourceCount() const { return biasSources().count(); }

/**
* Returns the time in milliseconds when the current render frame began. Used
* for interpolation purposes.
*/
uint biasCurrentTime() const;

/**
* Returns the frameCount of the current render frame. Used for tracking changes
* to bias sources/surfaces.
*/
uint biasLastChangeOnFrame() const;

/**
* Lookup a bias source in the map by it's unique @a index.
*/
BiasSource *biasSource(int index) const;

/**
* Finds the bias source nearest to the specified map space @a point.
*
* @note This result is not cached. May return @c 0 if no bias sources exist.
*/
BiasSource *biasSourceNear(de::Vector3d const &point) const;

/**
* Lookup the unique index for the given bias @a source.
*/
int toIndex(BiasSource const &source) const;

/// @todo Should be private?
void initClMobjs();

Expand Down Expand Up @@ -638,11 +716,6 @@ class Map
*/
SurfaceSet /*const*/ &glowingSurfaces();

/**
* $smoothmatoffset: interpolate the visual offset.
*/
void lerpScrollingSurfaces(bool resetNextViewer = false);

/**
* $smoothmatoffset: Roll the surface material offset tracker buffers.
*/
Expand All @@ -653,11 +726,6 @@ class Map
*/
SurfaceSet /*const*/ &scrollingSurfaces();

/**
* $smoothplane: interpolate the visual offset.
*/
void lerpTrackedPlanes(bool resetNextViewer = false);

/**
* $smoothplane: Roll the height tracker buffers.
*/
Expand Down Expand Up @@ -707,6 +775,14 @@ class Map
*/
void initPolyobjs();

/**
* To be called in response to a Material property changing which may
* require updating any map surfaces which are presently using it.
*
* @todo Replace with a de::Observers-based mechanism.
*/
void updateSurfacesOnMaterialChange(Material &material);

#ifdef __CLIENT__
/**
* Fixing the sky means that for adjacent sky sectors the lower sky
Expand All @@ -718,19 +794,20 @@ class Map
void buildSurfaceLists();

/**
* @todo Replace with a de::Observers-based mechanism.
* Initializes bias lighting for the map. New light sources are initialized
* from the loaded Light definitions. Map surfaces are prepared for tracking
* rays.
*
* Must be called before rendering a frame with bias lighting enabled.
*/
void updateMissingMaterialsForLinesOfSector(Sector const &sec);

#endif // __CLIENT__
void initBias();

/**
* To be called in response to a Material property changing which may
* require updating any map surfaces which are presently using it.
*
* @todo Replace with a de::Observers-based mechanism.
*/
void updateSurfacesOnMaterialChange(Material &material);
void updateMissingMaterialsForLinesOfSector(Sector const &sec);

#endif // __CLIENT__

public:
/*
Expand Down
21 changes: 15 additions & 6 deletions doomsday/client/include/world/segment.h
Expand Up @@ -31,7 +31,7 @@
#include "Vertex"

#ifdef __CLIENT__
struct BiasSurface;
# include "BiasSurface"
#endif

class Sector;
Expand All @@ -58,6 +58,10 @@ class Segment : public de::MapElement
/// The referenced geometry group does not exist. @ingroup errors
DENG2_ERROR(UnknownGeometryGroupError);

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

enum Flag
Expand Down Expand Up @@ -247,12 +251,17 @@ class Segment : public de::MapElement
/**
* Assign a new bias surface to the specified geometry @a group.
*
* @param group Geometry group identifier for the surface.
* @param biasSurface New BiasSurface for the identified @a group. Any
* existing bias surface will be replaced (destroyed).
* Ownership is given to the segment.
* @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.
*/
void setBiasSurface(int group, BiasSurface *biasSurface);
BiasSurfaces const &biasSurfaces() const;

#endif // __CLIENT__

Expand Down
42 changes: 9 additions & 33 deletions doomsday/client/src/render/r_main.cpp
Expand Up @@ -33,6 +33,9 @@
#include "de_misc.h"
#include "de_ui.h"

#ifdef __CLIENT__
# include "edit_bias.h"
#endif
#include "gl/svg.h"
#include "world/p_players.h"
#include "world/p_objlink.h"
Expand Down Expand Up @@ -706,34 +709,6 @@ void R_NewSharpWorld()
#endif
}

void R_CreateMobjLinks()
{
#ifdef __CLIENT__
#ifdef DD_PROFILE
static int p;

if(++p > 40)
{
p = 0;
PRINT_PROF( PROF_MOBJ_INIT_ADD );
}
#endif

if(!App_World().hasMap()) return;

BEGIN_PROF( PROF_MOBJ_INIT_ADD );

foreach(Sector *sector, App_World().map().sectors())
for(mobj_t *iter = sector->firstMobj(); iter; iter = iter->sNext)
{
R_ObjlinkCreate(*iter); // For spreading purposes.
}

END_PROF( PROF_MOBJ_INIT_ADD );

#endif
}

void R_UpdateViewer(int consoleNum)
{
DENG_ASSERT(consoleNum >= 0 && consoleNum < DDMAXPLAYERS);
Expand Down Expand Up @@ -991,7 +966,7 @@ DENG_EXTERN_C void R_RenderPlayerView(int num)

if(App_World().hasMap())
{
Rend_RenderMap();
Rend_RenderMap(App_World().map());
}

// Orthogonal projection to the view window.
Expand Down Expand Up @@ -1282,8 +1257,8 @@ void Rend_CacheForMap()
spritedef_t *sprDef = &sprites[i];

if(App_World().map().thinkers()
.iterate(reinterpret_cast<thinkfunc_t>(gx.MobjThinker),
0x1/* All mobjs are public*/, findSpriteOwner, sprDef))
.iterate(reinterpret_cast<thinkfunc_t>(gx.MobjThinker), 0x1/*mobjs are public*/,
findSpriteOwner, sprDef))
{
// This sprite is used by some state of at least one mobj.

Expand All @@ -1307,8 +1282,9 @@ void Rend_CacheForMap()
if(useModels && precacheSkins)
{
// All mobjs are public.
App_World().map().thinkers().iterate(reinterpret_cast<thinkfunc_t>(gx.MobjThinker),
0x1, Models_CacheForMobj, NULL);
App_World().map().thinkers()
.iterate(reinterpret_cast<thinkfunc_t>(gx.MobjThinker), 0x1,
Models_CacheForMobj);
}
}

Expand Down
3 changes: 1 addition & 2 deletions doomsday/client/src/resource/material.cpp
Expand Up @@ -26,9 +26,8 @@

#include "r_util.h" // R_NameForBlendMode

//#include "api_map.h"
#include "audio/s_environ.h"
#include "world/world.h" // theMap - Remove me
#include "world/map.h"

#include "resource/r_data.h" // R_FindTextureByResourceUri

Expand Down

0 comments on commit b0a311d

Please sign in to comment.