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
28 #include "wined3d_private.h"
29 #include "wined3d_gl.h"
31 #define VK_NO_PROTOTYPES
32 #include "wine/vulkan.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
;
46 struct wined3d_device
*device
;
50 struct wined3d_wndproc_table
52 struct wined3d_wndproc
*entries
;
57 struct wined3d_window_hook
64 struct wined3d_registered_swapchain_state
66 struct wined3d_swapchain_state
*state
;
70 struct wined3d_swapchain_state_table
72 struct wined3d_window_hook
*hooks
;
76 struct wined3d_registered_swapchain_state
*states
;
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
=
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
,
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 return wined3d_settings
.renderer
;
140 struct wined3d
* CDECL
wined3d_create(uint32_t flags
)
142 struct wined3d
*object
;
145 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d
, adapters
[1]))))
147 ERR("Failed to allocate wined3d object memory.\n");
151 if (wined3d_settings
.renderer
== WINED3D_RENDERER_NO3D
)
152 flags
|= WINED3D_NO3D
;
154 if (FAILED(hr
= wined3d_init(object
, flags
)))
156 WARN("Failed to initialize wined3d object, hr %#lx.\n", hr
);
161 TRACE("Created wined3d object %p.\n", object
);
166 static bool is_option_separator(char c
)
168 return c
== ',' || c
== ';' || c
== '\0';
171 static const char *config_list_get_value(const char *string
, const char *key
, size_t *len
)
179 if ((p
= strstr(p
, key
)))
181 prev_char
= p
> string
? p
[-1] : 0;
184 if (is_option_separator(prev_char
) && *p
== '=')
186 if ((end
= strpbrk(p
+ 1, ",;")))
187 *len
= end
- (p
+ 1);
189 *len
= strlen(p
+ 1);
198 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *env
, const char *name
, char *buffer
, DWORD size
)
200 const char *env_value
;
203 if ((env_value
= config_list_get_value(env
, name
, &env_len
)) && env_len
< size
)
205 memcpy(buffer
, env_value
, env_len
);
209 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
210 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
211 return ERROR_FILE_NOT_FOUND
;
214 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *env
, const char *name
, unsigned int *value
)
216 DWORD type
, data
, size
;
217 const char *env_value
;
221 if ((env_value
= config_list_get_value(env
, name
, &env_len
)))
223 *value
= strtoul(env_value
, &end
, 0);
224 if (end
!= env_value
)
228 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
230 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
232 return ERROR_FILE_NOT_FOUND
;
239 BOOL
wined3d_get_app_name(char *app_name
, unsigned int app_name_size
)
241 char buffer
[MAX_PATH
];
245 len
= GetModuleFileNameA(0, buffer
, ARRAY_SIZE(buffer
));
246 if (!(len
&& len
< MAX_PATH
))
250 if ((p
= strrchr(name
, '/' )))
252 if ((p
= strrchr(name
, '\\')))
255 len
= strlen(name
) + 1;
256 if (app_name_size
< len
)
259 memcpy(app_name
, name
, len
);
263 static void vkd3d_log_callback(const char *fmt
, va_list args
)
267 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
268 __wine_dbg_output(buffer
);
271 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
273 DWORD wined3d_context_tls_idx
;
274 char buffer
[MAX_PATH
+10];
275 DWORD size
= sizeof(buffer
);
279 unsigned int tmpvalue
;
282 wined3d_context_tls_idx
= TlsAlloc();
283 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
285 unsigned int err
= GetLastError();
286 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
289 context_set_tls_idx(wined3d_context_tls_idx
);
291 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
292 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
293 * Various articles/posts about OpenGL problems on Windows recommend this. */
294 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
295 wc
.lpfnWndProc
= DefWindowProcA
;
298 wc
.hInstance
= hInstDLL
;
299 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
300 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
301 wc
.hbrBackground
= NULL
;
302 wc
.lpszMenuName
= NULL
;
303 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
305 if (!RegisterClassA(&wc
))
307 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
308 if (!TlsFree(wined3d_context_tls_idx
))
310 unsigned int err
= GetLastError();
311 ERR("Failed to free context TLS index, err %#x.\n", err
);
316 DisableThreadLibraryCalls(hInstDLL
);
318 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
319 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
321 if (wined3d_get_app_name(buffer
, ARRAY_SIZE(buffer
)))
324 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
325 if (!RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
327 strcat(buffer
, "\\Direct3D");
328 TRACE("Application name %s.\n", buffer
);
329 if (RegOpenKeyA(tmpkey
, buffer
, &appkey
)) appkey
= 0;
334 /* Allow modifying settings using the WINE_D3D_CONFIG environment variable,
335 * which takes precedence over registry keys. An example is as follows:
337 * WINE_D3D_CONFIG=csmt=0x1,shader_backend=glsl
339 env
= getenv("WINE_D3D_CONFIG");
341 if (hkey
|| appkey
|| env
)
343 if (!get_config_key_dword(hkey
, appkey
, env
, "csmt", &wined3d_settings
.cs_multithreaded
))
344 ERR_(winediag
)("Setting multithreaded command stream to %#x.\n", wined3d_settings
.cs_multithreaded
);
345 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxVersionGL", &tmpvalue
))
347 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
348 tmpvalue
>> 16, tmpvalue
& 0xffff);
349 wined3d_settings
.max_gl_version
= tmpvalue
;
351 if (!get_config_key(hkey
, appkey
, env
, "shader_backend", buffer
, size
))
353 if (!stricmp(buffer
, "glsl"))
355 ERR_(winediag
)("Using the GLSL shader backend.\n");
356 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_GLSL
;
358 else if (!stricmp(buffer
, "arb"))
360 ERR_(winediag
)("Using the ARB shader backend.\n");
361 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_ARB
;
363 else if (!stricmp(buffer
, "none"))
365 ERR_(winediag
)("Disabling shader backends.\n");
366 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_NONE
;
369 if (wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_ARB
370 || wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_NONE
)
372 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
373 TRACE("Use of GL Shading Language disabled.\n");
375 if (!get_config_key(hkey
, appkey
, env
, "OffscreenRenderingMode", buffer
, size
)
376 && !strcmp(buffer
,"backbuffer"))
377 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
378 if (!get_config_key_dword(hkey
, appkey
, env
, "VideoPciDeviceID", &tmpvalue
))
380 int pci_device_id
= tmpvalue
;
382 /* A pci device id is 16-bit */
383 if(pci_device_id
> 0xffff)
385 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
389 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
390 wined3d_settings
.pci_device_id
= pci_device_id
;
393 if (!get_config_key_dword(hkey
, appkey
, env
, "VideoPciVendorID", &tmpvalue
))
395 int pci_vendor_id
= tmpvalue
;
397 /* A pci device id is 16-bit */
398 if(pci_vendor_id
> 0xffff)
400 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
404 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
405 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
408 if (!get_config_key(hkey
, appkey
, env
, "VideoMemorySize", buffer
, size
))
410 int TmpVideoMemorySize
= atoi(buffer
);
411 if(TmpVideoMemorySize
> 0)
413 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
414 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
416 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
419 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
421 if (!get_config_key(hkey
, appkey
, env
, "WineLogo", buffer
, size
))
423 size_t len
= strlen(buffer
) + 1;
425 if (!(wined3d_settings
.logo
= heap_alloc(len
)))
426 ERR("Failed to allocate logo path memory.\n");
428 memcpy(wined3d_settings
.logo
, buffer
, len
);
430 if (!get_config_key_dword(hkey
, appkey
, env
, "MultisampleTextures", &wined3d_settings
.multisample_textures
))
431 ERR_(winediag
)("Setting multisample textures to %#x.\n", wined3d_settings
.multisample_textures
);
432 if (!get_config_key_dword(hkey
, appkey
, env
, "SampleCount", &wined3d_settings
.sample_count
))
433 ERR_(winediag
)("Forcing sample count to %u. This may not be compatible with all applications.\n",
434 wined3d_settings
.sample_count
);
435 if (!get_config_key(hkey
, appkey
, env
, "CheckFloatConstants", buffer
, size
)
436 && !strcmp(buffer
, "enabled"))
438 TRACE("Checking relative addressing indices in float constants.\n");
439 wined3d_settings
.check_float_constants
= TRUE
;
441 if (!get_config_key_dword(hkey
, appkey
, env
, "strict_shader_math", &wined3d_settings
.strict_shader_math
))
442 ERR_(winediag
)("Setting strict shader math to %#x.\n", wined3d_settings
.strict_shader_math
);
443 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
444 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
445 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelHS", &wined3d_settings
.max_sm_hs
))
446 TRACE("Limiting HS shader model to %u.\n", wined3d_settings
.max_sm_hs
);
447 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelDS", &wined3d_settings
.max_sm_ds
))
448 TRACE("Limiting DS shader model to %u.\n", wined3d_settings
.max_sm_ds
);
449 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
450 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
451 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
452 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
453 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelCS", &wined3d_settings
.max_sm_cs
))
454 TRACE("Limiting CS shader model to %u.\n", wined3d_settings
.max_sm_cs
);
455 if (!get_config_key(hkey
, appkey
, env
, "renderer", buffer
, size
))
457 if (!strcmp(buffer
, "vulkan"))
459 ERR_(winediag
)("Using the Vulkan renderer.\n");
460 wined3d_settings
.renderer
= WINED3D_RENDERER_VULKAN
;
462 else if (!strcmp(buffer
, "gl"))
464 ERR_(winediag
)("Using the OpenGL renderer.\n");
465 wined3d_settings
.renderer
= WINED3D_RENDERER_OPENGL
;
467 else if (!strcmp(buffer
, "gdi") || !strcmp(buffer
, "no3d"))
469 ERR_(winediag
)("Disabling 3D support.\n");
470 wined3d_settings
.renderer
= WINED3D_RENDERER_NO3D
;
473 if (!get_config_key_dword(hkey
, appkey
, env
, "cb_access_map_w", &tmpvalue
) && tmpvalue
)
475 TRACE("Forcing all constant buffers to be write-mappable.\n");
476 wined3d_settings
.cb_access_map_w
= TRUE
;
480 if (appkey
) RegCloseKey( appkey
);
481 if (hkey
) RegCloseKey( hkey
);
483 if (!getenv( "VKD3D_DEBUG" ))
485 if (TRACE_ON(vkd3d
)) putenv( "VKD3D_DEBUG=trace" );
486 else if (WARN_ON(vkd3d
)) putenv( "VKD3D_DEBUG=warn" );
487 else if (FIXME_ON(vkd3d
)) putenv( "VKD3D_DEBUG=fixme" );
488 else if (ERR_ON(vkd3d
)) putenv( "VKD3D_DEBUG=err" );
489 else putenv( "VKD3D_DEBUG=none" );
491 if (!getenv( "VKD3D_SHADER_DEBUG" ))
493 if (TRACE_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=trace" );
494 else if (WARN_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=warn" );
495 else if (FIXME_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=fixme" );
496 else if (ERR_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=err" );
497 else putenv( "VKD3D_SHADER_DEBUG=none" );
500 vkd3d_set_log_callback(vkd3d_log_callback
);
502 if (wined3d_settings
.renderer
== WINED3D_RENDERER_AUTO
)
503 wined3d_settings
.renderer
= WINED3D_RENDERER_OPENGL
;
508 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
510 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
513 if (!TlsFree(wined3d_context_tls_idx
))
515 unsigned int err
= GetLastError();
516 ERR("Failed to free context TLS index, err %#x.\n", err
);
519 for (i
= 0; i
< wndproc_table
.count
; ++i
)
521 /* Trying to unregister these would be futile. These entries can only
522 * exist if either we skipped them in wined3d_unregister_window() due
523 * to the application replacing the wndproc after the entry was
524 * registered, or if the application still has an active wined3d
525 * device. In the latter case the application has bigger problems than
527 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
529 heap_free(wndproc_table
.entries
);
531 heap_free(swapchain_state_table
.states
);
532 for (i
= 0; i
< swapchain_state_table
.hook_count
; ++i
)
534 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table
.hooks
[i
]);
535 UnhookWindowsHookEx(swapchain_state_table
.hooks
[i
].hook
);
537 heap_free(swapchain_state_table
.hooks
);
539 heap_free(wined3d_settings
.logo
);
540 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
542 DeleteCriticalSection(&wined3d_command_cs
);
544 DeleteCriticalSection(&wined3d_wndproc_cs
);
545 DeleteCriticalSection(&wined3d_cs
);
549 void WINAPI
wined3d_mutex_lock(void)
551 EnterCriticalSection(&wined3d_cs
);
554 void WINAPI
wined3d_mutex_unlock(void)
556 LeaveCriticalSection(&wined3d_cs
);
559 static void wined3d_wndproc_mutex_lock(void)
561 EnterCriticalSection(&wined3d_wndproc_cs
);
564 static void wined3d_wndproc_mutex_unlock(void)
566 LeaveCriticalSection(&wined3d_wndproc_cs
);
569 static struct wined3d_output
* wined3d_get_output_from_window(const struct wined3d
*wined3d
,
572 unsigned int adapter_idx
, output_idx
;
573 struct wined3d_adapter
*adapter
;
574 MONITORINFOEXW monitor_info
;
577 TRACE("wined3d %p, hwnd %p.\n", wined3d
, hwnd
);
579 monitor
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
580 monitor_info
.cbSize
= sizeof(monitor_info
);
581 if (!GetMonitorInfoW(monitor
, (MONITORINFO
*)&monitor_info
))
583 ERR("GetMonitorInfoW failed, error %#lx.\n", GetLastError());
587 for (adapter_idx
= 0; adapter_idx
< wined3d
->adapter_count
; ++adapter_idx
)
589 adapter
= wined3d
->adapters
[adapter_idx
];
590 for (output_idx
= 0; output_idx
< adapter
->output_count
; ++output_idx
)
592 if (!lstrcmpiW(adapter
->outputs
[output_idx
].device_name
, monitor_info
.szDevice
))
593 return &adapter
->outputs
[output_idx
];
600 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
, struct wined3d
*wined3d
)
604 for (i
= 0; i
< wndproc_table
.count
; ++i
)
606 struct wined3d_wndproc
*entry
= &wndproc_table
.entries
[i
];
608 if (entry
->window
== window
&& entry
->wined3d
== wined3d
)
615 BOOL
wined3d_filter_messages(HWND window
, BOOL filter
)
617 struct wined3d_wndproc
*entry
;
620 wined3d_wndproc_mutex_lock();
622 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
624 wined3d_wndproc_mutex_unlock();
629 entry
->filter
= filter
;
631 wined3d_wndproc_mutex_unlock();
636 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
638 struct wined3d_wndproc
*entry
;
639 struct wined3d_device
*device
;
640 BOOL unicode
, filter
;
643 wined3d_wndproc_mutex_lock();
645 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
647 wined3d_wndproc_mutex_unlock();
648 ERR("Window %p is not registered with wined3d.\n", window
);
649 return DefWindowProcW(window
, message
, wparam
, lparam
);
652 device
= entry
->device
;
653 unicode
= entry
->unicode
;
654 filter
= entry
->filter
;
656 wined3d_wndproc_mutex_unlock();
660 if (filter
&& message
!= WM_DISPLAYCHANGE
)
662 TRACE("Filtering message: window %p, message %#x, wparam %#Ix, lparam %#Ix.\n",
663 window
, message
, wparam
, lparam
);
666 return DefWindowProcW(window
, message
, wparam
, lparam
);
667 return DefWindowProcA(window
, message
, wparam
, lparam
);
670 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
673 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
674 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
677 static LRESULT CALLBACK
wined3d_hook_proc(int code
, WPARAM wparam
, LPARAM lparam
)
679 struct wined3d_swapchain_desc swapchain_desc
;
680 struct wined3d_swapchain_state
*state
;
681 struct wined3d_wndproc
*entry
;
682 struct wined3d_output
*output
;
683 MSG
*msg
= (MSG
*)lparam
;
686 /* Handle Alt+Enter. */
687 if (code
== HC_ACTION
&& msg
->message
== WM_SYSKEYDOWN
688 && msg
->wParam
== VK_RETURN
&& (msg
->lParam
& (KF_ALTDOWN
<< 16)))
690 wined3d_wndproc_mutex_lock();
692 for (i
= 0; i
< swapchain_state_table
.state_count
; ++i
)
694 state
= swapchain_state_table
.states
[i
].state
;
696 if (state
->device_window
!= msg
->hwnd
)
699 if ((entry
= wined3d_find_wndproc(msg
->hwnd
, state
->wined3d
))
700 && (entry
->flags
& (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
701 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER
)))
704 swapchain_desc
= state
->desc
;
705 swapchain_desc
.windowed
= !swapchain_desc
.windowed
;
706 if (!(output
= wined3d_get_output_from_window(state
->wined3d
, state
->device_window
)))
708 ERR("Failed to get output from window %p.\n", state
->device_window
);
711 swapchain_desc
.output
= output
;
712 wined3d_swapchain_state_set_fullscreen(state
, &swapchain_desc
, NULL
);
714 wined3d_wndproc_mutex_unlock();
719 wined3d_wndproc_mutex_unlock();
722 return CallNextHookEx(0, code
, wparam
, lparam
);
725 BOOL CDECL
wined3d_register_window(struct wined3d
*wined3d
, HWND window
,
726 struct wined3d_device
*device
, unsigned int flags
)
728 struct wined3d_wndproc
*entry
;
730 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d
, window
, device
, flags
);
732 wined3d_wndproc_mutex_lock();
734 if ((entry
= wined3d_find_wndproc(window
, wined3d
)))
737 WARN("Window %p is already registered with wined3d.\n", window
);
738 entry
->flags
= flags
;
739 wined3d_wndproc_mutex_unlock();
743 if (!wined3d_array_reserve((void **)&wndproc_table
.entries
, &wndproc_table
.size
,
744 wndproc_table
.count
+ 1, sizeof(*entry
)))
746 wined3d_wndproc_mutex_unlock();
747 ERR("Failed to grow table.\n");
751 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
752 entry
->window
= window
;
753 entry
->unicode
= IsWindowUnicode(window
);
756 /* Set a window proc that matches the window. Some applications (e.g.
757 * NoX) replace the window proc after we've set ours, and expect to be
758 * able to call the previous one (ours) directly, without using
759 * CallWindowProc(). */
761 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
763 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
769 entry
->device
= device
;
770 entry
->wined3d
= wined3d
;
771 entry
->flags
= flags
;
773 wined3d_wndproc_mutex_unlock();
778 static BOOL
restore_wndproc(struct wined3d_wndproc
*entry
)
784 proc
= GetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
);
785 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
787 SetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
791 proc
= GetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
);
792 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
794 SetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
800 void wined3d_unregister_window(HWND window
)
802 struct wined3d_wndproc
*entry
, *last
;
804 wined3d_wndproc_mutex_lock();
806 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
808 wined3d_wndproc_mutex_unlock();
809 ERR("Window %p is not registered with wined3d.\n", window
);
813 if (entry
->proc
&& !restore_wndproc(entry
))
815 entry
->device
= NULL
;
816 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window
);
817 wined3d_wndproc_mutex_unlock();
821 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
822 if (entry
!= last
) *entry
= *last
;
824 wined3d_wndproc_mutex_unlock();
827 void CDECL
wined3d_unregister_windows(struct wined3d
*wined3d
)
829 struct wined3d_wndproc
*entry
, *last
;
832 TRACE("wined3d %p.\n", wined3d
);
834 wined3d_wndproc_mutex_lock();
836 while (i
< wndproc_table
.count
)
838 entry
= &wndproc_table
.entries
[i
];
840 if (entry
->wined3d
!= wined3d
)
846 if (entry
->proc
&& !restore_wndproc(entry
))
848 entry
->device
= NULL
;
849 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
855 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
862 wined3d_wndproc_mutex_unlock();
865 static struct wined3d_window_hook
*wined3d_find_hook(DWORD thread_id
)
869 for (i
= 0; i
< swapchain_state_table
.hook_count
; ++i
)
871 if (swapchain_state_table
.hooks
[i
].thread_id
== thread_id
)
872 return &swapchain_state_table
.hooks
[i
];
878 void wined3d_swapchain_state_register(struct wined3d_swapchain_state
*state
)
880 struct wined3d_registered_swapchain_state
*state_entry
;
881 struct wined3d_window_hook
*hook
;
883 wined3d_wndproc_mutex_lock();
885 if (!wined3d_array_reserve((void **)&swapchain_state_table
.states
, &swapchain_state_table
.states_size
,
886 swapchain_state_table
.state_count
+ 1, sizeof(*state_entry
)))
888 wined3d_wndproc_mutex_unlock();
892 state_entry
= &swapchain_state_table
.states
[swapchain_state_table
.state_count
++];
893 state_entry
->state
= state
;
894 state_entry
->thread_id
= GetWindowThreadProcessId(state
->device_window
, NULL
);
896 if ((hook
= wined3d_find_hook(state_entry
->thread_id
)))
899 wined3d_wndproc_mutex_unlock();
903 if (!wined3d_array_reserve((void **)&swapchain_state_table
.hooks
, &swapchain_state_table
.hooks_size
,
904 swapchain_state_table
.hook_count
+ 1, sizeof(*hook
)))
906 --swapchain_state_table
.state_count
;
907 wined3d_wndproc_mutex_unlock();
911 hook
= &swapchain_state_table
.hooks
[swapchain_state_table
.hook_count
++];
912 hook
->thread_id
= state_entry
->thread_id
;
913 hook
->hook
= SetWindowsHookExW(WH_GETMESSAGE
, wined3d_hook_proc
, 0, hook
->thread_id
);
916 wined3d_wndproc_mutex_unlock();
919 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state
*state
)
921 struct wined3d_registered_swapchain_state
*state_entry
, *last_state_entry
;
922 struct wined3d_window_hook
*hook
, *last_hook
;
925 wined3d_wndproc_mutex_lock();
927 for (i
= 0; i
< swapchain_state_table
.state_count
; ++i
)
929 state_entry
= &swapchain_state_table
.states
[i
];
931 if (state_entry
->state
!= state
)
934 if ((hook
= wined3d_find_hook(state_entry
->thread_id
)) && !--hook
->count
)
936 UnhookWindowsHookEx(hook
->hook
);
937 last_hook
= &swapchain_state_table
.hooks
[--swapchain_state_table
.hook_count
];
938 if (hook
!= last_hook
)
942 last_state_entry
= &swapchain_state_table
.states
[--swapchain_state_table
.state_count
];
943 if (state_entry
!= last_state_entry
)
944 *state_entry
= *last_state_entry
;
949 wined3d_wndproc_mutex_unlock();
952 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state
*state
)
954 wined3d_swapchain_state_unregister(state
);
957 /* At process attach */
958 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
962 case DLL_PROCESS_ATTACH
:
963 return wined3d_dll_init(inst
);
965 case DLL_PROCESS_DETACH
:
967 return wined3d_dll_destroy(inst
);
970 case DLL_THREAD_DETACH
:
971 if (!wined3d_context_gl_set_current(NULL
))
973 ERR("Failed to clear current context.\n");