Deferred Engine Playground - download


Thanks for the interest!

I am on mobile right now, I’ll get back to your question when I find time.

I did add a license to the github repo (MIT)

I’ve heard complaints about mit before, but i just went ahead with it since it’s better than nothing. I haven’t had time to review the others


thanks for your time, there’s no hurry in any of my questions.

I’ve spent the weekend transplanting you point lights into my game, they look great, thanks again! (although it’s a little sad that I’ve probably spend more time transplanting them than you doing them, but hey… let’s be positive :wink: )

I’m targetting the directional light now but I’m observing a problem. I’ve taken the source code at github as is, commented out all point lights, and removed comment for the directional light. The light looks good until moved. When moved, some shadows disappear.

I’ve made a small private video to show it:

After making the video I found that the directional light “going up” shown at the end of the video can be alleviated with the shadowDepth parameter so you can ignore that part. And anyways, I won’t need a shadow so high.

However I’m unable to understand nor find why the shadow disappears once you move the directional light.

At first I though that maybe the top roof geometry was being skipped because of frustum culling, but I removed the culling forcing all objects to be rendered always ( bool isUsed = true; in MeshMaterialLibrary.Draw ) but the problem persists.

Any ideas on what could be causing that? I’ve been playing a bit with the parameters but I’ve found no combination that works.

Thanks a lot!

I have a function where I check what shadows need updating and it checks for objects moving or lights moving. I can’t look it up right now, but the problem should be there (on mobile)

It’s in rendermodules/shadowmaprendermodule.cs ln 302ff I think

Hi Kosmonaut, thanks for the answer. I tried to remove all ifs so that it always enters the light generation, but the problem persists.

I’ve been having a look at it. I’m by not means an expert in this field, so take my message with a grain of salt ( lucky you, my blood pression is high and I can’t :slight_smile: )

I’ve rendered the shadowmap for the first frame and subsequent frames. It is rendered differently.

First shadowmap render:

Next shadowmap render:

If you look closer, the second shadowmap shows all the balls (including the ones that are below the roof). I think for some reason the depth buffer is not working well after the first frame. It’s like it’s rendered backwards, or maybe just not using the depth information at all (like DepthStencilState.None, but I suppose the shader doesn’t use the DepthStencilState.) Or maybe it’s near-clipping the roof (but I don’t think so because of the next image)

this image is curious and probably gives some clues about the problem:

I’m hopeful I’ll be able to nail the problem tomorrow :slight_smile:

p.s. in the screenshots I forgot to disable the point lights, so the floor level looks lighted and with shadows, but most of the light as well as all the ball shadows come from the point light.

I couldn’t resist until tomorrow :slight_smile:

commenting the following line makes the directional lights work:

if (!lightViewPointChanged && hasAnyObjectMoved)

in MeshMaterialLibrary.CheckShadowMapUpdateNeeds ~ line 478

maybe should be if (lightViewPointChanged || hasAnyObjectMoved) ?

yes you are probably right.

EDIT: I’m not sure about the problem, I couldn’t replicate it on a quick test. Maybe I didn’t delete all point lights.

weird, it happens to me all the time even without removing the point lights. Just git clone, uncomment the directional light (I have to change an enum otherwise won’t compile), run and move the light.

That’s something I thought last night that is also puzzling: The line I’m commenting is common to both Directional and Point lights. If it fails in Directional lights, why doesn’t have the same problem with Point lights, if most of the code is the same?

What MG version are you on? here I’ll try to update my drivers later just in case.

ah i understand your problem now. I thought all shadows would disappear. I will check it out.

I also went ahead and pushed the old experimental branch with some SDF stuff you can read about on the blog:


I fixed the problem with this commit and i also fixed the SSAO aspect ratio bug.


Hi Kosmonaut,

I’m still playing with directional lights and I found banding when integrating it in several scenes of my game. After some dwelling I realized that the problem also appeared in the DeferredEngine, but only became aparent when switching to Default Material.

It’s very apparent in vertical walls but it’s also happening in the floor

Any ideas of what can be causing this? ZBuffer precision maybe?


Banding is a problem in most 3d engines to some extent (see unity/Unreal/CryEngine) and then have the light be very close to a surface.

It’s a very known issue because of shadowmap precision, you will find a lot of resources on that if you google shadow banding.

A typical way to change this behaviour, is to add a little bias for the shadowmap. You can change it by going into the console (or Gamesettings) and playing around with ShadowBias. Big values will lead to objects looking floaty though.

EDIT: I’ll make a patch that supports the changing of this value a bit better, i noticed the code was not good enough.

1 Like

thanks for the banding explanation, I was aware of the bias and floating objects, however all examples I saw on internet where showing this problem as “shadow acne”, so I wasn’t sure if this was the same problem.

New question :wink:

When you reconstruct the position with the depth buffer, i.e.

float3 currentPos = input.ViewDir * linearDepth;

what space is the resulting currentPos on?

I’m trying to adapt the cascade shadow map example from Tim Jones and requires the positions in world space, but I’m struggling to get that position in world space because I don’t quite understand what space I’m on in the first place.

Thanks again, and sorry for the flurry of questions!

Hi again!

I wasn’t searching for it, but I realized that changing the shadow filtering type of the directional light removed the banding. I think the banding in SoftPCF3x/5x is caused by CalcShadowTermSoftPCF, which apparently is not using the DepthBias.

Hello Kosmonaut,deferred engine is a great work,but sadly I couldn’t build the project.
Here is a ArgumentNullException in Main function calling.
My environment is VS2015 update 3 with MonoGame
I need your help.

@kosmonautgames Are you still about?

Noticed this thread has been dead for months… just seeing what’s up and bumping this for newcomers to take a peak at this gem.

How kind of you. I still lurk around in this forum space, but my own development stopped for quite a while since my free time is limited lately :confused:

I am sad to hear that. Anyway I wish you best of luck, you done amazing work.

Hi,@kosmonautgames the depth buffer in the engine is linearEyeDepth? Do you know how what type depth buffer unity using? Becoz I got some source code from unity, and try to implement it.

how to render volume light for spot light, current ray marching algorithm only work on point light. any idea to modifier?

The shader of the point light raymarching should in theory be usable for the directional light or any other light. It simply steps through the shadow map.

However, if the directional light uses a different algorithm for storing shadows - that has to be adapted when reading the shadow data in the volumetric light shader.