-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGroundTruthAmbientOcclusion.cs
More file actions
314 lines (260 loc) · 13.2 KB
/
Copy pathGroundTruthAmbientOcclusion.cs
File metadata and controls
314 lines (260 loc) · 13.2 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
// 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.ComponentModel;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Graphics;
namespace Stride.Rendering.Images
{
/// <summary>
/// Applies an ambient occlusion effect to a scene. Ambient occlusion is a technique which fakes occlusion for objects close to other opaque objects.
/// It takes as input a color-buffer where the scene was rendered, with its associated depth-buffer.
/// You also need to provide the camera configuration you used when rendering the scene.
/// </summary>
[DataContract("GroundTruthAmbientOcclusion")]
public class GroundTruthAmbientOcclusion : ImageEffect
{
private ImageEffectShader aoRawImageEffect;
private ImageEffectShader blurH;
private ImageEffectShader blurV;
private string nameGaussianBlurH;
private string nameGaussianBlurV;
private float[] offsetsWeights;
private ImageEffectShader aoApplyImageEffect;
public GroundTruthAmbientOcclusion()
{
//Enabled = false;
NumberOfSamples = 13;
ParamProjScale = 0.5f;
ParamIntensity = 0.2f;
ParamBias = 0.01f;
ParamRadius = 1f;
NumberOfBounces = 2;
BlurScale = 1.85f;
EdgeSharpness = 3f;
TempSize = TemporaryBufferSize.SizeFull;
}
/// <userdoc>
/// The number of pixels sampled to determine how occluded a point is. Higher values reduce noise, but affect performance.
/// Use with "Blur count to find a balance between results and performance.
/// </userdoc>
[DataMember(10)]
[DefaultValue(13)]
[DataMemberRange(1, 50, 1, 5, 0)]
[Display("Samples")]
public int NumberOfSamples { get; set; } = 13;
/// <userdoc>
/// Scales the sample radius. In most cases, 1 (no scaling) produces the most accurate result.
/// </userdoc>
[DataMember(20)]
[DefaultValue(0.5f)]
[Display("Projection scale")]
public float ParamProjScale { get; set; } = 0.5f;
/// <userdoc>
/// The strength of the darkening effect in occluded areas
/// </userdoc>
[DataMember(30)]
[DefaultValue(0.2f)]
[Display("Intensity")]
public float ParamIntensity { get; set; } = 0.2f;
/// <userdoc>
/// The angle at which Stride considers an area of geometry an occluder. At high values, only narrow joins and crevices are considered occluders.
/// </userdoc>
[DataMember(40)]
[DefaultValue(0.01f)]
[Display("Sample bias")]
public float ParamBias { get; set; } = 0.01f;
/// <userdoc>
/// Use with "projection scale" to control the radius of the occlusion effect
/// </userdoc>
[DataMember(50)]
[DefaultValue(1f)]
[Display("Sample radius")]
public float ParamRadius { get; set; } = 1f;
/// <userdoc>
/// The number of times the ambient occlusion image is blurred. Higher numbers reduce noise, but can produce artifacts.
/// </userdoc>
[DataMember(70)]
[DefaultValue(2)]
[DataMemberRange(0, 3, 1, 1, 0)]
[Display("Blur count")]
public int NumberOfBounces { get; set; } = 2;
/// <userdoc>
/// The blur radius in pixels
/// </userdoc>
[DataMember(74)]
[DefaultValue(1.85f)]
[Display("Blur radius")]
public float BlurScale { get; set; } = 1.85f;
/// <userdoc>
/// How much the blur respects the depth differences of occluded areas. Lower numbers create more blur, but might blur unwanted areas (ie beyond occluded areas).
/// </userdoc>
[DataMember(78)]
[DefaultValue(3f)]
[Display("Edge sharpness")]
public float EdgeSharpness { get; set; } = 3f;
/// <userdoc>
/// The resolution the ambient occlusion is calculated at. The result is upscaled to the game resolution.
/// Larger sizes produce better results but use more memory and affect performance.
/// </userdoc>
[DataMember(100)]
[DefaultValue(TemporaryBufferSize.SizeFull)]
[Display("Buffer size")]
public TemporaryBufferSize TempSize { get; set; } = TemporaryBufferSize.SizeFull;
protected override void InitializeCore()
{
base.InitializeCore();
aoApplyImageEffect = ToLoadAndUnload(new ImageEffectShader("ApplyAmbientOcclusionShader"));
aoRawImageEffect = ToLoadAndUnload(new ImageEffectShader("AmbientOcclusionRawAOEffect"));
aoRawImageEffect.Initialize(Context);
blurH = ToLoadAndUnload(new ImageEffectShader("AmbientOcclusionBlurEffect"));
blurV = ToLoadAndUnload(new ImageEffectShader("AmbientOcclusionBlurEffect", true));
blurH.Initialize(Context);
blurV.Initialize(Context);
// Setup Horizontal parameters
blurH.Parameters.Set(AmbientOcclusionBlurKeys.VerticalBlur, false);
blurV.Parameters.Set(AmbientOcclusionBlurKeys.VerticalBlur, true);
}
protected override void Destroy()
{
base.Destroy();
}
/// <summary>
/// Provides a color buffer and a depth buffer to apply the depth-of-field to.
/// </summary>
/// <param name="colorBuffer">A color buffer to process.</param>
/// <param name="depthBuffer">The depth buffer corresponding to the color buffer provided.</param>
public void SetColorDepthNormalsInput(Texture colorBuffer, Texture depthBuffer, Texture normalsBuffer)
{
SetInput(0, colorBuffer);
SetInput(1, depthBuffer);
SetInput(2, normalsBuffer);
}
protected override void DrawCore(RenderDrawContext context)
{
var originalColorBuffer = GetSafeInput(0);
var originalDepthBuffer = GetSafeInput(1);
var outputTexture = GetSafeOutput(0);
var renderView = context.RenderContext.RenderView;
//---------------------------------
// Ambient Occlusion
//---------------------------------
var tempWidth = (originalColorBuffer.Width * (int)TempSize) / (int)TemporaryBufferSize.SizeFull;
var tempHeight = (originalColorBuffer.Height * (int)TempSize) / (int)TemporaryBufferSize.SizeFull;
var aoTexture1 = NewScopedRenderTarget2D(tempWidth, tempHeight, PixelFormat.R8_UNorm, 1);
var aoTexture2 = NewScopedRenderTarget2D(tempWidth, tempHeight, PixelFormat.R8_UNorm, 1);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOKeys.Count, NumberOfSamples > 0 ? NumberOfSamples : 9);
// check whether the projection matrix is orthographic
var isOrthographic = renderView.Projection.M44 == 1;
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOKeys.IsOrthographic, isOrthographic);
blurH.Parameters.Set(AmbientOcclusionBlurKeys.IsOrthographic, isOrthographic);
blurV.Parameters.Set(AmbientOcclusionBlurKeys.IsOrthographic, isOrthographic);
Vector2 zProj;
if (isOrthographic)
{
zProj = new Vector2(renderView.NearClipPlane, renderView.FarClipPlane - renderView.NearClipPlane);
}
else
{
zProj = CameraKeys.ZProjectionACalculate(renderView.NearClipPlane, renderView.FarClipPlane);
}
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
aoRawImageEffect.Parameters.Set(CameraKeys.ZProjection, ref zProj);
Vector4 screenSize = new Vector4(originalColorBuffer.Width, originalColorBuffer.Height, 0, 0);
screenSize.Z = screenSize.X / screenSize.Y;
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ScreenInfo, screenSize);
Vector4 projInfo;
if (isOrthographic)
{
// The orthographic scale to map the xy coordinates
float scaleX = 1 / renderView.Projection.M11;
float scaleY = 1 / renderView.Projection.M22;
// Constant factor to map the ProjScale parameter to the orthographic scale
float projZScale = System.Math.Max(scaleX, scaleY) * 4;
projInfo = new Vector4(scaleX, scaleY, projZScale, 0);
}
else
{
// Projection info used to reconstruct the View space position from linear depth
var p00 = renderView.Projection.M11;
var p11 = renderView.Projection.M22;
var p02 = renderView.Projection.M13;
var p12 = renderView.Projection.M23;
projInfo = new Vector4(
-2.0f / (screenSize.X * p00),
-2.0f / (screenSize.Y * p11),
(1.0f - p02) / p00,
(1.0f + p12) / p11);
}
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ProjInfo, ref projInfo);
//**********************************
// User parameters
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamProjScale, ParamProjScale);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamIntensity, ParamIntensity);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamBias, ParamBias);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadius, ParamRadius);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadiusSquared, ParamRadius * ParamRadius);
aoRawImageEffect.SetInput(0, originalDepthBuffer);
aoRawImageEffect.SetOutput(aoTexture1);
aoRawImageEffect.Draw(context, "AmbientOcclusionRawAO");
for (int bounces = 0; bounces < NumberOfBounces; bounces++)
{
if (offsetsWeights == null)
{
offsetsWeights = new[]
{
// 0.356642f, 0.239400f, 0.072410f, 0.009869f,
// 0.398943f, 0.241971f, 0.053991f, 0.004432f, 0.000134f, // stddev = 1.0
0.153170f, 0.144893f, 0.122649f, 0.092902f, 0.062970f, // stddev = 2.0
// 0.111220f, 0.107798f, 0.098151f, 0.083953f, 0.067458f, 0.050920f, 0.036108f, // stddev = 3.0
};
nameGaussianBlurH = string.Format("AmbientOcclusionBlurH{0}x{0}", offsetsWeights.Length);
nameGaussianBlurV = string.Format("AmbientOcclusionBlurV{0}x{0}", offsetsWeights.Length);
}
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
blurH.Parameters.Set(CameraKeys.ZProjection, ref zProj);
blurV.Parameters.Set(CameraKeys.ZProjection, ref zProj);
// Update permutation parameters
blurH.Parameters.Set(AmbientOcclusionBlurKeys.Count, offsetsWeights.Length);
blurH.Parameters.Set(AmbientOcclusionBlurKeys.BlurScale, BlurScale);
blurH.Parameters.Set(AmbientOcclusionBlurKeys.EdgeSharpness, EdgeSharpness);
blurH.EffectInstance.UpdateEffect(context.GraphicsDevice);
blurV.Parameters.Set(AmbientOcclusionBlurKeys.Count, offsetsWeights.Length);
blurV.Parameters.Set(AmbientOcclusionBlurKeys.BlurScale, BlurScale);
blurV.Parameters.Set(AmbientOcclusionBlurKeys.EdgeSharpness, EdgeSharpness);
blurV.EffectInstance.UpdateEffect(context.GraphicsDevice);
// Update parameters
blurH.Parameters.Set(AmbientOcclusionBlurShaderKeys.Weights, offsetsWeights);
blurV.Parameters.Set(AmbientOcclusionBlurShaderKeys.Weights, offsetsWeights);
// Horizontal pass
blurH.SetInput(0, aoTexture1);
blurH.SetInput(1, originalDepthBuffer);
blurH.SetOutput(aoTexture2);
blurH.Draw(context, nameGaussianBlurH);
// Vertical pass
blurV.SetInput(0, aoTexture2);
blurV.SetInput(1, originalDepthBuffer);
blurV.SetOutput(aoTexture1);
blurV.Draw(context, nameGaussianBlurV);
}
aoApplyImageEffect.SetInput(0, originalColorBuffer);
aoApplyImageEffect.SetInput(1, aoTexture1);
aoApplyImageEffect.SetOutput(outputTexture);
aoApplyImageEffect.Draw(context, "AmbientOcclusionApply");
}
public enum TemporaryBufferSize
{
[Display("Full size")]
SizeFull = 12,
[Display("5/6 size")]
Size1012 = 10,
[Display("3/4 size")]
Size0912 = 9,
[Display("2/3 size")]
Size0812 = 8,
[Display("1/2 size")]
Size0612 = 6,
}
}
}