-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
d3d9.cpp
184 lines (142 loc) · 4.83 KB
/
d3d9.cpp
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
#include <windows.h>
#include "d3d9helper.hpp"
#include "d3d9.hpp"
#include "d3dobject.hpp"
#include <iostream>
#include <winbase.h>
#include <drm/drm.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
//TODO: add ifdefs when implementing another gpu
#include <drm/i915_drm.h>
IDirect3D9::IDirect3D9(UINT SDKVersion) {
#ifdef DEBUG
std::cout << "libd3d9.so: IDirect3D9::IDirect3D9()" << std::endl;
#endif
//Create connection with DRM
int fd = open("/dev/dri/card0", O_RDWR, 0); //DirectX automatically selects the first GPU
if (fd < 0) {
std::cerr << "\033[1;31m" //RED BOLD
<< "ODX ERROR: Failed to open /dev/dri/card0. Please check if you have the proper permissions to access the device." << std::endl
<< "Direct3DCreate9 fails and returns NULL.\033[0;0m" << std::endl
<< std::endl;
close(fd);
return;
}
//check if the device is Hardware or Software (Does DirectX have this check?)
drm_get_cap gpuType;
int ioctlResult = ioctl(fd, DRM_IOCTL_GET_CAP, &gpuType);
//get device vendor id:
drmVersion* version = drmGetVersion(fd);
if (ioctlResult != 0) {
std::cerr << "\033[1;31m"
<< "ODX ERROR: Failed to get device info" << std::endl
<< "Direct3DCreate9 fails and returns NULL.\033[0;0m" << std::endl
<< std::endl;
close(fd);
perror("ioctl");
return;
}
#ifdef DEBUG
std::cout << "\033[1;32m" //GREEN BOLD
<< "ODX INFO: Device \"" << version->name << '(' << version->desc << ' ' << version->version_minor << '.' << version->version_patchlevel << ')' << "\" is " << (gpuType.value == DRM_CAP_DUMB_BUFFER ? "Software" : "Hardware") << std::endl
<< "\033[0;0m" << std::endl;
#endif
D3DDEVTYPE deviceType = D3DDEVTYPE::D3DDEVTYPE_NULLREF;
if (gpuType.value == DRM_CAP_DUMB_BUFFER) {
deviceType = D3DDEVTYPE::D3DDEVTYPE_SW;
} else {
deviceType = D3DDEVTYPE::D3DDEVTYPE_HAL;
}
IDirect3DDevice9* device;
this->CreateDevice(
D3DADAPTER_DEFAULT,
deviceType,
NULL, // TODO
NULL, // TODO
NULL, // TODO
&device
);
close(fd);
}
ULONG IDirect3D9::AddRef() {
DXGASSERT(((ULONG_PTR)(&m_cRef) & 3) == 0);
InterlockedIncrement((LONG *)&m_cRef);
return m_cRef;
}
HRESULT IDirect3D9::CreateDevice(
UINT Adapter,
D3DDEVTYPE DeviceType,
HWND hFocusWindow,
DWORD BehaviorFlags,
D3DPRESENT_PARAMETERS *pPresentationParameters,
IDirect3DDevice9 **ppReturnedDeviceInterface
) {
#ifdef DEBUG
std::cout << "libd3d9.so: IDirect3D9::CreateDevice()" << std::endl;
#endif
//get GtkWindow specs and put them into D3DPRESENT_PARAMETERS
pPresentationParameters->hDeviceWindow = hFocusWindow;
pPresentationParameters->BackBufferWidth = gtk_widget_get_size(hFocusWindow,GTK_ORIENTATION_HORIZONTAL);
pPresentationParameters->BackBufferHeight = gtk_widget_get_size(hFocusWindow,GTK_ORIENTATION_VERTICAL);
//create the device
IDirect3DDevice9 device = IDirect3DDevice9(/*this, pPresentationParameters*/);
*ppReturnedDeviceInterface = &device;
return 0;
}
ULONG IDirect3D9::Release() {
return 0;
}
HRESULT IDirect3D9::QueryInterface ( REFIID riid, void ** ppvObj ) {
return 0;
}
IDirect3D9* Direct3DCreate9(UINT SDKVersion) {
HINSTANCE instance = NULL;
constexpr const UINT supported_sdk = 0x0900;
if (SDKVersion != supported_sdk) {
std::cout << "\033[1;31m" //RED BOLD
<< std::endl
<< "D3D ERROR: This application compiled against improper D3D headers." << std::endl
<< "The application is compiled with SDK version (" << SDKVersion << ") but the currently installed" << std::endl
<< "runtime supports versions from (" << supported_sdk << ")." << std::endl
<< "Please recompile with an up-to-date SDK.\033[0;0m" << std::endl
<< std::endl;
return NULL;
}
IDirect3D9* r = new IDirect3D9(SDKVersion);
if (r == NULL)
std::cout << "\033[0;31m"
<< "Creating D3D enumeration object failed; out of memory. Direct3DCreate fails and returns NULL.\033[0;0m" << std::endl;
return r;
}
IDirect3DDevice9::IDirect3DDevice9(/*IDirect3D9* d3d, D3DPRESENT_PARAMETERS* pp*/) {
#ifdef DEBUG
std::cout << "libd3d9.so: IDirect3DDevice9::IDirect3DDevice9()" << std::endl;
#endif
refCount = 1;
}
ULONG IDirect3DDevice9::AddRef() {
DXGASSERT(((ULONG_PTR)(&refCount) & 3) == 0);
InterlockedIncrement((LONG *)&refCount);
return refCount;
}
ULONG IDirect3DDevice9::Release() {
InterlockedDecrement((LONG *)&refCount);
return refCount;
}
HRESULT IDirect3DDevice9::QueryInterface(REFIID riid, void** ppvObj) {
*ppvObj = this;
IDirect3DDevice9::AddRef();
return 0;
}
HRESULT IDirect3DDevice9::Clear(DWORD Count, const void/*D3DRECT*/ *pRects, DWORD Flags, int/*D3DCOLOR*/ Color, float Z, DWORD Stencil) {
#ifdef DEBUG
std::cout << "libd3d9.so: IDirect3DDevice9::Clear()" << std::endl;
#endif
//intel code
return 0;
}