-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPostProcessingEffects.cs
More file actions
548 lines (472 loc) · 21.8 KB
/
Copy pathPostProcessingEffects.cs
File metadata and controls
548 lines (472 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.ComponentModel;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Graphics;
using Stride.Rendering.Compositing;
using Stride.Rendering.Materials;
namespace Stride.Rendering.Images
{
/// <summary>
/// A default bundle of <see cref="ImageEffect"/>.
/// </summary>
[DataContract("PostProcessingEffects")]
[Display("Post-processing effects")]
public sealed class PostProcessingEffects : ImageEffect, IImageEffectRenderer, IPostProcessingEffects
{
private LuminanceEffect luminanceEffect;
private ColorTransformGroup colorTransformsGroup;
private ImageEffectShader rangeCompress;
private ImageEffectShader rangeDecompress;
/// <summary>
/// Initializes a new instance of the <see cref="PostProcessingEffects" /> class.
/// </summary>
/// <param name="services">The services.</param>
public PostProcessingEffects(IServiceRegistry services)
: this(RenderContext.GetShared(services))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PostProcessingEffects"/> class.
/// </summary>
public PostProcessingEffects()
{
Outline = new Outline
{
Enabled = false
};
Fog = new Fog
{
Enabled = false
};
AmbientOcclusion = new AmbientOcclusion();
LocalReflections = new LocalReflections();
DepthOfField = new DepthOfField();
luminanceEffect = new LuminanceEffect();
BrightFilter = new BrightFilter();
Bloom = new Bloom();
LightStreak = new LightStreak();
LensFlare = new LensFlare();
Antialiasing = new FXAAEffect();
rangeCompress = new ImageEffectShader("RangeCompressorShader");
rangeDecompress = new ImageEffectShader("RangeDecompressorShader");
colorTransformsGroup = new ColorTransformGroup();
}
/// <summary>
/// Initializes a new instance of the <see cref="PostProcessingEffects"/> class.
/// </summary>
/// <param name="context">The context.</param>
public PostProcessingEffects(RenderContext context)
: this()
{
Initialize(context);
}
/// <inheritdoc/>
[DataMember(-100), Display(Browsable = false)]
[NonOverridable]
public Guid Id { get; set; } = Guid.NewGuid();
/// <summary>
/// Gets the outline effect.
/// </summary>
[DataMember(6)]
[Category]
public Outline Outline { get; private set; }
/// <summary>
/// Gets the fog effect.
/// </summary>
[DataMember(7)]
[Category]
public Fog Fog { get; private set; }
/// <summary>
/// Gets the ambient occlusion effect.
/// </summary>
/// <userdoc>
/// Darkens areas where light is occluded by opaque objects, such as corners and crevices
/// </userdoc>
[DataMember(8)]
[Category]
public AmbientOcclusion AmbientOcclusion { get; private set; }
/// <summary>
/// Gets the ground truth ambient occlusion effect. Ported from XeGTAO of Intel.
/// </summary>
/// <userdoc>
/// Darkens areas where light is occluded by opaque objects, such as corners and crevices
/// </userdoc>
[DataMember(9)]
[Category]
public GroundTruthAmbientOcclusion GroundTruthAmbientOcclusion { get; private set; }
/// <summary>
/// Gets the local reflections effect.
/// </summary>
/// <value>The local reflection technique.</value>
/// <userdoc>Reflect the scene in glossy materials</userdoc>
[DataMember(10)]
[Category]
public LocalReflections LocalReflections { get; private set; }
/// <summary>
/// Gets the depth of field effect.
/// </summary>
/// <value>The depth of field.</value>
/// <userdoc>Accentuate regions of the image by blurring objects in the foreground or background</userdoc>
[DataMember(11)]
[Category]
public DepthOfField DepthOfField { get; private set; }
/// <summary>
/// Gets the bright pass-filter.
/// </summary>
/// <value>The bright filter.</value>
/// <userdoc>The bright filter isn't an effect by itself;
/// it extracts the brightest areas of the image and gives it to effects that use it (eg bloom, light streaks, lens flares).</userdoc>
[DataMember(20)]
[Category]
public BrightFilter BrightFilter { get; private set; }
/// <summary>
/// Gets the bloom effect.
/// </summary>
/// <value>The bloom.</value>
/// <userdoc>Bleed bright areas into surrounding areas</userdoc>
[DataMember(30)]
[Category]
public Bloom Bloom { get; private set; }
/// <summary>
/// Gets the light streak effect.
/// </summary>
/// <value>The light streak.</value>
/// <userdoc>Bleed bright points along streaks</userdoc>
[DataMember(40)]
[Category]
public LightStreak LightStreak { get; private set; }
/// <summary>
/// Gets the lens flare effect.
/// </summary>
/// <value>The lens flare.</value>
/// <userdoc>Simulate artifacts produced by the internal reflection or scattering of light within camera lenses</userdoc>
[DataMember(50)]
[Category]
public LensFlare LensFlare { get; private set; }
/// <summary>
/// Gets the final color transforms.
/// </summary>
/// <value>The color transforms.</value>
/// <userdoc>Perform a transformation onto the image colors</userdoc>
[DataMember(70)]
[Category]
public ColorTransformGroup ColorTransforms => colorTransformsGroup;
/// <summary>
/// Gets the antialiasing effect.
/// </summary>
/// <value>The antialiasing.</value>
/// <userdoc>Perform anti-aliasing filtering, smoothing the jagged edges of models</userdoc>
[DataMember(80)]
[Display("Type", "Antialiasing")]
public IScreenSpaceAntiAliasingEffect Antialiasing { get; set; } // TODO: Unload previous anti aliasing
/// <summary>
/// Disables all post processing effects.
/// </summary>
public void DisableAll()
{
Outline.Enabled = false;
Fog.Enabled = false;
AmbientOcclusion.Enabled = false;
LocalReflections.Enabled = false;
DepthOfField.Enabled = false;
Bloom.Enabled = false;
LightStreak.Enabled = false;
LensFlare.Enabled = false;
Antialiasing.Enabled = false;
rangeCompress.Enabled = false;
rangeDecompress.Enabled = false;
colorTransformsGroup.Enabled = false;
}
public override void Reset()
{
// TODO: Check how to reset other effects too
// Reset the luminance effect
luminanceEffect.Reset();
base.Reset();
}
protected override void InitializeCore()
{
base.InitializeCore();
Outline = ToLoadAndUnload(Outline);
Fog = ToLoadAndUnload(Fog);
AmbientOcclusion = ToLoadAndUnload(AmbientOcclusion);
LocalReflections = ToLoadAndUnload(LocalReflections);
DepthOfField = ToLoadAndUnload(DepthOfField);
luminanceEffect = ToLoadAndUnload(luminanceEffect);
BrightFilter = ToLoadAndUnload(BrightFilter);
Bloom = ToLoadAndUnload(Bloom);
LightStreak = ToLoadAndUnload(LightStreak);
LensFlare = ToLoadAndUnload(LensFlare);
//this can be null if no SSAA is selected in the editor
if (Antialiasing != null) Antialiasing = ToLoadAndUnload(Antialiasing);
rangeCompress = ToLoadAndUnload(rangeCompress);
rangeDecompress = ToLoadAndUnload(rangeDecompress);
colorTransformsGroup = ToLoadAndUnload(colorTransformsGroup);
}
public void Collect(RenderContext context)
{
}
public void Draw(RenderDrawContext drawContext, RenderOutputValidator outputValidator, Texture[] inputs, Texture inputDepthStencil, Texture outputTarget)
{
var colorIndex = outputValidator.Find<ColorTargetSemantic>();
if (colorIndex < 0)
return;
SetInput(0, inputs[colorIndex]);
SetInput(1, inputDepthStencil);
var normalsIndex = outputValidator.Find<NormalTargetSemantic>();
if (normalsIndex >= 0)
{
SetInput(2, inputs[normalsIndex]);
}
var specularRoughnessIndex = outputValidator.Find<SpecularColorRoughnessTargetSemantic>();
if (specularRoughnessIndex >= 0)
{
SetInput(3, inputs[specularRoughnessIndex]);
}
var reflectionIndex0 = outputValidator.Find<OctahedronNormalSpecularColorTargetSemantic>();
var reflectionIndex1 = outputValidator.Find<EnvironmentLightRoughnessTargetSemantic>();
if (reflectionIndex0 >= 0 && reflectionIndex1 >= 0)
{
SetInput(4, inputs[reflectionIndex0]);
SetInput(5, inputs[reflectionIndex1]);
}
var velocityIndex = outputValidator.Find<VelocityTargetSemantic>();
if (velocityIndex != -1)
{
SetInput(6, inputs[velocityIndex]);
}
SetOutput(outputTarget);
Draw(drawContext);
}
public bool RequiresVelocityBuffer => Antialiasing?.RequiresVelocityBuffer ?? false;
public bool RequiresNormalBuffer => LocalReflections.Enabled || GroundTruthAmbientOcclusion.Enabled;
public bool RequiresSpecularRoughnessBuffer => LocalReflections.Enabled;
protected override void DrawCore(RenderDrawContext context)
{
var input = GetInput(0);
var output = GetOutput(0);
if (input == null || output == null)
{
return;
}
var inputDepthTexture = GetInput(1); // Depth
// Update the parameters for this post effect
if (!Enabled)
{
if (input != output)
{
Scaler.SetInput(input);
Scaler.SetOutput(output);
Scaler.Draw(context);
}
return;
}
// If input == output, than copy the input to a temporary texture
if (input == output)
{
var newInput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
context.CommandList.Copy(input, newInput);
input = newInput;
}
var currentInput = input;
// Draw outline before AA
if (Outline.Enabled && inputDepthTexture != null)
{
// Outline
var outlineOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
Outline.SetColorDepthInput(currentInput, inputDepthTexture, context.RenderContext.RenderView.NearClipPlane, context.RenderContext.RenderView.FarClipPlane);
Outline.SetOutput(outlineOutput);
Outline.Draw(context);
currentInput = outlineOutput;
}
var fxaa = Antialiasing as FXAAEffect;
bool aaFirst = Bloom != null && Bloom.StableConvolution;
bool needAA = Antialiasing != null && Antialiasing.Enabled;
// do AA here, first. (hybrid method from Karis2013)
if (aaFirst && needAA)
{
// do AA:
if (fxaa != null)
fxaa.InputLuminanceInAlpha = true;
var bufferIndex = 1;
if (Antialiasing.RequiresDepthBuffer)
Antialiasing.SetInput(bufferIndex++, inputDepthTexture);
bool requiresVelocityBuffer = Antialiasing.RequiresVelocityBuffer;
if (requiresVelocityBuffer)
{
Antialiasing.SetInput(bufferIndex++, GetInput(6));
}
var aaSurface = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
if (Antialiasing.NeedRangeDecompress)
{
// explanation:
// The Karis method (Unreal Engine 4.1x), uses a hybrid pipeline to execute AA.
// The AA is usually done at the end of the pipeline, but we don't benefit from
// AA for the posteffects, which is a shame.
// The Karis method, executes AA at the beginning, but for AA to be correct, it must work post tonemapping,
// and even more in fact, in gamma space too. Plus, it waits for the alpha=luma to be a "perceptive luma" so also gamma space.
// in our case, working in gamma space created monstruous outlining artefacts around eggageratedely strong constrasted objects (way in hdr range).
// so AA works in linear space, but still with gamma luma, as a light tradeoff to supress artefacts.
// create a 16 bits target for FXAA:
// render range compression & perceptual luma to alpha channel:
rangeCompress.SetInput(currentInput);
rangeCompress.SetOutput(aaSurface);
rangeCompress.Draw(context);
Antialiasing.SetInput(0, aaSurface);
Antialiasing.SetOutput(currentInput);
Antialiasing.Draw(context);
// reverse tone LDR to HDR:
rangeDecompress.SetInput(currentInput);
rangeDecompress.SetOutput(aaSurface);
rangeDecompress.Draw(context);
}
else
{
Antialiasing.SetInput(0, currentInput);
Antialiasing.SetOutput(aaSurface);
Antialiasing.Draw(context);
}
currentInput = aaSurface;
}
if (AmbientOcclusion.Enabled && inputDepthTexture != null)
{
// Ambient Occlusion
var aoOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
AmbientOcclusion.SetColorDepthInput(currentInput, inputDepthTexture);
AmbientOcclusion.SetOutput(aoOutput);
AmbientOcclusion.Draw(context);
currentInput = aoOutput;
}
if (GroundTruthAmbientOcclusion.Enabled && inputDepthTexture != null)
{
var normalsBuffer = GetInput(2);
if (normalsBuffer != null)
{
// Ground Truth Ambient Occlusion
var gtaoOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
GroundTruthAmbientOcclusion.SetColorDepthNormalsInput(currentInput, inputDepthTexture, normalsBuffer);
GroundTruthAmbientOcclusion.SetOutput(gtaoOutput);
GroundTruthAmbientOcclusion.Draw(context);
currentInput = gtaoOutput;
}
}
if (LocalReflections.Enabled && inputDepthTexture != null)
{
var normalsBuffer = GetInput(2);
var specularRoughnessBuffer = GetInput(3);
if (normalsBuffer != null && specularRoughnessBuffer != null)
{
// Local reflections
var rlrOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
LocalReflections.SetInputSurfaces(currentInput, inputDepthTexture, normalsBuffer, specularRoughnessBuffer);
LocalReflections.SetOutput(rlrOutput);
LocalReflections.Draw(context);
currentInput = rlrOutput;
}
}
if (DepthOfField.Enabled && inputDepthTexture != null)
{
// DoF
var dofOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
DepthOfField.SetColorDepthInput(currentInput, inputDepthTexture);
DepthOfField.SetOutput(dofOutput);
DepthOfField.Draw(context);
currentInput = dofOutput;
}
if (Fog.Enabled && inputDepthTexture != null)
{
// Fog
var fogOutput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format);
Fog.SetColorDepthInput(currentInput, inputDepthTexture, context.RenderContext.RenderView.NearClipPlane, context.RenderContext.RenderView.FarClipPlane);
Fog.SetOutput(fogOutput);
Fog.Draw(context);
currentInput = fogOutput;
}
// Luminance pass (only if tone mapping is enabled)
// TODO: This is not super pluggable to have this kind of dependencies. Check how to improve this
var toneMap = colorTransformsGroup.Transforms.Get<ToneMap>();
if (colorTransformsGroup.Enabled && toneMap != null && toneMap.Enabled)
{
Texture luminanceTexture = null;
if (toneMap.UseLocalLuminance)
{
const int localLuminanceDownScale = 3;
// The luminance chain uses power-of-two intermediate targets, so it expects to output to one as well
var lumWidth = Math.Min(MathUtil.NextPowerOfTwo(currentInput.Size.Width), MathUtil.NextPowerOfTwo(currentInput.Size.Height));
lumWidth = Math.Max(1, lumWidth / 2);
var lumSize = new Size3(lumWidth, lumWidth, 1).Down2(localLuminanceDownScale);
luminanceTexture = NewScopedRenderTarget2D(lumSize.Width, lumSize.Height, PixelFormat.R16_Float, 1);
luminanceEffect.SetOutput(luminanceTexture);
}
luminanceEffect.EnableLocalLuminanceCalculation = toneMap.UseLocalLuminance;
luminanceEffect.SetInput(currentInput);
luminanceEffect.Draw(context);
// Set this parameter that will be used by the tone mapping
colorTransformsGroup.Parameters.Set(LuminanceEffect.LuminanceResult, new LuminanceResult(luminanceEffect.AverageLuminance, luminanceTexture));
}
if (BrightFilter.Enabled && (Bloom.Enabled || LightStreak.Enabled || LensFlare.Enabled))
{
Texture brightTexture = NewScopedRenderTarget2D(currentInput.Width, currentInput.Height, currentInput.Format, 1);
// Bright filter pass
BrightFilter.SetInput(currentInput);
BrightFilter.SetOutput(brightTexture);
BrightFilter.Draw(context);
// Bloom pass
if (Bloom.Enabled)
{
Bloom.SetInput(brightTexture);
Bloom.SetOutput(currentInput);
Bloom.Draw(context);
}
// Light streak pass
if (LightStreak.Enabled)
{
LightStreak.SetInput(brightTexture);
LightStreak.SetOutput(currentInput);
LightStreak.Draw(context);
}
// Lens flare pass
if (LensFlare.Enabled)
{
LensFlare.SetInput(brightTexture);
LensFlare.SetOutput(currentInput);
LensFlare.Draw(context);
}
}
bool aaLast = needAA && !aaFirst;
var toneOutput = aaLast ? NewScopedRenderTarget2D(input.Width, input.Height, input.Format) : output;
// When FXAA is enabled we need to detect whether the ColorTransformGroup should output the Luminance into the alpha or not
var luminanceToChannelTransform = colorTransformsGroup.PostTransforms.Get<LuminanceToChannelTransform>();
if (fxaa != null)
{
if (luminanceToChannelTransform == null)
{
luminanceToChannelTransform = new LuminanceToChannelTransform { ColorChannel = ColorChannel.A };
colorTransformsGroup.PostTransforms.Add(luminanceToChannelTransform);
}
// Only enabled when FXAA is enabled and InputLuminanceInAlpha is true
luminanceToChannelTransform.Enabled = fxaa.Enabled && fxaa.InputLuminanceInAlpha;
}
else if (luminanceToChannelTransform != null)
{
luminanceToChannelTransform.Enabled = false;
}
// Color transform group pass (tonemap, color grading)
var lastEffect = colorTransformsGroup.Enabled ? (ImageEffect)colorTransformsGroup : Scaler;
lastEffect.SetInput(currentInput);
lastEffect.SetOutput(toneOutput);
lastEffect.Draw(context);
// do AA here, last, if not already done.
if (aaLast)
{
Antialiasing.SetInput(toneOutput);
Antialiasing.SetOutput(output);
Antialiasing.Draw(context);
}
}
}
}