Shadow Mapping in Source

Grab your favourite IDE and tinker with the innards of game engines

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Varsity on Wed Aug 10, 2011 3:22 pm

Edit: this is for Alien Swarm, and probably won't work well in 2007.

Handling multiple depth textures is very easy. I generate 78 (most of them low-res!) and allocate them based on distance between the projection and camera.

I made a struct to keep things tidy:
Code: Select all
struct DepthTexCache {
   CUtlVector<CTextureReference> Textures;
   CUtlVector<bool> Locks;
   int Resolution;
} DepthTexCache_t;

Then added an array of it to CClientShadowMgr (I ended up not using the Huge size):
Code: Select all
DepthTexCache m_DepthTextureCaches[4]; // Huge, Large, Small, Tiny   

The render targets are generated in CClientShadowMgr::InitDepthTextureShadows()...
Code: Select all
// Create some number of depth-stencil textures
int nTextureResolutions[] = { 2048, 1024, 512, 256 };
int nTexturesPerResolution[] = { 0, 18, 24, 36 }; // 0, 3, 4, 6 omnis...but individual envprojtex'es will eat into that
int numInits = 0;

for (int j=0; j < 4; j++)
{
   m_DepthTextureCaches[j].Textures.Purge();
   m_DepthTextureCaches[j].Locks.Purge();

   m_DepthTextureCaches[j].Resolution = nTextureResolutions[j];

   for( int i=0; i < nTexturesPerResolution[j]; i++ )
   {
      CTextureReference depthTex;   // Depth-stencil surface
      bool bFalse = false;

      char strRTName[64];
      sprintf( strRTName, "_rt_ShadowDepthTexture_%d", numInits );
      numInits++;

      depthTex.InitRenderTarget( nTextureResolutions[j], nTextureResolutions[j], RT_SIZE_NO_CHANGE, dstFormat, MATERIAL_RT_DEPTH_ONLY, false, strRTName );

      m_DepthTextureCaches[j].Textures.AddToTail( depthTex );
      m_DepthTextureCaches[j].Locks.AddToTail( bFalse );
   }
}

...and allocated each frame in CClientShadowMgr::LockShadowDepthTexture():
Code: Select all
int CClientShadowMgr::LockShadowDepthTexture( CTextureReference *shadowDepthTexture, float dist )
{
   int StartCache = 3;
   if (dist < 512 )
      StartCache = 1;
   else if (dist < 1024)
      StartCache = 2;

   for (int i = StartCache; i < 4; i++)
      for ( int j = 0; j < m_DepthTextureCaches[i].Textures.Count(); j++ )
      {
         if ( m_DepthTextureCaches[i].Locks[j] == false && m_DepthTextureCaches[i].Textures[j].IsValid() ) // is it free?
         {
            *shadowDepthTexture = m_DepthTextureCaches[i].Textures[j];
            m_DepthTextureCaches[i].Locks[j] = true;
            return m_DepthTextureCaches[i].Resolution;
         }
      }

   return 0;
}


Edit: not forgetting destruction...
Code: Select all
void CClientShadowMgr::ShutdownDepthTextureShadows()
{
   if( m_bDepthTexturesAllocated )
   {
      // Shut down the dummy texture
      m_DummyColorTexture.Shutdown();

      for (int i=0; i < 4; i++)
         while( m_DepthTextureCaches[i].Textures.Count() )
         {
            int elem = m_DepthTextureCaches[i].Textures.Count() - 1;

            m_DepthTextureCaches[i].Textures.Tail().Shutdown();

            m_DepthTextureCaches[i].Locks.Remove( elem );
            m_DepthTextureCaches[i].Textures.Remove( elem );
         }

      m_bDepthTexturesAllocated = false;
   }
   m_bDepthTextureActive = false;
}


Edit: I also forgot to post the code which unlocks the textures.
Code: Select all
void CClientShadowMgr::UnlockAllShadowDepthTextures()
{
   for (int j=0; j < 4; j++)
      for (int i=0; i< m_DepthTextureCaches[j].Locks.Count(); i++ )
      {
         m_DepthTextureCaches[j].Locks[i] = false;
      }

   shadowmgr->SetSinglePassFlashlightRenderState( SHADOW_HANDLE_INVALID );
   shadowmgr->PopSinglePassFlashlightStateEnabled();   
}


And that's it, no entity modification needed. Pleasantly, I've not noticed any popping between LODs.

Problems:
  • There is currently a large hitch when the depth texture transitions to a fresh render target, but it should be easy to fix when I get round to it. :)
  • More concerning is the need to disable r_flashlightscissor if you want to display more than one projected texture at once, as this greatly increases overdraw (especially if you have omnidirectional projections!) and can send performance off a cliff. Not sure if this can even be fixed in mode code.
Last edited by Varsity on Thu Aug 11, 2011 9:29 am, edited 3 times in total.
User avatar
Varsity
Member
Member
 
Joined: Wed Apr 19, 2006 6:56 pm
Location: England

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby dark0r on Wed Aug 10, 2011 3:34 pm

This is excellent! Thanks for sharing. I've added you on Steam (I'm Saul [UK]), we should team up :D I'm just about to start a dynamic sun.
dark0r
Been Here A While
Been Here A While
 
Joined: Sat Mar 25, 2006 10:16 am
Location: de_hell

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Varsity on Wed Aug 10, 2011 3:40 pm

No problem. Have a look at env_global in the Alien Swarm codebase BTW: it doesn't work correctly (as far as I can tell) but is intended to be shadow-mapped sunlight. From what I hear Dota2 has that feature, so env_global is presumably a prototype.
User avatar
Varsity
Member
Member
 
Joined: Wed Apr 19, 2006 6:56 pm
Location: England

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Wed Aug 10, 2011 9:37 pm

I already looked at the sunlight stuff in AS, it's too specific to topdowns. You could never use it for anything else.

Also, I noticed what looked to be per-pt depth res options in AS, but it only allowed a high res and low res setting.

AS also appears to allow material(vmf) along with texture(vtf) projection for pts.


I'm done win Source for the most part, so I likely won't be doing any more testing.
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby stoopdapoop on Thu Aug 11, 2011 1:48 am

ohh, cascaded shadow maps, nice.

but as far as an orthographic light goes, shouldn't it be a matter of finding the code that handles to projection, and making the near clipping plane the same size as the far clipping plane? I can't say how difficult it would be considering I don't know where that code would be, but in regular cirumstances I'd imagine turning a perspective projection into an orthographic one wouldn't be so hard.
I'm Brown
Image
User avatar
stoopdapoop
Veteran
Veteran
 
Joined: Sun Aug 21, 2005 2:14 am
Location: Ann Arbor, MI

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Thu Aug 11, 2011 2:41 am

AS code already supports ortho projection. Used for their topdown-only shadowmapped sunlight.
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Varsity on Thu Aug 11, 2011 10:39 am

Today's experiment: volumetric light!

Image

It's not bad, but expand the image and you'll see there's a huge amount of banding going on. The shadow from the marine is so subtle that you'd be forgiven for thinking it's not there, too. Probably another effect that will only ever look good from a distance (or from a top-down camera).

stoopdapoop wrote:ohh, cascaded shadow maps, nice.

Well, sort of. Cascading is having multiple maps arranged in front of the the camera in layers and following it.
User avatar
Varsity
Member
Member
 
Joined: Wed Apr 19, 2006 6:56 pm
Location: England

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Thu Aug 11, 2011 12:36 pm

I remember seeing something in there for volumetric light, didn't think anything of it. Apparently it works(kinda).
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Dabu on Sat Oct 15, 2011 3:52 pm

In case if someone dosn't know about this. Portal 2 engine has fully functional sunlight with dynamic shadows. It's created by sunlight_shadow_control entity. On video below I also used some static lighting for smooth transition. Looks good even though it's only 1024x1024 resolution.

Image
User avatar
Dabu
Dumpling
Dumpling
 
Joined: Sun Feb 08, 2009 9:01 pm

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Sat Oct 15, 2011 4:16 pm

Looks like it's just a projected texture that moves relative to the player. I saw some code of that in ASW, it has a problem of distant shadows having to be lightmapped(thus an obvious transition). Still, depending on the scene, it could be useful for some.

I'll have to get the P2SDK updates and play with that some.
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Dabu on Sat Oct 15, 2011 4:22 pm

Yeah, but with P2's improved shadow filtering it looks much better. And I tried it in AS but it's quite buggy there.
Image
User avatar
Dabu
Dumpling
Dumpling
 
Joined: Sun Feb 08, 2009 9:01 pm

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Mon Feb 13, 2012 1:28 am

Gary wrote:I already looked at the sunlight stuff in AS, it's too specific to topdowns. You could never use it for anything else.

I'm done win Source for the most part, so I likely won't be doing any more testing.

I retract the above statement.

Here are some more pictures of global_light from ASW(Ignore the lighting errors, not ASW's fault):
If used right, can be awesome(this is about as big as you can get with this entity):
Image
Image

It's env_global_light, similar to "sunlight_shadow_control" Dabu just showed in that video(actually, I could of used that, but there doesn't seem to be much difference, though it appears env_global_light came after sunlight_shadow_control).

Anyways. I'm using two resoultions for shadow mapping at the moment:
r_flashlightdepthres 1024
r_flashlightdepthreshigh 4096 (This is what I'm using for the env_global_light up there)

All shadow mapping based entities(env_projectedtexture, sunlight_shadow_control, and env_global_light) have these values per-entity:
filter - How much to "blur" the shadow.
Shadowatten - Kinda like the brightness of the shadow[Doesn't fade in ASW]
highres - Use r_flashlightdepthreshigh, good if you need a Portal 2-like light for an entire area.



Varsity wrote:Today's experiment: volumetric light!

http://img814.imageshack.us/img814/593/volumetric.jpg

It's not bad, but expand the image and you'll see there's a huge amount of banding going on. The shadow from the marine is so subtle that you'd be forgiven for thinking it's not there, too. Probably another effect that will only ever look good from a distance (or from a top-down camera).


Where did you find the texture for that? When I try to turn it on it fails to find the proper texture.

Also, the banding may be reduced via one of these values:
m_flNoiseStrength = 0.8f;
m_nNumPlanes = 64;
m_flPlaneOffset = 0.0f;
m_flVolumetricIntensity = 1.0f;
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby dark0r on Mon Feb 13, 2012 2:35 am

Looking good Gary, have you seen this by the way:
http://garry.tv/post/17497090178/improved-shadows
dark0r
Been Here A While
Been Here A While
 
Joined: Sat Mar 25, 2006 10:16 am
Location: de_hell

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby Gary on Mon Feb 13, 2012 3:09 am

No. Thanks for sharing. I was aware of the problem, but didn't think much of it. I didn't realize how much of a problem it was until seeing Garry's comparisons.

Wonder at which level he fixed it(I think the engine or maybe shaders).
Have a question related to modding or something I posted? Something that needs staff attention? I haven't been active lately, but feel free to PM me or message me on Steam(link below)

User avatar
Gary
Interlopers Staff
Interlopers Staff
 
Joined: Wed Dec 16, 2009 12:40 am
Location: USA, FL

Re: Fixing env_pojectedtextue(Src2007 Mods)

Postby MrTwoVideoCards on Mon Feb 13, 2012 8:38 pm

He just fixed attenuation, and the filter it seems. I asked maxofs2d, and that is what he told me. So I dunno. :(.
User avatar
MrTwoVideoCards
Monothetic
 
Joined: Thu Aug 02, 2007 11:18 am
Location: IN YOUR SOUL
PreviousNext

Return to Programming

Who is online

Users browsing this forum: No registered users

cron