Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageEffectContext.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 
4 using System;
5 using System.Collections.Generic;
6 
7 using SiliconStudio.Core;
8 using SiliconStudio.Paradox.Graphics;
9 
10 namespace SiliconStudio.Paradox.Effects.Images
11 {
12  /// <summary>
13  /// Context for post effects.
14  /// </summary>
16  {
18  private readonly Dictionary<TextureDescription, List<TextureLink>> textureCache = new Dictionary<TextureDescription, List<TextureLink>>();
19 
20  /// <summary>
21  /// Initializes a new instance of the <see cref="ImageEffectContext" /> class.
22  /// </summary>
23  /// <param name="game">The game.</param>
24  public ImageEffectContext(Game game)
25  : this(game.Services)
26  {
27  }
28 
29  /// <summary>
30  /// Initializes a new instance of the <see cref="ImageEffectContext" /> class.
31  /// </summary>
32  /// <param name="serviceRegistry">The service registry.</param>
33  public ImageEffectContext(IServiceRegistry serviceRegistry)
34  {
35  Services = serviceRegistry;
36  Effects = serviceRegistry.GetSafeServiceAs<EffectSystem>();
37  GraphicsDevice = serviceRegistry.GetSafeServiceAs<IGraphicsDeviceService>().GraphicsDevice;
38  }
39 
40  /// <summary>
41  /// Gets the content manager.
42  /// </summary>
43  /// <value>The content manager.</value>
44  public EffectSystem Effects { get; private set; }
45 
46  /// <summary>
47  /// Gets or sets the graphics device.
48  /// </summary>
49  /// <value>The graphics device.</value>
50  public GraphicsDevice GraphicsDevice { get; private set; }
51 
52  /// <summary>
53  /// Gets the services registry.
54  /// </summary>
55  /// <value>The services registry.</value>
56  public IServiceRegistry Services { get; private set; }
57 
58  /// <summary>
59  /// Gets a <see cref="RenderTarget" /> output for the specified description.
60  /// </summary>
61  /// <returns>A new instance of <see cref="RenderTarget" /> class.</returns>
63  {
64  return GetTemporaryTexture(description).ToRenderTarget();
65  }
66 
67  /// <summary>
68  /// Gets a <see cref="RenderTarget" /> output for the specified description with a single mipmap.
69  /// </summary>
70  /// <param name="width">The width.</param>
71  /// <param name="height">The height.</param>
72  /// <param name="format">Describes the format to use.</param>
73  /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param>
74  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
75  /// <returns>A new instance of <see cref="RenderTarget" /> class.</returns>
76  /// <msdn-id>ff476521</msdn-id>
77  /// <unmanaged>HRESULT ID3D11Device::CreateTexture2D([In] const D3D11_TEXTURE2D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture2D** ppTexture2D)</unmanaged>
78  /// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short>
79  public RenderTarget GetTemporaryRenderTarget2D(int width, int height, PixelFormat format, TextureFlags flags = TextureFlags.RenderTarget | TextureFlags.ShaderResource, int arraySize = 1)
80  {
81  return GetTemporaryTexture(Texture2DBase.NewDescription(width, height, format, flags, 1, arraySize, GraphicsResourceUsage.Default)).ToRenderTarget();
82  }
83 
84  /// <summary>
85  /// Gets a <see cref="RenderTarget" /> output for the specified description.
86  /// </summary>
87  /// <param name="width">The width.</param>
88  /// <param name="height">The height.</param>
89  /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int &gt;=1 for a particular mipmap count.</param>
90  /// <param name="format">Describes the format to use.</param>
91  /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param>
92  /// <param name="arraySize">Size of the texture 2D array, default to 1.</param>
93  /// <returns>A new instance of <see cref="RenderTarget" /> class.</returns>
94  /// <msdn-id>ff476521</msdn-id>
95  /// <unmanaged>HRESULT ID3D11Device::CreateTexture2D([In] const D3D11_TEXTURE2D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture2D** ppTexture2D)</unmanaged>
96  /// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short>
97  public RenderTarget GetTemporaryRenderTarget2D(int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags flags = TextureFlags.RenderTarget | TextureFlags.ShaderResource, int arraySize = 1)
98  {
99  return GetTemporaryTexture(Texture2DBase.NewDescription(width, height, format, flags, mipCount, arraySize, GraphicsResourceUsage.Default)).ToRenderTarget();
100  }
101 
102  /// <summary>
103  /// Gets a texture for the specified description.
104  /// </summary>
105  /// <param name="description">The description.</param>
106  /// <returns>A texture</returns>
108  {
109  // For a specific description, get allocated textures
110  List<TextureLink> textureLinks = null;
111  if (!textureCache.TryGetValue(description, out textureLinks))
112  {
113  textureLinks = new List<TextureLink>();
114  textureCache.Add(description, textureLinks);
115  }
116 
117  // Find a texture available
118  foreach (var textureLink in textureLinks)
119  {
120  if (textureLink.RefCount == 0)
121  {
122  textureLink.RefCount = 1;
123  return textureLink.Texture;
124  }
125  }
126 
127  // If no texture available, then creates a new one
128  var newTexture = CreateTexture(description);
129  if (newTexture.Name == null)
130  {
131  newTexture.Name = string.Format("PostEffect{0}-{1}", Name == null ? string.Empty : string.Format("-{0}", Name), textureLinks.Count);
132  }
133 
134  // Add the texture to the allocated textures
135  // Start RefCount == 1, because we don't want this texture to be available if a post FxProcessor is calling
136  // several times this GetTemporaryTexture method.
137  var newTextureLink = new TextureLink(newTexture) { RefCount = 1 };
138  textureLinks.Add(newTextureLink);
139 
140  return newTexture;
141  }
142 
143  /// <summary>
144  /// Increments the reference to an temporary texture.
145  /// </summary>
146  /// <param name="texture"></param>
148  {
149  if (texture == null)
150  {
151  return;
152  }
153 
154  List<TextureLink> textureLinks = null;
155  if (textureCache.TryGetValue(texture.Description, out textureLinks))
156  {
157  foreach (var textureLink in textureLinks)
158  {
159  if (textureLink.Texture == texture)
160  {
161  textureLink.RefCount++;
162  return;
163  }
164  }
165  }
166  }
167 
168  /// <summary>
169  /// Decrements the reference to a temporary texture.
170  /// </summary>
171  /// <param name="texture">The texture.</param>
172  /// <exception cref="System.InvalidOperationException">Unexpected Texture RefCount < 0</exception>
173  public void ReleaseTemporaryTexture(Texture texture)
174  {
175  if (texture == null)
176  {
177  return;
178  }
179 
180  List<TextureLink> textureLinks = null;
181  if (textureCache.TryGetValue(texture.Description, out textureLinks))
182  {
183  foreach (var textureLink in textureLinks)
184  {
185  if (textureLink.Texture == texture)
186  {
187  textureLink.RefCount--;
188 
189  // If we are back to RefCount == 1, then the texture is
190  // available.
191  if (textureLink.RefCount < 0)
192  {
193  throw new InvalidOperationException("Unexpected Texture RefCount < 0");
194  }
195  return;
196  }
197  }
198  }
199  }
200 
201  /// <summary>
202  /// Creates a texture for output.
203  /// </summary>
204  /// <param name="description">The description.</param>
205  /// <returns>Texture.</returns>
206  protected virtual Texture CreateTexture(TextureDescription description)
207  {
208  return Texture.New(GraphicsDevice, description);
209  }
210 
211  protected override void Destroy()
212  {
213  foreach (var textureLinks in textureCache.Values)
214  {
215  foreach (var textureLink in textureLinks)
216  {
217  textureLink.Texture.Dispose();
218  }
219  textureLinks.Clear();
220  }
221  textureCache.Clear();
222 
223  base.Destroy();
224  }
225 
226  private class TextureLink
227  {
228  public TextureLink(Texture texture)
229  {
230  Texture = texture;
231  }
232 
233  /// <summary>
234  /// The texture
235  /// </summary>
236  public readonly Texture Texture;
237 
238  /// <summary>
239  /// The number of active reference to this texture
240  /// </summary>
241  public int RefCount;
242  }
243  }
244 }
RenderTarget GetTemporaryRenderTarget2D(TextureDescription description)
Gets a RenderTarget output for the specified description.
Service providing method to access GraphicsDevice life-cycle.
RenderTarget GetTemporaryRenderTarget2D(int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags flags=TextureFlags.RenderTarget|TextureFlags.ShaderResource, int arraySize=1)
Gets a RenderTarget output for the specified description.
void AddReferenceToTemporaryTexture(Texture texture)
Increments the reference to an temporary texture.
RenderTarget GetTemporaryRenderTarget2D(int width, int height, PixelFormat format, TextureFlags flags=TextureFlags.RenderTarget|TextureFlags.ShaderResource, int arraySize=1)
Gets a RenderTarget output for the specified description with a single mipmap.
A simple wrapper to specify number of mipmaps. Set to true to specify all mipmaps or sets an integer ...
Definition: MipMapCount.cs:43
static TextureDescription NewDescription(int width, int height, PixelFormat format, TextureFlags textureFlags, int mipCount, int arraySize, GraphicsResourceUsage usage)
ImageEffectContext(IServiceRegistry serviceRegistry)
Initializes a new instance of the ImageEffectContext class.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
Definition: DirectXTexP.h:170
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
A service registry is a IServiceProvider that provides methods to register and unregister services...
Base class for a framework component.
Main Game class system.
Definition: Game.cs:32
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.
A Common description for all textures.
HRESULT CreateTexture(_In_ ID3D11Device *pDevice, _In_reads_(nimages) const Image *srcImages, _In_ size_t nimages, _In_ const TexMetadata &metadata, _Outptr_ ID3D11Resource **ppResource)
override void Destroy()
Disposes of object resources.
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
void ReleaseTemporaryTexture(Texture texture)
Decrements the reference to a temporary texture.
Texture GetTemporaryTexture(TextureDescription description)
Gets a texture for the specified description.
readonly TextureDescription Description
Common description for the original texture.
Definition: Texture.cs:43
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
virtual Texture CreateTexture(TextureDescription description)
Creates a texture for output.
PixelFormat
Defines various types of pixel formats.
Definition: PixelFormat.cs:32
ImageEffectContext(Game game)
Initializes a new instance of the ImageEffectContext class.
Base class for texture resources.
Definition: Texture.cs:38