Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageEffectBase.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 
5 using SiliconStudio.Core;
6 using SiliconStudio.Core.Mathematics;
7 using SiliconStudio.Core.Serialization.Assets;
8 using SiliconStudio.Paradox.Graphics;
9 
10 namespace SiliconStudio.Paradox.Effects.Images
11 {
12  /// <summary>
13  /// Post effect base class.
14  /// </summary>
15  public abstract class ImageEffectBase : ComponentBase
16  {
17  private readonly Texture[] inputTextures;
18  private int maxInputTextureIndex;
19 
20  private DepthStencilBuffer outputDepthStencilBuffer;
21 
22  private RenderTarget outputRenderTargetView;
23 
24  private RenderTarget[] outputRenderTargetViews;
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="ImageEffectBase" /> class.
28  /// </summary>
29  /// <param name="context">The context.</param>
30  /// <param name="name">The name.</param>
31  /// <exception cref="System.ArgumentNullException">context</exception>
32  protected ImageEffectBase(ImageEffectContext context, string name = null) : base(name)
33  {
34  if (context == null) throw new ArgumentNullException("context");
35 
36  Context = context;
37  GraphicsDevice = Context.GraphicsDevice;
38  Assets = context.Services.GetSafeServiceAs<AssetManager>();
39  Name = name ?? GetType().Name;
40  Enabled = true;
41  inputTextures = new Texture[128];
42  maxInputTextureIndex = -1;
43  }
44 
45  /// <summary>
46  /// Gets or sets a value indicating whether this post effect is enabled.
47  /// </summary>
48  /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
49  public bool Enabled { get; set; }
50 
51  /// <summary>
52  /// Gets the context.
53  /// </summary>
54  /// <value>The context.</value>
55  public ImageEffectContext Context { get; private set; }
56 
57  /// <summary>
58  /// Gets the <see cref="AssetManager"/>.
59  /// </summary>
60  /// <value>The content.</value>
61  protected AssetManager Assets { get; private set; }
62 
63  /// <summary>
64  /// Gets the graphics device.
65  /// </summary>
66  /// <value>The graphics device.</value>
67  protected GraphicsDevice GraphicsDevice { get; private set; }
68 
69  /// <summary>
70  /// Sets an input texture
71  /// </summary>
72  /// <param name="slot">The slot.</param>
73  /// <param name="texture">The texture.</param>
74  public void SetInput(int slot, Texture texture)
75  {
76  if (slot < 0 || slot >= inputTextures.Length)
77  throw new ArgumentOutOfRangeException("slot", "slot must be in the range [0, 128[");
78 
79  inputTextures[slot] = texture;
80  if (slot > maxInputTextureIndex)
81  {
82  maxInputTextureIndex = slot;
83  }
84  }
85 
86  /// <summary>
87  /// Resets the input textures.
88  /// </summary>
89  public void ResetInputs()
90  {
91  maxInputTextureIndex = -1;
92  Array.Clear(inputTextures, 0, inputTextures.Length);
93  }
94 
95  /// <summary>
96  /// Sets the render target output.
97  /// </summary>
98  /// <param name="view">The render target output view.</param>
99  /// <param name="depthStencilBuffer">The depth stencil buffer.</param>
100  /// <exception cref="System.ArgumentNullException">view</exception>
101  public void SetOutput(RenderTarget view, DepthStencilBuffer depthStencilBuffer = null)
102  {
103  if (view == null) throw new ArgumentNullException("view");
104 
105  SetOutputInternal(view, depthStencilBuffer);
106  }
107 
108  /// <summary>
109  /// Sets the render target outputs.
110  /// </summary>
111  /// <param name="views">The render target output views.</param>
112  public void SetOutput(params RenderTarget[] views)
113  {
114  if (views == null) throw new ArgumentNullException("views");
115 
116  SetOutputInternal(null, views);
117  }
118 
119  /// <summary>
120  /// Sets the render target outputs.
121  /// </summary>
122  /// <param name="depthStencilBuffer">The depth stencil buffer.</param>
123  /// <param name="views">The render target output views.</param>
124  /// <returns>PostEffectBase.</returns>
125  /// <exception cref="System.ArgumentNullException">views</exception>
126  public void SetOutput(DepthStencilBuffer depthStencilBuffer, params RenderTarget[] views)
127  {
128  if (views == null) throw new ArgumentNullException("views");
129 
130  SetOutputInternal(depthStencilBuffer, views);
131  }
132 
133  /// <summary>
134  /// Draws a full screen quad using iterating on each pass of this effect.
135  /// </summary>
136  public void Draw(string name = null)
137  {
138  if (!Enabled)
139  {
140  return;
141  }
142 
143  PreDrawCore(name);
144  DrawCore();
145  PostDrawCore();
146  }
147 
148  /// <summary>
149  /// Returns a <see cref="System.String" /> that represents this instance.
150  /// </summary>
151  /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
152  public override string ToString()
153  {
154  return string.Format("Effect {0}", Name);
155  }
156 
157  /// <summary>
158  /// Prepare call before <see cref="DrawCore"/>.
159  /// </summary>
160  /// <param name="name">The name.</param>
161  protected virtual void PreDrawCore(string name)
162  {
163  GraphicsDevice.BeginProfile(Color.Green, name ?? Name);
164 
165  if (outputRenderTargetView != null)
166  {
167  GraphicsDevice.SetRenderTarget(outputDepthStencilBuffer, outputRenderTargetView);
168  }
169  else if (outputRenderTargetViews != null)
170  {
171  GraphicsDevice.SetRenderTargets(outputDepthStencilBuffer, outputRenderTargetViews);
172  }
173  }
174 
175  /// <summary>
176  /// Posts call after <see cref="DrawCore"/>
177  /// </summary>
178  protected virtual void PostDrawCore()
179  {
180  GraphicsDevice.EndProfile();
181  }
182 
183  /// <summary>
184  /// Draws this post effect for a specific pass, implementation dependent.
185  /// </summary>
186  protected virtual void DrawCore()
187  {
188  }
189 
190  /// <summary>
191  /// Gets the number of input textures.
192  /// </summary>
193  /// <value>The input count.</value>
194  protected int InputCount
195  {
196  get
197  {
198  return maxInputTextureIndex + 1;
199  }
200  }
201 
202  /// <summary>
203  /// Gets an input texture by the specified index.
204  /// </summary>
205  /// <param name="index">The index.</param>
206  /// <returns>Texture.</returns>
207  /// <exception cref="System.ArgumentOutOfRangeException">index</exception>
208  protected Texture GetInput(int index)
209  {
210  if (index < 0 || index > maxInputTextureIndex)
211  {
212  throw new ArgumentOutOfRangeException("index", string.Format("Invald texture input index [{0}]. Max value is [{1}]", index, maxInputTextureIndex));
213  }
214  return inputTextures[index];
215  }
216 
217  /// <summary>
218  /// Gets a non-null input texture by the specified index.
219  /// </summary>
220  /// <param name="index">The index.</param>
221  /// <returns>Texture.</returns>
222  /// <exception cref="System.InvalidOperationException"></exception>
223  protected Texture GetSafeInput(int index)
224  {
225  var input = GetInput(index);
226  if (input == null)
227  {
228  throw new InvalidOperationException(string.Format("Expecting texture input on slot [{0}]", index));
229  }
230 
231  return input;
232  }
233 
234  /// <summary>
235  /// Gets the number of output render target.
236  /// </summary>
237  /// <value>The output count.</value>
238  protected int OutputCount
239  {
240  get
241  {
242  return outputRenderTargetView != null ? 1 : outputRenderTargetViews != null ? outputRenderTargetViews.Length : 0;
243  }
244  }
245 
246  /// <summary>
247  /// Gets an output render target for the specified index.
248  /// </summary>
249  /// <param name="index">The index.</param>
250  /// <returns>RenderTarget.</returns>
251  /// <exception cref="System.ArgumentOutOfRangeException">index</exception>
252  protected RenderTarget GetOutput(int index)
253  {
254  if (index < 0)
255  {
256  throw new ArgumentOutOfRangeException("index", string.Format("Invald texture outputindex [{0}] cannot be negative for effect [{1}]", index, Name));
257  }
258 
259  return outputRenderTargetView ?? (outputRenderTargetViews != null ? outputRenderTargetViews[index] : null);
260  }
261 
262  /// <summary>
263  /// Gets an non-null output render target for the specified index.
264  /// </summary>
265  /// <param name="index">The index.</param>
266  /// <returns>RenderTarget.</returns>
267  /// <exception cref="System.InvalidOperationException"></exception>
268  protected RenderTarget GetSafeOutput(int index)
269  {
270  var output = GetOutput(index);
271  if (output == null)
272  {
273  throw new InvalidOperationException(string.Format("Expecting texture output on slot [{0}]", index));
274  }
275 
276  return output;
277  }
278 
279  private void SetOutputInternal(RenderTarget view, DepthStencilBuffer depthStencilBuffer)
280  {
281  // TODO: Do we want to handle the output the same way we handle the input textures?
282  outputDepthStencilBuffer = depthStencilBuffer;
283  outputRenderTargetView = view;
284  outputRenderTargetViews = null;
285  }
286 
287  private void SetOutputInternal(DepthStencilBuffer depthStencilBuffer, params RenderTarget[] views)
288  {
289  // TODO: Do we want to handle the output the same way we handle the input textures?
290  outputDepthStencilBuffer = depthStencilBuffer;
291  outputRenderTargetView = null;
292  outputRenderTargetViews = views;
293  }
294  }
295 }
void SetOutput(DepthStencilBuffer depthStencilBuffer, params RenderTarget[] views)
Sets the render target outputs.
override string ToString()
Returns a System.String that represents this instance.
void Draw(string name=null)
Draws a full screen quad using iterating on each pass of this effect.
void SetOutput(RenderTarget view, DepthStencilBuffer depthStencilBuffer=null)
Sets the render target output.
RenderTarget GetOutput(int index)
Gets an output render target for the specified index.
void ResetInputs()
Resets the input textures.
Base class for a framework component.
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.
ImageEffectBase(ImageEffectContext context, string name=null)
Initializes a new instance of the ImageEffectBase class.
void SetOutput(params RenderTarget[] views)
Sets the render target outputs.
virtual void PreDrawCore(string name)
Prepare call before DrawCore.
Texture GetInput(int index)
Gets an input texture by the specified index.
Texture GetSafeInput(int index)
Gets a non-null input texture by the specified index.
RenderTarget GetSafeOutput(int index)
Gets an non-null output render target for the specified index.
virtual void PostDrawCore()
Posts call after DrawCore
void SetInput(int slot, Texture texture)
Sets an input texture
virtual void DrawCore()
Draws this post effect for a specific pass, implementation dependent.
Base class for texture resources.
Definition: Texture.cs:38