Deferred Engine Playground - download

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? 3.7.0.1549 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: https://github.com/Kosmonaut3d/DeferredEngine/tree/SDF
https://kosmonautblog.wordpress.com/2017/05/01/signed-distance-field-rendering-journey-pt-1/

3 Likes

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

2 Likes

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?

Thanks!

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 3.7.1.189.
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.

Hi,@kosmonautgames, i try to implement volumetric light from unity to your source. I notice that the depthbuffer, and the light space is different. for unity source the volume light is computed in worldspace, ur source is viewspace. i can get it right no matter how i convert it from viewspace to worldspace. I would greatly appreciate it, if you or anyone can give me some tips?

image

I got it to work by modified the code as below, but I’m having a light leak issue when my light origin is offscreen or behind the camera.

RayMarching Function :

float4 RayMarch(float2 screenPos, float3 rayStart, float3 rayDir, float rayLength, float C2R, float3 cullRayStart, float3 cullRayEnd)
{
float offset = (frac(sin(dot(screenPos /**(frac(_iTime) + 1)*/, float2(15.8989f, 76.132f))) * 46336.23745f));

int _SampleCount = 24;
int stepCount = _SampleCount;
float stepSize = rayLength / stepCount;
float3 step = rayDir * stepSize;
float3 currentPosition = rayStart + step * offset;

float4 vlight = 0;
float cosAngle;
float3 cullRayDir = cullRayEnd - cullRayStart;

// we don’t know about density between camera and light’s volume, assume 0.5
float extinction = length(_cCamPosWS - currentPosition) * _volumetricData.y * 0.5;

[unroll]
for (int i = 0; i < stepCount; ++i)
{
float4 rayPosition = float4(float3(cullRayStart + ((i + offset)/(_SampleCount)) * cullRayDir), 1);

  float atten = GetLightAttenuation(currentPosition);
  float density = 1.0;//GetDensity(currentPosition);

  float scattering = _volumetricData.x * stepSize * density;
  extinction += _volumetricData.y * stepSize * density; // +scattering;

  float4 light = atten * scattering * exp(-extinction);

  if (length(rayPosition.xyz) < C2R - 0.99)
  	vlight += light;
  
  currentPosition += step;

}
return vlight;
}

PointLight Function :

   float3 posVS = max(0.01, lineardepth) * cameraDirVS;
	float3 wpos = mul(float4(positionVS.xyz, 1), _Mat_InvV).xyz;

	float3 rayStart = _cCamPosWS;
	float3 rayEnd =  wpos.xyz;
	//float3 rayEnd = wpos;

	float3 rayDir = (rayEnd - rayStart);
	float rayLength = length(rayDir);
	rayDir /= rayLength;
	
	float projectedDepth = (lineardepth) / dot(_CameraForward, rayDir);

	float L2C = length(_lightPosition);
	float C2R = distance(float3(0, 0, 0), positionVS);
	float3 cullRayStart = normalize(i.positionVS.xyz) * (L2C - _lightRadius);
	float3 cullRayEnd = normalize(i.positionVS.xyz) * (L2C + _lightRadius);

           float3 lightToCamera = _cCamPosWS - _LightPosWS.xyz;
		//lightToCamera -= lightToCamera;
		float b = dot(rayDir, lightToCamera);
		float c = dot(lightToCamera, lightToCamera) - (_lightRadius * _lightRadius);

		float d = sqrt((b * b) - c);
		float start = -b - d;
		float end = -b + d;
		end = max(end, projectedDepth);
		rayStart = rayStart + rayDir * start;
		rayLength = end - start;
	
		o.Volume = RayMarch(i.screenPos.xy, rayStart, rayDir, rayLength, C2R, cullRayStart, cullRayEnd);

Hello kosmonautgames, thank you for sharing this very educational project. It helped me a lot!
I also want to share my work to use the repo with Visual Studio 2019 / MG 3.7 to help others learning.
Because I also had issues with ArgumentNullException in Main function I imported the files to a new solution and did some minor tweak to get it working.
https://github.com/withoutaface/DeferredEngine
Thanks!

1 Like