-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
326 lines (265 loc) · 8.18 KB
/
main.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
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
#define INITGUID
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#include <d3d9.h>
#include <vector>
#include <sstream>
#include <cstdio>
#include "glew.h"
#include "wglew.h"
#include "trace.hpp"
#include "d3dgl.hpp"
#include "private_iids.hpp"
#define DECLSPEC_HOTPATCH __attribute__((__ms_hook_prologue__))
#define DECLSPEC_EXPORT __declspec (dllexport)
eLogLevel LogLevel = FIXME_;
FILE *LogFile = stderr;
eLogLevel GLDebugLevel = NONE_;
static CRITICAL_SECTION LogLock;
void log_printf(FILE *file, const char *fmt, ...)
{
va_list ap;
EnterCriticalSection(&LogLock);
va_start(ap, fmt);
vfprintf(file, fmt, ap);
va_end(ap);
fflush(file);
LeaveCriticalSection(&LogLock);
}
static const wchar_t WndClassName[] = L"D3DGLWndClass";
bool CreateFakeWindow(HINSTANCE hInstance, HWND &hWnd, HDC &dc)
{
dc = nullptr;
hWnd = CreateWindowExW(0, WndClassName, L"D3DGL Fake Window", WS_OVERLAPPEDWINDOW,
0, 0, 640, 480, nullptr, nullptr, hInstance, nullptr);
if(!hWnd)
{
ERR("Failed to create a window\n");
return false;
}
dc = GetDC(hWnd);
PIXELFORMATDESCRIPTOR pfd = PIXELFORMATDESCRIPTOR{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
32, //Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
0, //Number of bits for the depthbuffer
0, //Number of bits for the stencilbuffer
0, //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pfid = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pfid, &pfd);
return true;
}
extern "C" {
static int D3DPERF_event_level = 0;
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD reason, void */*lpReserved*/)
{
const char *str;
switch(reason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hModule);
InitializeCriticalSection(&LogLock);
str = getenv("D3DGL_LOGFILE");
if(str && str[0] != '\0')
{
std::stringstream sstr;
sstr<< str<<"-"<<getpid()<<".log";
FILE *logfile = fopen(sstr.str().c_str(), "wb");
if(logfile) LogFile = logfile;
else ERR("Failed to open %s for writing\n", sstr.str().c_str());
}
str = getenv("D3DGL_LOGLEVEL");
if(str && str[0] != '\0')
{
char *end = nullptr;
unsigned long val = strtoul(str, &end, 10);
if(end && *end == '\0')
LogLevel = eLogLevel(std::min<unsigned long>(val, TRACE_));
else
ERR("Invalid log level: %s\n", str);
}
str = getenv("D3DGL_DEBUGGL");
if(str && str[0] != '\0')
{
char *end = nullptr;
unsigned long val = strtoul(str, &end, 10);
if(end && *end == '\0')
GLDebugLevel = eLogLevel(std::min<unsigned long>(val, TRACE_));
else
ERR("Invalid log level: %s\n", str);
}
TRACE("DLL_PROCESS_ATTACH\n");
break;
case DLL_PROCESS_DETACH:
TRACE("DLL_PROCESS_DETACH\n");
DeleteCriticalSection(&LogLock);
break;
}
return TRUE;
}
static bool init_d3dgl(void)
{
static bool inited = false;
if(inited) return true;
HINSTANCE hInstance = GetModuleHandleW(nullptr);
WNDCLASSW wc;
memset(&wc, 0, sizeof(wc));
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = WndClassName;
wc.style = CS_OWNDC;
if(!RegisterClassW(&wc))
{
ERR("Failed to register fake GL window class\n");
return false;
}
HWND hWnd; HDC dc;
if(!CreateFakeWindow(hInstance, hWnd, dc))
return false;
HGLRC glrc = wglCreateContext(dc);
if(!glrc)
{
ERR("Failed to create WGL context\n");
ReleaseDC(hWnd, dc);
DestroyWindow(hWnd);
return false;
}
wglMakeCurrent(dc, glrc);
GLenum err = glewInit();
wglMakeCurrent(dc, nullptr);
wglDeleteContext(glrc);
if(err == GLEW_OK)
{
std::vector<std::array<int,2>> glattrs;
glattrs.reserve(2);
glattrs.push_back({WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB});
glattrs.push_back({0, 0});
glrc = wglCreateContextAttribsARB(dc, nullptr, &glattrs[0][0]);
if(!glrc)
{
ERR("Failed to recreate WGL context\n");
ReleaseDC(hWnd, dc);
DestroyWindow(hWnd);
return false;
}
wglMakeCurrent(dc, glrc);
/* Reinit GLEW with a core context. */
err = glewInit();
wglMakeCurrent(dc, nullptr);
wglDeleteContext(glrc);
}
ReleaseDC(hWnd, dc);
DestroyWindow(hWnd);
if(err != GLEW_OK)
{
/* Problem: glewInit failed, something is seriously wrong. */
ERR("GLEW error: %s\n", glewGetErrorString(err));
return false;
}
TRACE("Initialized GLEW %s\n", glewGetString(GLEW_VERSION));
inited = true;
return true;
}
DECLSPEC_EXPORT void WINAPI DebugSetMute(void)
{
FIXME("stub\n");
}
DECLSPEC_EXPORT IDirect3D9* WINAPI DECLSPEC_HOTPATCH Direct3DCreate9(UINT sdk_version)
{
TRACE("(%u)\n", sdk_version);
init_d3dgl();
Direct3DGL *d3d = new Direct3DGL();
if(!d3d->init())
{
delete d3d;
return nullptr;
}
d3d->AddRef();
return d3d;
}
DECLSPEC_EXPORT HRESULT WINAPI DECLSPEC_HOTPATCH Direct3DCreate9Ex(UINT sdk_version, IDirect3D9Ex **d3d9ex)
{
FIXME("(%u, %p)\n", sdk_version, d3d9ex);
init_d3dgl();
*d3d9ex = nullptr;
return D3DERR_NOTAVAILABLE;
}
/*******************************************************************
* Direct3DShaderValidatorCreate9 (D3D9.@)
*
* No documentation available for this function.
* SDK only says it is internal and shouldn't be used.
*/
DECLSPEC_EXPORT void* WINAPI Direct3DShaderValidatorCreate9(void)
{
static int once;
if(!once++) FIXME("stub\n");
return nullptr;
}
/***********************************************************************
* D3DPERF_BeginEvent (D3D9.@)
*/
DECLSPEC_EXPORT int WINAPI D3DPERF_BeginEvent(D3DCOLOR color, const WCHAR *name)
{
TRACE("color 0x%08lx, name %ls\n", color, name);
return D3DPERF_event_level++;
}
/***********************************************************************
* D3DPERF_EndEvent (D3D9.@)
*/
DECLSPEC_EXPORT int WINAPI D3DPERF_EndEvent(void)
{
TRACE("(void) : stub\n");
return --D3DPERF_event_level;
}
/***********************************************************************
* D3DPERF_GetStatus (D3D9.@)
*/
DECLSPEC_EXPORT DWORD WINAPI D3DPERF_GetStatus(void)
{
FIXME("(void) : stub\n");
return 0;
}
/***********************************************************************
* D3DPERF_SetOptions (D3D9.@)
*/
DECLSPEC_EXPORT void WINAPI D3DPERF_SetOptions(DWORD options)
{
FIXME("(0x%lx) : stub\n", options);
}
/***********************************************************************
* D3DPERF_QueryRepeatFrame (D3D9.@)
*/
DECLSPEC_EXPORT BOOL WINAPI D3DPERF_QueryRepeatFrame(void)
{
FIXME("(void) : stub\n");
return FALSE;
}
/***********************************************************************
* D3DPERF_SetMarker (D3D9.@)
*/
DECLSPEC_EXPORT void WINAPI D3DPERF_SetMarker(D3DCOLOR color, const WCHAR *name)
{
FIXME("color 0x%08lx, name %ls stub!\n", color, name);
}
/***********************************************************************
* D3DPERF_SetRegion (D3D9.@)
*/
DECLSPEC_EXPORT void WINAPI D3DPERF_SetRegion(D3DCOLOR color, const WCHAR *name)
{
FIXME("color 0x%08lx, name %ls stub!\n", color, name);
}
} // extern "C"