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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
/*
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2007 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/render/intern/source/sss.c
* \ingroup render
*/
/* Possible Improvements:
* - add fresnel terms
* - adapt Rd table to scale, now with small scale there are a lot of misses?
* - possible interesting method: perform sss on all samples in the tree,
* and then use those values interpolated somehow later. can also do this
* filtering on demand for speed. since we are doing things in screen
* space now there is an exact correspondence
* - avoid duplicate shading (filtering points in advance, irradiance cache
* like lookup?)
* - lower resolution samples
*/
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
/* external modules: */
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_memarena.h"
#include "PIL_time.h"
#include "DNA_material_types.h"
#include "BKE_colortools.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_node.h"
#include "BKE_scene.h"
/* this module */
#include "render_types.h"
#include "rendercore.h"
#include "renderdatabase.h"
#include "shading.h"
#include "sss.h"
#include "zbuf.h"
/* Generic Multiple Scattering API */
/* Relevant papers:
* [1] A Practical Model for Subsurface Light Transport
* [2] A Rapid Hierarchical Rendering Technique for Translucent Materials
* [3] Efficient Rendering of Local Subsurface Scattering
* [4] Implementing a skin BSSRDF (or several...)
*/
/* Defines */
#define RD_TABLE_RANGE 100.0f
#define RD_TABLE_RANGE_2 10000.0f
#define RD_TABLE_SIZE 10000
#define MAX_OCTREE_NODE_POINTS 8
#define MAX_OCTREE_DEPTH 15
/* Struct Definitions */
struct ScatterSettings {
float eta; /* index of refraction */
float sigma_a; /* absorption coefficient */
float sigma_s_; /* reduced scattering coefficient */
float sigma_t_; /* reduced extinction coefficient */
float sigma; /* effective extinction coefficient */
float Fdr; /* diffuse fresnel reflectance */
float D; /* diffusion constant */
float A;
float alpha_; /* reduced albedo */
float zr; /* distance of virtual lightsource above surface */
float zv; /* distance of virtual lightsource below surface */
float ld; /* mean free path */
float ro; /* diffuse reflectance */
float color;
float invsigma_t_;
float frontweight;
float backweight;
float *tableRd; /* lookup table to avoid computing Rd */
float *tableRd2; /* lookup table to avoid computing Rd for bigger values */
};
typedef struct ScatterPoint {
float co[3];
float rad[3];
float area;
int back;
} ScatterPoint;
typedef struct ScatterNode {
float co[3];
float rad[3];
float backrad[3];
float area, backarea;
int totpoint;
ScatterPoint *points;
float split[3];
struct ScatterNode *child[8];
} ScatterNode;
struct ScatterTree {
MemArena *arena;
ScatterSettings *ss[3];
float error, scale;
ScatterNode *root;
ScatterPoint *points;
ScatterPoint **refpoints;
ScatterPoint **tmppoints;
int totpoint;
float min[3], max[3];
};
typedef struct ScatterResult {
float rad[3];
float backrad[3];
float rdsum[3];
float backrdsum[3];
} ScatterResult;
/* Functions for BSSRDF reparametrization in to more intuitive parameters,
* see [2] section 4 for more info. */
static float f_Rd(float alpha_, float A, float ro)
{
float sq;
sq= sqrt(3.0f*(1.0f - alpha_));
return (alpha_/2.0f)*(1.0f + expf((-4.0f/3.0f)*A*sq))*expf(-sq) - ro;
}
static float compute_reduced_albedo(ScatterSettings *ss)
{
const float tolerance= 1e-8;
const int max_iteration_count= 20;
float d, fsub, xn_1= 0.0f, xn= 1.0f, fxn, fxn_1;
int i;
/* use secant method to compute reduced albedo using Rd function inverse
* with a given reflectance */
fxn= f_Rd(xn, ss->A, ss->ro);
fxn_1= f_Rd(xn_1, ss->A, ss->ro);
for (i= 0; i < max_iteration_count; i++) {
fsub= (fxn - fxn_1);
if (fabsf(fsub) < tolerance)
break;
d= ((xn - xn_1)/fsub)*fxn;
if (fabsf(d) < tolerance)
break;
xn_1= xn;
fxn_1= fxn;
xn= xn - d;
if (xn > 1.0f) xn= 1.0f;
if (xn_1 > 1.0f) xn_1= 1.0f;
fxn= f_Rd(xn, ss->A, ss->ro);
}
/* avoid division by zero later */
if (xn <= 0.0f)
xn= 0.00001f;
return xn;
}
/* Exponential falloff functions */
static float Rd_rsquare(ScatterSettings *ss, float rr)
{
float sr, sv, Rdr, Rdv;
sr= sqrt(rr + ss->zr*ss->zr);
sv= sqrt(rr + ss->zv*ss->zv);
Rdr= ss->zr*(1.0f + ss->sigma*sr)*expf(-ss->sigma*sr)/(sr*sr*sr);
Rdv= ss->zv*(1.0f + ss->sigma*sv)*expf(-ss->sigma*sv)/(sv*sv*sv);
return /*ss->alpha_*/(1.0f/(4.0f*(float)M_PI))*(Rdr + Rdv);
}
static float Rd(ScatterSettings *ss, float r)
{
return Rd_rsquare(ss, r*r);
}
/* table lookups for Rd. this avoids expensive exp calls. we use two
* separate tables as well for lower and higher numbers to improve
* precision, since the number are poorly distributed because we do
* a lookup with the squared distance for smaller distances, saving
* another sqrt. */
static void approximate_Rd_rgb(ScatterSettings **ss, float rr, float *rd)
{
float indexf, t, idxf;
int index;
if (rr > (RD_TABLE_RANGE_2*RD_TABLE_RANGE_2));
else if (rr > RD_TABLE_RANGE) {
rr= sqrt(rr);
indexf= rr*(RD_TABLE_SIZE/RD_TABLE_RANGE_2);
index= (int)indexf;
idxf= (float)index;
t= indexf - idxf;
if (index >= 0 && index < RD_TABLE_SIZE) {
rd[0]= (ss[0]->tableRd2[index]*(1-t) + ss[0]->tableRd2[index+1]*t);
rd[1]= (ss[1]->tableRd2[index]*(1-t) + ss[1]->tableRd2[index+1]*t);
rd[2]= (ss[2]->tableRd2[index]*(1-t) + ss[2]->tableRd2[index+1]*t);
return;
}
}
else {
indexf= rr*(RD_TABLE_SIZE/RD_TABLE_RANGE);
index= (int)indexf;
idxf= (float)index;
t= indexf - idxf;
if (index >= 0 && index < RD_TABLE_SIZE) {
rd[0]= (ss[0]->tableRd[index]*(1-t) + ss[0]->tableRd[index+1]*t);
rd[1]= (ss[1]->tableRd[index]*(1-t) + ss[1]->tableRd[index+1]*t);
rd[2]= (ss[2]->tableRd[index]*(1-t) + ss[2]->tableRd[index+1]*t);
return;
}
}
/* fallback to slow Rd computation */
rd[0]= Rd_rsquare(ss[0], rr);
rd[1]= Rd_rsquare(ss[1], rr);
rd[2]= Rd_rsquare(ss[2], rr);
}
static void build_Rd_table(ScatterSettings *ss)
{
float r;
int i, size = RD_TABLE_SIZE+1;
ss->tableRd= MEM_mallocN(sizeof(float)*size, "scatterTableRd");
ss->tableRd2= MEM_mallocN(sizeof(float)*size, "scatterTableRd");
for (i= 0; i < size; i++) {
r= i*(RD_TABLE_RANGE/RD_TABLE_SIZE);
/*if (r < ss->invsigma_t_*ss->invsigma_t_)
r= ss->invsigma_t_*ss->invsigma_t_;*/
ss->tableRd[i]= Rd(ss, sqrt(r));
r= i*(RD_TABLE_RANGE_2/RD_TABLE_SIZE);
/*if (r < ss->invsigma_t_)
r= ss->invsigma_t_;*/
ss->tableRd2[i]= Rd(ss, r);
}
}
ScatterSettings *scatter_settings_new(float refl, float radius, float ior, float reflfac, float frontweight, float backweight)
{
ScatterSettings *ss;
ss= MEM_callocN(sizeof(ScatterSettings), "ScatterSettings");
/* see [1] and [3] for these formulas */
ss->eta= ior;
ss->Fdr= -1.440f/ior*ior + 0.710f/ior + 0.668f + 0.0636f*ior;
ss->A= (1.0f + ss->Fdr)/(1.0f - ss->Fdr);
ss->ld= radius;
ss->ro= MIN2(refl, 0.999f);
ss->color= ss->ro*reflfac + (1.0f-reflfac);
ss->alpha_= compute_reduced_albedo(ss);
ss->sigma= 1.0f/ss->ld;
ss->sigma_t_= ss->sigma/sqrtf(3.0f*(1.0f - ss->alpha_));
ss->sigma_s_= ss->alpha_*ss->sigma_t_;
ss->sigma_a= ss->sigma_t_ - ss->sigma_s_;
ss->D= 1.0f/(3.0f*ss->sigma_t_);
ss->zr= 1.0f/ss->sigma_t_;
ss->zv= ss->zr + 4.0f*ss->A*ss->D;
ss->invsigma_t_= 1.0f/ss->sigma_t_;
ss->frontweight= frontweight;
ss->backweight= backweight;
/* precompute a table of Rd values for quick lookup */
build_Rd_table(ss);
return ss;
}
void scatter_settings_free(ScatterSettings *ss)
{
MEM_freeN(ss->tableRd);
MEM_freeN(ss->tableRd2);
MEM_freeN(ss);
}
/* Hierarchical method as in [2]. */
/* traversal */
#define SUBNODE_INDEX(co, split) \
((co[0]>=split[0]) + (co[1]>=split[1])*2 + (co[2]>=split[2])*4)
static void add_radiance(ScatterTree *tree, float *frontrad, float *backrad, float area, float backarea, float rr, ScatterResult *result)
{
float rd[3], frontrd[3], backrd[3];
approximate_Rd_rgb(tree->ss, rr, rd);
if (frontrad && area) {
frontrd[0] = rd[0]*area;
frontrd[1] = rd[1]*area;
frontrd[2] = rd[2]*area;
result->rad[0] += frontrad[0]*frontrd[0];
result->rad[1] += frontrad[1]*frontrd[1];
result->rad[2] += frontrad[2]*frontrd[2];
result->rdsum[0] += frontrd[0];
result->rdsum[1] += frontrd[1];
result->rdsum[2] += frontrd[2];
}
if (backrad && backarea) {
backrd[0] = rd[0]*backarea;
backrd[1] = rd[1]*backarea;
backrd[2] = rd[2]*backarea;
result->backrad[0] += backrad[0]*backrd[0];
result->backrad[1] += backrad[1]*backrd[1];
result->backrad[2] += backrad[2]*backrd[2];
result->backrdsum[0] += backrd[0];
result->backrdsum[1] += backrd[1];
result->backrdsum[2] += backrd[2];
}
}
static void traverse_octree(ScatterTree *tree, ScatterNode *node, float *co, int self, ScatterResult *result)
{
float sub[3], dist;
int i, index = 0;
if (node->totpoint > 0) {
/* leaf - add radiance from all samples */
for (i=0; i<node->totpoint; i++) {
ScatterPoint *p= &node->points[i];
sub_v3_v3v3(sub, co, p->co);
dist= dot_v3v3(sub, sub);
if (p->back)
add_radiance(tree, NULL, p->rad, 0.0f, p->area, dist, result);
else
add_radiance(tree, p->rad, NULL, p->area, 0.0f, dist, result);
}
}
else {
/* branch */
if (self)
index = SUBNODE_INDEX(co, node->split);
for (i=0; i<8; i++) {
if (node->child[i]) {
ScatterNode *subnode= node->child[i];
if (self && index == i) {
/* always traverse node containing the point */
traverse_octree(tree, subnode, co, 1, result);
}
else {
/* decide subnode traversal based on maximum solid angle */
sub_v3_v3v3(sub, co, subnode->co);
dist= dot_v3v3(sub, sub);
/* actually area/dist > error, but this avoids division */
if (subnode->area+subnode->backarea>tree->error*dist) {
traverse_octree(tree, subnode, co, 0, result);
}
else {
add_radiance(tree, subnode->rad, subnode->backrad,
subnode->area, subnode->backarea, dist, result);
}
}
}
}
}
}
static void compute_radiance(ScatterTree *tree, float *co, float *rad)
{
ScatterResult result;
float rdsum[3], backrad[3], backrdsum[3];
memset(&result, 0, sizeof(result));
traverse_octree(tree, tree->root, co, 1, &result);
/* the original paper doesn't do this, but we normalize over the
* sampled area and multiply with the reflectance. this is because
* our point samples are incomplete, there are no samples on parts
* of the mesh not visible from the camera. this can not only make
* it darker, but also lead to ugly color shifts */
mul_v3_fl(result.rad, tree->ss[0]->frontweight);
mul_v3_fl(result.backrad, tree->ss[0]->backweight);
copy_v3_v3(rad, result.rad);
add_v3_v3v3(backrad, result.rad, result.backrad);
copy_v3_v3(rdsum, result.rdsum);
add_v3_v3v3(backrdsum, result.rdsum, result.backrdsum);
if (rdsum[0] > 1e-16f) rad[0]= tree->ss[0]->color*rad[0]/rdsum[0];
if (rdsum[1] > 1e-16f) rad[1]= tree->ss[1]->color*rad[1]/rdsum[1];
if (rdsum[2] > 1e-16f) rad[2]= tree->ss[2]->color*rad[2]/rdsum[2];
if (backrdsum[0] > 1e-16f) backrad[0]= tree->ss[0]->color*backrad[0]/backrdsum[0];
if (backrdsum[1] > 1e-16f) backrad[1]= tree->ss[1]->color*backrad[1]/backrdsum[1];
if (backrdsum[2] > 1e-16f) backrad[2]= tree->ss[2]->color*backrad[2]/backrdsum[2];
rad[0]= MAX2(rad[0], backrad[0]);
rad[1]= MAX2(rad[1], backrad[1]);
rad[2]= MAX2(rad[2], backrad[2]);
}
/* building */
static void sum_leaf_radiance(ScatterTree *UNUSED(tree), ScatterNode *node)
{
ScatterPoint *p;
float rad, totrad= 0.0f, inv;
int i;
node->co[0]= node->co[1]= node->co[2]= 0.0;
node->rad[0]= node->rad[1]= node->rad[2]= 0.0;
node->backrad[0]= node->backrad[1]= node->backrad[2]= 0.0;
/* compute total rad, rad weighted average position,
* and total area */
for (i=0; i<node->totpoint; i++) {
p= &node->points[i];
rad= p->area*fabsf(p->rad[0] + p->rad[1] + p->rad[2]);
totrad += rad;
node->co[0] += rad*p->co[0];
node->co[1] += rad*p->co[1];
node->co[2] += rad*p->co[2];
if (p->back) {
node->backrad[0] += p->rad[0]*p->area;
node->backrad[1] += p->rad[1]*p->area;
node->backrad[2] += p->rad[2]*p->area;
node->backarea += p->area;
}
else {
node->rad[0] += p->rad[0]*p->area;
node->rad[1] += p->rad[1]*p->area;
node->rad[2] += p->rad[2]*p->area;
node->area += p->area;
}
}
if (node->area > 1e-16f) {
inv= 1.0f/node->area;
node->rad[0] *= inv;
node->rad[1] *= inv;
node->rad[2] *= inv;
}
if (node->backarea > 1e-16f) {
inv= 1.0f/node->backarea;
node->backrad[0] *= inv;
node->backrad[1] *= inv;
node->backrad[2] *= inv;
}
if (totrad > 1e-16f) {
inv= 1.0f/totrad;
node->co[0] *= inv;
node->co[1] *= inv;
node->co[2] *= inv;
}
else {
/* make sure that if radiance is 0.0f, we still have these points in
* the tree at a good position, they count for rdsum too */
for (i=0; i<node->totpoint; i++) {
p= &node->points[i];
node->co[0] += p->co[0];
node->co[1] += p->co[1];
node->co[2] += p->co[2];
}
node->co[0] /= node->totpoint;
node->co[1] /= node->totpoint;
node->co[2] /= node->totpoint;
}
}
static void sum_branch_radiance(ScatterTree *UNUSED(tree), ScatterNode *node)
{
ScatterNode *subnode;
float rad, totrad= 0.0f, inv;
int i, totnode;
node->co[0]= node->co[1]= node->co[2]= 0.0;
node->rad[0]= node->rad[1]= node->rad[2]= 0.0;
node->backrad[0]= node->backrad[1]= node->backrad[2]= 0.0;
/* compute total rad, rad weighted average position,
* and total area */
for (i=0; i<8; i++) {
if (node->child[i] == NULL)
continue;
subnode= node->child[i];
rad= subnode->area*fabsf(subnode->rad[0] + subnode->rad[1] + subnode->rad[2]);
rad += subnode->backarea*fabsf(subnode->backrad[0] + subnode->backrad[1] + subnode->backrad[2]);
totrad += rad;
node->co[0] += rad*subnode->co[0];
node->co[1] += rad*subnode->co[1];
node->co[2] += rad*subnode->co[2];
node->rad[0] += subnode->rad[0]*subnode->area;
node->rad[1] += subnode->rad[1]*subnode->area;
node->rad[2] += subnode->rad[2]*subnode->area;
node->backrad[0] += subnode->backrad[0]*subnode->backarea;
node->backrad[1] += subnode->backrad[1]*subnode->backarea;
node->backrad[2] += subnode->backrad[2]*subnode->backarea;
node->area += subnode->area;
node->backarea += subnode->backarea;
}
if (node->area > 1e-16f) {
inv= 1.0f/node->area;
node->rad[0] *= inv;
node->rad[1] *= inv;
node->rad[2] *= inv;
}
if (node->backarea > 1e-16f) {
inv= 1.0f/node->backarea;
node->backrad[0] *= inv;
node->backrad[1] *= inv;
node->backrad[2] *= inv;
}
if (totrad > 1e-16f) {
inv= 1.0f/totrad;
node->co[0] *= inv;
node->co[1] *= inv;
node->co[2] *= inv;
}
else {
/* make sure that if radiance is 0.0f, we still have these points in
* the tree at a good position, they count for rdsum too */
totnode= 0;
for (i=0; i<8; i++) {
if (node->child[i]) {
subnode= node->child[i];
node->co[0] += subnode->co[0];
node->co[1] += subnode->co[1];
node->co[2] += subnode->co[2];
totnode++;
}
}
node->co[0] /= totnode;
node->co[1] /= totnode;
node->co[2] /= totnode;
}
}
static void sum_radiance(ScatterTree *tree, ScatterNode *node)
{
if (node->totpoint > 0) {
sum_leaf_radiance(tree, node);
}
else {
int i;
for (i=0; i<8; i++)
if (node->child[i])
sum_radiance(tree, node->child[i]);
sum_branch_radiance(tree, node);
}
}
static void subnode_middle(int i, float *mid, float *subsize, float *submid)
{
int x= i & 1, y= i & 2, z= i & 4;
submid[0]= mid[0] + ((x)? subsize[0]: -subsize[0]);
submid[1]= mid[1] + ((y)? subsize[1]: -subsize[1]);
submid[2]= mid[2] + ((z)? subsize[2]: -subsize[2]);
}
static void create_octree_node(ScatterTree *tree, ScatterNode *node, float *mid, float *size, ScatterPoint **refpoints, int depth)
{
ScatterNode *subnode;
ScatterPoint **subrefpoints, **tmppoints= tree->tmppoints;
int index, nsize[8], noffset[8], i, subco, usednodes, usedi;
float submid[3], subsize[3];
/* stopping condition */
if (node->totpoint <= MAX_OCTREE_NODE_POINTS || depth == MAX_OCTREE_DEPTH) {
for (i=0; i<node->totpoint; i++)
node->points[i]= *(refpoints[i]);
return;
}
subsize[0]= size[0]*0.5f;
subsize[1]= size[1]*0.5f;
subsize[2]= size[2]*0.5f;
node->split[0]= mid[0];
node->split[1]= mid[1];
node->split[2]= mid[2];
memset(nsize, 0, sizeof(nsize));
memset(noffset, 0, sizeof(noffset));
/* count points in subnodes */
for (i=0; i<node->totpoint; i++) {
index= SUBNODE_INDEX(refpoints[i]->co, node->split);
tmppoints[i]= refpoints[i];
nsize[index]++;
}
/* here we check if only one subnode is used. if this is the case, we don't
* create a new node, but rather call this function again, with different
* size and middle position for the same node. */
for (usedi=0, usednodes=0, i=0; i<8; i++) {
if (nsize[i]) {
usednodes++;
usedi = i;
}
if (i != 0)
noffset[i]= noffset[i-1]+nsize[i-1];
}
if (usednodes<=1) {
subnode_middle(usedi, mid, subsize, submid);
create_octree_node(tree, node, submid, subsize, refpoints, depth+1);
return;
}
/* reorder refpoints by subnode */
for (i=0; i<node->totpoint; i++) {
index= SUBNODE_INDEX(tmppoints[i]->co, node->split);
refpoints[noffset[index]]= tmppoints[i];
noffset[index]++;
}
/* create subnodes */
for (subco=0, i=0; i<8; subco+=nsize[i], i++) {
if (nsize[i] > 0) {
subnode= BLI_memarena_alloc(tree->arena, sizeof(ScatterNode));
node->child[i]= subnode;
subnode->points= node->points + subco;
subnode->totpoint= nsize[i];
subrefpoints= refpoints + subco;
subnode_middle(i, mid, subsize, submid);
create_octree_node(tree, subnode, submid, subsize, subrefpoints,
depth+1);
}
else
node->child[i]= NULL;
}
node->points= NULL;
node->totpoint= 0;
}
/* public functions */
ScatterTree *scatter_tree_new(ScatterSettings *ss[3], float scale, float error,
float (*co)[3], float (*color)[3], float *area, int totpoint)
{
ScatterTree *tree;
ScatterPoint *points, **refpoints;
int i;
/* allocate tree */
tree= MEM_callocN(sizeof(ScatterTree), "ScatterTree");
tree->scale= scale;
tree->error= error;
tree->totpoint= totpoint;
tree->ss[0]= ss[0];
tree->ss[1]= ss[1];
tree->ss[2]= ss[2];
points= MEM_callocN(sizeof(ScatterPoint)*totpoint, "ScatterPoints");
refpoints= MEM_callocN(sizeof(ScatterPoint*)*totpoint, "ScatterRefPoints");
tree->points= points;
tree->refpoints= refpoints;
/* build points */
INIT_MINMAX(tree->min, tree->max);
for (i=0; i<totpoint; i++) {
copy_v3_v3(points[i].co, co[i]);
copy_v3_v3(points[i].rad, color[i]);
points[i].area= fabsf(area[i])/(tree->scale*tree->scale);
points[i].back= (area[i] < 0.0f);
mul_v3_fl(points[i].co, 1.0f/tree->scale);
DO_MINMAX(points[i].co, tree->min, tree->max);
refpoints[i]= points + i;
}
return tree;
}
void scatter_tree_build(ScatterTree *tree)
{
ScatterPoint *newpoints, **tmppoints;
float mid[3], size[3];
int totpoint= tree->totpoint;
newpoints= MEM_callocN(sizeof(ScatterPoint)*totpoint, "ScatterPoints");
tmppoints= MEM_callocN(sizeof(ScatterPoint*)*totpoint, "ScatterTmpPoints");
tree->tmppoints= tmppoints;
tree->arena= BLI_memarena_new(0x8000 * sizeof(ScatterNode), "sss tree arena");
BLI_memarena_use_calloc(tree->arena);
/* build tree */
tree->root= BLI_memarena_alloc(tree->arena, sizeof(ScatterNode));
tree->root->points= newpoints;
tree->root->totpoint= totpoint;
mid[0]= (tree->min[0]+tree->max[0])*0.5f;
mid[1]= (tree->min[1]+tree->max[1])*0.5f;
mid[2]= (tree->min[2]+tree->max[2])*0.5f;
size[0]= (tree->max[0]-tree->min[0])*0.5f;
size[1]= (tree->max[1]-tree->min[1])*0.5f;
size[2]= (tree->max[2]-tree->min[2])*0.5f;
create_octree_node(tree, tree->root, mid, size, tree->refpoints, 0);
MEM_freeN(tree->points);
MEM_freeN(tree->refpoints);
MEM_freeN(tree->tmppoints);
tree->refpoints= NULL;
tree->tmppoints= NULL;
tree->points= newpoints;
/* sum radiance at nodes */
sum_radiance(tree, tree->root);
}
void scatter_tree_sample(ScatterTree *tree, float *co, float *color)
{
float sco[3];
copy_v3_v3(sco, co);
mul_v3_fl(sco, 1.0f/tree->scale);
compute_radiance(tree, sco, color);
}
void scatter_tree_free(ScatterTree *tree)
{
if (tree->arena) BLI_memarena_free(tree->arena);
if (tree->points) MEM_freeN(tree->points);
if (tree->refpoints) MEM_freeN(tree->refpoints);
MEM_freeN(tree);
}
/* Internal Renderer API */
/* sss tree building */
typedef struct SSSData {
ScatterTree *tree;
ScatterSettings *ss[3];
} SSSData;
typedef struct SSSPoints {
struct SSSPoints *next, *prev;
float (*co)[3];
float (*color)[3];
float *area;
int totpoint;
} SSSPoints;
static void sss_create_tree_mat(Render *re, Material *mat)
{
SSSPoints *p;
RenderResult *rr;
ListBase points;
float (*co)[3] = NULL, (*color)[3] = NULL, *area = NULL;
int totpoint = 0, osa, osaflag, partsdone;
if (re->test_break(re->tbh))
return;
points.first= points.last= NULL;
/* TODO: this is getting a bit ugly, copying all those variables and
* setting them back, maybe we need to create our own Render? */
/* do SSS preprocessing render */
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
rr= re->result;
osa= re->osa;
osaflag= re->r.mode & R_OSA;
partsdone= re->i.partsdone;
re->osa= 0;
re->r.mode &= ~R_OSA;
re->sss_points= &points;
re->sss_mat= mat;
re->i.partsdone= 0;
if (!(re->r.scemode & R_PREVIEWBUTS))
re->result= NULL;
BLI_rw_mutex_unlock(&re->resultmutex);
RE_TileProcessor(re);
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
if (!(re->r.scemode & R_PREVIEWBUTS)) {
RE_FreeRenderResult(re->result);
re->result= rr;
}
BLI_rw_mutex_unlock(&re->resultmutex);
re->i.partsdone= partsdone;
re->sss_mat= NULL;
re->sss_points= NULL;
re->osa= osa;
if (osaflag) re->r.mode |= R_OSA;
/* no points? no tree */
if (!points.first)
return;
/* merge points together into a single buffer */
if (!re->test_break(re->tbh)) {
for (totpoint=0, p=points.first; p; p=p->next)
totpoint += p->totpoint;
co= MEM_mallocN(sizeof(*co)*totpoint, "SSSCo");
color= MEM_mallocN(sizeof(*color)*totpoint, "SSSColor");
area= MEM_mallocN(sizeof(*area)*totpoint, "SSSArea");
for (totpoint=0, p=points.first; p; p=p->next) {
memcpy(co+totpoint, p->co, sizeof(*co)*p->totpoint);
memcpy(color+totpoint, p->color, sizeof(*color)*p->totpoint);
memcpy(area+totpoint, p->area, sizeof(*area)*p->totpoint);
totpoint += p->totpoint;
}
}
/* free points */
for (p=points.first; p; p=p->next) {
MEM_freeN(p->co);
MEM_freeN(p->color);
MEM_freeN(p->area);
}
BLI_freelistN(&points);
/* build tree */
if (!re->test_break(re->tbh)) {
SSSData *sss= MEM_callocN(sizeof(*sss), "SSSData");
float ior= mat->sss_ior, cfac= mat->sss_colfac;
float *radius= mat->sss_radius;
float fw= mat->sss_front, bw= mat->sss_back;
float error = mat->sss_error;
error= get_render_aosss_error(&re->r, error);
if ((re->r.scemode & R_PREVIEWBUTS) && error < 0.5f)
error= 0.5f;
sss->ss[0]= scatter_settings_new(mat->sss_col[0], radius[0], ior, cfac, fw, bw);
sss->ss[1]= scatter_settings_new(mat->sss_col[1], radius[1], ior, cfac, fw, bw);
sss->ss[2]= scatter_settings_new(mat->sss_col[2], radius[2], ior, cfac, fw, bw);
sss->tree= scatter_tree_new(sss->ss, mat->sss_scale, error,
co, color, area, totpoint);
MEM_freeN(co);
MEM_freeN(color);
MEM_freeN(area);
scatter_tree_build(sss->tree);
BLI_ghash_insert(re->sss_hash, mat, sss);
}
else {
if (co) MEM_freeN(co);
if (color) MEM_freeN(color);
if (area) MEM_freeN(area);
}
}
void sss_add_points(Render *re, float (*co)[3], float (*color)[3], float *area, int totpoint)
{
SSSPoints *p;
if (totpoint > 0) {
p= MEM_callocN(sizeof(SSSPoints), "SSSPoints");
p->co= co;
p->color= color;
p->area= area;
p->totpoint= totpoint;
BLI_lock_thread(LOCK_CUSTOM1);
BLI_addtail(re->sss_points, p);
BLI_unlock_thread(LOCK_CUSTOM1);
}
}
static void sss_free_tree(SSSData *sss)
{
scatter_tree_free(sss->tree);
scatter_settings_free(sss->ss[0]);
scatter_settings_free(sss->ss[1]);
scatter_settings_free(sss->ss[2]);
MEM_freeN(sss);
}
/* public functions */
void make_sss_tree(Render *re)
{
Material *mat;
re->sss_hash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "make_sss_tree gh");
re->i.infostr= "SSS preprocessing";
re->stats_draw(re->sdh, &re->i);
for (mat= re->main->mat.first; mat; mat= mat->id.next)
if (mat->id.us && (mat->flag & MA_IS_USED) && (mat->sss_flag & MA_DIFF_SSS))
sss_create_tree_mat(re, mat);
/* XXX preview exception */
/* localizing preview render data is not fun for node trees :( */
if (re->main!=G.main) {
for (mat= G.main->mat.first; mat; mat= mat->id.next)
if (mat->id.us && (mat->flag & MA_IS_USED) && (mat->sss_flag & MA_DIFF_SSS))
sss_create_tree_mat(re, mat);
}
}
void free_sss(Render *re)
{
if (re->sss_hash) {
GHashIterator *it= BLI_ghashIterator_new(re->sss_hash);
while (!BLI_ghashIterator_isDone(it)) {
sss_free_tree(BLI_ghashIterator_getValue(it));
BLI_ghashIterator_step(it);
}
BLI_ghashIterator_free(it);
BLI_ghash_free(re->sss_hash, NULL, NULL);
re->sss_hash= NULL;
}
}
int sample_sss(Render *re, Material *mat, float *co, float *color)
{
if (re->sss_hash) {
SSSData *sss= BLI_ghash_lookup(re->sss_hash, mat);
if (sss) {
scatter_tree_sample(sss->tree, co, color);
return 1;
}
else {
color[0]= 0.0f;
color[1]= 0.0f;
color[2]= 0.0f;
}
}
return 0;
}
int sss_pass_done(struct Render *re, struct Material *mat)
{
return ((re->flag & R_BAKING) || !(re->r.mode & R_SSS) || (re->sss_hash && BLI_ghash_lookup(re->sss_hash, mat)));
}
|