Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GBufferRenderProcessor.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.IO;
5 using SiliconStudio.Core.Mathematics;
6 using SiliconStudio.Core.Serialization.Assets;
7 using SiliconStudio.Paradox.Graphics;
8 
9 namespace SiliconStudio.Paradox.Effects.Modules
10 {
12  {
13  private RenderTarget gbufferTextureRenderTarget;
14  private Texture2D gbufferTexture;
15  private Texture2D gbufferRenormTexture;
16  private DepthStencilBuffer depthStencilBuffer;
17 
18  private bool useNormalPack;
19 
20  public GBufferRenderProcessor(IServiceRegistry services, RenderPipeline recursivePipeline, DepthStencilBuffer depthStencilBuffer, bool normalPack)
21  : base(services, recursivePipeline)
22  {
23  this.depthStencilBuffer = depthStencilBuffer;
24  useNormalPack = normalPack;
25  }
26 
27  public Texture2D GBufferTexture
28  {
29  get { return gbufferTexture; }
30  }
31 
32  protected override void OnRendering(RenderContext context)
33  {
34  // Setup render target
35  GraphicsDevice.Clear(gbufferTextureRenderTarget, Color.Transparent);
36  GraphicsDevice.Clear(depthStencilBuffer, DepthStencilClearOptions.DepthBuffer | DepthStencilClearOptions.Stencil);
37  GraphicsDevice.SetRenderTarget(depthStencilBuffer, gbufferTextureRenderTarget);
38 
39  base.OnRendering(context);
40  }
41 
42  public override void Load()
43  {
44  base.Load();
45 
46  // Prepare GBuffer target
47  gbufferTexture = Texture2D.New(GraphicsDevice, GraphicsDevice.BackBuffer.Width,
48  GraphicsDevice.BackBuffer.Height, PixelFormat.R32G32B32A32_Float,
49  TextureFlags.RenderTarget | TextureFlags.ShaderResource);
50 
51  gbufferTextureRenderTarget = gbufferTexture.ToRenderTarget();
52 
53  if (useNormalPack)
54  {
55  var assetManager = (AssetManager)Services.GetServiceAs<IAssetManager>();
56 
57  // Load normal packing data
58  using (var texDataStream = assetManager.OpenAsStream("renorm.bin", StreamFlags.None))
59  {
60  var texFileLength = texDataStream.Length;
61  var texData = new byte[texFileLength];
62  texDataStream.Read(texData, 0, (int)texFileLength);
63 
64  gbufferRenormTexture = Texture2D.New(GraphicsDevice, 1024, 1024, PixelFormat.A8_UNorm, texData);
65  gbufferRenormTexture.Name = "Renorm";
66  }
67 
68  RecursivePipeline.Parameters.Set(GBufferKeys.NormalPack, gbufferRenormTexture);
69  }
70 
71  // Transmit main pass parameters for view
72  RecursivePipeline.Parameters.AddSources(Pass.Parameters);
73 
74  Pass.Parameters.Set(GBufferBaseKeys.GBufferTexture, gbufferTexture);
75  }
76 
77  public override void Unload()
78  {
79  base.Unload();
80 
81  // Remove parameters
82  RecursivePipeline.Parameters.Remove(GBufferKeys.NormalPack);
83  Pass.Parameters.Remove(GBufferBaseKeys.GBufferTexture);
84 
85  // Dispose GPU objects
86  Utilities.Dispose(ref gbufferTextureRenderTarget);
87  Utilities.Dispose(ref gbufferTexture);
88  Utilities.Dispose(ref gbufferRenormTexture);
89  }
90  }
91 }
override void Unload()
Unloads this instance. This method is called when a RenderPass is de-attached (directly or indirectly...
GBufferRenderProcessor(IServiceRegistry services, RenderPipeline recursivePipeline, DepthStencilBuffer depthStencilBuffer, bool normalPack)
A service registry is a IServiceProvider that provides methods to register and unregister services...
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.
This Renderer recursively render another RenderPass.
override void Load()
Loads this instance. This method is called when a RenderPass is attached (directly or indirectly) to ...
Thread-local storage context used during rendering.
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
Defines an entry point for mesh instantiation and recursive rendering.