generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: partial invalidation for preview pipelines (#273)
* feat: partial invalidation for preview pipelines This is currently significantly slower due to more precise change tracking, which causes more preview pipeline rebuilds. * chore: fix proxy object leak * chore: add ToString for NodeController
- Loading branch information
1 parent
2415e8d
commit 5099247
Showing
8 changed files
with
327 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using nadena.dev.ndmf.rq; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace nadena.dev.ndmf.preview | ||
{ | ||
internal class ProxyObjectCache : IDisposable | ||
{ | ||
private static HashSet<int> _proxyObjectInstanceIds = new(); | ||
|
||
public static bool IsProxyObject(GameObject obj) | ||
{ | ||
if (obj == null) return false; | ||
|
||
return _proxyObjectInstanceIds.Contains(obj.GetInstanceID()); | ||
} | ||
|
||
private class RendererState | ||
{ | ||
public Renderer InactiveProxy; | ||
public int ActiveProxyCount = 0; | ||
} | ||
|
||
private Dictionary<Renderer, RendererState> _renderers = new(new ObjectIdentityComparer<Renderer>()); | ||
private bool _cleanupPending = false; | ||
|
||
public Renderer GetOrCreate(Renderer original, Func<Renderer> create) | ||
{ | ||
if (!_renderers.TryGetValue(original, out var state)) | ||
{ | ||
state = new RendererState(); | ||
_renderers.Add(original, state); | ||
} | ||
|
||
Renderer proxy; | ||
if (state.InactiveProxy != null) | ||
{ | ||
proxy = state.InactiveProxy; | ||
state.InactiveProxy = null; | ||
state.ActiveProxyCount++; | ||
return proxy; | ||
} | ||
|
||
Debug.Log("=== Creating new proxy for " + original.gameObject.name); | ||
proxy = create(); | ||
if (proxy == null) | ||
{ | ||
return null; | ||
} | ||
|
||
state.ActiveProxyCount++; | ||
_proxyObjectInstanceIds.Add(proxy.gameObject.GetInstanceID()); | ||
|
||
return proxy; | ||
} | ||
|
||
public void ReturnProxy(Renderer original, Renderer proxy) | ||
{ | ||
if (!_renderers.TryGetValue(original, out var state)) | ||
{ | ||
Debug.Log("ProxyObjectCache: Renderer not found in cache"); | ||
DestroyProxy(proxy); | ||
return; | ||
} | ||
|
||
if (!_cleanupPending) | ||
{ | ||
EditorApplication.delayCall += Cleanup; | ||
_cleanupPending = true; | ||
} | ||
|
||
state.ActiveProxyCount--; | ||
if (state.ActiveProxyCount > 0 && state.InactiveProxy == null) | ||
{ | ||
state.InactiveProxy = proxy; | ||
return; | ||
} | ||
|
||
DestroyProxy(proxy); | ||
|
||
if (state.ActiveProxyCount == 0) | ||
{ | ||
DestroyProxy(state.InactiveProxy); | ||
_renderers.Remove(original); | ||
} | ||
} | ||
|
||
private static void DestroyProxy(Renderer proxy) | ||
{ | ||
if (proxy == null) return; | ||
|
||
var gameObject = proxy.gameObject; | ||
_proxyObjectInstanceIds.Remove(gameObject.GetInstanceID()); | ||
Object.DestroyImmediate(gameObject); | ||
} | ||
|
||
private void Cleanup() | ||
{ | ||
_cleanupPending = false; | ||
|
||
foreach (var entry in _renderers.Where(kv => kv.Key == null).ToList()) | ||
{ | ||
if (entry.Value.InactiveProxy != null) | ||
{ | ||
Object.DestroyImmediate(entry.Value.InactiveProxy.gameObject); | ||
} | ||
_renderers.Remove(entry.Key); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var entry in _renderers) | ||
{ | ||
if (entry.Value.InactiveProxy != null) | ||
{ | ||
Object.DestroyImmediate(entry.Value.InactiveProxy.gameObject); | ||
} | ||
} | ||
_renderers.Clear(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.