@@ -9,6 +9,7 @@ internal class MpbCacheEntry
99{
1010 internal readonly MaterialPropertyBlock Mpb = new ( ) ;
1111 internal readonly Dictionary < Props , List < int > > ManagedIds = [ ] ;
12+ internal bool Changed = true ;
1213}
1314
1415internal class CompiledProps
@@ -17,65 +18,82 @@ internal class CompiledProps
1718 SortedSet < Props > . CreateSetComparer ( ) ;
1819
1920 // FIXME: clear old entries...
20- private static readonly Dictionary < SortedSet < Props > , MpbCacheEntry > mpbCache =
21+ private static readonly Dictionary < SortedSet < Props > , MpbCacheEntry > MpbCache =
2122 new ( CascadeKeyComparer ) ;
2223
23- internal static void Clear ( ) => mpbCache . Clear ( ) ;
24+ internal static void Clear ( ) => MpbCache . Clear ( ) ;
2425
2526 private readonly SortedSet < Props > cascade = new ( Props . PriorityComparer ) ;
2627
2728 internal bool Add ( Props props )
2829 {
29- cachedMpb = null ;
30- return cascade . Add ( props ) ;
30+ var added = cascade . Add ( props ) ;
31+ if ( added ) cacheEntry = null ;
32+ return added ;
3133 }
3234
33- private MaterialPropertyBlock ? cachedMpb = null ;
35+ private MpbCacheEntry ? cacheEntry = null ;
3436
3537 // Should this be a hashset?
36- private static readonly List < Props > _dirtyProps = [ ] ;
38+ private static readonly List < Props > _changedProps = [ ] ;
3739
38- internal static void UpdateDirtyProps ( )
40+ internal static void RefreshChangedProps ( )
3941 {
40- foreach ( var ( cascade , cache ) in mpbCache ) {
42+ foreach ( var ( cascade , cache ) in MpbCache ) {
43+ cache . Changed = false ;
4144 foreach ( var props in cascade ) {
42- if ( ! props . Dirty ) continue ;
43- _dirtyProps . Add ( props ) ;
45+ if ( ! props . Changed ) continue ;
46+ cache . Changed = true ;
47+ _changedProps . Add ( props ) ;
4448 foreach ( var managedId in cache . ManagedIds [ props ] ) {
4549 props . Write ( managedId , cache . Mpb ) ;
4650 }
4751 }
4852 }
4953
50- foreach ( var props in _dirtyProps ) props . Dirty = false ;
51- _dirtyProps . Clear ( ) ;
54+ foreach ( var props in _changedProps ) props . Changed = false ;
55+ _changedProps . Clear ( ) ;
5256 }
5357
54- internal MaterialPropertyBlock Get ( )
58+ private static MpbCacheEntry BuildCacheEntry ( SortedSet < Props > cascade )
5559 {
56- if ( cachedMpb != null ) return cachedMpb ;
60+ var clonedCascade = new SortedSet < Props > ( cascade , Props . PriorityComparer ) ;
61+ var entry = MpbCache [ clonedCascade ] = new MpbCacheEntry ( ) ;
5762
58- if ( ! mpbCache . TryGetValue ( cascade , out var cacheEntry ) ) {
59- mpbCache [ cascade ] = cacheEntry = new MpbCacheEntry ( ) ;
63+ Dictionary < int , Props > idManagers = [ ] ;
64+ foreach ( var props in cascade ) {
65+ foreach ( var id in props . ManagedIds ) {
66+ idManagers [ id ] = props ;
67+ }
68+ }
6069
61- Dictionary < int , Props > idManagers = [ ] ;
62- foreach ( var props in cascade ) {
63- foreach ( var id in props . ManagedIds ) {
64- idManagers [ id ] = props ;
65- }
70+ foreach ( var ( id , props ) in idManagers ) {
71+ if ( ! entry . ManagedIds . TryGetValue ( props , out var ids ) ) {
72+ entry . ManagedIds [ props ] = ids = [ ] ;
6673 }
6774
68- foreach ( var ( id , props ) in idManagers ) {
69- if ( ! cacheEntry . ManagedIds . TryGetValue ( props , out var ids ) ) {
70- cacheEntry . ManagedIds [ props ] = ids = [ ] ;
71- }
75+ ids . Add ( id ) ;
76+ props . Write ( id , entry . Mpb ) ;
77+ }
7278
73- ids . Add ( id ) ;
74- props . Write ( id , cacheEntry . Mpb ) ;
75- }
79+ return entry ;
80+ }
81+
82+ internal bool GetIfChanged ( out MaterialPropertyBlock ? mpb )
83+ {
84+ if ( cacheEntry != null ) {
85+ mpb = cacheEntry . Changed ? cacheEntry . Mpb : null ;
86+ return cacheEntry . Changed ;
87+ }
88+
89+ if ( ! MpbCache . TryGetValue ( cascade , out cacheEntry ) ) {
90+ Debug . Log ( "cache not hit" ) ;
91+ cacheEntry = BuildCacheEntry ( cascade ) ;
92+ } else {
93+ Debug . Log ( "cache hit!" ) ;
7694 }
7795
78- cachedMpb = cacheEntry . Mpb ;
79- return cachedMpb ;
96+ mpb = cacheEntry . Mpb ;
97+ return true ;
8098 }
8199}
0 commit comments