* Slap *some* sense into our header inclusions.
[reactos.git] / reactos / dll / directx / wine / wined3d / wined3d_main.c
1 /*
2 * Direct3D wine internal interface main
3 *
4 * Copyright 2002-2003 The wine-d3d team
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Jason Edmeades
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include <config.h>
26 #include <wine/port.h>
27
28 //#include "initguid.h"
29 #include "wined3d_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag);
33
34 struct wined3d_wndproc
35 {
36 HWND window;
37 BOOL unicode;
38 WNDPROC proc;
39 struct wined3d_device *device;
40 };
41
42 struct wined3d_wndproc_table
43 {
44 struct wined3d_wndproc *entries;
45 unsigned int count;
46 unsigned int size;
47 };
48
49 static struct wined3d_wndproc_table wndproc_table;
50
51 static CRITICAL_SECTION wined3d_cs;
52 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
53 {
54 0, 0, &wined3d_cs,
55 {&wined3d_cs_debug.ProcessLocksList,
56 &wined3d_cs_debug.ProcessLocksList},
57 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
58 };
59 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
60
61 static CRITICAL_SECTION wined3d_wndproc_cs;
62 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
63 {
64 0, 0, &wined3d_wndproc_cs,
65 {&wined3d_wndproc_cs_debug.ProcessLocksList,
66 &wined3d_wndproc_cs_debug.ProcessLocksList},
67 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
68 };
69 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
70
71 /* When updating default value here, make sure to update winecfg as well,
72 * where appropriate. */
73 struct wined3d_settings wined3d_settings =
74 {
75 VS_HW, /* Hardware by default */
76 PS_HW, /* Hardware by default */
77 TRUE, /* Use of GLSL enabled by default */
78 ORM_FBO, /* Use FBOs to do offscreen rendering */
79 RTL_READTEX, /* Default render target locking method */
80 PCI_VENDOR_NONE,/* PCI Vendor ID */
81 PCI_DEVICE_NONE,/* PCI Device ID */
82 0, /* The default of memory is set in init_driver_info */
83 NULL, /* No wine logo by default */
84 TRUE, /* Multisampling enabled by default. */
85 FALSE, /* No strict draw ordering. */
86 TRUE, /* Don't try to render onscreen by default. */
87 };
88
89 /* Do not call while under the GL lock. */
90 struct wined3d * CDECL wined3d_create(UINT version, DWORD flags)
91 {
92 struct wined3d *object;
93 HRESULT hr;
94
95 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
96 if (!object)
97 {
98 ERR("Failed to allocate wined3d object memory.\n");
99 return NULL;
100 }
101
102 hr = wined3d_init(object, version, flags);
103 if (FAILED(hr))
104 {
105 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
106 HeapFree(GetProcessHeap(), 0, object);
107 return NULL;
108 }
109
110 TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
111
112 return object;
113 }
114
115 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
116 {
117 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
118 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
119 return ERROR_FILE_NOT_FOUND;
120 }
121
122 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
123 {
124 DWORD type;
125 DWORD size = sizeof(DWORD);
126 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
127 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
128 return ERROR_FILE_NOT_FOUND;
129 }
130
131 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
132 {
133 DWORD wined3d_context_tls_idx;
134 char buffer[MAX_PATH+10];
135 DWORD size = sizeof(buffer);
136 HKEY hkey = 0;
137 HKEY appkey = 0;
138 DWORD len, tmpvalue;
139 WNDCLASSA wc;
140
141 wined3d_context_tls_idx = TlsAlloc();
142 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
143 {
144 DWORD err = GetLastError();
145 ERR("Failed to allocate context TLS index, err %#x.\n", err);
146 return FALSE;
147 }
148 context_set_tls_idx(wined3d_context_tls_idx);
149
150 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
151 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
152 * Various articles/posts about OpenGL problems on Windows recommend this. */
153 wc.style = CS_HREDRAW | CS_VREDRAW;
154 wc.lpfnWndProc = DefWindowProcA;
155 wc.cbClsExtra = 0;
156 wc.cbWndExtra = 0;
157 wc.hInstance = hInstDLL;
158 wc.hIcon = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
159 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
160 wc.hbrBackground = NULL;
161 wc.lpszMenuName = NULL;
162 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
163
164 if (!RegisterClassA(&wc))
165 {
166 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
167 if (!TlsFree(wined3d_context_tls_idx))
168 {
169 DWORD err = GetLastError();
170 ERR("Failed to free context TLS index, err %#x.\n", err);
171 }
172 return FALSE;
173 }
174
175 DisableThreadLibraryCalls(hInstDLL);
176
177 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
178 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
179
180 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
181 if (len && len < MAX_PATH)
182 {
183 HKEY tmpkey;
184 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
185 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
186 {
187 char *p, *appname = buffer;
188 if ((p = strrchr( appname, '/' ))) appname = p + 1;
189 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
190 strcat( appname, "\\Direct3D" );
191 TRACE("appname = [%s]\n", appname);
192 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
193 RegCloseKey( tmpkey );
194 }
195 }
196
197 if (hkey || appkey)
198 {
199 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
200 {
201 if (!strcmp(buffer,"none"))
202 {
203 TRACE("Disable vertex shaders\n");
204 wined3d_settings.vs_mode = VS_NONE;
205 }
206 }
207 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
208 {
209 if (!strcmp(buffer,"enabled"))
210 {
211 TRACE("Allow pixel shaders\n");
212 wined3d_settings.ps_mode = PS_HW;
213 }
214 if (!strcmp(buffer,"disabled"))
215 {
216 TRACE("Disable pixel shaders\n");
217 wined3d_settings.ps_mode = PS_NONE;
218 }
219 }
220 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
221 {
222 if (!strcmp(buffer,"disabled"))
223 {
224 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
225 TRACE("Use of GL Shading Language disabled\n");
226 wined3d_settings.glslRequested = FALSE;
227 }
228 }
229 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
230 {
231 if (!strcmp(buffer,"backbuffer"))
232 {
233 TRACE("Using the backbuffer for offscreen rendering\n");
234 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
235 }
236 else if (!strcmp(buffer,"fbo"))
237 {
238 TRACE("Using FBOs for offscreen rendering\n");
239 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
240 }
241 }
242 if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
243 {
244 if (!strcmp(buffer,"readdraw"))
245 {
246 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
247 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
248 }
249 else if (!strcmp(buffer,"readtex"))
250 {
251 TRACE("Using glReadPixels for render target reading and textures for writing\n");
252 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
253 }
254 }
255 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
256 {
257 int pci_device_id = tmpvalue;
258
259 /* A pci device id is 16-bit */
260 if(pci_device_id > 0xffff)
261 {
262 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
263 }
264 else
265 {
266 TRACE("Using PCI Device ID %04x\n", pci_device_id);
267 wined3d_settings.pci_device_id = pci_device_id;
268 }
269 }
270 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
271 {
272 int pci_vendor_id = tmpvalue;
273
274 /* A pci device id is 16-bit */
275 if(pci_vendor_id > 0xffff)
276 {
277 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
278 }
279 else
280 {
281 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
282 wined3d_settings.pci_vendor_id = pci_vendor_id;
283 }
284 }
285 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
286 {
287 int TmpVideoMemorySize = atoi(buffer);
288 if(TmpVideoMemorySize > 0)
289 {
290 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
291 TRACE("Use %iMB = %d byte for emulated_textureram\n",
292 TmpVideoMemorySize,
293 wined3d_settings.emulated_textureram);
294 }
295 else
296 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
297 }
298 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
299 {
300 size_t len = strlen(buffer) + 1;
301
302 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
303 if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
304 else memcpy(wined3d_settings.logo, buffer, len);
305 }
306 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
307 {
308 if (!strcmp(buffer, "disabled"))
309 {
310 TRACE("Multisampling disabled.\n");
311 wined3d_settings.allow_multisampling = FALSE;
312 }
313 }
314 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
315 && !strcmp(buffer,"enabled"))
316 {
317 TRACE("Enforcing strict draw ordering.\n");
318 wined3d_settings.strict_draw_ordering = TRUE;
319 }
320 if (!get_config_key(hkey, appkey, "AlwaysOffscreen", buffer, size)
321 && !strcmp(buffer,"disabled"))
322 {
323 TRACE("Not always rendering backbuffers offscreen.\n");
324 wined3d_settings.always_offscreen = FALSE;
325 }
326 }
327 if (wined3d_settings.vs_mode == VS_HW)
328 TRACE("Allow HW vertex shaders\n");
329 if (wined3d_settings.ps_mode == PS_NONE)
330 TRACE("Disable pixel shaders\n");
331 if (wined3d_settings.glslRequested)
332 TRACE("If supported by your system, GL Shading Language will be used\n");
333
334 if (appkey) RegCloseKey( appkey );
335 if (hkey) RegCloseKey( hkey );
336
337 return TRUE;
338 }
339
340 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
341 {
342 DWORD wined3d_context_tls_idx = context_get_tls_idx();
343 unsigned int i;
344
345 if (!TlsFree(wined3d_context_tls_idx))
346 {
347 DWORD err = GetLastError();
348 ERR("Failed to free context TLS index, err %#x.\n", err);
349 }
350
351 for (i = 0; i < wndproc_table.count; ++i)
352 {
353 /* Trying to unregister these would be futile. These entries can only
354 * exist if either we skipped them in wined3d_unregister_window() due
355 * to the application replacing the wndproc after the entry was
356 * registered, or if the application still has an active wined3d
357 * device. In the latter case the application has bigger problems than
358 * these entries. */
359 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
360 }
361 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
362
363 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
364 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
365
366 DeleteCriticalSection(&wined3d_wndproc_cs);
367 DeleteCriticalSection(&wined3d_cs);
368 return TRUE;
369 }
370
371 void WINAPI wined3d_mutex_lock(void)
372 {
373 EnterCriticalSection(&wined3d_cs);
374 }
375
376 void WINAPI wined3d_mutex_unlock(void)
377 {
378 LeaveCriticalSection(&wined3d_cs);
379 }
380
381 static void wined3d_wndproc_mutex_lock(void)
382 {
383 EnterCriticalSection(&wined3d_wndproc_cs);
384 }
385
386 static void wined3d_wndproc_mutex_unlock(void)
387 {
388 LeaveCriticalSection(&wined3d_wndproc_cs);
389 }
390
391 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
392 {
393 unsigned int i;
394
395 for (i = 0; i < wndproc_table.count; ++i)
396 {
397 if (wndproc_table.entries[i].window == window)
398 {
399 return &wndproc_table.entries[i];
400 }
401 }
402
403 return NULL;
404 }
405
406 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
407 {
408 struct wined3d_wndproc *entry;
409 struct wined3d_device *device;
410 BOOL unicode;
411 WNDPROC proc;
412
413 wined3d_wndproc_mutex_lock();
414 entry = wined3d_find_wndproc(window);
415
416 if (!entry)
417 {
418 wined3d_wndproc_mutex_unlock();
419 ERR("Window %p is not registered with wined3d.\n", window);
420 return DefWindowProcW(window, message, wparam, lparam);
421 }
422
423 device = entry->device;
424 unicode = entry->unicode;
425 proc = entry->proc;
426 wined3d_wndproc_mutex_unlock();
427
428 if (device)
429 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
430 if (unicode)
431 return CallWindowProcW(proc, window, message, wparam, lparam);
432 return CallWindowProcA(proc, window, message, wparam, lparam);
433 }
434
435 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
436 {
437 struct wined3d_wndproc *entry;
438
439 wined3d_wndproc_mutex_lock();
440
441 if (wined3d_find_wndproc(window))
442 {
443 wined3d_wndproc_mutex_unlock();
444 WARN("Window %p is already registered with wined3d.\n", window);
445 return TRUE;
446 }
447
448 if (wndproc_table.size == wndproc_table.count)
449 {
450 unsigned int new_size = max(1, wndproc_table.size * 2);
451 struct wined3d_wndproc *new_entries;
452
453 if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
454 else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
455
456 if (!new_entries)
457 {
458 wined3d_wndproc_mutex_unlock();
459 ERR("Failed to grow table.\n");
460 return FALSE;
461 }
462
463 wndproc_table.entries = new_entries;
464 wndproc_table.size = new_size;
465 }
466
467 entry = &wndproc_table.entries[wndproc_table.count++];
468 entry->window = window;
469 entry->unicode = IsWindowUnicode(window);
470 /* Set a window proc that matches the window. Some applications (e.g. NoX)
471 * replace the window proc after we've set ours, and expect to be able to
472 * call the previous one (ours) directly, without using CallWindowProc(). */
473 if (entry->unicode)
474 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
475 else
476 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
477 entry->device = device;
478
479 wined3d_wndproc_mutex_unlock();
480
481 return TRUE;
482 }
483
484 void wined3d_unregister_window(HWND window)
485 {
486 struct wined3d_wndproc *entry, *last;
487 LONG_PTR proc;
488
489 wined3d_wndproc_mutex_lock();
490
491 if (!(entry = wined3d_find_wndproc(window)))
492 {
493 wined3d_wndproc_mutex_unlock();
494 ERR("Window %p is not registered with wined3d.\n", window);
495 return;
496 }
497
498 if (entry->unicode)
499 {
500 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
501 if (proc != (LONG_PTR)wined3d_wndproc)
502 {
503 entry->device = NULL;
504 wined3d_wndproc_mutex_unlock();
505 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
506 window, proc, wined3d_wndproc);
507 return;
508 }
509
510 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
511 }
512 else
513 {
514 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
515 if (proc != (LONG_PTR)wined3d_wndproc)
516 {
517 entry->device = NULL;
518 wined3d_wndproc_mutex_unlock();
519 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
520 window, proc, wined3d_wndproc);
521 return;
522 }
523
524 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
525 }
526
527 last = &wndproc_table.entries[--wndproc_table.count];
528 if (entry != last) *entry = *last;
529
530 wined3d_wndproc_mutex_unlock();
531 }
532
533 /* At process attach */
534 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
535 {
536 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
537
538 switch (fdwReason)
539 {
540 case DLL_PROCESS_ATTACH:
541 return wined3d_dll_init(hInstDLL);
542
543 case DLL_PROCESS_DETACH:
544 return wined3d_dll_destroy(hInstDLL);
545
546 case DLL_THREAD_DETACH:
547 {
548 if (!context_set_current(NULL))
549 {
550 ERR("Failed to clear current context.\n");
551 }
552 return TRUE;
553 }
554
555 default:
556 return TRUE;
557 }
558 }