This repository has been archived by the owner on Apr 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
SchedulerSystem.cs
163 lines (146 loc) · 5.66 KB
/
SchedulerSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Diagnostics;
using Stride.Games;
using Stride.Graphics;
using Stride.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using VL.Core;
namespace VL.Stride.Engine
{
/// <summary>
/// Allows to schedule game systems (e.g. a SceneSystem or a LayerSystem) as well as single renderers.
/// </summary>
public class SchedulerSystem : GameSystemBase
{
static readonly PropertyKey<bool> contentLoaded = new PropertyKey<bool>("ContentLoaded", typeof(SchedulerSystem));
readonly List<GameSystemBase> queue = new List<GameSystemBase>();
readonly Stack<ConsecutiveRenderSystem> pool = new Stack<ConsecutiveRenderSystem>();
public SchedulerSystem([NotNull] IServiceRegistry registry) : base(registry)
{
Enabled = true;
Visible = true;
}
/// <summary>
/// Schedule a game system to be processed in this frame.
/// </summary>
/// <param name="gameSystem">The game system to schedule.</param>
public void Schedule(GameSystemBase gameSystem)
{
queue.Add(gameSystem);
}
/// <summary>
/// Schedules a renderer for rendering.
/// </summary>
/// <param name="renderer">The layer to schedule.</param>
public void Schedule(IGraphicsRendererBase renderer)
{
var current = queue.LastOrDefault() as ConsecutiveRenderSystem;
if (current is null)
{
// Fetch from pool or create new
current = pool.Count > 0 ? pool.Pop() : new ConsecutiveRenderSystem(Services);
Schedule(current);
}
current.Renderers.Add(renderer);
}
public override void Update(GameTime gameTime)
{
foreach (var system in queue)
{
if (!system.Tags.Get(contentLoaded) && system is IContentable c)
{
c.LoadContent();
system.Tags.Set(contentLoaded, true);
}
if (system.Enabled)
system.Update(gameTime);
}
}
public override void Draw(GameTime gameTime)
{
var renderContext = RenderContext.GetShared(Services);
// Reset the context
renderContext.Reset();
// Recycle temporary resources (for example textures allocated by render features through GetTemporaryTexture)
renderContext.Allocator.Recycle(r => r.AccessCountSinceLastRecycle == 0);
try
{
foreach (var system in queue)
{
if (system.Visible)
{
if (system.BeginDraw())
{
system.Draw(gameTime);
system.EndDraw();
}
}
}
}
finally
{
// Put back into the pool
foreach (var system in queue)
if (system is ConsecutiveRenderSystem c)
pool.Push(c);
queue.Clear();
}
}
sealed class ConsecutiveRenderSystem : GameSystemBase
{
private RenderView renderView;
private RenderContext renderContext;
private RenderDrawContext renderDrawContext;
public ConsecutiveRenderSystem([NotNull] IServiceRegistry registry) : base(registry)
{
Enabled = true;
Visible = true;
}
public readonly List<IGraphicsRendererBase> Renderers = new List<IGraphicsRendererBase>();
protected override void LoadContent()
{
// Default render view
renderView = new RenderView()
{
NearClipPlane = 0.05f,
FarClipPlane = 1000,
};
// Create the drawing context
var graphicsContext = Services.GetSafeServiceAs<GraphicsContext>();
renderContext = RenderContext.GetShared(Services);
renderDrawContext = new RenderDrawContext(Services, renderContext, graphicsContext);
}
public override void Draw(GameTime gameTime)
{
try
{
using (renderContext.PushRenderViewAndRestore(renderView))
using (renderDrawContext.PushRenderTargetsAndRestore())
{
// Report the exceptions but continue drawing the next renderer. Otherwise one failing renderer can cause the whole app to fail.
foreach (var renderer in Renderers)
{
try
{
renderer?.Draw(renderDrawContext);
}
catch (Exception e)
{
RuntimeGraph.ReportException(e);
}
}
}
}
finally
{
Renderers.Clear();
renderDrawContext.ResourceGroupAllocator.Flush();
renderDrawContext.QueryManager.Flush();
}
}
}
}
}