summaryrefslogtreecommitdiff
path: root/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/gallium/VBoxGaD3DDevice9Ex.cpp
blob: 54a09293e9768e8f49b5f3fff2c66a45f2d025ee (plain)
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
/* $Id$ */
/** @file
 * VirtualBox Windows Guest Mesa3D - Gallium driver interface.
 *
 * GaDirect3DDevice9Ex implements IDirect3DDevice9Ex wrapper.
 */

/*
 * Copyright (C) 2016-2022 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#include "VBoxGaD3DDevice9Ex.h"
#include "VBoxD3DAdapter9.h"
#include "GaDrvEnvWddm.h"

#include <iprt/asm.h>

/*
 * GaDirect3DDevice9Ex
 *
 * IDirect3DDevice9Ex wrapper for implementation in Gallium D3D9 state tracker "nine".
 */

GaDirect3DDevice9Ex::GaDirect3DDevice9Ex(IGaDirect3D9Ex *pD3D9Ex,
                                         HANDLE hAdapter,
                                         HANDLE hDevice,
                                         const D3DDDI_DEVICECALLBACKS *pDeviceCallbacks)
    :
    mcRefs(0),
    mhAdapter(hAdapter),
    mhDevice(hDevice),
    mpD3D9Ex(pD3D9Ex),
    mpStack(0),
    mpPresentationGroup(0),
    mpDevice(0)
{
    mpD3D9Ex->AddRef();
    mDeviceCallbacks = *pDeviceCallbacks;
}

GaDirect3DDevice9Ex::~GaDirect3DDevice9Ex()
{
    cleanup();
}

HRESULT GaDirect3DDevice9Ex::Init(D3DDEVTYPE DeviceType,
                                  HWND hFocusWindow,
                                  DWORD BehaviorFlags,
                                  D3DPRESENT_PARAMETERS* pPresentationParameters,
                                  D3DDISPLAYMODEEX* pFullscreenDisplayMode)
{
    mpStack = mpD3D9Ex->GetGalliumStack();
    mpStack->AddRef();

    HRESULT hr = WDDMPresentGroupCreate(this, &mpPresentationGroup);
    if (SUCCEEDED(hr))
    {
        hr = D3DAdapter9_CreateDeviceEx(mpD3D9Ex->GetAdapter9(),
                                        D3DADAPTER_DEFAULT, DeviceType,
                                        hFocusWindow, BehaviorFlags,
                                        pPresentationParameters, pFullscreenDisplayMode,
                                        mpD3D9Ex, mpPresentationGroup,
                                        &mpDevice);
    }

    return hr;
}

void GaDirect3DDevice9Ex::cleanup()
{
    if (mpDevice)
    {
        mpDevice->Release();
        mpDevice = 0;
    }

    if (mpPresentationGroup)
    {
        mpPresentationGroup->Release();
        mpPresentationGroup = 0;
    }

    if (mpStack)
    {
        mpStack->Release();
        mpStack = 0;
    }

    if (mpD3D9Ex)
    {
        mpD3D9Ex->Release();
        mpD3D9Ex = 0;
    }
}

/* IUnknown wrappers. */
STDMETHODIMP_(ULONG) GaDirect3DDevice9Ex::AddRef()
{
    ULONG refs = InterlockedIncrement(&mcRefs);
    return refs;
}

STDMETHODIMP_(ULONG) GaDirect3DDevice9Ex::Release()
{
    ULONG refs = InterlockedDecrement(&mcRefs);
    if (refs == 0)
    {
        delete this;
    }

    return refs;
}

STDMETHODIMP GaDirect3DDevice9Ex::QueryInterface(REFIID riid,
                                                 void **ppvObject)
{
    if (!ppvObject)
    {
        return E_POINTER;
    }

    HRESULT hr = mpDevice?
                     mpDevice->QueryInterface(riid, ppvObject):
                     E_NOINTERFACE;
    if (FAILED(hr))
    {
        if (IsEqualGUID(IID_IGaDirect3DDevice9Ex, riid))
        {
            AddRef();
            *ppvObject = this;
            hr = S_OK;
        }
        else
        {
            *ppvObject = NULL;
            hr = E_NOINTERFACE;
        }
    }

    return hr;
}

#define GADEVICE9WRAP(name, params, vars) \
STDMETHODIMP GaDirect3DDevice9Ex::name params \
{ \
    if (mpDevice) \
        return mpDevice->name vars; \
    return E_FAIL; \
}

#define GADEVICE9WRAP_(type, name, params, vars) \
STDMETHODIMP_(type) GaDirect3DDevice9Ex::name params \
{ \
    if (mpDevice) \
        return mpDevice->name vars; \
    return 0; /** @todo retval */ \
}

#define GADEVICE9WRAPV(name, params, vars) \
STDMETHODIMP_(void) GaDirect3DDevice9Ex::name params \
{ \
    if (mpDevice) \
        return mpDevice->name vars; \
    return; \
}

GADEVICE9WRAP(TestCooperativeLevel,
                  (THIS),
                  ())
GADEVICE9WRAP_(UINT, GetAvailableTextureMem,
                  (THIS),
                  ())
GADEVICE9WRAP(EvictManagedResources,
                  (THIS),
                  ())
GADEVICE9WRAP(GetDirect3D,
                  (THIS_ IDirect3D9** ppD3D9),
                  (ppD3D9))
GADEVICE9WRAP(GetDeviceCaps,
                  (THIS_ D3DCAPS9* pCaps),
                  (pCaps))
GADEVICE9WRAP(GetDisplayMode,
                  (THIS_ UINT iSwapChain,D3DDISPLAYMODE* pMode),
                  (iSwapChain, pMode))
GADEVICE9WRAP(GetCreationParameters,
                  (THIS_ D3DDEVICE_CREATION_PARAMETERS *pParameters),
                  (pParameters))
GADEVICE9WRAP(SetCursorProperties,
                  (THIS_ UINT XHotSpot,UINT YHotSpot,IDirect3DSurface9* pCursorBitmap),
                  (XHotSpot, YHotSpot, pCursorBitmap))
GADEVICE9WRAPV(SetCursorPosition,
                  (THIS_ int X,int Y,DWORD Flags),
                  (X, Y, Flags))
GADEVICE9WRAP_(BOOL, ShowCursor,
                  (THIS_ BOOL bShow),
                  (bShow))
GADEVICE9WRAP(CreateAdditionalSwapChain,
                  (THIS_ D3DPRESENT_PARAMETERS* pPresentationParameters,IDirect3DSwapChain9** pSwapChain),
                  (pPresentationParameters, pSwapChain))
GADEVICE9WRAP(GetSwapChain,
                  (THIS_ UINT iSwapChain,IDirect3DSwapChain9** pSwapChain),
                  (iSwapChain, pSwapChain))
GADEVICE9WRAP_(UINT, GetNumberOfSwapChains,
                  (THIS),
                  ())
GADEVICE9WRAP(Reset,
                  (THIS_ D3DPRESENT_PARAMETERS* pPresentationParameters),
                  (pPresentationParameters))
GADEVICE9WRAP(Present,
                  (THIS_ CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion),
                  (pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion))
GADEVICE9WRAP(GetBackBuffer,
                  (THIS_ UINT iSwapChain,UINT iBackBuffer,D3DBACKBUFFER_TYPE Type,IDirect3DSurface9** ppBackBuffer),
                  (iSwapChain, iBackBuffer, Type, ppBackBuffer))
GADEVICE9WRAP(GetRasterStatus,
                  (THIS_ UINT iSwapChain,D3DRASTER_STATUS* pRasterStatus),
                  (iSwapChain, pRasterStatus))
GADEVICE9WRAP(SetDialogBoxMode,
                  (THIS_ BOOL bEnableDialogs),
                  (bEnableDialogs))
GADEVICE9WRAPV(SetGammaRamp,
                  (THIS_ UINT iSwapChain,DWORD Flags,CONST D3DGAMMARAMP* pRamp),
                  (iSwapChain, Flags, pRamp))
GADEVICE9WRAPV(GetGammaRamp,
                  (THIS_ UINT iSwapChain,D3DGAMMARAMP* pRamp),
                  (iSwapChain, pRamp))
GADEVICE9WRAP(CreateTexture,
                  (THIS_ UINT Width,UINT Height,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DTexture9** ppTexture,HANDLE* pSharedHandle),
                  (Width, Height, Levels, Usage, Format, Pool, ppTexture, pSharedHandle))
GADEVICE9WRAP(CreateVolumeTexture,
                  (THIS_ UINT Width,UINT Height,UINT Depth,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DVolumeTexture9** ppVolumeTexture,HANDLE* pSharedHandle),
                  (Width, Height, Depth, Levels, Usage, Format, Pool, ppVolumeTexture, pSharedHandle))
GADEVICE9WRAP(CreateCubeTexture,
                  (THIS_ UINT EdgeLength,UINT Levels,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DCubeTexture9** ppCubeTexture,HANDLE* pSharedHandle),
                  (EdgeLength, Levels, Usage, Format, Pool, ppCubeTexture, pSharedHandle))
GADEVICE9WRAP(CreateVertexBuffer,
                  (THIS_ UINT Length,DWORD Usage,DWORD FVF,D3DPOOL Pool,IDirect3DVertexBuffer9** ppVertexBuffer,HANDLE* pSharedHandle),
                  (Length, Usage, FVF, Pool, ppVertexBuffer, pSharedHandle))
GADEVICE9WRAP(CreateIndexBuffer,
                  (THIS_ UINT Length,DWORD Usage,D3DFORMAT Format,D3DPOOL Pool,IDirect3DIndexBuffer9** ppIndexBuffer,HANDLE* pSharedHandle),
                  (Length, Usage, Format, Pool, ppIndexBuffer, pSharedHandle))
GADEVICE9WRAP(CreateRenderTarget,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Lockable,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle),
                  (Width, Height, Format, MultiSample, MultisampleQuality, Lockable, ppSurface, pSharedHandle))
GADEVICE9WRAP(CreateDepthStencilSurface,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Discard,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle),
                  (Width, Height, Format, MultiSample, MultisampleQuality, Discard, ppSurface, pSharedHandle))
GADEVICE9WRAP(UpdateSurface,
                  (THIS_ IDirect3DSurface9* pSourceSurface,CONST RECT* pSourceRect,IDirect3DSurface9* pDestinationSurface,CONST POINT* pDestPoint),
                  (pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint))
GADEVICE9WRAP(UpdateTexture,
                  (THIS_ IDirect3DBaseTexture9* pSourceTexture,IDirect3DBaseTexture9* pDestinationTexture),
                  (pSourceTexture, pDestinationTexture))
GADEVICE9WRAP(GetRenderTargetData,
                  (THIS_ IDirect3DSurface9* pRenderTarget,IDirect3DSurface9* pDestSurface),
                  (pRenderTarget, pDestSurface))
GADEVICE9WRAP(GetFrontBufferData,
                  (THIS_ UINT iSwapChain,IDirect3DSurface9* pDestSurface),
                  (iSwapChain, pDestSurface))
GADEVICE9WRAP(StretchRect,
                  (THIS_ IDirect3DSurface9* pSourceSurface,CONST RECT* pSourceRect,IDirect3DSurface9* pDestSurface,CONST RECT* pDestRect,D3DTEXTUREFILTERTYPE Filter),
                  (pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter))
GADEVICE9WRAP(ColorFill,
                  (THIS_ IDirect3DSurface9* pSurface,CONST RECT* pRect,D3DCOLOR color),
                  (pSurface, pRect, color))
GADEVICE9WRAP(CreateOffscreenPlainSurface,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DPOOL Pool,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle),
                  (Width, Height, Format, Pool, ppSurface, pSharedHandle))
GADEVICE9WRAP(SetRenderTarget,
                  (THIS_ DWORD RenderTargetIndex,IDirect3DSurface9* pRenderTarget),
                  (RenderTargetIndex, pRenderTarget))
GADEVICE9WRAP(GetRenderTarget,
                  (THIS_ DWORD RenderTargetIndex,IDirect3DSurface9** ppRenderTarget),
                  (RenderTargetIndex, ppRenderTarget))
GADEVICE9WRAP(SetDepthStencilSurface,
                  (THIS_ IDirect3DSurface9* pNewZStencil),
                  (pNewZStencil))
GADEVICE9WRAP(GetDepthStencilSurface,
                  (THIS_ IDirect3DSurface9** ppZStencilSurface),
                  (ppZStencilSurface))
GADEVICE9WRAP(BeginScene,
                  (THIS),
                  ())
GADEVICE9WRAP(EndScene,
                  (THIS),
                  ())
GADEVICE9WRAP(Clear,
                  (THIS_ DWORD Count,CONST D3DRECT* pRects,DWORD Flags,D3DCOLOR Color,float Z,DWORD Stencil),
                  (Count, pRects, Flags, Color, Z, Stencil))
GADEVICE9WRAP(SetTransform,
                  (THIS_ D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix),
                  (State, pMatrix))
GADEVICE9WRAP(GetTransform,
                  (THIS_ D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix),
                  (State, pMatrix))
GADEVICE9WRAP(MultiplyTransform,
                  (THIS_ D3DTRANSFORMSTATETYPE State,CONST D3DMATRIX* pMatrix),
                  (State, pMatrix))
GADEVICE9WRAP(SetViewport,
                  (THIS_ CONST D3DVIEWPORT9* pViewport),
                  (pViewport))
GADEVICE9WRAP(GetViewport,
                  (THIS_ D3DVIEWPORT9* pViewport),
                  (pViewport))
GADEVICE9WRAP(SetMaterial,
                  (THIS_ CONST D3DMATERIAL9* pMaterial),
                  (pMaterial))
GADEVICE9WRAP(GetMaterial,
                  (THIS_ D3DMATERIAL9* pMaterial),
                  (pMaterial))
GADEVICE9WRAP(SetLight,
                  (THIS_ DWORD Index,CONST D3DLIGHT9* pLight),
                  (Index, pLight))
GADEVICE9WRAP(GetLight,
                  (THIS_ DWORD Index,D3DLIGHT9* pLight),
                  (Index, pLight))
GADEVICE9WRAP(LightEnable,
                  (THIS_ DWORD Index,BOOL Enable),
                  (Index, Enable))
GADEVICE9WRAP(GetLightEnable,
                  (THIS_ DWORD Index,BOOL* pEnable),
                  (Index, pEnable))
GADEVICE9WRAP(SetClipPlane,
                  (THIS_ DWORD Index,CONST float* pPlane),
                  (Index, pPlane))
GADEVICE9WRAP(GetClipPlane,
                  (THIS_ DWORD Index,float* pPlane),
                  (Index, pPlane))
GADEVICE9WRAP(SetRenderState,
                  (THIS_ D3DRENDERSTATETYPE State,DWORD Value),
                  (State, Value))
GADEVICE9WRAP(GetRenderState,
                  (THIS_ D3DRENDERSTATETYPE State,DWORD* pValue),
                  (State, pValue))
GADEVICE9WRAP(CreateStateBlock,
                  (THIS_ D3DSTATEBLOCKTYPE Type,IDirect3DStateBlock9** ppSB),
                  (Type, ppSB))
GADEVICE9WRAP(BeginStateBlock,
                  (THIS),
                  ())
GADEVICE9WRAP(EndStateBlock,
                  (THIS_ IDirect3DStateBlock9** ppSB),
                  (ppSB))
GADEVICE9WRAP(SetClipStatus,
                  (THIS_ CONST D3DCLIPSTATUS9* pClipStatus),
                  (pClipStatus))
GADEVICE9WRAP(GetClipStatus,
                  (THIS_ D3DCLIPSTATUS9* pClipStatus),
                  (pClipStatus))
GADEVICE9WRAP(GetTexture,
                  (THIS_ DWORD Stage,IDirect3DBaseTexture9** ppTexture),
                  (Stage, ppTexture))
GADEVICE9WRAP(SetTexture,
                  (THIS_ DWORD Stage,IDirect3DBaseTexture9* pTexture),
                  (Stage, pTexture))
GADEVICE9WRAP(GetTextureStageState,
                  (THIS_ DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue),
                  (Stage, Type, pValue))
GADEVICE9WRAP(SetTextureStageState,
                  (THIS_ DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD Value),
                  (Stage, Type, Value))
GADEVICE9WRAP(GetSamplerState,
                  (THIS_ DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD* pValue),
                  (Sampler, Type, pValue))
GADEVICE9WRAP(SetSamplerState,
                  (THIS_ DWORD Sampler,D3DSAMPLERSTATETYPE Type,DWORD Value),
                  (Sampler, Type, Value))
GADEVICE9WRAP(ValidateDevice,
                  (THIS_ DWORD* pNumPasses),
                  (pNumPasses))
GADEVICE9WRAP(SetPaletteEntries,
                  (THIS_ UINT PaletteNumber,CONST PALETTEENTRY* pEntries),
                  (PaletteNumber, pEntries))
GADEVICE9WRAP(GetPaletteEntries,
                  (THIS_ UINT PaletteNumber,PALETTEENTRY* pEntries),
                  (PaletteNumber, pEntries))
GADEVICE9WRAP(SetCurrentTexturePalette,
                  (THIS_ UINT PaletteNumber),
                  (PaletteNumber))
GADEVICE9WRAP(GetCurrentTexturePalette,
                  (THIS_ UINT *PaletteNumber),
                  (PaletteNumber))
GADEVICE9WRAP(SetScissorRect,
                  (THIS_ CONST RECT* pRect),
                  (pRect))
GADEVICE9WRAP(GetScissorRect,
                  (THIS_ RECT* pRect),
                  (pRect))
GADEVICE9WRAP(SetSoftwareVertexProcessing,
                  (THIS_ BOOL bSoftware),
                  (bSoftware))
GADEVICE9WRAP_(BOOL, GetSoftwareVertexProcessing,
                  (THIS),
                  ())
GADEVICE9WRAP(SetNPatchMode,
                  (THIS_ float nSegments),
                  (nSegments))
GADEVICE9WRAP_(float, GetNPatchMode,
                  (THIS),
                  ())
GADEVICE9WRAP(DrawPrimitive,
                  (THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT StartVertex,UINT PrimitiveCount),
                  (PrimitiveType, StartVertex, PrimitiveCount))
GADEVICE9WRAP(DrawIndexedPrimitive,
                  (THIS_ D3DPRIMITIVETYPE Primitive,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount),
                  (Primitive, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount))
GADEVICE9WRAP(DrawPrimitiveUP,
                  (THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride),
                  (PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride))
GADEVICE9WRAP(DrawIndexedPrimitiveUP,
                  (THIS_ D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,UINT NumVertices,UINT PrimitiveCount,CONST void* pIndexData,D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride),
                  (PrimitiveType, MinVertexIndex, NumVertices, PrimitiveCount, pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride))
GADEVICE9WRAP(ProcessVertices,
                  (THIS_ UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer9* pDestBuffer,IDirect3DVertexDeclaration9* pVertexDecl,DWORD Flags),
                  (SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags))
GADEVICE9WRAP(CreateVertexDeclaration,
                  (THIS_ CONST D3DVERTEXELEMENT9* pVertexElements,IDirect3DVertexDeclaration9** ppDecl),
                  (pVertexElements, ppDecl))
GADEVICE9WRAP(SetVertexDeclaration,
                  (THIS_ IDirect3DVertexDeclaration9* pDecl),
                  (pDecl))
GADEVICE9WRAP(GetVertexDeclaration,
                  (THIS_ IDirect3DVertexDeclaration9** ppDecl),
                  (ppDecl))
GADEVICE9WRAP(SetFVF,
                  (THIS_ DWORD FVF),
                  (FVF))
GADEVICE9WRAP(GetFVF,
                  (THIS_ DWORD* pFVF),
                  (pFVF))
GADEVICE9WRAP(CreateVertexShader,
                  (THIS_ CONST DWORD* pFunction,IDirect3DVertexShader9** ppShader),
                  (pFunction, ppShader))
GADEVICE9WRAP(SetVertexShader,
                  (THIS_ IDirect3DVertexShader9* pShader),
                  (pShader))
GADEVICE9WRAP(GetVertexShader,
                  (THIS_ IDirect3DVertexShader9** ppShader),
                  (ppShader))
GADEVICE9WRAP(SetVertexShaderConstantF,
                  (THIS_ UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount),
                  (StartRegister, pConstantData, Vector4fCount))
GADEVICE9WRAP(GetVertexShaderConstantF,
                  (THIS_ UINT StartRegister,float* pConstantData,UINT Vector4fCount),
                  (StartRegister, pConstantData, Vector4fCount))
GADEVICE9WRAP(SetVertexShaderConstantI,
                  (THIS_ UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount),
                  (StartRegister, pConstantData, Vector4iCount))
GADEVICE9WRAP(GetVertexShaderConstantI,
                  (THIS_ UINT StartRegister,int* pConstantData,UINT Vector4iCount),
                  (StartRegister, pConstantData, Vector4iCount))
GADEVICE9WRAP(SetVertexShaderConstantB,
                  (THIS_ UINT StartRegister,CONST BOOL* pConstantData,UINT  BoolCount),
                  (StartRegister, pConstantData, BoolCount))
GADEVICE9WRAP(GetVertexShaderConstantB,
                  (THIS_ UINT StartRegister,BOOL* pConstantData,UINT BoolCount),
                  (StartRegister, pConstantData, BoolCount))
GADEVICE9WRAP(SetStreamSource,
                  (THIS_ UINT StreamNumber,IDirect3DVertexBuffer9* pStreamData,UINT OffsetInBytes,UINT Stride),
                  (StreamNumber, pStreamData, OffsetInBytes, Stride))
GADEVICE9WRAP(GetStreamSource,
                  (THIS_ UINT StreamNumber,IDirect3DVertexBuffer9** ppStreamData,UINT* pOffsetInBytes,UINT* pStride),
                  (StreamNumber, ppStreamData, pOffsetInBytes, pStride))
GADEVICE9WRAP(SetStreamSourceFreq,
                  (THIS_ UINT StreamNumber,UINT Setting),
                  (StreamNumber, Setting))
GADEVICE9WRAP(GetStreamSourceFreq,
                  (THIS_ UINT StreamNumber,UINT* pSetting),
                  (StreamNumber, pSetting))
GADEVICE9WRAP(SetIndices,
                  (THIS_ IDirect3DIndexBuffer9* pIndexData),
                  (pIndexData))
GADEVICE9WRAP(GetIndices,
                  (THIS_ IDirect3DIndexBuffer9** ppIndexData),
                  (ppIndexData))
GADEVICE9WRAP(CreatePixelShader,
                  (THIS_ CONST DWORD* pFunction,IDirect3DPixelShader9** ppShader),
                  (pFunction, ppShader))
GADEVICE9WRAP(SetPixelShader,
                  (THIS_ IDirect3DPixelShader9* pShader),
                  (pShader))
GADEVICE9WRAP(GetPixelShader,
                  (THIS_ IDirect3DPixelShader9** ppShader),
                  (ppShader))
GADEVICE9WRAP(SetPixelShaderConstantF,
                  (THIS_ UINT StartRegister,CONST float* pConstantData,UINT Vector4fCount),
                  (StartRegister, pConstantData, Vector4fCount))
GADEVICE9WRAP(GetPixelShaderConstantF,
                  (THIS_ UINT StartRegister,float* pConstantData,UINT Vector4fCount),
                  (StartRegister, pConstantData, Vector4fCount))
GADEVICE9WRAP(SetPixelShaderConstantI,
                  (THIS_ UINT StartRegister,CONST int* pConstantData,UINT Vector4iCount),
                  (StartRegister, pConstantData, Vector4iCount))
GADEVICE9WRAP(GetPixelShaderConstantI,
                  (THIS_ UINT StartRegister,int* pConstantData,UINT Vector4iCount),
                  (StartRegister, pConstantData, Vector4iCount))
GADEVICE9WRAP(SetPixelShaderConstantB,
                  (THIS_ UINT StartRegister,CONST BOOL* pConstantData,UINT  BoolCount),
                  (StartRegister, pConstantData, BoolCount))
GADEVICE9WRAP(GetPixelShaderConstantB,
                  (THIS_ UINT StartRegister,BOOL* pConstantData,UINT BoolCount),
                  (StartRegister, pConstantData, BoolCount))
GADEVICE9WRAP(DrawRectPatch,
                  (THIS_ UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo),
                  (Handle, pNumSegs, pRectPatchInfo))
GADEVICE9WRAP(DrawTriPatch,
                  (THIS_ UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo),
                  (Handle, pNumSegs, pTriPatchInfo))
GADEVICE9WRAP(DeletePatch,
                  (THIS_ UINT Handle),
                  (Handle))
GADEVICE9WRAP(CreateQuery,
                  (THIS_ D3DQUERYTYPE Type,IDirect3DQuery9** ppQuery),
                  (Type, ppQuery))
GADEVICE9WRAP(SetConvolutionMonoKernel,
                  (THIS_ UINT width,UINT height,float* rows,float* columns),
                  (width, height, rows, columns))
GADEVICE9WRAP(ComposeRects,
                  (THIS_ IDirect3DSurface9* pSrc,IDirect3DSurface9* pDst,IDirect3DVertexBuffer9* pSrcRectDescs,UINT NumRects,IDirect3DVertexBuffer9* pDstRectDescs,D3DCOMPOSERECTSOP Operation,int Xoffset,int Yoffset),
                  (pSrc, pDst, pSrcRectDescs, NumRects, pDstRectDescs, Operation, Xoffset, Yoffset))
GADEVICE9WRAP(PresentEx,
                  (THIS_ CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion,DWORD dwFlags),
                  (pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags))
GADEVICE9WRAP(GetGPUThreadPriority,
                  (THIS_ INT* pPriority),
                  (pPriority))
GADEVICE9WRAP(SetGPUThreadPriority,
                  (THIS_ INT Priority),
                  (Priority))
GADEVICE9WRAP(WaitForVBlank,
                  (THIS_ UINT iSwapChain),
                  (iSwapChain))
GADEVICE9WRAP(CheckResourceResidency,
                  (THIS_ IDirect3DResource9** pResourceArray,UINT32 NumResources),
                  (pResourceArray, NumResources))
GADEVICE9WRAP(SetMaximumFrameLatency,
                  (THIS_ UINT MaxLatency),
                  (MaxLatency))
GADEVICE9WRAP(GetMaximumFrameLatency,
                  (THIS_ UINT* pMaxLatency),
                  (pMaxLatency))
GADEVICE9WRAP(CheckDeviceState,
                  (THIS_ HWND hDestinationWindow),
                  (hDestinationWindow))
GADEVICE9WRAP(CreateRenderTargetEx,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Lockable,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle,DWORD Usage),
                  (Width, Height, Format, MultiSample, MultisampleQuality, Lockable, ppSurface, pSharedHandle, Usage))
GADEVICE9WRAP(CreateOffscreenPlainSurfaceEx,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DPOOL Pool,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle,DWORD Usage),
                  (Width, Height, Format, Pool, ppSurface, pSharedHandle, Usage))
GADEVICE9WRAP(CreateDepthStencilSurfaceEx,
                  (THIS_ UINT Width,UINT Height,D3DFORMAT Format,D3DMULTISAMPLE_TYPE MultiSample,DWORD MultisampleQuality,BOOL Discard,IDirect3DSurface9** ppSurface,HANDLE* pSharedHandle,DWORD Usage),
                  (Width, Height, Format, MultiSample, MultisampleQuality, Discard, ppSurface, pSharedHandle, Usage))
GADEVICE9WRAP(ResetEx,
                  (THIS_ D3DPRESENT_PARAMETERS* pPresentationParameters,D3DDISPLAYMODEEX *pFullscreenDisplayMode),
                  (pPresentationParameters, pFullscreenDisplayMode))
GADEVICE9WRAP(GetDisplayModeEx,
                  (THIS_ UINT iSwapChain,D3DDISPLAYMODEEX* pMode,D3DDISPLAYROTATION* pRotation),
                  (iSwapChain, pMode, pRotation))

#undef GADEVICE9WRAPV
#undef GADEVICE9WRAP_
#undef GADEVICE9WRAP


/*
 * IGaDirect3DDevice9Ex methods.
 */
STDMETHODIMP GaDirect3DDevice9Ex::GaSurfaceId(IUnknown *pSurface, uint32_t *pu32Sid)
{
    struct pipe_resource *pResource = mpStack->GaNinePipeResourceFromSurface(pSurface);
    if (pResource)
    {
        struct pipe_screen *pScreen = mpD3D9Ex->GetScreen();
        *pu32Sid = mpStack->GaDrvGetSurfaceId(pScreen, pResource);
    }

    return S_OK;
}

STDMETHODIMP GaDirect3DDevice9Ex::GaWDDMContextHandle(HANDLE *phContext)
{
    struct pipe_context *pPipeContext = mpStack->GaNinePipeContextFromDevice(this->mpDevice);
    if (pPipeContext)
    {
        struct pipe_screen *pScreen = mpD3D9Ex->GetScreen();
        WDDMGalliumDriverEnv const *pEnv = mpStack->GaDrvGetWDDMEnv(pScreen);
        if (pEnv)
        {
            uint32_t u32Cid = mpStack->GaDrvGetContextId(pPipeContext);

            GaDrvEnvWddm *pEnvWddm = (GaDrvEnvWddm *)pEnv->pvEnv;
            *phContext = pEnvWddm->GaDrvEnvWddmContextHandle(u32Cid);
        }
    }

    return S_OK;
}

STDMETHODIMP GaDirect3DDevice9Ex::GaFlush()
{
    struct pipe_context *pPipeContext = mpStack->GaNinePipeContextFromDevice(this->mpDevice);
    if (pPipeContext)
    {
        mpStack->GaDrvContextFlush(pPipeContext);
    }

    return S_OK;
}

STDMETHODIMP GaDirect3DDevice9Ex::EscapeCb(const void *pvData, uint32_t cbData, bool fHardwareAccess)
{
    HANDLE hContext = 0;
    HRESULT hr = GaWDDMContextHandle(&hContext);
    if (SUCCEEDED(hr))
    {
        D3DDDICB_ESCAPE ddiEscape;
        ddiEscape.hDevice               = mhDevice;
        ddiEscape.Flags.Value           = 0;
        if (fHardwareAccess)
        {
            ddiEscape.Flags.HardwareAccess = 1;
        }
        ddiEscape.pPrivateDriverData    = (void *)pvData;
        ddiEscape.PrivateDriverDataSize = cbData;
        ddiEscape.hContext              = hContext;

        hr = mDeviceCallbacks.pfnEscapeCb(mhAdapter, &ddiEscape);
    }

    return hr;
}