Skip to content

Commit 1ef8400

Browse files
[Core] Fixes for Multithreaded renderer;
[Rendering] Add instance properties for Materials; [Baker] Add instance properties to auto generated materials;
1 parent 516c137 commit 1ef8400

File tree

11 files changed

+594
-276
lines changed

11 files changed

+594
-276
lines changed

Engine/Core/Entities/EntitySystemManager.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Threading;
56

67
namespace Staple;
78

@@ -21,7 +22,9 @@ internal sealed class EntitySystemManager : ISubsystem
2122

2223
private readonly Dictionary<string, object> cachedSubclasses = [];
2324

24-
private readonly object lockObject = new();
25+
private readonly Lock lockObject = new();
26+
27+
internal Action onSubsystemsModified;
2528

2629
public static readonly EntitySystemManager Instance = new();
2730

@@ -121,6 +124,15 @@ internal void UnloadSystemsFromAssembly(Assembly assembly)
121124
}
122125

123126
cachedSubclasses.Clear();
127+
128+
try
129+
{
130+
onSubsystemsModified?.Invoke();
131+
}
132+
catch (Exception e)
133+
{
134+
Log.Error($"[EntitySystemManager] Subsystem modified event exception: {e}");
135+
}
124136
}
125137
}
126138

@@ -181,6 +193,15 @@ public void RegisterSystem(object system)
181193
}
182194

183195
cachedSubclasses.Clear();
196+
197+
try
198+
{
199+
onSubsystemsModified?.Invoke();
200+
}
201+
catch (Exception e)
202+
{
203+
Log.Error($"[EntitySystemManager] Subsystem modified event exception: {e}");
204+
}
184205
}
185206
}
186207

Engine/Core/MessagePackGenerated/MessagePackGenerated.cs

Lines changed: 276 additions & 199 deletions
Large diffs are not rendered by default.

Engine/Core/Physics/3D/Physics3D.cs

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
33
using System.Numerics;
4+
using System.Threading;
45

56
namespace Staple.Internal;
67

@@ -36,7 +37,11 @@ internal set
3637
internal static float PhysicsDeltaTime = 1 / 60.0f;
3738

3839
private float deltaTimer = 0.0f;
40+
private bool needsSubsystemCheck = true;
3941
private readonly bool implIsValid;
42+
private readonly Lock lockObject = new();
43+
44+
private IPhysicsReceiver3D[] physicsReceivers = [];
4045

4146
public delegate void OnBodyActivated3D(IBody3D body);
4247
public delegate void OnBodyDeactivated3D(IBody3D body);
@@ -81,6 +86,8 @@ public Physics3D(IPhysics3D impl)
8186
{
8287
Impl = impl;
8388
implIsValid = impl != null;
89+
90+
EntitySystemManager.Instance.onSubsystemsModified += () => needsSubsystemCheck = true;
8491
}
8592

8693
#region API
@@ -590,6 +597,16 @@ public void Update()
590597
return;
591598
}
592599

600+
if(needsSubsystemCheck)
601+
{
602+
needsSubsystemCheck = false;
603+
604+
lock(lockObject)
605+
{
606+
physicsReceivers = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
607+
}
608+
}
609+
593610
deltaTimer += Time.unscaledDeltaTime;
594611

595612
if(deltaTimer >= PhysicsDeltaTime)
@@ -600,65 +617,71 @@ public void Update()
600617
}
601618
}
602619

603-
public static void BodyActivated(IBody3D body)
620+
public void BodyActivated(IBody3D body)
604621
{
605-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
606-
607-
foreach (var system in systems)
622+
lock (lockObject)
608623
{
609-
system.OnBodyActivated(body);
624+
foreach (var system in physicsReceivers)
625+
{
626+
system.OnBodyActivated(body);
627+
}
610628
}
611629
}
612630

613-
public static void BodyDeactivated(IBody3D body)
631+
public void BodyDeactivated(IBody3D body)
614632
{
615-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
616-
617-
foreach (var system in systems)
633+
lock (lockObject)
618634
{
619-
system.OnBodyDeactivated(body);
635+
foreach (var system in physicsReceivers)
636+
{
637+
system.OnBodyDeactivated(body);
638+
}
620639
}
621640
}
622641

623-
public static void ContactAdded(IBody3D A, IBody3D B)
642+
public void ContactAdded(IBody3D A, IBody3D B)
624643
{
625-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
626-
627-
foreach (var system in systems)
644+
lock (lockObject)
628645
{
629-
system.OnContactAdded(A, B);
646+
foreach (var system in physicsReceivers)
647+
{
648+
system.OnContactAdded(A, B);
649+
}
630650
}
631651
}
632652

633-
public static void ContactPersisted(IBody3D A, IBody3D B)
653+
public void ContactPersisted(IBody3D A, IBody3D B)
634654
{
635-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
636-
637-
foreach (var system in systems)
655+
lock (lockObject)
638656
{
639-
system.OnContactPersisted(A, B);
657+
foreach (var system in physicsReceivers)
658+
{
659+
system.OnContactPersisted(A, B);
660+
}
640661
}
641662
}
642663

643-
public static void ContactRemoved(IBody3D A, IBody3D B)
664+
public void ContactRemoved(IBody3D A, IBody3D B)
644665
{
645-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
646-
647-
foreach (var system in systems)
666+
lock (lockObject)
648667
{
649-
system.OnContactRemoved(A, B);
668+
foreach (var system in physicsReceivers)
669+
{
670+
system.OnContactRemoved(A, B);
671+
}
650672
}
651673
}
652674

653-
public static bool ContactValidate(IBody3D A, IBody3D B)
675+
public bool ContactValidate(IBody3D A, IBody3D B)
654676
{
655-
var systems = EntitySystemManager.Instance.FindEntitySystemsSubclassing<IPhysicsReceiver3D>();
656-
657-
foreach (var system in systems)
677+
lock (lockObject)
658678
{
659-
if(system.OnContactValidate(A, B) == false)
679+
foreach (var system in physicsReceivers)
660680
{
661-
return false;
681+
if (system.OnContactValidate(A, B) == false)
682+
{
683+
return false;
684+
}
662685
}
663686
}
664687

Engine/Core/Serialization/Material/MaterialMetadata.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public enum MaterialParameterType
2121
TextureWrap,
2222
}
2323

24+
[JsonConverter(typeof(JsonStringEnumConverter<MaterialParameterSource>))]
25+
public enum MaterialParameterSource
26+
{
27+
Uniform,
28+
Instance,
29+
}
30+
2431
[Serializable]
2532
[MessagePackObject]
2633
public class Vector2Holder
@@ -343,6 +350,9 @@ public class MaterialParameter
343350
[Key(7)]
344351
public TextureWrap textureWrapValue;
345352

353+
[Key(8)]
354+
public MaterialParameterSource source;
355+
346356
public bool ShouldSerializevec2Value() => vec2Value != null && type == MaterialParameterType.Vector2;
347357

348358
public bool ShouldSerializevec3Value() => vec3Value != null && type == MaterialParameterType.Vector3;
@@ -366,6 +376,7 @@ public MaterialParameter Clone()
366376
colorValue = colorValue,
367377
floatValue = floatValue,
368378
textureWrapValue = textureWrapValue,
379+
source = source,
369380
};
370381

371382
if(vec2Value != null)
@@ -420,7 +431,8 @@ public MaterialParameter Clone()
420431
lhs.textureValue == rhs.textureValue &&
421432
lhs.colorValue == rhs.colorValue &&
422433
lhs.floatValue == rhs.floatValue &&
423-
lhs.textureWrapValue == rhs.textureWrapValue;
434+
lhs.textureWrapValue == rhs.textureWrapValue &&
435+
lhs.source == rhs.source;
424436
}
425437

426438
public static bool operator !=(MaterialParameter lhs, MaterialParameter rhs)
@@ -442,7 +454,8 @@ public MaterialParameter Clone()
442454
lhs.textureValue != rhs.textureValue ||
443455
lhs.colorValue != rhs.colorValue ||
444456
lhs.floatValue != rhs.floatValue ||
445-
lhs.textureWrapValue != rhs.textureWrapValue;
457+
lhs.textureWrapValue != rhs.textureWrapValue ||
458+
lhs.source != rhs.source;
446459
}
447460

448461
public override bool Equals(object obj)
@@ -462,12 +475,25 @@ public override bool Equals(object obj)
462475

463476
public override int GetHashCode()
464477
{
465-
return HashCode.Combine(type, vec2Value, vec3Value, vec4Value, textureValue, colorValue, floatValue, textureWrapValue);
478+
var hash = new HashCode();
479+
480+
hash.Add(type);
481+
hash.Add(vec2Value);
482+
hash.Add(vec3Value);
483+
hash.Add(vec4Value);
484+
hash.Add(textureValue);
485+
hash.Add(colorValue);
486+
hash.Add(floatValue);
487+
hash.Add(textureWrapValue);
488+
hash.Add(source);
489+
490+
return hash.ToHashCode();
466491
}
467492
}
468493

469494
[JsonSourceGenerationOptions(IncludeFields = true)]
470495
[JsonSerializable(typeof(JsonStringEnumConverter<MaterialParameterType>))]
496+
[JsonSerializable(typeof(JsonStringEnumConverter<MaterialParameterSource>))]
471497
[JsonSerializable(typeof(float))]
472498
[JsonSerializable(typeof(string))]
473499
[JsonSerializable(typeof(Color32))]

Engine/Core/Serialization/Shader/ShaderMetadata.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public class ShaderUniform
3838
public int slot;
3939
}
4040

41+
[MessagePackObject]
42+
public class ShaderInstanceParameter
43+
{
44+
[Key(0)]
45+
public string name;
46+
47+
[Key(1)]
48+
public ShaderUniformType type;
49+
}
50+
4151
[MessagePackObject]
4252
public class ShaderMetadata
4353
{
@@ -55,12 +65,15 @@ public class ShaderMetadata
5565
public List<ShaderUniform> uniforms = [];
5666

5767
[Key(4)]
58-
public BlendMode sourceBlend = BlendMode.Off;
68+
public List<ShaderInstanceParameter> instanceParameters = [];
5969

6070
[Key(5)]
71+
public BlendMode sourceBlend = BlendMode.Off;
72+
73+
[Key(6)]
6174
public BlendMode destinationBlend = BlendMode.Off;
6275

6376
[HideInInspector]
64-
[Key(6)]
77+
[Key(7)]
6578
public string typeName = typeof(Shader).FullName;
6679
}

0 commit comments

Comments
 (0)