-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathr_externaltextures.pas
1249 lines (1154 loc) · 35 KB
/
r_externaltextures.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
//------------------------------------------------------------------------------
//
// FPCDoom - Port of Doom to Free Pascal Compiler
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2004-2007 by Jim Valavanis
// Copyright (C) 2017-2022 by Jim Valavanis
//
// 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., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// E-Mail: [email protected]
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit r_externaltextures;
interface
uses
r_draw_span;
//==============================================================================
//
// R_Reset32Cache
//
//==============================================================================
procedure R_Reset32Cache;
//==============================================================================
//
// R_Clear32Cache
//
//==============================================================================
procedure R_Clear32Cache;
//==============================================================================
//
// R_Init32Cache
//
//==============================================================================
procedure R_Init32Cache;
//==============================================================================
//
// R_ShutDown32Cache
//
//==============================================================================
procedure R_ShutDown32Cache;
//==============================================================================
//
// R_ReadDC32Cache
//
//==============================================================================
procedure R_ReadDC32Cache(const rtex, rcol: integer);
//==============================================================================
//
// R_Precache32bittexture
//
//==============================================================================
procedure R_Precache32bittexture(const rtex: integer);
//==============================================================================
//
// R_ClearDC32Cache
//
//==============================================================================
procedure R_ClearDC32Cache;
//==============================================================================
//
// R_ResetDC32Cache
//
//==============================================================================
procedure R_ResetDC32Cache;
//==============================================================================
//
// R_InitDC32Cache
//
//==============================================================================
procedure R_InitDC32Cache;
//==============================================================================
//
// R_ShutDownDC32Cache
//
//==============================================================================
procedure R_ShutDownDC32Cache;
//==============================================================================
//
// R_ReadDS32Cache
//
//==============================================================================
procedure R_ReadDS32Cache(const flat: integer);
//==============================================================================
//
// R_FlatScaleFromSize
//
//==============================================================================
function R_FlatScaleFromSize(const size: integer): dsscale_t;
//==============================================================================
//
// R_ClearDS32Cache
//
//==============================================================================
procedure R_ClearDS32Cache;
//==============================================================================
//
// R_ResetDS32Cache
//
//==============================================================================
procedure R_ResetDS32Cache;
//==============================================================================
//
// R_InitDS32Cache
//
//==============================================================================
procedure R_InitDS32Cache;
//==============================================================================
//
// R_ShutDownDS32Cache
//
//==============================================================================
procedure R_ShutDownDS32Cache;
implementation
uses
d_fpc,
m_fixed,
r_defs,
r_hires,
r_draw_column,
r_sky,
r_data,
r_mmx,
t_main,
v_video,
w_wad,
z_memory;
const
// Columns cache
COL32CACHESIZE = $4000;
const
MAXTEXTURESIZE32 = 128 * (1 shl MAXTEXTUREFACTORBITS);
type
dc32_t = array[0..MAXTEXTURESIZE32] of LongWord;
Pdc32_t = ^dc32_t;
Pdc32cacheitem_t = ^dc32cacheitem_t;
dc32cacheitem_t = record
dc32: Pdc32_t;
columnsize32: integer;
texture: integer;
column: integer;
texturemod: integer;
next: Pdc32cacheitem_t;
end;
dc32cacheitem_tArray = array[0..COL32CACHESIZE - 1] of dc32cacheitem_t;
dc32cacheitem_tPArray = array[0..COL32CACHESIZE - 1] of Pdc32cacheitem_t;
//==============================================================================
//
// R_Reset32Cache
//
//==============================================================================
procedure R_Reset32Cache;
begin
R_ResetDC32Cache;
R_ResetDS32Cache;
end;
//==============================================================================
//
// R_Clear32Cache
//
//==============================================================================
procedure R_Clear32Cache;
begin
R_ClearDC32Cache;
R_ClearDS32Cache;
end;
//==============================================================================
//
// R_ShutDown32Cache
//
//==============================================================================
procedure R_ShutDown32Cache;
begin
R_ShutDownDC32Cache;
R_ShutDownDS32Cache;
end;
//==============================================================================
//
// R_Init32Cache
//
//==============================================================================
procedure R_Init32Cache;
begin
R_InitDC32Cache;
R_InitDS32Cache;
end;
//==============================================================================
//
// R_GetHash
//
//==============================================================================
function R_GetHash(const tex, col, dmod: integer): integer;
// JVAL
// Get a hash value depending on tex, col and dc_mod.
// Although the followng hash is elementary, simple
// and it 's not the best gives good results,
// (about 98-99.9% hits correctly to cache (for standard resolution textures))
begin
result := (97 * tex + col * 3833 + dmod * 7867) and (COL32CACHESIZE - 1);
end;
var
dc32cache: dc32cacheitem_tPArray;
//==============================================================================
//
// R_Get_dc32
//
//==============================================================================
function R_Get_dc32(p: Pdc32cacheitem_t; columnsize: integer): Pdc32_t;
begin
if p.dc32 = nil then
begin
p.dc32 := malloc((columnsize + 1) * SizeOf(LongWord));
p.columnsize32 := columnsize;
end
else if p.columnsize32 <> columnsize then
begin
{$IFNDEF FPC}p.dc32 := {$ENDIF}realloc(p.dc32, (p.columnsize32 + 1) * SizeOf(LongWord), (columnsize + 1) * SizeOf(LongWord));
p.columnsize32 := columnsize;
end;
result := p.dc32;
end;
//==============================================================================
//
// R_FindDC32Rover
//
//==============================================================================
function R_FindDC32Rover(const hash, rtex, rcol, rtexturemod: integer): Pdc32cacheitem_t;
begin
result := dc32cache[hash];
while result <> nil do
begin
if (result.texture = rtex) and (result.column = rcol) and (result.texturemod = rtexturemod) then
exit;
result := result.next;
end;
end;
//==============================================================================
//
// R_NewDC32Rover
//
//==============================================================================
function R_NewDC32Rover(const hash: integer): Pdc32cacheitem_t;
begin
result := mallocz(SizeOf(dc32cacheitem_t));
result.next := dc32cache[hash];
dc32cache[hash] := result;
end;
//==============================================================================
//
// R_ReadDC32ExternalCache
//
// JVAL
// Create dc_source (32 bit) from an external texture
//
//==============================================================================
function R_ReadDC32ExternalCache(const rtex, rcol: integer): boolean;
var
plw: PLongWord;
plw2: PLongWord;
pdc32: Pdc32_t;
cachemiss: boolean;
t: PTexture;
col: integer;
i: integer;
dc32_a: dc32_t;
cfrac2: fixed_t;
r1, b1, g1: byte;
r2, g2, b2: byte;
r, g, b: LongWord;
c: LongWord;
twidth: integer;
theight: integer;
tfactor: integer;
columnsize: integer;
mod_c, mod_d: integer;
loops: integer;
hash: integer;
curgamma: PByteArray;
pb: PByte;
ptex: Ptexture_t;
rover: Pdc32cacheitem_t;
begin
if not useexternaltextures then
begin
result := false;
exit;
end;
// Cache read of the caclulated dc_source (32 bit), 98-99% propability not to recalc...
hash := R_GetHash(rtex, rcol, rcolumn.dc_texturemod);
rover := R_FindDC32Rover(hash, rtex, rcol, rcolumn.dc_texturemod);
cachemiss := rover = nil;
if cachemiss then
rover := R_NewDC32Rover(hash);
ptex := textures[rtex];
if cachemiss then
begin
t := ptex.texture32;
if t = nil then
begin
t := T_LoadHiResTexture(ptex.name);
if t = nil then
ptex.texture32 := pointer($1) // Mark as missing
else
begin
ptex.texture32 := t;
// JVAL Adjust very big textures
theight := t.GetHeight;
tfactor := theight div ptex.height; // Scaling
i := 0;
while i < MAXTEXTUREFACTORBITS do
begin
if tfactor <= 1 shl i then
break;
inc(i);
end;
// JVAL Final adjustment of hi resolution textures
twidth := (1 shl i) * ptex.width;
theight := (1 shl i) * ptex.height;
while (twidth > MAXTEXTURESIZE32) or (theight > MAXTEXTURESIZE32) do
begin
dec(i);
twidth := (1 shl i) * ptex.width;
theight := (1 shl i) * ptex.height;
end;
t.ScaleTo(twidth, theight); // JVAL Scale the texture if needed
ptex.factorbits := i;
end;
end;
if LongWord(t) > $1 then // if we have a hi resolution texture
begin
rover.texture := rtex;
rover.column := rcol;
rover.texturemod := rcolumn.dc_texturemod;
// JVAL
// Does not use [and (t.GetWidth - 1)] but [mod (t.GetWidth - 1)] because
// we don't require textures to have width as power of 2.
if rcol < 0 then
col := abs(rcol - ptex.width) mod ptex.width
else
col := rcol mod ptex.width;
if ptex.factorbits > 0 then
begin
// JVAL: Handle hi resolution texture
tfactor := 1 shl ptex.factorbits;
columnsize := 128 * tfactor;
mod_c := (rcolumn.dc_texturemod * tfactor) shr DC_HIRESBITS;
mod_d := rcolumn.dc_texturemod - mod_c * (1 shl (DC_HIRESBITS - ptex.factorbits));
col := col * tfactor + mod_c;
rcolumn.dc_texturemod := mod_d;
end
else
begin
tfactor := 1;
rcolumn.dc_texturemod := rcolumn.dc_mod;
columnsize := 128;
end;
pdc32 := R_Get_dc32(rover, columnsize);
plw := @pdc32[0];
curgamma := @gammatable[usegamma]; // To Adjust gamma
c := 0;
if t.GetBytesPerPixel = 1 then
begin
r1 := pal_color;
g1 := pal_color shr 8;
b1 := pal_color shr 16;
c := curgamma[r1] + curgamma[g1] shl 8 + curgamma[b1] shl 16;
t.GetPalettedColumn32(col, columnsize, plw, c);
end
else
t.GetColumn32(col, columnsize, plw);
// Texture filtering if dc_texturemod <> 0
if rcolumn.dc_texturemod <> 0 then
begin
if t.GetBytesPerPixel = 1 then
t.GetPalettedColumn32(col + 1, columnsize, @dc32_a, c)
else
t.GetColumn32(col + 1, columnsize, @dc32_a);
plw2 := @dc32_a;
cfrac2 := rcolumn.dc_texturemod shl (FRACBITS - DC_HIRESBITS);
for i := 0 to columnsize - 1 do
begin
plw^ := R_ColorAverageAlpha(plw^, plw2^, cfrac2);
inc(plw);
inc(plw2);
end;
end;
if t.GetBytesPerPixel <> 1 then
begin
pdc32 := R_Get_dc32(rover, columnsize);
plw := @pdc32[0];
// Simutate palette changes
if dc_32bittexturepaletteeffects and (pal_color <> 0) then
begin
r1 := pal_color;
g1 := pal_color shr 8;
b1 := pal_color shr 16;
loops := columnsize;
if usegamma > 0 then
begin
while loops >= 0 do
begin
c := plw^;
if c <> 0 then // JVAL: color $000000 is transparent index
begin
r2 := c;
g2 := c shr 8;
b2 := c shr 16;
r := r1 + r2;
if r > 255 then
r := 255
else
r := curgamma[r];
g := g1 + g2;
if g > 255 then
g := 255
else
g := curgamma[g];
b := b1 + b2;
if b > 255 then
plw^ := r + g shl 8 + $FF0000
else
plw^ := r + g shl 8 + curgamma[b] shl 16;
end;
inc(plw);
dec(loops);
end;
end
else
begin
if not R_BatchColorAdd32_MMX(plw, pal_color, columnsize) then
begin
while loops >= 0 do
begin
c := plw^;
if c <> 0 then // JVAL: color $000000 is transparent index
begin
r2 := c;
g2 := c shr 8;
b2 := c shr 16;
r := r1 + r2;
if r > 255 then
r := 255;
g := g1 + g2;
if g > 255 then
g := 255;
b := b1 + b2;
if b > 255 then
plw^ := r + g shl 8 + $FF0000
else
plw^ := r + g shl 8 + b shl 16;
end;
inc(plw);
dec(loops);
end;
end;
end;
end
else
begin
if usegamma > 0 then
begin
pb := PByte(plw);
loops := columnsize;
while loops > 0 do
begin
if PLongWord(pb)^ <> 0 then
begin
pb^ := curgamma[pb^];
inc(pb);
pb^ := curgamma[pb^];
inc(pb);
pb^ := curgamma[pb^];
inc(pb, 2);
end
else
inc(pb, 4);
dec(loops);
end;
end;
end;
end;
end
else // We don't have hi resolution texture
begin
result := false;
exit;
end;
if rtex = skytexture then
rover.dc32[columnsize] := rover.dc32[columnsize - 1]
else
rover.dc32[columnsize] := rover.dc32[0];
end;
rcolumn.dc_mod := rcolumn.dc_texturemod;
rcolumn.dc_texturefactorbits := ptex.factorbits;
rcolumn.dc_source := rover.dc32;
result := true;
end;
type
resize_t = function (const x1, x2, x3: LongWord; const offs: integer): LongWord;
//==============================================================================
//
// hqresize
//
//==============================================================================
function hqresize(const x1, x2, x3: LongWord; const offs: integer): LongWord;
function _color_sqdiff(const c1, c2: LongWord): LongWord;
var
dr, dg, db: LongWord;
begin
dr := ((c1 shr 16) and $FF) - ((c2 shr 16) and $FF);
dg := ((c1 shr 8) and $FF) - ((c1 shr 8) and $FF);
db := (c1 and $FF) - (c2 and $FF);
result := dr * dr + dg * dg + db * db;
end;
var
sqoffs: LongWord;
sqoffs1, sqoffs3: LongWord;
t1, t3: LongWord;
begin
sqoffs := offs * offs;
sqoffs1 := _color_sqdiff(x1, x2);
if sqoffs1 < sqoffs then
t1 := x1
else
t1 := x2;
sqoffs3 := _color_sqdiff(x3, x2);
if sqoffs3 < sqoffs then
t3 := x3
else
t3 := x2;
result := R_ColorArrayAverage([t1, x2, x2, t3]);
end;
//==============================================================================
//
// noresize
//
//==============================================================================
function noresize(const x1, x2, x3: LongWord; const offs: integer): LongWord;
begin
result := x2;
end;
//==============================================================================
//
// R_Grow_dc32
//
//==============================================================================
function R_Grow_dc32(p: Pdc32cacheitem_t; oldcolumnsize: integer; factor: integer; lastid: integer; proc: resize_t): Pdc32_t;
var
l: PLongWordArray;
i: integer;
begin
if p.dc32 = nil then
begin
p.dc32 := mallocz(factor * (oldcolumnsize + 1) * SizeOf(LongWord));
p.columnsize32 := factor * oldcolumnsize;
end
else if p.columnsize32 = oldcolumnsize then
begin
l := malloc((factor * oldcolumnsize + 1) * SizeOf(LongWord));
for i := 0 to factor * oldcolumnsize - 1 do
l[i] := p.dc32[i div factor];
l[factor * p.columnsize32] := p.dc32[lastid];
{$IFNDEF FPC}p.dc32 := {$ENDIF}realloc(p.dc32, (p.columnsize32 + 1) * SizeOf(LongWord), (factor * oldcolumnsize + 1) * SizeOf(LongWord));
p.columnsize32 := factor * oldcolumnsize;
p.dc32[0] := proc(l[p.columnsize32], l[0], l[1], 64);
for i := 1 to p.columnsize32 - 1 do
p.dc32[i] := proc(l[i - 1], l[i], l[i + 1], 64);
p.dc32[p.columnsize32] := proc(l[p.columnsize32 - 1], l[p.columnsize32], l[1], 64);
memfree(l, (factor * oldcolumnsize + 1) * SizeOf(LongWord));
end;
result := p.dc32;
end;
//==============================================================================
//
// R_ReadDC32InternalCache
//
// JVAL
// Create dc_source (32 bit) from internal (IWAD) texture
//
//==============================================================================
procedure R_ReadDC32InternalCache(const rtex, rcol: integer);
var
plw: PLongWord;
pdc32: Pdc32_t;
src1, src2: PByte;
tbl: Phiresmodtable_t;
cachemiss: boolean;
hash: integer;
i: integer;
dc_source1, dc_source2: PByteArray;
rover: Pdc32cacheitem_t;
begin
// Cache read of the caclulated dc_source (32 bit), 98-99% propability not to recalc...
hash := R_GetHash(rtex, rcol, rcolumn.dc_mod);
rover := R_FindDC32Rover(hash, rtex, rcol, rcolumn.dc_texturemod);
cachemiss := rover = nil;
if cachemiss then
rover := R_NewDC32Rover(hash);
if cachemiss then
begin
rover.texture := rtex;
rover.column := rcol;
rover.texturemod := rcolumn.dc_mod;
pdc32 := R_Get_dc32(rover, 128);
plw := @pdc32[0];
textures[rtex].factorbits := 0;
if rcolumn.dc_mod = 0 then
begin
dc_source1 := R_GetColumn(rtex, rcol);
src1 := @dc_source1[0];
for i := 0 to 127 do
begin
plw^ := videopal[src1^];
inc(plw);
inc(src1);
end;
end
else
begin
tbl := @hirestable[rcolumn.dc_mod];
dc_source1 := R_GetColumn(rtex, rcol);
src1 := @dc_source1[0];
dc_source2 := R_GetColumn(rtex, rcol + 1);
src2 := @dc_source2[0];
for i := 0 to 127 do
begin
plw^ := tbl[src1^, src2^];
inc(plw);
inc(src1);
inc(src2);
end;
end;
if rtex = skytexture then
begin
if smoothskies then
begin
R_Grow_dc32(rover, 128, 2, 127, @hqresize)
end
else
R_Grow_dc32(rover, 128, 2, 127, @noresize);
textures[rtex].factorbits := 1;
end
else
plw^ := rover.dc32[0];
end;
rcolumn.dc_texturefactorbits := textures[rtex].factorbits;
rcolumn.dc_source := rover.dc32;
end;
//==============================================================================
//
// R_ReadDC32Cache
//
//==============================================================================
procedure R_ReadDC32Cache(const rtex, rcol: integer);
begin
if not R_ReadDC32ExternalCache(rtex, rcol) then
R_ReadDC32InternalCache(rtex, rcol);
end;
//==============================================================================
//
// R_Precache32bittexture
//
//==============================================================================
procedure R_Precache32bittexture(const rtex: integer);
begin
R_ReadDC32Cache(rtex, 0);
end;
//==============================================================================
//
// R_ResetDC32Cache
//
//==============================================================================
procedure R_ResetDC32Cache;
var
i: integer;
rover: Pdc32cacheitem_t;
begin
for i := 0 to COL32CACHESIZE - 1 do
begin
rover := dc32cache[i];
while rover <> nil do
begin
rover.texture := -1;
rover := rover.next;
end;
end;
end;
//==============================================================================
//
// R_ClearDC32Cache
//
//==============================================================================
procedure R_ClearDC32Cache;
var
i: integer;
rover, next: Pdc32cacheitem_t;
begin
for i := 0 to numtextures - 1 do
begin
if LongWord(textures[i].texture32) > 1 then
dispose(textures[i].texture32, destroy);
textures[i].texture32 := nil;
end;
for i := 0 to COL32CACHESIZE - 1 do
begin
rover := dc32cache[i];
while rover <> nil do
begin
next := rover.next;
if rover.dc32 <> nil then
memfree(rover.dc32, (rover.columnsize32 + 1) * SizeOf(LongWord));
memfree(rover, SizeOf(dc32cacheitem_t));
rover := next;
end;
dc32cache[i] := nil;
end;
end;
//==============================================================================
//
// R_InitDC32Cache
//
//==============================================================================
procedure R_InitDC32Cache;
var
i: integer;
begin
for i := 0 to COL32CACHESIZE - 1 do
dc32cache[i] := nil;
end;
//==============================================================================
//
// R_ShutDownDC32Cache
//
//==============================================================================
procedure R_ShutDownDC32Cache;
begin
R_ClearDC32Cache;
end;
const
// Flat cache
FLAT32CACHESIZE = 256;
CACHEFLATMASK = FLAT32CACHESIZE - 1;
type
ds32_t = array[0..512 * 512 - 1] of LongWord;
Pds32_t = ^ds32_t;
Pds32cacheitem_t = ^ds32cacheitem_t;
ds32cacheitem_t = record
ds32: array[0..Ord(NUMDSSCALES) - 1] of Pds32_t;
lump: integer;
scale: dsscale_t;
next: Pds32cacheitem_t;
end;
ds32cacheitem_tArray = array[0..FLAT32CACHESIZE - 1] of ds32cacheitem_t;
ds32cacheitem_tPArray = array[0..FLAT32CACHESIZE - 1] of Pds32cacheitem_t;
//==============================================================================
//
// R_Get_ds32
//
//==============================================================================
function R_Get_ds32(p: Pds32cacheitem_t): Pds32_t;
begin
result := p.ds32[Ord(p.scale)];
if result = nil then
begin
result := malloc(dsscalesize[Ord(p.scale)] * SizeOf(LongWord));
p.ds32[Ord(p.scale)] := result;
end;
end;
type
span64x64_t = packed array[0..63, 0..63] of LongWord;
Pspan64x64_t = ^span64x64_t;
span128x128_t = packed array[0..127, 0..127] of LongWord;
Pspan128x128_t = ^span128x128_t;
//==============================================================================
//
// R_GrowSpan64to128
//
//==============================================================================
procedure R_GrowSpan64to128(const p: Pds32cacheitem_t);
var
i, j: integer;
p1, p2: Pds32_t;
pspan64: Pspan64x64_t;
pspan128: Pspan128x128_t;
outspan128: Pspan128x128_t;
c: LongWord;
begin
if p.scale <> ds64x64 then
exit;
p1 := R_Get_ds32(p);
p.scale := ds128x128;
p2 := R_Get_ds32(p);
pspan64 := @p1[0];
pspan128 := malloc(SizeOf(span128x128_t));
for i := 0 to 127 do
for j := 0 to 127 do
pspan128[i, j] := pspan64[i div 2, j div 2];
outspan128 := @p2[0];
for i := 0 to 127 do
for j := 0 to 127 do
begin
c := pspan128[i, j];
outspan128[i, j] := R_ColorArrayAverage(
[pspan128[(i - 1) and 127, j], c, pspan128[(i + 1) and 127, j],
pspan128[i, (j - 1) and 127], c, pspan128[i, (j + 1) and 127]]);
end;
memfree(pspan128, SizeOf(span128x128_t));
end;
var
ds32cache: ds32cacheitem_tPArray;
//==============================================================================
//
// R_FindDS32Rover
//
//==============================================================================
function R_FindDS32Rover(const hash, lump: integer): Pds32cacheitem_t;
begin
result := ds32cache[hash];
while result <> nil do
begin
if result.lump = lump then
exit;
result := result.next;
end;
end;
//==============================================================================
//
// R_NewDS32Rover
//
//==============================================================================
function R_NewDS32Rover(const hash: integer): Pds32cacheitem_t;
begin
result := mallocz(SizeOf(ds32cacheitem_t));
result.lump := -1;
result.next := ds32cache[hash];
ds32cache[hash] := result;
end;
//==============================================================================
//
// R_ReadDS32Cache
//
//==============================================================================
procedure R_ReadDS32Cache(const flat: integer);
var
cachemiss: boolean;
hash: integer;
t: PTexture;
rover: Pds32cacheitem_t;
pds32: Pds32_t;
plw: PLongWord;
src1: PByte;
i: integer;
fsize: integer;
r1, b1, g1: byte;
r2, g2, b2: byte;
r, g, b: LongWord;
c: LongWord;
lump: integer;
lumplen: integer;
loops: integer;
curgamma: PByteArray;
pb: PByte;
pbstop: PByte;
tpal: PLongWordArray;
numpixels: integer;
flatname: string;
ds_source8: PByteArray;
begin
hash := flat and CACHEFLATMASK;
lump := R_GetLumpForFlat(flat);
rover := R_FindDS32Rover(hash, lump);
cachemiss := rover = nil;
if cachemiss then
rover := R_NewDS32Rover(hash);
if cachemiss or (rover.lump <> lump) then
begin
rover.lump := lump;
t := flats[flats[flat].translation].flat32;
if useexternaltextures and (t = nil) then
begin
flatname := W_GetNameForNum(lump);
t := T_LoadHiResTexture(flatname);
if t = nil then // JVAL: This allow to use Doomsday resource pack
t := T_LoadHiResTexture('flat-' + flatname);
if t = nil then
flats[flats[flat].translation].flat32 := pointer($1) // Mark as missing
else
begin
if t.GetWidth <= 64 then
fsize := 64
else if t.GetWidth <= 128 then
fsize := 128
else if t.GetWidth <= 256 then
fsize := 256
else
fsize := 512;
t.ScaleTo(fsize, fsize);
flats[flats[flat].translation].flat32 := t;
end;
end;
if useexternaltextures and (PCAST(t) > $1) then // if we have a hi resolution flat
begin
fsize := t.GetWidth;
if fsize = 512 then