1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Functionality to render a glowing outline around client renderable objects.
//
//===============================================================================
#include "cbase.h"
#include "glow_outline_effect.h"
#include "model_types.h"
#include "shaderapi/ishaderapi.h"
#include "materialsystem/imaterialvar.h"
#include "materialsystem/itexture.h"
#include "view_shared.h"
#include "viewpostprocess.h"
#define FULL_FRAME_TEXTURE "_rt_FullFrameFB"
#ifdef GLOWS_ENABLE
ConVar glow_outline_effect_enable( "glow_outline_effect_enable", "1", FCVAR_ARCHIVE, "Enable entity outline glow effects." );
ConVar glow_outline_effect_width( "glow_outline_width", "10.0f", FCVAR_CHEAT, "Width of glow outline effect in screen space." );
extern bool g_bDumpRenderTargets; // in viewpostprocess.cpp
CGlowObjectManager g_GlowObjectManager;
struct ShaderStencilState_t
{
bool m_bEnable;
StencilOperation_t m_FailOp;
StencilOperation_t m_ZFailOp;
StencilOperation_t m_PassOp;
StencilComparisonFunction_t m_CompareFunc;
int m_nReferenceValue;
uint32 m_nTestMask;
uint32 m_nWriteMask;
ShaderStencilState_t()
{
m_bEnable = false;
m_PassOp = m_FailOp = m_ZFailOp = STENCILOPERATION_KEEP;
m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS;
m_nReferenceValue = 0;
m_nTestMask = m_nWriteMask = 0xFFFFFFFF;
}
void SetStencilState( CMatRenderContextPtr &pRenderContext )
{
pRenderContext->SetStencilEnable( m_bEnable );
pRenderContext->SetStencilFailOperation( m_FailOp );
pRenderContext->SetStencilZFailOperation( m_ZFailOp );
pRenderContext->SetStencilPassOperation( m_PassOp );
pRenderContext->SetStencilCompareFunction( m_CompareFunc );
pRenderContext->SetStencilReferenceValue( m_nReferenceValue );
pRenderContext->SetStencilTestMask( m_nTestMask );
pRenderContext->SetStencilWriteMask( m_nWriteMask );
}
};
void CGlowObjectManager::RenderGlowEffects( const CViewSetup *pSetup, int nSplitScreenSlot )
{
if ( g_pMaterialSystemHardwareConfig->SupportsPixelShaders_2_0() )
{
if ( glow_outline_effect_enable.GetBool() )
{
CMatRenderContextPtr pRenderContext( materials );
int nX, nY, nWidth, nHeight;
pRenderContext->GetViewport( nX, nY, nWidth, nHeight );
PIXEvent _pixEvent( pRenderContext, "EntityGlowEffects" );
ApplyEntityGlowEffects( pSetup, nSplitScreenSlot, pRenderContext, glow_outline_effect_width.GetFloat(), nX, nY, nWidth, nHeight );
}
}
}
static void SetRenderTargetAndViewPort( ITexture *rt, int w, int h )
{
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->SetRenderTarget(rt);
pRenderContext->Viewport(0,0,w,h);
}
void CGlowObjectManager::RenderGlowModels( const CViewSetup *pSetup, int nSplitScreenSlot, CMatRenderContextPtr &pRenderContext )
{
//==========================================================================================//
// This renders solid pixels with the correct coloring for each object that needs the glow. //
// After this function returns, this image will then be blurred and added into the frame //
// buffer with the objects stenciled out. //
//==========================================================================================//
pRenderContext->PushRenderTargetAndViewport();
// Save modulation color and blend
Vector vOrigColor;
render->GetColorModulation( vOrigColor.Base() );
float flOrigBlend = render->GetBlend();
// Get pointer to FullFrameFB
ITexture *pRtFullFrame = NULL;
pRtFullFrame = materials->FindTexture( FULL_FRAME_TEXTURE, TEXTURE_GROUP_RENDER_TARGET );
SetRenderTargetAndViewPort( pRtFullFrame, pSetup->width, pSetup->height );
pRenderContext->ClearColor3ub( 0, 0, 0 );
pRenderContext->ClearBuffers( true, false, false );
// Set override material for glow color
IMaterial *pMatGlowColor = NULL;
pMatGlowColor = materials->FindMaterial( "dev/glow_color", TEXTURE_GROUP_OTHER, true );
g_pStudioRender->ForcedMaterialOverride( pMatGlowColor );
ShaderStencilState_t stencilState;
stencilState.m_bEnable = false;
stencilState.m_nReferenceValue = 0;
stencilState.m_nTestMask = 0xFF;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS;
stencilState.m_PassOp = STENCILOPERATION_KEEP;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_KEEP;
stencilState.SetStencilState( pRenderContext );
//==================//
// Draw the objects //
//==================//
for ( int i = 0; i < m_GlowObjectDefinitions.Count(); ++ i )
{
if ( m_GlowObjectDefinitions[i].IsUnused() || !m_GlowObjectDefinitions[i].ShouldDraw( nSplitScreenSlot ) )
continue;
render->SetBlend( m_GlowObjectDefinitions[i].m_flGlowAlpha );
Vector vGlowColor = m_GlowObjectDefinitions[i].m_vGlowColor * m_GlowObjectDefinitions[i].m_flGlowAlpha;
render->SetColorModulation( &vGlowColor[0] ); // This only sets rgb, not alpha
m_GlowObjectDefinitions[i].DrawModel();
}
if ( g_bDumpRenderTargets )
{
DumpTGAofRenderTarget( pSetup->width, pSetup->height, "GlowModels" );
}
g_pStudioRender->ForcedMaterialOverride( NULL );
render->SetColorModulation( vOrigColor.Base() );
render->SetBlend( flOrigBlend );
ShaderStencilState_t stencilStateDisable;
stencilStateDisable.m_bEnable = false;
stencilStateDisable.SetStencilState( pRenderContext );
pRenderContext->PopRenderTargetAndViewport();
}
void CGlowObjectManager::ApplyEntityGlowEffects( const CViewSetup *pSetup, int nSplitScreenSlot, CMatRenderContextPtr &pRenderContext, float flBloomScale, int x, int y, int w, int h )
{
//=======================================================//
// Render objects into stencil buffer //
//=======================================================//
// Set override shader to the same simple shader we use to render the glow models
IMaterial *pMatGlowColor = materials->FindMaterial( "dev/glow_color", TEXTURE_GROUP_OTHER, true );
g_pStudioRender->ForcedMaterialOverride( pMatGlowColor );
ShaderStencilState_t stencilStateDisable;
stencilStateDisable.m_bEnable = false;
float flSavedBlend = render->GetBlend();
// Set alpha to 0 so we don't touch any color pixels
render->SetBlend( 0.0f );
pRenderContext->OverrideDepthEnable( true, false );
int iNumGlowObjects = 0;
for ( int i = 0; i < m_GlowObjectDefinitions.Count(); ++ i )
{
if ( m_GlowObjectDefinitions[i].IsUnused() || !m_GlowObjectDefinitions[i].ShouldDraw( nSplitScreenSlot ) )
continue;
if ( m_GlowObjectDefinitions[i].m_bRenderWhenOccluded || m_GlowObjectDefinitions[i].m_bRenderWhenUnoccluded )
{
if ( m_GlowObjectDefinitions[i].m_bRenderWhenOccluded && m_GlowObjectDefinitions[i].m_bRenderWhenUnoccluded )
{
ShaderStencilState_t stencilState;
stencilState.m_bEnable = true;
stencilState.m_nReferenceValue = 1;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS;
stencilState.m_PassOp = STENCILOPERATION_REPLACE;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_REPLACE;
stencilState.SetStencilState( pRenderContext );
m_GlowObjectDefinitions[i].DrawModel();
}
else if ( m_GlowObjectDefinitions[i].m_bRenderWhenOccluded )
{
ShaderStencilState_t stencilState;
stencilState.m_bEnable = true;
stencilState.m_nReferenceValue = 1;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS;
stencilState.m_PassOp = STENCILOPERATION_KEEP;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_REPLACE;
stencilState.SetStencilState( pRenderContext );
m_GlowObjectDefinitions[i].DrawModel();
}
else if ( m_GlowObjectDefinitions[i].m_bRenderWhenUnoccluded )
{
ShaderStencilState_t stencilState;
stencilState.m_bEnable = true;
stencilState.m_nReferenceValue = 2;
stencilState.m_nTestMask = 0x1;
stencilState.m_nWriteMask = 0x3;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_EQUAL;
stencilState.m_PassOp = STENCILOPERATION_INCRSAT;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_REPLACE;
stencilState.SetStencilState( pRenderContext );
m_GlowObjectDefinitions[i].DrawModel();
}
}
iNumGlowObjects++;
}
// Need to do a 2nd pass to warm stencil for objects which are rendered only when occluded
for ( int i = 0; i < m_GlowObjectDefinitions.Count(); ++ i )
{
if ( m_GlowObjectDefinitions[i].IsUnused() || !m_GlowObjectDefinitions[i].ShouldDraw( nSplitScreenSlot ) )
continue;
if ( m_GlowObjectDefinitions[i].m_bRenderWhenOccluded && !m_GlowObjectDefinitions[i].m_bRenderWhenUnoccluded )
{
ShaderStencilState_t stencilState;
stencilState.m_bEnable = true;
stencilState.m_nReferenceValue = 2;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_ALWAYS;
stencilState.m_PassOp = STENCILOPERATION_REPLACE;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_KEEP;
stencilState.SetStencilState( pRenderContext );
m_GlowObjectDefinitions[i].DrawModel();
}
}
pRenderContext->OverrideDepthEnable( false, false );
render->SetBlend( flSavedBlend );
stencilStateDisable.SetStencilState( pRenderContext );
g_pStudioRender->ForcedMaterialOverride( NULL );
// If there aren't any objects to glow, don't do all this other stuff
// this fixes a bug where if there are glow objects in the list, but none of them are glowing,
// the whole screen blooms.
if ( iNumGlowObjects <= 0 )
return;
//=============================================
// Render the glow colors to _rt_FullFrameFB
//=============================================
{
PIXEvent pixEvent( pRenderContext, "RenderGlowModels" );
RenderGlowModels( pSetup, nSplitScreenSlot, pRenderContext );
}
// Get viewport
int nSrcWidth = pSetup->width;
int nSrcHeight = pSetup->height;
int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
pRenderContext->GetViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
// Get material and texture pointers
ITexture *pRtQuarterSize1 = materials->FindTexture( "_rt_SmallFB1", TEXTURE_GROUP_RENDER_TARGET );
{
//=======================================================================================================//
// At this point, pRtQuarterSize0 is filled with the fully colored glow around everything as solid glowy //
// blobs. Now we need to stencil out the original objects by only writing pixels that have no //
// stencil bits set in the range we care about. //
//=======================================================================================================//
IMaterial *pMatHaloAddToScreen = materials->FindMaterial( "dev/halo_add_to_screen", TEXTURE_GROUP_OTHER, true );
// Do not fade the glows out at all (weight = 1.0)
IMaterialVar *pDimVar = pMatHaloAddToScreen->FindVar( "$C0_X", NULL );
pDimVar->SetFloatValue( 1.0f );
// Set stencil state
ShaderStencilState_t stencilState;
stencilState.m_bEnable = true;
stencilState.m_nWriteMask = 0x0; // We're not changing stencil
stencilState.m_nTestMask = 0xFF;
stencilState.m_nReferenceValue = 0x0;
stencilState.m_CompareFunc = STENCILCOMPARISONFUNCTION_EQUAL;
stencilState.m_PassOp = STENCILOPERATION_KEEP;
stencilState.m_FailOp = STENCILOPERATION_KEEP;
stencilState.m_ZFailOp = STENCILOPERATION_KEEP;
stencilState.SetStencilState( pRenderContext );
// Draw quad
pRenderContext->DrawScreenSpaceRectangle( pMatHaloAddToScreen, 0, 0, nViewportWidth, nViewportHeight,
0.0f, -0.5f, nSrcWidth / 4 - 1, nSrcHeight / 4 - 1,
pRtQuarterSize1->GetActualWidth(),
pRtQuarterSize1->GetActualHeight() );
stencilStateDisable.SetStencilState( pRenderContext );
}
}
void CGlowObjectManager::GlowObjectDefinition_t::DrawModel()
{
if ( m_hEntity.Get() )
{
m_hEntity->DrawModel( STUDIO_RENDER );
C_BaseEntity *pAttachment = m_hEntity->FirstMoveChild();
while ( pAttachment != NULL )
{
if ( !g_GlowObjectManager.HasGlowEffect( pAttachment ) && pAttachment->ShouldDraw() )
{
pAttachment->DrawModel( STUDIO_RENDER );
}
pAttachment = pAttachment->NextMovePeer();
}
}
}
#endif // GLOWS_ENABLE
|