Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderSystem.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using System.Threading.Tasks;
7 using SiliconStudio.Core.Diagnostics;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.Games;
10 using SiliconStudio.Core;
11 using SiliconStudio.Core.Collections;
12 
13 namespace SiliconStudio.Paradox.Effects
14 {
15  /// <summary>
16  /// Renders its <see cref="RenderSystem.Pipeline"/>, which will usually result in drawing all meshes, UI, etc...
17  /// </summary>
19  {
20  private static readonly Logger Log = GlobalLogger.GetLogger("RenderSystem");
21 
22  private RenderContext drawContext;
23  private TrackingHashSet<RenderPipeline> pipelines = new TrackingHashSet<RenderPipeline>();
24 
25  internal readonly List<SpriteRenderer> SpriteRenderProcessors = new List<SpriteRenderer>();
26 
27  public RenderSystem(IServiceRegistry registry)
28  : base(registry)
29  {
30  pipelines.CollectionChanged += Pipelines_CollectionChanged;
31 
32  // Register both implem and interface
33  Services.AddService(typeof(RenderSystem), this);
34 
35  // Create default pipeline
36  Pipeline = new RenderPipeline("Main");
37 
38  // Register default pipeline
39  pipelines.Add(Pipeline);
40 
41  Visible = true;
42  }
43 
44  /// <inheritdoc/>
45  protected override void LoadContent()
46  {
47  base.LoadContent();
48 
49  // Create the drawing context
50  drawContext = new RenderContext(GraphicsDevice);
51  }
52 
53  /// <summary>
54  /// Gets the root pipeline, used as entry point for rendering.
55  /// </summary>
56  /// <value>
57  /// The pipeline.
58  /// </value>
59  public RenderPipeline Pipeline { get; private set; }
60 
61  /// <summary>
62  /// Gets all the existing registered pipelines.
63  /// </summary>
64  /// <value>
65  /// The registered pipelines.
66  /// </value>
67  public ISet<RenderPipeline> Pipelines
68  {
69  get { return pipelines; }
70  }
71 
72  /// <inheritdoc/>
73  public override void Draw(GameTime gameTime)
74  {
75  base.Draw(gameTime);
76 
77  // TODO should we clear drawing context parameter collection at each frame?
78 
79  try
80  {
81  GraphicsDevice.Begin();
82 
83  GraphicsDevice.ClearState();
84 
85  if (GraphicsDevice.IsProfilingSupported)
86  {
87  GraphicsDevice.EnableProfile(true);
88  }
89 
90  // Draw recursively the Pipeline
91  Draw(Pipeline, drawContext);
92  }
93  catch (Exception ex)
94  {
95  Log.Error("An exception occured while rendering", ex);
96  }
97  finally
98  {
99  GraphicsDevice.End();
100  }
101  }
102 
103  /// <inheritdoc/>
104  public void Draw(RenderPass pass, RenderContext context)
105  {
106  context.CurrentPass = pass;
107 
108  if (pass.Name != null)
109  {
110  context.GraphicsDevice.BeginProfile(Color.Green, pass.Name);
111  }
112 
113  pass.StartPass.Invoke(context);
114 
115  foreach (var child in pass.Children)
116  {
117  Draw(child, context);
118  }
119 
120  context.CurrentPass = pass;
121  pass.EndPass.Invoke(context);
122 
123  if (pass.Name != null)
124  {
125  context.GraphicsDevice.EndProfile();
126  }
127  }
128 
129  private void RenderPassAdded(RenderPass renderPass)
130  {
131  foreach (var child in renderPass.Children)
132  {
133  RenderPassAdded(child);
134  }
135  renderPass.Children.CollectionChanged += Pipelines_CollectionChanged;
136 
137  foreach (var processor in renderPass.Renderers)
138  {
139  processor.Load();
140  }
141  renderPass.Renderers.CollectionChanged += Processors_CollectionChanged;
142  }
143 
144  private void RenderPassRemoved(RenderPass renderPass)
145  {
146  foreach (var child in renderPass.Children)
147  {
148  RenderPassRemoved(child);
149  }
150  renderPass.Children.CollectionChanged -= Pipelines_CollectionChanged;
151 
152  foreach (var processor in renderPass.Renderers)
153  {
154  processor.Unload();
155  }
156  renderPass.Renderers.CollectionChanged -= Processors_CollectionChanged;
157  }
158 
159  private void Pipelines_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
160  {
161  var renderPass = (RenderPass)e.Item;
162  switch (e.Action)
163  {
164  case NotifyCollectionChangedAction.Add:
165  RenderPassAdded(renderPass);
166  break;
167  case NotifyCollectionChangedAction.Remove:
168  RenderPassRemoved(renderPass);
169  break;
170  }
171  }
172 
173  void Processors_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
174  {
175  var renderer = (Renderer)e.Item;
176  switch (e.Action)
177  {
178  case NotifyCollectionChangedAction.Add:
179  renderer.Load();
180  break;
181  case NotifyCollectionChangedAction.Remove:
182  renderer.Unload();
183  break;
184  }
185  }
186  }
187 }
void Draw(RenderPass pass, RenderContext context)
TrackingCollection< RenderPass > Children
Gets the sub render passes.
Definition: RenderPass.cs:159
RenderSystem(IServiceRegistry registry)
Definition: RenderSystem.cs:27
override void Draw(GameTime gameTime)
Draws this instance. The current timing.
Definition: RenderSystem.cs:73
A service registry is a IServiceProvider that provides methods to register and unregister services...
Base class for a GameSystemBase component.
Base implementation for ILogger.
Definition: Logger.cs:10
switch(inFormat)
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
Thread-local storage context used during rendering.
Renders its RenderSystem.Pipeline, which will usually result in drawing all meshes, UI, etc...
Definition: RenderSystem.cs:18
object Item
Gets the added or removed item (if dictionary, value only).
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
string Name
Gets or sets the name of this component.
override void LoadContent()
Loads the assets.
Definition: RenderSystem.cs:45
RenderPass is a hierarchy that defines how to collect and render meshes.
Definition: RenderPass.cs:19
Output message to log right away.
Defines an entry point for mesh instantiation and recursive rendering.
TrackingCollection< Renderer > Renderers
Gets the Renderers attached to this renderpass.
Definition: RenderPass.cs:145