-
Notifications
You must be signed in to change notification settings - Fork 34
/
AmbShadowCalcThreadN.pas
1492 lines (1439 loc) · 50.8 KB
/
AmbShadowCalcThreadN.pas
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
unit AmbShadowCalcThreadN;
interface
uses
Classes, TypeDefinitions, Windows;
type
TAmbShadowCalc = class(TThread)
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbShadowCalcT0 = class(TThread)
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
{ TAmbShadowCalcPano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbShadowCalcT0Pano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end; }
function BuildATlevels(PsiLight, MWidth, MHeight: Integer; PATlevel: TPATlevel; var CorrMul: Single; var Zsub: Integer): Integer;
//function BuildATlevelsT0(PsiLight, MWidth, MHeight: Integer; PATlevel: TPATlevel; sZRT: Single): Integer;
//var RoughStat: array[0..255] of Integer;
implementation
uses Math, Mand, DivUtils, ImageProcess, SysUtils, Forms;
function BuildATlevels(PsiLight, MWidth, MHeight: Integer; PATlevel: TPATlevel; var CorrMul: Single; var Zsub: Integer): Integer;
var x, y, x2, zp, za: Integer;
PATL, PATL2: PWord;
ATl, iStep, iStep2, MWidth2step: Integer;
W4tmp, W4tmp2: array[0..3] of Word;
s1: Single;
begin
try
Result := 1;
x := MWidth div 16;
repeat
Inc(Result);
x := x shr 1;
until (Result = 8) or (x < 4);
za := Result;
for x := 1 to Result + 1 do
try
SetLength(PATlevel[x], MWidth * MHeight);
except
Result := x - 2;
Break;
end;
if Result < 1 then Exit;
if Result < za then Mand3DForm.OutMessage('Only using ' + IntToStr(Result) +
' out of ' + IntToStr(za) + ' wavelet levels due to memory limitation.');
PATL := PWord(PsiLight + 8); //Zpos=+8 ZposfineByte=+7
za := 32767;
zp := 0;
for x := 1 to MWidth * MHeight do
begin
if PATL^ < 32768 then
begin
if PATL^ > zp then zp := PATL^;
if PATL^ < za then za := PATL^;
end;
Inc(PATL, 9);
end;
if zp < za then
begin
zp := 32768;
za := 0;
end
else Inc(zp);
s1 := 128 / (zp - za);
za := za shl 8;
Zsub := za;
CorrMul := s1; //1 / (s1 * 256);
PATL := PWord(PsiLight + 8); // Zpos=+8 ZposfineByte=+7
PATL2 := @PATlevel[1][0];
for x := 1 to MWidth * MHeight do
begin
if PATL^ < 32768 then
PATL2^ := Round(((PInteger(Integer(PATL) - 2)^ shr 8) - za) * s1)
else PATL2^ := 0;
Inc(PATL, 9);
Inc(PATL2);
end;
for y := 1 to MHeight - 2 do //Median 3x3 midWeight=4 to blur out single false pixel
begin
PATL := @PATlevel[1][y * MWidth + 1];
for x := 1 to MWidth - 2 do
begin
za := 0;
zp := PATL^;
PATL2 := PWord(Integer(PATL) - MWidth * 2 - 2);
for x2 := 0 to 7 do
begin
if PATL2^ >= zp then
begin
za := 0;
Break;
end
else if PATL2^ > za then za := PATL2^;
if (x2 = 2) or (x2 = 4) then Inc(PATL2, MWidth - 2) else
if x2 = 3 then Inc(PATL2, 2) else Inc(PATL2);
end;
if za > 0 then PATL^ := za + 1;
Inc(PATL);
end;
end;
iStep := 1;
for ATl := 2 to Result do
begin
iStep2 := iStep * 2;
MWidth2step := MWidth * iStep2;
if SupportMMX then
asm
emms
end; //fp stack check error.. mmx used + fp in fastmove!
FastMove(PATlevel[ATl - 1][0], PATlevel[ATl][0], MWidth * MHeight * 2); //in anipreview
for y := 0 to MHeight - 1 do // first smooth in x direction
begin
Application.ProcessMessages; //new
x2 := y * MWidth;
PATL := @PATlevel[ATl + 1][x2];
PATL2 := @PATlevel[ATl - 1][x2];
zp := PATL2^ + 1;
for x := 1 to iStep do
begin
PATL^ := (PATL2^ + 1 + (zp + PWord(Integer(PATL2) + iStep2)^) shr 1) shr 1;
Inc(PATL);
Inc(PATL2);
end;
x2 := iStep2 + 1;
if SupportMMX and (MWidth - iStep2 > 3) then
asm
push eax
push ebx
push ecx
push esi
push edi
mov ebx, iStep2
mov ecx, MWidth
mov esi, PATL2
sub ecx, ebx
mov edi, PATL
shr ecx, 2
sub esi, ebx
mov eax, ecx
sub edi, esi
shl eax, 2
add x2, eax
@ll: movq mm0, [esi] // calculate 4 words at once
pavgw mm0, [esi + ebx * 2]
pavgw mm0, [esi + ebx]
movq [edi + esi], mm0
add esi, 8
dec ecx
jnz @ll
add edi, esi
mov PATL, edi
add esi, ebx
mov PATL2, esi
pop edi
pop esi
pop ecx
pop ebx
pop eax
end;
for x := x2 to MWidth do
begin
PATL^ := (PATL2^ + 1 + (PWord(Integer(PATL2) - iStep2)^ +
PWord(Integer(PATL2) + iStep2)^ + 1) shr 1) shr 1;
Inc(PATL);
Inc(PATL2);
end;
zp := PWord(Integer(PATL2) + (iStep - 1) * 2)^;
for x := 1 to iStep do
begin
PATL^ := (PATL2^ + 1 + (PWord(Integer(PATL2) - iStep2)^ + zp) shr 1) shr 1;
Inc(PATL);
Inc(PATL2);
end;
end;
// then smooth in y direction
x2 := 0;
while SupportMMX and (x2 < MWidth - 3) do
begin
PATL2 := @PATlevel[ATl + 1][x2];
PATL := PATL2;
for x := 0 to 3 do
begin
W4tmp[x] := PATL^;
Inc(PATL);
end;
PATL := @PATlevel[ATl + 1][x2 + (MHeight - 1) * MWidth];
for x := 0 to 3 do
begin
W4tmp2[x] := PATL^;
Inc(PATL);
end;
PATL := @PATlevel[ATl][x2];
asm
push eax
push ebx
push ecx
push edx
push esi
push edi
movq mm1, W4tmp
mov ebx, MWidth2step
mov esi, PATL2
mov edi, PATL
mov ecx, iStep
mov edx, MWidth
sub esi, ebx
add edx, edx
sub edi, esi
dec ecx
@l1: movq mm0, [esi + ebx * 2]
pavgw mm0, mm1
pavgw mm0, [esi + ebx]
movq [edi + esi], mm0
add esi, edx
dec ecx
jns @l1
mov ecx, MHeight
sub ecx, iStep2
dec ecx
js @u2
@l2: movq mm0, [esi]
pavgw mm0, [esi + ebx * 2]
pavgw mm0, [esi + ebx]
movq [edi + esi], mm0
add esi, edx
dec ecx
jns @l2
@u2:
movq mm1, W4tmp2
mov ecx, iStep
dec ecx
@l3: movq mm0, [esi]
pavgw mm0, mm1
pavgw mm0, [esi + ebx]
movq [edi + esi], mm0
add esi, edx
dec ecx
jns @l3
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
end;
Inc(x2, 4);
end;
Application.ProcessMessages; //new
for x := x2 to MWidth - 1 do
begin
PATL := @PATlevel[ATl][x];
PATL2 := @PATlevel[ATl + 1][x];
zp := PATL2^ + 1;
for y := 1 to iStep do
begin
PATL^ := (PATL2^ + (zp + PWord(Integer(PATL2) + MWidth2step + 1)^) shr 1) shr 1;
Inc(PATL, MWidth);
Inc(PATL2, MWidth);
end;
for y := 1 to MHeight - iStep2 do
begin
PATL^ := (PATL2^ + 1 + (PWord(Integer(PATL2) - MWidth2step)^ +
PWord(Integer(PATL2) + MWidth2step)^ + 1) shr 1) shr 1;
Inc(PATL, MWidth);
Inc(PATL2, MWidth);
end;
zp := PATlevel[ATl + 1][x + (MHeight - 1) * MWidth] + 1;
for y := 1 to iStep do
begin
PATL^ := (PATL2^ + 1 + (PWord(Integer(PATL2) - MWidth2step)^ + zp) shr 1) shr 1;
Inc(PATL, MWidth);
Inc(PATL2, MWidth);
end;
end;
iStep := iStep * 2;
end;
if SupportMMX then
asm
emms
end;
except
Result := 0;
end;
end;
function fastIntArcTan2(y, x: Integer): Integer; // intAngle 0..31
begin
if x = 0 then
Result := Integer(y >= 0) * 16 + 8
else
if y = 0 then
Result := Integer(x >= 0) * 16
else
if y < 0 then
begin
if x < 0 then
begin
if x >= y then Result := 7 - (x * 4) div y
else Result := (y * 4) div x;
end
else
begin
if -y < x then Result := 15 + (y * 4) div x
else Result := 8 - (x * 4) div y;
end;
end
else
begin
if x >= 0 then
begin
if x > y then Result := 16 + (y * 4) div x
else Result := 23 - (x * 4) div y;
end
else
begin
if y < -x then Result := 31 + (y * 4) div x
else Result := 24 - (x * 4) div y;
end;
end;
end;
procedure TAmbShadowCalc.Execute;
var x, y, x2, y2, ya, xa, xe, ye, xa2, xa2t, iRad: Integer;
zp4: array[0..3] of Integer;
PATL, PATL2: PWord;
PS, PS2: PSingle;
iMaxRad, iMinRad, iStep, iStep2, xt, ya2, id4c, x3, id4L: Integer;
MultiC, do4, do4Lev: Boolean;
ATl, iMinRadS, iMaxRadS, iAngC, MWidth, RadS, iC, ii, iLevNo: Integer;
st, st2, R1d, RM, sZRT, sMul, sZRTLev: Single;
PsiLight, PL2: TPsiLight5;
AngMaxArr4: array[0..35, 0..3] of Single;
begin
try
x := 8064;
if supportSSE then
asm
stmxcsr x //set roundingmode sse
end; // if i<>$1f80 then i:=0; //=8064 }
//test
// for x := 0 to 255 do RoughStat[x] := 0;
if ASCpar.aZRThreshold < 0.01 then ASCpar.aZRThreshold := 0.01;
MWidth := ASCpar.aWidth;
st2 := ASCpar.aZScaleFactor / (ASCpar.aCorrMul * 256);
y := ASCpar.aYStart;
sZRT := ASCpar.aZRThreshold / st2;
// sMul := 1.25 * 32767 / (Pi * 32 * Sqrt(ArcTan(ASCpar.aZRThreshold * Sqrt(ASCpar.aATlevelCount)))); //pi->16383.. ArcTan(ASCpar.aZRThreshold * Sqrt(ASCpar.aATlevelCount)) = max angle
sMul := 1.35 * 32767 / (Pi * 32 * Power(ArcTan(ASCpar.aZRThreshold * Sqrt(Sqrt(ASCpar.aATlevelCount))), 0.8));
while y < ASCpar.aHeight do
begin
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos := y;
PsiLight := ASCpar.aPsiLight;
x := 0;
Inc(PsiLight, y * MWidth);
while x < MWidth do
begin
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualXpos := x;
if PsiLight.Zpos < 32768 then zp4[0] := Round(((PInteger(@PsiLight.RoughZposFine)^ shr 8) - ASCpar.aZsub) * ASCpar.aCorrMul)
else zp4[0] := 32768;
do4 := (x < MWidth - 3);
if do4 then
begin
PL2 := PsiLight;
for x2 := 1 to 3 do
begin
Inc(PL2);
if PL2.Zpos < 32768 then zp4[x2] := Round(((PInteger(@PL2.RoughZposFine)^ shr 8) - ASCpar.aZsub) * ASCpar.aCorrMul)
else zp4[x2] := 32768;
end;
id4c := 3;
end
else id4c := 0;
if (zp4[0] < 32768) or (do4 and (Integer(zp4[1]) + zp4[2] + zp4[3] < 98304)) then
begin
for x2 := 0 to 35 do AngMaxArr4[x2, 0] := -1e10;
if do4 then
for x2 := 0 to 35 do
begin
AngMaxArr4[x2, 1] := -1e10;
AngMaxArr4[x2, 2] := -1e10;
AngMaxArr4[x2, 3] := -1e10;
end;
iMaxRad := 0;
iMinRad := 0;
for ATl := 1 to ASCpar.aATlevelCount do
begin
iStep := 1 shl (ATl - 1);
RM := Sqrt(iStep) * 5;
iMaxRad := iMaxRad + 4 * iStep;
iMaxRadS := iMaxRad * iMaxRad;
iMinRadS := iMinRad * iMinRad;
MultiC := Round(RM / (iMinRad + 1)) > 0;
sZRTLev := sZRT * Sqrt(Sqrt(ASCpar.aATlevelCount / ATl)); //new: THreshold depends on level -> less threshold in nearer parts
ya := - iMaxRad;
if ya + y < 0 then
begin
ya2 := ya;
while ya2 + y < 0 do Inc(ya2, iStep);
if ya2 + y >= ASCpar.aHeight then ya2 := ASCpar.aHeight - y - 1;
ya := - y;
if ya = ya2 then ya2 := $FFFFFF;
end
else ya2 := $FFFFFF;
if y + iMaxRad >= ASCpar.aHeight then ye := ASCpar.aHeight - y - 1
else ye := iMaxRad;
do4Lev := do4 and (x >= iMaxRad) and (x < MWidth - iMaxRad - 3);
if do4Lev then id4L := id4c else id4L := 0;
xa := - iMaxRad;
xa2 := $FFFFFF;
xe := iMaxRad;
y2 := ya;
repeat
if y2 > ya2 then
begin
y2 := ya2;
ya2 := $FFFFFF;
end;
iLevNo := 0;
repeat
if not do4Lev then
begin
x2 := x + iLevNo;
xa := - iMaxRad;
if xa + x2 < 0 then
begin
xa2 := xa;
while xa2 + x2 < 0 do Inc(xa2, iStep);
if xa2 + x2 >= MWidth then xa2 := MWidth - x2 - 1;
xa := - x2;
if xa = xa2 then xa2 := $FFFFFF;
end
else xa2 := $FFFFFF;
if x2 + iMaxRad >= MWidth then xe := MWidth - x2 - 1
else xe := iMaxRad;
end;
PATL := @ASCpar.PATlevel[ATl][x + xa + iLevNo + (y + y2) * MWidth];
x2 := xa;
xa2t := xa2;
repeat
if (x2 > xa2t) then
begin
Inc(PATL, xa2t - x2);
x2 := xa2t;
xa2t := $FFFFFF;
end;
RadS := y2 * y2 + x2 * x2;
if (RadS > iMinRadS) and (RadS <= iMaxRadS) then
begin
iAngC := fastIntArcTan2(y2, x2);
if MultiC then
begin
if (id4L = 3) and supportSSE then
asm
push eax
push ebx
push ecx
push esi
push edi
movss xmm7, RM
mov esi, PATL
lea edi, zp4
cvtsi2ss xmm4, RadS
movss xmm5, sZRTLev //sZRT
rsqrtss xmm4, xmm4
movzx eax, word [esi]
movzx ebx, word [esi + 2]
mulss xmm7, xmm4
sub eax, [edi]
sub ebx, [edi + 4]
cvtss2si ecx, xmm7 //iC
shufps xmm4, xmm4, 0 //R1d
cvtsi2ss xmm0, eax
cvtsi2ss xmm1, ebx
movzx eax, word [esi + 4]
movzx ebx, word [esi + 6]
sub eax, [edi + 8]
sub ebx, [edi + 12]
cvtsi2ss xmm2, eax
cvtsi2ss xmm3, ebx
shufps xmm0, xmm1, 0
shufps xmm2, xmm3, 0
shufps xmm5, xmm5, 0
shufps xmm0, xmm2, $88
mov eax, iAngC
mov ebx, ecx
shr ebx, 1
sub eax, ebx
and eax, 31
add eax, eax
mulps xmm0, xmm4
lea esi, [AngMaxArr4 + eax * 8]
minps xmm0, xmm5
@ll: movups xmm1, [esi]
maxps xmm1, xmm0
movups [esi], xmm1
add esi, 16
dec ecx
jns @ll
pop edi
pop esi
pop ecx
pop ebx
pop eax
end
else
begin
R1d := 1 / Sqrt(RadS);
iC := Round(RM * R1d);
PATL2 := PATL;
PS2 := @AngMaxArr4[(iAngC - (iC shr 1)) and 31, iLevNo];
for x3 := 0 to id4L do
begin
PS := PS2;
st := (PATL2^ - zp4[x3 + iLevNo]) * R1d;
if st > sZRTLev then st := sZRTLev;
for ii := 0 to iC do
begin
if PS^ < st then PS^ := st;
Inc(PS, 4);
end;
Inc(PATL2);
Inc(PS2);
end;
end;
end
else
begin
if (id4L = 3) and supportSSE then
asm
push eax
push ebx
push esi
push edi
mov esi, PATL
lea edi, zp4
cvtsi2ss xmm4, RadS
movzx eax, word [esi]
movzx ebx, word [esi + 2]
sub eax, [edi]
sub ebx, [edi + 4]
shufps xmm4, xmm4, 0
movss xmm5, sZRTLev
cvtsi2ss xmm0, eax
cvtsi2ss xmm1, ebx
rsqrtps xmm4, xmm4 //only 4..6 clocks, not slower than scalar
movzx eax, word [esi + 4]
movzx ebx, word [esi + 6]
sub eax, [edi + 8]
sub ebx, [edi + 12]
cvtsi2ss xmm2, eax
cvtsi2ss xmm3, ebx
shufps xmm5, xmm5, 0
shufps xmm0, xmm1, 0
shufps xmm2, xmm3, 0
mov eax, iAngC
shufps xmm0, xmm2, $88
add eax, eax
mulps xmm0, xmm4
movups xmm1, dqword [AngMaxArr4 + eax * 8]
minps xmm0, xmm5
maxps xmm1, xmm0
movups dqword [AngMaxArr4 + eax * 8], xmm1
pop edi
pop esi
pop ebx
pop eax
end
else
begin
PATL2 := PATL;
R1d := 1 / Sqrt(RadS);
for x3 := iLevNo to iLevNo + id4L do
begin
st := (PATL2^ - zp4[x3]) * R1d;
if st > sZRTLev then st := sZRTLev;
if AngMaxArr4[iAngC, x3] < st then
AngMaxArr4[iAngC, x3] := st;
Inc(PATL2);
end;
end;
end;
end;
xt := x2;
Inc(x2, iStep);
if (x2 > xe) and (xt < xe) then x2 := xe;
Inc(PATL, x2 - xt);
until x2 > xe;
Inc(iLevNo);
until do4Lev or (iLevNo > id4c);
Inc(y2, iStep);
if (y2 > ye) and ((y2 - iStep) < ye) then y2 := ye;
until y2 > ye;
iMinRad := iMaxRad;
end;
PL2 := PsiLight;
for x3 := 0 to id4c do
begin
for x2 := 0 to 3 do
if AngMaxArr4[x2 + 32, x3] > AngMaxArr4[x2, x3] then
AngMaxArr4[x2, x3] := AngMaxArr4[x2 + 32, x3];
st := 0;
for x2 := 0 to 31 do
if AngMaxArr4[x2, x3] > -1e9 then
st := st + ArcTan(AngMaxArr4[x2, x3] * st2);
PL2^.AmbShadow := Max(0, Min(16383{32767}, Round(st * sMul) {+ 16383})); //10000?
//Test
// Inc(RoughStat[PL2^.AmbShadow shr 8]);
Inc(PL2);
end;
end;
if do4 then
begin
Inc(PsiLight, 4);
Inc(x, 4);
end else begin
Inc(PsiLight);
Inc(x);
end;
if ASCpar.aPCTS.pLBcalcStop^ then Break;
end;
if ASCpar.aPCTS.pLBcalcStop^ then Break;
Inc(y, ASCpar.aYstep);
end;
finally
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].isActive := 0;
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos := ASCpar.aHeight - 1;
PostMessage(ASCpar.aPCTS.pMessageHwnd, WM_ThreadReady, 0, 1);
end;
end;
{
procedure TAmbShadowCalcPano.Execute;
var x, y, x2, y2, ya, xa, xe, ye, xa2, xa2t, iRad: Integer;
zp4: array[0..3] of Integer;
PATL, PATL2: PWord;
PS, PS2: PSingle;
iMaxRad, iMinRad, iStep, iStep2, xt, ya2, id4c, x3, id4L: Integer;
MultiC, do4, do4Lev: Boolean;
ATl, iMinRadS, iMaxRadS, iAngC, MWidth, RadS, iC, ii, iLevNo: Integer;
st, st2, R1d, RM, sZRT, sMul, sZRTLev: Single;
PsiLight, PL2: TPsiLight5;
AngMaxArr4: array[0..35, 0..3] of Single;
begin
try
x := 8064;
if supportSSE then
asm
stmxcsr x //set roundingmode sse
end;
if ASCpar.aZRThreshold < 0.01 then ASCpar.aZRThreshold := 0.01;
MWidth := ASCpar.aWidth;
st2 := ASCpar.aZScaleFactor / (ASCpar.aCorrMul * 256);
y := ASCpar.aYStart;
sZRT := ASCpar.aZRThreshold / st2;
sMul := 1.35 * 32767 / (Pi * 32 * Power(ArcTan(ASCpar.aZRThreshold * Sqrt(Sqrt(ASCpar.aATlevelCount))), 0.8));
while y < ASCpar.aHeight do
begin
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos := y;
PsiLight := ASCpar.aPsiLight;
x := 0;
Inc(PsiLight, y * MWidth);
while x < MWidth do
begin
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualXpos := x;
if PsiLight.Zpos < 32768 then zp4[0] := Round(((PInteger(@PsiLight.RoughZposFine)^ shr 8) - ASCpar.aZsub) * ASCpar.aCorrMul)
else zp4[0] := 32768;
do4 := (x < MWidth - 3);
if do4 then
begin
PL2 := PsiLight;
for x2 := 1 to 3 do
begin
Inc(PL2);
if PL2.Zpos < 32768 then zp4[x2] := Round(((PInteger(@PL2.RoughZposFine)^ shr 8) - ASCpar.aZsub) * ASCpar.aCorrMul)
else zp4[x2] := 32768;
end;
id4c := 3;
end
else id4c := 0;
if (zp4[0] < 32768) or (do4 and (Integer(zp4[1]) + zp4[2] + zp4[3] < 98304)) then
begin
for x2 := 0 to 35 do AngMaxArr4[x2, 0] := -1e10;
if do4 then
for x2 := 0 to 35 do
begin
AngMaxArr4[x2, 1] := -1e10;
AngMaxArr4[x2, 2] := -1e10;
AngMaxArr4[x2, 3] := -1e10;
end;
iMaxRad := 0;
iMinRad := 0;
for ATl := 1 to ASCpar.aATlevelCount do
begin
iStep := 1 shl (ATl - 1);
RM := Sqrt(iStep) * 5;
iMaxRad := iMaxRad + 4 * iStep;
iMaxRadS := iMaxRad * iMaxRad;
iMinRadS := iMinRad * iMinRad;
MultiC := Round(RM / (iMinRad + 1)) > 0;
sZRTLev := sZRT * Sqrt(Sqrt(ASCpar.aATlevelCount / ATl)); //new: THreshold depends on level -> less threshold in nearer parts
ya := - iMaxRad;
if ya + y < 0 then
begin
ya2 := ya;
while ya2 + y < 0 do Inc(ya2, iStep);
if ya2 + y >= ASCpar.aHeight then ya2 := ASCpar.aHeight - y - 1;
ya := - y;
if ya = ya2 then ya2 := $FFFFFF;
end
else ya2 := $FFFFFF;
if y + iMaxRad >= ASCpar.aHeight then ye := ASCpar.aHeight - y - 1
else ye := iMaxRad;
do4Lev := do4 and (x >= iMaxRad) and (x < MWidth - iMaxRad - 3);
if do4Lev then id4L := id4c else id4L := 0;
xa := - iMaxRad;
xa2 := $FFFFFF;
xe := iMaxRad;
y2 := ya;
repeat
if y2 > ya2 then
begin
y2 := ya2;
ya2 := $FFFFFF;
end;
iLevNo := 0;
repeat
if not do4Lev then
begin
x2 := x + iLevNo;
xa := - iMaxRad;
if xa + x2 < 0 then
begin
xa2 := xa;
while xa2 + x2 < 0 do Inc(xa2, iStep);
if xa2 + x2 >= MWidth then xa2 := MWidth - x2 - 1;
xa := - x2;
if xa = xa2 then xa2 := $FFFFFF;
end
else xa2 := $FFFFFF;
if x2 + iMaxRad >= MWidth then xe := MWidth - x2 - 1
else xe := iMaxRad;
end;
PATL := @ASCpar.PATlevel[ATl][x + xa + iLevNo + (y + y2) * MWidth];
x2 := xa;
xa2t := xa2;
repeat
if (x2 > xa2t) then
begin
Inc(PATL, xa2t - x2);
x2 := xa2t;
xa2t := $FFFFFF;
end;
RadS := y2 * y2 + x2 * x2;
if (RadS > iMinRadS) and (RadS <= iMaxRadS) then
begin
iAngC := fastIntArcTan2(y2, x2);
if MultiC then
begin
if (id4L = 3) and supportSSE then
asm
push eax
push ebx
push ecx
push esi
push edi
movss xmm7, RM
mov esi, PATL
lea edi, zp4
cvtsi2ss xmm4, RadS
movss xmm5, sZRTLev //sZRT
rsqrtss xmm4, xmm4
movzx eax, word [esi]
movzx ebx, word [esi + 2]
mulss xmm7, xmm4
sub eax, [edi]
sub ebx, [edi + 4]
cvtss2si ecx, xmm7 //iC
shufps xmm4, xmm4, 0 //R1d
cvtsi2ss xmm0, eax
cvtsi2ss xmm1, ebx
movzx eax, word [esi + 4]
movzx ebx, word [esi + 6]
sub eax, [edi + 8]
sub ebx, [edi + 12]
cvtsi2ss xmm2, eax
cvtsi2ss xmm3, ebx
shufps xmm0, xmm1, 0
shufps xmm2, xmm3, 0
shufps xmm5, xmm5, 0
shufps xmm0, xmm2, $88
mov eax, iAngC
mov ebx, ecx
shr ebx, 1
sub eax, ebx
and eax, 31
add eax, eax
mulps xmm0, xmm4
lea esi, [AngMaxArr4 + eax * 8]
minps xmm0, xmm5
@ll: movups xmm1, [esi]
maxps xmm1, xmm0
movups [esi], xmm1
add esi, 16
dec ecx
jns @ll
pop edi
pop esi
pop ecx
pop ebx
pop eax
end
else
begin
R1d := 1 / Sqrt(RadS);
iC := Round(RM * R1d);
PATL2 := PATL;
PS2 := @AngMaxArr4[(iAngC - (iC shr 1)) and 31, iLevNo];
for x3 := 0 to id4L do
begin
PS := PS2;
st := (PATL2^ - zp4[x3 + iLevNo]) * R1d;
if st > sZRTLev then st := sZRTLev;
for ii := 0 to iC do
begin
if PS^ < st then PS^ := st;
Inc(PS, 4);
end;
Inc(PATL2);
Inc(PS2);
end;
end;
end
else
begin
if (id4L = 3) and supportSSE then
asm
push eax
push ebx
push esi
push edi
mov esi, PATL
lea edi, zp4
cvtsi2ss xmm4, RadS
movzx eax, word [esi]
movzx ebx, word [esi + 2]
sub eax, [edi]
sub ebx, [edi + 4]
shufps xmm4, xmm4, 0
movss xmm5, sZRTLev
cvtsi2ss xmm0, eax
cvtsi2ss xmm1, ebx
rsqrtps xmm4, xmm4 //only 4..6 clocks, not slower than scalar
movzx eax, word [esi + 4]
movzx ebx, word [esi + 6]
sub eax, [edi + 8]
sub ebx, [edi + 12]
cvtsi2ss xmm2, eax
cvtsi2ss xmm3, ebx
shufps xmm5, xmm5, 0
shufps xmm0, xmm1, 0
shufps xmm2, xmm3, 0
mov eax, iAngC
shufps xmm0, xmm2, $88
add eax, eax
mulps xmm0, xmm4
movups xmm1, dqword [AngMaxArr4 + eax * 8]
minps xmm0, xmm5
maxps xmm1, xmm0
movups dqword [AngMaxArr4 + eax * 8], xmm1
pop edi
pop esi
pop ebx
pop eax
end
else
begin
PATL2 := PATL;
R1d := 1 / Sqrt(RadS);
for x3 := iLevNo to iLevNo + id4L do
begin
st := (PATL2^ - zp4[x3]) * R1d;
if st > sZRTLev then st := sZRTLev;
if AngMaxArr4[iAngC, x3] < st then
AngMaxArr4[iAngC, x3] := st;
Inc(PATL2);
end;
end;
end;
end;
xt := x2;
Inc(x2, iStep);
if (x2 > xe) and (xt < xe) then x2 := xe;
Inc(PATL, x2 - xt);
until x2 > xe;
Inc(iLevNo);
until do4Lev or (iLevNo > id4c);
Inc(y2, iStep);
if (y2 > ye) and ((y2 - iStep) < ye) then y2 := ye;
until y2 > ye;
iMinRad := iMaxRad;
end;
PL2 := PsiLight;
for x3 := 0 to id4c do
begin
for x2 := 0 to 3 do
if AngMaxArr4[x2 + 32, x3] > AngMaxArr4[x2, x3] then
AngMaxArr4[x2, x3] := AngMaxArr4[x2 + 32, x3];
st := 0;
for x2 := 0 to 31 do
if AngMaxArr4[x2, x3] > -1e9 then
st := st + ArcTan(AngMaxArr4[x2, x3] * st2);
PL2^.AmbShadow := Max(0, Min(16383, Round(st * sMul))); //10000?
Inc(PL2);
end;
end;
if do4 then
begin
Inc(PsiLight, 4);
Inc(x, 4);
end else begin
Inc(PsiLight);
Inc(x);
end;
if ASCpar.aPCTS.pLBcalcStop^ then Break;
end;
if ASCpar.aPCTS.pLBcalcStop^ then Break;
Inc(y, ASCpar.aYstep);
end;
finally
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].isActive := 0;
ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos := ASCpar.aHeight - 1;
PostMessage(ASCpar.aPCTS.pMessageHwnd, WM_ThreadReady, 0, 1);
end;
end; }
//not used:
function BuildATlevelsT0(PsiLight, MWidth, MHeight: Integer; PATlevel: TPATlevel; sZRT: Single): Integer;
var x, y, x2, xa, iwids: Integer;
PATL, PATL2: PWord;
ATl, iStep, iStep2, MWidth2step: Integer;
it1, it2, it3, iTh: Integer;
iTh4, sub32k: array[0..3] of Word;
begin
sub32k[0] := 32768;
sub32k[1] := 32768;
sub32k[2] := 32768;
sub32k[3] := 32768;