-
Notifications
You must be signed in to change notification settings - Fork 3
/
TiledSplatBlur.cpp
2845 lines (2435 loc) · 140 KB
/
TiledSplatBlur.cpp
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "PCH.h"
#include "TiledSplatBlur.h"
namespace TiledSplatBlur
{
////////////////////////////////////////////////////////////////////////////////
// Define the component
DEFINE_COMPONENT(TILED_SPLAT_BLUR);
DEFINE_OBJECT(TILED_SPLAT_BLUR);
REGISTER_OBJECT_UPDATE_CALLBACK(TILED_SPLAT_BLUR, AFTER, INPUT);
REGISTER_OBJECT_RENDER_CALLBACK(TILED_SPLAT_BLUR, "Tiled Splat Blur [HDR]", OpenGL, AFTER, "Effects (HDR) [Begin]", 1,
&TiledSplatBlur::renderObjectOpenGL, &RenderSettings::firstCallTypeCondition,
&TiledSplatBlur::renderObjectPreconditionHDROpenGL, nullptr, nullptr);
REGISTER_OBJECT_RENDER_CALLBACK(TILED_SPLAT_BLUR, "Tiled Splat Blur [LDR]", OpenGL, AFTER, "Effects (LDR) [Begin]", 1,
&TiledSplatBlur::renderObjectOpenGL, &RenderSettings::firstCallTypeCondition,
&TiledSplatBlur::renderObjectPreconditionLDROpenGL, nullptr, nullptr);
////////////////////////////////////////////////////////////////////////////////
namespace Tiling
{
////////////////////////////////////////////////////////////////////////////////
/** Compute the max render resolution. */
glm::ivec2 computeMaxRenderResolution(Scene::Scene& scene, Scene::Object* object)
{
return RenderSettings::getResolutionById(scene, 0);
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the normal render resolution. */
glm::ivec2 computeRenderResolution(Scene::Scene& scene, Scene::Object* object)
{
return RenderSettings::getResolutionById(scene, object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_renderResolutionId);
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the min and max fovy settings. */
std::array<float, 1> computeFovy(Scene::Scene& scene, Scene::Object* object)
{
Scene::Object* renderSettings = Scene::findFirstObject(scene, Scene::OBJECT_TYPE_RENDER_SETTINGS);
Scene::Object* camera = RenderSettings::getMainCamera(scene, renderSettings);
return { Camera::getFieldOfView(renderSettings, camera).y };
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the min and max fovy settings. */
std::array<float, 2> computeFovyLimits(Scene::Scene& scene, Scene::Object* object)
{
Scene::Object* renderSettings = Scene::findFirstObject(scene, Scene::OBJECT_TYPE_RENDER_SETTINGS);
Scene::Object* camera = RenderSettings::getMainCamera(scene, renderSettings);
return Camera::getFieldOfViewLimits(renderSettings, camera);
}
////////////////////////////////////////////////////////////////////////////////
int fragmentBlockSize(Scene::Scene& scene, Scene::Object* object, int mergeSteps)
{
return mergeSteps == 0 ? 1 : object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_mergePresets[mergeSteps - 1].m_blockSize;
}
////////////////////////////////////////////////////////////////////////////////
int fragmentBlockSize(Scene::Scene& scene, Scene::Object* object)
{
return fragmentBlockSize(scene, object, object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_fragmentMergeSteps);
}
////////////////////////////////////////////////////////////////////////////////
int maxFragmentBlockSize(Scene::Scene& scene, Scene::Object* object)
{
return fragmentBlockSize(scene, object, object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_mergePresets.size() - 1);
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the number of groups corresponding to the parameter resolution and group size */
glm::ivec2 computeNumGroups(glm::ivec2 resolution, int tileSize, int blockSize = 1)
{
return ((resolution + blockSize - 1) / blockSize + tileSize - 1) / tileSize;
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the number of tiles corresponding to the parameter resolution */
glm::ivec2 computeNumTiles(Scene::Scene& scene, Scene::Object* object, glm::ivec2 resolution, int blockSize = 1)
{
return computeNumGroups(resolution, object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize, blockSize);
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the number of tiles corresponding to the render resolution of the object */
glm::ivec2 computeNumTiles(Scene::Scene& scene, Scene::Object* object, int blockSize = 1)
{
return computeNumTiles(scene, object, computeRenderResolution(scene, object), blockSize);
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the padded resolution (size of the area spanned by the spawned tiles) corresponding to the parameter resolution */
glm::ivec2 computePaddedResolution(Scene::Scene& scene, Scene::Object* object, glm::ivec2 resolution, int blockSize = 1)
{
return computeNumTiles(scene, object, resolution, blockSize) * blockSize * object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize;
}
////////////////////////////////////////////////////////////////////////////////
/** Compute the padded resolution (size of the area spanned by the spawned tiles) corresponding to the render resolution of the object */
glm::ivec2 computePaddedResolution(Scene::Scene& scene, Scene::Object* object)
{
return computePaddedResolution(scene, object, computeRenderResolution(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
int fragmentBufferMaxFragmentsPerEntry(Scene::Scene& scene, Scene::Object* object, int layers)
{
return layers;
}
////////////////////////////////////////////////////////////////////////////////
float tileBufferLayerMultiplier(int layers)
{
return float(layers);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxCenterFragmentsPerEntry(int layers, int tileSize)
{
// Number of entries in the center zone
return int(tileBufferLayerMultiplier(layers) * tileSize * tileSize);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxCenterFragmentsPerEntry(Scene::Scene& scene, Scene::Object* object, int layers)
{
return tileBufferMaxCenterFragmentsPerEntry(
layers,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxFragmentsPerEntry(int layers, int tileSize, int maxCoc)
{
// Number of entries in the center zone
int center = tileSize * tileSize;
// Number of entries in the neighbor zones (top, bottom, left, right)
int side = 4.0f * tileSize * maxCoc;
// Number of entries in the neighbor zones (top, bottom, left, right)
int corner = 4.0f * maxCoc * maxCoc;
// Multiply by the number of layers and return
return int(tileBufferLayerMultiplier(layers) * (center + side + corner));
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxFragmentsPerEntry(Scene::Scene& scene, Scene::Object* object, int layers)
{
return tileBufferMaxFragmentsPerEntry(
layers,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_maxCoC);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferNumNeighborTilesSplat(int tileSize, int maxCoc)
{
return (maxCoc + tileSize - 1) / tileSize;
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferNumNeighborTilesSplat(Scene::Scene& scene, Scene::Object* object)
{
return tileBufferNumNeighborTilesSplat(
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_maxCoC);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxSortElements(int maxFragmentsPerEntry)
{
return std::next_pow2(maxFragmentsPerEntry);
}
////////////////////////////////////////////////////////////////////////////////
int sortGroupId(int groupSize, int maxSharedIndices)
{
return int(glm::log2((float)groupSize)) - int(glm::log2((float)maxSharedIndices));
}
////////////////////////////////////////////////////////////////////////////////
int dispatchElementIndex(int groupSize, int maxSharedIndices)
{
return (sortGroupId(groupSize, maxSharedIndices) * (sortGroupId(groupSize, maxSharedIndices) + 1)) / 2;
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxSortSharedElements(int maxSortElements, int maxFragmentsPerEntry)
{
return glm::min(tileBufferMaxSortElements(maxFragmentsPerEntry), maxSortElements);
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxSortSharedElements(Scene::Scene& scene, Scene::Object* object, int layers)
{
return tileBufferMaxSortSharedElements(
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_maxSortElements,
tileBufferMaxFragmentsPerEntry(scene, object, layers));
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxSortIterations(int maxFragmentsPerEntry, int tileBufferMaxSharedSortIndices)
{
// Maximum number of fragments in a tile buffer entry
int maxSortElements = tileBufferMaxSortElements(maxFragmentsPerEntry);
// Maximum number of total iterations needed
int maxTotalIterations = (int)glm::log2((float)glm::max(maxSortElements, tileBufferMaxSharedSortIndices));
// From which this many iterations are inner ones
int numInnerIterations = (int)glm::log2((float)tileBufferMaxSharedSortIndices);
// Maximum number of iterations necessary to sort a tile buffer entry
return maxTotalIterations - numInnerIterations + 1;
}
////////////////////////////////////////////////////////////////////////////////
int tileBufferMaxSortIterations(Scene::Scene& scene, Scene::Object* object, int layers)
{
return tileBufferMaxSortIterations(
tileBufferMaxFragmentsPerEntry(scene, object, layers),
tileBufferMaxSortSharedElements(scene, object, layers));
}
////////////////////////////////////////////////////////////////////////////////
int dispatchBufferMaxDispatchPerEntry(int maxSortIterations)
{
return (maxSortIterations * (maxSortIterations + 1)) / 2;
}
////////////////////////////////////////////////////////////////////////////////
int dispatchBufferMaxDispatchPerEntry(int maxFragmentsPerEntry, int tileBufferMaxSharedSortIndices)
{
return dispatchBufferMaxDispatchPerEntry(tileBufferMaxSortIterations(maxFragmentsPerEntry, tileBufferMaxSharedSortIndices));
}
////////////////////////////////////////////////////////////////////////////////
int dispatchBufferMaxDispatchPerEntry(Scene::Scene& scene, Scene::Object* object, int layers)
{
return dispatchBufferMaxDispatchPerEntry(
tileBufferMaxFragmentsPerEntry(scene, object, layers),
tileBufferMaxSortSharedElements(scene, object, layers));
}
}
////////////////////////////////////////////////////////////////////////////////
namespace Psfs
{
////////////////////////////////////////////////////////////////////////////////
Aberration::WavefrontAberration& getAberration(Scene::Scene& scene, Scene::Object* object)
{
return object->component<TiledSplatBlurComponent>().m_aberration;
}
////////////////////////////////////////////////////////////////////////////////
Aberration::PSFStack& getPsfStack(Scene::Scene& scene, Scene::Object* object)
{
return object->component<TiledSplatBlurComponent>().m_aberration.m_psfStack;
}
////////////////////////////////////////////////////////////////////////////////
Aberration::WavefrontAberrationPresets& getAberrationPresets(Scene::Scene& scene, Scene::Object* object)
{
return object->component<TiledSplatBlurComponent>().m_aberrationPresets;
}
////////////////////////////////////////////////////////////////////////////////
bool hasOffAxisPsfs(Scene::Scene& scene, Scene::Object* object)
{
return Aberration::getNumHorizontalAngles(scene, getAberration(scene, object)) > 1 ||
Aberration::getNumVerticalAngles(scene, getAberration(scene, object)) > 1;
}
////////////////////////////////////////////////////////////////////////////////
Aberration::PsfIndexIterator stackBegin(Scene::Scene& scene, Scene::Object* object)
{
return Aberration::psfStackBegin(scene, getAberration(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
Aberration::PsfIndexIterator stackEnd(Scene::Scene& scene, Scene::Object* object)
{
return Aberration::psfStackEnd(scene, getAberration(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
Aberration::PsfIndex getPsfIndex(Scene::Scene& scene, Scene::Object* object, const size_t psfIndex)
{
return Aberration::getPsfIndex(scene, getAberration(scene, object), psfIndex);
}
////////////////////////////////////////////////////////////////////////////////
Aberration::PsfStackElements::PsfEntry& selectEntry(Scene::Scene& scene, Scene::Object* object, Aberration::PsfIndex const& psfIndex)
{
return Aberration::getPsfEntry(scene, getAberration(scene, object), psfIndex);
}
////////////////////////////////////////////////////////////////////////////////
bool isIndexNeighbor(const size_t a, const size_t b)
{
return ((a - b) <= 1 || (b - a) <= 1);
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsNeighbors(Aberration::PsfIndex const& a, Aberration::PsfIndex const& b, const bool offAxis)
{
bool result = a != b;
result &= isIndexNeighbor(a[0], b[0]);
result &= ((offAxis && isIndexNeighbor(a[1], b[1])) || (!offAxis && a[1] == b[1]));
result &= ((offAxis && isIndexNeighbor(a[2], b[2])) || (!offAxis && a[2] == b[2]));
result &= (a[3] == b[3]);
result &= isIndexNeighbor(a[4], b[4]);
result &= isIndexNeighbor(a[5], b[5]);
return result;
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsNeighbors(Scene::Scene& scene, Scene::Object* object, Aberration::PsfIndex const& a, Aberration::PsfIndex const& b)
{
return arePsfsNeighbors(a, b, object->component<TiledSplatBlurComponent>().m_psfAxisMethod == TiledSplatBlurComponent::OffAxis);
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsSame(Aberration::PsfIndex const& a, Aberration::PsfIndex const& b)
{
return a == b;
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsSame(Scene::Scene& scene, Scene::Object* object, Aberration::PsfIndex const& a, Aberration::PsfIndex const& b)
{
return arePsfsSame(a, b);
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsSameOrNeighbors(Aberration::PsfIndex const& a, Aberration::PsfIndex const& b, const bool offAxis)
{
return arePsfsSame(a, b) || arePsfsNeighbors(a, b, offAxis);
}
////////////////////////////////////////////////////////////////////////////////
bool arePsfsSameOrNeighbors(Scene::Scene& scene, Scene::Object* object, Aberration::PsfIndex const& a, Aberration::PsfIndex const& b)
{
return arePsfsSameOrNeighbors(a, b, object->component<TiledSplatBlurComponent>().m_psfAxisMethod == TiledSplatBlurComponent::OffAxis);
}
////////////////////////////////////////////////////////////////////////////////
// Returns the blur radius (in pixels) for the parameter PSF index at the input resolution and fovy setting
float blurRadius(Scene::Scene& scene, Scene::Object* object, glm::ivec2 resolution, float fovy,
Aberration::PsfIndex const& psfIndex)
{
return Aberration::blurRadiusPixels(selectEntry(scene, object, psfIndex), resolution, fovy);
}
////////////////////////////////////////////////////////////////////////////////
// Returns the blur radius (in pixels) for the parameter PSF indices at the input resolution and fovy settings
template<size_t N, size_t M>
std::array<float, N * M> blurRadius(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, std::array<Aberration::PsfIndex, M> const& psfIndices)
{
std::array<float, N * M> result;
for (size_t psfId = 0; psfId < M; ++psfId)
{
auto const& psf = selectEntry(scene, object, psfIndices[psfId]);
for (size_t fovyId = 0; fovyId < N; ++fovyId)
{
result[psfId * N + fovyId] = Aberration::blurRadiusPixels(psf, resolution, fovys[fovyId]);
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Returns the blur radius (in pixels) for the parameter PSF index at the input resolution and fovy settings
template<size_t N>
std::array<float, N> blurRadius(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, Aberration::PsfIndex const& psfIndex)
{
return blurRadius(scene, object, resolution, fovys, std::array<Aberration::PsfIndex, 1>{ psfIndex });
}
////////////////////////////////////////////////////////////////////////////////
// Returns the minimum blur radius (in pixels) for the parameter PSF index given the input resolution and fovy settings
template<size_t N, size_t M>
size_t minBlurRadius(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, std::array<Aberration::PsfIndex, M> const& psfIndices)
{
auto const& radii = blurRadius(scene, object, resolution, fovys, psfIndices);
return size_t(glm::floor(*std::min_element(radii.begin(), radii.end())));
}
////////////////////////////////////////////////////////////////////////////////
// Returns the maximum blur radius (in pixels) for the parameter PSF index given the input resolution and fovy settings
template<size_t N, size_t M>
size_t maxBlurRadius(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, std::array<Aberration::PsfIndex, M> const& psfIndices)
{
auto const& radii = blurRadius(scene, object, resolution, fovys, psfIndices);
return size_t(glm::ceil(*std::max_element(radii.begin(), radii.end())));
}
////////////////////////////////////////////////////////////////////////////////
// Returns the absolute blur radius difference (in pixels) for the parameter PSF index given the input resolution,
// for each neighboring fovy setting
template<size_t N, size_t M>
std::array<float, N * (M - 1)> blurRadiusDifference(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, std::array<Aberration::PsfIndex, M> const& psfIndices)
{
std::array<float, N * (M - 1)> result;
for (size_t psfId = 0; psfId < (M - 1); ++psfId)
{
auto const& psf0 = selectEntry(scene, object, psfIndices[psfId]);
auto const& psf1 = selectEntry(scene, object, psfIndices[psfId + 1]);
for (size_t fovyId = 0; fovyId < N; ++fovyId)
{
const float r0 = Aberration::blurRadiusPixels(psf0, resolution, fovys[fovyId]);
const float r1 = Aberration::blurRadiusPixels(psf1, resolution, fovys[fovyId]);
result[psfId * N + fovyId] = glm::abs(r1 - r0);
}
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
// Returns the maximum absolute blur radius difference (in pixels) for the parameter PSF index given the
// input resolution and each neighboring fovy setting
template<size_t N, size_t M>
size_t maxBlurRadiusDifference(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 const& resolution, std::array<float, N> const& fovys, std::array<Aberration::PsfIndex, M> const& psfIndices)
{
auto const& differences = blurRadiusDifference(scene, object, resolution, fovys, psfIndices);
return size_t(glm::ceil(*std::max_element(differences.begin(), differences.end())));
}
////////////////////////////////////////////////////////////////////////////////
// Predicates for the min and max blur sizes
static auto const& s_minPred = [](const int p, const float r) { return glm::min(p, int(glm::floor(r))); };
static auto const& s_maxPred = [](const int p, const float r) { return glm::max(p, int(glm::ceil(r))); };
////////////////////////////////////////////////////////////////////////////////
// Predicate to calculate the min and max blur radii, given the previous min and max values and a set of blur radii
template<size_t N, typename Fp>
glm::ivec2 minMaxPred(Scene::Scene& scene, Scene::Object* object, glm::ivec2 resolution, std::array<float, N> const& fovys,
glm::ivec2 const& prev, Fp const& filterPred, Aberration::PsfIndex const& psfIndex)
{
if (!filterPred(psfIndex)) return prev;
std::array<float, N> const& radii = blurRadius(scene, object, resolution, fovys, psfIndex);
return glm::ivec2
(
std::accumulate(radii.begin(), radii.end(), prev[0], s_minPred),
std::accumulate(radii.begin(), radii.end(), prev[1], s_maxPred)
);
}
////////////////////////////////////////////////////////////////////////////////
template<size_t N, typename Fp>
glm::ivec2 blurRadiusLimits(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 resolution, std::array<float, N> const& fovys, Fp const& filterPred)
{
return std::accumulate(stackBegin(scene, object), stackEnd(scene, object), glm::ivec2(INT_MAX, -INT_MAX),
[&](glm::ivec2 prev, auto const& psfIndex)
{ return minMaxPred(scene, object, resolution, fovys, prev, filterPred, psfIndex); });
}
////////////////////////////////////////////////////////////////////////////////
template<size_t N>
glm::ivec2 blurRadiusLimits(Scene::Scene& scene, Scene::Object* object,
glm::ivec2 resolution, std::array<float, N> const& fovys)
{
return blurRadiusLimits(scene, object, resolution, fovys, [](auto const&) { return true; });
}
////////////////////////////////////////////////////////////////////////////////
glm::ivec2 blurRadiusLimitsCurrent(Scene::Scene& scene, Scene::Object* object)
{
return blurRadiusLimits(scene, object,
Tiling::computeRenderResolution(scene, object),
Tiling::computeFovy(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
glm::ivec2 blurRadiusLimitsGlobal(Scene::Scene& scene, Scene::Object* object)
{
return blurRadiusLimits(scene, object,
Tiling::computeMaxRenderResolution(scene, object),
Tiling::computeFovyLimits(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
glm::ivec2 blurRadiusLimitsEntry(Scene::Scene& scene, Scene::Object* object,
Aberration::PsfIndex const& psfIndex)
{
return blurRadiusLimits(scene, object,
Tiling::computeMaxRenderResolution(scene, object),
Tiling::computeFovyLimits(scene, object),
[&](auto const& neighborIndex) { return arePsfsSameOrNeighbors(scene, object, psfIndex, neighborIndex); });
}
////////////////////////////////////////////////////////////////////////////////
int weightsPerEntrySingleChannel(const int minRadius, const int maxRadius)
{
// FullSimplify[Sum[(i * 2 + 1) * (i * 2 + 1), { i, n0, n1 }]]
return (minRadius - 4 * minRadius * minRadius * minRadius + (1 + maxRadius) * (1 + 2 * maxRadius) * (3 + 2 * maxRadius)) / 3;
}
////////////////////////////////////////////////////////////////////////////////
int weightsPerEntrySingleChannel(const int maxRadius)
{
// FullSimplify[Sum[(i * 2 + 1) * (i * 2 + 1), { i, 0, n }]]
return ((1 + maxRadius) * (1 + 2 * maxRadius) * (3 + 2 * maxRadius)) / 3;
}
////////////////////////////////////////////////////////////////////////////////
int weightsPerEntry(const int maxRadius, const int maxChannels)
{
return maxChannels * weightsPerEntrySingleChannel(maxRadius);
}
////////////////////////////////////////////////////////////////////////////////
int weightsPerEntry(const int minRadius, const int maxRadius, const int maxChannels)
{
return maxChannels * weightsPerEntrySingleChannel(minRadius, maxRadius);
}
////////////////////////////////////////////////////////////////////////////////
int weightsPerEntry(const glm::ivec2 radii, const int maxChannels)
{
return weightsPerEntry(radii[0], radii[1], maxChannels);
}
////////////////////////////////////////////////////////////////////////////////
void initPsfStack(Scene::Scene& scene, Scene::Object* object)
{
const Aberration::PsfStackComputation computationFlags =
Aberration::PsfStackComputation_RelaxedEyeParameters |
Aberration::PsfStackComputation_FocusedEyeParameters |
Aberration::PsfStackComputation_PsfUnits |
Aberration::PsfStackComputation_PsfBesselTerms |
Aberration::PsfStackComputation_PsfEnzCoefficients;
Aberration::computePSFStack(scene, getAberration(scene, object), computationFlags);
}
////////////////////////////////////////////////////////////////////////////////
void computePsfs(Scene::Scene& scene, Scene::Object* object)
{
Aberration::computePSFStack(scene, getAberration(scene, object), Aberration::PsfStackComputation_Everything);
}
////////////////////////////////////////////////////////////////////////////////
void clearPsfCache(Scene::Scene& scene, Scene::Object* object)
{
Aberration::freeCacheResources(scene, getAberration(scene, object));
}
////////////////////////////////////////////////////////////////////////////////
using PsfGpu = Eigen::Matrix<GLfloat, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
PsfGpu computeGpuPsf(Scene::Scene& scene, Scene::Object* object,
Aberration::PsfStackElements::PsfEntry const& psfEntry, const size_t radius)
{
return Aberration::resizePsfNormalized(scene, getAberration(scene, object), psfEntry.m_psf, radius).cast<GLfloat>();
}
////////////////////////////////////////////////////////////////////////////////
void updateDerivedParameters(Scene::Scene& scene, Scene::Object* object)
{
// Extract the total number of PSFs and weights
const size_t numTotalPsfs = getPsfStack(scene, object).m_psfs.num_elements();
DateTime::ScopedTimer timer = DateTime::ScopedTimer(Debug::Debug, numTotalPsfs, DateTime::Seconds, "Derived PSF Params");
// Resize the derived parameters vector
object->component<TiledSplatBlurComponent>().m_derivedPsfParameters.resize(numTotalPsfs);
Threading::threadedExecuteIndices(Threading::numThreads(),
[&](Threading::ThreadedExecuteEnvironment const& environment, size_t psfId)
{
// Extract the psf and calculate the needed properties
const Aberration::PsfIndex psfIndex = Psfs::getPsfIndex(scene, object, psfId);
Aberration::PsfStackElements::PsfEntry const& psfEntry = Psfs::selectEntry(scene, object, psfIndex);
const glm::ivec2 blurRadii = Psfs::blurRadiusLimitsEntry(scene, object, psfIndex);
// Remember the total number of weights for this PSF entry
const size_t numPsfWeights = Psfs::weightsPerEntry(blurRadii[0], blurRadii[1], 1);
// Store the relevant props in the output structure
TiledSplatBlurComponent::DerivedPsfParameters& derivedPsfParameters =
object->component<TiledSplatBlurComponent>().m_derivedPsfParameters[psfId];
derivedPsfParameters.m_minBlurRadius = blurRadii[0];
derivedPsfParameters.m_maxBlurRadius = blurRadii[1];
derivedPsfParameters.m_numPsfWeights = numPsfWeights;
derivedPsfParameters.m_blurRadiusDeg = psfEntry.m_blurRadiusDeg;
},
numTotalPsfs);
}
////////////////////////////////////////////////////////////////////////////////
bool isEyeStateFixed(Scene::Scene& scene, Scene::Object* object)
{
return getPsfStack(scene, object).m_psfEntryParameters[0][0][0][0].size() == 1 &&
getPsfStack(scene, object).m_psfEntryParameters[0][0][0][0][0].size() == 1;
}
////////////////////////////////////////////////////////////////////////////////
size_t numTotalPsfs(Scene::Scene& scene, Scene::Object* object)
{
return object->component<TiledSplatBlurComponent>().m_derivedPsfParameters.size();
}
////////////////////////////////////////////////////////////////////////////////
size_t numTotalWeights(Scene::Scene& scene, Scene::Object* object)
{
size_t result = 0;
for (auto const& psfParameters : object->component<TiledSplatBlurComponent>().m_derivedPsfParameters)
result += psfParameters.m_numPsfWeights;
return result;
}
}
////////////////////////////////////////////////////////////////////////////////
namespace Shaders
{
////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> shaderNames(Scene::Scene& scene, Scene::Object* object)
{
return std::vector<std::string>
{
"psf_cache_command"s,
"psf_cache_params"s,
"psf_cache_texture"s,
"psf_texture_command_params"s,
"psf_texture_command_texture"s,
"psf_texture_params"s,
"psf_texture_texture"s,
"fragment_buffer_build"s,
"tile_buffer_build"s,
"fragment_buffer_merge"s,
"tile_buffer_splat"s,
"tile_buffer_splat_command"s,
"tile_buffer_sort_params"s,
"tile_buffer_sort_presort"s,
"tile_buffer_sort_inner"s,
"tile_buffer_sort_outer"s,
"convolution"s,
};
}
////////////////////////////////////////////////////////////////////////////////
bool isOutputModified(TiledSplatBlurComponent::OutputMode outputMode, TiledSplatBlurComponent::OverlayMode overlayMode)
{
return (outputMode != TiledSplatBlurComponent::Convolution || overlayMode != TiledSplatBlurComponent::None);
}
////////////////////////////////////////////////////////////////////////////////
bool shouldUseDebugShader(TiledSplatBlurComponent::ShaderType shaderType, TiledSplatBlurComponent::OutputMode outputMode, TiledSplatBlurComponent::OverlayMode overlayMode)
{
return shaderType == TiledSplatBlurComponent::Debug || (shaderType == TiledSplatBlurComponent::Auto && isOutputModified(outputMode, overlayMode));
}
////////////////////////////////////////////////////////////////////////////////
bool shouldUseDebugShader(Scene::Scene& scene, Scene::Object* object)
{
return shouldUseDebugShader(
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_shaderType,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_outputMode,
object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_overlayMode);
}
////////////////////////////////////////////////////////////////////////////////
std::string psfTextureFormatString(TiledSplatBlurComponent::PsfTextureFormat format)
{
switch (format)
{
case TiledSplatBlurComponent::F11: return "r11f_g11f_b10f";;
case TiledSplatBlurComponent::F16: return "rgba16f";
case TiledSplatBlurComponent::F32: return "rgba32f";
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
std::string psfTextureFormatString(Scene::Scene& scene, Scene::Object* object)
{
return psfTextureFormatString(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureFormat);
}
////////////////////////////////////////////////////////////////////////////////
/** The set of parameters needed for generating a shader. */
struct ShaderParameters
{
// Constructor that sets the default values
ShaderParameters(Scene::Scene& scene, Scene::Object* object) :
m_maxLayers(GPU::numLayers()),
m_tileSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_tileSize),
m_maxCoc(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_maxCoC),
m_mergeSteps(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_fragmentMergeSteps),
m_mergedBlockSize(Tiling::fragmentBlockSize(scene, object, m_mergeSteps)),
m_mergedTileSize((m_tileSize / Tiling::fragmentBlockSize(scene, object, m_mergeSteps))),
m_interpolationGroupSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_groupSizes.m_interpolation),
m_mergeGroupSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_groupSizes.m_merge),
m_splatGroupSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_groupSizes.m_splat),
m_sortGroupSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_groupSizes.m_sort),
m_convolutionGroupSize(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_groupSizes.m_convolution),
m_isDebugging(shouldUseDebugShader(scene, object) ? 1 : 0),
m_maxSortIndices(Tiling::tileBufferMaxSortSharedElements(scene, object, m_maxLayers)),
m_numNeighborTilesSplat(Tiling::tileBufferNumNeighborTilesSplat(m_tileSize, m_maxCoc)),
m_numSortIterations(Tiling::tileBufferMaxSortIterations(scene, object, m_maxLayers)),
m_sortElementsPerThread(glm::max(m_maxSortIndices / m_sortGroupSize / 2, 1)),
m_isEyeStateFixed(Psfs::isEyeStateFixed(scene, object)),
m_psfAxisMethod(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfAxisMethod),
m_psfTextureFormat(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureFormat),
m_psfTextureDepthLayout(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureDepthLayout),
m_psfTextureAngleLayout(object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureAngleLayout)
{}
// The various parameters
int m_maxLayers;
int m_tileSize;
int m_maxCoc;
int m_mergeSteps;
int m_mergedBlockSize;
int m_mergedTileSize;
int m_interpolationGroupSize;
int m_mergeGroupSize;
int m_splatGroupSize;
int m_sortGroupSize;
int m_convolutionGroupSize;
int m_isDebugging;
int m_maxSortIndices;
int m_numNeighborTilesSplat;
int m_numSortIterations;
int m_sortElementsPerThread;
int m_isEyeStateFixed;
TiledSplatBlurComponent::PsfAxisMethod m_psfAxisMethod;
TiledSplatBlurComponent::PsfTextureFormat m_psfTextureFormat;
TiledSplatBlurComponent::PsfTextureDepthLayout m_psfTextureDepthLayout;
TiledSplatBlurComponent::PsfTextureAngleLayout m_psfTextureAngleLayout;
////////////////////////////////////////////////////////////////////////////////
template<typename Fn>
void forEachParameter(Fn const& callback) const
{
callback("Layers", "MAX_LAYERS", m_maxLayers);
callback("Tile Size", "TILE_SIZE", m_tileSize);
callback("Merge Steps", "MERGE_STEPS", m_mergeSteps);
callback("Merged Tile Size", "MERGED_TILE_SIZE", m_mergedTileSize);
callback("Merged Block Size", "MERGED_BLOCK_SIZE", m_mergedBlockSize);
callback("Interpolation Group Size", "INTERPOLATION_GROUP_SIZE", m_interpolationGroupSize);
callback("Merged Group Size", "FRAGMENT_MERGE_GROUP_SIZE", m_mergeGroupSize);
callback("Splat Group Size", "SPLAT_GROUP_SIZE", m_splatGroupSize);
callback("Sort Group Size", "SORT_GROUP_SIZE", m_sortGroupSize);
callback("Convolution Group Size", "CONVOLUTION_GROUP_SIZE", m_convolutionGroupSize);
callback("Max Sort Indices", "SORT_SHARED_ARRAY_SIZE", m_maxSortIndices);
callback("Num Splat Neighbor Tiles", "NUM_NEIGHBOR_TILES_SPLAT", m_numNeighborTilesSplat);
callback("Num Sort Iterations", "NUM_SORT_ITERATIONS", m_numSortIterations);
callback("Sort Elements Per Thread", "SORT_ELEMENTS_PER_THREAD", m_sortElementsPerThread);
callback("Is Debugging", "DEBUG_OUTPUT", m_isDebugging);
callback("Max PSF Radius", "MAX_PSF_RADIUS", m_maxCoc);
callback("Max PSF Diameter", "MAX_PSF_DIAMETER", m_maxCoc * 2 + 1);
callback("Fixed Eye State", "FIXED_EYE_STATE", m_isEyeStateFixed ? 1 : 0);
callback("PSF Texture Format", "PSF_TEXTURE_FORMAT", std::string(TiledSplatBlurComponent::PsfTextureFormat_value_to_string(m_psfTextureFormat)));
callback("PSF Texture Format", "PSF_TEXTURE_FORMAT_ID", m_psfTextureFormat);
callback("PSF Texture Format String", "PSF_TEXTURE_TYPE", psfTextureFormatString(m_psfTextureFormat));
callback("PSF Axis Method", "PSF_AXIS_METHOD", std::string(TiledSplatBlurComponent::PsfAxisMethod_value_to_string(m_psfAxisMethod)));
callback("PSF Texture Layout", "PSF_TEXTURE_DEPTH_LAYOUT", std::string(TiledSplatBlurComponent::PsfTextureDepthLayout_value_to_string(m_psfTextureDepthLayout)));
callback("PSF Texture Layout", "PSF_TEXTURE_ANGLE_LAYOUT", std::string(TiledSplatBlurComponent::PsfTextureAngleLayout_value_to_string(m_psfTextureAngleLayout)));
callback("PSF Axis Method", "PSF_AXIS_METHOD_ID", m_psfAxisMethod);
callback("PSF Texture Layout", "PSF_TEXTURE_DEPTH_LAYOUT_ID", m_psfTextureDepthLayout);
callback("PSF Texture Layout", "PSF_TEXTURE_ANGLE_LAYOUT_ID", m_psfTextureAngleLayout);
}
////////////////////////////////////////////////////////////////////////////////
template<typename It, typename Fn>
void transformParameters(It outputIt, Fn const& transformFn) const
{
forEachParameter([&](std::string const& name, std::string const& id, auto const& value)
{ *(outputIt++) = transformFn(name, id, value); });
}
////////////////////////////////////////////////////////////////////////////////
inline size_t numParameters() const
{
size_t count = 0;
forEachParameter([&](std::string const& name, std::string const& id, auto const& value)
{ ++count; });
return count;
}
};
////////////////////////////////////////////////////////////////////////////////
std::string shaderParametersToString(ShaderParameters const& parameters)
{
std::stringstream result;
parameters.forEachParameter([&](std::string const& name, std::string const& id, auto const& value)
{
result << name << ": " << value << std::endl;
});
return result.str();
}
////////////////////////////////////////////////////////////////////////////////
void emitProfilerShaderParameters(Scene::Scene& scene, ShaderParameters const& parameters)
{
parameters.forEachParameter([&](std::string const& name, std::string const& id, auto const& value)
{
Profiler::storeData(scene, { "Shader Parameters", name }, value);
});
}
////////////////////////////////////////////////////////////////////////////////
std::string shaderNameSuffix(Scene::Scene& scene, Scene::Object* object, ShaderParameters parameters)
{
std::stringstream ss;
parameters.forEachParameter([&](std::string const& name, std::string const& id, auto const& value)
{
ss << "_" << value;
});
return ss.str();
}
////////////////////////////////////////////////////////////////////////////////
Asset::ShaderParameters shaderDefines(Scene::Scene& scene, Scene::Object* object, ShaderParameters parameters)
{
Asset::ShaderParameters result;
// Append the shader parameters
parameters.transformParameters(std::back_inserter(result.m_defines), [](std::string const& name, std::string const& id, auto const& value)
{
std::stringstream ss;
ss << id << " " << value;
return ss.str();
});
// Add the meta enums
result.m_enums = Asset::generateMetaEnumDefines
(
TiledSplatBlurComponent::OutputMode_meta,
TiledSplatBlurComponent::OverlayMode_meta,
TiledSplatBlurComponent::AccumulationMethod_meta,
TiledSplatBlurComponent::PsfAxisMethod_meta,
TiledSplatBlurComponent::WeightScaleMethod_meta,
TiledSplatBlurComponent::WeightRescaleMethod_meta,
TiledSplatBlurComponent::CoefficientLerpMethod_meta,
TiledSplatBlurComponent::PsfTextureDepthLayout_meta,
TiledSplatBlurComponent::PsfTextureAngleLayout_meta
);
return result;
}
////////////////////////////////////////////////////////////////////////////////
std::string getFullShaderName(Scene::Scene& scene, Scene::Object* object, std::string const& shaderName, std::string const& suffix)
{
return Asset::getShaderName("Aberration/TiledSplatBlur", shaderName, suffix);
}
////////////////////////////////////////////////////////////////////////////////
std::string getFullShaderName(Scene::Scene& scene, Scene::Object* object, std::string const& shaderName, ShaderParameters const& parameters)
{
return getFullShaderName(scene, object, shaderName, shaderNameSuffix(scene, object, parameters));
}
////////////////////////////////////////////////////////////////////////////////
void loadShader(Scene::Scene& scene, Scene::Object* object, std::string const& name, std::string const& fullName, Asset::ShaderParameters const& defines)
{
if (scene.m_shaders.find(fullName) == scene.m_shaders.end() || scene.m_shaders[fullName].m_program == 0)
Asset::loadShader(scene, "Aberration/TiledSplatBlur", name, fullName, defines);
}
////////////////////////////////////////////////////////////////////////////////
void loadShaders(Scene::Scene& scene, Scene::Object* object)
{
Debug::log_trace() << "Loading shaders for " << object->m_name << Debug::end;
// Various shader properties necessary
const ShaderParameters parameters(scene, object);
const std::string nameSuffix = shaderNameSuffix(scene, object, parameters);
const Asset::ShaderParameters defines = shaderDefines(scene, object, parameters);
const std::vector<std::string> names = shaderNames(scene, object);
// Load the the shaders, if needed
for (auto const& name : names)
loadShader(scene, object, name, getFullShaderName(scene, object, name, nameSuffix), defines);
Debug::log_trace() << "Shaders successfully loaded for " << object->m_name << Debug::end;
}
////////////////////////////////////////////////////////////////////////////////
void bindShader(Scene::Scene& scene, Scene::Object* object, std::string const& shaderName, ShaderParameters const& shaderParameters)
{
std::string const& fullShaderName = getFullShaderName(scene, object, shaderName, shaderParameters);
Debug::log_trace() << "Binding shader: " << shaderName << " (" << fullShaderName << ")" << Debug::end;
Scene::bindShader(scene, fullShaderName);
}
}
////////////////////////////////////////////////////////////////////////////////
namespace PsfTexture
{
////////////////////////////////////////////////////////////////////////////////
std::string textureName(Scene::Scene& scene, Scene::Object* object)
{
return object->m_name + "_" + "BasePsfs";
}
////////////////////////////////////////////////////////////////////////////////
std::string textureNameCache(Scene::Scene& scene, Scene::Object* object, const size_t a, const size_t f)
{
return object->m_name + "_" + "PsfCache" + std::to_string(a) + "_" + std::to_string(f);
}
////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> textureNames(Scene::Scene& scene, Scene::Object* object)
{
return std::vector<std::string>
{
textureName(scene, object)
};
}
////////////////////////////////////////////////////////////////////////////////
GLenum textureFormatEnum(Scene::Scene& scene, Scene::Object* object)
{
switch (object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureFormat)
{
case TiledSplatBlurComponent::F11: return GL_R11F_G11F_B10F;
case TiledSplatBlurComponent::F16: return GL_RGBA16F;
case TiledSplatBlurComponent::F32: return GL_RGBA32F;
}
return GL_R11F_G11F_B10F;
}
////////////////////////////////////////////////////////////////////////////////
GLenum imageFormatEnum(Scene::Scene& scene, Scene::Object* object)
{
switch (object->component<TiledSplatBlur::TiledSplatBlurComponent>().m_psfTextureFormat)
{
case TiledSplatBlurComponent::F11: return GL_R11F_G11F_B10F;
case TiledSplatBlurComponent::F16: return GL_RGBA16F;
case TiledSplatBlurComponent::F32: return GL_RGBA32F;
}
return GL_RGBA8;
}
////////////////////////////////////////////////////////////////////////////////
size_t textureSize(Scene::Scene& scene, Scene::Object* object, const glm::ivec3 dimensions)
{
return GPU::textureSizeBytes(dimensions, textureFormatEnum(scene, object), 0, 1);
}
////////////////////////////////////////////////////////////////////////////////
float calcNumSlicesReduction(const size_t maxRadius, const size_t maxDiff, const float s, const float p)
{
return 1.0f / glm::max(glm::pow(maxDiff * s, p), 1.0f);
}
////////////////////////////////////////////////////////////////////////////////
size_t calcNumReducedSlices(const size_t maxRadius, const size_t maxDiff, const float s, const float p)
{
return std::max(size_t(1), size_t(float(maxDiff) * calcNumSlicesReduction(maxRadius, maxDiff, s, p)));
}
////////////////////////////////////////////////////////////////////////////////
struct SliceInfo
{
struct PerSliceInfo
{
size_t m_numSlices = 0;
size_t m_maxDiff = 0;
float m_numLayerReduction = 0.0f;
std::array<size_t, 2> m_blurRadii = { 999, 0 };
Aberration::PsfIndex m_psfIndex;
};
std::array<float, 6> m_s;
std::array<float, 6> m_p;
std::array<size_t, 6> m_numSlices;
std::array<size_t, 6> m_numSlicesUnreduced;
std::array<Aberration::PsfIndex, 6> m_selectedPsfIndex;
std::array<std::array<size_t, 2>, 6> m_psfBurRadii;
std::array<float, 6> m_numLayerReduction;
std::array<std::vector<PerSliceInfo>, 6> m_slices;
void logStats() const
{
Debug::log_debug() << std::string(80, '=') << Debug::end;
Debug::log_debug() << "Number of texture layers needed for each axis: " << m_numSlices << Debug::end;
Debug::log_debug() << " - Without reductions: " << m_numSlicesUnreduced << Debug::end;
for (size_t axisId = 0; axisId < 6; ++axisId)
{
Debug::log_debug() << std::string(80, '-') << Debug::end;
Debug::log_debug() << "Axis #" << axisId << Debug::end;
//Debug::log_debug() << std::string(80, '-') << Debug::end;