wined3d: Remove references to the (no longer used) X11 lock.
[wine/multimedia.git] / dlls / wined3d / wined3d_main.c
blob2247e9c17ce737a8bce4b26a701b0446366a3976
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 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. */
89 /* Do not call while under the GL lock. */
90 struct wined3d * CDECL wined3d_create(UINT version, DWORD flags)
92 struct wined3d *object;
93 HRESULT hr;
95 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
96 if (!object)
98 ERR("Failed to allocate wined3d object memory.\n");
99 return NULL;
102 hr = wined3d_init(object, version, flags);
103 if (FAILED(hr))
105 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
106 HeapFree(GetProcessHeap(), 0, object);
107 return NULL;
110 TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
112 return object;
115 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
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;
122 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
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;
131 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
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;
141 wined3d_context_tls_idx = TlsAlloc();
142 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
144 DWORD err = GetLastError();
145 ERR("Failed to allocate context TLS index, err %#x.\n", err);
146 return FALSE;
148 context_set_tls_idx(wined3d_context_tls_idx);
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;
164 if (!RegisterClassA(&wc))
166 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
167 if (!TlsFree(wined3d_context_tls_idx))
169 DWORD err = GetLastError();
170 ERR("Failed to free context TLS index, err %#x.\n", err);
172 return FALSE;
175 DisableThreadLibraryCalls(hInstDLL);
177 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
178 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
180 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
181 if (len && len < MAX_PATH)
183 HKEY tmpkey;
184 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
185 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
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 );
197 if (hkey || appkey)
199 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
201 if (!strcmp(buffer,"none"))
203 TRACE("Disable vertex shaders\n");
204 wined3d_settings.vs_mode = VS_NONE;
207 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
209 if (!strcmp(buffer,"enabled"))
211 TRACE("Allow pixel shaders\n");
212 wined3d_settings.ps_mode = PS_HW;
214 if (!strcmp(buffer,"disabled"))
216 TRACE("Disable pixel shaders\n");
217 wined3d_settings.ps_mode = PS_NONE;
220 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
222 if (!strcmp(buffer,"disabled"))
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;
229 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
231 if (!strcmp(buffer,"backbuffer"))
233 TRACE("Using the backbuffer for offscreen rendering\n");
234 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
236 else if (!strcmp(buffer,"fbo"))
238 TRACE("Using FBOs for offscreen rendering\n");
239 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
242 if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
244 if (!strcmp(buffer,"readdraw"))
246 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
247 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
249 else if (!strcmp(buffer,"readtex"))
251 TRACE("Using glReadPixels for render target reading and textures for writing\n");
252 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
255 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
257 int pci_device_id = tmpvalue;
259 /* A pci device id is 16-bit */
260 if(pci_device_id > 0xffff)
262 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
264 else
266 TRACE("Using PCI Device ID %04x\n", pci_device_id);
267 wined3d_settings.pci_device_id = pci_device_id;
270 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
272 int pci_vendor_id = tmpvalue;
274 /* A pci device id is 16-bit */
275 if(pci_vendor_id > 0xffff)
277 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
279 else
281 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
282 wined3d_settings.pci_vendor_id = pci_vendor_id;
285 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
287 int TmpVideoMemorySize = atoi(buffer);
288 if(TmpVideoMemorySize > 0)
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);
295 else
296 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
298 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
300 size_t len = strlen(buffer) + 1;
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);
306 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
308 if (!strcmp(buffer, "disabled"))
310 TRACE("Multisampling disabled.\n");
311 wined3d_settings.allow_multisampling = FALSE;
314 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
315 && !strcmp(buffer,"enabled"))
317 TRACE("Enforcing strict draw ordering.\n");
318 wined3d_settings.strict_draw_ordering = TRUE;
320 if (!get_config_key(hkey, appkey, "AlwaysOffscreen", buffer, size)
321 && !strcmp(buffer,"disabled"))
323 TRACE("Not always rendering backbuffers offscreen.\n");
324 wined3d_settings.always_offscreen = FALSE;
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");
334 if (appkey) RegCloseKey( appkey );
335 if (hkey) RegCloseKey( hkey );
337 return TRUE;
340 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
342 DWORD wined3d_context_tls_idx = context_get_tls_idx();
343 unsigned int i;
345 if (!TlsFree(wined3d_context_tls_idx))
347 DWORD err = GetLastError();
348 ERR("Failed to free context TLS index, err %#x.\n", err);
351 for (i = 0; i < wndproc_table.count; ++i)
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]);
361 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
363 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
364 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
366 DeleteCriticalSection(&wined3d_wndproc_cs);
367 DeleteCriticalSection(&wined3d_cs);
368 return TRUE;
371 void WINAPI wined3d_mutex_lock(void)
373 EnterCriticalSection(&wined3d_cs);
376 void WINAPI wined3d_mutex_unlock(void)
378 LeaveCriticalSection(&wined3d_cs);
381 static void wined3d_wndproc_mutex_lock(void)
383 EnterCriticalSection(&wined3d_wndproc_cs);
386 static void wined3d_wndproc_mutex_unlock(void)
388 LeaveCriticalSection(&wined3d_wndproc_cs);
391 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
393 unsigned int i;
395 for (i = 0; i < wndproc_table.count; ++i)
397 if (wndproc_table.entries[i].window == window)
399 return &wndproc_table.entries[i];
403 return NULL;
406 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
408 struct wined3d_wndproc *entry;
409 struct wined3d_device *device;
410 BOOL unicode;
411 WNDPROC proc;
413 wined3d_wndproc_mutex_lock();
414 entry = wined3d_find_wndproc(window);
416 if (!entry)
418 wined3d_wndproc_mutex_unlock();
419 ERR("Window %p is not registered with wined3d.\n", window);
420 return DefWindowProcW(window, message, wparam, lparam);
423 device = entry->device;
424 unicode = entry->unicode;
425 proc = entry->proc;
426 wined3d_wndproc_mutex_unlock();
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);
435 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
437 struct wined3d_wndproc *entry;
439 wined3d_wndproc_mutex_lock();
441 if (wined3d_find_wndproc(window))
443 wined3d_wndproc_mutex_unlock();
444 WARN("Window %p is already registered with wined3d.\n", window);
445 return TRUE;
448 if (wndproc_table.size == wndproc_table.count)
450 unsigned int new_size = max(1, wndproc_table.size * 2);
451 struct wined3d_wndproc *new_entries;
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));
456 if (!new_entries)
458 wined3d_wndproc_mutex_unlock();
459 ERR("Failed to grow table.\n");
460 return FALSE;
463 wndproc_table.entries = new_entries;
464 wndproc_table.size = new_size;
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;
479 wined3d_wndproc_mutex_unlock();
481 return TRUE;
484 void wined3d_unregister_window(HWND window)
486 struct wined3d_wndproc *entry, *last;
487 LONG_PTR proc;
489 wined3d_wndproc_mutex_lock();
491 if (!(entry = wined3d_find_wndproc(window)))
493 wined3d_wndproc_mutex_unlock();
494 ERR("Window %p is not registered with wined3d.\n", window);
495 return;
498 if (entry->unicode)
500 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
501 if (proc != (LONG_PTR)wined3d_wndproc)
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;
510 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
512 else
514 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
515 if (proc != (LONG_PTR)wined3d_wndproc)
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;
524 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
527 last = &wndproc_table.entries[--wndproc_table.count];
528 if (entry != last) *entry = *last;
530 wined3d_wndproc_mutex_unlock();
533 /* At process attach */
534 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
536 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
538 switch (fdwReason)
540 case DLL_PROCESS_ATTACH:
541 return wined3d_dll_init(hInstDLL);
543 case DLL_PROCESS_DETACH:
544 return wined3d_dll_destroy(hInstDLL);
546 case DLL_THREAD_DETACH:
548 if (!context_set_current(NULL))
550 ERR("Failed to clear current context.\n");
552 return TRUE;
555 default:
556 return TRUE;