comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / wined3d / wined3d_main.c
blob9d1c11dd1a5b01df3c06ec37345d948978f81845
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 unsigned int 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 MAKEDWORD_VERSION(1, 0), /* Default to legacy OpenGL */
76 TRUE, /* Use of GLSL enabled by default */
77 ORM_FBO, /* Use FBOs to do offscreen rendering */
78 PCI_VENDOR_NONE,/* PCI Vendor ID */
79 PCI_DEVICE_NONE,/* PCI Device ID */
80 0, /* The default of memory is set in init_driver_info */
81 NULL, /* No wine logo by default */
82 TRUE, /* Multisampling enabled by default. */
83 ~0u, /* Don't force a specific sample count by default. */
84 FALSE, /* No strict draw ordering. */
85 TRUE, /* Don't try to render onscreen by default. */
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 FALSE, /* 3D support enabled by default. */
95 struct wined3d * CDECL wined3d_create(DWORD flags)
97 struct wined3d *object;
98 HRESULT hr;
100 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
101 if (!object)
103 ERR("Failed to allocate wined3d object memory.\n");
104 return NULL;
107 if (wined3d_settings.no_3d)
108 flags |= WINED3D_NO3D;
110 hr = wined3d_init(object, flags);
111 if (FAILED(hr))
113 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
114 HeapFree(GetProcessHeap(), 0, object);
115 return NULL;
118 TRACE("Created wined3d object %p.\n", object);
120 return object;
123 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
125 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
126 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
127 return ERROR_FILE_NOT_FOUND;
130 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
132 DWORD type;
133 DWORD size = sizeof(DWORD);
134 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
135 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
136 return ERROR_FILE_NOT_FOUND;
139 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
141 DWORD wined3d_context_tls_idx;
142 char buffer[MAX_PATH+10];
143 DWORD size = sizeof(buffer);
144 HKEY hkey = 0;
145 HKEY appkey = 0;
146 DWORD len, tmpvalue;
147 WNDCLASSA wc;
149 wined3d_context_tls_idx = TlsAlloc();
150 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
152 DWORD err = GetLastError();
153 ERR("Failed to allocate context TLS index, err %#x.\n", err);
154 return FALSE;
156 context_set_tls_idx(wined3d_context_tls_idx);
158 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
159 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
160 * Various articles/posts about OpenGL problems on Windows recommend this. */
161 wc.style = CS_HREDRAW | CS_VREDRAW;
162 wc.lpfnWndProc = DefWindowProcA;
163 wc.cbClsExtra = 0;
164 wc.cbWndExtra = 0;
165 wc.hInstance = hInstDLL;
166 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
167 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
168 wc.hbrBackground = NULL;
169 wc.lpszMenuName = NULL;
170 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
172 if (!RegisterClassA(&wc))
174 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
175 if (!TlsFree(wined3d_context_tls_idx))
177 DWORD err = GetLastError();
178 ERR("Failed to free context TLS index, err %#x.\n", err);
180 return FALSE;
183 DisableThreadLibraryCalls(hInstDLL);
185 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
186 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
188 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
189 if (len && len < MAX_PATH)
191 HKEY tmpkey;
192 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
193 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
195 char *p, *appname = buffer;
196 if ((p = strrchr( appname, '/' ))) appname = p + 1;
197 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
198 strcat( appname, "\\Direct3D" );
199 TRACE("appname = [%s]\n", appname);
200 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
201 RegCloseKey( tmpkey );
205 if (hkey || appkey)
207 if (!get_config_key_dword(hkey, appkey, "MaxVersionGL", &tmpvalue))
209 if (tmpvalue != wined3d_settings.max_gl_version)
211 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
212 tmpvalue >> 16, tmpvalue & 0xffff);
213 wined3d_settings.max_gl_version = tmpvalue;
216 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
218 if (!strcmp(buffer,"disabled"))
220 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
221 TRACE("Use of GL Shading Language disabled\n");
222 wined3d_settings.glslRequested = FALSE;
225 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
227 if (!strcmp(buffer,"backbuffer"))
229 TRACE("Using the backbuffer for offscreen rendering\n");
230 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
232 else if (!strcmp(buffer,"fbo"))
234 TRACE("Using FBOs for offscreen rendering\n");
235 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
238 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
240 int pci_device_id = tmpvalue;
242 /* A pci device id is 16-bit */
243 if(pci_device_id > 0xffff)
245 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
247 else
249 TRACE("Using PCI Device ID %04x\n", pci_device_id);
250 wined3d_settings.pci_device_id = pci_device_id;
253 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
255 int pci_vendor_id = tmpvalue;
257 /* A pci device id is 16-bit */
258 if(pci_vendor_id > 0xffff)
260 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
262 else
264 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
265 wined3d_settings.pci_vendor_id = pci_vendor_id;
268 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
270 int TmpVideoMemorySize = atoi(buffer);
271 if(TmpVideoMemorySize > 0)
273 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
274 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
275 TmpVideoMemorySize,
276 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
278 else
279 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
281 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
283 size_t len = strlen(buffer) + 1;
285 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
286 if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
287 else memcpy(wined3d_settings.logo, buffer, len);
289 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
291 if (!strcmp(buffer, "disabled"))
293 TRACE("Multisampling disabled.\n");
294 wined3d_settings.allow_multisampling = FALSE;
297 if (!get_config_key_dword(hkey, appkey, "SampleCount", &wined3d_settings.sample_count))
298 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
299 wined3d_settings.sample_count);
300 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
301 && !strcmp(buffer,"enabled"))
303 TRACE("Enforcing strict draw ordering.\n");
304 wined3d_settings.strict_draw_ordering = TRUE;
306 if (!get_config_key(hkey, appkey, "AlwaysOffscreen", buffer, size)
307 && !strcmp(buffer,"disabled"))
309 TRACE("Not always rendering backbuffers offscreen.\n");
310 wined3d_settings.always_offscreen = FALSE;
312 if (!get_config_key(hkey, appkey, "CheckFloatConstants", buffer, size)
313 && !strcmp(buffer, "enabled"))
315 TRACE("Checking relative addressing indices in float constants.\n");
316 wined3d_settings.check_float_constants = TRUE;
318 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
319 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
320 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
321 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
322 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
323 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
324 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
325 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
326 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
327 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
328 if (!get_config_key(hkey, appkey, "DirectDrawRenderer", buffer, size)
329 && !strcmp(buffer, "gdi"))
331 TRACE("Disabling 3D support.\n");
332 wined3d_settings.no_3d = TRUE;
336 if (appkey) RegCloseKey( appkey );
337 if (hkey) RegCloseKey( hkey );
339 return TRUE;
342 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
344 DWORD wined3d_context_tls_idx = context_get_tls_idx();
345 unsigned int i;
347 if (!TlsFree(wined3d_context_tls_idx))
349 DWORD err = GetLastError();
350 ERR("Failed to free context TLS index, err %#x.\n", err);
353 for (i = 0; i < wndproc_table.count; ++i)
355 /* Trying to unregister these would be futile. These entries can only
356 * exist if either we skipped them in wined3d_unregister_window() due
357 * to the application replacing the wndproc after the entry was
358 * registered, or if the application still has an active wined3d
359 * device. In the latter case the application has bigger problems than
360 * these entries. */
361 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
363 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
365 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
366 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
368 DeleteCriticalSection(&wined3d_wndproc_cs);
369 DeleteCriticalSection(&wined3d_cs);
370 return TRUE;
373 void WINAPI wined3d_mutex_lock(void)
375 EnterCriticalSection(&wined3d_cs);
378 void WINAPI wined3d_mutex_unlock(void)
380 LeaveCriticalSection(&wined3d_cs);
383 static void wined3d_wndproc_mutex_lock(void)
385 EnterCriticalSection(&wined3d_wndproc_cs);
388 static void wined3d_wndproc_mutex_unlock(void)
390 LeaveCriticalSection(&wined3d_wndproc_cs);
393 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
395 unsigned int i;
397 for (i = 0; i < wndproc_table.count; ++i)
399 if (wndproc_table.entries[i].window == window)
401 return &wndproc_table.entries[i];
405 return NULL;
408 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
410 struct wined3d_wndproc *entry;
411 struct wined3d_device *device;
412 BOOL unicode;
413 WNDPROC proc;
415 wined3d_wndproc_mutex_lock();
416 entry = wined3d_find_wndproc(window);
418 if (!entry)
420 wined3d_wndproc_mutex_unlock();
421 ERR("Window %p is not registered with wined3d.\n", window);
422 return DefWindowProcW(window, message, wparam, lparam);
425 device = entry->device;
426 unicode = entry->unicode;
427 proc = entry->proc;
428 wined3d_wndproc_mutex_unlock();
430 if (device)
431 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
432 if (unicode)
433 return CallWindowProcW(proc, window, message, wparam, lparam);
434 return CallWindowProcA(proc, window, message, wparam, lparam);
437 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
439 struct wined3d_wndproc *entry;
441 wined3d_wndproc_mutex_lock();
443 if (wined3d_find_wndproc(window))
445 wined3d_wndproc_mutex_unlock();
446 WARN("Window %p is already registered with wined3d.\n", window);
447 return TRUE;
450 if (wndproc_table.size == wndproc_table.count)
452 unsigned int new_size = max(1, wndproc_table.size * 2);
453 struct wined3d_wndproc *new_entries;
455 if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
456 else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
458 if (!new_entries)
460 wined3d_wndproc_mutex_unlock();
461 ERR("Failed to grow table.\n");
462 return FALSE;
465 wndproc_table.entries = new_entries;
466 wndproc_table.size = new_size;
469 entry = &wndproc_table.entries[wndproc_table.count++];
470 entry->window = window;
471 entry->unicode = IsWindowUnicode(window);
472 /* Set a window proc that matches the window. Some applications (e.g. NoX)
473 * replace the window proc after we've set ours, and expect to be able to
474 * call the previous one (ours) directly, without using CallWindowProc(). */
475 if (entry->unicode)
476 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
477 else
478 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
479 entry->device = device;
481 wined3d_wndproc_mutex_unlock();
483 return TRUE;
486 void wined3d_unregister_window(HWND window)
488 struct wined3d_wndproc *entry, *last;
489 LONG_PTR proc;
491 wined3d_wndproc_mutex_lock();
493 if (!(entry = wined3d_find_wndproc(window)))
495 wined3d_wndproc_mutex_unlock();
496 ERR("Window %p is not registered with wined3d.\n", window);
497 return;
500 if (entry->unicode)
502 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
503 if (proc != (LONG_PTR)wined3d_wndproc)
505 entry->device = NULL;
506 wined3d_wndproc_mutex_unlock();
507 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
508 window, proc, wined3d_wndproc);
509 return;
512 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
514 else
516 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
517 if (proc != (LONG_PTR)wined3d_wndproc)
519 entry->device = NULL;
520 wined3d_wndproc_mutex_unlock();
521 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
522 window, proc, wined3d_wndproc);
523 return;
526 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
529 last = &wndproc_table.entries[--wndproc_table.count];
530 if (entry != last) *entry = *last;
532 wined3d_wndproc_mutex_unlock();
535 /* At process attach */
536 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
538 switch (reason)
540 case DLL_PROCESS_ATTACH:
541 return wined3d_dll_init(inst);
543 case DLL_PROCESS_DETACH:
544 if (!reserved)
545 return wined3d_dll_destroy(inst);
546 break;
548 case DLL_THREAD_DETACH:
549 if (!context_set_current(NULL))
551 ERR("Failed to clear current context.\n");
553 return TRUE;
555 return TRUE;