Skip to content

Commit

Permalink
Merge branch 'master' into net9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanedds committed Nov 17, 2024
2 parents b93f424 + 58d9618 commit 72da482
Show file tree
Hide file tree
Showing 82 changed files with 547 additions and 724 deletions.
32 changes: 14 additions & 18 deletions Nu/Nu.Gaia/Assets/Default/PhysicallyBasedDeferredLighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@ const float ATTENUATION_CONSTANT = 1.0;
const int LIGHTS_MAX = 32;
const int SHADOW_TEXTURES_MAX = 16;
const int SHADOW_MAPS_MAX = 8;
const int SHADOW_SAMPLES = 3;
const float SHADOW_BIAS = 0.005;
const float SHADOW_FOV_MAX = 2.1;
const float SHADOW_SEAM_INSET = 0.001;
const float SHADOW_SAMPLE_SCALAR = 0.0025;
const int SHADOW_SAMPLE_OFFSETS_COUNT = 20;
const vec3 SHADOW_SAMPLE_OFFSETS[SHADOW_SAMPLE_OFFSETS_COUNT] =
vec3[](
vec3(1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
vec3(1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(1, 0, 1), vec3(-1, 0, 1), vec3(1, 0, -1), vec3(-1, 0, -1),
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1));

const vec4 SSVF_DITHERING[4] =
vec4[](
Expand All @@ -44,10 +37,9 @@ uniform vec3 eyeCenter;
uniform mat4 view;
uniform mat4 projection;
uniform float lightCutoffMargin;
uniform float lightShadowSampleScalar;
uniform float lightShadowExponent;
uniform float lightShadowDensity;
uniform float lightShadowBleedFilter;
uniform float lightShadowVarianceMin;
uniform int ssvfEnabled;
uniform int ssvfSteps;
uniform float ssvfAsymmetry;
Expand Down Expand Up @@ -196,15 +188,19 @@ float computeShadowMapScalar(vec4 position, vec3 lightOrigin, samplerCube shadow
{
vec3 positionShadow = position.xyz - lightOrigin;
float shadowZ = length(positionShadow);
float shadowZExp = exp(-lightShadowExponent * 0.1 * shadowZ);
float shadowDepthExp = 0.0;
for (int i = 0; i < SHADOW_SAMPLE_OFFSETS_COUNT; ++i)
float shadowHits = 0.0;
for (int i = 0; i < SHADOW_SAMPLES; ++i)
{
// NOTE: we divide at each step to avoid overflow with an already large number.
shadowDepthExp += texture(shadowMap, positionShadow / shadowZ + SHADOW_SAMPLE_OFFSETS[i] * SHADOW_SAMPLE_SCALAR).y / SHADOW_SAMPLE_OFFSETS_COUNT;
for (int j = 0; j < SHADOW_SAMPLES; ++j)
{
for (int k = 0; k < SHADOW_SAMPLES; ++k)
{
vec3 offset = (vec3(i, j, k) - vec3(SHADOW_SAMPLES / 2.0)) * (lightShadowSampleScalar / SHADOW_SAMPLES);
shadowHits += shadowZ - SHADOW_BIAS > texture(shadowMap, positionShadow + offset).x ? 1.0 : 0.0;
}
}
}
float shadowScalar = clamp(shadowZExp * shadowDepthExp, 0.0, 1.0);
return pow(shadowScalar, lightShadowDensity);
return 1.0 - shadowHits / (SHADOW_SAMPLES * SHADOW_SAMPLES * SHADOW_SAMPLES);
}

vec3 computeFogAccumDirectional(vec4 position, int lightIndex)
Expand Down
32 changes: 14 additions & 18 deletions Nu/Nu.Gaia/Assets/Default/PhysicallyBasedForwardStatic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,10 @@ const int LIGHT_MAPS_MAX = 2;
const int LIGHTS_MAX = 8;
const int SHADOW_TEXTURES_MAX = 16;
const int SHADOW_MAPS_MAX = 4;
const int SHADOW_SAMPLES = 3;
const float SHADOW_BIAS = 0.005;
const float SHADOW_FOV_MAX = 2.1;
const float SHADOW_SEAM_INSET = 0.001;
const float SHADOW_SAMPLE_SCALAR = 0.0025;
const int SHADOW_SAMPLE_OFFSETS_COUNT = 20;
const vec3 SHADOW_SAMPLE_OFFSETS[SHADOW_SAMPLE_OFFSETS_COUNT] =
vec3[](
vec3(1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
vec3(1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(1, 0, 1), vec3(-1, 0, 1), vec3(1, 0, -1), vec3(-1, 0, -1),
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1));

const vec4 SSVF_DITHERING[4] =
vec4[4](
Expand All @@ -90,8 +83,7 @@ uniform vec3 lightAmbientColor;
uniform float lightAmbientBrightness;
uniform float lightShadowExponent;
uniform float lightShadowDensity;
uniform float lightShadowBleedFilter;
uniform float lightShadowVarianceMin;
uniform float lightShadowSampleScalar;
uniform int ssvfEnabled;
uniform int ssvfSteps;
uniform float ssvfAsymmetry;
Expand Down Expand Up @@ -261,15 +253,19 @@ float computeShadowMapScalar(vec4 position, vec3 lightOrigin, samplerCube shadow
{
vec3 positionShadow = position.xyz - lightOrigin;
float shadowZ = length(positionShadow);
float shadowZExp = exp(-lightShadowExponent * 0.1 * shadowZ);
float shadowDepthExp = 0.0;
for (int i = 0; i < SHADOW_SAMPLE_OFFSETS_COUNT; ++i)
float shadowHits = 0.0;
for (int i = 0; i < SHADOW_SAMPLES; ++i)
{
// NOTE: we divide at each step to avoid overflow with an already large number.
shadowDepthExp += texture(shadowMap, positionShadow / shadowZ + SHADOW_SAMPLE_OFFSETS[i] * SHADOW_SAMPLE_SCALAR).y / SHADOW_SAMPLE_OFFSETS_COUNT;
for (int j = 0; j < SHADOW_SAMPLES; ++j)
{
for (int k = 0; k < SHADOW_SAMPLES; ++k)
{
vec3 offset = (vec3(i, j, k) - vec3(SHADOW_SAMPLES / 2.0)) * (lightShadowSampleScalar / SHADOW_SAMPLES);
shadowHits += shadowZ - SHADOW_BIAS > texture(shadowMap, positionShadow + offset).x ? 1.0 : 0.0;
}
}
}
float shadowScalar = clamp(shadowZExp * shadowDepthExp, 0.0, 1.0);
return pow(shadowScalar, lightShadowDensity);
return 1.0 - shadowHits / (SHADOW_SAMPLES * SHADOW_SAMPLES * SHADOW_SAMPLES);
}

vec3 computeFogAccumDirectional(vec4 position, int lightIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ void main()
{
case 0: // point light
depths.x = length(positionOut.xyz - eyeCenter);
depths.y = exp(lightShadowExponent * 0.1 * depths.x);
break;
case 1: // spot light
depths.x = gl_FragCoord.z;
Expand Down
1 change: 0 additions & 1 deletion Nu/Nu.Gaia/Assets/Default/PhysicallyBasedShadowStatic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void main()
{
case 0: // point light
depths.x = length(positionOut.xyz - eyeCenter);
depths.y = exp(lightShadowExponent * 0.1 * depths.x);
break;
case 1: // spot light
depths.x = gl_FragCoord.z;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void main()
{
case 0: // point light
depths.x = length(positionOut.xyz - eyeCenter);
depths.y = exp(lightShadowExponent * 0.1 * depths.x);
break;
case 1: // spot light
depths.x = gl_FragCoord.z;
Expand Down
29 changes: 19 additions & 10 deletions Nu/Nu.Math/ArrayPooled.cs → Nu/Nu.Math/PooledArray.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

Expand All @@ -10,12 +9,12 @@ namespace Nu
/// <summary>
/// An array from a sychronized global array pool.
/// </summary>
public class ArrayPooled<T> : IDisposable, IEnumerable<T>
public class PooledArray<T> : IDisposable, IEnumerable<T>
{
/// <summary>
/// Create a pooled array.
/// </summary>
public ArrayPooled(int length, bool clearOnFree)
public PooledArray(int length, bool clearOnFree)
{
array = Alloc(length);
this.clearOnFree = clearOnFree;
Expand Down Expand Up @@ -61,10 +60,10 @@ public T[] Deref
/// <summary>
/// Clone the pooled array.
/// </summary>
public ArrayPooled<T> Clone()
public PooledArray<T> Clone()
{
ThrowIfDisposed();
var arr = new ArrayPooled<T>(array.Length, clearOnFree);
var arr = new PooledArray<T>(array.Length, clearOnFree);
array.CopyTo(arr.array, 0);
return arr;
}
Expand All @@ -84,7 +83,7 @@ public override int GetHashCode()
public override bool Equals(object that)
{
ThrowIfDisposed();
var thatArrayPooled = that as ArrayPooled<T>;
var thatArrayPooled = that as PooledArray<T>;
return array == thatArrayPooled.array;
}

Expand All @@ -93,22 +92,27 @@ public override bool Equals(object that)
/// </summary>
public override string ToString()
{
ThrowIfDisposed();
return array.ToString();
}

/// <summary>
/// Generic enumeration.
/// Do NOT hold onto this past this object's life time!
/// </summary>
public IEnumerator<T> GetEnumerator()
{
ThrowIfDisposed();
return ((IEnumerable<T>)array).GetEnumerator();
}

/// <summary>
/// General enumeration.
/// Do NOT hold onto this past this object's life time!
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
ThrowIfDisposed();
return array.GetEnumerator();
}

Expand All @@ -117,11 +121,14 @@ IEnumerator IEnumerable.GetEnumerator()
/// </summary>
public void Dispose()
{
Free(array, clearOnFree);
GC.SuppressFinalize(this);
if (Interlocked.CompareExchange(ref disposed, 1, 0) == 0)
{
Free(array, clearOnFree);
GC.SuppressFinalize(this);
}
}

~ArrayPooled()
~PooledArray()
{
Free(array, clearOnFree);
}
Expand All @@ -130,12 +137,14 @@ public void Dispose()
private void ThrowIfDisposed()
{
if (Interlocked.CompareExchange(ref disposed, 0, 0) == 1)
{
throw new ObjectDisposedException(GetType().FullName);
}
}

private readonly T[] array;
private readonly bool clearOnFree;
private int disposed;
private volatile int disposed;

private static T[] Alloc(int length)
{
Expand Down
31 changes: 23 additions & 8 deletions Nu/Nu.Math/CollectionPooled.cs → Nu/Nu.Math/PooledCollection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

Expand All @@ -10,12 +9,12 @@ namespace Nu
/// A collection from a sychronized global collection pool.
/// Note that you'll have to Deref in order to enumerate this efficiently.
/// </summary>
public class CollectionPooled<C, T> : IDisposable where C : ICollection<T>
public class PooledCollection<C, T> : IDisposable where C : ICollection<T>
{
/// <summary>
/// Create a pooled collection.
/// </summary>
public CollectionPooled(Func<C> create)
public PooledCollection(Func<C> create)
{
coll = Alloc(create);
}
Expand Down Expand Up @@ -102,6 +101,16 @@ public void CopyTo(T[] array, int arrayIndex)
coll.CopyTo(array, arrayIndex);
}

/// <summary>
/// Clone.
/// </summary>
public PooledCollection<C, T> Clone(Func<C> create)
{
var coll = new PooledCollection<C, T>(create);
foreach (var item in this.coll) coll.Add(item);
return coll;
}

/// <summary>
/// Hashing.
/// </summary>
Expand All @@ -118,7 +127,7 @@ public override bool Equals(object that)
{
if (that == null) return false;
ThrowIfDisposed();
var thatObjectPooled = that as CollectionPooled<C, T>;
var thatObjectPooled = that as PooledCollection<C, T>;
return coll.Equals(thatObjectPooled.coll);
}

Expand All @@ -127,6 +136,7 @@ public override bool Equals(object that)
/// </summary>
public override string ToString()
{
ThrowIfDisposed();
return coll.ToString();
}

Expand All @@ -135,11 +145,14 @@ public override string ToString()
/// </summary>
public void Dispose()
{
Free(coll);
GC.SuppressFinalize(this);
if (Interlocked.CompareExchange(ref disposed, 1, 0) == 0)
{
Free(coll);
GC.SuppressFinalize(this);
}
}

~CollectionPooled()
~PooledCollection()
{
Free(coll);
}
Expand All @@ -148,11 +161,13 @@ public void Dispose()
private void ThrowIfDisposed()
{
if (Interlocked.CompareExchange(ref disposed, 0, 0) == 1)
{
throw new ObjectDisposedException(GetType().FullName);
}
}

private readonly C coll;
private int disposed;
private volatile int disposed;

private static C Alloc(Func<C> create)
{
Expand Down
Loading

0 comments on commit 72da482

Please sign in to comment.