-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
GraphicsCompositor.cs
198 lines (169 loc) · 7.85 KB
/
GraphicsCompositor.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Collections;
using Stride.Core.Serialization;
using Stride.Core.Serialization.Contents;
using Stride.Engine;
using Stride.Graphics;
namespace Stride.Rendering.Compositing
{
[DataSerializerGlobal(typeof(ReferenceSerializer<GraphicsCompositor>), Profile = "Content")]
[ReferenceSerializer, ContentSerializer(typeof(DataContentSerializerWithReuse<GraphicsCompositor>))]
[DataContract]
// Needed for indirect serialization of RenderSystem.RenderStages and RenderSystem.RenderFeatures
// TODO: we would like an attribute to specify that serializing through the interface type is fine in this case (bypass type detection)
[DataSerializerGlobal(null, typeof(FastTrackingCollection<RenderStage>))]
[DataSerializerGlobal(null, typeof(FastTrackingCollection<RootRenderFeature>))]
public class GraphicsCompositor : RendererBase
{
/// <summary>
/// A property key to get the current graphics compositor from the <see cref="RenderContext.Tags"/>.
/// </summary>
public static readonly PropertyKey<GraphicsCompositor> Current = new PropertyKey<GraphicsCompositor>("GraphicsCompositor.Current", typeof(GraphicsCompositor));
private readonly List<SceneInstance> initializedSceneInstances = new List<SceneInstance>();
/// <summary>
/// Gets the render system used with this graphics compositor.
/// </summary>
[DataMemberIgnore]
public RenderSystem RenderSystem { get; } = new RenderSystem();
/// <summary>
/// Gets the cameras used by this composition.
/// </summary>
/// <value>The cameras.</value>
/// <userdoc>The list of cameras used in the graphic pipeline</userdoc>
[DataMember(10)]
[Category]
[MemberCollection(NotNullItems = true)]
public SceneCameraSlotCollection Cameras { get; } = new SceneCameraSlotCollection();
/// <summary>
/// The list of render stages.
/// </summary>
[DataMember(20)]
[Category]
[MemberCollection(NotNullItems = true)]
public IList<RenderStage> RenderStages => RenderSystem.RenderStages;
/// <summary>
/// The list of render features.
/// </summary>
[DataMember(30)]
[Category]
[MemberCollection(NotNullItems = true)]
public IList<RootRenderFeature> RenderFeatures => RenderSystem.RenderFeatures;
/// <summary>
/// The entry point for the game compositor.
/// </summary>
public ISceneRenderer Game { get; set; }
/// <summary>
/// The entry point for a compositor that can render a single view.
/// </summary>
public ISceneRenderer SingleView { get; set; }
/// <summary>
/// The entry point for a compositor used by the scene editor.
/// </summary>
public ISceneRenderer Editor { get; set; }
/// <inheritdoc/>
protected override void InitializeCore()
{
base.InitializeCore();
RenderSystem.Initialize(Context);
}
/// <inheritdoc/>
protected override void Destroy()
{
// Dispose renderers
Game?.Dispose();
// Cleanup created visibility groups
foreach (var sceneInstance in initializedSceneInstances)
{
for (var i = 0; i < sceneInstance.VisibilityGroups.Count; i++)
{
var visibilityGroup = sceneInstance.VisibilityGroups[i];
if (visibilityGroup.RenderSystem == RenderSystem)
{
sceneInstance.VisibilityGroups.RemoveAt(i);
break;
}
}
}
RenderSystem.Dispose();
base.Destroy();
}
/// <inheritdoc/>
protected override void DrawCore(RenderDrawContext context)
{
if (Game != null)
{
// Get or create VisibilityGroup for this RenderSystem + SceneInstance
var sceneInstance = SceneInstance.GetCurrent(context.RenderContext);
VisibilityGroup visibilityGroup = null;
if (sceneInstance != null)
{
// Find if VisibilityGroup
foreach (var currentVisibilityGroup in sceneInstance.VisibilityGroups)
{
if (currentVisibilityGroup.RenderSystem == RenderSystem)
{
visibilityGroup = currentVisibilityGroup;
break;
}
}
// If first time, let's create and register it
if (visibilityGroup == null)
{
sceneInstance.VisibilityGroups.Add(visibilityGroup = new VisibilityGroup(RenderSystem));
initializedSceneInstances.Add(sceneInstance);
}
// Reset & cleanup
visibilityGroup.Reset();
}
using (context.RenderContext.PushTagAndRestore(SceneInstance.CurrentVisibilityGroup, visibilityGroup))
using (context.RenderContext.PushTagAndRestore(SceneInstance.CurrentRenderSystem, RenderSystem))
using (context.RenderContext.PushTagAndRestore(SceneCameraSlotCollection.Current, Cameras))
using (context.RenderContext.PushTagAndRestore(Current, this))
{
// Set render system
context.RenderContext.RenderSystem = RenderSystem;
context.RenderContext.VisibilityGroup = visibilityGroup;
// Set start states for viewports and output (it will be used during the Collect phase)
var renderOutputs = new RenderOutputDescription();
renderOutputs.CaptureState(context.CommandList);
context.RenderContext.RenderOutput = renderOutputs;
var viewports = new ViewportState();
viewports.CaptureState(context.CommandList);
context.RenderContext.ViewportState = viewports;
try
{
// Collect in the game graphics compositor: Setup features/stages, enumerate views and populates VisibilityGroup
Game.Collect(context.RenderContext);
// Collect in render features
RenderSystem.Collect(context.RenderContext);
// Collect visibile objects from each view (that were not properly collected previously)
if (visibilityGroup != null)
{
foreach (var view in RenderSystem.Views)
visibilityGroup.TryCollect(view);
}
// Extract
RenderSystem.Extract(context.RenderContext);
// Prepare
RenderSystem.Prepare(context);
// Draw using the game graphics compositor
Game.Draw(context);
// Flush
RenderSystem.Flush(context);
}
finally
{
// Reset render context data
RenderSystem.Reset();
}
}
}
}
}
}