Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ResumeManager.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.Linq;
5 using SiliconStudio.Core;
6 
7 namespace SiliconStudio.Paradox.Graphics
8 {
9  public class ResumeManager
10  {
12  private bool deviceHasBeenDestroyed = false;
13  private bool deviceHasBeenPaused = false;
14 
16  {
17  GraphicsDevice = services.GetServiceAs<IGraphicsDeviceService>().GraphicsDevice;
18  }
19 
20  public void OnRender()
21  {
22  if (deviceHasBeenDestroyed)
23  {
24  // Destroy resources
25  OnDestroyed();
26 
27  // Reload items (data from HDD: texture, buffers, etc...)
28  OnReload();
29 
30  // Recreate other items
31  OnRecreate();
32  }
33  else if (deviceHasBeenPaused)
34  {
35  // Recreate items that were freed voluntarily during pause (render target, dynamic resources, etc...)
36  OnResume();
37  }
38  }
39 
40  public void Pause()
41  {
42  foreach (var resource in GraphicsDevice.Resources)
43  {
44  if (resource.OnPause())
45  resource.LifetimeState = GraphicsResourceLifetimeState.Paused;
46  }
47  }
48 
49  public void OnResume()
50  {
51  foreach (var resource in GraphicsDevice.Resources)
52  {
53  if (resource.LifetimeState == GraphicsResourceLifetimeState.Paused)
54  {
55  resource.OnResume();
56  resource.LifetimeState = GraphicsResourceLifetimeState.Active;
57  }
58  }
59  }
60 
61  public void OnRecreate()
62  {
63  // Recreate presenter
64  GraphicsDevice.Presenter.OnRecreated();
65 
66  bool wasSomethingRecreated = true;
67  bool hasDestroyedObjects = true;
68 
69  // Only continue if we made some progress, otherwise that means we reached something that could not be solved
70  // This allows for dependencies to still be handled without some complex system
71  // (we don't really care of recreation performance/complexity).
72  while (wasSomethingRecreated && hasDestroyedObjects)
73  {
74  // Let's track if something happened during this loop
75  wasSomethingRecreated = false;
76  hasDestroyedObjects = false;
77 
78  foreach (var resource in GraphicsDevice.Resources)
79  {
80  if (resource.LifetimeState == GraphicsResourceLifetimeState.Destroyed)
81  {
82  if (resource.OnRecreate())
83  {
84  wasSomethingRecreated = true;
85  resource.LifetimeState = GraphicsResourceLifetimeState.Active;
86  }
87  else
88  {
89  // Couldn't be recreated?
90  hasDestroyedObjects = true;
91  }
92  }
93  }
94  }
95 
96  if (hasDestroyedObjects)
97  {
98  // Attach the list of objects that could not be recreated to the exception.
99  var destroyedObjects = GraphicsDevice.Resources.Where(x => x.LifetimeState == GraphicsResourceLifetimeState.Destroyed).ToList();
100  throw new InvalidOperationException("Could not recreate all objects.") { Data = { { "DestroyedObjects", destroyedObjects } } };
101  }
102  }
103 
104  public void OnDestroyed()
105  {
106  // Destroy presenter first (so that its backbuffer and render target are destroyed properly before other resources)
107  GraphicsDevice.Presenter.OnDestroyed();
108 
109  foreach (var resource in GraphicsDevice.Resources)
110  {
111  resource.OnDestroyed();
112  resource.LifetimeState = GraphicsResourceLifetimeState.Destroyed;
113  }
114 
115  // Clear various graphics device internal states (input layouts, FBOs, etc...)
116  GraphicsDevice.OnDestroyed();
117  }
118 
119  public void OnReload()
120  {
121  foreach (var resource in GraphicsDevice.Resources)
122  {
123  if (resource.LifetimeState == GraphicsResourceLifetimeState.Destroyed)
124  {
125  if (resource.Reload != null)
126  {
127  resource.Reload(resource);
128  resource.LifetimeState = GraphicsResourceLifetimeState.Active;
129  }
130  }
131  }
132  }
133  }
134 }
Service providing method to access GraphicsDevice life-cycle.
A service registry is a IServiceProvider that provides methods to register and unregister services...
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
GraphicsResourceLifetimeState
Describes the lifetime state of a graphics resource.
ResumeManager(IServiceRegistry services)