@@ -9,6 +9,7 @@ internal class MpbCacheEntry
9
9
{
10
10
internal readonly MaterialPropertyBlock Mpb = new ( ) ;
11
11
internal readonly Dictionary < Props , List < int > > ManagedIds = [ ] ;
12
+ internal bool Changed = true ;
12
13
}
13
14
14
15
internal class CompiledProps
@@ -17,65 +18,82 @@ internal class CompiledProps
17
18
SortedSet < Props > . CreateSetComparer ( ) ;
18
19
19
20
// FIXME: clear old entries...
20
- private static readonly Dictionary < SortedSet < Props > , MpbCacheEntry > mpbCache =
21
+ private static readonly Dictionary < SortedSet < Props > , MpbCacheEntry > MpbCache =
21
22
new ( CascadeKeyComparer ) ;
22
23
23
- internal static void Clear ( ) => mpbCache . Clear ( ) ;
24
+ internal static void Clear ( ) => MpbCache . Clear ( ) ;
24
25
25
26
private readonly SortedSet < Props > cascade = new ( Props . PriorityComparer ) ;
26
27
27
28
internal bool Add ( Props props )
28
29
{
29
- cachedMpb = null ;
30
- return cascade . Add ( props ) ;
30
+ var added = cascade . Add ( props ) ;
31
+ if ( added ) cacheEntry = null ;
32
+ return added ;
31
33
}
32
34
33
- private MaterialPropertyBlock ? cachedMpb = null ;
35
+ private MpbCacheEntry ? cacheEntry = null ;
34
36
35
37
// Should this be a hashset?
36
- private static readonly List < Props > _dirtyProps = [ ] ;
38
+ private static readonly List < Props > _changedProps = [ ] ;
37
39
38
- internal static void UpdateDirtyProps ( )
40
+ internal static void RefreshChangedProps ( )
39
41
{
40
- foreach ( var ( cascade , cache ) in mpbCache ) {
42
+ foreach ( var ( cascade , cache ) in MpbCache ) {
43
+ cache . Changed = false ;
41
44
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 ) ;
44
48
foreach ( var managedId in cache . ManagedIds [ props ] ) {
45
49
props . Write ( managedId , cache . Mpb ) ;
46
50
}
47
51
}
48
52
}
49
53
50
- foreach ( var props in _dirtyProps ) props . Dirty = false ;
51
- _dirtyProps . Clear ( ) ;
54
+ foreach ( var props in _changedProps ) props . Changed = false ;
55
+ _changedProps . Clear ( ) ;
52
56
}
53
57
54
- internal MaterialPropertyBlock Get ( )
58
+ private static MpbCacheEntry BuildCacheEntry ( SortedSet < Props > cascade )
55
59
{
56
- if ( cachedMpb != null ) return cachedMpb ;
60
+ var clonedCascade = new SortedSet < Props > ( cascade , Props . PriorityComparer ) ;
61
+ var entry = MpbCache [ clonedCascade ] = new MpbCacheEntry ( ) ;
57
62
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
+ }
60
69
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 = [ ] ;
66
73
}
67
74
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
+ }
72
78
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!" ) ;
76
94
}
77
95
78
- cachedMpb = cacheEntry . Mpb ;
79
- return cachedMpb ;
96
+ mpb = cacheEntry . Mpb ;
97
+ return true ;
80
98
}
81
99
}
0 commit comments