-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
759 lines (668 loc) · 23.4 KB
/
Copy pathrenderer.cpp
File metadata and controls
759 lines (668 loc) · 23.4 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
#include "template.h"
#include <sstream>
#include <iomanip>
#include <string>
#include "Noise.h"
#include "ogt_vox.h"
#include "ImGuizmo.h"
#ifdef ML_DATASET
#include "DatasetExporter.h"
#endif // ML_DATASET
#include "amath.h"
Renderer::~Renderer()
{
}
void Renderer::Init()
{
ResetAccumulator();
PrecomputeKernel(scene.GetSettingsR().m_denoiser.m_kernelRadius, scene.GetSettingsR().m_denoiser.m_kernelMargin);
m_gui.Init();
Noise::Init();
Skydome::InitAvailablePaths();
sky.LoadAuto(scene.GetSettingsR().m_skydome, Skydome::paths[scene.GetSettingsR().m_skydome.m_fileIndex]);
scene.Create(this);
m_player.Init(this);
#ifdef ML_DENOISING
m_mldenoiser.Init();
#endif // ML_DENOISING
}
void Renderer::ResetAccumulator()
{
spp = 1;
fbHistoryLight.Clear();
fbHistoryAlbedo.Clear();
fbCurrentDistances.Clear();
fbCurrentSky.Clear();
// SVGF reprojection history data
fbHistoryLengthHistory.Clear();
fbHistoryLength.Clear();
}
void Renderer::SampleSkydome(float3 direction, PixelMetadata& result, int2 pixelDrift) const
{
int u = static_cast<int>(static_cast<float>(sky.width) * atan2f( direction.z, direction.x ) * INV2PI - 0.5f);
int v = static_cast<int>(static_cast<float>(sky.height) * acosf( direction.y ) * INVPI - 0.5f);
if (pixelDrift.x != 0) u = std::clamp(u + pixelDrift.x, 0, sky.width);
if (pixelDrift.y != 0) v = std::clamp(v + pixelDrift.y, 0, sky.height);
return SampleSkydomeUV(static_cast<uint>(u), static_cast<uint>(v), result);
}
void Renderer::SampleSkybox(float3 direction, PixelMetadata& result) const
{
const float absX = fabsf(direction.x);
const float absY = fabsf(direction.y);
const float absZ = fabsf(direction.z);
int faceIndex = 0;
float axis = 0, xcoord = 0, ycoord = 0;
// Determine which face of the cube we are hitting
if (absX >= absY && absX >= absZ)
{
axis = absX;
faceIndex = direction.x < 0 ? 0 : 3; // +X or -X (indices are based on files order)
xcoord = direction.x > 0 ? -direction.z : direction.z;
ycoord = -direction.y;
} else if (absY >= absX && absY >= absZ)
{
axis = absY;
faceIndex = direction.y < 0 ? 1 : 4; // +Y or -Y (indices are based on files order)
xcoord = direction.x;
ycoord = direction.y > 0 ? direction.z : -direction.z;
} else
{
axis = absZ;
faceIndex = direction.z < 0 ? 2 : 5; // +Z or -Z (indices are based on files order)
xcoord = direction.z > 0 ? direction.x : -direction.x;
ycoord = -direction.y;
}
// Map to [0, 1] range UVs
const float u = (xcoord / axis + 1.0f) * 0.5f;
const float v = (ycoord / axis + 1.0f) * 0.5f;
// Convert UV to pixel coordinates
const uint px = (uint)(u * (sky.width - 1));
const uint py = (uint)(v * (sky.height - 1));
const uint skyIdx = (px + py * sky.width) * 3;
constexpr float FAC_ALBEDO = 0.65f;
constexpr float FAC_LIGHT = 4.0f;
result.albedo = float3(sky.boxfaces[faceIndex][skyIdx], sky.boxfaces[faceIndex][skyIdx + 1], sky.boxfaces[faceIndex][skyIdx + 2]) * FAC_ALBEDO;
result.light = result.albedo * FAC_LIGHT;
result.normal = -direction;
result.sky = 1.0f;
}
void Renderer::SampleSky(float3 direction, PixelMetadata& result, float diffuse) const
{
switch (sky.type)
{
case Skydome::Type::Sphere:
{
int2 drift = 0;
if (diffuse > 0.05f)
{
drift = int2(
static_cast<int>(Noise::SimpleWhite::GetNext<float>() * 10401.f + 719), // arbitrary shifts for randomness
static_cast<int>(Noise::SimpleWhite::GetNext<float>() * 52109.3f + 132) // arbitrary shifts for randomness
);
constexpr int KERNEL_SIZE = 50;
const int scale = std::max(static_cast<int>(diffuse * KERNEL_SIZE), 1);
if (drift.x < 0) drift.x = -drift.x;
if (drift.y < 0) drift.y = -drift.y;
drift.x = drift.x % scale - scale / 2;
drift.y = drift.y % scale - scale / 2;
}
SampleSkydome(direction, result, drift);
} break;
case Skydome::Type::Box: SampleSkybox(direction, result); break;
}
}
float3 Renderer::SampleSkyDiffuseWithOcclusion(Ray ray, PixelMetadata& result, float diffuse) const
{
if (scene.GetSettingsR().m_enableSkyOcclusion)
{
if (diffuse > 0.01f)
{
ray.m_direction = amath::RandomConeOffset(ray.m_direction, diffuse);
}
if (scene.IsOccluded(ray, true))
{
result.light = 0;
result.distance = 1e30f;
result.albedo = 1;
result.sky = 0;
return result.light;
}
}
SampleSky(ray.m_direction, result, diffuse);
return result.light;
}
float3 refract(const float3& I, const float3& N, const float& ior)
{
Range<float, -1.f, 1.f> cosi = dot(I, N);
float etai = 1, etat = ior;
float3 n = N;
if (cosi < 0) { cosi = -cosi; }
else { std::swap(etai, etat); n = -N; }
const float eta = etai / etat;
const float k = 1 - eta * eta * (1 - cosi * cosi);
return k < 0 ? 0 : eta * I + (eta * cosi - sqrtf(k)) * n;
}
PixelMetadata Renderer::Transmit(Ray& ray, const Material& enterMat, const Context& context, int depth)
{
PixelMetadata result;
float3 direction = ray.m_direction;
const float3 enterNormal = ray.GetNormal();
if (enterMat.m_ior > 0)
{
direction = refract(direction, enterNormal, enterMat.m_ior);
if (dot(direction, direction) < 0.0001f)
{
result.albedo = float3(1, 0, 1);
return result; // TIR
}
}
ray = Ray(ray.IntersectionPoint(), direction);
scene.FindNearest(ray);
const float3 exitNormal = ray.GetNormal();
const Material& exitMat = scene.m_LevelMaterialManager->GetMaterialR(ray.GetMaterialIndex());
const int MAX_DEPTH = scene.GetSettingsR().m_recursionDepth.Get();
if (depth >= MAX_DEPTH)
{
return result;
}
// Apply Beer's law
Range<float, 0.f, 1.f> loss = ((MAX_DEPTH - depth) / static_cast<float>(MAX_DEPTH));
const float length = ray.m_length + 1e-4f;
const float sigma = enterMat.m_density * 10.0f;
const float attenuation = amath::FastExpNeg(sigma * length);
ray.m_weight = std::max(0.f, ray.m_weight.Get() * attenuation);
result.albedo = exitMat.m_color;
result.normal = exitNormal;
result.distance = ray.m_length;
if (ray.IsInfinite()) // ray goes out into the sky
{
SampleSky(ray.m_direction, result);
result.sky *= attenuation;
}
else if (exitMat.IsEmpty()) // ray entered air, change speed of light
{
Ray newray(ray.IntersectionPoint(), refract(ray.m_direction, exitNormal, 1.0f / exitMat.m_ior));
PixelMetadata pm = Trace(newray, context, depth + 1);
result = pm;
}
else if (exitMat.m_transparency <= EPSILON || ray.m_weight <= 0) // ray hit a diffuse object or lost all energy
{
float3 transmissionLight = scene.SampleLights(ray, true, context);
PixelMetadata skypm;
Ray normalRay(ray.IntersectionPoint(), exitNormal);
float3 skylight = SampleSkyDiffuseWithOcclusion(normalRay, skypm, 1.f - exitMat.m_transparency);
result.light = transmissionLight + skylight;
}
else // ray entered another transparent volume
{
Ray newray = ray;
PixelMetadata next = Transmit(newray, exitMat, context, depth + 1);
result.albedo = lerp(result.albedo, next.albedo, ray.m_weight);
result.light = lerp(result.light, next.light, ray.m_weight);
result.distance = result.distance + next.distance;
result.normal = lerp(result.normal, next.normal, ray.m_weight);
}
result.light *= ray.m_weight.Get();
return result;
}
PixelMetadata Tmpl8::Renderer::Reflect(Ray& ray, const Material& origmat, const Context& context, int depth)
{
PixelMetadata result;
const int MAX_DEPTH = scene.GetSettingsR().m_recursionDepth.Get();
const float3 normal = ray.GetNormal();
const float3 hitpoint = ray.IntersectionPoint();
const float3 direction = ray.m_direction;
const Range<float, 0.f, 1.f> loss = ((MAX_DEPTH - depth) / static_cast<float>(MAX_DEPTH)); // goes from 1 to 0
const float roughness = (origmat.m_roughness + 0.1f) / (loss + 0.01f);
// (fuzzy) reflection
float3 randomDirection = 2.f * float3(RandomFloat(), RandomFloat(), RandomFloat()) - float3(1.f);
if (dot(randomDirection, normal) < 0) randomDirection = -randomDirection;
const float3 nextDirection = normalize((1.f - origmat.m_reflectivity) * roughness * randomDirection + reflect(direction, normal));
Ray reflectionRay(hitpoint, nextDirection);
scene.FindNearest(reflectionRay);
if (reflectionRay.IsInfinite()) // ray goes out into the sky
{
PixelMetadata skyPixel;
SampleSky(nextDirection, skyPixel);
result.albedo = skyPixel.albedo * loss;
result.light = skyPixel.light * loss;
result.distance = reflectionRay.m_length;
//result.albedo = lerp(result.albedo, sky.albedo, origmat.m_reflectivity);
//result.light = lerp(result.light, sky.light, origmat.m_reflectivity);
//result.distance = reflectionRay.m_length;
//result.light *= loss;
return result;
}
const Material& nextMat = scene.m_LevelMaterialManager->GetMaterialR(reflectionRay.GetMaterialIndex());
result.normal = reflectionRay.GetNormal();
result.albedo = nextMat.m_color;
result.distance = reflectionRay.m_length;
float3 relfectionLight = scene.SampleLights(reflectionRay, false, context);
result.light = relfectionLight * loss;
if (depth >= MAX_DEPTH || nextMat.m_reflectivity < EPSILON)
{
return result;
}
// continue path
PixelMetadata next = Trace(reflectionRay, context, depth + 1);
result.albedo = lerp(result.albedo, next.albedo, nextMat.m_reflectivity) * loss;
result.light = lerp(result.light, next.light, nextMat.m_reflectivity) * loss;
result.distance = next.distance;
result.normal = lerp(result.normal, next.normal, nextMat.m_reflectivity);
return result;
}
PixelMetadata Renderer::Trace(Ray& ray, const Context& context, int depth)
{
PixelMetadata result;
const int MAX_DEPTH = scene.GetSettingsR().m_recursionDepth.Get();
if (depth >= MAX_DEPTH)
{
return result;
}
scene.FindNearest( ray );
if (ray.IsInfinite()) // ray goes out into the sky
{
SampleSky(ray.m_direction, result);
return result;
}
const Material& mat = scene.m_LevelMaterialManager->GetMaterialR(ray.GetMaterialIndex());
const float3 normal = ray.GetNormal();
const float3 hitpoint = ray.IntersectionPoint();
const float3 albedo = mat.m_color;
const float3 direction = ray.m_direction;
// primary light
const float3 light = scene.SampleLights(ray, false, context);
result.albedo = albedo;
result.light = light;
auto _SHALLOW = [](float a)
{
if (a < 0.5f) return powf(a, 5.f) * 16;
else return -powf(-a + 1, 5.f) * 16 + 1;
};
Range<float, 0.f, 1.f> transparency = 0;
float2 stoch = -1;
switch (scene.GetSettingsR().m_denoiser.m_type)
{
case Settings::Denoiser::Noise::WhiteNoise:
{
stoch = float2(RandomFloat(), RandomFloat());
} break;
case Settings::Denoiser::Noise::BlueNoise:
{
const int2 xy = context.XY();
stoch = Noise::Blue::GetFloat2(xy.x, xy.y);
} break;
}
stoch *= scene.GetSettingsR().m_microfacetStochasticStrength;
const float detailDistance = scene.GetSettingsR().m_detailDistanceFalloff;
stoch += std::min(ray.m_length / detailDistance, 1.f);
result.distance = ray.m_length;
result.normal = normal;
bool diffuse = true;
if (mat.m_transparency > 0 && stoch.x < mat.m_transparency)
{
Ray transmissionRay = ray;
const PixelMetadata transmission = Transmit(transmissionRay, mat, context, depth + 1);
transparency = mat.m_transparency * _SHALLOW(dot(-direction, normal));
result.albedo = lerp(result.albedo, transmission.albedo, transparency);
result.light = lerp(result.light, transmission.light, transparency);
result.distance = result.distance + transmission.distance;
result.normal = lerp(result.normal, transmission.normal, transparency);
diffuse = false;
}
if (mat.m_reflectivity > 0 && stoch.y < mat.m_reflectivity)
{
Ray reflectionRay = ray;
const PixelMetadata reflection = Reflect(reflectionRay, mat, context, depth + 1);
const float factor = mat.m_reflectivity * (1 - transparency);
result.albedo = lerp(result.albedo, reflection.albedo, factor);
result.light = lerp(result.light, reflection.light, factor);
result.distance = result.distance + reflection.distance;
result.sky = std::lerp(result.sky, reflection.sky, factor);
if (!result.IsSky())
{
result.normal = lerp(result.normal, reflection.normal, factor);
}
//diffuse = false;
}
if (diffuse)
{
PixelMetadata skyProjection;
Ray normalRay(hitpoint, normal);
result.light += SampleSkyDiffuseWithOcclusion(normalRay, skyProjection, 1.f - mat.m_reflectivity);
}
return result;
}
Context Renderer::GetContext(float u, float v, int index) const
{
return Context::Create(fbFilteredLight.At(index), u, v, frame, spp);
}
Context Renderer::GetContext(int x, int y) const
{
const int i = x + y * SCRWIDTH;
const float u = (static_cast<float>(x)) * SCR_SIZE_RECIP.x;
const float v = (static_cast<float>(y)) * SCR_SIZE_RECIP.y;
return Context::Create(fbFilteredLight.At(i), u, v, frame, spp);
}
static bool DEMO_BLUE_NOISE = false; // for debugging
// -----------------------------------------------------------
// Main application tick function - Executed every frame
// -----------------------------------------------------------
void Renderer::Tick(float deltaTime)
{
Timer timer; // high-resolution timer, see template.h
const Settings& settings = scene.GetSettingsR();
const Settings::Denoiser::Noise denoise = settings.m_denoiser.m_type;
const bool enableTAA = settings.m_enableTAA;
const float taaStrength = settings.m_taaStrength.Get();
const auto denoiserStrength = settings.m_denoiser.m_strength;
const Range<float, 1e-5f, 1.f> scale = 1.0f / spp;
bool dosample = spp <= 1 + settings.m_maxFrames;
const Settings::Denoiser::Worker denoiserWorker = settings.m_denoiser.m_worker;
const bool useSVGF = (denoiserWorker == Settings::Denoiser::Worker::SVGF);
const bool enablePhysics = settings.m_enablePhysics;
if (dosample) ++spp;
if (DEMO_BLUE_NOISE) dosample = false;
ExtraFilteringInput extraInputs
(
settings.m_lens.m_useDepthOfField,
settings.m_lens.m_focusDistance,
settings.m_lens.m_depthOfField,
denoiserStrength,
settings.m_denoiser.m_kernelRadius,
settings.m_denoiser.m_kernelMargin,
scale.Get()
);
m_isRendering = true;
if (useSVGF)
{
// with reprojection and SVGF accumulation is done via blending, no need to push
fbHistoryLight.DisableAccumulation();
fbHistoryAlbedo.DisableAccumulation();
}
else
{
fbHistoryAlbedo.EnableAccumulation();
fbHistoryAlbedo.SetScale(scale);
fbHistoryLight.EnableAccumulation();
fbHistoryLight.SetScale(scale);
}
if (dosample)
{
#pragma omp parallel for schedule(dynamic)
for (int y = 0; y < SCRHEIGHT; y++)
{
for (int x = 0; x < SCRWIDTH; x++)
{
const int i = x + y * SCRWIDTH;
float3 resultAlbedo = 1;
float3 resultLight = 1;
float2 bias = 0;
if (enableTAA) // && RandomFloat() > 0.2f
{
if (denoise == Settings::Denoiser::Noise::WhiteNoise || spp > 2)
{
bias = float2(RandomFloat(), RandomFloat());
}
else
{
bias = Noise::Blue::GetFloat2(x, y);
}
bias -= 0.5f;
}
bias *= taaStrength;
const float u = (static_cast<float>(x) + bias.x) * SCR_SIZE_RECIP.x;
const float v = (static_cast<float>(y) + bias.y) * SCR_SIZE_RECIP.y;
Ray r = camera.GetPrimaryRay(u, v);
PixelMetadata sample = Trace(r, GetContext(u, v, i));
fbCurrentAlbedo.Push(i, sample.albedo);
fbCurrentLight.Push(i, sample.light);
float distance = sample.distance;
float3 normal = sample.normal;
//bool wasSky = PixelMetadata::IsSky(fbCurrentSky.At(i));
bool nowSky = sample.IsSky();
//bool isSky = wasSky || nowSky;
bool isSky = nowSky;
fbCurrentSky.Push(i, sample.sky);
//if (!wasSky)
//{
// wasSky = PixelMetadata::IsSky(fbCurrentSky.At(i));
// //isSky = wasSky || nowSky;
//}
float angle = isSky ? 0.f : acos(dot(sample.normal, r.m_direction)) / PI;
//const float distDiff = abs(fbCurrentDistances.At(i) - distance);
//if (!isSky && distDiff < distThreshold) // adjacent surface
//{
// // blend
// angle = std::lerp(fbAngle.At(i), angle, softBlendFactor);
// normal = lerp(fbCurrentNormals.At(i), normal, softBlendFactor);
// distance = std::lerp(fbCurrentDistances.At(i), distance, softBlendFactor);
//}
if (isSky)
{
// override
distance = 1000.f;
normal = 0;
}
fbCurrentNormals.Push(i, normal);
fbCurrentAngle.Push(i, angle);
fbCurrentDistances.Push(i, distance);
fbNoisyDisplayFloat3.Push(i, fbCurrentAlbedo.At(i) * fbCurrentLight.At(i));
if (useSVGF)
{
// calculate world point of the pixel
const float3 worldPos = r.m_origin + r.m_direction * sample.distance;
// project the 3d point using previous camera transform
const float4 prevClipSpace = m_prevViewProjection * float4(worldPos, 1.0f);
// perspective division for Normalized Device Coordinates in the range [-1, 1]
const float3 prevNDC = float3(prevClipSpace) / prevClipSpace.w;
// convert NDC back to uv coordinates [0, 1]
const float2 prevUV(
prevNDC.x * 0.5f + 0.5f,
0.5f - (prevNDC.y * 0.5f)
);
const float ustable = static_cast<float>(x) * SCR_SIZE_RECIP.x;
const float vstable = static_cast<float>(y) * SCR_SIZE_RECIP.y;
float2 motionVector = prevUV - float2(ustable, vstable);
if (isSky) // no motion for sky
{
motionVector = float2(0.0f, 0.0f);
}
fbMotionVectors.Push(i, motionVector);
}
if (!useSVGF) // with reprojection and SVGF accumulation is done via blending, no need to push
{
fbHistoryLight.Push(i, sample.light);
fbHistoryNormals.Push(i, normal);
fbHistoryDistances.Push(i, distance);
fbHistoryAlbedo.Push(i, sample.albedo);
}
if (denoiserWorker == Settings::Denoiser::Worker::None)
{
auto display = settings.m_display;
const int num = display.GetPanesNum();
const int width = SCRWIDTH / num;
float3 displayColor;
for (int p = 0; p < num; p++)
{
if (x >= p * width && x < (p + 1) * width)
{
Settings::Display::Pane pane = display.GetPaneR(p);
if (pane.m_type == Settings::Display::BufferType::Display)
{
//displayColor = fbNoisyDisplayFloat3.At(i);
displayColor = fbHistoryLight.At(i) * fbHistoryAlbedo.At(i);
}
else
{
displayColor = GetBufferValueDisplay(pane, i);
}
}
}
uint displayValue = RGBAF32_to_RGBA8(float4(displayColor, 0));
screen->pixels[i] = displayValue;
}
}
}
if (useSVGF)
{
DenoiseTemporal(); // use previously collected data for SVGF
DenoiseVariance();
DenoiseATrous(extraInputs);
}
else
{
Denoise(extraInputs);
}
}
if (DEMO_BLUE_NOISE && spp == 2)
//#pragma omp parallel for schedule(dynamic)
for (int y = 0; y < SCRHEIGHT; y++)
{
// trace a primary ray for each tracedPixel on the line
for (int x = 0; x < SCRWIDTH; x++)
{
const int i = x + y * SCRWIDTH;
float3 color = float3(Noise::Blue::GetFloat3(x, y));
screen->pixels[i] = RGBF32_to_RGB8(color);
}
}
#ifdef ML_DATASET
if (settings.m_DL_Dataset.m_exportDataset)
{
auto CAPTURE = [&]()
{
DatasetExporter::FrameData framedata;
framedata.width = SCRWIDTH; framedata.height = SCRHEIGHT;
framedata.rgb = fbDisplayMirror.GetDataUnsafe();
framedata.normal = (float*)fbCurrentNormals.GetDataUnsafe();
framedata.illumination = (float*)fbHistoryLight.GetDataUnsafe();
framedata.distance = (float*)fbCurrentDistances.GetDataUnsafe();
framedata.rayAngle = (float*)fbAngle.GetDataUnsafe();
framedata.spp = spp;
framedata.frame = frame;
return framedata;
};
static constexpr int MAX_IMAGES = 600;
static int IMAGE_COUNTER = 0;
const int sppPerHD = settings.m_DL_Dataset.m_sppConvergence;
static int lowqFrame = -1;
if (frame % sppPerHD == 0)
{
lowqFrame = frame;
DatasetExporter::FrameData framedata = CAPTURE();
DatasetExporter::PushNoisy(framedata);
IMAGE_COUNTER++;
}
else if (lowqFrame != -1 && frame % sppPerHD == sppPerHD - 1 /* last before next */)
{
DatasetExporter::FrameData framedata = CAPTURE();
framedata.frame = lowqFrame; // frame index belongs to the lowq version
DatasetExporter::PushHD(framedata);
camera.Random();
scene.LoadRandomLevel();
ResetAccumulator();
IMAGE_COUNTER++;
}
if (IMAGE_COUNTER >= MAX_IMAGES)
{
exit(0);
}
}
#endif // ML_DATASET
/*{
const size_t index = std::clamp(mousePos.x, 0, SCRWIDTH - 1) + SCRWIDTH * std::clamp(mousePos.y, 0, SCRHEIGHT - 1);
const float3 lightraw = fbHistoryLight.AtRaw(index);
const float3 lightavg = fbHistoryLight.At(index);
printf("scale = %.4f, light accum raw = %.3f %.3f %.3f , light avg = %.3f %.3f %.3f\n", scale.Get(), lightraw.x, lightraw.y, lightraw.z, lightavg.x, lightavg.y, lightavg.z);
}*/
// performance report - running average - ms, MRays/s
static float avg = 10, alpha = 1;
avg = (1 - alpha) * avg + alpha * timer.elapsed() * 1000;
if (alpha > 0.05f) alpha *= 0.5f;
float fps = 1000.0f / avg, rps = (SCRWIDTH * SCRHEIGHT) / avg;
m_profileInfo.fps = fps;
m_profileInfo.ms = avg;
m_profileInfo.rps = rps;
mat4 prevView, prevProjection;
camera.GetViewMatrix(prevView.cell);
camera.GetProjectionMatrix(prevProjection.cell);
m_prevViewProjection = prevProjection.Transposed() * prevView.Transposed();
// handle user input
bool cameraMoved = false;
if (settings.m_isInGame)
{
cameraMoved = m_player.HandleInput(deltaTime);
}
else
{
cameraMoved = camera.HandleInput(deltaTime, settings.m_autoCameraRotate);
}
// TODO: ResetAccumulator when levels switch or when camera teleports/rotates too quickly.
if ((cameraMoved && !useSVGF) || enablePhysics) // physics-powered objects cannot be tracked with SVGF (yet?)
{
ResetAccumulator();
}
m_player.Tick(deltaTime);
++frame;
m_isRendering = false;
if (enablePhysics)
{
// moving spheres!
for (Sphere& sphere : scene.GetCurrentLevelRW()->m_spheres.objects)
{
sphere.Move(deltaTime, scene);
}
scene.BuildChunks();
}
if (useSVGF)
{
// for SVGF
fbHistoryLight.CopyFrom(fbCurrentLight);
fbHistoryNormals.CopyFrom(fbCurrentNormals);
fbHistoryDistances.CopyFrom(fbCurrentDistances);
fbHistoryAlbedo.CopyFrom(fbCurrentAlbedo);
fbHistoryLengthHistory.CopyFrom(fbHistoryLength);
fbHistoryMoments.CopyFrom(fbCurrentMoments); // for variance guidance
}
}
void Renderer::UI()
{
m_gui.Render();
}
float3 Renderer::GetBufferValueDisplay(const Settings::Display::Pane& pane, size_t index)
{
float3 result = 0;
switch (pane.m_type)
{
case Settings::Display::BufferType::Albedo: result = fbHistoryAlbedo.At(index); break;
case Settings::Display::BufferType::AccumLight: result = fbHistoryLight.At(index); break;
case Settings::Display::BufferType::FilterLight: result = fbFilteredLight.At(index); break;
case Settings::Display::BufferType::Normals:
{
float3 normal = fbCurrentNormals.At(index);
if (pane.m_worldSpace)
{
result = normal;
}
else
{
result = (normal + 1.f) / 2.f;
}
} break;
case Settings::Display::BufferType::Distances: result = float3(fbCurrentDistances.At(index)); break;
case Settings::Display::BufferType::Display: result = RGB8_to_RGBF32(fbDisplayMirror.At(index)); break;
case Settings::Display::BufferType::Angle: result = float3(fbCurrentAngle.At(index)); break;
case Settings::Display::BufferType::Sky: result = float3(fbCurrentSky.At(index)); break;
case Settings::Display::BufferType::Motion:
{
const float2 v = (fbMotionVectors.At(index) * 5.f + 1.f) / 2.0f;
result = float3(v.x, v.y, 0);
} break;
case Settings::Display::BufferType::HistoryLength: result = float3(fbHistoryLength.At(index)) / static_cast<float>(scene.GetSettingsR().m_maxFrames); break;
}
result = Settings::Display::Pane::ApplyContrast(result, pane.m_contrast);
return result;
}