wined3d: Introduce the "glsl-vkd3d" shader backend.
[wine.git] / dlls / wined3d / wined3d_main.c
blobcd20fe8b80d10e97804a5834fa3b0d8422b48764
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 #define VKD3D_NO_VULKAN_H
26 #define VKD3D_NO_WIN32_TYPES
27 #include "initguid.h"
28 #include "wined3d_private.h"
29 #include "wined3d_gl.h"
30 #include "d3d12.h"
31 #define VK_NO_PROTOTYPES
32 #include "wine/vulkan.h"
33 #include <vkd3d.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
36 WINE_DECLARE_DEBUG_CHANNEL(vkd3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_wndproc
41 struct wined3d *wined3d;
42 HWND window;
43 BOOL unicode;
44 BOOL filter;
45 WNDPROC proc;
46 struct wined3d_device *device;
47 uint32_t flags;
50 struct wined3d_wndproc_table
52 struct wined3d_wndproc *entries;
53 SIZE_T count;
54 SIZE_T size;
57 struct wined3d_window_hook
59 HHOOK hook;
60 DWORD thread_id;
61 unsigned int count;
64 struct wined3d_registered_swapchain_state
66 struct wined3d_swapchain_state *state;
67 DWORD thread_id;
70 struct wined3d_swapchain_state_table
72 struct wined3d_window_hook *hooks;
73 SIZE_T hooks_size;
74 SIZE_T hook_count;
76 struct wined3d_registered_swapchain_state *states;
77 SIZE_T states_size;
78 SIZE_T state_count;
81 static struct wined3d_wndproc_table wndproc_table;
82 static struct wined3d_swapchain_state_table swapchain_state_table;
84 static CRITICAL_SECTION wined3d_cs;
85 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
87 0, 0, &wined3d_cs,
88 {&wined3d_cs_debug.ProcessLocksList,
89 &wined3d_cs_debug.ProcessLocksList},
90 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
92 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
94 static CRITICAL_SECTION wined3d_wndproc_cs;
95 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
97 0, 0, &wined3d_wndproc_cs,
98 {&wined3d_wndproc_cs_debug.ProcessLocksList,
99 &wined3d_wndproc_cs_debug.ProcessLocksList},
100 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
102 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
104 CRITICAL_SECTION wined3d_command_cs;
105 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug =
107 0, 0, &wined3d_command_cs,
108 {&wined3d_command_cs_debug.ProcessLocksList,
109 &wined3d_command_cs_debug.ProcessLocksList},
110 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_command_cs")}
112 CRITICAL_SECTION wined3d_command_cs = {&wined3d_command_cs_debug, -1, 0, 0, 0, 0};
114 /* When updating default value here, make sure to update winecfg as well,
115 * where appropriate. */
116 struct wined3d_settings wined3d_settings =
118 .cs_multithreaded = WINED3D_CSMT_ENABLE,
119 .max_gl_version = MAKEDWORD_VERSION(4, 4),
120 .offscreen_rendering_mode = ORM_FBO,
121 .pci_vendor_id = PCI_VENDOR_NONE,
122 .pci_device_id = PCI_DEVICE_NONE,
123 .multisample_textures = TRUE,
124 .sample_count = ~0u,
125 .max_sm_vs = UINT_MAX,
126 .max_sm_ps = UINT_MAX,
127 .max_sm_ds = UINT_MAX,
128 .max_sm_hs = UINT_MAX,
129 .max_sm_gs = UINT_MAX,
130 .max_sm_cs = UINT_MAX,
131 .renderer = WINED3D_RENDERER_AUTO,
132 .shader_backend = WINED3D_SHADER_BACKEND_AUTO,
135 enum wined3d_renderer CDECL wined3d_get_renderer(void)
137 if (wined3d_settings.renderer == WINED3D_RENDERER_AUTO)
138 return WINED3D_RENDERER_OPENGL;
140 return wined3d_settings.renderer;
143 struct wined3d * CDECL wined3d_create(uint32_t flags)
145 struct wined3d *object;
146 HRESULT hr;
148 if (!(object = calloc(1, FIELD_OFFSET(struct wined3d, adapters[1]))))
150 ERR("Failed to allocate wined3d object memory.\n");
151 return NULL;
154 if (wined3d_settings.renderer == WINED3D_RENDERER_NO3D)
155 flags |= WINED3D_NO3D;
157 if (FAILED(hr = wined3d_init(object, flags)))
159 WARN("Failed to initialize wined3d object, hr %#lx.\n", hr);
160 free(object);
161 return NULL;
164 TRACE("Created wined3d object %p.\n", object);
166 return object;
169 static bool is_option_separator(char c)
171 return c == ',' || c == ';' || c == '\0';
174 static const char *config_list_get_value(const char *string, const char *key, size_t *len)
176 const char *p, *end;
177 char prev_char;
179 p = string;
180 while (p)
182 if ((p = strstr(p, key)))
184 prev_char = p > string ? p[-1] : 0;
185 p += strlen(key);
187 if (is_option_separator(prev_char) && *p == '=')
189 if ((end = strpbrk(p + 1, ",;")))
190 *len = end - (p + 1);
191 else
192 *len = strlen(p + 1);
193 return p + 1;
198 return NULL;
201 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *env, const char *name, char *buffer, DWORD size)
203 const char *env_value;
204 size_t env_len;
206 if ((env_value = config_list_get_value(env, name, &env_len)) && env_len < size)
208 memcpy(buffer, env_value, env_len);
209 buffer[env_len] = 0;
210 return 0;
212 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
213 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
214 return ERROR_FILE_NOT_FOUND;
217 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *env, const char *name, unsigned int *value)
219 DWORD type, data, size;
220 const char *env_value;
221 size_t env_len;
222 char *end;
224 if ((env_value = config_list_get_value(env, name, &env_len)))
226 *value = strtoul(env_value, &end, 0);
227 if (end != env_value)
228 return 0;
230 size = sizeof(data);
231 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
232 size = sizeof(data);
233 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
235 return ERROR_FILE_NOT_FOUND;
237 success:
238 *value = data;
239 return 0;
242 BOOL wined3d_get_app_name(char *app_name, unsigned int app_name_size)
244 char buffer[MAX_PATH];
245 unsigned int len;
246 char *p, *name;
248 len = GetModuleFileNameA(0, buffer, ARRAY_SIZE(buffer));
249 if (!(len && len < MAX_PATH))
250 return FALSE;
252 name = buffer;
253 if ((p = strrchr(name, '/' )))
254 name = p + 1;
255 if ((p = strrchr(name, '\\')))
256 name = p + 1;
258 len = strlen(name) + 1;
259 if (app_name_size < len)
260 return FALSE;
262 memcpy(app_name, name, len);
263 return TRUE;
266 static void vkd3d_log_callback(const char *fmt, va_list args)
268 char buffer[1024];
270 vsnprintf(buffer, sizeof(buffer), fmt, args);
271 __wine_dbg_output(buffer);
274 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
276 DWORD wined3d_context_tls_idx;
277 char buffer[MAX_PATH+10];
278 DWORD size = sizeof(buffer);
279 const char *env;
280 HKEY hkey = 0;
281 HKEY appkey = 0;
282 unsigned int tmpvalue;
283 WNDCLASSA wc;
285 wined3d_context_tls_idx = TlsAlloc();
286 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
288 unsigned int err = GetLastError();
289 ERR("Failed to allocate context TLS index, err %#x.\n", err);
290 return FALSE;
292 context_set_tls_idx(wined3d_context_tls_idx);
294 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
295 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
296 * Various articles/posts about OpenGL problems on Windows recommend this. */
297 wc.style = CS_HREDRAW | CS_VREDRAW;
298 wc.lpfnWndProc = DefWindowProcA;
299 wc.cbClsExtra = 0;
300 wc.cbWndExtra = 0;
301 wc.hInstance = hInstDLL;
302 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
303 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
304 wc.hbrBackground = NULL;
305 wc.lpszMenuName = NULL;
306 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
308 if (!RegisterClassA(&wc))
310 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
311 if (!TlsFree(wined3d_context_tls_idx))
313 unsigned int err = GetLastError();
314 ERR("Failed to free context TLS index, err %#x.\n", err);
316 return FALSE;
319 DisableThreadLibraryCalls(hInstDLL);
321 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
322 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
324 if (wined3d_get_app_name(buffer, ARRAY_SIZE(buffer)))
326 HKEY tmpkey;
327 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
328 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
330 strcat(buffer, "\\Direct3D");
331 TRACE("Application name %s.\n", buffer);
332 if (RegOpenKeyA(tmpkey, buffer, &appkey)) appkey = 0;
333 RegCloseKey(tmpkey);
337 /* Allow modifying settings using the WINE_D3D_CONFIG environment variable,
338 * which takes precedence over registry keys. An example is as follows:
340 * WINE_D3D_CONFIG=csmt=0x1,shader_backend=glsl
342 env = getenv("WINE_D3D_CONFIG");
344 if (hkey || appkey || env)
346 if (!get_config_key_dword(hkey, appkey, env, "csmt", &wined3d_settings.cs_multithreaded))
347 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
348 if (!get_config_key_dword(hkey, appkey, env, "MaxVersionGL", &tmpvalue))
350 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
351 tmpvalue >> 16, tmpvalue & 0xffff);
352 wined3d_settings.max_gl_version = tmpvalue;
354 if (!get_config_key(hkey, appkey, env, "shader_backend", buffer, size))
356 if (!stricmp(buffer, "glsl-vkd3d"))
358 ERR_(winediag)("Using the vkd3d-shader GLSL shader backend.\n");
359 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL_VKD3D;
361 if (!stricmp(buffer, "glsl"))
363 ERR_(winediag)("Using the GLSL shader backend.\n");
364 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL;
367 if (!get_config_key_dword(hkey, appkey, env, "VideoPciDeviceID", &tmpvalue))
369 int pci_device_id = tmpvalue;
371 /* A pci device id is 16-bit */
372 if(pci_device_id > 0xffff)
374 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
376 else
378 TRACE("Using PCI Device ID %04x\n", pci_device_id);
379 wined3d_settings.pci_device_id = pci_device_id;
382 if (!get_config_key_dword(hkey, appkey, env, "VideoPciVendorID", &tmpvalue))
384 int pci_vendor_id = tmpvalue;
386 /* A pci device id is 16-bit */
387 if(pci_vendor_id > 0xffff)
389 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
391 else
393 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
394 wined3d_settings.pci_vendor_id = pci_vendor_id;
397 if (!get_config_key(hkey, appkey, env, "VideoMemorySize", buffer, size))
399 int TmpVideoMemorySize = atoi(buffer);
400 if(TmpVideoMemorySize > 0)
402 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
403 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
404 TmpVideoMemorySize,
405 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
407 else
408 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
410 if (!get_config_key(hkey, appkey, env, "WineLogo", buffer, size))
412 size_t len = strlen(buffer) + 1;
414 if (!(wined3d_settings.logo = malloc(len)))
415 ERR("Failed to allocate logo path memory.\n");
416 else
417 memcpy(wined3d_settings.logo, buffer, len);
419 if (!get_config_key_dword(hkey, appkey, env, "MultisampleTextures", &wined3d_settings.multisample_textures))
420 ERR_(winediag)("Setting multisample textures to %#x.\n", wined3d_settings.multisample_textures);
421 if (!get_config_key_dword(hkey, appkey, env, "SampleCount", &wined3d_settings.sample_count))
422 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
423 wined3d_settings.sample_count);
424 if (!get_config_key(hkey, appkey, env, "CheckFloatConstants", buffer, size)
425 && !strcmp(buffer, "enabled"))
427 TRACE("Checking relative addressing indices in float constants.\n");
428 wined3d_settings.check_float_constants = TRUE;
430 if (!get_config_key_dword(hkey, appkey, env, "strict_shader_math", &wined3d_settings.strict_shader_math))
431 ERR_(winediag)("Setting strict shader math to %#x.\n", wined3d_settings.strict_shader_math);
432 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
433 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
434 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
435 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
436 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
437 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
438 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
439 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
440 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
441 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
442 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
443 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
444 if (!get_config_key(hkey, appkey, env, "renderer", buffer, size))
446 if (!strcmp(buffer, "vulkan"))
448 ERR_(winediag)("Using the Vulkan renderer.\n");
449 wined3d_settings.renderer = WINED3D_RENDERER_VULKAN;
451 else if (!strcmp(buffer, "gl"))
453 ERR_(winediag)("Using the OpenGL renderer.\n");
454 wined3d_settings.renderer = WINED3D_RENDERER_OPENGL;
456 else if (!strcmp(buffer, "gdi") || !strcmp(buffer, "no3d"))
458 ERR_(winediag)("Disabling 3D support.\n");
459 wined3d_settings.renderer = WINED3D_RENDERER_NO3D;
462 if (!get_config_key_dword(hkey, appkey, env, "cb_access_map_w", &tmpvalue) && tmpvalue)
464 TRACE("Forcing all constant buffers to be write-mappable.\n");
465 wined3d_settings.cb_access_map_w = TRUE;
469 if (appkey) RegCloseKey( appkey );
470 if (hkey) RegCloseKey( hkey );
472 if (!getenv( "VKD3D_DEBUG" ))
474 if (TRACE_ON(vkd3d)) putenv( "VKD3D_DEBUG=trace" );
475 else if (WARN_ON(vkd3d)) putenv( "VKD3D_DEBUG=warn" );
476 else if (FIXME_ON(vkd3d)) putenv( "VKD3D_DEBUG=fixme" );
477 else if (ERR_ON(vkd3d)) putenv( "VKD3D_DEBUG=err" );
478 else putenv( "VKD3D_DEBUG=none" );
480 if (!getenv( "VKD3D_SHADER_DEBUG" ))
482 if (TRACE_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=trace" );
483 else if (WARN_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=warn" );
484 else if (FIXME_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=fixme" );
485 else if (ERR_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=err" );
486 else putenv( "VKD3D_SHADER_DEBUG=none" );
489 vkd3d_set_log_callback(vkd3d_log_callback);
491 return TRUE;
494 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
496 DWORD wined3d_context_tls_idx = context_get_tls_idx();
497 unsigned int i;
499 if (!TlsFree(wined3d_context_tls_idx))
501 unsigned int err = GetLastError();
502 ERR("Failed to free context TLS index, err %#x.\n", err);
505 for (i = 0; i < wndproc_table.count; ++i)
507 /* Trying to unregister these would be futile. These entries can only
508 * exist if either we skipped them in wined3d_unregister_window() due
509 * to the application replacing the wndproc after the entry was
510 * registered, or if the application still has an active wined3d
511 * device. In the latter case the application has bigger problems than
512 * these entries. */
513 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
515 free(wndproc_table.entries);
517 free(swapchain_state_table.states);
518 for (i = 0; i < swapchain_state_table.hook_count; ++i)
520 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table.hooks[i]);
521 UnhookWindowsHookEx(swapchain_state_table.hooks[i].hook);
523 free(swapchain_state_table.hooks);
525 free(wined3d_settings.logo);
526 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
528 DeleteCriticalSection(&wined3d_command_cs);
530 DeleteCriticalSection(&wined3d_wndproc_cs);
531 DeleteCriticalSection(&wined3d_cs);
532 return TRUE;
535 void WINAPI wined3d_mutex_lock(void)
537 EnterCriticalSection(&wined3d_cs);
540 void WINAPI wined3d_mutex_unlock(void)
542 LeaveCriticalSection(&wined3d_cs);
545 static void wined3d_wndproc_mutex_lock(void)
547 EnterCriticalSection(&wined3d_wndproc_cs);
550 static void wined3d_wndproc_mutex_unlock(void)
552 LeaveCriticalSection(&wined3d_wndproc_cs);
555 static struct wined3d_output * wined3d_get_output_from_window(const struct wined3d *wined3d,
556 HWND hwnd)
558 unsigned int adapter_idx, output_idx;
559 struct wined3d_adapter *adapter;
560 MONITORINFOEXW monitor_info;
561 HMONITOR monitor;
563 TRACE("wined3d %p, hwnd %p.\n", wined3d, hwnd);
565 monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
566 monitor_info.cbSize = sizeof(monitor_info);
567 if (!GetMonitorInfoW(monitor, (MONITORINFO *)&monitor_info))
569 ERR("GetMonitorInfoW failed, error %#lx.\n", GetLastError());
570 return NULL;
573 for (adapter_idx = 0; adapter_idx < wined3d->adapter_count; ++adapter_idx)
575 adapter = wined3d->adapters[adapter_idx];
576 for (output_idx = 0; output_idx < adapter->output_count; ++output_idx)
578 if (!lstrcmpiW(adapter->outputs[output_idx].device_name, monitor_info.szDevice))
579 return &adapter->outputs[output_idx];
583 return NULL;
586 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window, struct wined3d *wined3d)
588 unsigned int i;
590 for (i = 0; i < wndproc_table.count; ++i)
592 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
594 if (entry->window == window && entry->wined3d == wined3d)
595 return entry;
598 return NULL;
601 BOOL wined3d_filter_messages(HWND window, BOOL filter)
603 struct wined3d_wndproc *entry;
604 BOOL ret;
606 wined3d_wndproc_mutex_lock();
608 if (!(entry = wined3d_find_wndproc(window, NULL)))
610 wined3d_wndproc_mutex_unlock();
611 return FALSE;
614 ret = entry->filter;
615 entry->filter = filter;
617 wined3d_wndproc_mutex_unlock();
619 return ret;
622 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
624 struct wined3d_wndproc *entry;
625 struct wined3d_device *device;
626 BOOL unicode, filter;
627 WNDPROC proc;
629 wined3d_wndproc_mutex_lock();
631 if (!(entry = wined3d_find_wndproc(window, NULL)))
633 wined3d_wndproc_mutex_unlock();
634 ERR("Window %p is not registered with wined3d.\n", window);
635 return DefWindowProcW(window, message, wparam, lparam);
638 device = entry->device;
639 unicode = entry->unicode;
640 filter = entry->filter;
641 proc = entry->proc;
642 wined3d_wndproc_mutex_unlock();
644 if (device)
646 if (filter && message != WM_DISPLAYCHANGE)
648 TRACE("Filtering message: window %p, message %#x, wparam %#Ix, lparam %#Ix.\n",
649 window, message, wparam, lparam);
651 if (unicode)
652 return DefWindowProcW(window, message, wparam, lparam);
653 return DefWindowProcA(window, message, wparam, lparam);
656 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
658 if (unicode)
659 return CallWindowProcW(proc, window, message, wparam, lparam);
660 return CallWindowProcA(proc, window, message, wparam, lparam);
663 static LRESULT CALLBACK wined3d_hook_proc(int code, WPARAM wparam, LPARAM lparam)
665 struct wined3d_swapchain_desc swapchain_desc;
666 struct wined3d_swapchain_state *state;
667 struct wined3d_wndproc *entry;
668 struct wined3d_output *output;
669 MSG *msg = (MSG *)lparam;
670 unsigned int i;
672 /* Handle Alt+Enter. */
673 if (code == HC_ACTION && msg->message == WM_SYSKEYDOWN
674 && msg->wParam == VK_RETURN && (msg->lParam & (KF_ALTDOWN << 16)))
676 wined3d_wndproc_mutex_lock();
678 for (i = 0; i < swapchain_state_table.state_count; ++i)
680 state = swapchain_state_table.states[i].state;
682 if (state->device_window != msg->hwnd)
683 continue;
685 if ((entry = wined3d_find_wndproc(msg->hwnd, state->wined3d))
686 && (entry->flags & (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
687 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER)))
688 continue;
690 swapchain_desc = state->desc;
691 swapchain_desc.windowed = !swapchain_desc.windowed;
692 if (!(output = wined3d_get_output_from_window(state->wined3d, state->device_window)))
694 ERR("Failed to get output from window %p.\n", state->device_window);
695 break;
697 swapchain_desc.output = output;
698 wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
700 wined3d_wndproc_mutex_unlock();
702 return 1;
705 wined3d_wndproc_mutex_unlock();
708 return CallNextHookEx(0, code, wparam, lparam);
711 BOOL CDECL wined3d_register_window(struct wined3d *wined3d, HWND window,
712 struct wined3d_device *device, unsigned int flags)
714 struct wined3d_wndproc *entry;
716 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d, window, device, flags);
718 wined3d_wndproc_mutex_lock();
720 if ((entry = wined3d_find_wndproc(window, wined3d)))
722 if (!wined3d)
723 WARN("Window %p is already registered with wined3d.\n", window);
724 entry->flags = flags;
725 wined3d_wndproc_mutex_unlock();
726 return TRUE;
729 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
730 wndproc_table.count + 1, sizeof(*entry)))
732 wined3d_wndproc_mutex_unlock();
733 ERR("Failed to grow table.\n");
734 return FALSE;
737 entry = &wndproc_table.entries[wndproc_table.count++];
738 entry->window = window;
739 entry->unicode = IsWindowUnicode(window);
740 if (!wined3d)
742 /* Set a window proc that matches the window. Some applications (e.g.
743 * NoX) replace the window proc after we've set ours, and expect to be
744 * able to call the previous one (ours) directly, without using
745 * CallWindowProc(). */
746 if (entry->unicode)
747 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
748 else
749 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
751 else
753 entry->proc = NULL;
755 entry->device = device;
756 entry->wined3d = wined3d;
757 entry->flags = flags;
759 wined3d_wndproc_mutex_unlock();
761 return TRUE;
764 static BOOL restore_wndproc(struct wined3d_wndproc *entry)
766 LONG_PTR proc;
768 if (entry->unicode)
770 proc = GetWindowLongPtrW(entry->window, GWLP_WNDPROC);
771 if (proc != (LONG_PTR)wined3d_wndproc)
772 return FALSE;
773 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
775 else
777 proc = GetWindowLongPtrA(entry->window, GWLP_WNDPROC);
778 if (proc != (LONG_PTR)wined3d_wndproc)
779 return FALSE;
780 SetWindowLongPtrA(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
783 return TRUE;
786 void wined3d_unregister_window(HWND window)
788 struct wined3d_wndproc *entry, *last;
790 wined3d_wndproc_mutex_lock();
792 if (!(entry = wined3d_find_wndproc(window, NULL)))
794 wined3d_wndproc_mutex_unlock();
795 ERR("Window %p is not registered with wined3d.\n", window);
796 return;
799 if (entry->proc && !restore_wndproc(entry))
801 entry->device = NULL;
802 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window);
803 wined3d_wndproc_mutex_unlock();
804 return;
807 last = &wndproc_table.entries[--wndproc_table.count];
808 if (entry != last) *entry = *last;
810 wined3d_wndproc_mutex_unlock();
813 void CDECL wined3d_unregister_windows(struct wined3d *wined3d)
815 struct wined3d_wndproc *entry, *last;
816 unsigned int i = 0;
818 TRACE("wined3d %p.\n", wined3d);
820 wined3d_wndproc_mutex_lock();
822 while (i < wndproc_table.count)
824 entry = &wndproc_table.entries[i];
826 if (entry->wined3d != wined3d)
828 ++i;
829 continue;
832 if (entry->proc && !restore_wndproc(entry))
834 entry->device = NULL;
835 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
836 entry->window);
837 ++i;
838 continue;
841 last = &wndproc_table.entries[--wndproc_table.count];
842 if (entry != last)
843 *entry = *last;
844 else
845 ++i;
848 wined3d_wndproc_mutex_unlock();
851 static struct wined3d_window_hook *wined3d_find_hook(DWORD thread_id)
853 unsigned int i;
855 for (i = 0; i < swapchain_state_table.hook_count; ++i)
857 if (swapchain_state_table.hooks[i].thread_id == thread_id)
858 return &swapchain_state_table.hooks[i];
861 return NULL;
864 void wined3d_swapchain_state_register(struct wined3d_swapchain_state *state)
866 struct wined3d_registered_swapchain_state *state_entry;
867 struct wined3d_window_hook *hook;
869 wined3d_wndproc_mutex_lock();
871 if (!wined3d_array_reserve((void **)&swapchain_state_table.states, &swapchain_state_table.states_size,
872 swapchain_state_table.state_count + 1, sizeof(*state_entry)))
874 wined3d_wndproc_mutex_unlock();
875 return;
878 state_entry = &swapchain_state_table.states[swapchain_state_table.state_count++];
879 state_entry->state = state;
880 state_entry->thread_id = GetWindowThreadProcessId(state->device_window, NULL);
882 if ((hook = wined3d_find_hook(state_entry->thread_id)))
884 ++hook->count;
885 wined3d_wndproc_mutex_unlock();
886 return;
889 if (!wined3d_array_reserve((void **)&swapchain_state_table.hooks, &swapchain_state_table.hooks_size,
890 swapchain_state_table.hook_count + 1, sizeof(*hook)))
892 --swapchain_state_table.state_count;
893 wined3d_wndproc_mutex_unlock();
894 return;
897 hook = &swapchain_state_table.hooks[swapchain_state_table.hook_count++];
898 hook->thread_id = state_entry->thread_id;
899 hook->hook = SetWindowsHookExW(WH_GETMESSAGE, wined3d_hook_proc, 0, hook->thread_id);
900 hook->count = 1;
902 wined3d_wndproc_mutex_unlock();
905 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state *state)
907 struct wined3d_registered_swapchain_state *state_entry, *last_state_entry;
908 struct wined3d_window_hook *hook, *last_hook;
909 unsigned int i;
911 wined3d_wndproc_mutex_lock();
913 for (i = 0; i < swapchain_state_table.state_count; ++i)
915 state_entry = &swapchain_state_table.states[i];
917 if (state_entry->state != state)
918 continue;
920 if ((hook = wined3d_find_hook(state_entry->thread_id)) && !--hook->count)
922 UnhookWindowsHookEx(hook->hook);
923 last_hook = &swapchain_state_table.hooks[--swapchain_state_table.hook_count];
924 if (hook != last_hook)
925 *hook = *last_hook;
928 last_state_entry = &swapchain_state_table.states[--swapchain_state_table.state_count];
929 if (state_entry != last_state_entry)
930 *state_entry = *last_state_entry;
932 break;
935 wined3d_wndproc_mutex_unlock();
938 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state *state)
940 wined3d_swapchain_state_unregister(state);
943 /* At process attach */
944 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
946 switch (reason)
948 case DLL_PROCESS_ATTACH:
949 return wined3d_dll_init(inst);
951 case DLL_PROCESS_DETACH:
952 if (!reserved)
953 return wined3d_dll_destroy(inst);
954 break;
956 case DLL_THREAD_DETACH:
957 if (!wined3d_context_gl_set_current(NULL))
959 ERR("Failed to clear current context.\n");
961 return TRUE;
963 return TRUE;