msvcrt: _Gettnames() should respect user overrides.
[wine.git] / dlls / wined3d / wined3d_main.c
blob28a34a71b6f41dffb540a5ad1a90762ecbf1d127
1 /*
2 * Direct3D wine internal interface main
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include "initguid.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag);
34 struct wined3d_wndproc
36 HWND window;
37 BOOL unicode;
38 WNDPROC proc;
39 struct wined3d_device *device;
42 struct wined3d_wndproc_table
44 struct wined3d_wndproc *entries;
45 unsigned int count;
46 SIZE_T size;
49 static struct wined3d_wndproc_table wndproc_table;
51 static CRITICAL_SECTION wined3d_cs;
52 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
54 0, 0, &wined3d_cs,
55 {&wined3d_cs_debug.ProcessLocksList,
56 &wined3d_cs_debug.ProcessLocksList},
57 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
59 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
61 static CRITICAL_SECTION wined3d_wndproc_cs;
62 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
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")}
69 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
71 /* When updating default value here, make sure to update winecfg as well,
72 * where appropriate. */
73 struct wined3d_settings wined3d_settings =
75 FALSE, /* No multithreaded CS by default. */
76 FALSE, /* explicit_gl_version */
77 MAKEDWORD_VERSION(1, 0), /* Default to legacy OpenGL */
78 TRUE, /* Use of GLSL enabled by default */
79 ORM_FBO, /* Use FBOs to do offscreen rendering */
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 ~0u, /* Don't force a specific sample count by default. */
85 FALSE, /* No strict draw ordering. */
86 FALSE, /* Don't range check relative addressing indices in float constants. */
87 ~0U, /* No VS shader model limit by default. */
88 ~0U, /* No HS shader model limit by default. */
89 ~0U, /* No DS shader model limit by default. */
90 ~0U, /* No GS shader model limit by default. */
91 ~0U, /* No PS shader model limit by default. */
92 ~0u, /* No CS shader model limit by default. */
93 FALSE, /* 3D support enabled by default. */
96 struct wined3d * CDECL wined3d_create(DWORD flags)
98 struct wined3d *object;
99 HRESULT hr;
101 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
102 if (!object)
104 ERR("Failed to allocate wined3d object memory.\n");
105 return NULL;
108 if (wined3d_settings.no_3d)
109 flags |= WINED3D_NO3D;
111 hr = wined3d_init(object, flags);
112 if (FAILED(hr))
114 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
115 HeapFree(GetProcessHeap(), 0, object);
116 return NULL;
119 TRACE("Created wined3d object %p.\n", object);
121 return object;
124 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
126 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
127 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
128 return ERROR_FILE_NOT_FOUND;
131 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *value)
133 DWORD type, data, size;
135 size = sizeof(data);
136 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
137 size = sizeof(data);
138 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
140 return ERROR_FILE_NOT_FOUND;
142 success:
143 *value = data;
144 return 0;
147 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
149 DWORD wined3d_context_tls_idx;
150 char buffer[MAX_PATH+10];
151 DWORD size = sizeof(buffer);
152 HKEY hkey = 0;
153 HKEY appkey = 0;
154 DWORD len, tmpvalue;
155 WNDCLASSA wc;
157 wined3d_context_tls_idx = TlsAlloc();
158 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
160 DWORD err = GetLastError();
161 ERR("Failed to allocate context TLS index, err %#x.\n", err);
162 return FALSE;
164 context_set_tls_idx(wined3d_context_tls_idx);
166 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
167 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
168 * Various articles/posts about OpenGL problems on Windows recommend this. */
169 wc.style = CS_HREDRAW | CS_VREDRAW;
170 wc.lpfnWndProc = DefWindowProcA;
171 wc.cbClsExtra = 0;
172 wc.cbWndExtra = 0;
173 wc.hInstance = hInstDLL;
174 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
175 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
176 wc.hbrBackground = NULL;
177 wc.lpszMenuName = NULL;
178 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
180 if (!RegisterClassA(&wc))
182 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
183 if (!TlsFree(wined3d_context_tls_idx))
185 DWORD err = GetLastError();
186 ERR("Failed to free context TLS index, err %#x.\n", err);
188 return FALSE;
191 DisableThreadLibraryCalls(hInstDLL);
193 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
194 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
196 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
197 if (len && len < MAX_PATH)
199 HKEY tmpkey;
200 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
201 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
203 char *p, *appname = buffer;
204 if ((p = strrchr( appname, '/' ))) appname = p + 1;
205 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
206 strcat( appname, "\\Direct3D" );
207 TRACE("appname = [%s]\n", appname);
208 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
209 RegCloseKey( tmpkey );
213 if (hkey || appkey)
215 if (!get_config_key_dword(hkey, appkey, "csmt", &wined3d_settings.cs_multithreaded))
216 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
217 if (!get_config_key_dword(hkey, appkey, "MaxVersionGL", &tmpvalue))
219 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
220 tmpvalue >> 16, tmpvalue & 0xffff);
221 wined3d_settings.explicit_gl_version = TRUE;
222 wined3d_settings.max_gl_version = tmpvalue;
224 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
226 if (!strcmp(buffer,"disabled"))
228 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
229 TRACE("Use of GL Shading Language disabled\n");
230 wined3d_settings.glslRequested = FALSE;
233 if (!get_config_key(hkey, appkey, "OffscreenRenderingMode", buffer, size)
234 && !strcmp(buffer,"backbuffer"))
235 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
236 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
238 int pci_device_id = tmpvalue;
240 /* A pci device id is 16-bit */
241 if(pci_device_id > 0xffff)
243 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
245 else
247 TRACE("Using PCI Device ID %04x\n", pci_device_id);
248 wined3d_settings.pci_device_id = pci_device_id;
251 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
253 int pci_vendor_id = tmpvalue;
255 /* A pci device id is 16-bit */
256 if(pci_vendor_id > 0xffff)
258 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
260 else
262 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
263 wined3d_settings.pci_vendor_id = pci_vendor_id;
266 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
268 int TmpVideoMemorySize = atoi(buffer);
269 if(TmpVideoMemorySize > 0)
271 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
272 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
273 TmpVideoMemorySize,
274 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
276 else
277 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
279 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
281 size_t len = strlen(buffer) + 1;
283 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
284 if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
285 else memcpy(wined3d_settings.logo, buffer, len);
287 if (!get_config_key_dword(hkey, appkey, "SampleCount", &wined3d_settings.sample_count))
288 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
289 wined3d_settings.sample_count);
290 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
291 && !strcmp(buffer,"enabled"))
293 ERR_(winediag)("\"StrictDrawOrdering\" is deprecated, please use \"csmt\" instead.\n");
294 TRACE("Enforcing strict draw ordering.\n");
295 wined3d_settings.strict_draw_ordering = TRUE;
297 if (!get_config_key(hkey, appkey, "CheckFloatConstants", buffer, size)
298 && !strcmp(buffer, "enabled"))
300 TRACE("Checking relative addressing indices in float constants.\n");
301 wined3d_settings.check_float_constants = TRUE;
303 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
304 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
305 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
306 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
307 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
308 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
309 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
310 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
311 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
312 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
313 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
314 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
315 if (!get_config_key(hkey, appkey, "DirectDrawRenderer", buffer, size)
316 && !strcmp(buffer, "gdi"))
318 TRACE("Disabling 3D support.\n");
319 wined3d_settings.no_3d = TRUE;
323 if (appkey) RegCloseKey( appkey );
324 if (hkey) RegCloseKey( hkey );
326 return TRUE;
329 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
331 DWORD wined3d_context_tls_idx = context_get_tls_idx();
332 unsigned int i;
334 if (!TlsFree(wined3d_context_tls_idx))
336 DWORD err = GetLastError();
337 ERR("Failed to free context TLS index, err %#x.\n", err);
340 for (i = 0; i < wndproc_table.count; ++i)
342 /* Trying to unregister these would be futile. These entries can only
343 * exist if either we skipped them in wined3d_unregister_window() due
344 * to the application replacing the wndproc after the entry was
345 * registered, or if the application still has an active wined3d
346 * device. In the latter case the application has bigger problems than
347 * these entries. */
348 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
350 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
352 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
353 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
355 DeleteCriticalSection(&wined3d_wndproc_cs);
356 DeleteCriticalSection(&wined3d_cs);
357 return TRUE;
360 void WINAPI wined3d_mutex_lock(void)
362 EnterCriticalSection(&wined3d_cs);
365 void WINAPI wined3d_mutex_unlock(void)
367 LeaveCriticalSection(&wined3d_cs);
370 static void wined3d_wndproc_mutex_lock(void)
372 EnterCriticalSection(&wined3d_wndproc_cs);
375 static void wined3d_wndproc_mutex_unlock(void)
377 LeaveCriticalSection(&wined3d_wndproc_cs);
380 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
382 unsigned int i;
384 for (i = 0; i < wndproc_table.count; ++i)
386 if (wndproc_table.entries[i].window == window)
388 return &wndproc_table.entries[i];
392 return NULL;
395 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
397 struct wined3d_wndproc *entry;
398 struct wined3d_device *device;
399 BOOL unicode;
400 WNDPROC proc;
402 wined3d_wndproc_mutex_lock();
403 entry = wined3d_find_wndproc(window);
405 if (!entry)
407 wined3d_wndproc_mutex_unlock();
408 ERR("Window %p is not registered with wined3d.\n", window);
409 return DefWindowProcW(window, message, wparam, lparam);
412 device = entry->device;
413 unicode = entry->unicode;
414 proc = entry->proc;
415 wined3d_wndproc_mutex_unlock();
417 if (device)
418 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
419 if (unicode)
420 return CallWindowProcW(proc, window, message, wparam, lparam);
421 return CallWindowProcA(proc, window, message, wparam, lparam);
424 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
426 struct wined3d_wndproc *entry;
428 wined3d_wndproc_mutex_lock();
430 if (wined3d_find_wndproc(window))
432 wined3d_wndproc_mutex_unlock();
433 WARN("Window %p is already registered with wined3d.\n", window);
434 return TRUE;
437 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
438 wndproc_table.count + 1, sizeof(*entry)))
440 wined3d_wndproc_mutex_unlock();
441 ERR("Failed to grow table.\n");
442 return FALSE;
445 entry = &wndproc_table.entries[wndproc_table.count++];
446 entry->window = window;
447 entry->unicode = IsWindowUnicode(window);
448 /* Set a window proc that matches the window. Some applications (e.g. NoX)
449 * replace the window proc after we've set ours, and expect to be able to
450 * call the previous one (ours) directly, without using CallWindowProc(). */
451 if (entry->unicode)
452 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
453 else
454 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
455 entry->device = device;
457 wined3d_wndproc_mutex_unlock();
459 return TRUE;
462 void wined3d_unregister_window(HWND window)
464 struct wined3d_wndproc *entry, *last;
465 LONG_PTR proc;
467 wined3d_wndproc_mutex_lock();
469 if (!(entry = wined3d_find_wndproc(window)))
471 wined3d_wndproc_mutex_unlock();
472 ERR("Window %p is not registered with wined3d.\n", window);
473 return;
476 if (entry->unicode)
478 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
479 if (proc != (LONG_PTR)wined3d_wndproc)
481 entry->device = NULL;
482 wined3d_wndproc_mutex_unlock();
483 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
484 window, proc, wined3d_wndproc);
485 return;
488 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
490 else
492 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
493 if (proc != (LONG_PTR)wined3d_wndproc)
495 entry->device = NULL;
496 wined3d_wndproc_mutex_unlock();
497 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
498 window, proc, wined3d_wndproc);
499 return;
502 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
505 last = &wndproc_table.entries[--wndproc_table.count];
506 if (entry != last) *entry = *last;
508 wined3d_wndproc_mutex_unlock();
511 /* At process attach */
512 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
514 switch (reason)
516 case DLL_PROCESS_ATTACH:
517 return wined3d_dll_init(inst);
519 case DLL_PROCESS_DETACH:
520 if (!reserved)
521 return wined3d_dll_destroy(inst);
522 break;
524 case DLL_THREAD_DETACH:
525 if (!context_set_current(NULL))
527 ERR("Failed to clear current context.\n");
529 return TRUE;
531 return TRUE;