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 "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
30 struct wined3d_wndproc
35 struct wined3d_device
*device
;
38 struct wined3d_wndproc_table
40 struct wined3d_wndproc
*entries
;
45 static struct wined3d_wndproc_table wndproc_table
;
47 static CRITICAL_SECTION wined3d_cs
;
48 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
51 {&wined3d_cs_debug
.ProcessLocksList
,
52 &wined3d_cs_debug
.ProcessLocksList
},
53 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
55 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
57 static CRITICAL_SECTION wined3d_wndproc_cs
;
58 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug
=
60 0, 0, &wined3d_wndproc_cs
,
61 {&wined3d_wndproc_cs_debug
.ProcessLocksList
,
62 &wined3d_wndproc_cs_debug
.ProcessLocksList
},
63 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_wndproc_cs")}
65 static CRITICAL_SECTION wined3d_wndproc_cs
= {&wined3d_wndproc_cs_debug
, -1, 0, 0, 0, 0};
67 /* When updating default value here, make sure to update winecfg as well,
68 * where appropriate. */
69 struct wined3d_settings wined3d_settings
=
71 TRUE
, /* Use of GLSL enabled by default */
72 ORM_FBO
, /* Use FBOs to do offscreen rendering */
73 PCI_VENDOR_NONE
,/* PCI Vendor ID */
74 PCI_DEVICE_NONE
,/* PCI Device ID */
75 0, /* The default of memory is set in init_driver_info */
76 NULL
, /* No wine logo by default */
77 TRUE
, /* Multisampling enabled by default. */
78 FALSE
, /* No strict draw ordering. */
79 TRUE
, /* Don't try to render onscreen by default. */
80 ~0U, /* No VS shader model limit by default. */
81 ~0U, /* No GS shader model limit by default. */
82 ~0U, /* No PS shader model limit by default. */
83 FALSE
, /* 3D support enabled by default. */
86 /* Do not call while under the GL lock. */
87 struct wined3d
* CDECL
wined3d_create(UINT version
, DWORD flags
)
89 struct wined3d
*object
;
92 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
95 ERR("Failed to allocate wined3d object memory.\n");
99 if (version
== 7 && wined3d_settings
.no_3d
)
100 flags
|= WINED3D_NO3D
;
102 hr
= wined3d_init(object
, version
, flags
);
105 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
106 HeapFree(GetProcessHeap(), 0, object
);
110 TRACE("Created wined3d object %p for d3d%d support.\n", object
, version
);
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
)
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
);
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
);
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
;
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
);
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
)
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
);
199 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
201 if (!strcmp(buffer
,"disabled"))
203 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
204 TRACE("Use of GL Shading Language disabled\n");
205 wined3d_settings
.glslRequested
= FALSE
;
208 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
210 if (!strcmp(buffer
,"backbuffer"))
212 TRACE("Using the backbuffer for offscreen rendering\n");
213 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
215 else if (!strcmp(buffer
,"fbo"))
217 TRACE("Using FBOs for offscreen rendering\n");
218 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
221 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
223 int pci_device_id
= tmpvalue
;
225 /* A pci device id is 16-bit */
226 if(pci_device_id
> 0xffff)
228 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
232 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
233 wined3d_settings
.pci_device_id
= pci_device_id
;
236 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
238 int pci_vendor_id
= tmpvalue
;
240 /* A pci device id is 16-bit */
241 if(pci_vendor_id
> 0xffff)
243 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
247 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
248 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
251 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
253 int TmpVideoMemorySize
= atoi(buffer
);
254 if(TmpVideoMemorySize
> 0)
256 wined3d_settings
.emulated_textureram
= TmpVideoMemorySize
*1024*1024;
257 TRACE("Use %iMB = %d byte for emulated_textureram\n",
259 wined3d_settings
.emulated_textureram
);
262 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
264 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
266 size_t len
= strlen(buffer
) + 1;
268 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
269 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
270 else memcpy(wined3d_settings
.logo
, buffer
, len
);
272 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
274 if (!strcmp(buffer
, "disabled"))
276 TRACE("Multisampling disabled.\n");
277 wined3d_settings
.allow_multisampling
= FALSE
;
280 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
281 && !strcmp(buffer
,"enabled"))
283 TRACE("Enforcing strict draw ordering.\n");
284 wined3d_settings
.strict_draw_ordering
= TRUE
;
286 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
287 && !strcmp(buffer
,"disabled"))
289 TRACE("Not always rendering backbuffers offscreen.\n");
290 wined3d_settings
.always_offscreen
= FALSE
;
292 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
293 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
294 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
295 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
296 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
297 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
298 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
299 && !strcmp(buffer
, "gdi"))
301 TRACE("Disabling 3D support.\n");
302 wined3d_settings
.no_3d
= TRUE
;
306 if (appkey
) RegCloseKey( appkey
);
307 if (hkey
) RegCloseKey( hkey
);
312 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
314 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
317 if (!TlsFree(wined3d_context_tls_idx
))
319 DWORD err
= GetLastError();
320 ERR("Failed to free context TLS index, err %#x.\n", err
);
323 for (i
= 0; i
< wndproc_table
.count
; ++i
)
325 /* Trying to unregister these would be futile. These entries can only
326 * exist if either we skipped them in wined3d_unregister_window() due
327 * to the application replacing the wndproc after the entry was
328 * registered, or if the application still has an active wined3d
329 * device. In the latter case the application has bigger problems than
331 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
333 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
335 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
336 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
338 DeleteCriticalSection(&wined3d_wndproc_cs
);
339 DeleteCriticalSection(&wined3d_cs
);
343 void WINAPI
wined3d_mutex_lock(void)
345 EnterCriticalSection(&wined3d_cs
);
348 void WINAPI
wined3d_mutex_unlock(void)
350 LeaveCriticalSection(&wined3d_cs
);
353 static void wined3d_wndproc_mutex_lock(void)
355 EnterCriticalSection(&wined3d_wndproc_cs
);
358 static void wined3d_wndproc_mutex_unlock(void)
360 LeaveCriticalSection(&wined3d_wndproc_cs
);
363 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
367 for (i
= 0; i
< wndproc_table
.count
; ++i
)
369 if (wndproc_table
.entries
[i
].window
== window
)
371 return &wndproc_table
.entries
[i
];
378 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
380 struct wined3d_wndproc
*entry
;
381 struct wined3d_device
*device
;
385 wined3d_wndproc_mutex_lock();
386 entry
= wined3d_find_wndproc(window
);
390 wined3d_wndproc_mutex_unlock();
391 ERR("Window %p is not registered with wined3d.\n", window
);
392 return DefWindowProcW(window
, message
, wparam
, lparam
);
395 device
= entry
->device
;
396 unicode
= entry
->unicode
;
398 wined3d_wndproc_mutex_unlock();
401 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
403 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
404 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
407 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
409 struct wined3d_wndproc
*entry
;
411 wined3d_wndproc_mutex_lock();
413 if (wined3d_find_wndproc(window
))
415 wined3d_wndproc_mutex_unlock();
416 WARN("Window %p is already registered with wined3d.\n", window
);
420 if (wndproc_table
.size
== wndproc_table
.count
)
422 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
423 struct wined3d_wndproc
*new_entries
;
425 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
426 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
430 wined3d_wndproc_mutex_unlock();
431 ERR("Failed to grow table.\n");
435 wndproc_table
.entries
= new_entries
;
436 wndproc_table
.size
= new_size
;
439 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
440 entry
->window
= window
;
441 entry
->unicode
= IsWindowUnicode(window
);
442 /* Set a window proc that matches the window. Some applications (e.g. NoX)
443 * replace the window proc after we've set ours, and expect to be able to
444 * call the previous one (ours) directly, without using CallWindowProc(). */
446 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
448 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
449 entry
->device
= device
;
451 wined3d_wndproc_mutex_unlock();
456 void wined3d_unregister_window(HWND window
)
458 struct wined3d_wndproc
*entry
, *last
;
461 wined3d_wndproc_mutex_lock();
463 if (!(entry
= wined3d_find_wndproc(window
)))
465 wined3d_wndproc_mutex_unlock();
466 ERR("Window %p is not registered with wined3d.\n", window
);
472 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
473 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
475 entry
->device
= NULL
;
476 wined3d_wndproc_mutex_unlock();
477 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
478 window
, proc
, wined3d_wndproc
);
482 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
486 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
487 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
489 entry
->device
= NULL
;
490 wined3d_wndproc_mutex_unlock();
491 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
492 window
, proc
, wined3d_wndproc
);
496 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
499 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
500 if (entry
!= last
) *entry
= *last
;
502 wined3d_wndproc_mutex_unlock();
505 /* At process attach */
506 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
508 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason
);
512 case DLL_PROCESS_ATTACH
:
513 return wined3d_dll_init(hInstDLL
);
515 case DLL_PROCESS_DETACH
:
517 return wined3d_dll_destroy(hInstDLL
);
519 case DLL_THREAD_DETACH
:
520 if (!context_set_current(NULL
))
522 ERR("Failed to clear current context.\n");