Skip to content

Commit

Permalink
chore: add profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jul 3, 2024
1 parent 05d426f commit 0fbedb4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Packages/src/Runtime/Injectors/InjectionProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,13 @@ public bool IsValid(Material material)

public void Inject(List<Material> materials)
{
Profiler.BeginSample("(MPI)[InjectionProperty] Inject");
for (var j = 0; j < materials.Count; j++)
{
Inject(materials[j]);
}

Profiler.EndSample();
}

public void Inject(Material material)
Expand Down Expand Up @@ -311,7 +314,11 @@ public void Init(Material mat)
m_Int = default;
m_Injector = default;
id = Shader.PropertyToID(m_PropertyName);
if (!mat || !mat.shader) return;
if (!mat || !mat.shader)
{
Profiler.EndSample();
return;
}

var index = mat.shader.FindPropertyIndex(m_PropertyName);
m_Type = 0 <= index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static void Rebuild(this List<InjectionProperty> self, UIMaterialProperty
// Animatable: Create new Injector if needed and rebind it.
if (!ip.injector && allowAddInjector)
{
Profiler.BeginSample("(MPI)[InjectorList] Rebuild > AddInjector");
ip.injector = ip.AddInjector(host);
Profiler.EndSample();
}
}
else
Expand All @@ -73,10 +75,13 @@ public static void Rebuild(this List<InjectionProperty> self, UIMaterialProperty

public static void ResetToDefault(this List<InjectionProperty> self, Material material)
{
Profiler.BeginSample("(MPI)[InjectorList] ResetToDefault");
for (var i = 0; i < self.Count; i++)
{
self[i].ResetToDefault(material);
}

Profiler.EndSample();
}
}
}
28 changes: 19 additions & 9 deletions Packages/src/Runtime/UIMaterialPropertyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,19 @@ private void OnValidate()

Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
{
Profiler.BeginSample("(MPI)[MPInjector] GetModifiedMaterial");
if (_defaultMaterialMode)
{
_defaultMaterialMode = false;
Profiler.EndSample();

return baseMaterial;
}

if (!isActiveAndEnabled || baseMaterial == null || !canInject)
{
MaterialRepository.Release(ref _material);
Profiler.EndSample();

return baseMaterial;
}

Profiler.BeginSample("(MPI)[MPInjector] GetModifiedMaterial");
Profiler.BeginSample("(MPI)[MPInjector] GetModifiedMaterial > Calc Hash");
var pHash = 0L;
for (var i = 0; i < properties.Count; i++)
Expand Down Expand Up @@ -367,17 +363,21 @@ private InjectionProperty GetOrAddProperty(string propertyName, PropertyType typ
if (m_Properties.TryGet(propertyName, out var result)) return result;

// Not found: Add new property.
Profiler.BeginSample("(MPI)[Injector] GetOrAddProperty");
var p = new InjectionProperty(this, propertyName, type);
if (animatable)
{
// Animatable: Create new Injector if needed and rebind it.
Profiler.BeginSample("(MPI)[Injector] GetOrAddProperty > AddInjector");
p.injector = p.AddInjector(this);
Profiler.EndSample();
}

// Reset to default first.
p.ResetToDefault(material);
m_Properties.Add(p);
ShouldRebuild();
Profiler.EndSample();
return p;
}

Expand All @@ -386,6 +386,7 @@ private void InjectIfNeeded()
// Skip if not dirty.
if (!graphic || !_dirty || !canInject) return;
_dirty = false;
Profiler.BeginSample("(MPI)[Injector] InjectIfNeeded");

// Inject properties to materials.
graphic.GetMaterialsForRendering(s_Materials);
Expand All @@ -395,21 +396,29 @@ private void InjectIfNeeded()
}

s_Materials.Clear();
Profiler.EndSample();
}

/// <summary>
/// Set material dirty for the graphic and children.
/// </summary>
private void SetMaterialDirty()
{
Profiler.BeginSample("(MPI)[MPInjector] SetMaterialDirty");

// Set material dirty for the graphic.
if (graphic && graphic.isActiveAndEnabled)
{
graphic.SetMaterialDirty();
}

// Set material dirty for children.
if (_children == null) return;
if (_children == null)
{
Profiler.EndSample();
return;
}

for (var i = 0; i < children.Count; i++)
{
var child = children[i];
Expand All @@ -418,18 +427,19 @@ private void SetMaterialDirty()
child.SetMaterialDirty();
}
}

Profiler.EndSample();
}

/// <summary>
/// Mark the injectors as dirty.
/// </summary>
public void SetDirty()
{
Profiler.BeginSample("(MPI)[MPInjector] SetDirty");
_dirty = true;


if (_children == null) return;

Profiler.BeginSample("(MPI)[MPInjector] SetDirty");
for (var i = 0; i < children.Count; i++)
{
var child = children[i];
Expand Down

0 comments on commit 0fbedb4

Please sign in to comment.