Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderTargetSetter.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 SiliconStudio.Core;
4 using SiliconStudio.Core.Mathematics;
5 using SiliconStudio.Paradox.Effects.Modules;
6 using SiliconStudio.Paradox.Graphics;
7 
8 namespace SiliconStudio.Paradox.Effects
9 {
10  /// <summary>
11  /// A processor that setup a <see cref="RenderTarget"/> and a <see cref="DepthStencil"/> on a <see cref="RenderPass"/>.
12  /// </summary>
14  {
15  /// <summary>
16  /// Initializes a new instance of the <see cref="Renderer" /> class.
17  /// </summary>
18  /// <param name="services">The services.</param>
20  : base(services)
21  {
22  ClearColor = Color.Black;
23  ClearDepth = 1.0f;
24  ClearStencil = 0;
25  EnableClearTarget = true;
26  EnableClearDepth = true;
27  EnableClearStencil = false;
28  EnableSetTargets = true;
29 
30  RenderTarget = GraphicsDevice.BackBuffer;
31  DepthStencil = GraphicsDevice.DepthStencilBuffer;
32  }
33 
34  /// <summary>
35  /// Gets or sets the key to get the render target from <see cref="RenderPipeline.Parameters"/>.
36  /// </summary>
37  /// <value>
38  /// The render target key.
39  /// </value>
40  public RenderTarget RenderTarget { get; set; }
41 
42  /// <summary>
43  /// Gets or sets the key to get the depth stencil from <see cref="RenderPipeline.Parameters"/>.
44  /// </summary>
45  /// <value>
46  /// The depth stencil key.
47  /// </value>
48  public DepthStencilBuffer DepthStencil { get; set; }
49 
50  /// <summary>
51  /// Gets or sets the viewport.
52  /// </summary>
53  /// <value>
54  /// The viewport.
55  /// </value>
56  public Viewport Viewport { get; set; }
57 
58  /// <summary>
59  /// Gets or sets the color used to clear the render target.
60  /// </summary>
61  /// <value>
62  /// The the color used to clear the render target.
63  /// </value>
64  public Color ClearColor { get; set; }
65 
66  /// <summary>
67  /// Gets or sets the depth value used to clear the depth stencil buffer.
68  /// </summary>
69  /// <value>
70  /// The depth value used to clear the depth stencil buffer.
71  /// </value>
72  public float ClearDepth { get; set; }
73 
74  /// <summary>
75  /// Gets or sets the stencil value used to clear the depth stencil buffer.
76  /// </summary>
77  /// <value>
78  /// The stencil value used to clear the depth stencil buffer.
79  /// </value>
80  public byte ClearStencil { get; set; }
81 
82  /// <summary>
83  /// Gets or sets a value indicating whether [enable clear render target].
84  /// </summary>
85  /// <value>
86  /// <c>true</c> if [enable clear render target]; otherwise, <c>false</c>.
87  /// </value>
88  public bool EnableClearTarget { get; set; }
89 
90  /// <summary>
91  /// Gets or sets a value indicating whether [enable clear depth].
92  /// </summary>
93  /// <value>
94  /// <c>true</c> if [enable clear depth ; otherwise, <c>false</c>.
95  /// </value>
96  public bool EnableClearDepth { get; set; }
97 
98  /// <summary>
99  /// Gets or sets a value indicating whether [enable clear stencil].
100  /// </summary>
101  /// <value>
102  /// <c>true</c> if [enable clear stencil]; otherwise, <c>false</c>.
103  /// </value>
104  public bool EnableClearStencil { get; set; }
105 
106  /// <summary>
107  /// Gets or sets a value indicating whether [enable set targets].
108  /// </summary>
109  /// <value>
110  /// <c>true</c> if [enable set targets]; otherwise, <c>false</c>.
111  /// </value>
112  public bool EnableSetTargets { get; set; }
113 
114  protected override void OnRendering(RenderContext context)
115  {
116  var graphicsDevice = context.GraphicsDevice;
117 
118  // clear the targets
119  if ((EnableClearDepth || EnableClearStencil) && DepthStencil != null)
120  {
121  var clearOptions = (DepthStencilClearOptions)0;
122  if (EnableClearDepth)
123  clearOptions |= DepthStencilClearOptions.DepthBuffer;
124  if (EnableClearStencil)
125  clearOptions |= DepthStencilClearOptions.Stencil;
126 
127  graphicsDevice.Clear(DepthStencil, clearOptions, ClearDepth, ClearStencil);
128  }
129  if (EnableClearTarget && RenderTarget != null)
130  graphicsDevice.Clear(RenderTarget, ClearColor);
131 
132  // set the view size parameter
133  var pass = context.CurrentPass;
134  var viewParameters = pass.Parameters;
135  viewParameters.Set(CameraKeys.ViewSize, new Vector2(Viewport.Width, Viewport.Height));
136 
137  // set the targets
138  if (EnableSetTargets)
139  {
140  graphicsDevice.SetRenderTarget(DepthStencil, RenderTarget);
141  var viewPort = Viewport;
142  if (viewPort != Viewport.Empty)
143  graphicsDevice.SetViewport(viewPort);
144  }
145  }
146  }
147 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
float Width
Gets or sets the width dimension of the viewport on the render-target surface, in pixels...
Definition: Viewport.cs:30
float Height
Gets or sets the height dimension of the viewport on the render-target surface, in pixels...
Definition: Viewport.cs:33
override void OnRendering(RenderContext context)
Performs render pipeline transformations attached to a specific RenderPass.
Definition: Renderer.cs:13
A service registry is a IServiceProvider that provides methods to register and unregister services...
Thread-local storage context used during rendering.
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
static readonly Viewport Empty
Empty value for an undefined viewport.
Definition: Viewport.cs:19
RenderTargetSetter(IServiceRegistry services)
Initializes a new instance of the Renderer class.
A processor that setup a RenderTarget and a DepthStencil on a RenderPass.
DepthStencilClearOptions
Specifies the buffer to use when calling Clear.