Skip to content

Commit 3fed126

Browse files
authored
Scheduler improvements (#75)
* android icon fix * readme update * Results native has map added * Chunk State Cleanup * version name added to release artifacts * zip command fix * Explicit State Handling * state machine update * Batch Scheduler Done
1 parent a3d452f commit 3fed126

File tree

11 files changed

+315
-155
lines changed

11 files changed

+315
-155
lines changed

.github/workflows/release.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ jobs:
108108

109109
- name: Zip artifacts
110110
run: |
111-
zip -r android-mono.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildAndroidMonoRelease/
112-
zip -r android-il2cpp.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildAndroidIL2CPPRelease/
113-
zip -r windows-mono.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildWindowsMonoRelease/
114-
zip -r windows-il2cpp.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildWindowsIL2CPPRelease/
111+
zip -r android-mono-${{ github.ref_name }}.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildAndroidMonoRelease/
112+
zip -r android-il2cpp-${{ github.ref_name }}.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildAndroidIL2CPPRelease/
113+
zip -r windows-mono-${{ github.ref_name }}.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildWindowsMonoRelease/
114+
zip -r windows-il2cpp-${{ github.ref_name }}.zip artifacts/Build-CodeBlaze.Editor.Build.Builder.BuildWindowsIL2CPPRelease/
115115
116116
- uses: "marvinpinto/action-automatic-releases@latest"
117117
with:
118118
repo_token: "${{ secrets.GITHUB_TOKEN }}"
119119
prerelease: false
120120
files: |
121-
android-mono.zip
122-
android-il2cpp.zip
123-
windows-mono.zip
124-
windows-il2cpp.zip
121+
android-mono-${{ github.ref_name }}.zip
122+
android-il2cpp-${{ github.ref_name }}.zip
123+
windows-mono-${{ github.ref_name }}.zip
124+
windows-il2cpp-${{ github.ref_name }}.zip

Assets/Scripts/Vloxy/Engine/Data/ChunkManager.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public ChunkManager(VloxySettings settings) {
4848
_Reclaim.Clear();
4949
_Claim.Clear();
5050

51-
Update(_Claim, newFocusChunkCoord, diff, _ChunkSettings.LoadDistance, ChunkState.State.STREAMING);
52-
Update(_Reclaim, focusChunkCoord, -diff, _ChunkSettings.LoadDistance, ChunkState.State.UNLOADED);
51+
Update(_Claim, newFocusChunkCoord, diff, _ChunkSettings.LoadDistance);
52+
Update(_Reclaim, focusChunkCoord, -diff, _ChunkSettings.LoadDistance);
5353

5454
#if VLOXY_LOGGING
5555
VloxyLogger.Info<ChunkManager>($"Data Claim : {_Claim.Count()}, Data Reclaim : {_Reclaim.Count}");
@@ -65,8 +65,8 @@ public ChunkManager(VloxySettings settings) {
6565
_Claim.Clear();
6666

6767
if (!initial.AndReduce()) {
68-
Update(_Claim, newFocusChunkCoord, diff, _ChunkSettings.DrawDistance, ChunkState.State.MESHING);
69-
Update(_Reclaim, focusChunkCoord, -diff, _ChunkSettings.DrawDistance, ChunkState.State.LOADED);
68+
Update(_Claim, newFocusChunkCoord, diff, _ChunkSettings.DrawDistance);
69+
Update(_Reclaim, focusChunkCoord, -diff, _ChunkSettings.DrawDistance);
7070
} else {
7171
InitialViewRegion(newFocusChunkCoord);
7272
}
@@ -107,51 +107,32 @@ private void InitialViewRegion(int3 focus) {
107107
for (int x = -_ChunkSettings.DrawDistance; x <= _ChunkSettings.DrawDistance; x++) {
108108
for (int z = -_ChunkSettings.DrawDistance; z <= _ChunkSettings.DrawDistance; z++) {
109109
for (int y = -_ChunkSettings.DrawDistance; y <= _ChunkSettings.DrawDistance; y++) {
110-
Add(_Claim, focus + new int3(x, y, z) * _ChunkSettings.ChunkSize, ChunkState.State.MESHING);
110+
_Claim.Add(focus + new int3(x, y, z) * _ChunkSettings.ChunkSize);
111111
}
112112
}
113113
}
114114
}
115115

116-
private void Update(ISet<int3> set, int3 focus, int3 diff, int distance, ChunkState.State state) {
116+
private void Update(ISet<int3> set, int3 focus, int3 diff, int distance) {
117117
var size = _ChunkSettings.ChunkSize;
118118

119119
for (int i = -distance; i <= distance; i++) {
120120
for (int j = -distance; j <= distance; j++) {
121121
if (diff.x != 0) {
122-
Add(set, new int3(focus + new int3(diff.x * distance, i * size.y, j * size.z)), state);
122+
set.Add(new int3(focus + new int3(diff.x * distance, i * size.y, j * size.z)));
123123
}
124124

125125
if (diff.y != 0) {
126-
Add(set, new int3(focus + new int3(i * size.x, diff.y * distance, j * size.z)), state);
126+
set.Add(new int3(focus + new int3(i * size.x, diff.y * distance, j * size.z)));
127127
}
128128

129129
if (diff.z != 0) {
130-
Add(set, new int3(focus + new int3(i * size.x, j * size.y, diff.z * distance)), state);
130+
set.Add(new int3(focus + new int3(i * size.x, j * size.y, diff.z * distance)));
131131
}
132132
}
133133
}
134134
}
135135

136-
private void Add(ISet<int3> set, int3 position, ChunkState.State state) {
137-
var current = State.GetState(position);
138-
139-
switch (state) {
140-
case ChunkState.State.UNLOADED when current == ChunkState.State.LOADED:
141-
case ChunkState.State.STREAMING when current == ChunkState.State.UNLOADED:
142-
case ChunkState.State.LOADED when current == ChunkState.State.ACTIVE:
143-
case ChunkState.State.MESHING when current == ChunkState.State.LOADED:
144-
set.Add(position);
145-
State.SetState(position, state);
146-
break;
147-
default:
148-
#if VLOXY_LOGGING
149-
VloxyLogger.Warn<ChunkManager>($"Invalid Claim/Reclaim : Position : {position}, State : {state}, Current : {current}");
150-
#endif
151-
break;
152-
}
153-
}
154-
155136
}
156137

157138
}

Assets/Scripts/Vloxy/Engine/Data/ChunkState.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class ChunkState {
1111

1212
public enum State {
1313

14-
DEFAULT,
1514
UNLOADED,
1615
STREAMING,
1716
LOADED,
@@ -21,18 +20,9 @@ public enum State {
2120
}
2221

2322
private IDictionary<int3, State> _Dictionary;
24-
25-
private int3 _ChunkSize;
26-
private int _YPageSize;
27-
private int _PageSize;
2823

2924
public ChunkState(VloxySettings settings) {
30-
_ChunkSize = settings.Chunk.ChunkSize;
31-
_PageSize = settings.Chunk.LoadDistance;
32-
33-
_YPageSize = settings.Noise.Height / _ChunkSize.y / 2;
34-
35-
_Dictionary = new Dictionary<int3, State>(_PageSize.CubedSize());
25+
_Dictionary = new Dictionary<int3, State>(settings.Chunk.LoadDistance.CubedSize());
3626
}
3727

3828
public void RemoveState(int3 position) => _Dictionary.Remove(position);

Assets/Scripts/Vloxy/Engine/Jobs/Data/ChunkDataScheduler.cs

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Linq;
45

56
using CodeBlaze.Vloxy.Engine.Components;
67
using CodeBlaze.Vloxy.Engine.Data;
78
using CodeBlaze.Vloxy.Engine.Noise;
89
using CodeBlaze.Vloxy.Engine.Settings;
10+
using CodeBlaze.Vloxy.Engine.Utils.Extensions;
911
using CodeBlaze.Vloxy.Engine.Utils.Logger;
1012

1113
using Unity.Collections;
@@ -27,7 +29,10 @@ public class ChunkDataScheduler {
2729
private JobHandle _Handle;
2830

2931
private NativeList<int3> _Jobs;
30-
private Queue<int3> _Queue;
32+
private NativeParallelHashMap<int3, Chunk> _Results;
33+
private Queue<VloxyBatch<int3>> _Batches;
34+
private VloxyBatch<int3> _CurrentBatch;
35+
3136
private bool _Scheduled;
3237
private int _BatchSize;
3338

@@ -49,8 +54,10 @@ BurstFunctionPointers burstFunctionPointers
4954
_NoiseProfile = noiseProfile;
5055
_BurstFunctionPointers = burstFunctionPointers;
5156

52-
_Queue = new Queue<int3>();
57+
_Batches = new Queue<VloxyBatch<int3>>();
58+
5359
_Jobs = new NativeList<int3>(Allocator.Persistent);
60+
_Results = new NativeParallelHashMap<int3, Chunk>(settings.Chunk.LoadDistance.CubedSize(), Allocator.Persistent);
5461

5562
#if VLOXY_LOGGING
5663
_Watch = new Stopwatch();
@@ -59,7 +66,7 @@ BurstFunctionPointers burstFunctionPointers
5966
}
6067

6168
internal bool Update() {
62-
if (_Scheduled || _Queue.Count <= 0) return false;
69+
if (_Scheduled || !Processing) return false;
6370

6471
Process();
6572

@@ -69,8 +76,17 @@ internal bool Update() {
6976
internal bool LateUpdate() {
7077
return _Scheduled && Complete();
7178
}
79+
80+
internal void Dispose() {
81+
_Jobs.Dispose();
82+
_Results.Dispose();
83+
}
7284

73-
public void GenerateChunks(NativeArray<int3> jobs) {
85+
/// <summary>
86+
/// Initial Generation
87+
/// </summary>
88+
/// <param name="jobs"></param>
89+
internal void GenerateChunks(NativeArray<int3> jobs) {
7490
var job = new ChunkDataJob {
7591
Jobs = jobs,
7692
ChunkSize = _ChunkSize,
@@ -80,12 +96,12 @@ public void GenerateChunks(NativeArray<int3> jobs) {
8096
};
8197

8298
var handle = job.Schedule(jobs.Length, 4);
83-
99+
84100
handle.Complete();
85-
101+
86102
for (var index = 0; index < jobs.Length; index++) {
87103
var position = jobs[index];
88-
104+
89105
if (_ChunkState.GetState(position) == ChunkState.State.STREAMING) {
90106
_ChunkState.SetState(position, ChunkState.State.LOADED);
91107
} else { // This is unnecessary, how can we avoid this ?
@@ -94,23 +110,87 @@ public void GenerateChunks(NativeArray<int3> jobs) {
94110
#endif
95111
}
96112
}
97-
113+
98114
jobs.Dispose();
99115
}
116+
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+
}
100148

101-
public void Schedule(List<int3> jobs) {
149+
internal void Schedule(List<int3> jobs) {
150+
var batch = new VloxyBatch<int3>(jobs.Count);
151+
102152
for (int i = 0; i < jobs.Count; i++) {
103-
_Queue.Enqueue(jobs[i]);
153+
var position = jobs[i];
154+
var state = _ChunkState.GetState(position);
155+
156+
switch (state) {
157+
case ChunkState.State.UNLOADED:
158+
batch.Enqueue(position);
159+
_ChunkState.SetState(position, ChunkState.State.STREAMING);
160+
break;
161+
case ChunkState.State.STREAMING:
162+
#if VLOXY_LOGGING
163+
VloxyLogger.Warn<ChunkDataScheduler>($"Waiting streaming for : {position}");
164+
#endif
165+
break;
166+
case ChunkState.State.LOADED:
167+
case ChunkState.State.MESHING:
168+
case ChunkState.State.ACTIVE:
169+
#if VLOXY_LOGGING
170+
VloxyLogger.Warn<ChunkDataScheduler>($"Invalid state : {state} for : {position}");
171+
#endif
172+
break;
173+
default:
174+
throw new ArgumentOutOfRangeException();
175+
}
104176
}
105177

106-
Processing = _Queue.Count > 0;
178+
_Batches.Enqueue(batch);
179+
180+
Processing = _Batches.Count > 0;
107181
}
108182

109183
private void Process() {
184+
_CurrentBatch ??= _Batches.Dequeue();
185+
110186
var count = _BatchSize;
111-
112-
while (count > 0 && _Queue.Count > 0) {
113-
_Jobs.Add(_Queue.Dequeue());
187+
188+
while (count > 0 && _CurrentBatch.Count > 0) {
189+
var position = _CurrentBatch.Dequeue();
190+
191+
if (_ChunkState.GetState(position) != ChunkState.State.STREAMING) continue;
192+
193+
_Jobs.Add(position);
114194
count--;
115195
}
116196

@@ -122,7 +202,7 @@ private void Process() {
122202
Jobs = _Jobs,
123203
ChunkSize = _ChunkSize,
124204
NoiseProfile = _NoiseProfile,
125-
Results = _ChunkStore.Chunks.AsParallelWriter(),
205+
Results = _Results.AsParallelWriter(),
126206
BurstFunctionPointers = _BurstFunctionPointers,
127207
};
128208

@@ -140,6 +220,8 @@ private bool Complete() {
140220
var position = _Jobs[index];
141221

142222
if (_ChunkState.GetState(position) == ChunkState.State.STREAMING) {
223+
var chunk = _Results[position];
224+
_ChunkStore.Chunks.Add(position, chunk);
143225
_ChunkState.SetState(position, ChunkState.State.LOADED);
144226
} else { // This is unnecessary, how can we avoid this ?
145227
#if VLOXY_LOGGING
@@ -149,9 +231,13 @@ private bool Complete() {
149231
}
150232

151233
_Jobs.Clear();
234+
_Results.Clear();
235+
152236
_Scheduled = false;
153237

154-
Processing = _Queue.Count > 0;
238+
if (_CurrentBatch.Count == 0) _CurrentBatch = null;
239+
240+
Processing = _Batches.Count > 0 || _CurrentBatch != null;
155241

156242
#if VLOXY_LOGGING
157243
_Watch.Stop();
@@ -160,10 +246,6 @@ private bool Complete() {
160246
return true;
161247
}
162248

163-
public void Dispose() {
164-
_Jobs.Dispose();
165-
}
166-
167249
#if VLOXY_LOGGING
168250
public float AvgTime => (float) _Timings.Sum() / 10;
169251

0 commit comments

Comments
 (0)