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