Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GBufferPlugin.cs
Go to the documentation of this file.
1 // Copyright (c) 2011 Silicon Studio
2 
3 using System;
4 using System.IO;
5 
6 using SiliconStudio.Paradox.Effects.Modules;
7 using SiliconStudio.Paradox.Games;
8 using SiliconStudio.Paradox.Graphics;
9 using SiliconStudio.Core.IO;
10 using SiliconStudio.Core.Mathematics;
11 
12 namespace SiliconStudio.Paradox.Effects
13 {
14  /// <summary>
15  /// Plugin used to render to a GBuffer from a MainPlugin.
16  /// </summary>
17  /// <remarks>
18  /// This plugin depends on <see cref="MainPlugin"/> parameters.
19  /// </remarks>
21  {
22  private IGraphicsDeviceService graphicsDeviceService;
23 
24  /// <summary>
25  /// Initializes a new instance of the <see cref="GBufferPlugin"/> class.
26  /// </summary>
27  public GBufferPlugin() : this("GBuffer")
28  {
29  }
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="GBufferPlugin"/> class.
33  /// </summary>
34  /// <param name="name">The name.</param>
35  public GBufferPlugin(string name) : base(name)
36  {
37  Parameters.SetDefault(GBufferBaseKeys.GBufferTexture);
38  ClearColor = Color.Transparent;
39 
40  // Make sure that the Depth Stencil will be created with ShaderResource
41  Tags.Set(RenderTargetKeys.RequireDepthStencilShaderResource, true);
42  }
43 
44  /// <summary>
45  /// Gets or sets the main plugin this instance is attached to.
46  /// </summary>
47  /// <value>
48  /// The main plugin.
49  /// </value>
50  public MainPlugin MainPlugin { get; set; }
51 
52  public RenderTargetsPlugin MainTargetPlugin { get; set; }
53 
54  public Texture2D GBufferTexture
55  {
56  get { return Parameters.Get(GBufferBaseKeys.GBufferTexture); }
57  }
58 
59  /// <summary>
60  /// Gets or sets the depth stencil state Z read only.
61  /// </summary>
62  /// <value>
63  /// The depth stencil state Z read only.
64  /// </value>
65  internal DepthStencilState DepthStencilStateZReadOnly { get; set; }
66 
67  /// <inheritdoc/>
68  public override void Initialize()
69  {
70  base.Initialize();
71  graphicsDeviceService = Services.GetServiceAs<IGraphicsDeviceService>();
72  var graphicsDevice = graphicsDeviceService.GraphicsDevice;
73 
74 
75  Parameters.AddSources(MainPlugin.ViewParameters);
76 
77  if (OfflineCompilation)
78  return;
79 
80  // Create texture used for normal packing
81  var texture2D = Texture2D.New(GraphicsDevice, 1024, 1024, 1, PixelFormat.A8_UNorm);
82  texture2D.Name = "Renorm";
83 
84  // Load normal packing data
85  var texDataStream = VirtualFileSystem.OpenStream("/assets/effects/gbuffer/renorm.bin", VirtualFileMode.Open, VirtualFileAccess.Read);
86  var texFileLength = texDataStream.Length;
87  var texData = new byte[texFileLength];
88  texDataStream.Read(texData, 0, (int)texFileLength);
89  texture2D.SetData(graphicsDevice, texData);
90  texDataStream.Dispose();
91 
92  // Force custom depth stencil state on main pass
93  var mainDepthStencilState = MainTargetPlugin.Parameters.TryGet(EffectPlugin.DepthStencilStateKey) ?? graphicsDevice.DepthStencilStates.Default;
94  MainTargetPlugin.Parameters.Set(EffectPlugin.DepthStencilStateKey, mainDepthStencilState);
95 
96  // Use depth stencil value from MainPlugin
97  var defaultDescription = mainDepthStencilState.Description;
98  ClearDepth = MainTargetPlugin.ClearDepth;
99 
100  // Use Default ZTest for GBuffer
101  var depthStencilStateZStandard = DepthStencilState.New(GraphicsDevice, defaultDescription);
102  depthStencilStateZStandard.Name = "ZStandard";
103 
104  Parameters.Set(EffectPlugin.DepthStencilStateKey, depthStencilStateZStandard);
105 
106  Parameters.Set(GBufferKeys.NormalPack, texture2D);
107  Parameters.Set(TexturingKeys.PointSampler, graphicsDevice.SamplerStates.PointWrap);
108 
109  // MainPlugin is going to use the readonly depth stencil buffer
110  if (DepthStencilBuffer.IsReadOnlySupported(GraphicsDevice))
111  {
112  MainTargetPlugin.UseDepthStencilReadOnly = true;
113  MainTargetPlugin.Parameters.Set(RenderTargetKeys.DepthStencilSource, DepthStencil.Texture);
114  MainTargetPlugin.DepthStencilReadOnly = DepthStencil.Texture.ToDepthStencilBuffer(true);
115  }
116  else
117  {
118  RenderPass.EndPass += (context) =>
119  {
120  //context.GraphicsDevice.Copy(DepthStencil
121 
122  //DepthStencil.SynchronizeReadonly(context.GraphicsDevice)
123  };
124  }
125 
126  defaultDescription = mainDepthStencilState.Description;
127  defaultDescription.DepthBufferWriteEnable = false;
128  DepthStencilStateZReadOnly = DepthStencilState.New(GraphicsDevice,defaultDescription);
129  DepthStencilStateZReadOnly.Name = "ZReadOnly";
130 
131  // Create normal texture (that LightPlugin will use)
132  var gbufferTexture = Texture2D.New(GraphicsDevice, graphicsDevice.BackBuffer.Width, graphicsDevice.BackBuffer.Height, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
133  gbufferTexture.Name = "GBufferTexture";
134  Parameters.Set(GBufferBaseKeys.GBufferTexture, gbufferTexture);
135  RenderTarget = gbufferTexture.ToRenderTarget();
136 
137  // Set parameters for MainPlugin
138  MainTargetPlugin.Parameters.Set(GBufferBaseKeys.GBufferTexture, gbufferTexture);
139  }
140 
141  public override void ProcessModelView(RenderModelView2 effectModelView)
142  {
143  //var newEffectModelView = new EffectModelView();
144  //foreach (var mesh in effectModelView.Meshes)
145  //{
146  // var effect = Services.GetSafeServiceAs<IEffectSystemOld>().CreateEffect("GBuffer");
147  // var effectMesh = new EffectMesh(effect, mesh);
148  //}
149  }
150  }
151 }
Service providing method to access GraphicsDevice life-cycle.
Contains depth-stencil state for the device.
Plugin used to render to a GBuffer from a MainPlugin.
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.
GBufferPlugin()
Initializes a new instance of the GBufferPlugin class.
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
Level10 render pass using a depth buffer and a render target.
override void ProcessModelView(RenderModelView2 effectModelView)
GBufferPlugin(string name)
Initializes a new instance of the GBufferPlugin class.