Friday, June 12, 2009

Light Prepass Shadow Mapping

Ok, update on my Light Prepass journey. Shadow mapping is actually new to me, let alone ever using it in a deferred rendering. In fact this my first time to probably nail this thing to its head. Currently, its a simple shadow mapping... no magic.. no fancy footwork. The image you see here has a point light(w/ attenuation) casting shadow with 5x5 PCF (percent closer filtering). At this point, its all raw-brute-coding, definitely screaming for optimization. And obviously, I'm hiding all the shadow errors with a neat camera angle.

The model rendering however exhibits sound optimation. I've managed to remove all matrix computation in the pixel shader on the light passes (full screen quad and light convex mesh - I call these guys 'light blobs'). The mathematics here was such a nose bleed. The key here is making View Space your battle ground. Good thing the graphics gurus are around... MJP, Drilian, Engel, etc. Here's the link.

In terms of prepass packing, specifically the normals, the Cryengine 3 suggestion seem to produce some inaccuracies when I error check it with the fresh untouch normals. I added a weird value just to remove the error. Here's the code.

float4 PackDepthNormal(in float z, in float3 normal)
float4 output; normal = normalize(normal);
normal.x *= 1.000000000000001f; //<--- my nasty mod
output.xy = normalize(normal.xy) * sqrt(normal.z * .5f + .5f);
return PackDepth(output, z);

Anyway, I need to investigate this further. I find Pat Wilson's idea on converting normals to spherical coord better. I haven't profiled this but this seem to be a more optimized approach.

Back to shadows, my target is to use Cascaded Shadow Maps. Hopefully, in a few days time I can post the results.


psykumagski said...

Hi Dave! :)

Musta. Love what you're doing so far. Keep it up. I also tried doing Deferred Shading after seeing the Killzone 2 demo. I love the fact that you can have lots and lots of lights in a scene without the multipass hit in forward rendering. Lack of decent translucency and MSAA almost killed it for me though, but nevertheless I feel this is where many games will use in the near future. I never did try to take it seriously as you did though. I got as far as compressing them all in 32-bit surfaces and using exp2/log2 encoding, and making a simple lighting editor, but that's it. I haven't done those things you've done so far, so now you're miles ahead of me on that one. I'll be waiting for your next entry ;) ...

vidextreme said...

Well hello there master ry.... I mean psykumagski. heheheh

Yup. Thats the limitation of Full Deferred rendering. Light Prepass on the other hand is a diet slim down version of Deferred. When the prepass and light pass are done, you will do a forward render. Here you can turn on MSAA. Not sure about transluncy though, but it should work with a bit of trick.

Currently, I'm drowning myself with math RE:Cascading Shadow Map. I'm basing my implementation on Shader X5-6 and maybe if I get my hands on X7 that also.

I also included the exp2/log2 but apparently, this will be tricky with the tone mapping. But according to the graphics GURUs there's a way.... there's always a way.

psykumagski said...

I've read about light prepass from before and looks like it's a good idea. Also not sure about translucency, and I may need it more than MSAA. Forward Rendering na lang siguro ung translucent objects. Exp2/Log2 works fine by me, but I guess we have different taste, as it does usually break on some levels. It's more like a middle dynamic range instead of high. Ever tried logluv? (never figured out how to blend it though). Anyways, I might not try any freaky stuffs for quite some time as I vowed to make a 'functioning' platforming game and a shmup before I touch graphics again. So I think this time, you're the master now. ;)

alalay said...

(biglang singit ang isang slave)

waaaah... mga master (and fafa) ng buhay ko! hehehe. Si sir dave ang magiging future wolfgang at si sir ryan naman ang future carmack. Ako naman eh babalik na lang sa probinsya namin at magtatanim ng kamote.

(ps3 fanboy mode)
Ndi kakayanin ng 360 deferred shading o light prepass!!
(sabay takbo)

vidextreme said...

The servant shall never be great as the master. Learning means both are advancing.

My masters will always be 'da masters.

(fan_boy mode)
Batuhin kita ng ps3-radioactive lollipop!!!

psykumagski said...

(fan_boy mode) f*ck you, my DS rules!

(serious mode) People do say PS3 had some mechanisms that would speed up deferred/light prepass. I saw a white paper regarding this, but I did not read because I wasn't interested, since (fan boy mode) ps3 sucks (prepares for incoming onslaught).

Arrggh, must... not... touch... graphics code. Must... complete... shmup engine... ;_;

alalay said...

(ps3 squad)

Mga kawal! Hulihin ang uhm.. anti-ps3 boy!

(pero regarding sa deferred sa ps3) kaya pansin mo usually deferred/pre-pass o anumang variation nun ang gamit ng mga first party dev ng sony (i.e. insomniac, guerilla, tska ung phyre engine nila na panlaban kuno sa xna). Although games like dead space and bad company which according to wolfgang, uses also a variation of light prepass, runs well on 360. Ah well, sige na nga... project natal ftw!! hehehe

alalay said...

teka... bat nga pala napunta sa console wars ang usapan? :D

vidextreme said...

o nga... make peace, not war.

Now KILL!!! KILL!!! KILL!!...lah.

RE: Light Prepass
I just thought of a way to make the lightpass rendering faster.

IDEA A: (LOW-RES Lights then BLUR) Render the lights on a lower resolution. And do a slight blurring after all light accumulation. Before doing the Forward rendering.

IDEA B: (GROUP LIGHTS then do IDEA A on furthest group) Segregate rendering of lights by distance from the camera in groups. The furthest group render them in a lower resolution buffer. And the closer group into a higher resolution. Something like Pseudo-Cascade Lightpass technique. I guess this is a way to reduce fill-rate bottleneck... if it works ofcourse.

psykumagski said...

i'm imagining your idea A might light up places that shouldn't be lit due to the blur, ala SSAO when the settings ain't right. idea b sounds a bit better, maybe with more scissoring than before (not the lesbian variation) to cut off more fillrate bottleneck. Not sure about the memory requirements. I dunno. I didn't dabble with graphics seriously for weeks (months?) now, so I guess I could be mistaken (the last thing i seriously wanted to do that is graphics related is to dabble with volume textures as my lighting sources, ala LBP, just to replace deferred style lighting. That never took off. Kinda depressing.)

@ps3 goons: i owned a PSP. That still makes me a sony ally. If you arrest me, you'll cause very serious diplomatic issues.