forked from geissomatik/geiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_map.cpp
1093 lines (901 loc) · 39.8 KB
/
proc_map.cpp
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
// This file is part of: Geiss screensaver and Winamp music visualization plug-in
// ___ ___ ___ ___ ___
// / __| __|_ _/ __/ __|
// | (_ | _| | |\__ \__ \
// \___|___|___|___/___/
// 3-Clause BSD License
//
// Copyright (c) 1998-2022 Ryan Geiss (@geissomatik)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef GEISS_PROCESS_MAP
#define GEISS_PROCESS_MAP 1
#include "proc_map.h"
#include <ddraw.h>
#include <memoryapi.h>
// This disables warning
// "frame pointer register 'ebp' modified by inline assembly code"
// for MSVC; for why that's okay, see:
// https://stackoverflow.com/a/21718937/11626624
#pragma warning( disable: 4731 )
//-----for slider--------
//#include <math.h>
//extern int volpos;
//extern long intframe;
extern int slider1;
//extern int slider2;
extern bool bMMX;
//-----------------------
//------------------------------------------------------------------
static void *realAddress(void *fn)
{
#ifdef _DEBUG
// Debug Mode
char *ptr = (char *)fn;
return ptr + (*(int *)(ptr+1))+5;
#else
// Release Mode
return fn;
#endif
}
//------------------------------------------------------------------
int ALIGNCODE(int n, unsigned char *dest_code_buffer)
{
int ret = 0;
while ((DWORD)dest_code_buffer % n)
{
*dest_code_buffer++ = 0x90; // NOP
ret++;
}
return ret;
}
//------------------------------------------------------------------
int ADDBLOCK(unsigned char* fn, unsigned char *dest_code_buffer)
{
fn = (unsigned char *)realAddress(fn);
int ret = 0;
for (ret=0; ret<4; ret++)
dest_code_buffer[ret] = fn[ret];
do
{
dest_code_buffer[ret] = fn[ret];
ret++;
if (fn[ret-5]==0x68 && // watch for "push 0xDEADBEEF" end sequence
fn[ret-4]==0xEF &&
fn[ret-3]==0xBE &&
fn[ret-2]==0xAD &&
fn[ret-1]==0xDE
)
break;
}
while (1);
return ret - 5;
}
//------------------------------------------------------------------
int FixJumpTarget(unsigned char* code, int start_byte, int end_byte, int new_target_addr)
{
// finds the first occurrence of 'jnz 0' and replaces it with a
// [properly relative] jump to new_target_addr.
// returns the byte offset of the beginning of the next instruction AFTER
// the adjusted "jnz 0" instruction, or 0 on failure to find one.
for (int i=start_byte; i<end_byte-5; i++)
{
if (code[i] == 0x0F && code[i+1] == 0x85)
{
int* p = (int*)(&code[i+2]);
if (*p == 0)
{
*p = new_target_addr - i - 6;
return i+6;
}
}
}
return 0;
}
//------------------------------------------------------------------
int ReplaceDWORD(unsigned char* code, int start_byte, int end_byte,
DWORD old_value, DWORD new_value)
{
for (int i=start_byte; i<end_byte-4; i++)
{
if (*(DWORD*)(&code[i]) == old_value)
{
*(DWORD*)(&code[i]) = new_value;
return i;
}
}
return 0;
}
//------------------------------------------------------------------
__declspec ( naked ) void _return_baby(void) { __asm {
ret
// MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS:
push 0xDEADBEEF // 0x68DEADBEEF
emms
}
}
void* g_proc_map_p1;
void* g_proc_map_p2;
//------------------------------------------------------------------
__declspec ( naked ) void _proc_map_8bit_part01(void) { __asm {
mov ecx, FX_YCUT_NUM_LINES // our "loop counter", goes down to 0
push esi
push edi
mov ESI, DATA_FX
mov EDI, g_proc_map_p1
mov EBX, g_proc_map_p2 // will need this momentarily...
add ESI, FX_YCUT_xFXW_x8
//add EDI, FX_YCUT_xFXW
//add EDI, initial_map_offset
add EDI, slider1 // we start further into the transformation map...
//sub EDI, slider2
//add EBX, slider // we start further into the VS...
add EBX, FX_YCUT_xFXW
push ebp
mov EBP, ebx
// ESI: data array; ++8
// EDI: source screen; will be mutilitated and fixed repeatedly
// EBP: dest. screen; ++1
//xor eax, eax
//sub EDI, 32767
mov eax, [ESI+4]
dec ebp
shr ecx, 1
//sub ecx, 256
ALIGN 8
CyrixLoopA:
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part02(void) { __asm {
//---------------------------------------------------------------
//xor eax, eax
mov eax, [ESI+4]
xor dh, dh
add EDI, eax // now EDI points to VS1:topleft. upper 16 bits of eax always stay at zero.
// top left and top right pixels
mov ebx, [ESI]
mov al, [EDI]
mul bl
inc ebp
add dx, ax
mov al, [EDI+1]
mul bh
shr ebx, 16
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part03(void) { __asm {
//---------------------------------------------------------------
// btm left and btm right pixels
mov al, [EDI+23456]
mul bl
add esi, 8
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part04(void) { __asm {
//---------------------------------------------------------------
mov al, [EDI+23457]
mul bh
//sub EDI, 32767
add dx, ax
//dec dh
// now DH stores the averaged color value.
//xor eax, eax
mov eax, [ESI+4]
mov [ebp], dh // plot
xor dh, dh
add EDI, eax // now EDI points to VS1:topleft. upper 16 bits of eax always stay at zero.
// top left and top right pixels
mov ebx, [ESI]
mov ax, [EDI]
mul bl
dec ecx
inc ebp
add dx, ax
mov al, [EDI+1]
mul bh
shr ebx, 16
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part05(void) { __asm {
//---------------------------------------------------------------
// btm left and btm right pixels
mov al, [EDI+23456]
mul bl
add esi, 8
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part06(void) { __asm {
//---------------------------------------------------------------
mov al, [EDI+23457]
mul bh
//sub EDI, 32767
add dx, ax
//dec dh
// now DH stores the averaged color value.
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part07(void) { __asm {
//---------------------------------------------------------------
test cl, 63
mov [ebp], dh // plot
// Produces: "jnz 0", will be replaced live by "jnz CyrixLoopA"; every 256th pixel do some prefetches
_emit 0x0F
_emit 0x85
_emit 0x00
_emit 0x00
_emit 0x00
_emit 0x00
mov ebx, 8*128 // need to prefetch 256*6 bytes
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part08(void) { __asm {
//---------------------------------------------------------------
CYRIX_CACHE_ESI_A:
mov eax, [ebx+esi]
mov eax, [ebx+esi+32]
mov eax, [ebx+esi+64]
mov eax, [ebx+esi+96]
sub ebx, 128
// Produces: "jnz 0", will be replaced live by "jnz CYRIX_CACHE_ESI_A"
_emit 0x0F
_emit 0x85
_emit 0x00
_emit 0x00
_emit 0x00
_emit 0x00
xor eax, eax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_8bit_part09(void) { __asm {
//---------------------------------------------------------------
sub ecx, 0
// Produces: "jnz 0", will be replaced live by "jnz CyrixLoopA"
_emit 0x0F
_emit 0x85
_emit 0x00
_emit 0x00
_emit 0x00
_emit 0x00
pop ebp
pop edi
pop esi
// MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS:
push 0xDEADBEEF // 0x68DEADBEEF
emms
}
}
//------------------------------------------------------------------
__declspec ( naked ) void _proc_map_32bit_part01(void) { __asm {
mov ecx, FX_YCUT_NUM_LINES // our "loop counter", goes down to 0
push esi
push edi
mov ESI, DATA_FX // data_fx[]
add ESI, FX_YCUT_xFXW_x8
mov EDI, g_proc_map_p1//p1 // lookat pixel pointer
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, initial_map_offset
//add EDI, initial_map_offset
//add EDI, initial_map_offset
//add EDI, initial_map_offset
add EDI, slider1
add EDI, slider1
add EDI, slider1
add EDI, slider1
//sub EDI, slider2
//sub EDI, slider2
//sub EDI, slider2
//sub EDI, slider2
mov EBX, g_proc_map_p2//p2 // dest pixel pointer (will be EBP)
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
//add EDI, slider // we start further into the transformation map...
//add EBX, slider // we start further into the VS...
push ebp
mov EBP, ebx
// ESI: data array; ++8
// EDI: source screen; will be mutilitated and fixed repeatedly
// EBP: dest. screen; ++1
//sub EDI, 32767*4
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part02(void) { __asm {
//---------------------------------------------------------------
NewPixelLoop1:
xor eax, eax
mov eax, [ESI+4] // amount to move lookat pixel + 32767 (we pre-subtracted)
//shl eax, 2 // move that # times 4
add EDI, eax // apply; now EDI points to VS1:topleft. upper 16 bits of eax always stay at zero.
mov ebx, [ESI] // the 4 weights
// ------------ RED PIXEL ---------------------------------------
// top left and top right pixels
mov al, [EDI]
xor dh, dh // INIT: leave some random dither
mul bl
add dx, ax
mov al, [EDI+4]
mul bh
ror ebx, 16
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part03(void) { __asm {
//---------------------------------------------------------------
// btm left and btm right pixels
mov al, [EDI+17387]//[EDI+320*4]
mul bl
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part04(void) { __asm {
//---------------------------------------------------------------
mov al, [EDI+17388]//[EDI+320*4+4]
mul bh
add dx, ax // u-pipe
// now DH stores the averaged color value.
mov [ebp], dh // plot
// ----------- GREEN PIXEL --------------------------------------
inc EDI // INIT: lookat pixel
rol EBX, 16 // INIT: start w/weights in original place
//inc EBP // INIT: dest pixel
//xor dh, dh // INIT: leave some random dither
// top left and top right pixels
mov al, [EDI]
xor dh, dh // INIT:leave some random dither
mul bl
inc EBP // INIT: dest pixel
add dx, ax
mov al, [EDI+4]
mul bh
ror ebx, 16
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part05(void) { __asm {
//---------------------------------------------------------------
// btm left and btm right pixels
mov al, [EDI+17387]//[EDI+320*4]
mul bl
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part06(void) { __asm {
//---------------------------------------------------------------
mov al, [EDI+17388]//[EDI+320*4+4]
mul bh
add dx, ax
// now DH stores the averaged color value.
mov [ebp], dh // plot
// ----------- BLUE PIXEL --------------------------------------
inc EDI // INIT: lookat pixel
rol EBX, 16 // INIT: start w/weights in original place
//inc EBP // INIT: dest pixel
//xor dh, dh // INIT: leave some random dither
// top left and top right pixels
mov al, [EDI]
xor dh, dh // INIT:leave some random dither
mul bl
inc EBP // INIT: dest pixel
add dx, ax
mov al, [EDI+4]
mul bh
ror ebx, 16
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part07(void) { __asm {
//---------------------------------------------------------------
// btm left and btm right pixels
mov al, [EDI+17387]//[EDI+320*4]
mul bl
add esi, 8 // CLEANUP: move along through DATA_FX[]
add dx, ax
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part08(void) { __asm {
//---------------------------------------------------------------
mov al, [EDI+17388]//[EDI+320*4+4]
mul bh
//sub EDI, 32767*4+2 // CLEANUP: reset lookat pixel to what it should be & pre-subtract
sub EDI, 2 // CLEANUP: reset lookat pixel to what it should be & pre-subtract
add dx, ax // u-pipe
// now DH stores the averaged color value.
mov [ebp], dh // plot
// ----------- CLEANUP -----------------------------------------
add EBP, 2 // CLEANUP: dest pixel should be @ new position
//add esi, 6 // CLEANUP: move along through DATA_FX[]
//sub EDI, 32767*4+2 // CLEANUP: reset lookat pixel to what it should be & pre-subtract
//---------------------------------------------------------------
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
} } __declspec ( naked ) void _proc_map_32bit_part09(void) { __asm {
//---------------------------------------------------------------
dec ecx
// Produces: "jnz 0", will be replaced live by "jnz NewPixelLoop1"
_emit 0x0F
_emit 0x85
_emit 0x00
_emit 0x00
_emit 0x00
_emit 0x00
pop ebp
pop edi
pop esi
push 0xDEADBEEF // MANDATORY FOOTER FOR ALL OUR NAKED FUNCTIONS: 0x68DEADBEEF
emms
}
}
//---------------------------------------------------------------
// DON'T CALL THIS FUNCTION DIRECTLY
void Process_Map_Asm(void *p1, void *p2)//, LPDIRECTDRAWSURFACE lpDDSurf)
// DON'T CALL THIS FUNCTION DIRECTLY
{
//int slider = (!bScrollMap) ? 0 : FXW/2 + FXW/3*sin(intframe*0.059) + FXW/7*sin(intframe*0.044);
unsigned __int16 NUM_LINES = (unsigned __int16)((FXH-FX_YCUT*2));
unsigned __int16 NUM_PIXELS = (unsigned __int16)(FXW);
unsigned __int32 SCANLINE_PADDING = 0;
unsigned __int32 FX_YCUT_xPITCH = 0;
void *video = NULL;
g_proc_map_p1 = p1;
g_proc_map_p2 = p2;
// ------------- begin building custom code seg -----------------
if (iDispBits == 8)
{
// copy the code into a data segment, fix up the jump targets
// and hard-code our custom variables into the instructions (...our
// solution for not having enough registers), then execute the code.
const SIZE_T code_size_bytes = 8192;
unsigned char * const code = (unsigned char *)VirtualAlloc(NULL, code_size_bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
int codepos=0;
int start_codepos[16];
if (code == NULL) {
return;
}
//----------------
// Paste all the function chunks together into one piece of code.
//----------------
codepos += ALIGNCODE(16, &code[codepos]);
start_codepos[1] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part01, &code[codepos]);
start_codepos[2] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part02, &code[codepos]);
start_codepos[3] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part03, &code[codepos]);
start_codepos[4] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part04, &code[codepos]);
start_codepos[5] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part05, &code[codepos]);
start_codepos[6] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part06, &code[codepos]);
start_codepos[7] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part07, &code[codepos]);
start_codepos[8] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part08, &code[codepos]);
start_codepos[9] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_8bit_part09, &code[codepos]);
start_codepos[10] = codepos; codepos += ADDBLOCK((unsigned char *)_return_baby, &code[codepos]);
start_codepos[11] = codepos;
int ret;
//----------------
// Fix up the "jnz 0" instructions
//----------------
ret = FixJumpTarget(code, start_codepos[7], start_codepos[ 8], start_codepos[2]);
ret = FixJumpTarget(code, start_codepos[8], start_codepos[ 9], start_codepos[8]);
ret = FixJumpTarget(code, start_codepos[9], start_codepos[10], start_codepos[2]);
//----------------
// Write some custom variables
//----------------
ret = ReplaceDWORD(code, start_codepos[3], start_codepos[4], 23456, FXW );
ret = ReplaceDWORD(code, start_codepos[4], start_codepos[5], 23457, FXW+1);
ret = ReplaceDWORD(code, start_codepos[5], start_codepos[6], 23456, FXW );
ret = ReplaceDWORD(code, start_codepos[6], start_codepos[7], 23457, FXW+1);
//----------------
// EXECUTE
//----------------
void* start_ptr = (void*)code;
__asm
{
pusha // release builds will break without this!
call start_ptr
popa // release builds will break without this!
emms
};
VirtualFree(code, code_size_bytes, MEM_DECOMMIT);
}
else //32-bit case
{
if (!bMMX)
{
// copy the code into a data segment, fix up the jump targets
// and hard-code our custom variables into the instructions (...our
// solution for not having enough registers), then execute the code.
const SIZE_T code_size_bytes = 8192;
unsigned char * const code = (unsigned char *)VirtualAlloc(NULL, code_size_bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
int codepos=0;
int start_codepos[16];
if (code == NULL) {
return;
}
//----------------
// Paste all the function chunks together into one piece of code.
//----------------
codepos += ALIGNCODE(16, &code[codepos]);
start_codepos[1] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part01, &code[codepos]);
start_codepos[2] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part02, &code[codepos]);
start_codepos[3] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part03, &code[codepos]);
start_codepos[4] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part04, &code[codepos]);
start_codepos[5] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part05, &code[codepos]);
start_codepos[6] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part06, &code[codepos]);
start_codepos[7] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part07, &code[codepos]);
start_codepos[8] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part08, &code[codepos]);
start_codepos[9] = codepos; codepos += ADDBLOCK((unsigned char *)_proc_map_32bit_part09, &code[codepos]);
start_codepos[10] = codepos; codepos += ADDBLOCK((unsigned char *)_return_baby, &code[codepos]);
start_codepos[11] = codepos;
int ret;
//----------------
// Fix up the "jnz 0" instructions
//----------------
ret = FixJumpTarget(code, start_codepos[9], start_codepos[10], start_codepos[2]);
//----------------
// Write some custom variables
//----------------
ret = ReplaceDWORD(code, start_codepos[3], start_codepos[4], 17387, FXW*4 );
ret = ReplaceDWORD(code, start_codepos[4], start_codepos[5], 17388, FXW*4+4);
ret = ReplaceDWORD(code, start_codepos[5], start_codepos[6], 17387, FXW*4 );
ret = ReplaceDWORD(code, start_codepos[6], start_codepos[7], 17388, FXW*4+4);
ret = ReplaceDWORD(code, start_codepos[7], start_codepos[8], 17387, FXW*4 );
ret = ReplaceDWORD(code, start_codepos[8], start_codepos[9], 17388, FXW*4+4);
//----------------
// EXECUTE
//----------------
void* start_ptr = (void*)code;
__asm
{
pusha // release builds will break without this!
call start_ptr
popa // release builds will break without this!
emms
};
VirtualFree(code, code_size_bytes, MEM_DECOMMIT);
}
else // MMX version
{
__asm // MMX version. Works 100%, except you must write the weights in the order +2+3+0+1 (vs. +0+1+2+3).
// Doesn't hold up very well when other windows are on the screen!
// Sort of sucks... it's only about 60% as fast as the integer version.
{
push esi
push edi
mov ESI, DATA_FX // data_fx[]
add ESI, FX_YCUT_xFXW_x8
mov EDI, p1 // lookat pixel pointer
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, FX_YCUT_xFXW
//add EDI, initial_map_offset
//add EDI, initial_map_offset
//add EDI, initial_map_offset
//add EDI, initial_map_offset
add EDI, slider1
add EDI, slider1
add EDI, slider1
add EDI, slider1
//sub EDI, slider2
//sub EDI, slider2
//sub EDI, slider2
//sub EDI, slider2
mov EBX, p2 // dest pixel pointer (will be EBP)
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
add EBX, FX_YCUT_xFXW
//add EDI, slider // we start further into the transformation map...
//add EBX, slider // we start further into the VS...
// ESI: data array; ++8
// EDI: source screen; will be mutilitated and fixed repeatedly
// EBX: dest. screen; ++1
// EBP: video mem pointer
//sub EDI, 32767*4
mov cx, NUM_LINES // our "loop counter", goes down to 0
mov edx, FXW
add edx, edx
add edx, edx // edx holds FXW*4
//vid mov eax, video
//vid add eax, FX_YCUT_xPITCH
push ebp
//vid mov ebp, eax
pxor mm7, mm7
ALIGN 8
ScanlineLoop2:
//vid mov eax, ebp // back up our video mem pointer
pop ebp // get base pointer back (to get to NUM_PIXELS)
shl ecx, 16
mov cx, NUM_PIXELS
// also, add our scanline padding =)
//vid add eax, SCANLINE_PADDING
push ebp // save base pointer onto stack
//vid mov ebp, eax // restore video mem pointer
ALIGN 8
PixelLoop2:
test bx, 0x03ff
jnz PROCESS_PIXEL_MMX32 // every 256th pixel do some prefetches
push ebx
mov ebx, 8*256 // need to prefetch 256*6 bytes
LOAD_ESI_ARRAY_MMX32:
mov eax, [ebx+esi]
mov eax, [ebx+esi+32]
sub ebx, 64
jnz LOAD_ESI_ARRAY_MMX32
pop ebx
PROCESS_PIXEL_MMX32:
/*
// BACKUP OF UNINTERLEAVED 32-BIT MMX LOOP
// BACKUP OF UNINTERLEAVED 32-BIT MMX LOOP
// BACKUP OF UNINTERLEAVED 32-BIT MMX LOOP
movq mm0, dword ptr [esi] ; 0 0 0 0 c3 c2 c1 c0
pxor mm7, mm7
punpcklbw mm0, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
movq mm2, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
punpcklbw mm0, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
punpckhbw mm2, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
movq mm1, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
movq mm3, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
punpcklbw mm0, mm7 ; 0 c0 0 c0 0 c0 0 c0
punpckhbw mm1, mm7 ; 0 c1 0 c1 0 c1 0 c1
punpcklbw mm2, mm7 ; 0 c2 0 c2 0 c2 0 c2
punpckhbw mm3, mm7 ; 0 c3 0 c3 0 c3 0 c3
// fake (equal) weights:
//mov eax, 0x40404040
//movd mm0, eax ; 0 0 0 0 64 64 64 64
//punpcklbw mm0, mm0 ; 64 64 64 64 64 64 64 64
//psrlw mm0, 8 ; 0 64 0 64 0 64 0 64
//movq mm1, mm0
//movq mm2, mm0
//movq mm3, mm0
// now get the colors, multiply, and add:
movq mm4, qword ptr [edi] ; a1 b1 g1 r1 a0 b0 g0 r0
movq mm5, mm4 ; a1 b1 g1 r1 a0 b0 g0 r0
punpcklbw mm4, mm7 ; 0 a0 0 b0 0 g0 0 r0
punpckhbw mm5, mm7 ; 0 a1 0 b1 0 g1 0 r1
pmullw mm0, mm4 ; a0*c0 b0*c0 g0*c0 r0*c0
pmullw mm1, mm5 ; a1*c1 b1*c1 g1*c1 r1*c1
movq mm4, qword ptr [edi+edx] ; a3 b3 g3 r3 a2 b2 g2 r2
movq mm5, mm4 ; a3 b3 g3 r3 a2 b2 g2 r2
punpcklbw mm4, mm7 ; 0 a2 0 b2 0 g2 0 r2
punpckhbw mm5, mm7 ; 0 a3 0 b3 0 g3 0 r3
pmullw mm2, mm4 ; a2*c2 b2*c2 g2*c2 r2*c2
pmullw mm3, mm5 ; a3*c3 b3*c3 g3*c3 r3*c3
paddw mm0, mm1
paddw mm0, mm2
paddw mm0, mm3 ; a' b' g' r'
//now get the colors
//xor eax, eax
//mov ax, [ESI+4]
//shl eax, 2 // move that # times 4
//add EDI, eax
//PUNPCKHBW MM4, [EDI]
//PUNPCKHBW MM5, [EDI+4]
//PUNPCKHBW MM6, [EDI+640*4]
//PUNPCKHBW MM7, [EDI+641*4]
//PSRLW mm4, 8
//PSRLW mm5, 8
//PSRLW mm6, 8
//PSRLW mm7, 8
//PMULLW MM0, MM4
//PMULLW MM1, MM5
//PMULLW MM2, MM6
//PMULLW MM3, MM7
//PADDW MM0, MM1
//PADDW MM2, MM3
//PADDW MM0, MM2
psrlw mm0, 8 ; 0 a' 0 b' 0 g' 0 r'
sub EDI, 32767*4
packuswb mm0, mm0 ; a' b' g' r' a' b' g' r'
add ESI, 6
movd dword ptr [ebx], mm0 ; store to the destination array
add EBP, 4
dec ecx
jnz PixelLoop2
*/
/*
xor eax, eax
movq mm0, dword ptr [esi] ; 0 0 0 0 c3 c2 c1 c0
mov eax, [ESI+4]
punpcklbw mm0, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
//pxor mm7, mm7
//shl eax, 2 // move that # times 4
movq mm2, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
add EDI, eax
punpcklbw mm0, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
movq mm4, qword ptr [edi] ; a1 b1 g1 r1 a0 b0 g0 r0
punpckhbw mm2, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
movq mm1, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
movq mm5, mm4 ; a1 b1 g1 r1 a0 b0 g0 r0
movq mm3, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
punpcklbw mm4, mm7 ; 0 a0 0 b0 0 g0 0 r0
punpcklbw mm0, mm7 ; 0 c0 0 c0 0 c0 0 c0
punpckhbw mm5, mm7 ; 0 a1 0 b1 0 g1 0 r1
punpckhbw mm1, mm7 ; 0 c1 0 c1 0 c1 0 c1
pmullw mm0, mm4 ; a0*c0 b0*c0 g0*c0 r0*c0
punpcklbw mm2, mm7 ; 0 c2 0 c2 0 c2 0 c2
pmullw mm1, mm5 ; a1*c1 b1*c1 g1*c1 r1*c1
punpckhbw mm3, mm7 ; 0 c3 0 c3 0 c3 0 c3
*/
//xor eax, eax
movq mm0, dword ptr [esi] ; 0 0 0 0 c3 c2 c1 c0
mov eax, [ESI+4]
punpcklbw mm0, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
//pxor mm7, mm7
//mov eax, [ESI+4]
//shl eax, 2 // move that # times 4
add EDI, eax
movq mm2, mm0 ; c3 c3 c2 c2 c1 c1 c0 c0
punpcklbw mm0, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
movq mm4, qword ptr [edi] ; a1 b1 g1 r1 a0 b0 g0 r0
punpckhbw mm2, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
movq mm1, mm0 ; c1 c1 c1 c1 c0 c0 c0 c0
movq mm5, mm4 ; a1 b1 g1 r1 a0 b0 g0 r0
movq mm3, mm2 ; c3 c3 c3 c3 c2 c2 c2 c2
punpcklbw mm4, mm7 ; 0 a0 0 b0 0 g0 0 r0
punpcklbw mm0, mm7 ; 0 c0 0 c0 0 c0 0 c0
punpckhbw mm5, mm7 ; 0 a1 0 b1 0 g1 0 r1
punpckhbw mm1, mm7 ; 0 c1 0 c1 0 c1 0 c1
pmullw mm0, mm4 ; a0*c0 b0*c0 g0*c0 r0*c0
punpcklbw mm2, mm7 ; 0 c2 0 c2 0 c2 0 c2
pmullw mm1, mm5 ; a1*c1 b1*c1 g1*c1 r1*c1
punpckhbw mm3, mm7 ; 0 c3 0 c3 0 c3 0 c3
// now get the colors, multiply, and add:
movq mm4, qword ptr [edi+edx] ; a3 b3 g3 r3 a2 b2 g2 r2
paddw mm0, mm1
movq mm5, mm4 ; a3 b3 g3 r3 a2 b2 g2 r2
punpcklbw mm4, mm7 ; 0 a2 0 b2 0 g2 0 r2
punpckhbw mm5, mm7 ; 0 a3 0 b3 0 g3 0 r3
paddw mm0, mm6 //DITHER 1
pmullw mm2, mm4 ; a2*c2 b2*c2 g2*c2 r2*c2
pmullw mm3, mm5 ; a3*c3 b3*c3 g3*c3 r3*c3
paddw mm0, mm2
paddw mm0, mm3 ; a' b' g' r'
movq mm6, mm0 //DITHER 2
// ---DITHER---
//paddusw mm0, mm6 ; DITHER: add old dither values
//movq mm6, mm0 ; DITHER: copy sum
//psllw mm6, 8 ; DITHER: save low bits for next dither values
//psrlw mm6, 8 ; DITHER: save low bits for next dither values
// ---DITHER---
psrlw mm0, 8 ; 0 a' 0 b' 0 g' 0 r'
psllw mm6, 8 //DITHER 3
//sub EDI, 32767*4
packuswb mm0, mm0 ; a' b' g' r' a' b' g' r'
add ESI, 8
movd dword ptr [ebx], mm0 ; store to the destination array
add EBX, 4
// store pseudo-random dither info for next time in EBP
//vid PADDUSB mm0, mm0
//vid movd dword ptr [ebp], mm0 ; store to VIDEO
//vid add EBP, 4
dec cx
psrlw mm6, 8 //DITHER 4
jnz PixelLoop2
shr ecx, 16
dec cx
jnz ScanlineLoop2
EMMS
pop ebp
pop edi
pop esi
} // end MMX version
//vid ddrval = lpDDSurf->Unlock(NULL);
//vid }
}
}
}