-
Notifications
You must be signed in to change notification settings - Fork 34
/
CustomFormulas.pas
1084 lines (1043 loc) · 41.3 KB
/
CustomFormulas.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 CustomFormulas;
interface
uses DivUtils, SysUtils, FileHandling, TypeDefinitions, JITFormulas;
function LoadCustomFormulaFromHeader(var CustomFname: array of Byte;
var lCustomFormula: TCustomFormula;
var dOptionVars: array of Double): Boolean;
function LoadCustomFormula(FileName: String; var lCustomFormula: TCustomFormula;
var CustomFname: array of Byte; var dOptionValues: array of Double; bVerbose: LongBool;
MinModTime: TDatetime; ParseResult: TJITFormula; const IgnoreCodeError: Boolean): Boolean;
function AssignCustomFormula(CF1, CF2: PTCustomFormula): LongBool;
procedure FillCustomVBufWithVars(CF1: PTCustomFormula; dOptionValues: array of Double{;isAlt: LongBool});
procedure IniCustomF(CF: PTCustomFormula);
procedure FreeCF(CF: PTCustomFormula);
procedure ParseCFfromOld(iFormula: Integer; CF: PTCustomFormula;
dOptionValues: array of Double);
procedure MakeCustomFsFromHeader(Header: TMandHeader10{; isAlt: LongBool});
procedure SetCFoptionsFromOldF(f: Integer; CF: PTCustomFormula);
procedure CopyTypeAndOptionFromCFtoHAddon(CF: PTCustomFormula; HA: PTHeaderCustomAddon; fnr: Integer{; bLoadedPars: LongBool});
function CanLoadCustomFormula(FileName: String; var DEfunction: Integer): Boolean;
function CanLoadF(FormulaName: String): Boolean;
function Is4Dtype(Header: TPMandHeader10): LongBool;
function isInternFormula(FName: String; var i: Integer): LongBool;
function DescrOfFName(FName: String): String;
function SameFName(Fn1, Fn2: String): LongBool;
procedure ResetFormulas(HAddon: PTHeaderCustomAddon);
function isIntType(ftype: Integer): LongBool;
function isAngleType(ftype: Integer): LongBool;
procedure CheckHybridOptions(pHCA: PTHeaderCustomAddon);
var CFdescription: String;
CFdescriptionIntern: array[0..9] of String = (
'The triplex math sine bulb, as suggested by Daniel White and Paul Nylander. These are fast implementions of powers of 2,3,4,5,6,7 and 8.'+
#13#10+#13#10+'If you want to make an animation with smooth power changings, please use the ''Real Power'' formula.',
'The sine bulb where arbitrary power values can be used, the calculation is slower than of integer powers.'+#13#10+#13#10+
'r = sqrt(x*x + y*y + z*z)'+#13#10+
'th = ArcTan2(y, x) * Float_power'+#13#10+
'ph = ArcSin(z/r) * Float_power'+#13#10+
'r = Power(r, Float_power)'+#13#10+
'x = r * cos(ph) * cos(th) + Cx'+#13#10+
'y = r * cos(ph) * sin(th) + Cy'+#13#10+
'z = Z_multiplier * r * sin(ph) + Cz',
'A 4d quaternion formula, one iteration:'+#13#10+#13#10+
'x'' = x*x - y*y - z*z - w*w + Cx'+#13#10+
'y'' = 2*(y*x + z*w) + Cy'+#13#10+
'z'' = 2*(z*x + YW_multiplier*y*w) + Cz'+#13#10+
'w'' = 2*(w*x + y*z) + W_add + Cw',
'', //Tricorn
'The ''Amazing Box'' aka Mandbox formula, invented by TGlad at fractalforums.com.'+#13#10+#13#10+
'The formula for one iteration looks like this:'+#13#10+#13#10+
'x = abs(x+Fold) - abs(x-Fold) - x'+#13#10+
'y = abs(y+Fold) - abs(y-Fold) - y'+#13#10+
'z = abs(z+Fold) - abs(z-Fold) - z'+#13#10+
'rr = x*x + y*y + z*z'+#13#10+
'if rr < sqr(Min_R) then m = Scale/sqr(Min_R) else'+#13#10+
'if rr < 1 then m = Scale/rr else m = Scale'+#13#10+
'x = x * m + Cx'+#13#10+
'y = y * m + Cy'+#13#10+
'z = z * m + Cz',
'A hybrid of a sine power 2 bulb and the amazing box, the 3d vector length decides which formula is used:'+#13#10+
'If it is bigger than ''Box/Bulb R threshold'' the amazing box is used, else if smaller than ''..threshold 2'' the sine bulb,'+#13#10+
'inbetween both formulas will be interpolated. Make the second threshold a little smaller than the first for more smoothness.',
'A sine integer power bulb with foldings before, like in the amazing box.', '', '',
'A bulb formula as suggested by Aexion:'+#13#10+ //Aexion rotate c [Power, Z mul, Enable RotC (0,1), Cond Phi (0,1), Power C, Cz mul]
'http://www.fractalforums.com/the-3d-mandelbulb/iterating-c/'+#13#10#13#10+
'Added two options to power-rotate the Constant by the distance of the current vectors of Z and C (Cond Phi=1),'+#13#10+
'and the following Mode options:' +#13#10+
'(Bits of Mode value)' +#13#10+
'Bit1: Flip atan theta components' +#13#10+
'Bit2: Flip atan phi components' +#13#10+
'Bit3: Flip theta and phi' +#13#10+
'Bit4: Flip CyCz for angle calc' +#13#10+
'Bit5: Multiply powerC by the distance of vector Z and C');
InternFNames: String = '.Integer Power.Real Power.Quaternion.Tricorn..Amazing Box.Bulbox.Folding Int Pow.test..testIFS....Aexion C.';
//2 17 28 39 48 60 67 83 89 100
Fdescription: array of String; //div11 1 2 3 4 5 6 7 8 9
FdescrName: array of String;
FdescrModTime: array of TDateTime;
FdescCount: Integer = 0;
const
actFormulaId: Integer = 9;
implementation
uses Mand, Math, Dialogs, Windows, Math3D, formulas, FormulaGUI, HeaderTrafos, FormulaCompiler,
Classes, CompilerUtil;
procedure CheckHybridOptions(pHCA: PTHeaderCustomAddon);
var x, end1, repeat1, start2, end2, repeat2: Integer;
begin
if (pHCA.bOptions1 and 3) = 1 then
begin //ipol hybrid
end1 := 0;
repeat1 := 0;
start2 := 1;
end2 := 5;
repeat2 := 1;
end
else
begin
x := 5;
while (x > 0) and (pHCA.Formulas[x].iItCount = 0) do Dec(x);
start2 := Max(1, Min(x, pHCA.bHybOpt2 and 7));
if (pHCA.bOptions1 and 3) = 0 then end1 := x else end1 := start2 - 1;
end2 := Max(start2, x);
repeat2 := Max(start2, Min(end2, (pHCA.bHybOpt2 shr 8) and 7)); //start2, end2, repeat2 3x 4bit
x := end1;
while (x > 0) and (pHCA.Formulas[x].iItCount <= 0) do Dec(x);
repeat1 := Min(x, pHCA.bHybOpt1 shr 4);
if (pHCA.bOptions1 and 3) = 2 then
begin
x := 5;
while (x > start2) and (pHCA.Formulas[x].iItCount <= 0) do Dec(x);
repeat2 := Min(x, repeat2);
end;
end;
pHCA.bHybOpt1 := end1 or (repeat1 shl 4);
pHCA.bHybOpt2 := start2 or (end2 shl 4) or (repeat2 shl 8);
end;
procedure InsertDescription(Fname, Fdescr: String; ModTime: TDateTime);
var i: Integer;
bIn: LongBool;
begin
bIn := False;
for i := 0 to FdescCount - 1 do
begin
if SameText(Fname, FdescrName[i]) then
begin
if High(Fdescription) < i then SetLength(Fdescription, i + 1);
Fdescription[i] := Fdescr;
FdescrModTime[i] := ModTime;
bIn := True;
Break;
end;
end;
if not bIn then
begin
Inc(FdescCount);
i := FdescCount;
SetLength(FdescrName, i);
SetLength(Fdescription, i);
SetLength(FdescrModTime, i);
Dec(i);
FdescrName[i] := Fname;
Fdescription[i] := Fdescr;
FdescrModTime[i] := ModTime;
end;
end;
function SameFName(Fn1, Fn2: String): LongBool;
begin
Result := UpperCase(Trim(Fn1)) = UpperCase(Trim(Fn2));
end;
function DescrOfFName(FName: String): String;
var i: Integer;
begin
Result := '';
if isInternFormula(FName, i) then
begin
Result := CFdescriptionIntern[i];
Exit;
end;
for i := 0 to FdescCount - 1 do if SameFName(FName, FdescrName[i]) then
begin
Result := Fdescription[i];
Break;
end;
end;
procedure ResetFormulas(HAddon: PTHeaderCustomAddon);
var n: Integer;
begin
for n := 0 to 5 do
with HAddon.Formulas[n] do
begin
iItCount := 0;
iFnr := -1;
iOptionCount := 0;
CustomFname[0] := 0;
end;
end;
function isInternFormula(FName: String; var i: Integer): LongBool;
var s: String;
begin
s := Trim(FName);
Result := False;
i := -1;
if s <> '' then
begin
s := '.' + s + '.';
i := Pos(s, InternFNames);
if i > 0 then
begin
i := (i + 1) div 11;
Result := True;
end
else i := -1;
end;
end;
function isIntType(ftype: Integer): LongBool;
begin
Result := ftype in [2, 10, 20];
end;
function isAngleType(ftype: Integer): LongBool;
begin
Result := ftype in [3..6, 12];
end;
{function isAngleTypeIpol(ftype: Integer): LongBool;
begin
Result := ftype in [3..6, 12, 18, 19];
end; }
procedure CopyTypeAndOptionFromCFtoHAddon(CF: PTCustomFormula; HA: PTHeaderCustomAddon; fnr: Integer{; bLoadedPars: LongBool});
var i: Integer;
begin
for i := 0 to 15 do
HA.Formulas[fnr].byOptionType[i] := CF.byOptionTypes[i];
{ if bLoadedPars and (HA.Formulas[fnr].iOptionCount < CF.iCFOptionCount) then
begin // HA.Formulas[fnr].wOptionCountLoaded := HA.Formulas[fnr].wOptionCount; + fill all above loadedcount with defaults when loading formula?
for i := HA.Formulas[fnr].iOptionCount to CF.iCFOptionCount - 1 do
HA.Formulas[fnr].dOptionValue[i] := 0; //not zero.. ini val, done in IniCFs...
end; }
HA.Formulas[fnr].iOptionCount := CF.iCFOptionCount;
end;
procedure SetCFoptionsFromOldF(f: Integer; CF: PTCustomFormula);
var i: Integer;
const
sa: array[0..9, 0..1] of String = (('Integer power (2..8)','Z multiplier'),
('Float power','Z multiplier'),('YW multiplier','W add'),('Z multiplier','CZ multiplier'),
('Scale','Min R'),('Box Scale','Box Min R'),('Integer power (2..8)','Z multiplier'),('',''),('',''),
('Float power','Z multiplier'));
begin
if f in [0..9] then
with CF^ do
begin
for i := 0 to 15 do byOptionTypes[i] := 0; //Double //write of in m3i load stream
if f in [0, 6] then byOptionTypes[0] := 10; //IntPow=NoVar
if f in [4, 5] then byOptionTypes[1] := 7; //AmazBoxMinR
iCFOptionCount := 2;
for i := 0 to 1 do sOptionStrings[i] := sa[f][i];
if f = 4 then
begin
iCFOptionCount := 3;
sOptionStrings[2] := 'Fold';
byOptionTypes[2] := 11; //Folding16 R,R,-R,-R
end
else if f = 5 then //BulBox
begin
iCFOptionCount := 6;
sOptionStrings[2] := 'Box fold';
sOptionStrings[3] := 'Bulb scaling';
sOptionStrings[4] := 'Box/Bulb R threshold';
sOptionStrings[5] := 'Box/Bulb R threshold 2';
byOptionTypes[2] := 11; //Folding16 R,R,-R,-R
byOptionTypes[3] := 0; //13: reciproc
byOptionTypes[4] := 9; //Squared
byOptionTypes[5] := 9;
end
else if f = 6 then //FoldInt
begin
iCFOptionCount := 3;
byOptionTypes[2] := 8;
sOptionStrings[2] := 'R fold';
end
else if f = 7 then
begin
iCFOptionCount := testhybridOptionCount;
for i := 0 to iCFOptionCount - 1 do
begin
sOptionStrings[i] := testhybridOptionsStrings[i];
byOptionTypes[i] := testhybridOptionTypes[i];
end;
end
else if f = 8 then
begin
iCFOptionCount := testIFSOptionCount;
for i := 0 to iCFOptionCount - 1 do
begin
sOptionStrings[i] := testIFSOptionsStrings[i];
byOptionTypes[i] := testIFSOptionTypes[i];
end;
end
else if f = 9 then //Aexion rotate c [Power, Z mul, Enable RotC (0,1), Cond Phi (0,1), Power C, Cz mul, PowC on dist Z-C, Mod (0,1) ]
begin
iCFOptionCount := 8;
sOptionStrings[2] := 'Enable rotate C (0,1)';
sOptionStrings[3] := 'Condi. Phi (0,1)';
sOptionStrings[4] := 'Float power C';
// sOptionStrings[5] := 'Pow C multiplier';
sOptionStrings[5] := 'Cz multiplier';
sOptionStrings[6] := 'PowC on dist Vec-C (0,1)';
sOptionStrings[7] := 'Mode (0..31)';
byOptionTypes[2] := 2;
byOptionTypes[3] := 2;
byOptionTypes[6] := 2;
byOptionTypes[7] := 2;
end;
end;
end;
procedure ParseCFfromOld(iFormula: Integer; CF: PTCustomFormula;
dOptionValues: array of Double);
var f: Integer;
const
ds: array[0..9] of Double = (1,1,1,1,0.2,0.2,0.5,1,1,1);
begin
if iFormula in [0..9] then
with CF^ do
begin
f := iFormula;
SetCFoptionsFromOldF(f, CF);
SIMDlevel := 0;
dDEscale := ds[f];
dADEscale := 1;
if f = 0 then dSIpow := Min(8, Max(2, dOptionValues[0])) else
if f in [1, 9] then dSIpow := NonZero(dOptionValues[0]) else dSIpow := 2;
dRstop := 16;
iConstCount := 0;
iDEoption := 0;
iVersion := 3;
if f in [4, 5, 6] then dRstop := 1024;
SetLength(VarBuffer, 1024);
pConstPointer16 := Pointer((Integer(@VarBuffer[0]) + 271) and $FFFFFFF0);
FastMove(PAligned16^, pConstPointer16^, 216);
if bCPmemReserved and (Pointer(pCodePointer) <> nil) then
VirtualFree(pCodePointer, {0}4096, {MEM_RELEASE} MEM_DECOMMIT);
bCPmemReserved := False;
case f of
0: ThybridIteration(pCodePointer) := fHIntFunctions[Round(dSIpow)];
1: ThybridIteration(pCodePointer) := HybridFloatPow;
2: begin
ThybridIteration(pCodePointer) := fHybridQuat;
iDEoption := 4;
end;
3: ThybridIteration(pCodePointer) := HybridItTricorn;
4: begin
ThybridIteration(pCodePointer) := fHybridCubeDE;
iDEoption := 11;
end;
5: ThybridIteration(pCodePointer) := HybridSuperCube2; //todo: all powers with: ThybridIteration(pCodePointer) := fHIntFunctions[Round(dSIpow)];
6: ThybridIteration(pCodePointer) := HybridFolding;
7: begin
ThybridIteration(pCodePointer) := TestHybrid;
iDEoption := testhybridDEoption;
dRstop := testhybridRstop;
dDEscale := testhybridDEscale;
dSIpow := testhybridPow;
{ pCodePointer := VirtualAlloc(nil, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
FastMove(TestHybrid, pCodePointer^, 4096);
//ThybridIteration(pCodePointer) := TestHybrid;
bCPmemReserved := True; }
end;
8: begin
TIFSIteration(pCodePointer) := HybridCustomIFStest;
iDEoption := testIFSDEoption;
end;
9: ThybridIteration(pCodePointer) := AexionC;
end;
end;
end;
procedure IniCustomF(CF: PTCustomFormula);
begin
CF.iCFOptionCount := 0;
CF.iConstCount := 0;
CF.pConstPointer16 := nil;
CF.pCodePointer := nil;
CF.LastModTime := 0;
// CF.LastCFloaded := '';
CF.bCPmemReserved := False;
end;
procedure FreeCF(CF: PTCustomFormula);
begin
if CF.bCPmemReserved and (Pointer(CF.pCodePointer) <> nil) then
VirtualFree(CF.pCodePointer, {0}4096, {MEM_RELEASE} MEM_DECOMMIT);
CF.bCPmemReserved := False;
Pointer(CF.pCodePointer) := nil;
SetLength(CF.VarBuffer, 0);
end;
procedure FillCustomVBufWithVars(CF1: PTCustomFormula; dOptionValues: array of Double);
var i, j, l: Integer;
p, ps: PSingle;
pd: PDouble;
M: TMatrix3;
MS4: TSMatrix4;
da: array[0..5] of Double;
const MemNeeded: array[0..22] of Integer = (8,4,4,16,8,72,36,16,40,8,0,32,64,8,
16,8,8,16,36,8,8,8,16);
begin
with CF1^ do //10: Fold with only R and 2R
begin //iOptionTypes[i] 0 1 2 3 4 5 6 7 (MinR) 8 Rthr 9 Double, will be squared
p := pConstPointer16; //'.DOUBLE.SINGLE.INTEGER.DOUBLEANGLE.SINGLEANGLE.3DOUBLEANGLES.3SINGLEANGLES.BOXSCALE.FOLDING.DSQUARE.
Dec(p, 2); // NOVARIABLE.FOLDING16.6SINGLEANGLES.DRECIPRO.2DOUBLES..DSQRRECI...2SINGLES.4SINGLES..3SCALESANGLES.SCALESROT.2INTEGER.SRECI2.DRECI2';
// 10 11 12 13 14 15 16 17 18 19 20 21 22
PDouble(p)^ := 0.5; //AmazingBox vars: scale=double, MinR=boxscale: Scale/Sqr(MinR), Sqr(MinR)
//BulBox: scale=double, MinR=boxscale, BulbScale=double, Rthreshold=double ^2
i := 0; //FoldInt: IntPow=novar, Zmul=double, FoldR=folding
while i < Min(16, iCFOptionCount) do
begin
pd := @M[0, 0];
if (Integer(pConstPointer16) - Integer(p) + MemNeeded[byOptionTypes[i]]) < 257 then
case byOptionTypes[i] of
0: begin
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
end;
1: begin
Dec(p);
p^ := dOptionValues[i];
end;
2: begin
Dec(p);
PInteger(p)^ := Round(dOptionValues[i]);
end;
3: begin
Dec(p, 2);
PDouble(p)^ := Sin(dOptionValues[i] * Pid180);
Dec(p, 2);
PDouble(p)^ := Cos(dOptionValues[i] * Pid180);
end;
4: begin
Dec(p);
p^ := Sin(dOptionValues[i] * Pid180);
Dec(p);
p^ := Cos(dOptionValues[i] * Pid180);
end;
5: begin
BuildRotMatrix(dOptionValues[i] * Pid180, dOptionValues[i + 1] * Pid180,
dOptionValues[i + 2] * Pid180, @M);
for j := 0 to 8 do
begin
Dec(p, 2);
PDouble(p)^ := pd^;
Inc(pd);
end;
Inc(i, 2);
end;
6: begin //3SINGLEANGLES
BuildRotMatrix(dOptionValues[i] * Pid180, dOptionValues[i + 1] * Pid180,
dOptionValues[i + 2] * Pid180, @M);
for j := 0 to 8 do
begin
Dec(p);
p^ := pd^;
Inc(pd);
end;
Inc(i, 2);
end;
7: begin //Scale/Sqr(MinR), Sqr(MinR)
Dec(p, 2);
PDouble(p)^ := dOptionValues[i - 1] / Sqr(Max(1e-40, dOptionValues[i]));
Dec(p, 2);
PDouble(p)^ := Sqr(Max(1e-40, dOptionValues[i]));
end;
8: begin //Folding R,2R,-R,-2R
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := 2 * dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := -dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := -2 * dOptionValues[i];
Dec(p);
if i > 1 then TPhybridIteration(p)^ := fHIntFunctions[Max(2, Min(8, Round(dOptionValues[i - 2])))];
end;
9: begin
Dec(p, 2);
PDouble(p)^ := Sqr(dOptionValues[i]);
end;
11: begin //Folding16 R,R,-R,-R
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := -dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := -dOptionValues[i];
end;
12: begin
for j := 0 to 5 do da[j] := dOptionValues[i + j] * pid180;
BuildRotMatrix4d(da, MS4);
ps := @MS4[0];
for j := 0 to 15 do
begin
Dec(p);
p^ := ps^;
Inc(ps);
end;
Inc(i, 5);
end;
13: begin
Dec(p, 2);
PDouble(p)^ := 1 / MaxAbsCD(1e-40, dOptionValues[i]);
end;
14: begin //for SSE2
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
end;
15: begin
Dec(p, 2);
PDouble(p)^ := 1 / MaxCD(1e-40, Sqr(dOptionValues[i]));
end;
16,17: begin
if byOptionTypes[i] = 17 then l := 4 else l := 2;
repeat
Dec(p);
p^ := dOptionValues[i];
Dec(l)
until l = 0;
end;
18: begin //3SCALESANGLES
BuildRotMatrix(dOptionValues[i + 1] * Pid180, dOptionValues[i + 2] * Pid180,
dOptionValues[i + 3] * Pid180, @M);
ScaleMatrix(dOptionValues[i], @M);
for j := 0 to 8 do
begin
Dec(p);
p^ := pd^;
Inc(pd);
end;
Inc(i, 3);
end;
19: begin //SCALESROT 2d scaled rotation , cos, sin
Dec(p);
p^ := Sin(dOptionValues[i + 1] * Pid180) * dOptionValues[i];
Dec(p);
p^ := Cos(dOptionValues[i + 1] * Pid180) * dOptionValues[i];
Inc(i);
end;
20: begin
Dec(p);
PInteger(p)^ := Round(dOptionValues[i]);
Dec(p);
PInteger(p)^ := Round(dOptionValues[i]);
end;
21: begin //.SRECI2 single + reciproc single
Dec(p);
p^ := dOptionValues[i];
Dec(p);
p^ := 1 / MaxCS(s1em30, dOptionValues[i]);
end;
22: begin //.DRECI2
Dec(p, 2);
PDouble(p)^ := dOptionValues[i];
Dec(p, 2);
PDouble(p)^ := 1 / MaxCD(d1em40, dOptionValues[i]);
end;
end;
Inc(i);
end;
end;
end;
procedure MakeCustomFsFromHeader(Header: TMandHeader10);
var i, n: Integer;
CF: PTCustomFormula;
dOptionVtmp: array[0..15] of Double;
begin
if (PTHeaderCustomAddon(Header.PCFAddon).bOptions1 and 3) = 1 then n := 1 else n := 5;
for i := 0 to n do
with PTHeaderCustomAddon(Header.PCFAddon).Formulas[i] do
if (n = 1) or (iItCount > 0) then
begin
CF := PTCustomFormula(Header.PHCustomF[i]);
if iFnr < 20 then
begin
if iFnr < 0 then
begin
iItCount := 0;
Break;
end
else ParseCFfromOld(iFnr, CF, dOptionValue); //dOPtionVals only read for CF
end
else
begin //to not overwrite actual vars with defaults
if not LoadCustomFormulaFromHeader(CustomFname, CF^, dOptionVtmp) then
begin
iItCount := 0;
Break;
end;
end;
FillCustomVBufWithVars(CF, dOptionValue);
end;
end;
function AssignCustomFormula(CF1, CF2: PTCustomFormula): LongBool; //only after filling with vars
var i: Integer;
begin
if (CF2 <> nil) and (CF2.pCodePointer <> nil) then
with CF1^ do
begin
for i := 0 to 15 do
begin
sOptionStrings[i] := CF2.sOptionStrings[i];
byOptionTypes[i] := CF2.byOptionTypes[i];
end;
SIMDlevel := CF2.SIMDlevel;
iCFOptionCount := CF2.iCFOptionCount;
dDEscale := CF2.dDEscale;
dADEscale := CF2.dADEscale;
dSIpow := CF2.dSIpow;
dRstop := CF2.dRstop;
iConstCount := CF2.iConstCount;
iDEoption := CF2.iDEoption;
iVersion := CF2.iVersion;
LastModTime := CF2.LastModTime;
SetLength(VarBuffer, 1024);
pConstPointer16 := Pointer((Integer(@VarBuffer[0]) + 271) and $FFFFFFF0);
FastMove(Pointer(Integer(CF2.pConstPointer16) - 256)^, Pointer(Integer(pConstPointer16) - 256)^, 1008);
if (not bCPmemReserved) and CF2.bCPmemReserved then
pCodePointer := VirtualAlloc(nil, 4096, {MEM_RESERVE} MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if bCPmemReserved and (not CF2.bCPmemReserved) then
VirtualFree(pCodePointer, {0}4096, {MEM_RELEASE} MEM_DECOMMIT);
bCPmemReserved := CF2.bCPmemReserved;
if bCPmemReserved then FastMove(CF2.pCodePointer^, pCodePointer^, 4096)
else pCodePointer := CF2.pCodePointer; //existing formula
Result := True;
end
else Result := False;
end;
function CanLoadCustomFormula(FileName: String; var DEfunction: Integer): Boolean;
var n, iVersion, SIMDlevel: Integer;
f: TextFile;
s: String;
const
cOptions: String = '.SSE2.DESC.SIPO.RSTO.SSE3.SSSE.SSE4.VERS.DEOPTION';
procedure LoadNextStr;
begin
repeat
Readln(f, s);
s := Trim(s);
until EOF(f) or (s > '');
end;
begin
Result := False;
AssignFile(f, FileName);
try
SIMDlevel := 0;
iVersion := 0;
DEfunction := 0;
Reset(f);
LoadNextStr;
if s = '[OPTIONS]' then
begin
repeat
LoadNextStr;
n := Pos(UpCaseN(s, 5), cOPtions);
case n of
1: SIMDlevel := SIMDlevel or 1;
21: SIMDlevel := SIMDlevel or 2;
26: SIMDlevel := SIMDlevel or 4;
31: SIMDlevel := SIMDlevel or 8;
36: iVersion := StrToInt(StrLastWord(s));
41: DEfunction := StrToInt(StrLastWord(s));
end;
until EOF(f) or (s[1] = '[');
end;
Result := ((hasSIMDlevel and SIMDlevel) = SIMDlevel) and (iVersion in [2..actFormulaId]);
finally
CloseFile(f);
end;
end;
function CanLoadF(FormulaName: String): Boolean;
var df: Integer;
begin
Result := CanLoadCustomFormula(IncludeTrailingPathDelimiter(IniDirs[3]) + FormulaName + '.m3f', df);
end;
function LoadCustomFormula(FileName: String; var lCustomFormula: TCustomFormula;
var CustomFname: array of Byte; var dOptionValues: array of Double;
bVerbose: LongBool; MinModTime: TDatetime;
ParseResult: TJITFormula; const IgnoreCodeError: Boolean): Boolean;
var n, i, j: Integer;
f: TextFile;
s, s2, s3: String;
pb: PByte;
d: Double;
EndOfSection: Boolean;
Code: TStringList;
CompiledFormula: TCompiledArtifact;
Formulaname: String;
p: Pointer;
FreeParseResult: Boolean;
const //DEoption > 0 if analytic on X4
cOptions: String = '.SSE2.DESC.SIPO.RSTO.SSE3.SSSE.SSE4.VERS.DEOP.DIFS.ADES.';
cOptions2: String = '.DOUBLE.SINGLE.INTEGER.DOUBLEANGLE.SINGLEANGLE.3DOUBLEANGLES.3SINGLEANGLES.BOXSCALE.FOLDING.DSQUARE.'+
'NOVARIABLE.FOLDING16.6SINGLEANGLES.DRECIPRO.2DOUBLES..DSQRRECI..2SINGLES..4SINGLES..3SCALESANGLES.SCALESROT.2INTEGER..SRECI2....DRECI2.';
cVars: String = 'DOUBLINTINT64SINGLE';
sra: array[0..5] of String = (' YZ',' XZ',' XY',' XW',' YW',' ZW');
procedure LoadNextStr;
begin
repeat
Readln(f, s);
s := Trim(s);
until EOF(f) or (s > '');
end;
begin
Result := False;
AssignFile(f, FileName);
if Assigned(ParseResult) then begin
FreeParseResult := False;
end
else begin
ParseResult := TJITFormula.Create;
FreeParseResult := True;
end;
try
if FileExists(FileName) then with lCustomFormula do try
Formulaname := ExtractFilename( FileName );
ParseResult.Formulaname := Copy(Formulaname, 1, Length(Formulaname) - Length('.m3f'));
SetLength(VarBuffer, 1024);
pConstPointer16 := Pointer((Integer(@VarBuffer[0]) + 271) and $FFFFFFF0);
pb := PByte(pConstPointer16);
SIMDlevel := 0;
iCFOptionCount := 0;
iConstCount := 0;
iDEoption := 0;
iVersion := 0;
dSIpow := 0;
dDEscale := 1;
dADEscale := 1;
dRstop := 16;
Reset(f);
LoadNextStr;
if s = '[OPTIONS]' then
begin
repeat
LoadNextStr;
n := Pos(UpCaseN(s, 5), cOPtions);
case n of
1: begin
SIMDlevel := SIMDlevel or 1;
ParseResult.SetValue(vtOption, 'SSE2', dtString, '');
end;
6: begin
dDEscale := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtOption, 'DEScale', dtDouble, dDEscale);
end;
11: begin
dSIpow := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtOption, 'SIPow', dtDouble, dSIpow);
end;
16: begin
dRstop := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtOption, 'RStop', dtDouble, dRstop);
end;
21: begin
SIMDlevel := SIMDlevel or 2;
ParseResult.SetValue(vtOption, 'SSE3', dtString, '');
end;
26: begin
SIMDlevel := SIMDlevel or 4;
ParseResult.SetValue(vtOption, 'SSEE', dtString, '');
end;
31: begin
SIMDlevel := SIMDlevel or 8;
ParseResult.SetValue(vtOption, 'SSE4', dtString, '');
end;
36: begin
iVersion := StrToInt(StrLastWord(s));
ParseResult.SetValue(vtOption, 'Version', dtInteger, iVersion);
end;
41: begin
iDEoption := StrToInt(StrLastWord(s));
ParseResult.SetValue(vtOption, 'DEoption', dtInteger, iDEoption);
end;
51: begin
dADEscale := StrToFloatK(StrLastWord(s)); //-6 -6
ParseResult.SetValue(vtOption, 'ADEscale', dtDouble, dADEscale);
end;
//9 16 23 31 43 55 63 87 86 94 102 113 129 137
else //1 8 15 23 35 47 61 75 84 92 100 111 121 129 138 148
begin //.DOUBLE.SINGLE.INTEGER.DOUBLEANGLE.SINGLEANGLE.3DOUBLEANGLES.3SINGLEANGLES.BOXSCALE.FOLDING.DSQUARE.NOVARIABLE.FOLDING16.6SINGLEANGLES.DRECIPRO.2DOUBLES..DSQRRECI.'
s2 := StrFirstWord(s); //.2SINGLES..4SINGLES..3SCALESANGLES.SCALESROT.
n := Pos(UpperCase(s2), cOPtions2); //158 168 178 192-4
if n > 50 then Dec(n, 6);
if n > 130 then Dec(n, 6);
if n > 180 then Dec(n, 4);
if (n > 0) and (iCFOptionCount < 16) then begin
byOptionTypes[iCFOptionCount] := (n + 8) div 10;
dOptionValues[iCFOptionCount] := StrToFloatK(StrLastWord(s));
s3 := StrSecondWord(s);
if byOptionTypes[iCFOptionCount] = 12 then //6 singleangles
begin
ParseResult.CanSave := False;
j := iCFOptionCount;
for i := 0 to 5 do
begin
byOptionTypes[iCFOptionCount] := 12;
dOptionValues[iCFOptionCount] := dOptionValues[j];
sOptionStrings[iCFOptionCount] := s3 + sra[i];
Inc(iCFOptionCount);
end;
Dec(iCFOptionCount);
end
else
begin
sOptionStrings[iCFOptionCount] := s3;
ParseResult.SetValue(vtParam, s3, dtDouble, dOptionValues[iCFOptionCount]);
if byOptionTypes[iCFOptionCount] in [5, 6] then begin
ParseResult.CanSave := False;
d := dOptionValues[iCFOptionCount];
sOptionStrings[iCFOptionCount] := s3 + ' X';
Inc(iCFOptionCount);
byOptionTypes[iCFOptionCount] := byOptionTypes[iCFOptionCount - 1];
dOptionValues[iCFOptionCount] := d;
sOptionStrings[iCFOptionCount] := s3 + ' Y';
Inc(iCFOptionCount);
byOptionTypes[iCFOptionCount] := byOptionTypes[iCFOptionCount - 1];
dOptionValues[iCFOptionCount] := d;
sOptionStrings[iCFOptionCount] := s3 + ' Z';
end
else if byOptionTypes[iCFOptionCount] = 18 then
begin
ParseResult.CanSave := False;
for i := 0 to 2 do
begin
Inc(iCFOptionCount);
byOptionTypes[iCFOptionCount] := 6;
sOptionStrings[iCFOptionCount] := 'Rotation ' + Chr(65 + i);
dOptionValues[iCFOptionCount] := 0;
end;
end
else if byOptionTypes[iCFOptionCount] = 19 then
begin
ParseResult.CanSave := False;
sOptionStrings[iCFOptionCount] := 'Scale ' + s3;
Inc(iCFOptionCount);
byOptionTypes[iCFOptionCount] := 6;
sOptionStrings[iCFOptionCount] := 'Rotation ' + s3;
dOptionValues[iCFOptionCount] := dOptionValues[iCFOptionCount - 1];
dOptionValues[iCFOptionCount - 1] := StrToFloatK(StrFirstWordAfterEqual(s));
end;
end;
Inc(iCFOptionCount);
end;
end;
end;
until n = 0;
if (hasSIMDlevel and SIMDlevel) < SIMDlevel then
begin
s := '';
if bVerbose then ShowMessage('This computer does not support the SIMD level required by this formula.')
else Mand3DForm.OutMessage('This computer does not support the SIMD level required by this formula.');
end;
end;
Result := (s > '') and (iVersion in [2..actFormulaId]);
if s > '' then
begin
if iVersion < 2 then
begin
if bVerbose then ShowMessage('The formula is an old one, only versions 2 to ' +
IntToStr(actFormulaId) + #13#10 + ' are allowed in this program version.')
else Mand3DForm.OutMessage('The formula is an old one, only versions 2 to ' + IntToStr(actFormulaId) + ' are allowed in this program version.');
end
else if iVersion > actFormulaId then
begin
if bVerbose then ShowMessage('This formula was designed for a more actual version of Mandelbulb 3D')
else Mand3DForm.OutMessage('This formula was designed for a more actual version of Mandelbulb 3D');
end;
end;
if not Result then s := '';
if s = '[CONSTANTS]' then
begin
repeat
LoadNextStr;
if (Length(s) > 1) and (s[1]='.') then begin
// new style constants with names
n := Pos(UpCaseN(Copy(s, 2, Length(s)-1), 5), cVars);
case n of
1: begin
PDouble(pb)^ := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtConst, StrSecondWord(s), dtDouble, PDouble(pb)^ );
end;
6: begin
PInteger(pb)^ := StrToInt(StrLastWord(s));
ParseResult.SetValue(vtConst, StrSecondWord(s), dtInteger, PInteger(pb)^);
end;
9: begin
PInt64(pb)^ := StrToInt64(StrLastWord(s));
ParseResult.SetValue(vtConst, StrSecondWord(s), dtInt64, PInt64(pb)^);
end;
14: begin
PSingle(pb)^ := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtConst, StrSecondWord(s), dtSingle, PSingle(pb)^);
end;
else
begin
Dec(pb, 8);
Dec(iConstCount);
end;
end;
end
else begin
n := Pos(UpCaseN(s, 5), cVars);
case n of
1: begin
PDouble(pb)^ := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtConst, ParseResult.NextValueName(vtConst), dtDouble, PDouble(pb)^ );
end;
6: begin
PInteger(pb)^ := StrToInt(StrLastWord(s));
ParseResult.SetValue(vtConst, ParseResult.NextValueName(vtConst), dtInteger, PInteger(pb)^);
end;
9: begin
PInt64(pb)^ := StrToInt64(StrLastWord(s));
ParseResult.SetValue(vtConst, ParseResult.NextValueName(vtConst), dtInt64, PInt64(pb)^);
end;
14: begin
PSingle(pb)^ := StrToFloatK(StrLastWord(s));
ParseResult.SetValue(vtConst, ParseResult.NextValueName(vtConst), dtSingle, PSingle(pb)^);
end;
else
begin
Dec(pb, 8);
Dec(iConstCount);
end;
end;
end;
Inc(pb, 8);
Inc(iConstCount);
until (Length(s) > 0) and (s[1] = '[');
end;
if s = '[SOURCE]' then begin
Code := TStringList.Create;
try
repeat
Readln(f, s);
s2 := Trim(s);
EndOfSection := (Length(s2) > 0) and (s2[1] = '[') and (s2[Length(s2)]=']');
if not EndOfSection then
Code.Add(s);
until EOF(f) or EndOfSection;
if Code.Count > 0 then begin
ParseResult.Code := Code.Text;
CompiledFormula := TFormulaCompilerRegistry.GetCompilerInstance(langDELPHI).CompileFormula( ParseResult );
if CompiledFormula.IsValid then begin
ThybridIteration(pCodePointer) := CompiledFormula.CodePointer;
bCPmemReserved := False;
Result := True;
end
else begin
if IgnoreCodeError then
Result := True
else
raise Exception.Create(Code.Text+#13#10+ CompiledFormula.ErrorMessage.Text);
end;
end;
ParseResult.Code := Code.Text;
finally
Code.Free;
end;
end;
if s = '[CODE]' then
begin
if (not bCPmemReserved) or (pCodePointer = nil) then
pCodePointer := VirtualAlloc(nil, 4096, {MEM_RESERVE} MEM_COMMIT, PAGE_EXECUTE_READWRITE);
bCPmemReserved := True;
n := 0;
pb := Pointer(pCodePointer);
repeat
LoadNextStr;
if (Length(s) > 0) and (s[1] <> '[') then
begin
while (Length(s) > 1) and (n < 4095) do
begin
pb^ := StrToInt('$' + s[1] + s[2]);
Inc(pb);
Inc(n);
Delete(s, 1, 2);
end;
end;
until EOF(f) or (s = '[END]');