-
Notifications
You must be signed in to change notification settings - Fork 34
/
AmbHiQ.pas
2651 lines (2598 loc) · 92.1 KB
/
AmbHiQ.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 AmbHiQ; //24bit SSAO
interface
uses
Classes, TypeDefinitions, Windows;
type
TAmbHiQCalc = class(TThread)
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcPano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcT0 = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcT0pano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcR = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcRpano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcRT0 = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbHiQCalcRT0pano = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
TAmbVideo = class(TThread)
private
public
ASCpar: TASCparameter;
protected
procedure Execute; override;
end;
procedure FirstATlevelHiQ(PIA: TPCardinalArray; PsiLight: TPsiLight5; Leng: Integer);
procedure NextATlevelHiQ(PIA: TPCardinalArray; Wid, Hei, Step: Integer);
function CalcAmbShadowTHiQ(Header: TPMandHeader10; PsiLight: TPsiLight5;
PCTS: TPCalcThreadStats; PATlevel: TPATlevel): Boolean;
var ATlevelHiQ: array of Cardinal;
ATmaxArrHiQ: TAngMaxArrSI; // = array of array[0..31] of Smallint;
ATdirSteps: array[0..31] of array[0..1] of Single;
const d00002441406: Double = 0.0002441406;
implementation
uses Math, Mand, Math3D, DivUtils, ImageProcess, SysUtils, Forms;
procedure DelayATRet(ThreadCount: Integer; PCTS: TPCalcThreadStats);
var Tick: Double;
x, y, Milliseconds: Integer;
begin
Milliseconds := 300;
Tick := getHiQmillis + Milliseconds;
while (Milliseconds > 0) and
(MsgWaitForMultipleObjects(0, Pointer(nil)^, False, Milliseconds, QS_ALLINPUT)
<> WAIT_TIMEOUT) do
begin
Application.ProcessMessages;
if Application.Terminated or PCTS.pLBcalcStop^ then Exit;
x := 0;
for y := 1 to ThreadCount do if PCTS.CTrecords[y].isActive > 0 then Inc(x);
if x = 0 then Milliseconds := 0 else Milliseconds := Round(Tick - getHiQmillis);
if Milliseconds > 500 then Exit;
end;
end;
{procedure Delay(Milliseconds: Integer);
var
Tick: Double;
begin
Tick := getHiQmillis + Milliseconds;
try
while (Milliseconds > 0) and
(MsgWaitForMultipleObjects(0, Pointer(nil)^, False, Milliseconds, QS_ALLINPUT)
<> WAIT_TIMEOUT) do
begin
Application.ProcessMessages;
if Application.Terminated then Exit;
Milliseconds := Round(Tick - getHiQmillis); //timeGetTime
if Milliseconds > 5000 then Exit;
end;
finally
end;
end;
}
function CalcAmbShadowTHiQ(Header: TPMandHeader10; PsiLight: TPsiLight5;
PCTS: TPCalcThreadStats; PATlevel: TPATlevel): Boolean;
var x, y, MWidth, MHeight, ThreadCount, ymin, RowCount, ATi: Integer;
IsPano: LongBool;
ASCparameter: TASCparameter;
Zcorr, ZcMul, ZZstmitDif: Double;
pc: PCardinal;
ascThread: array of TAmbHiQCalc;
ascThreadT0: array of TAmbHiQCalcT0;
ascThreadR: array of TAmbHiQCalcR;
ascThreadRT0: array of TAmbHiQCalcRT0;
ascThreadPano: array of TAmbHiQCalcPano;
ascThreadT0Pano: array of TAmbHiQCalcT0Pano;
ascThreadRPano: array of TAmbHiQCalcRPano;
ascThreadRT0Pano: array of TAmbHiQCalcRT0Pano;
ascThreadVideo: array of TAmbVideo;
begin
MWidth := Header.Width;
MHeight := Header.Height;
ATi := (Header.bCalcAmbShadowAutomatic shr 1) and 7; //2: Calc 3: CalcT0 4: CalcR 5: CalcRT0 6(->10): SSAOvideo
Result := False; //(6 would be DEAO)
IsPano := Header.bPlanarOptic = 2;
if not (ATi in [2,3,4,5,6]) then Exit;
CalcPPZvals(Header^, Zcorr, ZcMul, ZZstmitDif);
ASCparameter.aZScaleFactor := (Sqr(256 / ZcMul + 1) - 1) / Zcorr;
ASCparameter.aPsiLight := PsiLight;
ASCparameter.aZRThreshold := Header.sAmbShadowThreshold;
ASCparameter.aBorderMirrorSize := MinCS(Header.bSSAO24BorderMirrorSize * 0.01, 0.9);
ymin := Round(Sqrt(Sqr(MWidth) + Sqr(MHeight)) * 0.5);
// ATi := 6; //TEST!!!!!!!!
y := 1;
x := 5;
repeat
Inc(y);
x := x shl 1;
until (y = 15) or (x > ymin);
ASCparameter.aATlevelCount := y;
try
if ATi <> 6 then SetLength(ATlevelHiQ, MWidth * MHeight);
RowCount := Max(1, Min(PhysikMemAvail div (MWidth * 32 * 4), MHeight));
ymin := ((MHeight - 1) div RowCount) + 1; //blockcount
RowCount := Min(MHeight, (MHeight + ymin) div ymin);
ymin := 1;
if ATi = 6 then
begin
RowCount := MHeight;
ASCparameter.aATlevelCount := 1;
end
else
repeat
try
SetLength(ATmaxArrHiQ, MWidth * RowCount);
ymin := 0;
except
RowCount := (RowCount div 2) + 1;
end;
until ymin = 0;
ThreadCount := Min(Mand3DForm.UpDown3.Position, RowCount);
if ATi = 6 then Ati := 10 else if IsPano then ATi := ATi + 4;
case ATi of
2: SetLength(ascThread, ThreadCount);
3: SetLength(ascThreadT0, ThreadCount);
4: SetLength(ascThreadR, ThreadCount);
5: SetLength(ascThreadRT0, ThreadCount);
6: SetLength(ascThreadPano, ThreadCount);
7: SetLength(ascThreadT0Pano, ThreadCount);
8: SetLength(ascThreadRPano, ThreadCount);
9: SetLength(ascThreadRT0Pano, ThreadCount);
10: SetLength(ascThreadVideo, ThreadCount);
end;
ASCparameter.aWidth := MWidth;
ASCparameter.aHeight := MHeight;
ASCparameter.aPCTS := PCTS;
ASCparameter.PATlevel := @ATlevelHiQ[0];
ASCparameter.PATmaxArr := @ATmaxArrHiQ;
ASCparameter.aYBlockStart := 0;
ASCparameter.aRcount := Header.SSAORcount;
Result := True;
if ((Header.bCalcAmbShadowAutomatic and 12) <> 8) or (Mand3DForm.SSAORiteration = 1) then
PCTS.cCalcTime := GetTickCount;
while ASCparameter.aYBlockStart < MHeight do
begin
ASCparameter.aCurrentLevel := 1; //level of wavelet
if ATi < 10 then
begin
FirstATlevelHiQ(@ATlevelHiQ[0], PsiLight, MWidth * MHeight);
pc := @ATmaxArrHiQ[0][0];
for x := 0 to 16 * MWidth * RowCount - 1 do
begin
pc^ := $80008000; //-32767,-32767 in smallint (2 at a time)
Inc(pc);
end;
end;
while Result and (ASCparameter.aCurrentLevel <= ASCparameter.aATlevelCount) do
begin
for x := 0 to ThreadCount - 1 do
begin
ASCparameter.aYstep := ThreadCount;
ASCparameter.aYStart := ASCparameter.aYBlockStart + x;
ASCparameter.aYEnd := Min(MHeight, ASCparameter.aYBlockStart + RowCount) - 1;
ASCparameter.aThreadID := x + 1;
try
case ATi of
2: begin
ascThread[x] := TAmbHiQCalc.Create(True);
ascThread[x].FreeOnTerminate := True;
ascThread[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThread[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThread[x];
end;
3: begin
ascThreadT0[x] := TAmbHiQCalcT0.Create(True);
ascThreadT0[x].FreeOnTerminate := True;
ascThreadT0[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadT0[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadT0[x];
end;
4: begin
ascThreadR[x] := TAmbHiQCalcR.Create(True);
ascThreadR[x].FreeOnTerminate := True;
ascThreadR[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadR[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadR[x];
end;
5: begin
ascThreadRT0[x] := TAmbHiQCalcRT0.Create(True);
ascThreadRT0[x].FreeOnTerminate := True;
ascThreadRT0[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadRT0[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadRT0[x];
end;
6: begin
ascThreadPano[x] := TAmbHiQCalcPano.Create(True);
ascThreadPano[x].FreeOnTerminate := True;
ascThreadPano[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadPano[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadPano[x];
end;
7: begin
ascThreadT0Pano[x] := TAmbHiQCalcT0Pano.Create(True);
ascThreadT0Pano[x].FreeOnTerminate := True;
ascThreadT0Pano[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadT0Pano[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadT0Pano[x];
end;
8: begin
ascThreadRPano[x] := TAmbHiQCalcRPano.Create(True);
ascThreadRPano[x].FreeOnTerminate := True;
ascThreadRPano[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadRPano[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadRPano[x];
end;
9: begin
ascThreadRT0Pano[x] := TAmbHiQCalcRT0Pano.Create(True);
ascThreadRT0Pano[x].FreeOnTerminate := True;
ascThreadRT0Pano[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadRT0Pano[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadRT0Pano[x];
end;
10: begin
ascThreadVideo[x] := TAmbVideo.Create(True);
ascThreadVideo[x].FreeOnTerminate := True;
ascThreadVideo[x].Priority := cTPrio[Mand3DForm.ComboBox2.ItemIndex];
ascThreadVideo[x].ASCpar := ASCparameter;
PCTS.CThandles[x + 1] := ascThreadVideo[x];
end;
end;
PCTS.CTrecords[x + 1].iActualYpos := 0;
PCTS.CTrecords[x + 1].iActualXpos := 0;
PCTS.CTrecords[x + 1].isActive := 1;
// PCTS.CTprios[x + 1] := Header.iThreadPriority;
except
ThreadCount := x;
if ThreadCount > 0 then for y := 0 to ThreadCount - 1 do
case ATi of
2: ascThread[y].ASCpar.aYstep := ThreadCount;
3: ascThreadT0[y].ASCpar.aYstep := ThreadCount;
4: ascThreadR[y].ASCpar.aYstep := ThreadCount;
5: ascThreadRT0[y].ASCpar.aYstep := ThreadCount;
6: ascThreadPano[y].ASCpar.aYstep := ThreadCount;
7: ascThreadT0Pano[y].ASCpar.aYstep := ThreadCount;
8: ascThreadRPano[y].ASCpar.aYstep := ThreadCount;
9: ascThreadRT0Pano[y].ASCpar.aYstep := ThreadCount;
10: ascThreadVideo[y].ASCpar.aYstep := ThreadCount;
end;
Break;
end;
end;
PCTS.HandleType := 1;
PCTS.iTotalThreadCount := ThreadCount;
case ATi of
2: for x := 0 to ThreadCount - 1 do ascThread[x].Start;
3: for x := 0 to ThreadCount - 1 do ascThreadT0[x].Start;
4: for x := 0 to ThreadCount - 1 do ascThreadR[x].Start;
5: for x := 0 to ThreadCount - 1 do ascThreadRT0[x].Start;
6: for x := 0 to ThreadCount - 1 do ascThreadPano[x].Start;
7: for x := 0 to ThreadCount - 1 do ascThreadT0Pano[x].Start;
8: for x := 0 to ThreadCount - 1 do ascThreadRPano[x].Start;
9: for x := 0 to ThreadCount - 1 do ascThreadRT0Pano[x].Start;
10: for x := 0 to ThreadCount - 1 do ascThreadVideo[x].Start;
end;
Result := Result and (ThreadCount > 0);
//wait for current level to complete
repeat
DelayATRet(ThreadCount, PCTS);
x := 0;
ymin := 999999;
for y := 1 to ThreadCount do
begin
if PCTS.CTrecords[y].isActive > 0 then Inc(x);
ymin := Min(ymin, PCTS.CTrecords[y].iActualYpos);
end;
Mand3DForm.ProgressBar1.Position := ASCparameter.aYBlockStart +
(((ymin + MHeight * (ASCparameter.aCurrentLevel - 1)) div ASCparameter.aATlevelCount) * RowCount) div MHeight;
until (x = 0) or PCTS.pLBcalcStop^;
if PCTS.pLBcalcStop^ then Break;
Inc(ASCparameter.aCurrentLevel);
if ASCparameter.aCurrentLevel <= ASCparameter.aATlevelCount then
NextATlevelHiQ(@ATlevelHiQ[0], MWidth, MHeight, 1 shl (ASCparameter.aCurrentLevel - 1));
end;
Inc(ASCparameter.aYBlockStart, RowCount);
if PCTS.pLBcalcStop^ then Break;
end;
if not PCTS.pLBcalcStop^ then
Header.iAmbCalcTime := Round(0.01 * Max(0, GetTickCount - PCTS.cCalcTime));
SetLength(ATlevelHiQ, 0);
SetLength(ATmaxArrHiQ, 0);
except
Result := False;
SetLength(ATlevelHiQ, 0);
SetLength(ATmaxArrHiQ, 0);
Mand3DForm.OutMessage('Ambshadow calculation aborted.');
end;
end;
procedure IniATdirSteps;
var x: Integer;
s, c: Double;
begin
for x := 0 to 31 do
begin
SinCosD(x * Pi / 16, s, c);
ATdirSteps[x][0] := s;
ATdirSteps[x][1] := c;
end;
end;
procedure FirstATlevelHiQ(PIA: TPCardinalArray; PsiLight: TPsiLight5; Leng: Integer);
{var x: Integer; // eax edx ecx
begin
for x := 0 to Leng - 1 do
begin
if PsiLight.Zpos < 32768 then //edx+8
PIA[x] := (PCardinal(@PsiLight.RoughZposFine)^ and $FFFFFF00) shr 1
else PIA[x] := 0;
Inc(PsiLight);
end; }
asm
push esi
dec ecx
js @@out
inc ecx
add edx, 8
@@1:
cmp word [edx], $8000
jnb @@2
mov esi, [edx-2]
and esi, $ffffff00
shr esi, 1
jmp @@3
@@2:
xor esi, esi
@@3:
mov [eax], esi
add edx, 18
add eax, 4
dec ecx
jnz @@1
@@out:
pop esi
end;
procedure SmoothH(PIA, SA: TPCardinalArray; ya, Step: Integer);
{var x, a, b: Integer;// eax edx ecx ebp+8
begin
for x := 0 to ya do
begin
a := x - Step;
if a < 0 then a := 0;
b := x + Step;
if b > ya then b := ya;
PIA[x] := (PIA[x] + (SA[a] + PIA[b]) shr 1) shr 1;
end; } //SA[x] SA[b]
asm
add esp, -12
push ebx
push esi
push edi
mov [ebp-8], ecx
mov ebx, edx
mov edi, [ebp+8]
mov edx, ecx
test edx, edx
jl @@2
inc edx
mov [ebp-12], edx
xor esi, esi
@@1:
mov edx, esi
sub edx, edi
test edx, edx
jnl @@3
xor edx, edx
@@3:
mov ecx, edi
add ecx, esi
cmp ecx, [ebp-8]
jle @@4
mov ecx, [ebp-8]
@@4:
mov ecx, [ebx+ecx*4]
add ecx, [ebx+edx*4]
shr ecx, 1
add ecx, [eax]
shr ecx, 1
mov [eax], ecx
inc esi
add eax, 4
dec dword [ebp-12]
jnz @@1
@@2:
pop edi
pop esi
pop ebx
add esp, 12
end;
procedure SmoothV(PIA, SA: TPCardinalArray; ye, Step, wid: Integer);
{var y, a, b, ya: Integer;// eax edx ecx ebp+12 ebp+8
begin
ya := 0;
for y := 0 to ye do
begin
a := y - Step;
if a < 0 then a := 0;
b := y + Step;
if b > ye then b := ye;
PIA[ya] := (SA[y] + (SA[a] + SA[b]) shr 1) shr 1;
Inc(ya, wid); //PIA[ya]
end; }
asm
add esp, -12
push ebx
push esi
push edi
mov [ebp-8], ecx
mov ebx, edx
mov edi, [ebp+12]
mov edx, ecx
test edx, edx
jl @@2
inc edx
mov [ebp-12], edx
xor esi, esi
@@1:
mov edx, esi
sub edx, edi
test edx, edx
jnl @@3
xor edx, edx
@@3:
mov ecx, edi
add ecx, esi
cmp ecx, [ebp-8]
jle @@4
mov ecx, [ebp-8]
@@4:
mov ecx, [ebx+ecx*4]
add ecx, [ebx+edx*4]
shr ecx, 1
add ecx, [eax]
shr ecx, 1
mov [eax], ecx
inc esi
add eax, dword [ebp+8]
dec dword [ebp-12]
jnz @@1
@@2:
pop edi
pop esi
pop ebx
add esp, 12
end;
procedure NextATlevelHiQ(PIA: TPCardinalArray; Wid, Hei, Step: Integer);
var x, y, y2: Integer;
SA: array of Cardinal;
begin
SetLength(SA, Max(Hei, Wid));
for y := 0 to Hei - 1 do
begin
FastMove(PIA[y * Wid], SA[0], 4 * Wid);
SmoothH(@PIA[y * Wid], @SA[0], Wid - 1, Step);
end;
for x := 0 to Wid - 1 do
begin
y2 := x;
for y := 0 to Hei - 1 do
begin
SA[y] := PIA[y2];
Inc(y2, Wid);
end;
SmoothV(@PIA[x], @SA[0], Hei - 1, Step, 4 * Wid);
end;
end;
procedure MinSI(var SI: SmallInt; var i: Integer);
//begin // eax edx
// if SI < i then if i > 32767 then SI := 32767 else SI := i;
asm
movsx ecx, word [eax]
cmp ecx, [edx]
jnl @@1
cmp dword [edx], $7FFF
jl @@2
mov word [eax], $7FFF
ret
@@2:
mov edx, [edx]
mov word [eax], dx
@@1:
end;
function NotOnlyBackGround4(p: Pointer): Integer;
//begin // eax
// Result := (PCardinal(p)^ and PCardinal(Integer(p) + 18)^ and
// PCardinal(Integer(p) + 36)^ and PCardinal(Integer(p) + 54)^) and $80000000;
asm
mov edx, [eax]
and edx, [eax + 18]
and edx, [eax + 36]
and edx, [eax + 54]
and edx, $80000000
mov eax, edx
end;
procedure MakeZP4(p: Pointer; var zp: array of Integer);
{begin // eax edx
zp[0] := ((PCardinal(p)^ and $FFFFFF00) shr 1);
zp[1] := ((PCardinal(Integer(p) + 18)^ and $FFFFFF00) shr 1);
zp[2] := ((PCardinal(Integer(p) + 36)^ and $FFFFFF00) shr 1);
zp[3] := ((PCardinal(Integer(p) + 54)^ and $FFFFFF00) shr 1); }
asm
mov ecx, [eax]
and ecx, $FFFFFF00
shr ecx, 1
mov [edx], ecx
mov ecx, [eax + 18]
and ecx, $FFFFFF00
shr ecx, 1
mov [edx + 4], ecx
mov ecx, [eax + 36]
and ecx, $FFFFFF00
shr ecx, 1
mov [edx + 8], ecx
mov ecx, [eax + 54]
and ecx, $FFFFFF00
shr ecx, 1
mov [edx + 12], ecx
end;
procedure TAmbHiQCalc.Execute;
var x, y, x2, y2, apos, iStep: Integer;
MWidth, MHeight, iDir, StepCount, sc, it, MH2, MW2, WLo, WHi, HLo, HHi: Integer;
zp: array[0..3] of Integer;
PATL: PIntegerArray;
PS: TPSPoint;
sMinRad, sx, sy, sit: Single;
st, sZRT, sMul, sMHm1, sMWm1: Single;
PsiLightW: PWord;
AngMaxArr: TPAngMaxArrSI; //Smallint
bSummUp: LongBool;
psm, psi2: PSmallInt;
RMA: array[0..5] of Single;
iMaxRad: Integer;
pint: PInteger;
label lab1, lab2, lab3, lab4;
begin
try
pint := @ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos;
AngMaxArr := ASCpar.PATmaxArr;
if ASCpar.aZRThreshold < 0.01 then ASCpar.aZRThreshold := 0.01;
bSummUp := ASCpar.aCurrentLevel = ASCpar.aATlevelCount;
MWidth := ASCpar.aWidth;
MHeight := ASCpar.aHeight;
sMHm1 := MHeight - 1;
sMWm1 := MWidth - 1;
MH2 := 2 * (MHeight - 1);
MW2 := 2 * (MWidth - 1);
WLo := Round(MWidth * ASCpar.aBorderMirrorSize);
WHi := MWidth - 1 - WLo;
HLo := Round(MHeight * ASCpar.aBorderMirrorSize);
HHi := MHeight - 1 - HLo;
sit := ASCpar.aZScaleFactor / 22000.0 * 4096;
y := ASCpar.aYStart;
sZRT := ASCpar.aZRThreshold * 0.7 * 4096 / sit * Sqrt(Sqrt(ASCpar.aATlevelCount / ASCpar.aCurrentLevel)); //Threshold depends on level
sMul := 1.5 * 32767 / (Pi * 32 * Power(ArcTan(ASCpar.aZRThreshold * 0.6 * Sqrt(Sqrt(ASCpar.aATlevelCount))), 0.9)); //pi->16383.. ArcTan(ASCpar.aZRThreshold * Sqrt(ASCpar.aATlevelCount)) = max angle
iStep := 1 shl (ASCpar.aCurrentLevel - 1);
if iStep < 2 then sMinRad := 1
else sMinRad := 3.25 * iStep;// 3 * + 1.1;
if iStep < 2 then StepCount := 5 else StepCount := 3;
iMaxRad := Round(sMinRad + iStep * StepCount + 0.1);
for x := 0 to StepCount - 1 do RMA[x] := 1 / (sMinRad + x * iStep + 0.1);
sit := sit * sZRT;
while y <= ASCpar.aYend do
begin
pint^ := y;
PsiLightW := PWord(Integer(ASCpar.aPsiLight) + 6); //@RoughZposfine
apos := (y - ASCpar.aYBlockStart) * MWidth;
Inc(PsiLightW, y * MWidth * 9);
x := 0;
while x < MWidth do
begin
if (not bSummUp) and (x >= iMaxRad) and (x < MWidth - iMaxRad - 3) then // 4 at once
begin
if NotOnlyBackGround4(PsiLightW) = 0 then
begin
PATL := PIntegerArray(ASCpar.PATlevel);
MakeZP4(PsiLightW, zp);
PS := @ATdirSteps[0];
psi2 := @AngMaxArr^[apos][0];
for iDir := 0 to 31 do
begin
sx := x + PS[0] * sMinRad;
sy := y + PS[1] * sMinRad;
sc := 0;
Lab2: x2 := Round(sx);
y2 := Round(sy);
if y2 < 0 then
begin
y2 := -y2;
if y2 >= HLo then goto Lab1;
end
else if y2 >= MHeight then
begin
y2 := MH2 - y2;
if y2 < HHi then goto Lab1;
end;
psm := psi2;
y2 := y2 * MWidth + x2;
st := (PATL^[y2] - zp[0]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 1] - zp[1]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 2] - zp[2]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 3] - zp[3]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
sx := sx + PS[0] * iStep;
sy := sy + PS[1] * iStep;
Inc(sc);
if sc < StepCount then goto Lab2;
Lab1: Inc(PS);
Inc(psi2);
end;
end;
Inc(PsiLightW, 36);
Inc(x, 4);
Inc(apos, 4);
end else begin
if (PCardinal(PsiLightW)^ and $80000000) = 0 then //or bDoBGshadow
begin
PATL := PIntegerArray(ASCpar.PATlevel);
zp[0] := (PCardinal(PsiLightW)^ and $FFFFFF00) shr 1;
PS := @ATdirSteps[0];
psm := @AngMaxArr^[apos][0];
psi2 := psm;
for iDir := 0 to 31 do
begin
sx := x + PS[0] * sMinRad;
sy := y + PS[1] * sMinRad;
sc := 0;
Lab4: x2 := Round(sx);
y2 := Round(sy);
if x2 < 0 then
begin
x2 := -x2;
if x2 >= WLo then goto Lab3;
end
else if x2 >= MWidth then
begin
x2 := MW2 - x2;
if x2 < WHi then goto Lab3;
end;
if y2 < 0 then
begin
y2 := -y2;
if y2 >= HLo then goto Lab3;
end
else if y2 >= MHeight then
begin
y2 := MH2 - y2;
if y2 < HHi then goto Lab3;
end;
st := (PATL^[y2 * MWidth + x2] - zp[0]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
sx := sx + PS[0] * iStep;
sy := sy + PS[1] * iStep;
Inc(sc);
if sc < StepCount then goto Lab4;
Lab3: Inc(PS);
Inc(psm);
end;
if bSummUp then
begin
st := 0;
for x2 := 0 to 31 do
begin
if psi2^ > -32768 then st := st + ArcTan(psi2^ * d00002441406);
Inc(psi2);
end;
PWord(Integer(PsiLightW) + 6)^ := Round(Min0MaxCS(st * sMul, 16383));
end;
end;
Inc(PsiLightW, 9);
Inc(x);
Inc(apos);
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 TAmbHiQCalcPano.Execute;
var x, y, x2, y2, apos, iStep: Integer;
MWidth, MHeight, iDir, StepCount, sc, it, MH2, MW2, WLo, WHi, HLo, HHi: Integer;
zp: array[0..3] of Integer;
PATL: PIntegerArray;
PS: TPSPoint;
sMinRad, sx, sy, sit: Single;
st, sZRT, sMul, sMHm1, sMWm1: Single;
PsiLightW: PWord;
AngMaxArr: TPAngMaxArrSI; //Smallint
bSummUp: LongBool;
psm, psi2: PSmallInt;
RMA: array[0..5] of Single;
iMaxRad: Integer;
pint: PInteger;
label lab1, lab2, lab3, lab4;
begin
try
pint := @ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos;
AngMaxArr := ASCpar.PATmaxArr;
if ASCpar.aZRThreshold < 0.01 then ASCpar.aZRThreshold := 0.01;
bSummUp := ASCpar.aCurrentLevel = ASCpar.aATlevelCount;
MWidth := ASCpar.aWidth;
MHeight := ASCpar.aHeight;
sMHm1 := MHeight - 1;
sMWm1 := MWidth - 1;
MH2 := 2 * (MHeight - 1);
MW2 := 2 * (MWidth - 1);
WLo := Round(MWidth * ASCpar.aBorderMirrorSize);
WHi := MWidth - 1 - WLo;
HLo := Round(MHeight * ASCpar.aBorderMirrorSize);
HHi := MHeight - 1 - HLo;
sit := ASCpar.aZScaleFactor / 22000.0 * 4096;
y := ASCpar.aYStart;
sZRT := ASCpar.aZRThreshold * 0.7 * 4096 / sit * Sqrt(Sqrt(ASCpar.aATlevelCount / ASCpar.aCurrentLevel)); //Threshold depends on level
sMul := 1.5 * 32767 / (Pi * 32 * Power(ArcTan(ASCpar.aZRThreshold * 0.6 * Sqrt(Sqrt(ASCpar.aATlevelCount))), 0.9)); //pi->16383.. ArcTan(ASCpar.aZRThreshold * Sqrt(ASCpar.aATlevelCount)) = max angle
iStep := 1 shl (ASCpar.aCurrentLevel - 1);
if iStep < 2 then sMinRad := 1
else sMinRad := 3.25 * iStep;// 3 * + 1.1;
if iStep < 2 then StepCount := 5 else StepCount := 3;
iMaxRad := Round(sMinRad + iStep * StepCount + 0.1);
for x := 0 to StepCount - 1 do RMA[x] := 1 / (sMinRad + x * iStep + 0.1);
sit := sit * sZRT;
while y <= ASCpar.aYend do
begin
pint^ := y;
PsiLightW := PWord(Integer(ASCpar.aPsiLight) + 6); //@RoughZposfine
apos := (y - ASCpar.aYBlockStart) * MWidth;
Inc(PsiLightW, y * MWidth * 9);
x := 0;
while x < MWidth do
begin
if (not bSummUp) and (x >= iMaxRad) and (x < MWidth - iMaxRad - 3) then // 4 at once
begin
if NotOnlyBackGround4(PsiLightW) = 0 then
begin
PATL := PIntegerArray(ASCpar.PATlevel);
MakeZP4(PsiLightW, zp);
PS := @ATdirSteps[0];
psi2 := @AngMaxArr^[apos][0];
for iDir := 0 to 31 do
begin
sx := x + PS[0] * sMinRad;
sy := y + PS[1] * sMinRad;
sc := 0;
Lab2: x2 := Round(sx);
y2 := Round(sy);
if y2 < 0 then
begin
y2 := -y2;
if y2 >= HLo then goto Lab1;
end
else if y2 >= MHeight then
begin
y2 := MH2 - y2;
if y2 < HHi then goto Lab1;
end;
psm := psi2;
y2 := y2 * MWidth + x2;
st := (PATL^[y2] - zp[0]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 1] - zp[1]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 2] - zp[2]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
st := (PATL^[y2 + 3] - zp[3]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
Inc(psm, 32);
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
sx := sx + PS[0] * iStep;
sy := sy + PS[1] * iStep;
Inc(sc);
if sc < StepCount then goto Lab2;
Lab1: Inc(PS);
Inc(psi2);
end;
end;
Inc(PsiLightW, 36);
Inc(x, 4);
Inc(apos, 4);
end else begin
if (PCardinal(PsiLightW)^ and $80000000) = 0 then //or bDoBGshadow
begin
PATL := PIntegerArray(ASCpar.PATlevel);
zp[0] := (PCardinal(PsiLightW)^ and $FFFFFF00) shr 1;
PS := @ATdirSteps[0];
psm := @AngMaxArr^[apos][0];
psi2 := psm;
for iDir := 0 to 31 do
begin
sx := x + PS[0] * sMinRad;
sy := y + PS[1] * sMinRad;
sc := 0;
Lab4: x2 := Round(sx);
y2 := Round(sy);
if x2 < 0 then
begin
x2 := x2 + MWidth;
if x2 < 0 then goto Lab3;
end
else if x2 >= MWidth then
begin
x2 := x2 - MWidth;
if x2 >= MWidth then goto Lab3;
end;
if y2 < 0 then
begin
y2 := -y2;
if y2 >= HLo then goto Lab3;
end
else if y2 >= MHeight then
begin
y2 := MH2 - y2;
if y2 < HHi then goto Lab3;
end;
st := (PATL^[y2 * MWidth + x2] - zp[0]) * RMA[sc];
it := Round(st * sit / (sZRT + Abs(st)));
if psm^ < it then if it >= 32767 then psm^ := 32767 else psm^ := it;
sx := sx + PS[0] * iStep;
sy := sy + PS[1] * iStep;
Inc(sc);
if sc < StepCount then goto Lab4;
Lab3: Inc(PS);
Inc(psm);
end;
if bSummUp then
begin
st := 0;
for x2 := 0 to 31 do
begin
if psi2^ > -32768 then st := st + ArcTan(psi2^ * d00002441406);
Inc(psi2);
end;
PWord(Integer(PsiLightW) + 6)^ := Round(Min0MaxCS(st * sMul, 16383));
end;
end;
Inc(PsiLightW, 9);
Inc(x);
Inc(apos);
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 TAmbHiQCalcT0.Execute;
var x, y, x2, y2, apos, iStep: Integer;
MWidth, MHeight, iDir, StepCount, sc, it, MH2, MW2, WLo, WHi, HLo, HHi: Integer;
zp: array[0..3] of Integer;
PATL: PIntegerArray;
PS: TPSPoint;
sMinRad, sx, sy, sit: Single;
st, sZRT, sMul, sMHm1, sMWm1: Single;
PsiLightW: PWord;
AngMaxArr: TPAngMaxArrSI; //Smallint
bSummUp: LongBool;
psm, psi2: PSmallInt;
RMA: array[0..5] of Single;
iMaxRad: Integer;
pint: PInteger;
label lab1, lab2, lab3, lab4;
begin
try
pint := @ASCpar.aPCTS.CTrecords[ASCpar.aThreadID].iActualYpos;
AngMaxArr := ASCpar.PATmaxArr;
if ASCpar.aZRThreshold < 0.01 then ASCpar.aZRThreshold := 0.01;
bSummUp := ASCpar.aCurrentLevel = ASCpar.aATlevelCount;
MWidth := ASCpar.aWidth;
MHeight := ASCpar.aHeight;
MH2 := 2 * (MHeight - 1);
MW2 := 2 * (MWidth - 1);
sMHm1 := MHeight - 1;
sMWm1 := MWidth - 1;
WLo := Round(MWidth * ASCpar.aBorderMirrorSize);
WHi := MWidth - 1 - WLo;
HLo := Round(MHeight * ASCpar.aBorderMirrorSize);
HHi := MHeight - 1 - HLo;
sit := ASCpar.aZScaleFactor / 22000.0 * 4096;
y := ASCpar.aYStart;
sZRT := ASCpar.aZRThreshold {* 0.7} * 4096 / sit * Sqrt(Sqrt(ASCpar.aATlevelCount / ASCpar.aCurrentLevel)); //Threshold depends on level
sZRT := Sqr(sZRT);
sMul := 1.5 * 32767 / (Pi * 32 * ArcTan(ASCpar.aZRThreshold * 0.6 * Sqrt(Sqrt(ASCpar.aATlevelCount)))); //pi->16383.. ArcTan(ASCpar.aZRThreshold * Sqrt(ASCpar.aATlevelCount)) = max angle
iStep := 1 shl (ASCpar.aCurrentLevel - 1);
if iStep < 2 then sMinRad := 1 else sMinRad := 3.25 * iStep;// + 1.1;
if iStep < 2 then StepCount := 5 else StepCount := 3;
iMaxRad := Round(sMinRad + iStep * StepCount + 0.1);
for x := 0 to StepCount - 1 do RMA[x] := 1 / (sMinRad + x * iStep + 0.1);
sit := sit * sZRT;
while y <= ASCpar.aYend do
begin