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
#include <d3d9.h>
#include <ddraw.h>
#include <d3dnthal.h>
#include <d3dhal.h>
#include <ddrawi.h>
#include <ddrawgdi.h>
#include <dll/directx/d3d8thk.h>
#include <debug.h>
#include <limits.h>
#include "d3d9_helpers.h"
#include "d3d9_caps.h"
#include "adapter.h"
#include "d3d9_callbacks.h"

static INT g_NumDevices = 0;

void CreateDisplayModeList(LPCSTR lpszDeviceName, D3DDISPLAYMODE* pDisplayModes, DWORD* pNumDisplayModes, D3DFORMAT Default16BitFormat, D3D9_Unknown6BC* pUnknown6BC)
{
    DEVMODEA DevMode;
    DWORD ModeIndex = 0;
    DWORD ValidModes = 0;

    while (TRUE == EnumDisplaySettingsA(lpszDeviceName, ModeIndex, &DevMode))
    {
        D3DFORMAT DefaultFormat;

        if (DevMode.dmBitsPerPel != 15 &&
            DevMode.dmBitsPerPel != 16 &&
            DevMode.dmBitsPerPel != 32)
        {
            ++ModeIndex;
            continue;
        }

        ++ValidModes;

        if (DevMode.dmBitsPerPel == 15 || DevMode.dmBitsPerPel == 16)
        {
            if (NULL == pUnknown6BC)
            {
                ++ModeIndex;
                continue;
            }

            DefaultFormat = Default16BitFormat;
        }
        else
        {
            DefaultFormat = D3DFMT_X8R8G8B8;
        }

        if (NULL != pDisplayModes)
        {
            if (ValidModes == *pNumDisplayModes)
                break;

            pDisplayModes->Width = DevMode.dmPelsWidth;
            pDisplayModes->Height = DevMode.dmPelsHeight;
            pDisplayModes->RefreshRate = DevMode.dmDisplayFrequency;
            pDisplayModes->Format = DefaultFormat;
            ++pDisplayModes;
        }

        ++ModeIndex;
    }

    *pNumDisplayModes = ValidModes;
}

static void CreateInternalDeviceData(HDC hDC, LPCSTR lpszDeviceName, D3D9_Unknown6BC** ppUnknown, D3DDEVTYPE DeviceType, HMODULE* hD3DRefDll)
{
    D3D9_Unknown6BC* pUnknown6BC;
    DWORD ValueSize;

    if (ppUnknown) *ppUnknown = NULL;
    if (hD3DRefDll) *hD3DRefDll = NULL;

    if (DeviceType != D3DDEVTYPE_HAL)
    {
        /* TODO: Implement D3DDEVTYPE_REF and D3DDEVTYPE_SW */
        UNIMPLEMENTED;
        return;
    }

    pUnknown6BC = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3D9_Unknown6BC));
    if (NULL == pUnknown6BC)
    {
        DPRINT1("Out of memory");
        return;
    }

    pUnknown6BC->hDirectDrawLocal = OsThunkDdCreateDirectDrawObject(hDC);
    if (0 == pUnknown6BC->hDirectDrawLocal)
    {
        HeapFree(GetProcessHeap(), 0, pUnknown6BC);
        return;
    }


    SafeCopyString(pUnknown6BC->szDeviceName, CCHDEVICENAME, lpszDeviceName);
    //pUnknown6BC->DeviceUniq = DdQueryDisplaySettingsUniqueness();
    pUnknown6BC->DeviceType = DeviceType;


    ValueSize = sizeof(DWORD);
    ReadRegistryValue(REG_DWORD, "ForceDriverFlagsOn", (LPBYTE)&pUnknown6BC->bForceDriverFlagsOn, &ValueSize);

    ValueSize = sizeof(DWORD);
    ReadRegistryValue(REG_DWORD, "ForceDriverFlagsOff", (LPBYTE)&pUnknown6BC->bForceDriverFlagsOff, &ValueSize);

    ++g_NumDevices;

    *ppUnknown = pUnknown6BC;
}

static void ReleaseInternalDeviceData(LPD3D9_DEVICEDATA pDeviceData)
{
    OsThunkDdDeleteDirectDrawObject(pDeviceData->pUnknown6BC->hDirectDrawLocal);

    HeapFree(GetProcessHeap(), 0, pDeviceData->pUnknown6BC);
    pDeviceData->pUnknown6BC = NULL;

    --g_NumDevices;
}

BOOL GetDeviceData(LPD3D9_DEVICEDATA pDeviceData)
{
    BOOL bRet;
    D3DHAL_GLOBALDRIVERDATA GlobalDriverData;
    D3DHAL_D3DEXTENDEDCAPS D3dExtendedCaps;
    LPDDSURFACEDESC puD3dTextureFormats;
    DDPIXELFORMAT* pD3dZStencilFormatList;
    D3DDISPLAYMODE* pD3dDisplayModeList;
    D3DQUERYTYPE* pD3dQueryList;
    DWORD NumTextureFormats = 0;
    DWORD NumStencilFormats = 0;
    DWORD NumExtendedFormats = 0;
    DWORD NumQueries = 0;

    if (NULL == pDeviceData->pUnknown6BC)
    {
        CreateInternalDeviceData(
            pDeviceData->hDC,
            pDeviceData->szDeviceName,
            &pDeviceData->pUnknown6BC,
            pDeviceData->DeviceType,
            &pDeviceData->hD3DRefDll
            );

        if (NULL == pDeviceData->pUnknown6BC)
        {
            DPRINT1("Failed to create DirectDrawObject for Direct3D9");
            return FALSE;
        }
    }
    else
    {
        D3D9_DRIVERCAPS DriverCaps;
        D3D9_CALLBACKS D3D9Callbacks;

        if (FALSE == CanReenableDirectDrawObject(pDeviceData->pUnknown6BC))
        {
            DPRINT1("Failed to re-enable DirectDrawObject");
            return FALSE;
        }

        bRet = GetD3D9DriverInfo(
            pDeviceData->pUnknown6BC,
            &DriverCaps,
            &D3D9Callbacks,
            pDeviceData->szDeviceName,
            pDeviceData->hD3DRefDll,
            &GlobalDriverData,
            &D3dExtendedCaps,
            NULL,
            NULL,
            NULL,
            NULL,
            &NumTextureFormats,
            &NumStencilFormats,
            &NumExtendedFormats,
            &NumQueries
            );

        if (TRUE == bRet)
        {
            pDeviceData->DriverCaps.dwDisplayWidth = DriverCaps.dwDisplayWidth;
            pDeviceData->DriverCaps.dwDisplayHeight = DriverCaps.dwDisplayHeight;
            pDeviceData->DriverCaps.RawDisplayFormat = DriverCaps.RawDisplayFormat;
            pDeviceData->DriverCaps.DisplayFormat = DriverCaps.DisplayFormat;
            pDeviceData->DriverCaps.dwRefreshRate = DriverCaps.dwRefreshRate;
        }

        return bRet;
    }

    /* Cleanup of old stuff */
    if (pDeviceData->DriverCaps.pSupportedFormatOps)
    {
        HeapFree(GetProcessHeap(), 0, pDeviceData->DriverCaps.pSupportedFormatOps);
        pDeviceData->DriverCaps.pSupportedFormatOps = NULL;
    }
    if (pDeviceData->DriverCaps.pSupportedExtendedModes)
    {
        HeapFree(GetProcessHeap(), 0, pDeviceData->DriverCaps.pSupportedExtendedModes);
        pDeviceData->DriverCaps.pSupportedExtendedModes = NULL;
    }
    if (pDeviceData->DriverCaps.pSupportedQueriesList)
    {
        HeapFree(GetProcessHeap(), 0, pDeviceData->DriverCaps.pSupportedQueriesList);
        pDeviceData->DriverCaps.pSupportedQueriesList = NULL;
    }

    if (FALSE == CanReenableDirectDrawObject(pDeviceData->pUnknown6BC))
    {
        DPRINT1("Failed to re-enable DirectDrawObject");
        ReleaseInternalDeviceData(pDeviceData);
        return FALSE;
    }

    bRet = GetD3D9DriverInfo(
        pDeviceData->pUnknown6BC,
        &pDeviceData->DriverCaps,
        &pDeviceData->D3D9Callbacks,
        pDeviceData->szDeviceName,
        pDeviceData->hD3DRefDll,
        &GlobalDriverData,
        &D3dExtendedCaps,
        NULL,
        NULL,
        NULL,
        NULL,
        &NumTextureFormats,
        &NumStencilFormats,
        &NumExtendedFormats,
        &NumQueries
        );

    if (FALSE == bRet)
    {
        DPRINT1("Could not query DirectDrawObject, aborting");
        ReleaseInternalDeviceData(pDeviceData);
        return FALSE;
    }

    puD3dTextureFormats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, max(NumTextureFormats, 1) * sizeof(DDSURFACEDESC));
    pD3dZStencilFormatList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, max(NumStencilFormats, 1) * sizeof(DDPIXELFORMAT));
    pD3dDisplayModeList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, max(NumExtendedFormats, 1) * sizeof(D3DDISPLAYMODE));
    pD3dQueryList = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, max(NumQueries, 1) * sizeof(D3DQUERYTYPE));

    bRet = GetD3D9DriverInfo(
        pDeviceData->pUnknown6BC,
        &pDeviceData->DriverCaps,
        &pDeviceData->D3D9Callbacks,
        pDeviceData->szDeviceName,
        pDeviceData->hD3DRefDll,
        &GlobalDriverData,
        &D3dExtendedCaps,
        puD3dTextureFormats,
        pD3dZStencilFormatList,
        pD3dDisplayModeList,
        pD3dQueryList,
        &NumTextureFormats,
        &NumStencilFormats,
        &NumExtendedFormats,
        &NumQueries
        );

    if (FALSE == bRet)
    {
        DPRINT1("Could not query DirectDrawObject, aborting");
        HeapFree(GetProcessHeap(), 0, puD3dTextureFormats);
        HeapFree(GetProcessHeap(), 0, pD3dZStencilFormatList);
        HeapFree(GetProcessHeap(), 0, pD3dDisplayModeList);
        HeapFree(GetProcessHeap(), 0, pD3dQueryList);
        ReleaseInternalDeviceData(pDeviceData);
        return FALSE;
    }

    pDeviceData->DriverCaps.NumSupportedFormatOps = NumTextureFormats;
    if (NumTextureFormats > 0)
        pDeviceData->DriverCaps.pSupportedFormatOps = puD3dTextureFormats;

    pDeviceData->DriverCaps.NumSupportedExtendedModes = NumExtendedFormats;
    if (NumExtendedFormats > 0)
        pDeviceData->DriverCaps.pSupportedExtendedModes = pD3dDisplayModeList;

    pDeviceData->DriverCaps.NumSupportedQueries = NumQueries;
    if (NumQueries > 0)
        pDeviceData->DriverCaps.pSupportedQueriesList = pD3dQueryList;
    
    HeapFree(GetProcessHeap(), 0, pD3dZStencilFormatList); 

    return TRUE;
}



BOOL CanReenableDirectDrawObject(D3D9_Unknown6BC* pUnknown)
{
    BOOL bDisplayModeWasChanged;

    /* Try the real way first */
    if (TRUE == OsThunkDdReenableDirectDrawObject(pUnknown->hDirectDrawLocal, &bDisplayModeWasChanged))
        return TRUE;

    /* Ref types and software types can always be reenabled after a mode switch */
    if (pUnknown->DeviceType == D3DDEVTYPE_REF || pUnknown->DeviceType == D3DDEVTYPE_SW)
        return TRUE;

    return FALSE;
}



static void PrepareDriverInfoData(DD_GETDRIVERINFODATA* DrvInfo, LPVOID pData, DWORD dwExpectedSize)
{
    memset(DrvInfo, 0, sizeof(DD_GETDRIVERINFODATA));
    DrvInfo->dwSize = sizeof(DD_GETDRIVERINFODATA);
    DrvInfo->guidInfo = GUID_GetDriverInfo2;
    DrvInfo->dwExpectedSize = dwExpectedSize;
    DrvInfo->lpvData = pData;
    DrvInfo->ddRVal = E_FAIL;
}

static void ResetGetDriverInfo2Data(DD_GETDRIVERINFO2DATA* DrvInfo2, DWORD dwType, DWORD dwExpectedSize)
{
    memset(DrvInfo2, 0, dwExpectedSize);
    DrvInfo2->dwMagic = D3DGDI2_MAGIC;
    DrvInfo2->dwType = dwType;
    DrvInfo2->dwExpectedSize = dwExpectedSize;
}

BOOL GetD3D9DriverInfo( D3D9_Unknown6BC* pUnknown6BC,
                        LPD3D9_DRIVERCAPS pDriverCaps,
                        D3D9_CALLBACKS* pD3D9Callbacks,
                        LPCSTR lpszDeviceName,
                        HMODULE hD3dRefDll,
                        D3DHAL_GLOBALDRIVERDATA* pGblDriverData,
                        D3DHAL_D3DEXTENDEDCAPS* pD3dExtendedCaps,
                        LPDDSURFACEDESC puD3dTextureFormats,
                        DDPIXELFORMAT* pD3dZStencilFormatList,
                        D3DDISPLAYMODE* pD3dDisplayModeList,
                        D3DQUERYTYPE* pD3dQueryList,
                        LPDWORD pNumTextureFormats,
                        LPDWORD pNumZStencilFormats,
                        LPDWORD pNumExtendedFormats,
                        LPDWORD pNumQueries)
{
    BOOL bRet;
    DWORD ValueSize;
    DWORD dwDXVersion;

    DD_HALINFO HalInfo;
    DWORD CallBackFlags[3];
    D3DNTHAL_CALLBACKS D3dCallbacks;
    D3DNTHAL_GLOBALDRIVERDATA D3dDriverData;
    DD_D3DBUFCALLBACKS D3dBufferCallbacks;
    DWORD NumHeaps = 0;
    DWORD NumFourCC = 0;

    BOOL bDX8Mode = FALSE;

    DD_GETDRIVERINFODATA DrvInfo;
    DD_GETDDIVERSIONDATA DdiVersion;
    DD_GETFORMATCOUNTDATA FormatCountData;
    DD_GETEXTENDEDMODECOUNTDATA ExModeCountData;
    DD_GETD3DQUERYCOUNTDATA D3dQueryCountData;

    /* Init */
    *pNumTextureFormats = 0;
    *pNumZStencilFormats = 0;
    *pNumExtendedFormats = 0;
    *pNumQueries = 0;
    memset(pD3dExtendedCaps, 0, sizeof(D3DHAL_D3DEXTENDEDCAPS));
    memset(pGblDriverData, 0, sizeof(D3DHAL_GLOBALDRIVERDATA));
    memset(pDriverCaps, 0, sizeof(D3D9_DRIVERCAPS));

    /* Set runtime version */
    ValueSize = sizeof(dwDXVersion);
    if (FALSE == ReadRegistryValue(REG_DWORD, "DD_RUNTIME_VERSION", (LPBYTE)&dwDXVersion, &ValueSize))
        dwDXVersion = DD_RUNTIME_VERSION;


    bRet = OsThunkDdQueryDirectDrawObject(
        pUnknown6BC->hDirectDrawLocal,
        &HalInfo,
        CallBackFlags,
        &D3dCallbacks,
        &D3dDriverData,
        &D3dBufferCallbacks,
        NULL,
        &NumHeaps,
        NULL,
        &NumFourCC,
        NULL
        );

    if (bRet == FALSE)
    {
        /* TODO: Handle error */
        return FALSE;
    }

    if ((HalInfo.ddCaps.dwSVBCaps2 & DDCAPS2_AUTOFLIPOVERLAY) == 0 &&
        puD3dTextureFormats != NULL)
    {
        bRet = OsThunkDdQueryDirectDrawObject(
            pUnknown6BC->hDirectDrawLocal,
            &HalInfo,
            CallBackFlags,
            &D3dCallbacks,
            &D3dDriverData,
            &D3dBufferCallbacks,
            puD3dTextureFormats,
            &NumHeaps,
            NULL,
            &NumFourCC,
            NULL
            );

        if (FALSE == bRet)
            return FALSE;
    }

    if (NULL == pUnknown6BC->swDDICreateDirectDrawObject)
    {
        *pNumTextureFormats = D3dDriverData.dwNumTextureFormats;
    }

    pDriverCaps->DriverCaps9.Caps = HalInfo.ddCaps.dwCaps;
    pDriverCaps->DriverCaps9.Caps2 = HalInfo.ddCaps.dwCaps2;
    pDriverCaps->DriverCaps9.Caps3 = HalInfo.ddCaps.dwSVCaps;
    pDriverCaps->dwSVBCaps = HalInfo.ddCaps.dwSVBCaps;
    pDriverCaps->dwVSBCaps = HalInfo.ddCaps.dwVSBCaps;
    pDriverCaps->dwSVBCaps2 = HalInfo.ddCaps.dwSVBCaps2;
    pUnknown6BC->lDisplayPitch = HalInfo.vmiData.lDisplayPitch;

    if (HalInfo.dwFlags & DDHALINFO_GETDRIVERINFO2)
    {
        /* GUID_GetDriverInfo2 - Inform driver of DX version */
        {
            DD_DXVERSION DxVersion;

            ResetGetDriverInfo2Data(&DxVersion.gdi2, D3DGDI2_TYPE_DXVERSION, sizeof(DD_DXVERSION));
            DxVersion.dwDXVersion = dwDXVersion;

            PrepareDriverInfoData(&DrvInfo, &DxVersion, sizeof(DxVersion));
            OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo);
        }


        /* GUID_GetDriverInfo2 - Get DDI version */
        {
            ResetGetDriverInfo2Data(&DdiVersion.gdi2, D3DGDI2_TYPE_GETDDIVERSION, sizeof(DD_GETDDIVERSIONDATA));
            PrepareDriverInfoData(&DrvInfo, &DdiVersion, sizeof(DdiVersion));
            bRet = OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo);<--- bRet is assigned

            if (DdiVersion.dwDDIVersion != DX9_DDI_VERSION)
            {
                DWORD ForceDDIOn;

                ValueSize = sizeof(ForceDDIOn);
                if (TRUE == ReadRegistryValue(REG_DWORD, "ForceOldDDIOn", (LPBYTE)&ForceDDIOn, &ValueSize) &&
                    0 != ForceDDIOn)
                {
                    DdiVersion.dwDDIVersion = DX9_DDI_VERSION;
                }
            }
        }


        /* Check for errors to fallback to DX8 mode  */
        if (DdiVersion.dwDDIVersion < DX9_DDI_VERSION)
        {
            bDX8Mode = TRUE;

            if (DdiVersion.dwDDIVersion == 0)
            {
                DPRINT1("Driver claims to be DX9 driver, but didn't report DX9 DDI version - reverting to DX8 mode");
            }
            else
            {
                DPRINT1("Driver claims to be DX9 driver, but was built with an old DDI version - reverting to DX8 mode");
            }

            /* GUID_GetDriverInfo2 - Get D3DCAPS8 */
            {
                D3DCAPS8 DriverCaps8;

                ResetGetDriverInfo2Data((DD_GETDRIVERINFO2DATA*)&DriverCaps8, D3DGDI2_TYPE_GETD3DCAPS8, sizeof(D3DCAPS8));
                PrepareDriverInfoData(&DrvInfo, &DriverCaps8, sizeof(D3DCAPS8));
                
                if (FALSE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo) ||
                    S_OK != DrvInfo.ddRVal ||
                    DrvInfo.dwActualSize != sizeof(D3DCAPS8))
                {
                    DPRINT1("Driver returned an invalid D3DCAPS8 structure - aborting");
                    return FALSE;
                }

                memcpy(&pDriverCaps->DriverCaps9, &DriverCaps8, sizeof(D3DCAPS8));
                pDriverCaps->DriverCaps9.Caps = HalInfo.ddCaps.dwCaps;
                pDriverCaps->dwDriverCaps |= D3D9_INT_D3DCAPS8_VALID;
            }
        }


        /* GUID_GetDriverInfo2 - Get D3DCAPS9 */
        if (FALSE == bDX8Mode)
        {
            D3DCAPS9 DriverCaps9;

            ResetGetDriverInfo2Data((DD_GETDRIVERINFO2DATA*)&DriverCaps9, D3DGDI2_TYPE_GETD3DCAPS9, sizeof(D3DCAPS9));
            PrepareDriverInfoData(&DrvInfo, &DriverCaps9, sizeof(D3DCAPS9));
            
            if (FALSE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo) ||
                S_OK != DrvInfo.ddRVal ||
                DrvInfo.dwActualSize != sizeof(D3DCAPS9))
            {
                DPRINT1("Driver returned an invalid D3DCAPS9 structure - aborting");
                return FALSE;
            }

            pDriverCaps->DriverCaps9 = DriverCaps9;
            pDriverCaps->DriverCaps9.Caps = HalInfo.ddCaps.dwCaps;
            pDriverCaps->dwDriverCaps |= D3D9_INT_D3DCAPS9_VALID;
        }


        /* GUID_GetDriverInfo2 - Get format count data */
        {
            ResetGetDriverInfo2Data(&FormatCountData.gdi2, D3DGDI2_TYPE_GETFORMATCOUNT, sizeof(DD_GETFORMATCOUNTDATA));
            PrepareDriverInfoData(&DrvInfo, &FormatCountData, sizeof(DD_GETFORMATCOUNTDATA));
            FormatCountData.dwFormatCount = UINT_MAX;
            FormatCountData.dwReserved = dwDXVersion;

            if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
            {
                if (DrvInfo.ddRVal != S_OK)
                {
                    DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI_TYPE_GETFORMATCOUNT in GetDriverInfo call");
                    return FALSE;
                }
                else if (DrvInfo.dwActualSize != sizeof(DD_GETFORMATCOUNTDATA))
                {
                    DPRINT1("Driver returned an invalid DD_GETFORMATCOUNTDATA structure - aborting");
                    return FALSE;
                }
                else if (FormatCountData.dwFormatCount == UINT_MAX)
                {
                    DPRINT1("Driver didn't set DD_GETFORMATCOUNTDATA.dwFormatCount - aborting");
                    return FALSE;
                }

                *pNumTextureFormats = FormatCountData.dwFormatCount;
            }
        }

        /* GUID_GetDriverInfo2 - Get format data */
        if (puD3dTextureFormats != NULL)
        {
            DWORD FormatIndex;
            DD_GETFORMATDATA FormatData;

            for (FormatIndex = 0; FormatIndex < FormatCountData.dwFormatCount; FormatIndex++)
            {
                ResetGetDriverInfo2Data(&FormatData.gdi2, D3DGDI2_TYPE_GETFORMAT, sizeof(DD_GETFORMATDATA));
                PrepareDriverInfoData(&DrvInfo, &FormatData, sizeof(DD_GETFORMATDATA));
                FormatData.dwFormatIndex = FormatIndex;

                if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
                {
                    if (DrvInfo.ddRVal != S_OK)
                    {
                        DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI_TYPE_GETFORMAT in GetDriverInfo call");
                        return FALSE;
                    }
                    else if (DrvInfo.dwActualSize != sizeof(DD_GETFORMATDATA))
                    {
                        DPRINT1("Driver returned an invalid DD_GETFORMATDATA structure - aborting");
                        return FALSE;
                    }
                    else if (FormatData.format.dwSize != sizeof(DDPIXELFORMAT))
                    {
                        DPRINT1("Driver didn't set DD_GETFORMATDATA.format - aborting");
                        return FALSE;
                    }

                    /* Copy format data to puD3dTextureFormats */
                    memset(puD3dTextureFormats, 0, sizeof(DDSURFACEDESC));
                    puD3dTextureFormats->dwSize = sizeof(DDSURFACEDESC);
                    puD3dTextureFormats->dwFlags = DDSD_PIXELFORMAT;
                    memcpy(&puD3dTextureFormats->ddpfPixelFormat, &FormatData.format, sizeof(DDPIXELFORMAT));

                    if ((FormatData.format.dwOperations & D3DFORMAT_OP_PIXELSIZE) != 0 &&
                        FormatData.format.dwPrivateFormatBitCount > 0)
                    {
                        /* TODO: Register driver's own pixelformat */
                    }

                    ++puD3dTextureFormats;
                }
            }
        }

        /* GUID_GetDriverInfo2 - Get extended mode count data */
        {
            ResetGetDriverInfo2Data(&ExModeCountData.gdi2, D3DGDI2_TYPE_GETEXTENDEDMODECOUNT, sizeof(DD_GETEXTENDEDMODECOUNTDATA));
            PrepareDriverInfoData(&DrvInfo, &ExModeCountData, sizeof(DD_GETEXTENDEDMODECOUNTDATA));
            ExModeCountData.dwModeCount = UINT_MAX;
            ExModeCountData.dwReserved = dwDXVersion;

            if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
            {
                if (DrvInfo.ddRVal == S_OK)
                {
                    if (DrvInfo.dwActualSize != sizeof(DD_GETEXTENDEDMODECOUNTDATA))
                    {
                        DPRINT1("Driver returned an invalid DD_GETEXTENDEDFORMATCOUNTDATA structure - aborting");
                        return FALSE;
                    }
                    else if (ExModeCountData.dwModeCount == UINT_MAX)
                    {
                        DPRINT1("Driver didn't set DD_GETEXTENDEDMODECOUNTDATA.dwModeCount - aborting");
                        return FALSE;
                    }

                    *pNumExtendedFormats = ExModeCountData.dwModeCount;
                }
                else
                {
                    ExModeCountData.dwModeCount = 0;
                }
            }
        }

        /* GUID_GetDriverInfo2 - Get extended mode data */
        if (pD3dDisplayModeList != NULL)
        {
            DWORD ModeIndex;
            DD_GETEXTENDEDMODEDATA ExModeData;

            for (ModeIndex = 0; ModeIndex < ExModeCountData.dwModeCount; ModeIndex++)
            {
                ResetGetDriverInfo2Data(&ExModeData.gdi2, D3DGDI2_TYPE_GETEXTENDEDMODE, sizeof(DD_GETEXTENDEDMODEDATA));
                PrepareDriverInfoData(&DrvInfo, &ExModeData, sizeof(DD_GETEXTENDEDMODEDATA));
                ExModeData.dwModeIndex = ModeIndex;
                ExModeData.mode.Width = UINT_MAX;

                if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
                {
                    if (DrvInfo.ddRVal != S_OK)
                    {
                        DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI2_TYPE_GETEXTENDEDMODE in GetDriverInfo call");
                        return FALSE;
                    }
                    else if (DrvInfo.dwActualSize != sizeof(DD_GETEXTENDEDMODEDATA))
                    {
                        DPRINT1("Driver returned an invalid DD_GETEXTENDEDMODEDATA structure - aborting");
                        return FALSE;
                    }
                    else if (ExModeData.mode.Width != UINT_MAX)
                    {
                        DPRINT1("Driver didn't set DD_GETEXTENDEDMODEDATA.mode - aborting");
                        return FALSE;
                    }

                    memcpy(pD3dDisplayModeList, &ExModeData.mode, sizeof(D3DDISPLAYMODE));
                    ++pD3dDisplayModeList;
                }
            }
        }

        /* GUID_GetDriverInfo2 - Get adapter group */
        {
            DD_GETADAPTERGROUPDATA AdapterGroupData;
            ResetGetDriverInfo2Data(&AdapterGroupData.gdi2, D3DGDI2_TYPE_GETADAPTERGROUP, sizeof(DD_GETADAPTERGROUPDATA));
            PrepareDriverInfoData(&DrvInfo, &AdapterGroupData, sizeof(DD_GETADAPTERGROUPDATA));
            AdapterGroupData.ulUniqueAdapterGroupId = UINT_MAX;

            if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
            {
                if (DrvInfo.ddRVal != S_OK)
                {
                    DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI2_TYPE_GETADAPTERGROUP in GetDriverInfo call");
                    return FALSE;
                }
                else if (DrvInfo.dwActualSize != sizeof(DD_GETADAPTERGROUPDATA))
                {
                    DPRINT1("Driver returned an invalid DD_GETADAPTERGROUPDATA structure - aborting");
                    return FALSE;
                }
                else if (AdapterGroupData.ulUniqueAdapterGroupId == UINT_MAX)
                {
                    DPRINT1("Driver didn't set DD_GETADAPTERGROUPDATA.ulUniqueAdapterGroupId - aborting");
                    return FALSE;
                }

                pDriverCaps->ulUniqueAdapterGroupId = (ULONG)AdapterGroupData.ulUniqueAdapterGroupId;
            }
        }

        /* GUID_GetDriverInfo2 - Query count data */
        {
            ResetGetDriverInfo2Data(&D3dQueryCountData.gdi2, D3DGDI2_TYPE_GETD3DQUERYCOUNT, sizeof(DD_GETD3DQUERYCOUNTDATA));
            PrepareDriverInfoData(&DrvInfo, &D3dQueryCountData, sizeof(DD_GETD3DQUERYCOUNTDATA));
            D3dQueryCountData.dwNumQueries = UINT_MAX;

            if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
            {
                if (DrvInfo.ddRVal != S_OK)
                {
                    DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI2_TYPE_GETD3DQUERYCOUNT in GetDriverInfo call");
                    return FALSE;
                }
                else if (DrvInfo.dwActualSize != sizeof(DD_GETD3DQUERYCOUNTDATA))
                {
                    DPRINT1("Driver returned an invalid DD_GETD3DQUERYCOUNTDATA structure - aborting");
                    return FALSE;
                }
                else if (D3dQueryCountData.dwNumQueries == UINT_MAX)
                {
                    DPRINT1("Driver didn't set DD_GETD3DQUERYCOUNTDATA.dwNumQueries - aborting");
                    return FALSE;
                }

                *pNumQueries = D3dQueryCountData.dwNumQueries;
            }
        }

        /* GUID_GetDriverInfo2 - Query data */
        if (pD3dQueryList != NULL)
        {
            DWORD QueryIndex;
            DD_GETD3DQUERYDATA D3dQueryData;

            for (QueryIndex = 0; QueryIndex < D3dQueryCountData.dwNumQueries; QueryIndex++)
            {
                ResetGetDriverInfo2Data(&D3dQueryData.gdi2, D3DGDI2_TYPE_GETD3DQUERY, sizeof(DD_GETD3DQUERYDATA));
                PrepareDriverInfoData(&DrvInfo, &D3dQueryData, sizeof(DD_GETD3DQUERYDATA));
                D3dQueryData.dwQueryIndex = QueryIndex;

                if (TRUE == OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo))
                {
                    if (DrvInfo.ddRVal != S_OK)
                    {
                        DPRINT1("Driver claimed to be DX9 driver, but didn't support D3DGDI2_TYPE_GETD3DQUERY in GetDriverInfo call");
                        return FALSE;
                    }
                    else if (DrvInfo.dwActualSize != sizeof(DD_GETD3DQUERYDATA))
                    {
                        DPRINT1("Driver returned an invalid DD_GETD3DQUERYDATA structure - aborting");
                        return FALSE;
                    }

                    *pD3dQueryList = D3dQueryData.QueryType;
                    ++pD3dQueryList;
                }
            }
        }
    }

    /* D3dDriverData -> pGblDriverData */
    memcpy(&pGblDriverData->hwCaps, &D3dDriverData.hwCaps, sizeof(D3DNTHALDEVICEDESC_V1));
    pGblDriverData->dwNumVertices = D3dDriverData.dwNumVertices;
    pGblDriverData->dwNumClipVertices = D3dDriverData.dwNumClipVertices;

    /* GUID_D3DExtendedCaps */
    {     
        DrvInfo.dwSize = sizeof(DD_GETDRIVERINFODATA);
        DrvInfo.guidInfo = GUID_D3DExtendedCaps;
        DrvInfo.dwExpectedSize = sizeof(D3DHAL_D3DEXTENDEDCAPS);
        DrvInfo.lpvData = pD3dExtendedCaps;
        bRet = OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo);<--- bRet is overwritten

        if (TRUE != bRet || DrvInfo.ddRVal != S_OK)
        {
            DPRINT1("Driver failed call to GetDriverInfo() with: GUID_D3DExtendedCaps");
            return FALSE;
        }
    }

    /* GUID_ZPixelFormats */
    {
        DDPIXELFORMAT *pZPixelFormats = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FormatCountData.dwFormatCount * sizeof(DDPIXELFORMAT));

        DrvInfo.dwSize = sizeof(DD_GETDRIVERINFODATA);
        DrvInfo.guidInfo = GUID_ZPixelFormats;
        DrvInfo.dwExpectedSize = FormatCountData.dwFormatCount * sizeof(DDPIXELFORMAT);
        DrvInfo.lpvData = pZPixelFormats;
        bRet = OsThunkDdGetDriverInfo(pUnknown6BC->hDirectDrawLocal, &DrvInfo);

        if (TRUE != bRet || DrvInfo.ddRVal != S_OK)
        {
            DPRINT1("Driver failed call to GetDriverInfo() with: GUID_ZPixelFormats");
            HeapFree(GetProcessHeap(), 0, pZPixelFormats);
            return FALSE;
        }

        *pNumZStencilFormats = FormatCountData.dwFormatCount;

        if (pD3dZStencilFormatList != NULL)
            memcpy(pD3dZStencilFormatList, pZPixelFormats, FormatCountData.dwFormatCount * sizeof(DDPIXELFORMAT));

        HeapFree(GetProcessHeap(), 0, pZPixelFormats);
    }

    /* Get current display format */
    {
        D3DDISPLAYMODE CurrentDisplayMode;
        GetAdapterMode(lpszDeviceName, &CurrentDisplayMode);
        pUnknown6BC->RawDisplayFormat = CurrentDisplayMode.Format;
        pUnknown6BC->DisplayFormat = CurrentDisplayMode.Format;

        if ((HalInfo.vmiData.ddpfDisplay.dwFlags & DDPF_ALPHAPIXELS) != 0)
        {
            if (CurrentDisplayMode.Format == D3DFMT_X8R8G8B8)
            {
                pUnknown6BC->DisplayFormat = D3DFMT_A8R8G8B8;
            }
            else if (CurrentDisplayMode.Format == D3DFMT_X1R5G5B5)
            {
                pUnknown6BC->DisplayFormat = D3DFMT_A1R5G5B5;
            }
        }

        pDriverCaps->dwDisplayWidth = CurrentDisplayMode.Width;
        pDriverCaps->dwDisplayHeight = CurrentDisplayMode.Height;
        pDriverCaps->RawDisplayFormat = CurrentDisplayMode.Format;
        pDriverCaps->DisplayFormat = pUnknown6BC->DisplayFormat;
        pDriverCaps->dwRefreshRate = CurrentDisplayMode.RefreshRate;
    }

    /* TODO: Set all internal function pointers to create surface, etc. */
    pD3D9Callbacks->DdGetAvailDriverMemory = &D3d9GetAvailDriverMemory;

    /* Set device rect */
    {
        HMONITOR hMonitor;
        MONITORINFO MonitorInfo;
        
        memset(&MonitorInfo, 0, sizeof(MONITORINFO));
        MonitorInfo.cbSize = sizeof(MONITORINFO);
        
        hMonitor = GetAdapterMonitor(lpszDeviceName);
        if (TRUE == GetMonitorInfoA(hMonitor, &MonitorInfo))
        {
            pUnknown6BC->DeviceRect = MonitorInfo.rcMonitor;
        }
        else
        {
            DPRINT1("Could not get monitor information");
        }
    }

    pUnknown6BC->dwCaps = pDriverCaps->DriverCaps9.Caps;
    pUnknown6BC->dwSVBCaps = pDriverCaps->dwSVBCaps;

    if (FALSE == bDX8Mode)
    {
        pUnknown6BC->MajorDxVersion = 9;

        if (0 != (pDriverCaps->DriverCaps9.VertexProcessingCaps & D3DVTXPCAPS_NO_VSDT_UBYTE4))
        {
            DPRINT1("Driver claimed to be DX9 driver, but used depricated D3DCAPS9.VertexProcessingCaps: D3DVTXPCAPS_NO_VSDT_UBYTE4 instead of not setting D3DCAPS9.DeclTypes: D3DDTCAPS_UBYTE4.");
            return FALSE;
        }
    }
    else
    {
        pUnknown6BC->MajorDxVersion = 8;

        if (0 == (pDriverCaps->DriverCaps9.VertexProcessingCaps & D3DVTXPCAPS_NO_VSDT_UBYTE4))
        {
            pDriverCaps->DriverCaps9.DeclTypes |= D3DDTCAPS_UBYTE4;
            pDriverCaps->DriverCaps9.VertexProcessingCaps &= ~D3DVTXPCAPS_NO_VSDT_UBYTE4;
        }
    }

    return TRUE;
}