Implementing Deferred lighting into Source 2013: Difference between revisions
Line 91: | Line 91: | ||
IMesh *m_pMesh_RadiosityScreenGrid[2]; | IMesh *m_pMesh_RadiosityScreenGrid[2]; | ||
CUtlVector< IMesh* > m_hRadiosityDebugMeshList[2]; | CUtlVector< IMesh* > m_hRadiosityDebugMeshList[2]; | ||
#endif | |||
</source> | |||
Now open viewrender.cpp and around lines 81 paste the following code. | |||
<source lang=cpp> | |||
#ifdef DEFERRED_ENABLED | |||
#include "deferred/deferred_shared_common.h" | |||
#include "tier1/callqueue.h" | |||
#endif // DEFERRED_ENABLED | |||
</source> | |||
And now around lines 98 replace static convar with a regular convar like so. | |||
<source lang=cpp> | |||
ConVar r_visocclusion( "r_visocclusion", "0", FCVAR_CHEAT ); | |||
</source> | |||
Now around lines 155 replace static convars with a regular convars like so. | |||
<source lang=cpp> | |||
ConVar r_ForceWaterLeaf( "r_ForceWaterLeaf", "1", 0, "Enable for optimization to water - considers view in leaf under water for purposes of culling" ); | |||
static ConVar mat_drawwater( "mat_drawwater", "1", FCVAR_CHEAT ); | |||
ConVar mat_clipz( "mat_clipz", "1" ); | |||
</source> | |||
And again aorund lines 175 turn the static convar into a regular one. | |||
<source lang=cpp> | |||
ConVar r_eyewaterepsilon( "r_eyewaterepsilon", "7.0f", FCVAR_CHEAT ); | |||
</source> | |||
And around lines 199 replace static int with regular int like so. | |||
<source lang=cpp> | |||
int g_CurrentViewID = VIEW_NONE; | |||
</source> | |||
And around lines 383 put the following code. | |||
<source lang=cpp> | |||
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard | |||
void FlushWorldLists() | |||
{ | |||
g_WorldListCache.Flush(); | |||
} | |||
#endif | |||
</source> | |||
And around lines 397 replace Setup with the fallowing code. | |||
<source lang=cpp> | |||
bool Setup( const CViewSetup &view, int *pClearFlags, SkyboxVisibility_t *pSkyboxVisible, bool bGBuffer = false ); | |||
</source> | |||
And around lines 399 add the following code inside class CSkyboxView : public CRendering3dView. | |||
<source lang=cpp> | |||
#if DEFERRED_ENABLED // @Deferred - Biohazard | |||
bool m_bGBufferPass; | |||
#endif | |||
</source> | |||
And around lines 694 paste the following code. | |||
<source lang=cpp> | |||
// Deferred views | |||
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard | |||
class CBaseWorldBlendViewDeferred : public CBaseWorldView | |||
{ | |||
DECLARE_CLASS( CBaseWorldBlendViewDeferred, CBaseWorldView ); | |||
public: | |||
CBaseWorldBlendViewDeferred(CViewRender *pMainView) : CBaseWorldView( pMainView ) | |||
{ | |||
} | |||
void DrawSetup( float waterHeight, int flags, float waterZAdjust, int iForceViewLeaf = -1, bool bShadowDepth = false ); | |||
void DrawExecute( float waterHeight, view_id_t viewID, float waterZAdjust, bool bShadowDepth = false ); | |||
// BUGBUG this causes all sorts of problems | |||
virtual bool ShouldCacheLists(){ return false; }; | |||
protected: | |||
void DrawOpaqueRenderablesDeferred( bool bNoDecals ); | |||
}; | |||
class CGBufferBlendView : public CBaseWorldBlendViewDeferred | |||
{ | |||
DECLARE_CLASS( CGBufferBlendView, CBaseWorldBlendViewDeferred ); | |||
public: | |||
CGBufferBlendView(CViewRender *pMainView) : CBaseWorldBlendViewDeferred( pMainView ) | |||
{ | |||
} | |||
void Setup( const CViewSetup &view, bool bDrewSkybox ); | |||
void Draw(); | |||
virtual void PushView( float waterHeight ); | |||
virtual void PopView(); | |||
private: | |||
VisibleFogVolumeInfo_t m_fogInfo; | |||
bool m_bDrewSkybox; | |||
}; | |||
abstract_class CBaseShadowBlendView : public CBaseWorldBlendViewDeferred | |||
{ | |||
DECLARE_CLASS( CBaseShadowBlendView, CBaseWorldBlendViewDeferred ); | |||
public: | |||
CBaseShadowBlendView(CViewRender *pMainView) : CBaseWorldBlendViewDeferred( pMainView ) | |||
{ | |||
m_bOutputRadiosity = false; | |||
}; | |||
void Setup( const CViewSetup &view, | |||
ITexture *pDepthTexture, | |||
ITexture *pDummyTexture ); | |||
void SetupRadiosityTargets( | |||
ITexture *pAlbedoTexture, | |||
ITexture *pNormalTexture ); | |||
void SetRadiosityOutputEnabled( bool bEnabled ); | |||
void Draw(); | |||
virtual bool AdjustView( float waterHeight ); | |||
virtual void PushView( float waterHeight ); | |||
virtual void PopView(); | |||
virtual void CalcShadowView() = 0; | |||
virtual void CommitData(){}; | |||
virtual int GetShadowMode() = 0; | |||
private: | |||
ITexture *m_pDepthTexture; | |||
ITexture *m_pDummyTexture; | |||
ITexture *m_pRadAlbedoTexture; | |||
ITexture *m_pRadNormalTexture; | |||
ViewCustomVisibility_t shadowVis; | |||
bool m_bOutputRadiosity; | |||
}; | |||
class COrthoShadowBlendView : public CBaseShadowBlendView | |||
{ | |||
DECLARE_CLASS( COrthoShadowBlendView, CBaseShadowBlendView ); | |||
public: | |||
COrthoShadowBlendView(CViewRender *pMainView, const int &index) | |||
: CBaseShadowBlendView( pMainView ) | |||
{ | |||
iCascadeIndex = index; | |||
} | |||
virtual void CalcShadowView(); | |||
virtual void CommitData(); | |||
virtual int GetShadowMode(){ | |||
return DEFERRED_SHADOW_MODE_ORTHO; | |||
}; | |||
private: | |||
int iCascadeIndex; | |||
}; | |||
class CDualParaboloidShadowBlendView : public CBaseShadowBlendView | |||
{ | |||
DECLARE_CLASS( CDualParaboloidShadowBlendView, CBaseShadowBlendView ); | |||
public: | |||
CDualParaboloidShadowBlendView(CViewRender *pMainView, | |||
def_light_t *pLight, | |||
const bool &bSecondary) | |||
: CBaseShadowBlendView( pMainView ) | |||
{ | |||
m_pLight = pLight; | |||
m_bSecondary = bSecondary; | |||
} | |||
virtual bool AdjustView( float waterHeight ); | |||
virtual void PushView( float waterHeight ); | |||
virtual void PopView(); | |||
virtual void CalcShadowView(); | |||
virtual int GetShadowMode(){ | |||
return DEFERRED_SHADOW_MODE_DPSM; | |||
}; | |||
private: | |||
bool m_bSecondary; | |||
def_light_t *m_pLight; | |||
}; | |||
class CSpotLightShadowBlendView : public CBaseShadowBlendView | |||
{ | |||
DECLARE_CLASS( CSpotLightShadowBlendView, CBaseShadowBlendView ); | |||
public: | |||
CSpotLightShadowBlendView(CViewRender *pMainView, | |||
def_light_t *pLight, int index ) | |||
: CBaseShadowBlendView( pMainView ) | |||
{ | |||
m_pLight = pLight; | |||
m_iIndex = index; | |||
} | |||
virtual void CalcShadowView(); | |||
virtual void CommitData(); | |||
virtual int GetShadowMode(){ | |||
return DEFERRED_SHADOW_MODE_PROJECTED; | |||
}; | |||
private: | |||
def_light_t *m_pLight; | |||
int m_iIndex; | |||
}; | |||
#endif | #endif | ||
</source> | </source> |
Revision as of 04:41, 13 April 2024


Having deferred lighting in your mod comes with quite a lot of upsides as with it, you can have volumetric lighting, realtime shadows and lighting from the sun and from other light sources, while still keeping the performance steady which is something that Source's regular dynamic light entities would be incapable of (aka projected textures).
Implementing into your Source 2013SP mod
Now we are going to use Lambda Wars's implementation for it as its an enhanced/upgraded version of Alien Swarm Deferred's, but beware implementing deferred lighting in your mod is extremely complex requiring you to modify client and server side code, and even add new shaders to your game. Among other things source's default shaders are also needed to be modified.
Clientside
Firstly we are going to modify cdll_client_int.cpp. Now put the following code at the top of the file around lines 175 but it must be before #include "tier0/memdbgon.h".
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
#include "deferred/deferred_shared_common.h"
#endif
Now aound lines 1816 in void CHLClient::ResetStringTablePointers() place the following code.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
g_pStringTable_LightCookies = NULL;
#endif
Now around lines 2058 put the following code inside void CHLClient::InstallStringTableCallback( const char *tableName ). And after that we should be done with cdll_client_int.cpp.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
else if ( !Q_strcasecmp( tableName, COOKIE_STRINGTBL_NAME ) )
{
g_pStringTable_LightCookies = networkstringtable->FindTable( tableName );
g_pStringTable_LightCookies->SetStringChangedCallback( NULL, OnCookieTableChanged );
}
#endif // DEFERRED_ENABLED
Now open viewrender.h and around lines 19 paste the following code.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
#include "../../materialsystem/stdshaders/deferred_global_common.h"
#endif
And around lines 39 paste this too.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
struct def_light_t;
#endif
Now around lines 96, but the following code into enum view_id_t.
// @Deferred - Biohazard
VIEW_DEFERRED_GBUFFER = 8,
VIEW_DEFERRED_SHADOW = 9,
And around lines 263, but the following code into class CRendering3dView : public CBase3dView.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
void PushComposite();
void PopComposite();
static void PushGBuffer( bool bInitial, float zScale = 1.0f, bool bClearDepth = true );
static void PopGBuffer();
#endif
And around lines 505, paste the following code, and we should be done with viewrender.h.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
void ProcessDeferredGlobals( const CViewSetup &view );
void ViewDrawGBuffer( const CViewSetup &view, bool &bDrew3dSkybox, SkyboxVisibility_t &nSkyboxVisible,
bool bDrawViewModel );
void BeginRadiosity( const CViewSetup &view );
void UpdateRadiosityPosition();
void PerformRadiosityGlobal( const int iRadiosityCascade, const CViewSetup &view );
void EndRadiosity( const CViewSetup &view );
void DebugRadiosity( const CViewSetup &view );
IMesh *GetRadiosityScreenGrid( const int iCascade );
IMesh *CreateRadiosityScreenGrid( const Vector2D &vecViewportBase, const float flWorldStepSize );
void PerformLighting( const CViewSetup &view );
void ResetCascadeDelay();
void RenderCascadedShadows( const CViewSetup &view, const bool bEnableRadiosity );
public:
virtual void DrawLightShadowView( const CViewSetup &view, int iDesiredShadowmap, def_light_t *l );
protected:
float m_flRenderDelay[SHADOW_NUM_CASCADES];
Vector m_vecRadiosityOrigin[2];
IMesh *m_pMesh_RadiosityScreenGrid[2];
CUtlVector< IMesh* > m_hRadiosityDebugMeshList[2];
#endif
Now open viewrender.cpp and around lines 81 paste the following code.
#ifdef DEFERRED_ENABLED
#include "deferred/deferred_shared_common.h"
#include "tier1/callqueue.h"
#endif // DEFERRED_ENABLED
And now around lines 98 replace static convar with a regular convar like so.
ConVar r_visocclusion( "r_visocclusion", "0", FCVAR_CHEAT );
Now around lines 155 replace static convars with a regular convars like so.
ConVar r_ForceWaterLeaf( "r_ForceWaterLeaf", "1", 0, "Enable for optimization to water - considers view in leaf under water for purposes of culling" );
static ConVar mat_drawwater( "mat_drawwater", "1", FCVAR_CHEAT );
ConVar mat_clipz( "mat_clipz", "1" );
And again aorund lines 175 turn the static convar into a regular one.
ConVar r_eyewaterepsilon( "r_eyewaterepsilon", "7.0f", FCVAR_CHEAT );
And around lines 199 replace static int with regular int like so.
int g_CurrentViewID = VIEW_NONE;
And around lines 383 put the following code.
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
void FlushWorldLists()
{
g_WorldListCache.Flush();
}
#endif
And around lines 397 replace Setup with the fallowing code.
bool Setup( const CViewSetup &view, int *pClearFlags, SkyboxVisibility_t *pSkyboxVisible, bool bGBuffer = false );
And around lines 399 add the following code inside class CSkyboxView : public CRendering3dView.
#if DEFERRED_ENABLED // @Deferred - Biohazard
bool m_bGBufferPass;
#endif
And around lines 694 paste the following code.
// Deferred views
#ifdef DEFERRED_ENABLED // @Deferred - Biohazard
class CBaseWorldBlendViewDeferred : public CBaseWorldView
{
DECLARE_CLASS( CBaseWorldBlendViewDeferred, CBaseWorldView );
public:
CBaseWorldBlendViewDeferred(CViewRender *pMainView) : CBaseWorldView( pMainView )
{
}
void DrawSetup( float waterHeight, int flags, float waterZAdjust, int iForceViewLeaf = -1, bool bShadowDepth = false );
void DrawExecute( float waterHeight, view_id_t viewID, float waterZAdjust, bool bShadowDepth = false );
// BUGBUG this causes all sorts of problems
virtual bool ShouldCacheLists(){ return false; };
protected:
void DrawOpaqueRenderablesDeferred( bool bNoDecals );
};
class CGBufferBlendView : public CBaseWorldBlendViewDeferred
{
DECLARE_CLASS( CGBufferBlendView, CBaseWorldBlendViewDeferred );
public:
CGBufferBlendView(CViewRender *pMainView) : CBaseWorldBlendViewDeferred( pMainView )
{
}
void Setup( const CViewSetup &view, bool bDrewSkybox );
void Draw();
virtual void PushView( float waterHeight );
virtual void PopView();
private:
VisibleFogVolumeInfo_t m_fogInfo;
bool m_bDrewSkybox;
};
abstract_class CBaseShadowBlendView : public CBaseWorldBlendViewDeferred
{
DECLARE_CLASS( CBaseShadowBlendView, CBaseWorldBlendViewDeferred );
public:
CBaseShadowBlendView(CViewRender *pMainView) : CBaseWorldBlendViewDeferred( pMainView )
{
m_bOutputRadiosity = false;
};
void Setup( const CViewSetup &view,
ITexture *pDepthTexture,
ITexture *pDummyTexture );
void SetupRadiosityTargets(
ITexture *pAlbedoTexture,
ITexture *pNormalTexture );
void SetRadiosityOutputEnabled( bool bEnabled );
void Draw();
virtual bool AdjustView( float waterHeight );
virtual void PushView( float waterHeight );
virtual void PopView();
virtual void CalcShadowView() = 0;
virtual void CommitData(){};
virtual int GetShadowMode() = 0;
private:
ITexture *m_pDepthTexture;
ITexture *m_pDummyTexture;
ITexture *m_pRadAlbedoTexture;
ITexture *m_pRadNormalTexture;
ViewCustomVisibility_t shadowVis;
bool m_bOutputRadiosity;
};
class COrthoShadowBlendView : public CBaseShadowBlendView
{
DECLARE_CLASS( COrthoShadowBlendView, CBaseShadowBlendView );
public:
COrthoShadowBlendView(CViewRender *pMainView, const int &index)
: CBaseShadowBlendView( pMainView )
{
iCascadeIndex = index;
}
virtual void CalcShadowView();
virtual void CommitData();
virtual int GetShadowMode(){
return DEFERRED_SHADOW_MODE_ORTHO;
};
private:
int iCascadeIndex;
};
class CDualParaboloidShadowBlendView : public CBaseShadowBlendView
{
DECLARE_CLASS( CDualParaboloidShadowBlendView, CBaseShadowBlendView );
public:
CDualParaboloidShadowBlendView(CViewRender *pMainView,
def_light_t *pLight,
const bool &bSecondary)
: CBaseShadowBlendView( pMainView )
{
m_pLight = pLight;
m_bSecondary = bSecondary;
}
virtual bool AdjustView( float waterHeight );
virtual void PushView( float waterHeight );
virtual void PopView();
virtual void CalcShadowView();
virtual int GetShadowMode(){
return DEFERRED_SHADOW_MODE_DPSM;
};
private:
bool m_bSecondary;
def_light_t *m_pLight;
};
class CSpotLightShadowBlendView : public CBaseShadowBlendView
{
DECLARE_CLASS( CSpotLightShadowBlendView, CBaseShadowBlendView );
public:
CSpotLightShadowBlendView(CViewRender *pMainView,
def_light_t *pLight, int index )
: CBaseShadowBlendView( pMainView )
{
m_pLight = pLight;
m_iIndex = index;
}
virtual void CalcShadowView();
virtual void CommitData();
virtual int GetShadowMode(){
return DEFERRED_SHADOW_MODE_PROJECTED;
};
private:
def_light_t *m_pLight;
int m_iIndex;
};
#endif
Serverside
Shaders

Lamba War's source code Reference project : Source 2013MP deferred this project uses the older Alien swarm deferred's version but it still should give us some idea what needs to be changed.
Cpp files and header files needed to be modified or added
- Client
- cdll_client_int.cpp ,modification needed
- viewrender.h ,modification needed
- viewrender.cpp ,modification needed
- flashlighteffect.h ,modification needed
- flashlighteffect.cpp ,modification needed
- c_entityflame.cpp ,modification needed although it's not necessary it's still a quality of life feature
- deferred ,This folder contents are needed which are:
- cascade_t.cpp
- cascade_t.h
- DefCookieProjectable.cpp
- DefCookieProjectable.h
- DefCookieTexture.cpp
- DefCookieTexture.h
- IDefCookie.h
- IDeferredExtClient.cpp
- cdeferred_manager_client.cpp
- cdeferred_manager_client.h
- clight_editor.cpp
- clight_editor.h
- clight_manager.cpp
- clight_manager.h
- def_light_t.cpp
- def_light_t.h
- deferred_client_common.cpp
- deferred_client_common.h
- deferred_rt.cpp
- deferred_rt.h
- flashlighteffect_deferred.cpp
- flashlighteffect_deferred.h
- viewrender_deferred.cpp
- viewrender_deferred.h
- vgui ,This folder is also needed
- projectable_factory.cpp
- projectable_factory.h
- vgui_deferred.h
- vgui_editor_controls.cpp
- vgui_editor_controls.h
- vgui_editor_props.cpp
- vgui_editor_props.h
- vgui_editor_root.cpp
- vgui_marquee.cpp
- vgui_marquee.h
- vgui_particles.cpp
- vgui_particles.h
- vgui_particles.cpp
- vgui_projectable.cpp
- vgui_projectable.h
- Server
- gameinterface.cpp ,modification needed
- lights.cpp ,modification needed
- EntityFlame.h ,modification needed although it's not necessary it's still a quality of life feature
- deferred ,This folder contents are needed which are:
- cdeferred_manager_server.cpp
- deferred_server_common.h
- cdeferred_manager_server.h
- Shared
- deferred ,This folder contents are needed which are:
- CDefLight.cpp
- CDefLight.h
- CDefLightContainer.cpp
- CDefLightContainer.h
- CDefLightGlobal.cpp
- CDefLightGlobal.h
- deferred_shared_common.cpp
- deferred_shared_common.h
- ssemath_ext.h
- deferred ,This folder contents are needed which are:
- Public
- renderparm.h ,modification needed
Shaders
- common_deferred_fxc.h
- deferred_context.h
- deferred_global_common.h
- deferred_includes.h
- deferred_utility.h
- defpass_composite.h
- defpass_gbuffer.h
- defpass_shadow.h
- IDeferredExt.h
- lighting_helper.h
- lighting_pass_basic.h
- lighting_pass_volum.h
- lightshafts_helper.h
- vertexlitgeneric_dx9_helper.h
- lightmappedgeneric_deferred_ps30.h
- lightmappedgeneric_dx9_helper.h
- debug_lightingctrl.cpp
- debug_radiosity_grid.cpp
- deferred_decalModulate.cpp
- deferred_model.cpp
- deferred_brush.cpp
- defpass_composite.cpp
- deferred_utility.cpp
- defpass_gbuffer.cpp
- defpass_shadow.cpp
- GlobalLitGeneric.cpp
- IDeferredExt.cpp
- lighting_global.cpp
- lighting_pass_basic.cpp
- lighting_pass_volum.cpp
- lighting_volume.cpp
- lighting_world.cpp
- radiosity_blend.cpp
- radiosity_global.cpp
- radiosity_propagate.cpp
- volume_prepass.cpp
- volume_blend.cpp
- unlitgeneric_dx9.cpp
- vertexlitgeneric_dx9.cpp
- vertexlitgeneric_dx9_helper.cpp
- lightmappedgeneric_dx9.cpp
- lightmappedgeneric_dx9_helper.cpp
- lightmappedgeneric_dx9_deferred_helper.cpp
- phong_dx9_helper.cpp
- gbuffer_vs30.fxc
- gbuffer_ps30.fxc
- gbuffer_defshading_ps30.fxc
- shadowpass_vs30.fxc
- shadowpass_ps30.fxc
- composite_vs30.fxc
- composite_ps30.fxc
- defconstruct_vs30.fxc
- decalmodulate_vs20.fxc
- decalmodulate_ps2x.fxc
- lightingpass_global_ps30.fxc
- lightingpass_point_ps30.fxc
- lightingpass_spot_ps30.fxc
- screenspace_shading_ps30.fxc
- screenspace_combine_ps30.fxc
- volume_blend_ps30.fxc
- volume_prepass_vs30.fxc
- volume_prepass_ps30.fxc
- volumpass_point_ps30.fxc
- volumpass_spot_ps30.fxc
- radiosity_gen_global_ps30.fxc
- radiosity_gen_vs30.fxc
- radiosity_propagate_ps30.fxc
- radiosity_propagate_vs30.fxc
- radiosity_blend_ps30.fxc
- screenspace_vs20.fxc
- gaussianblur_6_ps30.fxc
- debug_shadow_ortho_ps30.fxc
- debug_lighting_ctrl_ps30.fxc
- debug_radiosity_grid_ps30.fxc
- debug_radiosity_grid_vs30.fxc
- globallitgeneric_ps30.fxc
- globallitgeneric_vs30.fxc
- phong_deferred_ps30.fxc
- lightmappedgeneric_deferred_vs30.fxc
- lightmappedgeneric_deferred_ps30.fxc
- vertexlit_and_unlit_generic_bump_deferred_ps30.fxc