Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderTargetsPlugin.cs
Go to the documentation of this file.
1 // Copyright (c) 2011 Silicon Studio
2 
3 using SiliconStudio.Paradox.Effects.Modules;
4 using SiliconStudio.Paradox.Games;
5 using SiliconStudio.Paradox.Graphics;
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Effects
9 {
10  /// <summary>
11  /// Level10 render pass using a depth buffer and a render target.
12  /// </summary>
14  {
15  private DelegateHolder<ThreadContext>.DelegateType startPassAction;
16  private DelegateHolder<ThreadContext>.DelegateType endPassAction;
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="RenderTargetsPlugin"/> class.
20  /// </summary>
21  public RenderTargetsPlugin() : this(null)
22  {
23  }
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref="RenderTargetsPlugin"/> class.
27  /// </summary>
28  /// <param name="name">The name.</param>
29  public RenderTargetsPlugin(string name) : base(name)
30  {
31  ClearColor = Color.Black;
32  ClearDepth = 1.0f;
33  ClearStencil = 0;
34  EnableClearTarget = true;
35  EnableClearDepth = true;
36  EnableSetTargets = true;
37  }
38 
39  /// <summary>
40  /// Gets or sets the render target.
41  /// </summary>
42  /// <value>
43  /// The render target.
44  /// </value>
45  /// <remarks>
46  /// This value is a shared parameters with the key <see cref="RenderTargetKeys.RenderTarget"/>
47  /// </remarks>
48  public virtual RenderTarget RenderTarget { get; set; }
49 
50  /// <summary>
51  /// Gets or sets the depth stencil buffer.
52  /// </summary>
53  /// <value>
54  /// The depth stencil buffer.
55  /// </value>
56  /// <remarks>
57  /// This value is a shared parameters with the key <see cref="RenderTargetKeys.DepthStencil"/>
58  /// </remarks>
59  public virtual DepthStencilBuffer DepthStencil { get; set; }
60 
61 
62  /// <summary>
63  /// Gets or sets the depth stencil buffer.
64  /// </summary>
65  /// <value>
66  /// The depth stencil buffer.
67  /// </value>
68  /// <remarks>
69  /// This value is a shared parameters with the key <see cref="RenderTargetKeys.DepthStencil"/>
70  /// </remarks>
71  public Texture2D DepthStencilTexture { get; set; }
72 
73  /// <summary>
74  /// Gets or sets the depth stencil buffer.
75  /// </summary>
76  /// <value>
77  /// The depth stencil buffer.
78  /// </value>
79  /// <remarks>
80  /// This value is a shared parameters with the key <see cref="RenderTargetKeys.DepthStencil"/>
81  /// </remarks>
82  public DepthStencilBuffer DepthStencilReadOnly { get; set; }
83 
84  /// <summary>
85  /// Gets or sets the color used to clear the render target.
86  /// </summary>
87  /// <value>
88  /// The the color used to clear the render target.
89  /// </value>
90  public Color ClearColor { get; set; }
91 
92  /// <summary>
93  /// Gets or sets the depth value used to clear the depth stencil buffer.
94  /// </summary>
95  /// <value>
96  /// The depth value used to clear the depth stencil buffer.
97  /// </value>
98  public float ClearDepth { get; set; }
99 
100  /// <summary>
101  /// Gets or sets the stencil value used to clear the depth stencil buffer.
102  /// </summary>
103  /// <value>
104  /// The stencil value used to clear the depth stencil buffer.
105  /// </value>
106  public byte ClearStencil { get; set; }
107 
108  /// <summary>
109  /// Gets or sets the viewport.
110  /// </summary>
111  /// <value>
112  /// The viewport.
113  /// </value>
114  public Viewport Viewport { get; set; }
115 
116  /// <summary>
117  /// Gets or sets a value indicating whether [enable clear render target].
118  /// </summary>
119  /// <value>
120  /// <c>true</c> if [enable clear render target]; otherwise, <c>false</c>.
121  /// </value>
122  public bool EnableClearTarget { get; set; }
123 
124  /// <summary>
125  /// Gets or sets a value indicating whether [enable clear depth stencil].
126  /// </summary>
127  /// <value>
128  /// <c>true</c> if [enable clear depth stencil]; otherwise, <c>false</c>.
129  /// </value>
130  public bool EnableClearDepth { get; set; }
131 
132  /// <summary>
133  /// Gets or sets a value indicating whether [enable set targets].
134  /// </summary>
135  /// <value>
136  /// <c>true</c> if [enable set targets]; otherwise, <c>false</c>.
137  /// </value>
138  public bool EnableSetTargets { get; set; }
139 
140  /// <summary>
141  /// Gets or sets the depth stencil state.
142  /// </summary>
144  {
145  get { return Parameters.TryGet(EffectPlugin.DepthStencilStateKey); }
146  set { Parameters.Set(EffectPlugin.DepthStencilStateKey, value); }
147  }
148 
149  /// <summary>
150  /// Gets or sets a value indicating whether [use depth stencil read only].
151  /// </summary>
152  /// <value>
153  /// <c>true</c> if [use depth stencil read only]; otherwise, <c>false</c>.
154  /// </value>
155  public bool UseDepthStencilReadOnly { get; set; }
156 
157  public override void Load()
158  {
159  base.Load();
160 
161  if (OfflineCompilation)
162  return;
163 
164  if (!Parameters.ContainsKey(EffectPlugin.DepthStencilStateKey))
166 
167  if (EnableSetTargets || EnableClearTarget || EnableClearDepth)
168  {
169  // Main pre-pass: clear render target & depth buffer, and bind them
170  // TODO: Separate prepass from per-rendercontext init
171  startPassAction = (threadContext) =>
172  {
173  if (threadContext.FirstContext)
174  {
175  if (EnableClearDepth && DepthStencil != null && !UseDepthStencilReadOnly)
176  threadContext.GraphicsDevice.Clear(DepthStencil, DepthStencilClearOptions.DepthBuffer, ClearDepth, ClearStencil);
177  if (EnableClearTarget && RenderTarget != null)
178  threadContext.GraphicsDevice.Clear(RenderTarget, ClearColor);
179  }
180 
181  if (EnableSetTargets)
182  {
183  // If the Viewport is undefined, use the render target dimension
184  var viewPort = Viewport;
185  if (viewPort == Viewport.Empty)
186  {
187  var desc = RenderTarget != null ? RenderTarget.Description : threadContext.GraphicsDevice.BackBuffer.Description;
188  viewPort = new Viewport(0, 0, desc.Width, desc.Height);
189  Viewport = viewPort;
190  }
191 
192  var depthStencil = UseDepthStencilReadOnly ? DepthStencilReadOnly : DepthStencil;
193 
194  threadContext.GraphicsDevice.SetViewport(viewPort);
195  threadContext.GraphicsDevice.SetRenderTargets(depthStencil, RenderTarget);
196  }
197  };
198  RenderPass.StartPass += startPassAction;
199  }
200 
201  // Unbind depth stencil buffer and render targets
202  if (EnableSetTargets)
203  {
204  endPassAction = (threadContext) => threadContext.GraphicsDevice.UnsetRenderTargets();
205  RenderPass.EndPass += endPassAction;
206  }
207  }
208 
209  public override void Unload()
210  {
211  base.Unload();
212 
213  if (OfflineCompilation)
214  return;
215 
216  RenderPass.StartPass -= startPassAction;
217  startPassAction = null;
218 
219  RenderPass.EndPass -= endPassAction;
220  endPassAction = null;
221  }
222  }
223 }
DepthStencilStateFactory DepthStencilStates
Gets the DepthStencilStateFactory factory.
Contains depth-stencil state for the device.
readonly DepthStencilState Default
A built-in state object with default settings for using a depth stencil buffer.
RenderTargetsPlugin(string name)
Initializes a new instance of the RenderTargetsPlugin class.
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.
RenderTargetsPlugin()
Initializes a new instance of the RenderTargetsPlugin class.
Defines the window dimensions of a render-target surface onto which a 3D volume projects.
Definition: Viewport.cs:14
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
Level10 render pass using a depth buffer and a render target.
static readonly Viewport Empty
Empty value for an undefined viewport.
Definition: Viewport.cs:19