@@ -18,6 +18,7 @@ namespace CodeBlaze.Vloxy.Engine.Jobs.Data {
1818
1919 public class ChunkDataScheduler {
2020
21+ internal bool CanProcess { get ; private set ; }
2122 internal bool Processing { get ; private set ; }
2223
2324 private int3 _ChunkSize ;
@@ -26,16 +27,17 @@ public class ChunkDataScheduler {
2627 private NoiseProfile _NoiseProfile ;
2728 private BurstFunctionPointers _BurstFunctionPointers ;
2829
30+ private bool _Scheduled ;
31+ private int _BatchSize ;
32+
33+ private List < int3 > _ReclaimBatches ;
34+ private Queue < VloxyBatch < int3 > > _ClaimBatches ;
35+ private VloxyBatch < int3 > _CurrentClaimBatch ;
36+
2937 private JobHandle _Handle ;
30-
3138 private NativeList < int3 > _Jobs ;
3239 private NativeParallelHashMap < int3 , Chunk > _Results ;
33- private Queue < VloxyBatch < int3 > > _Batches ;
34- private VloxyBatch < int3 > _CurrentBatch ;
3540
36- private bool _Scheduled ;
37- private int _BatchSize ;
38-
3941#if VLOXY_LOGGING
4042 private Queue < long > _Timings ;
4143 private Stopwatch _Watch ;
@@ -53,8 +55,9 @@ BurstFunctionPointers burstFunctionPointers
5355 _ChunkStore = chunkStore ;
5456 _NoiseProfile = noiseProfile ;
5557 _BurstFunctionPointers = burstFunctionPointers ;
56-
57- _Batches = new Queue < VloxyBatch < int3 > > ( ) ;
58+
59+ _ReclaimBatches = new List < int3 > ( ) ;
60+ _ClaimBatches = new Queue < VloxyBatch < int3 > > ( ) ;
5861
5962 _Jobs = new NativeList < int3 > ( Allocator . Persistent ) ;
6063 _Results = new NativeParallelHashMap < int3 , Chunk > ( settings . Chunk . LoadDistance . CubedSize ( ) , Allocator . Persistent ) ;
@@ -66,7 +69,7 @@ BurstFunctionPointers burstFunctionPointers
6669 }
6770
6871 internal bool Update ( ) {
69- if ( _Scheduled || ! Processing ) return false ;
72+ if ( _Scheduled || ! ( Processing || CanProcess ) ) return false ;
7073
7174 Process ( ) ;
7275
@@ -114,39 +117,9 @@ internal void GenerateChunks(NativeArray<int3> jobs) {
114117 jobs . Dispose ( ) ;
115118 }
116119
117- internal void Reclaim ( List < int3 > positions ) {
118- for ( int i = 0 ; i < positions . Count ; i ++ ) {
119- var position = positions [ i ] ;
120- var state = _ChunkState . GetState ( position ) ;
121-
122- switch ( state ) {
123- case ChunkState . State . UNLOADED :
124- #if VLOXY_LOGGING
125- VloxyLogger . Warn < ChunkDataScheduler > ( $ "Invalid state : { state } for : { position } ") ;
126- #endif
127- break ;
128- case ChunkState . State . STREAMING :
129- _ChunkState . RemoveState ( position ) ;
130- break ;
131- case ChunkState . State . LOADED :
132- _ChunkStore . RemoveChunk ( position ) ;
133- _ChunkState . RemoveState ( position ) ;
134- break ;
135- case ChunkState . State . MESHING :
136- _ChunkStore . RemoveChunk ( position ) ;
137- _ChunkState . RemoveState ( position ) ;
138- break ;
139- case ChunkState . State . ACTIVE :
140- _ChunkStore . RemoveChunk ( position ) ;
141- _ChunkState . RemoveState ( position ) ;
142- break ;
143- default :
144- throw new ArgumentOutOfRangeException ( ) ;
145- }
146- }
147- }
120+ internal void ScheduleReclaim ( List < int3 > positions ) => _ReclaimBatches = positions ;
148121
149- internal void Schedule ( List < int3 > jobs ) {
122+ internal void ScheduleClaim ( List < int3 > jobs ) {
150123 var batch = new VloxyBatch < int3 > ( jobs . Count ) ;
151124
152125 for ( int i = 0 ; i < jobs . Count ; i ++ ) {
@@ -175,18 +148,61 @@ internal void Schedule(List<int3> jobs) {
175148 }
176149 }
177150
178- _Batches . Enqueue ( batch ) ;
151+ _ClaimBatches . Enqueue ( batch ) ;
179152
180- Processing = _Batches . Count > 0 ;
153+ CanProcess = _ClaimBatches . Count > 0 ;
181154 }
182155
183156 private void Process ( ) {
184- _CurrentBatch ??= _Batches . Dequeue ( ) ;
157+ ProcessReclaim ( ) ;
158+ ProcessClaim ( ) ;
159+ }
160+
161+ private void ProcessReclaim ( ) {
162+ if ( _ReclaimBatches . Count == 0 ) return ;
163+
164+ for ( int i = 0 ; i < _ReclaimBatches . Count ; i ++ ) {
165+ var position = _ReclaimBatches [ i ] ;
166+ var state = _ChunkState . GetState ( position ) ;
167+
168+ switch ( state ) {
169+ case ChunkState . State . UNLOADED :
170+ #if VLOXY_LOGGING
171+ VloxyLogger . Warn < ChunkDataScheduler > ( $ "Invalid state : { state } for : { position } ") ;
172+ #endif
173+ break ;
174+ case ChunkState . State . STREAMING :
175+ _ChunkState . RemoveState ( position ) ;
176+ break ;
177+ case ChunkState . State . LOADED :
178+ _ChunkStore . RemoveChunk ( position ) ;
179+ _ChunkState . RemoveState ( position ) ;
180+ break ;
181+ case ChunkState . State . MESHING :
182+ _ChunkStore . RemoveChunk ( position ) ;
183+ _ChunkState . RemoveState ( position ) ;
184+ break ;
185+ case ChunkState . State . ACTIVE :
186+ _ChunkStore . RemoveChunk ( position ) ;
187+ _ChunkState . RemoveState ( position ) ;
188+ break ;
189+ default :
190+ throw new ArgumentOutOfRangeException ( ) ;
191+ }
192+ }
193+
194+ _ReclaimBatches . Clear ( ) ;
195+ }
196+
197+ private void ProcessClaim ( ) {
198+ Processing = true ;
199+
200+ _CurrentClaimBatch ??= _ClaimBatches . Dequeue ( ) ;
185201
186202 var count = _BatchSize ;
187203
188- while ( count > 0 && _CurrentBatch . Count > 0 ) {
189- var position = _CurrentBatch . Dequeue ( ) ;
204+ while ( count > 0 && _CurrentClaimBatch . Count > 0 ) {
205+ var position = _CurrentClaimBatch . Dequeue ( ) ;
190206
191207 if ( _ChunkState . GetState ( position ) != ChunkState . State . STREAMING ) continue ;
192208
@@ -234,11 +250,13 @@ private bool Complete() {
234250 _Results . Clear ( ) ;
235251
236252 _Scheduled = false ;
237-
238- if ( _CurrentBatch . Count == 0 ) _CurrentBatch = null ;
239-
240- Processing = _Batches . Count > 0 || _CurrentBatch != null ;
241-
253+
254+ if ( _CurrentClaimBatch . Count == 0 ) {
255+ _CurrentClaimBatch = null ;
256+ Processing = false ;
257+ CanProcess = _ClaimBatches . Count > 0 ;
258+ }
259+
242260#if VLOXY_LOGGING
243261 _Watch . Stop ( ) ;
244262 Timestamp ( _Watch . ElapsedMilliseconds ) ;
0 commit comments