Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptColor.cs
Go to the documentation of this file.
1 using System;
2 using System.Diagnostics;
3 using System.Linq;
4 using System.Threading.Tasks;
5 
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects.Modules;
8 #if PARADOX_YEBIS
9 using Paradox.Effects.Yebis;
10 #endif
11 using SiliconStudio.Paradox.Games;
12 using SiliconStudio.Paradox;
13 using SiliconStudio.Paradox.Effects;
14 using SiliconStudio.Paradox.Graphics;
15 using SiliconStudio.Paradox.Graphics.Data;
16 using SiliconStudio.Paradox.Games.MicroThreading;
17 using SiliconStudio.Paradox.Games.Mathematics;
18 using Paradox.Framework.Shaders;
19 
20 namespace ScriptTest
21 {
22  [ParadoxScript]
23  public class ScriptColor
24  {
25  public void Dispose()
26  {
27  //depthTextureHolder.Release();
28  }
29 
30  class Sphere
31  {
32  public EffectMesh Mesh;
33  public float Phase;
34  public float Speed;
35  }
36 
37  struct LightInfo
38  {
39  public float Radius;
40  public float Phase;
41  public float Z;
42  }
43 
44 
45  public class RenderingSetup
46  {
47  public static readonly RenderingSetup Singleton = new RenderingSetup();
48 
49  public PostEffectPlugin LinearColorPlugin { get; set; }
50 
52 
53 #if PARADOX_YEBIS
54  public YebisPlugin YebisPlugin { get; set; }
55 #endif
56 
57  public PostEffectPlugin FilmicColorPlugin { get; set; }
58 
59  public PostEffectPlugin MainPlugin { get; set; }
60 
61  public void Initialize(EngineContext engineContext)
62  {
63  var renderContext = engineContext.RenderContext;
64  var rootRenderPass = renderContext.RootRenderPass;
65 
66  var linearColorPass = new RenderPass("LinearColor");
67  var reinhardColorPass = new RenderPass("ReinhardColor");
68  var yebisPass = new RenderPass("YebisColor");
69  var filmicColorPass = new RenderPass("FilmicColor");
70  var composeColorPass = new RenderPass("ComposeColor");
71 
72  rootRenderPass.AddPass(linearColorPass);
73  rootRenderPass.AddPass(reinhardColorPass);
74  rootRenderPass.AddPass(yebisPass);
75  rootRenderPass.AddPass(filmicColorPass);
76  rootRenderPass.AddPass(composeColorPass);
77 
78  LinearColorPlugin = new PostEffectPlugin("LinearColor") { RenderPass = linearColorPass };
79 
80  ReinhardColorPlugin = new PostEffectPlugin("ReinhardColor") { RenderPass = reinhardColorPass };
81 
82 #if PARADOX_YEBIS
83  YebisPlugin = new YebisPlugin("ReinhardColor") { RenderPass = yebisPass };
84 #endif
85 
86  FilmicColorPlugin = new PostEffectPlugin("FilmicColor") { RenderPass = filmicColorPass };
87 
88  MainPlugin = new PostEffectPlugin("MainColor") { RenderPass = composeColorPass };
89 
90  //MainDepthReadOnlyPlugin
91 
92  // YebisPlugin = new YebisPlugin() { RenderPass = yebisColorPass, MainDepthReadOnlyPlugin = null };
93 
94  //renderContext.Register(MainDepthReadOnlyPlugin);
95  //renderContext.Register(SkyBoxPlugin);
96  //renderContext.Register(PostEffectPlugin);
97 
98  //// Create and bind depth stencil buffer
99  //renderContext.GraphicsResizeContext.SetupResize(
100  // (resizeContext) =>
101  // {
102  // MainDepthReadOnlyPlugin.DepthStencil = renderContext.GraphicsDevice.DepthStencilBuffer.New(DepthFormat.Depth32, renderContext.Width, renderContext.Height, true, "MainDepthBuffer");
103  // // Bind render target - comment when Yebis is off
104  // MainDepthReadOnlyPlugin.RenderTarget = renderContext.GraphicsDevice.RenderTarget2D.New(renderContext.Width, renderContext.Height, PixelFormat.HalfVector4, name: "MainRenderTarget");
105 
106  // // Comment - when Yebis is on
107  // //MainDepthReadOnlyPlugin.RenderTarget = renderContext.GraphicsDevice.RenderTarget2D.New(renderContext.Width, renderContext.Height, PixelFormat.R8G8B8A8, name: "MainRenderTarget");
108  // //renderContext.GlobalPass.EndPass.AddFirst = threadContext => threadContext.GraphicsDevice.Copy(MainDepthReadOnlyPlugin.RenderTarget, engineContext.RenderContext.RenderTarget);
109  // });
110 
111  //// Yebis plugin must be initialized after creating MainDepthReadOnlyPlugin.RenderTarget
112  //renderContext.Register(YebisPlugin);
113 
114  //YebisPlugin.ToneMap.Gamma = 1.0f;
115  //YebisPlugin.ToneMap.Type = ToneMapType.Auto;
116  //YebisPlugin.ToneMap.AutoExposure.MiddleGray = 0.25f;
117  //YebisPlugin.ToneMap.AutoExposure.AdaptationSensitivity = 0.5f;
118  //YebisPlugin.ToneMap.AutoExposure.AdaptationScale = 0.8f;
119  //YebisPlugin.ToneMap.AutoExposure.AdaptationSpeedLimit = 4.0f;
120  //YebisPlugin.ToneMap.AutoExposure.DarkAdaptationSensitivity = 0.9f;
121  //YebisPlugin.ToneMap.AutoExposure.DarkAdaptationScale = 0.6f;
122  //YebisPlugin.ToneMap.AutoExposure.DarkAdaptationSpeedLimit = 4.0f;
123  //YebisPlugin.ToneMap.AutoExposure.LightDarkExposureBorder = 1.0f;
124 
125  //YebisPlugin.Glare.Enable = true;
126  //YebisPlugin.Glare.RemapFactor = 1f;
127  //YebisPlugin.Glare.Threshold = 0f;
128 
129  //YebisPlugin.Lens.Vignette.Enable = true;
130 
131  //YebisPlugin.ColorCorrection.ColorTemperature = 3500;
132  }
133  }
134 
135 
136  [ParadoxScript]
137  public static async Task Run(EngineContext engineContext)
138  {
139  var renderingSetup = new RenderingSetup();
140  renderingSetup.Initialize(engineContext);
141  var device = engineContext.RenderContext.GraphicsDevice;
142 
143  var effectMeshGroup = new RenderPassListEnumerator();
144  engineContext.RenderContext.RenderPassEnumerators.Add(effectMeshGroup);
145 
146 
147  EffectOld linearEffect = engineContext.RenderContext.BuildEffect("LinearColor")
148  .Using(new PostEffectShaderPlugin())
149  .Using(new BasicShaderPlugin(new ShaderClassSource("ComputeToneMap", "0")));
150  var linearMesh = new EffectMesh(linearEffect);
151  renderingSetup.LinearColorPlugin.RenderPass.AddPass(linearMesh.EffectMeshPasses[0].EffectPass);
152  var linearTexture = Texture2D.New(engineContext.RenderContext.GraphicsDevice, 512, 512, PixelFormat.R16G16B16A16_Float, TextureFlags.RenderTarget);
153  linearMesh.Parameters.Set(RenderTargetKeys.RenderTarget, linearTexture.ToRenderTarget());
154  effectMeshGroup.AddMesh(linearMesh);
155 
156  EffectOld reinhardColor = engineContext.RenderContext.BuildEffect("ReinhardColor")
157  .Using(new PostEffectShaderPlugin())
158  .Using(new BasicShaderPlugin(new ShaderClassSource("ComputeToneMap", "1")));
159  var reinhardMesh = new EffectMesh(reinhardColor);
160  renderingSetup.ReinhardColorPlugin.RenderPass.AddPass(reinhardMesh.EffectMeshPasses[0].EffectPass);
161  var reinhardTexture = Texture2D.New(engineContext.RenderContext.GraphicsDevice, 512, 512, PixelFormat.R16G16B16A16_Float, TextureFlags.RenderTarget);
162  reinhardMesh.Parameters.Set(RenderTargetKeys.RenderTarget, reinhardTexture.ToRenderTarget());
163  effectMeshGroup.AddMesh(reinhardMesh);
164 
165  var yebisTexture = Texture2D.New(engineContext.RenderContext.GraphicsDevice, 512, 512, PixelFormat.R8G8B8A8_UNorm, TextureFlags.RenderTarget);
166 #if PARADOX_YEBIS
167  var yebisPlugin = renderingSetup.YebisPlugin;
168 
169  yebisPlugin.RenderSource = reinhardTexture;
170  yebisPlugin.RenderTarget = yebisTexture.ToRenderTarget();
171 
172  yebisPlugin.Glare.Enable = false;
173  yebisPlugin.Lens.Vignette.Enable = false;
174  yebisPlugin.Lens.Distortion.Enable = false;
175 
176  yebisPlugin.ToneMap.Exposure = 1.0f;
177  yebisPlugin.ToneMap.Gamma = 2.2f;
178  yebisPlugin.ToneMap.Type = ToneMapType.Reinhard;
179  engineContext.RenderContext.Register(yebisPlugin);
180 #endif
181 
182  EffectOld filmicEffect = engineContext.RenderContext.BuildEffect("FilmicColor")
183  .Using(new PostEffectShaderPlugin())
184  .Using(new BasicShaderPlugin(new ShaderClassSource("ComputeToneMap", "2")));
185  var filmicMesh = new EffectMesh(filmicEffect);
186  renderingSetup.FilmicColorPlugin.RenderPass.AddPass(filmicMesh.EffectMeshPasses[0].EffectPass);
187  var filmicTexture = Texture2D.New(engineContext.RenderContext.GraphicsDevice, 512, 512, PixelFormat.R16G16B16A16_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
188  filmicTexture.Name = "FilmicTexture";
189  filmicMesh.Parameters.Set(RenderTargetKeys.RenderTarget, filmicTexture.ToRenderTarget());
190  effectMeshGroup.AddMesh(filmicMesh);
191 
192  EffectOld mainEffect = engineContext.RenderContext.BuildEffect("ComposeToneMap")
193  .Using(new PostEffectShaderPlugin())
194  .Using(new BasicShaderPlugin(new ShaderClassSource("ComposeToneMap")));
195  var mainMesh = new EffectMesh(mainEffect);
196  renderingSetup.MainPlugin.RenderPass.AddPass(mainMesh.EffectMeshPasses[0].EffectPass);
197 
198  mainMesh.Parameters.Set(TexturingKeys.Texture0, linearTexture);
199  mainMesh.Parameters.Set(TexturingKeys.Texture2, yebisTexture);
200  mainMesh.Parameters.Set(TexturingKeys.Texture3, filmicTexture);
201  mainMesh.Parameters.Set(RenderTargetKeys.RenderTarget, engineContext.RenderContext.RenderTarget);
202  effectMeshGroup.AddMesh(mainMesh);
203 
204  }
205  }
206 }
static readonly RenderingSetup Singleton
Definition: ScriptColor.cs:47
void Initialize(EngineContext engineContext)
Definition: ScriptColor.cs:61
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
RenderPass is a hierarchy that defines how to collect and render meshes.
Definition: RenderPass.cs:19
static async Task Run(EngineContext engineContext)
Definition: ScriptColor.cs:137