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