xref: /reactos/dll/directx/d3d9/device.c (revision 50cf16b3)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS ReactX
4  * FILE:            dll/directx/d3d9/device.c
5  * PURPOSE:         Direct3D9's device creation
6  * PROGRAMERS:      Gregor Gullwi <gbrunmar (dot) ros (at) gmail (dot) com>
7  */
8 
9 #include "device.h"
10 #include <debug.h>
11 #include "d3d9_helpers.h"
12 #include "d3d9_create.h"
13 
14 static HRESULT InitD3D9ResourceManager(D3D9ResourceManager* pThisResourceManager, LPDIRECT3DDEVICE9_INT pDirect3DDevice9)
15 {
16     DWORD MaxSimultaneousTextures;
17 
18     MaxSimultaneousTextures = max(1, pDirect3DDevice9->DeviceData[0].DriverCaps.DriverCaps9.MaxSimultaneousTextures);
19 
20     if (FAILED(AlignedAlloc((LPVOID *)&pThisResourceManager->pTextureHeap, sizeof(DWORD) + MaxSimultaneousTextures * sizeof(int) * 3)))
21     {
22         DPRINT1("Could not allocate texture heap");
23         return DDERR_OUTOFMEMORY;
24     }
25 
26     // TODO: Init texture heap
27 
28     pThisResourceManager->MaxSimultaneousTextures = MaxSimultaneousTextures;
29     pThisResourceManager->pBaseDevice = pDirect3DDevice9;
30 
31     return D3D_OK;
32 }
33 
34 HRESULT InitD3D9BaseDevice(LPDIRECT3DDEVICE9_INT pThisBaseDevice, LPDIRECT3D9_INT pDirect3D9,
35                            UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviourFlags,
36                            D3DPRESENT_PARAMETERS* pPresentationParameters, DWORD NumAdaptersToCreate)
37 {
38     D3D9ResourceManager* pResourceManager;
39     DWORD i;
40 
41     // Insert Reset/Ctor here
42 
43     if (FAILED(AlignedAlloc((LPVOID *)&pResourceManager, sizeof(D3D9ResourceManager))) ||
44         FAILED(InitD3D9ResourceManager(pResourceManager, pThisBaseDevice)))
45     {
46         DPRINT1("Could not create resource manager");
47         return DDERR_OUTOFMEMORY;
48     }
49 
50     pThisBaseDevice->pResourceManager = pResourceManager;
51 
52     pThisBaseDevice->lpVtbl = &Direct3DDevice9HAL_Vtbl;
53     pThisBaseDevice->lRefCnt = 1;
54     pThisBaseDevice->dwProcessId = GetCurrentThreadId();
55     pThisBaseDevice->pUnknown = (IUnknown*) &pThisBaseDevice->lpVtbl;
56     InitializeCriticalSection(&pThisBaseDevice->CriticalSection);
57 
58     pThisBaseDevice->pDirect3D9 = pDirect3D9;
59     pThisBaseDevice->DeviceType = DeviceType;
60     pThisBaseDevice->hWnd = hFocusWindow;
61     pThisBaseDevice->AdjustedBehaviourFlags = BehaviourFlags;
62     pThisBaseDevice->BehaviourFlags = BehaviourFlags;
63     pThisBaseDevice->NumAdaptersInDevice = NumAdaptersToCreate;
64 
65     // TODO: Query driver for correct DX version
66     pThisBaseDevice->dwDXVersion = 9;
67 
68     for (i = 0; i < NumAdaptersToCreate; i++)
69     {
70         if (FALSE == CreateD3D9DeviceData(&pDirect3D9->DisplayAdapters[i], &pThisBaseDevice->DeviceData[i]))
71         {
72             DPRINT1("Failed to get device data for adapter: %d", i);
73             return DDERR_GENERIC;
74         }
75 
76         pThisBaseDevice->AdapterIndexInGroup[i] = i;
77         pThisBaseDevice->CurrentDisplayMode[i].Width = pDirect3D9->DisplayAdapters[i].DriverCaps.dwDisplayWidth;
78         pThisBaseDevice->CurrentDisplayMode[i].Height = pDirect3D9->DisplayAdapters[i].DriverCaps.dwDisplayHeight;
79         pThisBaseDevice->CurrentDisplayMode[i].RefreshRate = pDirect3D9->DisplayAdapters[i].DriverCaps.dwRefreshRate;
80         pThisBaseDevice->CurrentDisplayMode[i].Format = pDirect3D9->DisplayAdapters[i].DriverCaps.RawDisplayFormat;
81 
82         pThisBaseDevice->pSwapChains[i] = CreateDirect3DSwapChain9(RT_BUILTIN, pThisBaseDevice, i);
83         pThisBaseDevice->pSwapChains2[i] = pThisBaseDevice->pSwapChains[i];
84 
85         if (FAILED(Direct3DSwapChain9_Init(pThisBaseDevice->pSwapChains[i], pPresentationParameters)))
86         {
87             DPRINT1("Failed to init swap chain: %d", i);
88             return DDERR_GENERIC;
89         }
90     }
91 
92     return D3D_OK;
93 }
94 
95 HRESULT CreateD3D9HalDevice(LPDIRECT3D9_INT pDirect3D9, UINT Adapter,
96                             HWND hFocusWindow, DWORD BehaviourFlags,
97                             D3DPRESENT_PARAMETERS* pPresentationParameters,
98                             DWORD NumAdaptersToCreate,
99                             struct IDirect3DDevice9** ppReturnedDeviceInterface)
100 {
101     HRESULT Ret;
102 
103     if (FAILED(AlignedAlloc((LPVOID *)ppReturnedDeviceInterface, sizeof(D3D9HALDEVICE))))
104     {
105         DPRINT1("Not enough memory to create HAL device");
106         return DDERR_OUTOFMEMORY;
107     }
108 
109     Ret = InitD3D9BaseDevice((LPDIRECT3DDEVICE9_INT)*ppReturnedDeviceInterface, pDirect3D9, Adapter,
110                              D3DDEVTYPE_HAL, hFocusWindow, BehaviourFlags,
111                              pPresentationParameters, NumAdaptersToCreate);
112 
113     if (FAILED(Ret))
114     {
115         AlignedFree((LPVOID)*ppReturnedDeviceInterface);
116         return Ret;
117     }
118 
119     return D3D_OK;
120 }
121