1212#include " materialsystem/itexture.h"
1313#include " view_shared.h"
1414#include " viewpostprocess.h"
15+ #include " cdll_client_int.h"
16+
17+ static ITexture* s_pRtGlowColor = NULL ;
18+ static ITexture* s_pRtGameSceneBackup = NULL ;
1519
1620#define FULL_FRAME_TEXTURE " _rt_FullFrameFB"
1721
@@ -264,30 +268,36 @@ void CGlowObjectManager::RenderGlowModels( const CViewSetup *pSetup, int nSplitS
264268 const float flOrigBlend = render->GetBlend ();
265269
266270 // avoid touching _rt_FullFrameFB which is used elsewhere
267- static ITexture* pRtGlowColor = NULL ;
268- static ITexture* pRtGameSceneBackup = NULL ;
271+ if ( !s_pRtGlowColor || !s_pRtGameSceneBackup )
269272 {
270273 ImageFormat format = materials->GetBackBufferFormat ();
271274 if ( g_pMaterialSystemHardwareConfig->GetHDRType () != HDR_TYPE_NONE )
272275 format = IMAGE_FORMAT_RGBA16161616F;
273276
274277 ITexture* pRtFullFrameFB = materials->FindTexture ( FULL_FRAME_TEXTURE, TEXTURE_GROUP_RENDER_TARGET );
275278
279+ materials->BeginRenderTargetAllocation ();
280+
276281 // we use RT_SIZE_NO_CHANGE here and the frame texture's actual size to avoid DX9 rect resize issues when copying
277282 // usually there's some 0.5 padding or whatever on the hardware and the engine naively isn't aware of this, so if you do a
278283 // CopyTextureToRenderTargetEx that isn't aware of the underlying issue, you'll cause a slight blur, which changes bloom
279- pRtGlowColor = materials->CreateNamedRenderTargetTextureEx (
284+ s_pRtGlowColor = materials->CreateNamedRenderTargetTextureEx2 (
280285 " _rt_GlowColor" , pRtFullFrameFB->GetActualWidth (), pRtFullFrameFB->GetActualHeight (),
281286 RT_SIZE_NO_CHANGE, format,
282287 MATERIAL_RT_DEPTH_NONE, TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT, 0 );
283- pRtGameSceneBackup = materials->CreateNamedRenderTargetTextureEx (
288+ s_pRtGameSceneBackup = materials->CreateNamedRenderTargetTextureEx2 (
284289 " _rt_GlowGameSceneBackup" , pRtFullFrameFB->GetActualWidth (), pRtFullFrameFB->GetActualHeight (),
285290 RT_SIZE_NO_CHANGE, format,
286291 MATERIAL_RT_DEPTH_NONE, TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT, 0 );
292+
293+ s_pRtGlowColor->AddRef ();
294+ s_pRtGameSceneBackup->AddRef ();
295+
296+ materials->EndRenderTargetAllocation ();
287297 }
288298
289299 // copy our scene to our backup texture
290- pRenderContext->CopyRenderTargetToTexture ( pRtGameSceneBackup );
300+ pRenderContext->CopyRenderTargetToTexture ( s_pRtGameSceneBackup );
291301
292302 // Clear backbuffer color, including alpha, to make sure this pass is isolated
293303 pRenderContext->ClearColor4ub ( 0 , 0 , 0 , 0 );
@@ -298,7 +308,7 @@ void CGlowObjectManager::RenderGlowModels( const CViewSetup *pSetup, int nSplitS
298308 IMaterial *pMatGlowColor = NULL ;
299309
300310 pMatGlowColor = materials->FindMaterial ( " dev/glow_color" , TEXTURE_GROUP_OTHER, true );
301- g_pStudioRender ->ForcedMaterialOverride ( pMatGlowColor );
311+ modelrender ->ForcedMaterialOverride ( pMatGlowColor );
302312
303313 // Don't write alpha
304314 pRenderContext->OverrideAlphaWriteEnable ( true , false );
@@ -335,7 +345,7 @@ void CGlowObjectManager::RenderGlowModels( const CViewSetup *pSetup, int nSplitS
335345 }
336346
337347 // Restore modulation color and blend
338- g_pStudioRender ->ForcedMaterialOverride ( NULL );
348+ modelrender ->ForcedMaterialOverride ( NULL );
339349 render->SetColorModulation ( vOrigColor.Base () );
340350 render->SetBlend ( flOrigBlend );
341351
@@ -346,14 +356,14 @@ void CGlowObjectManager::RenderGlowModels( const CViewSetup *pSetup, int nSplitS
346356 pRenderContext->OverrideDepthEnable ( false , false );
347357
348358 // Copy out the glow models to our texture
349- pRenderContext->CopyRenderTargetToTexture ( pRtGlowColor );
359+ pRenderContext->CopyRenderTargetToTexture ( s_pRtGlowColor );
350360
351361 // Re-enable writes here for when we copy our backup to the backbuffer
352362 pRenderContext->OverrideColorWriteEnable ( false , false );
353363 pRenderContext->OverrideAlphaWriteEnable ( false , false );
354364
355365 // Copy with proper coordinates
356- pRenderContext->CopyTextureToRenderTargetEx ( 0 , pRtGameSceneBackup , nullptr );
366+ pRenderContext->CopyTextureToRenderTargetEx ( 0 , s_pRtGameSceneBackup , nullptr );
357367
358368 pRenderContext->PopRenderTargetAndViewport ();
359369}
@@ -406,10 +416,9 @@ void CGlowObjectManager::ApplyEntityGlowEffects( const CViewSetup *pSetup, int n
406416
407417 // We use a created _rt_GlowColor as our texture, not whatever the material is set to
408418 IMaterialVar* pBaseTexVar = pMatHaloAddToScreen->FindVar ( " $basetexture" , NULL );
409- if ( pBaseTexVar )
419+ if ( pBaseTexVar && s_pRtGlowColor )
410420 {
411- ITexture* pRtGlowColor = materials->FindTexture ( " _rt_GlowColor" , TEXTURE_GROUP_RENDER_TARGET );
412- pBaseTexVar->SetTextureValue ( pRtGlowColor );
421+ pBaseTexVar->SetTextureValue ( s_pRtGlowColor );
413422 }
414423
415424 // Do not fade the glows out at all (weight = 1.0)
@@ -448,15 +457,15 @@ void CGlowObjectManager::GlowObjectDefinition_t::DrawModel( IMaterial* pMatGlowC
448457{
449458 if ( m_hEntity.Get () )
450459 {
451- g_pStudioRender ->ForcedMaterialOverride ( pMatGlowColor );
460+ modelrender ->ForcedMaterialOverride ( pMatGlowColor );
452461 m_hEntity->DrawModel ( STUDIO_RENDER );
453462 C_BaseEntity *pAttachment = m_hEntity->FirstMoveChild ();
454463
455464 while ( pAttachment != NULL )
456465 {
457466 if ( !g_GlowObjectManager.HasGlowEffect ( pAttachment ) && pAttachment->ShouldDraw () && pAttachment->CanGlow () )
458467 {
459- g_pStudioRender ->ForcedMaterialOverride ( pMatGlowColor );
468+ modelrender ->ForcedMaterialOverride ( pMatGlowColor );
460469 pAttachment->DrawModel ( STUDIO_RENDER );
461470 }
462471 pAttachment = pAttachment->NextMovePeer ();
0 commit comments