Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GraphicsResizeContext.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 System.Collections.Generic;
6 
7 using SiliconStudio.Paradox.Games;
8 using SiliconStudio.Core;
9 
10 namespace SiliconStudio.Paradox.Effects
11 {
12  /// <summary>
13  /// Keep track of actions to execute when GraphicsDevice is resized.
14  /// </summary>
15  public class GraphicsResizeContext
16  {
17  private List<ParameterLocation> currentParameterLocations = new List<ParameterLocation>();
18  private List<ResizeAction> resizeActions = new List<ResizeAction>();
19  private ResizeAction currentAction;
20 
21  /// <summary>
22  /// Execute the resize action and register it for later reexecution.
23  /// </summary>
24  /// <param name="action">The action.</param>
25  public void SetupResize(Action<GraphicsResizeContext> action)
26  {
27  // TODO: Unregister SetupResize callbacks
28  // Setup this action and its associated registered resources.
29  resizeActions.Add(new ResizeAction { Action = action, ParameterLocations = currentParameterLocations });
30  currentAction = resizeActions.Last();
31 
32  currentParameterLocations = new List<ParameterLocation>();
33 
34  // Execute the action
35  action(this);
36 
37  currentAction = null;
38  }
39 
40  /// <summary>
41  /// First step of the resize process: it sets to null all the resources assigned to a group through SetWithResize.
42  /// Later, EndResize() should be called to execute second step of this system.
43  /// This two steps system should help avoiding unecessary memory peaks and reduce GPU memory fragmentation.
44  /// </summary>
45  public void StartResize()
46  {
47  // Erase all the resources assigned through SetWithResize.
48  foreach (var resizeAction in resizeActions)
49  {
50  if (!resizeAction.ParameterLocations.All(x => (IReferencable)x.ParameterCollection.GetObject(x.ParameterKey) == x.Value))
51  {
52  throw new InvalidOperationException("Attempted to resize resource that changed.");
53  }
54 
55  foreach (var parameterLocation in resizeAction.ParameterLocations)
56  {
57  parameterLocation.ParameterCollection.SetObject(parameterLocation.ParameterKey, null);
58  }
59  resizeAction.ParameterLocations.Clear();
60  }
61  }
62 
63  /// <summary>
64  /// Recreate all the resizable resources.
65  /// It reexecute all the resize handlers registered through SetupResize to recreate necessary resources.
66  /// </summary>
67  public void EndResize()
68  {
69  // Reexecute the resize handler (in the order they were added)
70  foreach (var resizeAction in resizeActions)
71  {
72  currentAction = resizeAction;
73  resizeAction.Action(this);
74  }
75  currentAction = null;
76  }
77 
78  internal void SetWithResize<T>(ParameterCollection parameterCollection, ParameterKey<T> key, T resourceValue) where T : IReferencable
79  {
80  parameterCollection.Set(key, resourceValue);
81  if (currentAction == null)
82  throw new InvalidOperationException("Tried to use SetWithResize outside of a SetupResize callback.");
83 
84  currentAction.ParameterLocations.Add(new ParameterLocation { ParameterCollection = parameterCollection, ParameterKey = key, Value = resourceValue });
85  }
86 
87  private class ResizeAction
88  {
89  public Action<GraphicsResizeContext> Action;
90  public List<ParameterLocation> ParameterLocations;
91  }
92 
93  private struct ParameterLocation
94  {
95  public ParameterCollection ParameterCollection;
96  public ParameterKey ParameterKey;
97  public IReferencable Value;
98  }
99  }
100 }
Keep track of actions to execute when GraphicsDevice is resized.
Key of an effect parameter.
Definition: ParameterKey.cs:15
Base interface for all referencable objects.
Definition: IReferencable.cs:8
void StartResize()
First step of the resize process: it sets to null all the resources assigned to a group through SetWi...
Key of an gereric effect parameter.
void SetupResize(Action< GraphicsResizeContext > action)
Execute the resize action and register it for later reexecution.
void EndResize()
Recreate all the resizable resources. It reexecute all the resize handlers registered through SetupRe...
A container to handle a hierarchical collection of effect variables.