Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1] 2 3 ... 5
  Print  
Author Topic: [Source, Library, Sample] Parallel Split Shadow Mapping that you can re-USE  (Read 10288 times)
Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« on: February 06, 2009, 05:14:14 PM »



So finally I had some freetime to crunch up my PSSM implementation into both a sample and a library. You can easily use the library in .NET based languages, without having to re-code it to the language of your choice! The package I provide contains the following:

- Source code for both the sample
- Source code for the PSSM library
- Source code for the shaders

Basicaly, my implementation has the following properties:

1. It uses four frustum splits
2. All four frustum splits are saved in only ONE texture, each depth map is saved in a separated ARGB channel. Thus you can NOT use Variance Shadow Mapping or every other probability shadow mapping method, since they require at least two channels per depth map.
3. It uses a five-pass model, instead of the eight-pass model mentioned in the original PSSM papers. My implementation can render the final scene in only a single pass!
4. It does NOT require dynamic branching on the GPU, as most implementation does.
5. It uses orthogonal camera matrices, since they make more sense than the perspective ones used in the original PSSM papers.

The basic render flow of my implementation is the following:

You just pass the light and camera information and your shaders into the library.
 The library itself does not know the shadow casting/receiving geometry at all - and it doesn't need to do. To get the shadows working, you apply your shaders on your geometry (which can be TVTerrain, TVMesh, TVActor, TVMiniMesh, TVParticleSystem, in other words: Every imaginable geometry! and ensure that they have two techniques implemented: "shadowed" and "depth"  (More on this later on.) The library does switch between the techniques while generating the depth maps. This way, there is no need to re-apply the shaders to your geometry and the library doesn't need to know where the shaders are applied on. To get the occluders onto the depht map, you hook up a delegate in the library that asks YOU to render the geometry - Thats it.

The eStudioz.PSSM library contains one main class called CParallelSplitShadowMapping, which you need to instanciate. While doing so, you need to pass the

following information to its constructor:

Code:
// 256, 512, 1024, 2048
int nShadowMapSize

// FLOAT16 or FLOAT32. The more precision, the better. Needs at least four channels!
CONST_TV_RENDERSURFACEFORMAT eRenderSurfaceFormat

// The render interval in seconds. There is definately no need to render the depth maps every frame. Every 1/100th
float fRenderInterval second for example is enough at all. Can be limited to every other interval, e.g. on slower computers. 0 will be every frame rendering.

// Since it can only use one directional light, you must pass it into. However, you can change this direction later on in the LightDirection attribute of the

CParallelSplitShadowMapping class
TV_3DVECTOR vLightDirection

After that, you call the AddShader() method to add every shader you want to add. The only thing you need to take care of, is the AddMeshToSceneBoundingBox() method. This method contructs the scenes bounding box volume to let it be able to care about the worlds borders. The simplest ways would be to pass in only one large box, that is never rendered. Of course you could skip this if you want to, but I advice you not to, because it can optimize the scenes frustum to ensure the best included shadow areas. Then, you hook up the RenderGeometry() delegate and you're ready. To render the depth maps and calculate all neccessary stuuf, call the Update() method BEFORE your final scene rendering pass and that's it. Easy, isn't it? I think five lines of code is easy enough though Smiley

On the shader side, you need to include the following parameters and one texture:

Code:
float3 g_vLightDirection;
float4x4 g_mShadowMap[4];
float4x4 g_mLightViewProj[4];
float4 g_fSplitDistances;

texture texShadowMap;
sampler2D sampShadowMap  =
sampler_state
{
Texture = (texShadowMap);
MinFilter = Point;
MagFilter = Point;
MipFilter = None;
AddressU = Border;
AddressV = Border;
AddressW = Border;
BorderColor = 0xFFFFFF;
};

In addition, I provide highly optimized functions for the shadow test:

Code:
half GetSplitByDepth(float fDepth)
{
float4 fTest = fDepth > g_fSplitDistances;
return dot(fTest, fTest);
}

float GetShadow(half fSplitIndex, float2 faSplitUV[4], float4 faLightDepths)
{
float fShadow = faLightDepths[fSplitIndex] > tex2D(sampShadowMap, faSplitUV[fSplitIndex].xy)[fSplitIndex];
return 1.0f - fShadow * SHADOW_OPACITY;
}

Refer to my blog at http://www.e-studioz.de/wordpress/?p=14 to see why this is optimized as possible. For more information, take a look at my code. It's not the best code, but clean, readable and effective. Oh, and keep in mind, that the project is a MSVS 2008 one, but the code itself is .NET 2.0 (as I don't like 3.0). Ah, and in addition, there is no MTV3D65.dll included, as usual.

Here's the download for you:

http://www.e-studioz.de/pssm/PSSM4u.zip

To move the camera in the sample, hold your mouse button 2 and use WASD to move.


Overall, this is open source! All I ask for is:

- Please don't change the library name (eStudioz.PSSM.ddl) if you use it "as is".
- Show me all the freaking stuff you made with it!
- A credit might be cool, but being a beta-tester of your stuff might be even better! Wink

Please do me this favor, since I'm sharing ~150 hours of work here! Thanks!

Hope you enjoy it!
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
SylvainTV
Administrator
Community Member
*****
Posts: 4824


WWW
« Reply #1 on: February 06, 2009, 05:16:37 PM »

Yey Thanks for this Eichi ! I'm sure it will be useful for a lot of people !! Making it sticky.
Logged

Regards

Sylvain Dupont
TrueVision3D Developer
sylvain@truevision3d.com

TV3D IRC at http://chat.truevision3d.com or on server irc.truevision3d.com #Truevision3D. Come talk with us !
AriusEso
Customers
Community Member
*****
Posts: 791

Esoteric


« Reply #2 on: February 06, 2009, 05:21:14 PM »

Excellent work Mietze. Really looking forward to playing with this tommorow. Thanks for releasing, I'm sure it'll be informative. Grin
Logged

-...-
Raine
Customers
Community Member
*****
Posts: 1212


« Reply #3 on: February 06, 2009, 05:21:28 PM »

Wonderful, thanks a lot Smiley

*virtual handshake*
Logged

Toaster
Community Member
*
Posts: 272

Crap I burnt the toast! :P


WWW
« Reply #4 on: February 06, 2009, 05:38:29 PM »

hmm it looks like the teapot self shadows are wrong.  Undecided Maybe its just me though...

http://willhostforfood.com/files4/5799818/selfshadowswrong.JPG

-Toaster
Logged

Visit my site at: Unknown Abstraction
Shargot
Customers
Community Member
*****
Posts: 162


WWW
« Reply #5 on: February 06, 2009, 06:22:03 PM »

very big thanx Mietze.
Toaster i also have this bug (((
Logged

" width="130" height="45" border="0
Baiame
Customers
Community Member
*****
Posts: 43


« Reply #6 on: February 07, 2009, 05:17:16 AM »

Looks like the pinnacle of awesomeness, but... could you please briefly state how difficult you think it would be to adapt to native C++? If I finished such a conversion I'd definitely release the result.
Logged
Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #7 on: February 07, 2009, 06:03:23 AM »

Toaster i also have this bug (((
Actually, this isn't a bug, it's a trap Smiley I left this in there for you guys to fix. Small hint: Depth Bias in the shader Smiley

Looks like the pinnacle of awesomeness, but... could you please briefly state how difficult you think it would be to adapt to native C++?
This shouldn't be as that hard, cause the original PSSM papers and samples are in native C++. You can find it here: http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_project/
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Gamecode
Customers
Community Member
*****
Posts: 438


WWW
« Reply #8 on: February 07, 2009, 11:19:45 AM »

can use in VB6 ? Sad
Logged

aiR Captains - RC aircraft project TV65
Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #9 on: February 07, 2009, 11:42:35 AM »

Nope, you can't!
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Aurora
Community Member
*
Posts: 36


WWW
« Reply #10 on: February 07, 2009, 02:11:22 PM »

Actually, this isn't a bug, it's a trap Smiley I left this in there for you guys to fix. Small hint: Depth Bias in the shader Smiley

So... it isnt excatly working in its current state unless we fix it ourself ? (have to check) Smiley
Logged

ASUS P5E AiLifestyle /w Intel Core 2 Quad Q9550 @ 3.6 Ghz
4GB Mushkin PC-8500, Asus Gforce Asus GTX295 1792MB, 1.95 TB HDD
28" ViewSonic VX2835wm 3ms on Windows Vista Home Premium x64.

Ninnea Online - http://www.Ninnea-Online.com (In progress)
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #11 on: February 07, 2009, 02:59:49 PM »

Mietze, I am having a sex change operation for yo.

Sex?

I kid, but thanks man. Great job!

-Pat
Logged
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #12 on: February 07, 2009, 03:00:30 PM »

Mietze, I am having a sex change operation for you.

Sex?

I kid, but thanks man. Great job!

-Pat
Logged
nadjibus
Community Member
*
Posts: 248

Heavy Dev Process


« Reply #13 on: February 07, 2009, 03:57:51 PM »



Mietze, I am having a sex change operation for yo.

Sex?
I kid, but thanks man. Great job!

-Pat

Logged

Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #14 on: February 07, 2009, 04:36:14 PM »

lol?
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Gamecode
Customers
Community Member
*****
Posts: 438


WWW
« Reply #15 on: February 08, 2009, 04:54:34 AM »

hmm no VB6 Sad ohhhhh
Logged

aiR Captains - RC aircraft project TV65
Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #16 on: February 10, 2009, 02:51:07 AM »

Just for you to notice. There is a small part in the code, that can be removed:


foreach (TVShader pShader in m_paShaderList)
    pShader.SetEffectParamInteger("g_nCurrentSplit", nSplit);

In addition, this can be merged:


foreach (TVShader pShader in m_paShaderList)
{
    pShader.SetEffectParamFloatArray("g_fLightFarPlanes", m_fLightFarPlanes, CONST_SPLIT_COUNT);
    pShader.SetEffectParamMatrixArray("g_mShadowMap", m_maShadowMapTextureMatrices, CONST_SPLIT_COUNT);
    pShader.SetEffectParamMatrixArray("g_mLightViewProj", m_maLightViewProjection, CONST_SPLIT_COUNT);
    pShader.SetTechnique("shadowed");
}

Might spare minimum processing time though.
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
SylvainTV
Administrator
Community Member
*****
Posts: 4824


WWW
« Reply #17 on: February 10, 2009, 05:25:06 AM »

BTW, don't forget that you can use "shared" parameters now.
You could set the params "g_mLightViewProj", "g_mShadowMap" as shared, then just set the params once in the engine for all loaded shaders.
Logged

Regards

Sylvain Dupont
TrueVision3D Developer
sylvain@truevision3d.com

TV3D IRC at http://chat.truevision3d.com or on server irc.truevision3d.com #Truevision3D. Come talk with us !
potato
Customers
Community Member
*****
Posts: 800


WWW
« Reply #18 on: February 10, 2009, 11:58:23 AM »

I'm looking to integrate this into my demo Smiley I have a question about the shaders though - since we seem to need to do more work to existing shaders, does this preclude using TV's default normal/parallax shaders with this lib?
Logged
Mietze
Community Member
*
Posts: 412

Pleeease, don't let it crash!


WWW
« Reply #19 on: February 10, 2009, 12:05:34 PM »

The answer is: no. Custom shaders override the internal ones, but normal mapping and parallax stuff is very trivial and widely spread over the internet Smiley
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Pages: [1] 2 3 ... 5
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks