This repository was archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathvid_directx9.cpp
More file actions
2307 lines (1870 loc) · 61.9 KB
/
Copy pathvid_directx9.cpp
File metadata and controls
2307 lines (1870 loc) · 61.9 KB
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
// DirectX9 Enhanced video output
#include "burner.h"
#include "vid_softfx.h"
//#ifdef _MSC_VER
//#pragma comment(lib, "d3d9")
//#pragma comment(lib, "d3dx9")
// #define ENABLE_PROFILING FBA_DEBUG
// #define LOAD_EFFECT_FROM_FILE
#include <guiddef.h>
#define DIRECT3D_VERSION 0x0900 // Use this Direct3D version
#define D3D_OVERLOADS
#include <d3d9.h>
#include <d3dx9effect.h>
#include "directx9_core.h"
const float PI = 3.14159265358979323846f;
typedef struct _D3DLVERTEX2 {
union { FLOAT x; FLOAT dvX; };
union { FLOAT y; FLOAT dvY; };
union { FLOAT z; FLOAT dvZ; };
union { D3DCOLOR color; D3DCOLOR dcColor; };
union { D3DCOLOR specular; D3DCOLOR dcSpecular; };
union { FLOAT tu; FLOAT dvTU; };
union { FLOAT tv; FLOAT dvTV; };
union { FLOAT tu1; FLOAT dvTU1; };
union { FLOAT tv1; FLOAT dvTV1; };
#if(DIRECT3D_VERSION >= 0x0500)
#if (defined __cplusplus) && (defined D3D_OVERLOADS)
_D3DLVERTEX2() { }
_D3DLVERTEX2(FLOAT _x, FLOAT _y, FLOAT _z, D3DCOLOR _color, D3DCOLOR _specular, FLOAT _tu, FLOAT _tv, FLOAT _tu1, FLOAT _tv1)
{
x = _x; y = _y; z = _z;
color = _color; specular = _specular;
tu = _tu; tv = _tv;
tu1 = _tu1; tv1 = _tv1;
}
#endif
#endif
} D3DLVERTEX2, *LPD3DLVERTEX2;
#define D3DFVF_LVERTEX2 ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) | D3DFVF_TEXCOORDSIZE2(2) | D3DFVF_TEXCOORDSIZE2(3) )
#define RENDER_STRIPS (4)
#define DX9_SHADERPRECISION ((nVidBlitterOpt[nVidSelect] & (7 << 28)) >> 28)
#define DX9_FILTER ((nVidBlitterOpt[nVidSelect] & (3 << 24)) >> 24)
#define DX9_USE_PS20 ( nVidBlitterOpt[nVidSelect] & (1 << 9))
#define DX9_USE_FPTEXTURE ( nVidBlitterOpt[nVidSelect] & (1 << 8))
static IDirect3D9* pD3D = NULL; // Direct3D interface
static D3DPRESENT_PARAMETERS d3dpp;
static IDirect3DDevice9* pD3DDevice = NULL;
static IDirect3DVertexBuffer9* pVB[RENDER_STRIPS] = { NULL, };
static IDirect3DTexture9* pTexture = NULL;
static IDirect3DTexture9* pScanlineTexture[2] = { NULL, };
static int nTextureWidth = 0, nTextureHeight = 0;
static IDirect3DSurface9* pSurface = NULL;
static IDirect3DVertexBuffer9* pIntermediateVB = NULL;
static IDirect3DTexture9* pIntermediateTexture = NULL;
static int nIntermediateTextureWidth, nIntermediateTextureHeight;
static ID3DXEffect* pEffect = NULL;
static ID3DXTextureShader* pEffectShader = NULL;
static D3DXHANDLE hTechnique = NULL;
//static D3DXHANDLE hScanIntensity = NULL;
static IDirect3DTexture9* pEffectTexture = NULL;
static ID3DXFont* pFont = NULL; // OSD font
static D3DCOLOR osdColor = D3DCOLOR_ARGB(0xFF, 0xFF, 0xFF, 0xFF);
static double dPrevCubicB, dPrevCubicC;
static bool bUsePS14;
static int nGameWidth = 0, nGameHeight = 0; // Game screen size
static int nGameImageWidth, nGameImageHeight;
static int nRotateGame;
static int nImageWidth, nImageHeight/*, nImageZoom*/;
static RECT Dest;
static unsigned int nD3DAdapter;
// ----------------------------------------------------------------------------
#ifdef PRINT_DEBUG_INFO
static TCHAR* TextureFormatString(D3DFORMAT nFormat)
{
switch (nFormat) {
case D3DFMT_X1R5G5B5:
return _T("16-bit xRGB 1555");
case D3DFMT_R5G6B5:
return _T("16-bit RGB 565");
case D3DFMT_X8R8G8B8:
return _T("32-bit xRGB 8888");
case D3DFMT_A8R8G8B8:
return _T("32-bit ARGB 8888");
case D3DFMT_A16B16G16R16F:
return _T("64-bit ARGB 16161616fp");
case D3DFMT_A32B32G32R32F:
return _T("128-bit ARGB 32323232fp");
default:
return _T("unknown format");
}
return _T("unknown format");
}
#endif
// ----------------------------------------------------------------------------
static int GetTextureSize(int nSize)
{
int nTextureSize = 128;
while (nTextureSize < nSize) {
nTextureSize <<= 1;
}
return nTextureSize;
}
/*
static void PutPixel(unsigned char** ppSurface, unsigned int nColour)
{
switch (nVidScrnDepth) {
case 15:
*((unsigned short*)(*ppSurface)) = ((nColour >> 9) & 0x7C00) | ((nColour >> 6) & 0x03E0) | ((nColour >> 3) & 0x001F);
*ppSurface += 2;
break;
case 16:
*((unsigned short*)(*ppSurface)) = ((nColour >> 8) & 0xF800) | ((nColour >> 5) & 0x07E0) | ((nColour >> 3) & 0x001F);
*ppSurface += 2;
break;
case 24:
(*ppSurface)[0] = (nColour >> 16) & 0xFF;
(*ppSurface)[1] = (nColour >> 8) & 0xFF;
(*ppSurface)[2] = (nColour >> 0) & 0xFF;
*ppSurface += 3;
break;
case 32:
*((unsigned int*)(*ppSurface)) = nColour;
*ppSurface += 4;
break;
}
}
*/
static UINT dx9GetAdapter(wchar_t* name)
{
for (int a = pD3D->GetAdapterCount() - 1; a >= 0; a--)
{
D3DADAPTER_IDENTIFIER9 identifier;
pD3D->GetAdapterIdentifier(a, 0, &identifier);
if (identifier.DeviceName[11] == name[11]) {
return a;
}
}
return 0;
}
// Select optimal full-screen resolution
int dx9SelectFullscreenMode(VidSDisplayScoreInfo* pScoreInfo)
{
//int nWidth, nHeight;
if (bVidArcaderes) {
if (!VidSGetArcaderes((int*)&(pScoreInfo->nBestWidth), (int*)&(pScoreInfo->nBestHeight))) {
return 1;
}
} else {
if (nScreenSize) {
D3DFORMAT nFormat = (nVidDepth == 16) ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8;
D3DDISPLAYMODE dm;
memset(pScoreInfo, 0, sizeof(VidSDisplayScoreInfo));
pScoreInfo->nRequestedZoom = nScreenSize;
VidSInitScoreInfo(pScoreInfo);
// Enumerate the available screenmodes
for (int i = pD3D->GetAdapterModeCount(nD3DAdapter, nFormat) - 1; i >= 0; i--) {
if (FAILED(pD3D->EnumAdapterModes(nD3DAdapter, nFormat, i, &dm))) {
return 1;
}
pScoreInfo->nModeWidth = dm.Width;
pScoreInfo->nModeHeight = dm.Height;
VidSScoreDisplayMode(pScoreInfo);
}
if (pScoreInfo->nBestWidth == -1U) {
FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_UI_FULL_NOMODE));
FBAPopupDisplay(PUF_TYPE_ERROR);
return 1;
}
} else {
pScoreInfo->nBestWidth = nVidWidth;
pScoreInfo->nBestHeight = nVidHeight;
}
}
if (!bDrvOkay && (pScoreInfo->nBestWidth < 640 || pScoreInfo->nBestHeight < 480)) {
return 1;
}
return 0;
}
// ----------------------------------------------------------------------------
static void FillEffectTexture()
{
FLOAT B = (FLOAT)dVidCubicB;
FLOAT C = (FLOAT)dVidCubicC;
if (pEffect == NULL) {
return;
}
/*
if (dVidCubicSharpness > 0.5) {
C = dVidCubicSharpness;
B = 0.0;
} else {
C = dVidCubicSharpness;
B = 1.0 - 2.0 * C;
}
*/
if (DX9_SHADERPRECISION == 4 || bUsePS14) {
B = 0.0;
}
if (pEffectShader && pEffectTexture) {
pEffectShader->SetFloat("B", B);
pEffectShader->SetFloat("C", C);
_D3DXFillTextureTX(pEffectTexture, pEffectShader);
}
pEffect->SetFloat("B", B);
pEffect->SetFloat("C", C);
}
// ----------------------------------------------------------------------------
static void dx9ReleaseResources()
{
RELEASE(pEffect);
RELEASE(pEffectShader);
RELEASE(pEffectTexture);
RELEASE(pScanlineTexture[0]);
RELEASE(pScanlineTexture[1]);
RELEASE(pIntermediateTexture);
RELEASE(pSurface);
RELEASE(pTexture);
for (int y = 0; y < RENDER_STRIPS; y++) {
RELEASE(pVB[y]);
}
RELEASE(pIntermediateVB);
}
static int dx9Exit()
{
dx9ReleaseResources();
VidSFreeVidImage();
RELEASE(pFont);
RELEASE(pD3DDevice);
RELEASE(pD3D);
nRotateGame = 0;
return 0;
}
static int dx9SurfaceInit()
{
D3DFORMAT nFormat = D3DFMT_UNKNOWN;
if (nRotateGame & 1) {
nVidImageWidth = nGameHeight;
nVidImageHeight = nGameWidth;
} else {
nVidImageWidth = nGameWidth;
nVidImageHeight = nGameHeight;
}
nGameImageWidth = nVidImageWidth;
nGameImageHeight = nVidImageHeight;
if (bDrvOkay && (BurnDrvGetFlags() & BDF_16BIT_ONLY)) {
nVidImageDepth = 15;
} else {
nVidImageDepth = nVidScrnDepth;
}
switch (nVidImageDepth) {
case 32:
nFormat = D3DFMT_X8R8G8B8;
break;
case 24:
nFormat = D3DFMT_R8G8B8;
break;
case 16:
nFormat = D3DFMT_R5G6B5;
break;
case 15:
nFormat = D3DFMT_X1R5G5B5;
break;
}
nVidImageBPP = (nVidImageDepth + 7) >> 3;
nBurnBpp = nVidImageBPP; // Set Burn library Bytes per pixel
// Use our callback to get colors:
SetBurnHighCol(nVidImageDepth);
// Make the normal memory buffer
if (VidSAllocVidImage()) {
dx9Exit();
return 1;
}
if (FAILED(pD3DDevice->CreateOffscreenPlainSurface(nVidImageWidth, nVidImageHeight, nFormat, D3DPOOL_DEFAULT, &pSurface, NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create surface.\n"));
#endif
return 1;
}
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Allocated a %i x %i (%s) surface.\n"), nVidImageWidth, nVidImageHeight, TextureFormatString(nFormat));
#endif
nTextureWidth = GetTextureSize(nGameImageWidth);
nTextureHeight = GetTextureSize(nGameImageHeight);
if (FAILED(pD3DDevice->CreateTexture(nTextureWidth, nTextureHeight, 1, D3DUSAGE_RENDERTARGET, nFormat, D3DPOOL_DEFAULT, &pTexture, NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create texture.\n"));
#endif
return 1;
}
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Allocated a %i x %i (%s) image texture.\n"), nTextureWidth, nTextureHeight, TextureFormatString(nFormat));
#endif
return 0;
}
static int dx9EffectSurfaceInit()
{
D3DFORMAT nFormat;
if (DX9_FILTER == 2 && (bUsePS14 || (DX9_SHADERPRECISION != 0 && DX9_SHADERPRECISION != 2))) {
switch (DX9_SHADERPRECISION) {
case 0:
nFormat = D3DFMT_A32B32G32R32F;
break;
case 1:
nFormat = D3DFMT_A16B16G16R16F;
break;
case 2:
nFormat = D3DFMT_A32B32G32R32F;
break;
case 3:
nFormat = D3DFMT_A16B16G16R16F;
break;
default:
nFormat = D3DFMT_A8R8G8B8;
}
if (bUsePS14) {
nFormat = D3DFMT_A8R8G8B8;
}
if (FAILED(pD3DDevice->CreateTexture(1024, 1, 1, D3DUSAGE_RENDERTARGET, nFormat, D3DPOOL_DEFAULT, &pEffectTexture, NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create effects texture.\n"));
#endif
return 1;
}
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Allocated a %i x %i (%s) LUT texture.\n"), 1024, 1, TextureFormatString(nFormat));
#endif
}
if ((DX9_FILTER == 1 && bVidScanlines) || (DX9_FILTER == 2 && (bUsePS14 || DX9_SHADERPRECISION >= 2 || bVidScanlines))) {
if (nVidFullscreen) {
nIntermediateTextureWidth = GetTextureSize(nRotateGame ? nVidScrnHeight : nVidScrnWidth);
} else {
nIntermediateTextureWidth = GetTextureSize(nRotateGame ? SystemWorkArea.bottom - SystemWorkArea.top : SystemWorkArea.right - SystemWorkArea.left);
}
nIntermediateTextureHeight = nTextureHeight;
nFormat = D3DFMT_A8R8G8B8;
if ((DX9_FILTER == 2) && !bUsePS14 && DX9_USE_FPTEXTURE) {
#if 0
if ((DX9_SHADERPRECISION == 0 || DX9_SHADERPRECISION == 2) && !bVidScanlines) {
nFormat = D3DFMT_A32B32G32R32F;
} else {
nFormat = D3DFMT_A16B16G16R16F;
}
#else
nFormat = D3DFMT_A16B16G16R16F;
#endif
}
if (FAILED(pD3DDevice->CreateTexture(nIntermediateTextureWidth, nIntermediateTextureHeight, 1, D3DUSAGE_RENDERTARGET, nFormat, D3DPOOL_DEFAULT, &pIntermediateTexture, NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create intermediate texture.\n"));
#endif
return 1;
}
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Allocated a %i x %i (%s) intermediate texture.\n"), nIntermediateTextureWidth, nIntermediateTextureHeight, TextureFormatString(nFormat));
#endif
}
{
// Clear textures and set border colour
IDirect3DSurface9* pSurf;
pTexture->GetSurfaceLevel(0, &pSurf);
pD3DDevice->ColorFill(pSurf, 0, D3DCOLOR_XRGB(0, 0, 0));
RELEASE(pSurf);
if (pIntermediateTexture) {
pIntermediateTexture->GetSurfaceLevel(0, &pSurf);
pD3DDevice->ColorFill(pSurf, 0, D3DCOLOR_XRGB(0, 0, 0));
RELEASE(pSurf);
}
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(0, 0, 0));
pD3DDevice->SetSamplerState(1, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(0, 0, 0));
}
// Allocate scanline textures
for (int i = 0, nSize = 2; i < 2; i++, nSize <<= 1) {
RECT rect = { 0, 0, nSize, nSize };
IDirect3DSurface9* pSurf = NULL;
unsigned int scan2x2[] = { 0xFFFFFF, 0xFFFFFF,
0x000000, 0x000000 };
unsigned int scan4x4[] = { 0x9F9F9F, 0x9F9F9F, 0x9F9F9F, 0x9F9F9F,
0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF,
0x9F9F9F, 0x9F9F9F, 0x9F9F9F, 0x9F9F9F,
0x000000, 0x000000, 0x000000, 0x000000 };
unsigned int* scandata[] = { scan2x2, scan4x4 };
if (FAILED(pD3DDevice->CreateTexture(nSize, nSize, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &(pScanlineTexture[i]), NULL))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create texture.\n"));
#endif
return 1;
}
if (FAILED(pScanlineTexture[i]->GetSurfaceLevel(0, &pSurf))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't get texture surface.\n"));
#endif
}
if (FAILED(_D3DXLoadSurfaceFromMemory(pSurf, NULL, &rect, scandata[i], D3DFMT_X8R8G8B8, nSize * sizeof(int), NULL, &rect, D3DX_FILTER_NONE, 0))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: D3DXLoadSurfaceFromMemory failed.\n"));
#endif
}
RELEASE(pSurf);
}
return 0;
}
int dx9GeometryInit()
{
for (int y = 0; y < RENDER_STRIPS; y++) {
D3DLVERTEX2 vScreen[4];
// The polygons for the emulator image on the screen
{
int nWidth = pIntermediateTexture ? nImageWidth : nGameImageWidth;
int nHeight = nGameImageHeight;
int nTexWidth = pIntermediateTexture ? nIntermediateTextureWidth : nTextureWidth;
int nTexHeight = nTextureHeight;
// Add 0.0000001 to ensure consistent rounding
double dTexCoordXl = 0.0000001;
double dTexCoordXr = (double)nWidth / (double)nTexWidth + 0.0000001;
double dTexCoordYt = (double)nHeight * (double)(y + 0) / (double)(nTexHeight * RENDER_STRIPS) + 0.0000001;
double dTexCoordYb = (double)nHeight * (double)(y + 1) / (double)(nTexHeight * RENDER_STRIPS) + 0.0000001;
double dLeft = 1.0 - 2.0;
double dRight = 1.0 - 0.0;
double dTop = 1.0 - (((double)((nRotateGame & 1) ? (RENDER_STRIPS - y - 0) : (y + 0))) * 2.0 / RENDER_STRIPS);
double dBottom = 1.0 - (((double)((nRotateGame & 1) ? (RENDER_STRIPS - y - 1) : (y + 1))) * 2.0 / RENDER_STRIPS);
// ugly fix for flipped game, modified by regret
if ((nRotateGame & 1) && (nRotateGame & 2)) {
dTop = 1.0 - (((double)(y + 0)) * 2.0 / RENDER_STRIPS);
dBottom = 1.0 - (((double)(y + 1)) * 2.0 / RENDER_STRIPS);
}
else if (nRotateGame & 2) {
dTop = 1.0 - (((double)(RENDER_STRIPS - y - 0)) * 2.0 / RENDER_STRIPS);
dBottom = 1.0 - (((double)(RENDER_STRIPS - y - 1)) * 2.0 / RENDER_STRIPS);
}
if (pIntermediateTexture && bVidScanlines) {
dTexCoordXl += 0.5 / (double)nIntermediateTextureWidth;
dTexCoordXr += 0.5 / (double)nIntermediateTextureWidth;
dTexCoordYt += 1.0 / (double)nIntermediateTextureHeight;
dTexCoordYb += 1.0 / (double)nIntermediateTextureHeight;
}
#if 0
if (nPreScaleEffect) {
if (nPreScale & 1) {
nWidth *= nPreScaleZoom;
nTexWidth = nPreScaleTextureWidth;
}
if (nPreScale & 2) {
nHeight *= nPreScaleZoom;
nTexHeight = nPreScaleTextureHeight;
}
}
#endif
// Set up the vertices for the game image, including the texture coordinates for the game image only
// ugly fix for flipped game, modified by regret
if (nRotateGame & 1) {
vScreen[(nRotateGame & 2) ? 3 : 0] = D3DLVERTEX2(dTop, dLeft, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXr : dTexCoordXl, dTexCoordYt, 0, 0);
vScreen[(nRotateGame & 2) ? 2 : 1] = D3DLVERTEX2(dTop, dRight, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXl : dTexCoordXr, dTexCoordYt, 0, 0);
vScreen[(nRotateGame & 2) ? 1 : 2] = D3DLVERTEX2(dBottom, dLeft, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXr : dTexCoordXl, dTexCoordYb, 0, 0);
vScreen[(nRotateGame & 2) ? 0 : 3] = D3DLVERTEX2(dBottom, dRight, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXl : dTexCoordXr, dTexCoordYb, 0, 0);
} else {
vScreen[(nRotateGame & 2) ? 3 : 0] = D3DLVERTEX2(dLeft, dTop, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXr : dTexCoordXl, dTexCoordYt, 0, 0);
vScreen[(nRotateGame & 2) ? 2 : 1] = D3DLVERTEX2(dRight, dTop, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXl : dTexCoordXr, dTexCoordYt, 0, 0);
vScreen[(nRotateGame & 2) ? 1 : 2] = D3DLVERTEX2(dLeft, dBottom, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXr : dTexCoordXl, dTexCoordYb, 0, 0);
vScreen[(nRotateGame & 2) ? 0 : 3] = D3DLVERTEX2(dRight, dBottom, 0.0, 0xFFFFFFFF, 0, (nRotateGame & 2) ? dTexCoordXl : dTexCoordXr, dTexCoordYb, 0, 0);
}
{
// Set up the 2nd pair of texture coordinates, used for scanlines or the high performance version of the cubic filter
double dScanOffset = nGameImageHeight / -2.0;
if (bVidScanlines) {
int s, z = nImageHeight / nGameImageHeight;
for (s = 2; s < z; s <<= 1) { }
dScanOffset += 0.5 / s;
} else {
dScanOffset += 0.5;
}
// Set the texture coordinates for the scanlines
if (nRotateGame & 1) {
vScreen[nRotateGame & 2 ? 3 : 0].tu1 = dScanOffset + 0.0; vScreen[nRotateGame & 2 ? 3 : 0].tv1 = dScanOffset + 0.0 + 0.0000001;
vScreen[nRotateGame & 2 ? 2 : 1].tu1 = dScanOffset + (double)nGameHeight; vScreen[nRotateGame & 2 ? 2 : 1].tv1 = dScanOffset + 0.0 + 0.0000001;
vScreen[nRotateGame & 2 ? 1 : 2].tu1 = dScanOffset + 0.0; vScreen[nRotateGame & 2 ? 1 : 2].tv1 = dScanOffset + (double)nGameWidth / RENDER_STRIPS + 0.0000001;
vScreen[nRotateGame & 2 ? 0 : 3].tu1 = dScanOffset + (double)nGameHeight; vScreen[nRotateGame & 2 ? 0 : 3].tv1 = dScanOffset + (double)nGameWidth / RENDER_STRIPS + 0.0000001;
} else {
vScreen[nRotateGame & 2 ? 3 : 0].tu1 = dScanOffset + 0.0; vScreen[nRotateGame & 2 ? 3 : 0].tv1 = dScanOffset + 0.0 + 0.0000001;
vScreen[nRotateGame & 2 ? 2 : 1].tu1 = dScanOffset + (double)nGameWidth; vScreen[nRotateGame & 2 ? 2 : 1].tv1 = dScanOffset + 0.0 + 0.0000001;
vScreen[nRotateGame & 2 ? 1 : 2].tu1 = dScanOffset + 0.0; vScreen[nRotateGame & 2 ? 1 : 2].tv1 = dScanOffset + (double)nGameHeight / RENDER_STRIPS + 0.0000001;
vScreen[nRotateGame & 2 ? 0 : 3].tu1 = dScanOffset + (double)nGameWidth; vScreen[nRotateGame & 2 ? 0 : 3].tv1 = dScanOffset + (double)nGameHeight / RENDER_STRIPS + 0.0000001;
}
}
}
{
D3DLVERTEX2* pVertexData;
if (FAILED(pVB[y]->Lock(0, 4 * sizeof(D3DLVERTEX2), (void**)&pVertexData, 0))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create vertex buffer.\n"));
#endif
return 1;
}
memcpy(pVertexData, &vScreen, 4 * sizeof(D3DLVERTEX2));
pVB[y]->Unlock();
}
}
if (bVidScanlines) {
pEffect->SetTexture("scanTexture", pScanlineTexture[(nImageHeight / nGameImageHeight >= 4) ? 1 : 0]);
}
if (pIntermediateTexture) {
D3DLVERTEX2* pVertexData;
D3DLVERTEX2 vTemp[4];
double dTexCoordXl = 0.0;
double dTexCoordXr = (double)nGameImageWidth / (double)nTextureWidth;
double dTexCoordYt = 0.0 + 0.0000001;
double dTexCoordYb = 1.0 + 0.0000001;
if (DX9_FILTER == 1) {
dTexCoordYt -= 0.5 / nTextureHeight;
dTexCoordYb -= 0.5 / nTextureHeight;
}
vTemp[0] = D3DLVERTEX2(-1.0, 1.0, 0.0, 0xFFFFFFFF, 0, dTexCoordXl, dTexCoordYt, 0.5 , 0.5);
vTemp[1] = D3DLVERTEX2( 1.0, 1.0, 0.0, 0xFFFFFFFF, 0, dTexCoordXr, dTexCoordYt, 0.5 + nGameImageWidth, 0.5);
vTemp[2] = D3DLVERTEX2(-1.0, -1.0, 0.0, 0xFFFFFFFF, 0, dTexCoordXl, dTexCoordYb, 0.5, 0.5 + nGameImageHeight);
vTemp[3] = D3DLVERTEX2( 1.0, -1.0, 0.0, 0xFFFFFFFF, 0, dTexCoordXr, dTexCoordYb, 0.5 + nGameImageWidth, 0.5 + nGameImageHeight);
if (FAILED(pIntermediateVB->Lock(0, 4 * sizeof(D3DLVERTEX2), (void**)&pVertexData, 0))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create vertex buffer.\n"));
#endif
return 1;
}
memcpy(pVertexData, &vTemp, 4 * sizeof(D3DLVERTEX2));
pIntermediateVB->Unlock();
}
return 0;
}
int dx9EffectInit()
{
// Set up the bicubic filter
// char szB[MAX_PATH] = "(1.0)";
// char szC[MAX_PATH] = "(0.0)";
// D3DXMACRO d3dxm[] = {{ "USE_BC_MACRO", "1", }, { "B", szB, }, { "C", szC, }, { NULL, NULL, }};
char* pszTechniqueOpt[] = { "ScanBilinear", "Bilinear", "ScanPoint", "Point" };
char* pszTechnique = NULL;
ID3DXBuffer* pErrorBuffer = NULL;
FLOAT pFloatArray[2];
pEffect = NULL;
pEffectShader = NULL;
_D3DXCreateBuffer(0x10000, &pErrorBuffer);
#ifdef LOAD_EFFECT_FROM_FILE
if (FAILED(_D3DXCreateEffectFromFile(pD3DDevice, _T("bicubic.fx"), NULL /* d3dxm */, NULL, D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &pEffect, &pErrorBuffer))) {
#else
if (FAILED(_D3DXCreateEffectFromResource(pD3DDevice, NULL, MAKEINTRESOURCE(ID_DX9EFFECT), NULL /* d3dxm */, NULL, D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &pEffect, &pErrorBuffer))) {
#endif
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't compile effect.\n"));
dprintf(_T("\n%hs\n\n"), pErrorBuffer->GetBufferPointer());
#endif
goto HANDLE_ERROR;
}
// Check if we need to use PS1.4 (instead of 2.0)
bUsePS14 = !DX9_USE_PS20;
if (pEffect->GetTechniqueByName("SinglePassHQBicubic") == NULL) {
bUsePS14 = true;
}
if (dx9EffectSurfaceInit()) {
goto HANDLE_ERROR;
}
switch (DX9_FILTER) {
case 1: {
pszTechnique = bVidScanlines ? pszTechniqueOpt[0] : pszTechniqueOpt[1];
break;
}
case 2: {
if (bVidScanlines) {
char* pszFunction[8] = { "ScanHQBicubic", "ScanBicubic", "ScanHQBicubic", "ScanBicubic", "ScanHP20Bicubic", "ScanHP14Bicubic", "ScanHP14Bicubic", "ScanHP14Bicubic" };
pszTechnique = pszFunction[bUsePS14 ? 7 : DX9_SHADERPRECISION];
} else {
char* pszFunction[8] = { "SinglePassHQBicubic", "SinglePassBicubic", "MultiPassHQBicubic", "MultiPassBicubic", "MultiPassHP20Bicubic", "MultiPassHP14Bicubic", "MultiPassHP14Bicubic", "MultiPassHP14Bicubic" };
pszTechnique = pszFunction[bUsePS14 ? 7 : DX9_SHADERPRECISION];
}
break;
}
default: {
pszTechnique = bVidScanlines ? pszTechniqueOpt[2] : pszTechniqueOpt[3];
break;
}
}
pEffect->SetTexture("imageTexture", pTexture);
pEffect->SetTexture("intermediateTexture", pIntermediateTexture);
pEffect->SetTexture("scanTexture", pScanlineTexture[(nImageHeight / nGameImageHeight >= 4) ? 1 : 0]);
pFloatArray[0] = nTextureWidth; pFloatArray[1] = nTextureHeight;
pEffect->SetFloatArray("imageSize", pFloatArray, 2);
pFloatArray[0] = 1.0 / nTextureWidth; pFloatArray[1] = 1.0 / nTextureHeight;
pEffect->SetFloatArray("texelSize", pFloatArray, 2);
/*
{
FLOAT fScanIntensity[4] = { (float)((nVidScanIntensity >> 16) & 255) / 255.0,
(float)((nVidScanIntensity >> 8) & 255) / 255.0,
(float)((nVidScanIntensity >> 0) & 255) / 255.0,
(float)((nVidScanIntensity >> 24) & 255) / 255.0 };
pEffect->SetFloatArray("scanIntensity", fScanIntensity, 4);
}
*/
hTechnique = pEffect->GetTechniqueByName(pszTechnique);
if (FAILED(pEffect->SetTechnique(hTechnique))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't set technique.\n"));
#endif
goto HANDLE_ERROR;
}
pEffect->SetTexture("weightTex", pEffectTexture);
{
ID3DXBuffer* pEffectShaderBuffer = NULL;
_D3DXCreateBuffer(0x10000, &pEffectShaderBuffer);
#ifdef LOAD_EFFECT_FROM_FILE
if (FAILED(_D3DXCompileShaderFromFile(_T("bicubic.fx"), NULL, NULL, (DX9_SHADERPRECISION < 4 && !bUsePS14) ? "genWeightTex20" : "genWeightTex14", "tx_1_0", 0, &pEffectShaderBuffer, &pErrorBuffer, NULL))) {
#else
if (FAILED(_D3DXCompileShaderFromResource(NULL, MAKEINTRESOURCE(ID_DX9EFFECT), NULL, NULL, (DX9_SHADERPRECISION < 4 && !bUsePS14) ? "genWeightTex20" : "genWeightTex14", "tx_1_0", 0, &pEffectShaderBuffer, &pErrorBuffer, NULL))) {
#endif
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't compile shader.\n"));
dprintf(_T("\n%hs\n\n"), pErrorBuffer->GetBufferPointer());
#endif
goto HANDLE_ERROR;
}
if (FAILED(_D3DXCreateTextureShader((DWORD*)(pEffectShaderBuffer->GetBufferPointer()), &pEffectShader))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create texture shader.\n"));
#endif
RELEASE(pEffectShaderBuffer);
goto HANDLE_ERROR;
}
FillEffectTexture();
RELEASE(pEffectShaderBuffer);
}
RELEASE(pErrorBuffer);
return 0;
HANDLE_ERROR:
RELEASE(pErrorBuffer);
return 1;
}
// ==> osd for dx9 video output (ugly), added by regret
static int dx9CreateFont()
{
if (pFont) {
return 0;
}
HRESULT hr = _D3DXCreateFont(pD3DDevice, d3dpp.BackBufferHeight / 18,
0, FW_DEMIBOLD, 1, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
DEFAULT_PITCH || FF_DONTCARE,
_T("Arial"), &pFont);
if (FAILED(hr)) {
return 1;
}
return 0;
}
static void dx9DrawText()
{
if (nVidSDisplayStatus == 0 || nOSDTimer == 0) {
return;
}
if (nFramesEmulated > nOSDTimer) {
VidSKillShortMsg();
VidSKillOSDMsg();
}
RECT osdRect;
if (nVidFullscreen) {
osdRect.left = Dest.left;
osdRect.top = Dest.top;
osdRect.right = Dest.right - 1;
osdRect.bottom = Dest.bottom - 1;
} else {
osdRect.left = 0;
osdRect.top = 0;
osdRect.right = Dest.right - Dest.left - 1;
osdRect.bottom = Dest.bottom - Dest.top - 1;
}
pFont->DrawText(NULL, OSDMsg, -1, &osdRect, DT_RIGHT | DT_TOP, osdColor);
}
// <== osd for dx9 video output (ugly)
static int dx9Init()
{
if (nVidFullscreen && !hScrnWnd) {
return 1;
}
#ifdef ENABLE_PROFILING
ProfileInit();
#endif
#ifdef PRINT_DEBUG_INFO
dprintf(_T("*** Initialising Direct3D 9 blitter.\n"));
#endif
hVidWnd = hScrnWnd; // Use Screen window for video
// Get pointer to Direct3D
if ((pD3D = _Direct3DCreate9(D3D_SDK_VERSION)) == NULL) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't initialise Direct3D.\n"));
#endif
dx9Exit();
return 1;
}
nRotateGame = 0;
if (bDrvOkay) {
if (BurnDrvGetFlags() & BDF_ORIENTATION_VERTICAL) {
if (nVidRotationAdjust & 1) {
nRotateGame |= (nVidRotationAdjust & 2);
} else {
nRotateGame |= 1;
}
}
if (BurnDrvGetFlags() & BDF_ORIENTATION_FLIPPED) {
nRotateGame ^= 2;
}
}
nD3DAdapter = D3DADAPTER_DEFAULT;
if (nRotateGame & 1 && VerScreen[0]) {
nD3DAdapter = dx9GetAdapter(VerScreen);
} else {
if (HorScreen[0]) {
nD3DAdapter = dx9GetAdapter(HorScreen);
}
}
memset(&d3dpp, 0, sizeof(d3dpp));
if (nVidFullscreen) {
VidSDisplayScoreInfo ScoreInfo;
if (dx9SelectFullscreenMode(&ScoreInfo)) {
dx9Exit();
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't determine display mode.\n"));
#endif
return 1;
}
d3dpp.BackBufferWidth = ScoreInfo.nBestWidth;
d3dpp.BackBufferHeight = ScoreInfo.nBestHeight;
d3dpp.BackBufferFormat = (nVidDepth == 16) ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.BackBufferCount = bVidTripleBuffer ? 2 : 1;
d3dpp.hDeviceWindow = hVidWnd;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
} else {
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.hDeviceWindow = hVidWnd;
d3dpp.Windowed = TRUE;
d3dpp.PresentationInterval = bVidVSync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
}
DWORD dwBehaviorFlags = bVidVSync ? (D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE | D3DCREATE_MULTITHREADED) : (D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE);
#ifdef _DEBUG
dwBehaviorFlags |= D3DCREATE_DISABLE_DRIVER_MANAGEMENT;
#endif
if (FAILED(pD3D->CreateDevice(nD3DAdapter, D3DDEVTYPE_HAL, hVidWnd, dwBehaviorFlags, &d3dpp, &pD3DDevice))) {
// if (FAILED(pD3D->CreateDevice(pD3D->GetAdapterCount() - 1, D3DDEVTYPE_REF, hVidWnd, dwBehaviorFlags, &d3dpp, &pD3DDevice))) {
#ifdef PRINT_DEBUG_INFO
dprintf(_T(" * Error: Couldn't create Direct3D device.\n"));
#endif
if (nVidFullscreen) {
FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_UI_FULL_PROBLEM));
if (bVidArcaderes && (d3dpp.BackBufferWidth != 320 && d3dpp.BackBufferHeight != 240)) {
FBAPopupAddText(PUF_TEXT_DEFAULT, MAKEINTRESOURCE(IDS_ERR_UI_FULL_CUSTRES));
}
FBAPopupDisplay(PUF_TYPE_ERROR);
}
dx9Exit();
return 1;
}
{
D3DDISPLAYMODE dm;
pD3D->GetAdapterDisplayMode(nD3DAdapter, &dm);
nVidScrnWidth = dm.Width; nVidScrnHeight = dm.Height;
nVidScrnDepth = (dm.Format == D3DFMT_R5G6B5) ? 16 : 32;
}
nGameWidth = nVidImageWidth; nGameHeight = nVidImageHeight;
nRotateGame = 0;
if (bDrvOkay) {
// Get the game screen size
BurnDrvGetVisibleSize(&nGameWidth, &nGameHeight);
if (BurnDrvGetFlags() & BDF_ORIENTATION_VERTICAL) {
if (nVidRotationAdjust & 1) {
int n = nGameWidth;
nGameWidth = nGameHeight;
nGameHeight = n;
nRotateGame |= (nVidRotationAdjust & 2);
} else {
nRotateGame |= 1;
}
}
if (BurnDrvGetFlags() & BDF_ORIENTATION_FLIPPED) {
nRotateGame ^= 2;
}
}
// VidSSetupGamma(BlitFXPrim);
// Initialize the buffer surfaces
if (dx9SurfaceInit()) {
dx9Exit();
return 1;
}
if (dx9EffectInit()) {
dx9Exit();
return 1;
}
for (int y = 0; y < RENDER_STRIPS; y++) {
if (FAILED(pD3DDevice->CreateVertexBuffer(4 * sizeof(D3DLVERTEX2), D3DUSAGE_WRITEONLY, D3DFVF_LVERTEX2, D3DPOOL_DEFAULT, &pVB[y], NULL))) {
dx9Exit();
return 1;
}
}
if (FAILED(pD3DDevice->CreateVertexBuffer(4 * sizeof(D3DLVERTEX2), D3DUSAGE_WRITEONLY, D3DFVF_LVERTEX2, D3DPOOL_DEFAULT, &pIntermediateVB, NULL))) {
dx9Exit();
return 1;
}
nImageWidth = 0; nImageHeight = 0;
dPrevCubicB = dPrevCubicC = -999;
pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
// Clear the swapchain's buffers
if (nVidFullscreen) {
for (int i = 0; i < 3; i++) {
pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
} else {
RECT rect;
GetClientScreenRect(hVidWnd, &rect);
rect.top += nMenuHeight; rect.bottom += nMenuHeight;
pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pD3DDevice->Present(&rect, &rect, NULL, NULL);
}
// Create osd font
dx9CreateFont();
#ifdef PRINT_DEBUG_INFO
{
dprintf(_T(" * Initialisation complete: %.2lfMB texture memory free (total).\n"), (double)pD3DDevice->GetAvailableTextureMem() / (1024 * 1024));
dprintf(_T(" Displaying and rendering in %i-bit mode, emulation running in %i-bit mode.\n"), nVidScrnDepth, nVidImageDepth);