Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RenderStateSetter.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.Paradox.Graphics;
5 
6 namespace SiliconStudio.Paradox.Effects
7 {
8  /// <summary>
9  /// Sets given render state during rendering.
10  /// </summary>
11  public class RenderStateSetter : Renderer
12  {
13  public RenderStateSetter(IServiceRegistry services) : base(services)
14  {
15  }
16 
17  /// <summary>
18  /// Gets or sets the depth stencil state.
19  /// </summary>
20  /// <value>
21  /// The depth stencil state.
22  /// </value>
23  public DepthStencilState DepthStencilState { get; set; }
24 
25  /// <summary>
26  /// Gets or sets the blend state.
27  /// </summary>
28  /// <value>
29  /// The blend state.
30  /// </value>
31  public BlendState BlendState { get; set; }
32 
33  /// <summary>
34  /// Gets or sets the rasterizer state.
35  /// </summary>
36  /// <value>
37  /// The rasterizer state.
38  /// </value>
39  public RasterizerState RasterizerState { get; set; }
40 
41  /// <inheritdoc/>
42  public override void Load()
43  {
44  base.Load();
45  Pass.EndPass += EndPass;
46  }
47 
48  /// <inheritdoc/>
49  public override void Unload()
50  {
51  base.Unload();
52  Pass.EndPass -= EndPass;
53  }
54 
55  protected override void OnRendering(RenderContext context)
56  {
57  var graphicsParameters = context.GraphicsDevice.Parameters;
58 
59  // Set states
60  if (DepthStencilState != null)
61  graphicsParameters.Set(Effect.DepthStencilStateKey, DepthStencilState);
62 
63  if (BlendState != null)
64  graphicsParameters.Set(Effect.BlendStateKey, BlendState);
65 
66  if (RasterizerState != null)
67  graphicsParameters.Set(Effect.RasterizerStateKey, RasterizerState);
68  }
69 
70  private void EndPass(RenderContext context)
71  {
72  var graphicsParameters = context.GraphicsDevice.Parameters;
73 
74  // Reset states
75  if (DepthStencilState != null)
76  graphicsParameters.Reset(Effect.DepthStencilStateKey);
77 
78  if (BlendState != null)
79  graphicsParameters.Reset(Effect.BlendStateKey);
80 
81  if (RasterizerState != null)
82  graphicsParameters.Reset(Effect.RasterizerStateKey);
83  }
84  }
85 }
Contains depth-stencil state for the device.
override void Load()
Loads this instance. This method is called when a RenderPass is attached (directly or indirectly) to ...
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.
override void OnRendering(RenderContext context)
Sets given render state during rendering.
override void Unload()
Unloads this instance. This method is called when a RenderPass is de-attached (directly or indirectly...