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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
|
/****************************************************************************
*
* Mesa 3-D graphics library
* Direct3D Driver Interface
*
* ========================================================================
*
* Copyright (C) 1991-2004 SciTech Software, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* SCITECH SOFTWARE INC BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* ======================================================================
*
* Language: ANSI C
* Environment: Windows 9x/2000/XP/XBox (Win32)
*
* Description: GLDirect Direct3D 8.x WGL (WindowsGL)
*
****************************************************************************/
#include "dglcontext.h"
#include "gld_driver.h"
//#include "gld_dxerr8.h"
#include "gld_dx7.h"
#include "tnl/tnl.h"
#include "tnl/t_context.h"
// Copied from dglcontect.c
#define GLDERR_NONE 0
#define GLDERR_MEM 1
#define GLDERR_DDRAW 2
#define GLDERR_D3D 3
#define GLDERR_BPP 4
#define GLDERR_DDS 5
// This external var keeps track of any error
extern int nContextError;
// Uncomment this for persistant resources
//#define _GLD_PERSISTANT
#define DDLOG_CRITICAL_OR_WARN DDLOG_CRITICAL
extern void _gld_mesa_warning(GLcontext *, char *);
extern void _gld_mesa_fatal(GLcontext *, char *);
//---------------------------------------------------------------------------
static char szColorDepthWarning[] =
"GLDirect does not support the current desktop\n\
color depth.\n\n\
You may need to change the display resolution to\n\
16 bits per pixel or higher color depth using\n\
the Windows Display Settings control panel\n\
before running this OpenGL application.\n";
// The only depth-stencil formats currently supported by Direct3D
// Surface Format Depth Stencil Total Bits
// D3DFMT_D32 32 - 32
// D3DFMT_D15S1 15 1 16
// D3DFMT_D24S8 24 8 32
// D3DFMT_D16 16 - 16
// D3DFMT_D24X8 24 - 32
// D3DFMT_D24X4S4 24 4 32
// This pixel format will be used as a template when compiling the list
// of pixel formats supported by the hardware. Many fields will be
// filled in at runtime.
// PFD flag defaults are upgraded to match ChoosePixelFormat() -- DaveM
static DGL_pixelFormat pfTemplateHW =
{
{
sizeof(PIXELFORMATDESCRIPTOR), // Size of the data structure
1, // Structure version - should be 1
// Flags:
PFD_DRAW_TO_WINDOW | // The buffer can draw to a window or device surface.
PFD_DRAW_TO_BITMAP | // The buffer can draw to a bitmap. (DaveM)
PFD_SUPPORT_GDI | // The buffer supports GDI drawing. (DaveM)
PFD_SUPPORT_OPENGL | // The buffer supports OpenGL drawing.
PFD_DOUBLEBUFFER | // The buffer is double-buffered.
0, // Placeholder for easy commenting of above flags
PFD_TYPE_RGBA, // Pixel type RGBA.
16, // Total colour bitplanes (excluding alpha bitplanes)
5, 0, // Red bits, shift
5, 0, // Green bits, shift
5, 0, // Blue bits, shift
0, 0, // Alpha bits, shift (destination alpha)
0, // Accumulator bits (total)
0, 0, 0, 0, // Accumulator bits: Red, Green, Blue, Alpha
0, // Depth bits
0, // Stencil bits
0, // Number of auxiliary buffers
0, // Layer type
0, // Specifies the number of overlay and underlay planes.
0, // Layer mask
0, // Specifies the transparent color or index of an underlay plane.
0 // Damage mask
},
D3DX_SF_UNKNOWN, // No depth/stencil buffer
};
//---------------------------------------------------------------------------
// Vertex Shaders
//---------------------------------------------------------------------------
/*
// Vertex Shader Declaration
static DWORD dwTwoSidedLightingDecl[] =
{
D3DVSD_STREAM(0),
D3DVSD_REG(0, D3DVSDT_FLOAT3), // XYZ position
D3DVSD_REG(1, D3DVSDT_FLOAT3), // XYZ normal
D3DVSD_REG(2, D3DVSDT_D3DCOLOR), // Diffuse color
D3DVSD_REG(3, D3DVSDT_D3DCOLOR), // Specular color
D3DVSD_REG(4, D3DVSDT_FLOAT2), // 2D texture unit 0
D3DVSD_REG(5, D3DVSDT_FLOAT2), // 2D texture unit 1
D3DVSD_END()
};
// Vertex Shader for two-sided lighting
static char *szTwoSidedLightingVS =
// This is a test shader!
"vs.1.0\n"
"m4x4 oPos,v0,c0\n"
"mov oD0,v2\n"
"mov oD1,v3\n"
"mov oT0,v4\n"
"mov oT1,v5\n"
;
*/
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
typedef struct {
// HINSTANCE hD3D8DLL; // Handle to d3d8.dll
// FNDIRECT3DCREATE7 fnDirect3DCreate7; // Direct3DCreate8 function prototype
// BOOL bDirect3D; // Persistant Direct3D7 exists
// BOOL bDirect3DDevice; // Persistant Direct3DDevice7 exists
// IDirect3D7 *pD3D; // Persistant Direct3D7
// IDirect3DDevice7 *pDev; // Persistant Direct3DDevice7
BOOL bD3DXStarted;
} GLD_dx7_globals;
// These are "global" to all DX7 contexts. KeithH
static GLD_dx7_globals dx7Globals;
// Added for correct clipping of multiple open windows. (DaveM)
LPDIRECTDRAWSURFACE7 lpDDSPrimary = NULL;
LPDIRECTDRAWCLIPPER lpDDClipper = NULL;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL gldGetDXErrorString_DX(
HRESULT hr,
char *buf,
int nBufSize)
{
//
// Return a string describing the input HRESULT error code
//
D3DXGetErrorString(hr, nBufSize, buf);
return TRUE;
}
//---------------------------------------------------------------------------
//
// DX7 does not support multisample
/*
static D3DMULTISAMPLE_TYPE _gldGetDeviceMultiSampleType(
IDirect3D8 *pD3D8,
D3DFORMAT SurfaceFormat,
D3DDEVTYPE d3dDevType,
BOOL Windowed)
{
int i;
HRESULT hr;
if (glb.dwMultisample == GLDS_MULTISAMPLE_NONE)
return D3DMULTISAMPLE_NONE;
if (glb.dwMultisample == GLDS_MULTISAMPLE_FASTEST) {
// Find fastest multisample
for (i=2; i<17; i++) {
hr = IDirect3D8_CheckDeviceMultiSampleType(
pD3D8,
glb.dwAdapter,
d3dDevType,
SurfaceFormat,
Windowed,
(D3DMULTISAMPLE_TYPE)i);
if (SUCCEEDED(hr)) {
return (D3DMULTISAMPLE_TYPE)i;
}
}
} else {
// Find nicest multisample
for (i=16; i>1; i--) {
hr = IDirect3D8_CheckDeviceMultiSampleType(
pD3D8,
glb.dwAdapter,
d3dDevType,
SurfaceFormat,
Windowed,
(D3DMULTISAMPLE_TYPE)i);
if (SUCCEEDED(hr)) {
return (D3DMULTISAMPLE_TYPE)i;
}
}
}
// Nothing found - return default
return D3DMULTISAMPLE_NONE;
}
*/
//---------------------------------------------------------------------------
void _gldDestroyPrimitiveBuffer(
GLD_pb_dx7 *gldVB)
{
SAFE_RELEASE(gldVB->pVB);
// Sanity check...
gldVB->nLines = gldVB->nPoints = gldVB->nTriangles = 0;
}
//---------------------------------------------------------------------------
HRESULT _gldCreatePrimitiveBuffer(
GLcontext *ctx,
GLD_driver_dx7 *lpCtx,
GLD_pb_dx7 *gldVB)
{
HRESULT hResult;
char *szCreateVertexBufferFailed = "CreateVertexBuffer failed";
DWORD dwMaxVertices; // Max number of vertices in vertex buffer
DWORD dwVBSize; // Total size of vertex buffer
D3DVERTEXBUFFERDESC vbdesc;
// If CVA (Compiled Vertex Array) is used by an OpenGL app, then we
// will need enough vertices to cater for Mesa::Const.MaxArrayLockSize.
// We'll use IMM_SIZE if it's larger (which it should not be).
dwMaxVertices = MAX_ARRAY_LOCK_SIZE;
// Max vertex buffer size limited in DX7. (DaveM)
if (dwMaxVertices*9 > D3DMAXNUMVERTICES)
dwMaxVertices = D3DMAXNUMVERTICES/9;
// Now calculate how many vertices to allow for in total
// 1 per point, 2 per line, 6 per quad = 9
dwVBSize = dwMaxVertices * 9 * gldVB->dwStride;
vbdesc.dwSize = sizeof(vbdesc);
vbdesc.dwCaps = gldVB->dwCreateFlags;
vbdesc.dwFVF = gldVB->dwFVF;
vbdesc.dwNumVertices = dwMaxVertices * 9;
/* hResult = IDirect3DDevice8_CreateVertexBuffer(
lpCtx->pDev,
dwVBSize,
RAgldVB->dwUsage,
gldVB->dwFVF,
gldVB->dwPool,
&gldVB->pVB);*/
hResult = IDirect3D7_CreateVertexBuffer(
lpCtx->pD3D,
&vbdesc,
&gldVB->pVB,
0);
if (FAILED(hResult)) {
ddlogMessage(DDLOG_CRITICAL_OR_WARN, szCreateVertexBufferFailed);
return hResult;
}
gldVB->nLines = gldVB->nPoints = gldVB->nTriangles = 0;
gldVB->pPoints = gldVB->pLines = gldVB->pTriangles = NULL;
gldVB->iFirstLine = dwMaxVertices; // Index of first line in VB
gldVB->iFirstTriangle = dwMaxVertices*3; // Index of first triangle in VB
return S_OK;
}
//---------------------------------------------------------------------------
// Function: _gldCreateVertexShaders
// Create DX8 Vertex Shaders.
//---------------------------------------------------------------------------
/*
void _gldCreateVertexShaders(
GLD_driver_dx8 *gld)
{
DWORD dwFlags;
LPD3DXBUFFER pVSOpcodeBuffer; // Vertex Shader opcode buffer
HRESULT hr;
#ifdef _DEBUG
dwFlags = D3DXASM_DEBUG;
#else
dwFlags = 0; // D3DXASM_SKIPVALIDATION;
#endif
ddlogMessage(DDLOG_INFO, "Creating shaders...\n");
// Init the shader handle
gld->VStwosidelight.hShader = 0;
if (gld->d3dCaps8.MaxStreams == 0) {
// Lame DX8 driver doesn't support streams
// Not fatal, as defaults will be used
ddlogMessage(DDLOG_WARN, "Driver doesn't support Vertex Shaders (MaxStreams==0)\n");
return;
}
// ** THIS DISABLES VERTEX SHADER SUPPORT **
// return;
// ** THIS DISABLES VERTEX SHADER SUPPORT **
//
// Two-sided lighting
//
#if 0
//
// DEBUGGING: Load shader from a text file
//
{
LPD3DXBUFFER pVSErrorBuffer; // Vertex Shader error buffer
hr = D3DXAssembleShaderFromFile(
"twoside.vsh",
dwFlags,
NULL, // No constants
&pVSOpcodeBuffer,
&pVSErrorBuffer);
if (pVSErrorBuffer && pVSErrorBuffer->lpVtbl->GetBufferPointer(pVSErrorBuffer))
ddlogMessage(DDLOG_INFO, pVSErrorBuffer->lpVtbl->GetBufferPointer(pVSErrorBuffer));
SAFE_RELEASE(pVSErrorBuffer);
}
#else
{
LPD3DXBUFFER pVSErrorBuffer; // Vertex Shader error buffer
// Assemble ascii shader text into shader opcodes
hr = D3DXAssembleShader(
szTwoSidedLightingVS,
strlen(szTwoSidedLightingVS),
dwFlags,
NULL, // No constants
&pVSOpcodeBuffer,
&pVSErrorBuffer);
if (pVSErrorBuffer && pVSErrorBuffer->lpVtbl->GetBufferPointer(pVSErrorBuffer))
ddlogMessage(DDLOG_INFO, pVSErrorBuffer->lpVtbl->GetBufferPointer(pVSErrorBuffer));
SAFE_RELEASE(pVSErrorBuffer);
}
#endif
if (FAILED(hr)) {
ddlogError(DDLOG_WARN, "AssembleShader failed", hr);
SAFE_RELEASE(pVSOpcodeBuffer);
return;
}
// This is for debugging. Remove to enable vertex shaders in HW
#define _GLD_FORCE_SW_VS 0
if (_GLD_FORCE_SW_VS) {
// _GLD_FORCE_SW_VS should be disabled for Final Release
ddlogMessage(DDLOG_SYSTEM, "[Forcing shaders in SW]\n");
}
// Try and create shader in hardware.
// NOTE: The D3D Ref device appears to succeed when trying to
// create the device in hardware, but later complains
// when trying to set it with SetVertexShader(). Go figure.
if (_GLD_FORCE_SW_VS || glb.dwDriver == GLDS_DRIVER_REF) {
// Don't try and create a hardware shader with the Ref device
hr = E_FAIL; // COM error/fail result
} else {
gld->VStwosidelight.bHardware = TRUE;
hr = IDirect3DDevice8_CreateVertexShader(
gld->pDev,
dwTwoSidedLightingDecl,
pVSOpcodeBuffer->lpVtbl->GetBufferPointer(pVSOpcodeBuffer),
&gld->VStwosidelight.hShader,
0);
}
if (FAILED(hr)) {
ddlogMessage(DDLOG_INFO, "... HW failed, trying SW...\n");
// Failed. Try and create shader for software processing
hr = IDirect3DDevice8_CreateVertexShader(
gld->pDev,
dwTwoSidedLightingDecl,
pVSOpcodeBuffer->lpVtbl->GetBufferPointer(pVSOpcodeBuffer),
&gld->VStwosidelight.hShader,
D3DUSAGE_SOFTWAREPROCESSING);
if (FAILED(hr)) {
gld->VStwosidelight.hShader = 0; // Sanity check
ddlogError(DDLOG_WARN, "CreateVertexShader failed", hr);
return;
}
// Succeeded, but for software processing
gld->VStwosidelight.bHardware = FALSE;
}
SAFE_RELEASE(pVSOpcodeBuffer);
ddlogMessage(DDLOG_INFO, "... OK\n");
}
//---------------------------------------------------------------------------
void _gldDestroyVertexShaders(
GLD_driver_dx8 *gld)
{
if (gld->VStwosidelight.hShader) {
IDirect3DDevice8_DeleteVertexShader(gld->pDev, gld->VStwosidelight.hShader);
gld->VStwosidelight.hShader = 0;
}
}
*/
//---------------------------------------------------------------------------
BOOL gldCreateDrawable_DX(
DGL_ctx *ctx,
// BOOL bDefaultDriver,
BOOL bDirectDrawPersistant,
BOOL bPersistantBuffers)
{
//
// bDirectDrawPersistant: applies to IDirect3D8
// bPersistantBuffers: applies to IDirect3DDevice8
//
// D3DDEVTYPE d3dDevType;
// D3DPRESENT_PARAMETERS d3dpp;
// D3DDISPLAYMODE d3ddm;
// DWORD dwBehaviourFlags;
// D3DADAPTER_IDENTIFIER8 d3dIdent;
HRESULT hr;
GLD_driver_dx7 *lpCtx = NULL;
D3DX_VIDMODEDESC d3ddm;
// Parameters for D3DXCreateContextEx
// These will be different for fullscreen and windowed
DWORD dwDeviceIndex;
DWORD dwFlags;
HWND hwnd;
HWND hwndFocus;
DWORD numColorBits;
DWORD numAlphaBits;
DWORD numDepthBits;
DWORD numStencilBits;
DWORD numBackBuffers;
DWORD dwWidth;
DWORD dwHeight;
DWORD refreshRate;
// Error if context is NULL.
if (ctx == NULL)
return FALSE;
if (ctx->glPriv) {
lpCtx = ctx->glPriv;
// Release any existing interfaces (in reverse order)
SAFE_RELEASE(lpCtx->pDev);
SAFE_RELEASE(lpCtx->pD3D);
lpCtx->pD3DXContext->lpVtbl->Release(lpCtx->pD3DXContext);
lpCtx->pD3DXContext = NULL;
} else {
lpCtx = (GLD_driver_dx7*)malloc(sizeof(GLD_driver_dx7));
ZeroMemory(lpCtx, sizeof(lpCtx));
}
// d3dDevType = (glb.dwDriver == GLDS_DRIVER_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;
// Use REF device if requested. Otherwise D3DX_DEFAULT will choose highest level
// of HW acceleration.
dwDeviceIndex = (glb.dwDriver == GLDS_DRIVER_REF) ? D3DX_HWLEVEL_REFERENCE : D3DX_DEFAULT;
// TODO: Check this
// if (bDefaultDriver)
// d3dDevType = D3DDEVTYPE_REF;
#ifdef _GLD_PERSISTANT
// Use persistant interface if needed
if (bDirectDrawPersistant && dx7Globals.bDirect3D) {
lpCtx->pD3D = dx7Globals.pD3D;
IDirect3D7_AddRef(lpCtx->pD3D);
goto SkipDirectDrawCreate;
}
#endif
/*
// Create Direct3D7 object
lpCtx->pD3D = dx7Globals.fnDirect3DCreate8(D3D_SDK_VERSION_DX8_SUPPORT_WIN95);
if (lpCtx->pD3D == NULL) {
MessageBox(NULL, "Unable to initialize Direct3D8", "GLDirect", MB_OK);
ddlogMessage(DDLOG_CRITICAL_OR_WARN, "Unable to create Direct3D8 interface");
nContextError = GLDERR_D3D;
goto return_with_error;
}
*/
#ifdef _GLD_PERSISTANT
// Cache Direct3D interface for subsequent GLRCs
if (bDirectDrawPersistant && !dx8Globals.bDirect3D) {
dx7Globals.pD3D = lpCtx->pD3D;
IDirect3D7_AddRef(dx7Globals.pD3D);
dx7Globals.bDirect3D = TRUE;
}
SkipDirectDrawCreate:
#endif
/*
// Get the display mode so we can make a compatible backbuffer
hResult = IDirect3D8_GetAdapterDisplayMode(lpCtx->pD3D, glb.dwAdapter, &d3ddm);
if (FAILED(hResult)) {
nContextError = GLDERR_D3D;
goto return_with_error;
}
*/
#if 0
// Get device caps
hResult = IDirect3D8_GetDeviceCaps(lpCtx->pD3D, glb.dwAdapter, d3dDevType, &lpCtx->d3dCaps8);
if (FAILED(hResult)) {
ddlogError(DDLOG_CRITICAL_OR_WARN, "IDirect3D8_GetDeviceCaps failed", hResult);
nContextError = GLDERR_D3D;
goto return_with_error;
}
// Check for hardware transform & lighting
lpCtx->bHasHWTnL = lpCtx->d3dCaps8.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ? TRUE : FALSE;
// If this flag is present then we can't default to Mesa
// SW rendering between BeginScene() and EndScene().
if (lpCtx->d3dCaps8.Caps2 & D3DCAPS2_NO2DDURING3DSCENE) {
ddlogMessage(DDLOG_WARN,
"Warning : No 2D allowed during 3D scene.\n");
}
#endif
//
// Create the Direct3D context
//
#ifdef _GLD_PERSISTANT
// Re-use original IDirect3DDevice if persistant buffers exist.
// Note that we test for persistant IDirect3D8 as well
// bDirectDrawPersistant == persistant IDirect3D8 (DirectDraw8 does not exist)
if (bDirectDrawPersistant && bPersistantBuffers && dx7Globals.pD3D && dx7Globals.pDev) {
lpCtx->pDev = dx7Globals.pDev;
IDirect3DDevice7_AddRef(dx7Globals.pDev);
goto skip_direct3ddevice_create;
}
#endif
/*
// Clear the presentation parameters (sets all members to zero)
ZeroMemory(&d3dpp, sizeof(d3dpp));
// Recommended by MS; needed for MultiSample.
// Be careful if altering this for FullScreenBlit
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = _gldGetDeviceMultiSampleType(lpCtx->pD3D, d3ddm.Format, d3dDevType, !ctx->bFullscreen);
d3dpp.AutoDepthStencilFormat = ctx->lpPF->dwDriverData;
d3dpp.EnableAutoDepthStencil = (d3dpp.AutoDepthStencilFormat == D3DFMT_UNKNOWN) ? FALSE : TRUE;
if (ctx->bFullscreen) {
ddlogWarnOption(FALSE); // Don't popup any messages in fullscreen
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = d3ddm.Width;
d3dpp.BackBufferHeight = d3ddm.Height;
d3dpp.hDeviceWindow = ctx->hWnd;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
// Support for vertical retrace synchronisation.
// Set default presentation interval in case caps bits are missing
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
if (glb.bWaitForRetrace) {
if (lpCtx->d3dCaps8.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
} else {
if (lpCtx->d3dCaps8.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
}
} else {
ddlogWarnOption(glb.bMessageBoxWarnings); // OK to popup messages
d3dpp.Windowed = TRUE;
d3dpp.BackBufferWidth = ctx->dwWidth;
d3dpp.BackBufferHeight = ctx->dwHeight;
d3dpp.hDeviceWindow = ctx->hWnd;
d3dpp.FullScreen_RefreshRateInHz = 0;
// FullScreen_PresentationInterval must be default for Windowed mode
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
}
// Decide if we can use hardware TnL
dwBehaviourFlags = (lpCtx->bHasHWTnL) ?
D3DCREATE_MIXED_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// Add flag to tell D3D to be thread-safe
if (glb.bMultiThreaded)
dwBehaviourFlags |= D3DCREATE_MULTITHREADED;
hResult = IDirect3D8_CreateDevice(lpCtx->pD3D,
glb.dwAdapter,
d3dDevType,
ctx->hWnd,
dwBehaviourFlags,
&d3dpp,
&lpCtx->pDev);
if (FAILED(hResult)) {
ddlogError(DDLOG_CRITICAL_OR_WARN, "IDirect3D8_CreateDevice failed", hResult);
nContextError = GLDERR_D3D;
goto return_with_error;
}
*/
// Create D3DX context
if (ctx->bFullscreen) {
//
// FULLSCREEN
//
// Get display mode
D3DXGetCurrentVideoMode(D3DX_DEFAULT, &d3ddm);
// Fullscreen Parameters
dwFlags = D3DX_CONTEXT_FULLSCREEN;
hwnd = ctx->hWnd;
hwndFocus = ctx->hWnd;
numColorBits = ctx->lpPF->pfd.cColorBits;
numAlphaBits = ctx->lpPF->pfd.cAlphaBits;
numDepthBits = ctx->lpPF->pfd.cDepthBits + ctx->lpPF->pfd.cStencilBits;
numStencilBits = ctx->lpPF->pfd.cStencilBits;
numBackBuffers = D3DX_DEFAULT; // Default is 1 backbuffer
dwWidth = d3ddm.width;
dwHeight = d3ddm.height;
refreshRate = d3ddm.refreshRate; // D3DX_DEFAULT;
} else {
//
// WINDOWED
//
// Windowed Parameters
dwFlags = 0; // No flags means "windowed"
hwnd = ctx->hWnd;
hwndFocus = (HWND)D3DX_DEFAULT;
numColorBits = D3DX_DEFAULT; // Use Desktop depth
numAlphaBits = ctx->lpPF->pfd.cAlphaBits;
numDepthBits = ctx->lpPF->pfd.cDepthBits + ctx->lpPF->pfd.cStencilBits;
numStencilBits = ctx->lpPF->pfd.cStencilBits;
numBackBuffers = D3DX_DEFAULT; // Default is 1 backbuffer
dwWidth = ctx->dwWidth;
dwHeight = ctx->dwHeight;
refreshRate = D3DX_DEFAULT;
}
hr = D3DXCreateContextEx(dwDeviceIndex, dwFlags, hwnd, hwndFocus,
numColorBits, numAlphaBits, numDepthBits, numStencilBits,
numBackBuffers,
dwWidth, dwHeight, refreshRate,
&lpCtx->pD3DXContext);
if (FAILED(hr)) {
ddlogError(DDLOG_CRITICAL_OR_WARN, "D3DXCreateContextEx failed", hr);
nContextError = GLDERR_D3D;
goto return_with_error;
}
// Obtain D3D7 interfaces from ID3DXContext
// lpCtx->pDD = ID3DXContext_GetDD(lpCtx->pD3DXContext);
lpCtx->pDD = lpCtx->pD3DXContext->lpVtbl->GetDD(lpCtx->pD3DXContext);
if (lpCtx->pDD == NULL)
goto return_with_error;
lpCtx->pD3D = lpCtx->pD3DXContext->lpVtbl->GetD3D(lpCtx->pD3DXContext);
if (lpCtx->pD3D == NULL)
goto return_with_error;
lpCtx->pDev = lpCtx->pD3DXContext->lpVtbl->GetD3DDevice(lpCtx->pD3DXContext);
if (lpCtx->pDev == NULL)
goto return_with_error;
// Need to manage clipper manually for multiple windows
// since DX7 D3DX utility lib does not appear to do that. (DaveM)
if (!ctx->bFullscreen) {
// Get primary surface too
lpDDSPrimary = lpCtx->pD3DXContext->lpVtbl->GetPrimary(lpCtx->pD3DXContext);
if (lpDDSPrimary == NULL) {
ddlogPrintf(DDLOG_WARN, "GetPrimary");
goto return_with_error;
}
// Create clipper for correct window updates
if (IDirectDraw7_CreateClipper(lpCtx->pDD, 0, &lpDDClipper, NULL) != DD_OK) {
ddlogPrintf(DDLOG_WARN, "CreateClipper");
goto return_with_error;
}
// Set the window that the clipper belongs to
if (IDirectDrawClipper_SetHWnd(lpDDClipper, 0, hwnd) != DD_OK) {
ddlogPrintf(DDLOG_WARN, "SetHWnd");
goto return_with_error;
}
// Attach the clipper to the primary surface
if (IDirectDrawSurface7_SetClipper(lpDDSPrimary, lpDDClipper) != DD_OK) {
ddlogPrintf(DDLOG_WARN, "SetClipper");
goto return_with_error;
}
}
// Get device caps
IDirect3DDevice7_GetCaps(lpCtx->pDev, &lpCtx->d3dCaps);
// Determine HW TnL
lpCtx->bHasHWTnL = lpCtx->d3dCaps.dwDevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ? TRUE : FALSE;
#ifdef _GLD_PERSISTANT
if (bDirectDrawPersistant && bPersistantBuffers && dx7Globals.pD3D) {
dx7Globals.pDev = lpCtx->pDev;
dx7Globals.bDirect3DDevice = TRUE;
}
#endif
#if 0
// Dump some useful stats
hResult = IDirect3D8_GetAdapterIdentifier(
lpCtx->pD3D,
glb.dwAdapter,
D3DENUM_NO_WHQL_LEVEL, // Avoids 1 to 2 second delay
&d3dIdent);
if (SUCCEEDED(hResult)) {
ddlogPrintf(DDLOG_INFO, "[Driver Description: %s]", &d3dIdent.Description);
ddlogPrintf(DDLOG_INFO, "[Driver file: %s %d.%d.%02d.%d]",
d3dIdent.Driver,
HIWORD(d3dIdent.DriverVersion.HighPart),
LOWORD(d3dIdent.DriverVersion.HighPart),
HIWORD(d3dIdent.DriverVersion.LowPart),
LOWORD(d3dIdent.DriverVersion.LowPart));
ddlogPrintf(DDLOG_INFO, "[VendorId: 0x%X, DeviceId: 0x%X, SubSysId: 0x%X, Revision: 0x%X]",
d3dIdent.VendorId, d3dIdent.DeviceId, d3dIdent.SubSysId, d3dIdent.Revision);
}
#endif
// Init projection matrix for D3D TnL
D3DXMatrixIdentity((D3DXMATRIX*)&lpCtx->matProjection);
lpCtx->matModelView = lpCtx->matProjection;
// gld->bUseMesaProjection = TRUE;
skip_direct3ddevice_create:
// Create buffers to hold primitives
lpCtx->PB2d.dwFVF = GLD_FVF_2D_VERTEX;
// lpCtx->PB2d.dwPool = D3DPOOL_SYSTEMMEM;
lpCtx->PB2d.dwStride = sizeof(GLD_2D_VERTEX);
lpCtx->PB2d.dwCreateFlags = D3DVBCAPS_DONOTCLIP |
D3DVBCAPS_SYSTEMMEMORY |
D3DVBCAPS_WRITEONLY;
hr = _gldCreatePrimitiveBuffer(ctx->glCtx, lpCtx, &lpCtx->PB2d);
if (FAILED(hr))
goto return_with_error;
lpCtx->PB3d.dwFVF = GLD_FVF_3D_VERTEX;
// lpCtx->PB3d.dwPool = D3DPOOL_DEFAULT;
lpCtx->PB3d.dwStride = sizeof(GLD_3D_VERTEX);
lpCtx->PB3d.dwCreateFlags = D3DVBCAPS_WRITEONLY;
hr = _gldCreatePrimitiveBuffer(ctx->glCtx, lpCtx, &lpCtx->PB3d);
if (FAILED(hr))
goto return_with_error;
// Zero the pipeline usage counters
lpCtx->PipelineUsage.qwMesa.QuadPart =
// lpCtx->PipelineUsage.dwD3D2SVS.QuadPart =
lpCtx->PipelineUsage.qwD3DFVF.QuadPart = 0;
// Assign drawable to GL private
ctx->glPriv = lpCtx;
return TRUE;
return_with_error:
// Clean up and bail
_gldDestroyPrimitiveBuffer(&lpCtx->PB3d);
_gldDestroyPrimitiveBuffer(&lpCtx->PB2d);
SAFE_RELEASE(lpCtx->pDev);
SAFE_RELEASE(lpCtx->pD3D);
//SAFE_RELEASE(lpCtx->pD3DXContext);
lpCtx->pD3DXContext->lpVtbl->Release(lpCtx->pD3DXContext);
return FALSE;
}
//---------------------------------------------------------------------------
BOOL gldResizeDrawable_DX(
DGL_ctx *ctx,
BOOL bDefaultDriver,
BOOL bPersistantInterface,
BOOL bPersistantBuffers)
{
GLD_driver_dx7 *gld = NULL;
// D3DDEVTYPE d3dDevType;
// D3DPRESENT_PARAMETERS d3dpp;
// D3DDISPLAYMODE d3ddm;
D3DX_VIDMODEDESC d3ddm;
HRESULT hr;
DWORD dwWidth, dwHeight;
// Error if context is NULL.
if (ctx == NULL)
return FALSE;
gld = ctx->glPriv;
if (gld == NULL)
return FALSE;
if (ctx->bSceneStarted) {
IDirect3DDevice7_EndScene(gld->pDev);
ctx->bSceneStarted = FALSE;
}
/*
d3dDevType = (glb.dwDriver == GLDS_DRIVER_HAL) ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF;
if (!bDefaultDriver)
d3dDevType = D3DDEVTYPE_REF; // Force Direct3D Reference Rasterise (software)
// Get the display mode so we can make a compatible backbuffer
hResult = IDirect3D8_GetAdapterDisplayMode(gld->pD3D, glb.dwAdapter, &d3ddm);
if (FAILED(hResult)) {
nContextError = GLDERR_D3D;
// goto return_with_error;
return FALSE;
}
*/
// Release objects before Reset()
_gldDestroyPrimitiveBuffer(&gld->PB3d);
_gldDestroyPrimitiveBuffer(&gld->PB2d);
/*
// Clear the presentation parameters (sets all members to zero)
ZeroMemory(&d3dpp, sizeof(d3dpp));
// Recommended by MS; needed for MultiSample.
// Be careful if altering this for FullScreenBlit
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = _gldGetDeviceMultiSampleType(gld->pD3D, d3ddm.Format, d3dDevType, !ctx->bFullscreen);
d3dpp.AutoDepthStencilFormat = ctx->lpPF->dwDriverData;
d3dpp.EnableAutoDepthStencil = (d3dpp.AutoDepthStencilFormat == D3DFMT_UNKNOWN) ? FALSE : TRUE;
// TODO: Sync to refresh
if (ctx->bFullscreen) {
ddlogWarnOption(FALSE); // Don't popup any messages in fullscreen
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = d3ddm.Width;
d3dpp.BackBufferHeight = d3ddm.Height;
d3dpp.hDeviceWindow = ctx->hWnd;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
// Get better benchmark results? KeithH
// d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_UNLIMITED;
} else {
ddlogWarnOption(glb.bMessageBoxWarnings); // OK to popup messages
d3dpp.Windowed = TRUE;
d3dpp.BackBufferWidth = ctx->dwWidth;
d3dpp.BackBufferHeight = ctx->dwHeight;
d3dpp.hDeviceWindow = ctx->hWnd;
d3dpp.FullScreen_RefreshRateInHz = 0;
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
}
hResult = IDirect3DDevice8_Reset(gld->pDev, &d3dpp);
if (FAILED(hResult)) {
ddlogError(DDLOG_CRITICAL_OR_WARN, "dglResize: Reset failed", hResult);
return FALSE;
//goto cleanup_and_return_with_error;
}
*/
// Obtain dimensions of 'window'
if (ctx->bFullscreen) {
D3DXGetCurrentVideoMode(D3DX_DEFAULT, &d3ddm);
dwWidth = d3ddm.width;
dwHeight = d3ddm.height;
} else {
dwWidth = ctx->dwWidth;
dwHeight = ctx->dwHeight;
}
// Resize context
hr = gld->pD3DXContext->lpVtbl->Resize(gld->pD3DXContext, dwWidth, dwHeight);
if (FAILED(hr)) {
ddlogError(DDLOG_CRITICAL_OR_WARN, "gldResizeDrawable_DX: Resize failed", hr);
return FALSE;
}
// Clear the resized surface (DaveM)
{
D3DVIEWPORT7 vp1, vp2;
IDirect3DDevice7_GetViewport(gld->pDev, &vp1);
IDirect3DDevice7_GetViewport(gld->pDev, &vp2);
vp2.dwX = 0;
vp2.dwY = 0;
vp2.dwWidth = dwWidth;
vp2.dwHeight = dwHeight;
IDirect3DDevice7_SetViewport(gld->pDev, &vp2);
hr = gld->pD3DXContext->lpVtbl->Clear(gld->pD3DXContext, D3DCLEAR_TARGET);
if (FAILED(hr))
ddlogError(DDLOG_WARN, "gldResizeDrawable_DX: Clear failed", hr);
IDirect3DDevice7_SetViewport(gld->pDev, &vp1);
}
//
// Recreate objects
//
_gldCreatePrimitiveBuffer(ctx->glCtx, gld, &gld->PB2d);
_gldCreatePrimitiveBuffer(ctx->glCtx, gld, &gld->PB3d);
// Signal a complete state update
ctx->glCtx->Driver.UpdateState(ctx->glCtx, _NEW_ALL);
// Begin a new scene
IDirect3DDevice7_BeginScene(gld->pDev);
ctx->bSceneStarted = TRUE;
return TRUE;
}
//---------------------------------------------------------------------------
BOOL gldDestroyDrawable_DX(
DGL_ctx *ctx)
{
GLD_driver_dx7 *lpCtx = NULL;
// Error if context is NULL.
if (!ctx)
return FALSE;
// Error if the drawable does not exist.
if (!ctx->glPriv)
return FALSE;
lpCtx = ctx->glPriv;
#ifdef _DEBUG
// Dump out stats
ddlogPrintf(DDLOG_SYSTEM, "Usage: M:0x%X%X, D:0x%X%X",
lpCtx->PipelineUsage.qwMesa.HighPart,
lpCtx->PipelineUsage.qwMesa.LowPart,
lpCtx->PipelineUsage.qwD3DFVF.HighPart,
lpCtx->PipelineUsage.qwD3DFVF.LowPart);
#endif
// Destroy Primtive Buffers
_gldDestroyPrimitiveBuffer(&lpCtx->PB3d);
_gldDestroyPrimitiveBuffer(&lpCtx->PB2d);
// Release DX interfaces (in reverse order)
SAFE_RELEASE(lpCtx->pDev);
SAFE_RELEASE(lpCtx->pD3D);
//SAFE_RELEASE(lpCtx->pD3DXContext);
lpCtx->pD3DXContext->lpVtbl->Release(lpCtx->pD3DXContext);
// Free the private drawable data
free(ctx->glPriv);
ctx->glPriv = NULL;
return TRUE;
}
//---------------------------------------------------------------------------
BOOL gldCreatePrivateGlobals_DX(void)
{
/*
ZeroMemory(&dx7Globals, sizeof(dx7Globals));
// Load d3d8.dll
dx8Globals.hD3D8DLL = LoadLibrary("D3D8.DLL");
if (dx8Globals.hD3D8DLL == NULL)
return FALSE;
// Now try and obtain Direct3DCreate8
dx8Globals.fnDirect3DCreate8 = (FNDIRECT3DCREATE8)GetProcAddress(dx8Globals.hD3D8DLL, "Direct3DCreate8");
if (dx8Globals.fnDirect3DCreate8 == NULL) {
FreeLibrary(dx8Globals.hD3D8DLL);
return FALSE;
}
*/
// Initialise D3DX
return FAILED(D3DXInitialize()) ? FALSE : TRUE;
}
//---------------------------------------------------------------------------
BOOL gldDestroyPrivateGlobals_DX(void)
{
/*
if (dx7Globals.bDirect3DDevice) {
SAFE_RELEASE(dx7Globals.pDev);
dx7Globals.bDirect3DDevice = FALSE;
}
if (dx7Globals.bDirect3D) {
SAFE_RELEASE(dx7Globals.pD3D);
dx7Globals.bDirect3D = FALSE;
}
FreeLibrary(dx8Globals.hD3D8DLL);
dx8Globals.hD3D8DLL = NULL;
dx8Globals.fnDirect3DCreate8 = NULL;
*/
return FAILED(D3DXUninitialize()) ? FALSE : TRUE;
}
//---------------------------------------------------------------------------
static void _BitsFromDisplayFormat(
D3DX_SURFACEFORMAT fmt,
BYTE *cColorBits,
BYTE *cRedBits,
BYTE *cGreenBits,
BYTE *cBlueBits,
BYTE *cAlphaBits)
{
switch (fmt) {
/* case D3DX_SF_X1R5G5B5:
*cColorBits = 16;
*cRedBits = 5;
*cGreenBits = 5;
*cBlueBits = 5;
*cAlphaBits = 0;
return;*/
case D3DX_SF_R5G5B5:
*cColorBits = 16;
*cRedBits = 5;
*cGreenBits = 5;
*cBlueBits = 5;
*cAlphaBits = 0;
return;
case D3DX_SF_R5G6B5:
*cColorBits = 16;
*cRedBits = 5;
*cGreenBits = 6;
*cBlueBits = 5;
*cAlphaBits = 0;
return;
case D3DX_SF_X8R8G8B8:
*cColorBits = 32;
*cRedBits = 8;
*cGreenBits = 8;
*cBlueBits = 8;
*cAlphaBits = 0;
return;
case D3DX_SF_A8R8G8B8:
*cColorBits = 32;
*cRedBits = 8;
*cGreenBits = 8;
*cBlueBits = 8;
*cAlphaBits = 8;
return;
}
// Should not get here!
*cColorBits = 32;
*cRedBits = 8;
*cGreenBits = 8;
*cBlueBits = 8;
*cAlphaBits = 0;
}
//---------------------------------------------------------------------------
static void _BitsFromDepthStencilFormat(
D3DX_SURFACEFORMAT fmt,
BYTE *cDepthBits,
BYTE *cStencilBits)
{
// NOTE: GL expects either 32 or 16 as depth bits.
switch (fmt) {
case D3DX_SF_Z16S0:
*cDepthBits = 16;
*cStencilBits = 0;
return;
case D3DX_SF_Z32S0:
*cDepthBits = 32;
*cStencilBits = 0;
return;
case D3DX_SF_Z15S1:
*cDepthBits = 15;
*cStencilBits = 1;
return;
case D3DX_SF_Z24S8:
*cDepthBits = 24;
*cStencilBits = 8;
return;
case D3DX_SF_S1Z15:
*cDepthBits = 15;
*cStencilBits = 1;
return;
case D3DX_SF_S8Z24:
*cDepthBits = 24;
*cStencilBits = 8;
return;
}
}
//---------------------------------------------------------------------------
/*
BOOL GLD_CheckDepthStencilMatch(
DWORD dwDeviceIndex,
D3DX_SURFACEFORMAT sfWant)
{
// Emulate function built in to DX9
D3DX_SURFACEFORMAT sfFound;
int i;
int nFormats = D3DXGetMaxSurfaceFormats(dwDeviceIndex, NULL, D3DX_SC_DEPTHBUFFER);
if (nFormats) {
for (i=0; i<nFormats; i++) {
D3DXGetSurfaceFormat(dwDeviceIndex, NULL, D3DX_SC_DEPTHBUFFER, i, &sfFound); }
if (sfFound == sfWant)
return TRUE;
}
return FALSE;
}
*/
//---------------------------------------------------------------------------
D3DX_SURFACEFORMAT _gldFindCompatibleDepthStencilFormat(
DWORD dwDeviceIndex)
{
// Jump through some hoops...
ID3DXContext *pD3DXContext = NULL;
IDirectDrawSurface7 *pZBuffer = NULL;
DDPIXELFORMAT ddpf;
HWND hWnd;
// Get an HWND - use Desktop's
hWnd = GetDesktopWindow();
// Create a fully specified default context.
D3DXCreateContextEx(dwDeviceIndex, 0, hWnd, (HWND)D3DX_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
&pD3DXContext);
// Obtain depth buffer that was created in context
pZBuffer = pD3DXContext->lpVtbl->GetZBuffer(pD3DXContext);
// Get pixel format of depth buffer
ddpf.dwSize = sizeof(ddpf);
pZBuffer->lpVtbl->GetPixelFormat(pZBuffer, &ddpf);
// Done with surface - release it
pZBuffer->lpVtbl->Release(pZBuffer);
// Done with D3DX context
pD3DXContext->lpVtbl->Release(pD3DXContext);
// Convert and return
return D3DXMakeSurfaceFormat(&ddpf);
}
//---------------------------------------------------------------------------
BOOL gldBuildPixelformatList_DX(void)
{
D3DX_DEVICEDESC d3dxdd;
D3DX_VIDMODEDESC d3ddm;
D3DX_SURFACEFORMAT fmt[64]; // 64 should be enough...
DWORD dwDeviceIndex;
DWORD surfClassFlags;
// IDirect3D7 *pD3D = NULL;
HRESULT hr;
int nSupportedFormats = 0; // Total formats
int nDepthOnlyFormats = 0;
int nDepthStencilFormats = 0;
int i;
DGL_pixelFormat *pPF;
BYTE cColorBits, cRedBits, cGreenBits, cBlueBits, cAlphaBits;
// char buf[128];
// char cat[8];
// Direct3D (SW or HW)
// These are arranged so that 'best' pixelformat
// is higher in the list (for ChoosePixelFormat).
/* const D3DFORMAT DepthStencil[4] = {
D3DX_SF_Z16S0, //D3DX_SF_D16,
D3DX_SF_Z15S1, //D3DX_SF_D15S1,
D3DX_SF_Z32S0, //D3DX_SF_D32,
D3DX_SF_Z24S8, //D3DX_SF_D24S8,
//D3DX_SF_D24X8,
//D3DX_SF_D24X4S4,
};*/
// Dump DX version
ddlogMessage(GLDLOG_SYSTEM, "DirectX Version : 7.0\n");
// Release any existing pixelformat list
if (glb.lpPF) {
free(glb.lpPF);
}
glb.nPixelFormatCount = 0;
glb.lpPF = NULL;
//
// Pixelformats for Direct3D (SW or HW) rendering
//
dwDeviceIndex = (glb.dwDriver == GLDS_DRIVER_REF) ? D3DX_HWLEVEL_REFERENCE : D3DX_DEFAULT;
// Dump description
D3DXGetDeviceDescription(dwDeviceIndex, &d3dxdd);
ddlogPrintf(GLDLOG_SYSTEM, "Device: %s", d3dxdd.driverDesc);
// Get display mode
D3DXGetCurrentVideoMode(D3DX_DEFAULT, &d3ddm);
#if 0
// Phooey - this don't work...
/*
// Since D3DXGetMaxSurfaceFormats() can lie to us, we'll need a workaround.
// Explicitly test for matching depth/stencil to display bpp.
if (d3ddm.bpp <= 16) {
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z16S0))
fmt[nSupportedFormats++] = D3DX_SF_Z16S0;
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z15S1))
fmt[nSupportedFormats++] = D3DX_SF_Z15S1;
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_S1Z15))
fmt[nSupportedFormats++] = D3DX_SF_S1Z15;
// Didn't find anything? Try default
if (nSupportedFormats == 0) {
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z32S0))
fmt[nSupportedFormats++] = D3DX_SF_Z32S0;
}
} else {
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z32S0))
fmt[nSupportedFormats++] = D3DX_SF_Z32S0;
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z24S8))
fmt[nSupportedFormats++] = D3DX_SF_Z24S8;
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_S8Z24))
fmt[nSupportedFormats++] = D3DX_SF_S8Z24;
// Didn't find anything? Try default
if (nSupportedFormats == 0) {
if (GLD_CheckDepthStencilMatch(dwDeviceIndex, D3DX_SF_Z16S0))
fmt[nSupportedFormats++] = D3DX_SF_Z16S0;
}
}
*/
// Go the Whole Hog...
fmt[nSupportedFormats++] = _gldFindCompatibleDepthStencilFormat(dwDeviceIndex);
#else
//
// Depth buffer formats WITHOUT stencil
//
surfClassFlags = D3DX_SC_DEPTHBUFFER;
nDepthOnlyFormats = D3DXGetMaxSurfaceFormats(dwDeviceIndex, NULL, surfClassFlags);
//
// Depth buffer formats WITH stencil
//
surfClassFlags = D3DX_SC_DEPTHBUFFER | D3DX_SC_STENCILBUFFER;
nDepthStencilFormats = D3DXGetMaxSurfaceFormats(dwDeviceIndex, NULL, surfClassFlags);
// Work out how many formats we have in total
if ((nDepthOnlyFormats + nDepthStencilFormats) == 0)
return FALSE; // Bail: no compliant pixelformats
// Get depth buffer formats WITHOUT stencil
surfClassFlags = D3DX_SC_DEPTHBUFFER;
for (i=0; i<nDepthOnlyFormats; i++) {
D3DXGetSurfaceFormat(dwDeviceIndex, NULL, surfClassFlags, i, &fmt[nSupportedFormats++]);
}
// NOTE: For some reason we already get stencil formats when only specifying D3DX_SC_DEPTHBUFFER
/*
// Get depth buffer formats WITH stencil
surfClassFlags = D3DX_SC_DEPTHBUFFER | D3DX_SC_STENCILBUFFER;
for (i=0; i<nDepthStencilFormats; i++) {
D3DXGetSurfaceFormat(dwDeviceIndex, NULL, surfClassFlags, i, &fmt[nSupportedFormats++]);
}
*/
#endif
// Total count of pixelformats is:
// (nSupportedFormats+1)*2
glb.lpPF = (DGL_pixelFormat *)calloc((nSupportedFormats)*2, sizeof(DGL_pixelFormat));
glb.nPixelFormatCount = (nSupportedFormats)*2;
if (glb.lpPF == NULL) {
glb.nPixelFormatCount = 0;
return FALSE;
}
// Get a copy of pointer that we can alter
pPF = glb.lpPF;
// Cache colour bits from display format
// _BitsFromDisplayFormat(d3ddm.Format, &cColorBits, &cRedBits, &cGreenBits, &cBlueBits, &cAlphaBits);
// Get display mode
D3DXGetCurrentVideoMode(D3DX_DEFAULT, &d3ddm);
cColorBits = d3ddm.bpp;
cAlphaBits = 0;
switch (d3ddm.bpp) {
case 15:
cRedBits = 5; cGreenBits = 5; cBlueBits = 5;
break;
case 16:
cRedBits = 5; cGreenBits = 6; cBlueBits = 5;
break;
case 24:
case 32:
cRedBits = 8; cGreenBits = 8; cBlueBits = 8;
break;
default:
cRedBits = 5; cGreenBits = 5; cBlueBits = 5;
}
//
// Add single-buffer formats
//
/* // Single-buffer, no depth-stencil buffer
memcpy(pPF, &pfTemplateHW, sizeof(DGL_pixelFormat));
pPF->pfd.dwFlags &= ~PFD_DOUBLEBUFFER; // Remove doublebuffer flag
pPF->pfd.cColorBits = cColorBits;
pPF->pfd.cRedBits = cRedBits;
pPF->pfd.cGreenBits = cGreenBits;
pPF->pfd.cBlueBits = cBlueBits;
pPF->pfd.cAlphaBits = cAlphaBits;
pPF->pfd.cDepthBits = 0;
pPF->pfd.cStencilBits = 0;
pPF->dwDriverData = D3DX_SF_UNKNOWN;
pPF++;*/
for (i=0; i<nSupportedFormats; i++, pPF++) {
memcpy(pPF, &pfTemplateHW, sizeof(DGL_pixelFormat));
pPF->pfd.dwFlags &= ~PFD_DOUBLEBUFFER; // Remove doublebuffer flag
pPF->pfd.cColorBits = cColorBits;
pPF->pfd.cRedBits = cRedBits;
pPF->pfd.cGreenBits = cGreenBits;
pPF->pfd.cBlueBits = cBlueBits;
pPF->pfd.cAlphaBits = cAlphaBits;
_BitsFromDepthStencilFormat(fmt[i], &pPF->pfd.cDepthBits, &pPF->pfd.cStencilBits);
pPF->dwDriverData = fmt[i];
}
//
// Add double-buffer formats
//
/* memcpy(pPF, &pfTemplateHW, sizeof(DGL_pixelFormat));
pPF->pfd.cColorBits = cColorBits;
pPF->pfd.cRedBits = cRedBits;
pPF->pfd.cGreenBits = cGreenBits;
pPF->pfd.cBlueBits = cBlueBits;
pPF->pfd.cAlphaBits = cAlphaBits;
pPF->pfd.cDepthBits = 0;
pPF->pfd.cStencilBits = 0;
pPF->dwDriverData = D3DX_SF_UNKNOWN;
pPF++;*/
for (i=0; i<nSupportedFormats; i++, pPF++) {
memcpy(pPF, &pfTemplateHW, sizeof(DGL_pixelFormat));
pPF->pfd.cColorBits = cColorBits;
pPF->pfd.cRedBits = cRedBits;
pPF->pfd.cGreenBits = cGreenBits;
pPF->pfd.cBlueBits = cBlueBits;
pPF->pfd.cAlphaBits = cAlphaBits;
_BitsFromDepthStencilFormat(fmt[i], &pPF->pfd.cDepthBits, &pPF->pfd.cStencilBits);
pPF->dwDriverData = fmt[i];
}
// Popup warning message if non RGB color mode
{
// This is a hack. KeithH
HDC hdcDesktop = GetDC(NULL);
DWORD dwDisplayBitDepth = GetDeviceCaps(hdcDesktop, BITSPIXEL);
ReleaseDC(0, hdcDesktop);
if (dwDisplayBitDepth <= 8) {
ddlogPrintf(DDLOG_WARN, "Current Color Depth %d bpp is not supported", dwDisplayBitDepth);
MessageBox(NULL, szColorDepthWarning, "GLDirect", MB_OK | MB_ICONWARNING);
}
}
// Mark list as 'current'
glb.bPixelformatsDirty = FALSE;
return TRUE;
}
//---------------------------------------------------------------------------
BOOL gldInitialiseMesa_DX(
DGL_ctx *lpCtx)
{
GLD_driver_dx7 *gld = NULL;
int MaxTextureSize, TextureLevels;
BOOL bSoftwareTnL;
if (lpCtx == NULL)
return FALSE;
gld = lpCtx->glPriv;
if (gld == NULL)
return FALSE;
if (glb.bMultitexture) {
lpCtx->glCtx->Const.MaxTextureUnits = gld->d3dCaps.wMaxSimultaneousTextures;
// Only support MAX_TEXTURE_UNITS texture units.
// ** If this is altered then the FVF formats must be reviewed **.
if (lpCtx->glCtx->Const.MaxTextureUnits > GLD_MAX_TEXTURE_UNITS_DX7)
lpCtx->glCtx->Const.MaxTextureUnits = GLD_MAX_TEXTURE_UNITS_DX7;
} else {
// Multitexture override
lpCtx->glCtx->Const.MaxTextureUnits = 1;
}
// max texture size
// MaxTextureSize = min(gld->d3dCaps8.MaxTextureHeight, gld->d3dCaps8.MaxTextureWidth);
MaxTextureSize = min(gld->d3dCaps.dwMaxTextureHeight, gld->d3dCaps.dwMaxTextureWidth);
if (MaxTextureSize == 0)
MaxTextureSize = 256; // Sanity check
//
// HACK!!
if (MaxTextureSize > 1024)
MaxTextureSize = 1024; // HACK - CLAMP TO 1024
// HACK!!
//
// TODO: Check this again for Mesa 5
// Got to set MAX_TEXTURE_SIZE as max levels.
// Who thought this stupid idea up? ;)
TextureLevels = 0;
// Calculate power-of-two.
while (MaxTextureSize) {
TextureLevels++;
MaxTextureSize >>= 1;
}
lpCtx->glCtx->Const.MaxTextureLevels = (TextureLevels) ? TextureLevels : 8;
// Defaults
IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_LIGHTING, FALSE);
IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_DITHERENABLE, TRUE);
IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD);
// Set texture coord set to be used with each stage
IDirect3DDevice7_SetTextureStageState(gld->pDev, 0, D3DTSS_TEXCOORDINDEX, 0);
IDirect3DDevice7_SetTextureStageState(gld->pDev, 1, D3DTSS_TEXCOORDINDEX, 1);
// Set up Depth buffer
IDirect3DDevice7_SetRenderState(gld->pDev, D3DRENDERSTATE_ZENABLE,
(lpCtx->lpPF->dwDriverData!=D3DX_SF_UNKNOWN) ? D3DZB_TRUE : D3DZB_FALSE);
// Set the view matrix
{
D3DXMATRIX vm;
#if 1
D3DXMatrixIdentity(&vm);
#else
D3DXVECTOR3 Eye(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 At(0.0f, 0.0f, -1.0f);
D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtRH(&vm, &Eye, &At, &Up);
vm._31 = -vm._31;
vm._32 = -vm._32;
vm._33 = -vm._33;
vm._34 = -vm._34;
#endif
IDirect3DDevice7_SetTransform(gld->pDev, D3DTRANSFORMSTATE_VIEW, &vm);
}
// DX7 does not support D3DRS_SOFTWAREVERTEXPROCESSING
/*
if (gld->bHasHWTnL) {
if (glb.dwTnL == GLDS_TNL_DEFAULT)
bSoftwareTnL = FALSE; // HW TnL
else {
bSoftwareTnL = ((glb.dwTnL == GLDS_TNL_MESA) || (glb.dwTnL == GLDS_TNL_D3DSW)) ? TRUE : FALSE;
}
} else {
// No HW TnL, so no choice possible
bSoftwareTnL = TRUE;
}
IDirect3DDevice8_SetRenderState(gld->pDev, D3DRS_SOFTWAREVERTEXPROCESSING, bSoftwareTnL);
*/
// Dump this in a Release build as well, now.
//#ifdef _DEBUG
ddlogPrintf(DDLOG_INFO, "HW TnL: %s",
// gld->bHasHWTnL ? (bSoftwareTnL ? "Disabled" : "Enabled") : "Unavailable");
gld->bHasHWTnL ? "Enabled" : "Unavailable");
//#endif
// Set up interfaces to Mesa
gldEnableExtensions_DX7(lpCtx->glCtx);
gldInstallPipeline_DX7(lpCtx->glCtx);
gldSetupDriverPointers_DX7(lpCtx->glCtx);
// Signal a complete state update
lpCtx->glCtx->Driver.UpdateState(lpCtx->glCtx, _NEW_ALL);
// Start a scene
IDirect3DDevice7_BeginScene(gld->pDev);
lpCtx->bSceneStarted = TRUE;
return TRUE;
}
//---------------------------------------------------------------------------
BOOL gldSwapBuffers_DX(
DGL_ctx *ctx,
HDC hDC,
HWND hWnd)
{
HRESULT hr;
GLD_driver_dx7 *gld = NULL;
DWORD dwFlags;
if (ctx == NULL)
return FALSE;
gld = ctx->glPriv;
if (gld == NULL)
return FALSE;
// End the scene if one is started
if (ctx->bSceneStarted) {
IDirect3DDevice7_EndScene(gld->pDev);
ctx->bSceneStarted = FALSE;
}
// Needed by D3DX for MDI multi-window apps (DaveM)
if (lpDDClipper)
IDirectDrawClipper_SetHWnd(lpDDClipper, 0, hWnd);
// Swap the buffers. hWnd may override the hWnd used for CreateDevice()
// hr = IDirect3DDevice8_Present(gld->pDev, NULL, NULL, hWnd, NULL);
// Set refresh sync flag
dwFlags = glb.bWaitForRetrace ? 0 : D3DX_UPDATE_NOVSYNC;
// Render and show frame
hr = gld->pD3DXContext->lpVtbl->UpdateFrame(gld->pD3DXContext, dwFlags);
if (FAILED(hr))
ddlogError(DDLOG_WARN, "gldSwapBuffers_DX: UpdateFrame", hr);
if (hr == DDERR_SURFACELOST) {
hr = gld->pD3DXContext->lpVtbl->RestoreSurfaces(gld->pD3DXContext);
if (FAILED(hr))
ddlogError(DDLOG_WARN, "gldSwapBuffers_DX: RestoreSurfaces", hr);
}
exit_swap:
// Begin a new scene
IDirect3DDevice7_BeginScene(gld->pDev);
ctx->bSceneStarted = TRUE;
return (FAILED(hr)) ? FALSE : TRUE;
}
//---------------------------------------------------------------------------
BOOL gldGetDisplayMode_DX(
DGL_ctx *ctx,
GLD_displayMode *glddm)
{
// D3DDISPLAYMODE d3ddm;
D3DX_VIDMODEDESC d3ddm;
HRESULT hr;
GLD_driver_dx7 *lpCtx = NULL;
BYTE cColorBits, cRedBits, cGreenBits, cBlueBits, cAlphaBits;
if ((glddm == NULL) || (ctx == NULL))
return FALSE;
lpCtx = ctx->glPriv;
if (lpCtx == NULL)
return FALSE;
if (lpCtx->pD3D == NULL)
return FALSE;
// hr = IDirect3D8_GetAdapterDisplayMode(lpCtx->pD3D, glb.dwAdapter, &d3ddm);
hr = D3DXGetCurrentVideoMode(D3DX_DEFAULT, &d3ddm);
if (FAILED(hr))
return FALSE;
// Get info from the display format
// _BitsFromDisplayFormat(d3ddm.Format,
// &cColorBits, &cRedBits, &cGreenBits, &cBlueBits, &cAlphaBits);
glddm->Width = d3ddm.width;
glddm->Height = d3ddm.height;
glddm->BPP = d3ddm.bpp;
glddm->Refresh = d3ddm.refreshRate;
return TRUE;
}
//---------------------------------------------------------------------------
|