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
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
32 struct wined3d_wndproc
37 struct wined3d_device
*device
;
40 struct wined3d_wndproc_table
42 struct wined3d_wndproc
*entries
;
47 static struct wined3d_wndproc_table wndproc_table
;
50 void (CDECL
*wine_tsx11_lock_ptr
)(void) = NULL
;
51 void (CDECL
*wine_tsx11_unlock_ptr
)(void) = NULL
;
53 static CRITICAL_SECTION wined3d_cs
;
54 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
57 {&wined3d_cs_debug
.ProcessLocksList
,
58 &wined3d_cs_debug
.ProcessLocksList
},
59 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
61 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
63 static CRITICAL_SECTION wined3d_wndproc_cs
;
64 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug
=
66 0, 0, &wined3d_wndproc_cs
,
67 {&wined3d_wndproc_cs_debug
.ProcessLocksList
,
68 &wined3d_wndproc_cs_debug
.ProcessLocksList
},
69 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_wndproc_cs")}
71 static CRITICAL_SECTION wined3d_wndproc_cs
= {&wined3d_wndproc_cs_debug
, -1, 0, 0, 0, 0};
73 /* When updating default value here, make sure to update winecfg as well,
74 * where appropriate. */
75 struct wined3d_settings wined3d_settings
=
77 VS_HW
, /* Hardware by default */
78 PS_HW
, /* Hardware by default */
79 TRUE
, /* Use of GLSL enabled by default */
80 ORM_FBO
, /* Use FBOs to do offscreen rendering */
81 RTL_READTEX
, /* Default render target locking method */
82 PCI_VENDOR_NONE
,/* PCI Vendor ID */
83 PCI_DEVICE_NONE
,/* PCI Device ID */
84 0, /* The default of memory is set in init_driver_info */
85 NULL
, /* No wine logo by default */
86 FALSE
, /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
87 FALSE
, /* No strict draw ordering. */
90 /* Do not call while under the GL lock. */
91 struct wined3d
* CDECL
wined3d_create(UINT version
, DWORD flags
, void *parent
)
93 struct wined3d
*object
;
96 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
99 ERR("Failed to allocate wined3d object memory.\n");
103 hr
= wined3d_init(object
, version
, flags
, parent
);
106 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
107 HeapFree(GetProcessHeap(), 0, object
);
111 TRACE("Created wined3d object %p for d3d%d support.\n", object
, version
);
116 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
118 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
119 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
120 return ERROR_FILE_NOT_FOUND
;
123 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
126 DWORD size
= sizeof(DWORD
);
127 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
128 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
129 return ERROR_FILE_NOT_FOUND
;
132 static void CDECL
wined3d_do_nothing(void)
136 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
138 DWORD wined3d_context_tls_idx
;
140 char buffer
[MAX_PATH
+10];
141 DWORD size
= sizeof(buffer
);
147 wined3d_context_tls_idx
= TlsAlloc();
148 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
150 DWORD err
= GetLastError();
151 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
154 context_set_tls_idx(wined3d_context_tls_idx
);
156 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
157 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
158 * Various articles/posts about OpenGL problems on Windows recommend this. */
159 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
160 wc
.lpfnWndProc
= DefWindowProcA
;
163 wc
.hInstance
= hInstDLL
;
164 wc
.hIcon
= LoadIconA(NULL
, (LPCSTR
)IDI_WINLOGO
);
165 wc
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)IDC_ARROW
);
166 wc
.hbrBackground
= NULL
;
167 wc
.lpszMenuName
= NULL
;
168 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
170 if (!RegisterClassA(&wc
))
172 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
173 if (!TlsFree(wined3d_context_tls_idx
))
175 DWORD err
= GetLastError();
176 ERR("Failed to free context TLS index, err %#x.\n", err
);
181 DisableThreadLibraryCalls(hInstDLL
);
183 mod
= GetModuleHandleA( "winex11.drv" );
186 wine_tsx11_lock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_lock" );
187 wine_tsx11_unlock_ptr
= (void *)GetProcAddress( mod
, "wine_tsx11_unlock" );
189 else /* We are most likely on Windows */
191 wine_tsx11_lock_ptr
= wined3d_do_nothing
;
192 wine_tsx11_unlock_ptr
= wined3d_do_nothing
;
194 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
195 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
197 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
198 if (len
&& len
< MAX_PATH
)
201 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
202 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
204 char *p
, *appname
= buffer
;
205 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
206 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
207 strcat( appname
, "\\Direct3D" );
208 TRACE("appname = [%s]\n", appname
);
209 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
210 RegCloseKey( tmpkey
);
216 if ( !get_config_key( hkey
, appkey
, "VertexShaderMode", buffer
, size
) )
218 if (!strcmp(buffer
,"none"))
220 TRACE("Disable vertex shaders\n");
221 wined3d_settings
.vs_mode
= VS_NONE
;
224 if ( !get_config_key( hkey
, appkey
, "PixelShaderMode", buffer
, size
) )
226 if (!strcmp(buffer
,"enabled"))
228 TRACE("Allow pixel shaders\n");
229 wined3d_settings
.ps_mode
= PS_HW
;
231 if (!strcmp(buffer
,"disabled"))
233 TRACE("Disable pixel shaders\n");
234 wined3d_settings
.ps_mode
= PS_NONE
;
237 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
239 if (!strcmp(buffer
,"disabled"))
241 TRACE("Use of GL Shading Language disabled\n");
242 wined3d_settings
.glslRequested
= FALSE
;
245 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
247 if (!strcmp(buffer
,"backbuffer"))
249 TRACE("Using the backbuffer for offscreen rendering\n");
250 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
252 else if (!strcmp(buffer
,"fbo"))
254 TRACE("Using FBOs for offscreen rendering\n");
255 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
258 if ( !get_config_key( hkey
, appkey
, "RenderTargetLockMode", buffer
, size
) )
260 if (!strcmp(buffer
,"disabled"))
262 TRACE("Disabling render target locking\n");
263 wined3d_settings
.rendertargetlock_mode
= RTL_DISABLE
;
265 else if (!strcmp(buffer
,"readdraw"))
267 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
268 wined3d_settings
.rendertargetlock_mode
= RTL_READDRAW
;
270 else if (!strcmp(buffer
,"readtex"))
272 TRACE("Using glReadPixels for render target reading and textures for writing\n");
273 wined3d_settings
.rendertargetlock_mode
= RTL_READTEX
;
276 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
278 int pci_device_id
= tmpvalue
;
280 /* A pci device id is 16-bit */
281 if(pci_device_id
> 0xffff)
283 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
287 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
288 wined3d_settings
.pci_device_id
= pci_device_id
;
291 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
293 int pci_vendor_id
= tmpvalue
;
295 /* A pci device id is 16-bit */
296 if(pci_vendor_id
> 0xffff)
298 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
302 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
303 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
306 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
308 int TmpVideoMemorySize
= atoi(buffer
);
309 if(TmpVideoMemorySize
> 0)
311 wined3d_settings
.emulated_textureram
= TmpVideoMemorySize
*1024*1024;
312 TRACE("Use %iMB = %d byte for emulated_textureram\n",
314 wined3d_settings
.emulated_textureram
);
317 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
319 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
321 size_t len
= strlen(buffer
) + 1;
323 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
324 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
325 else memcpy(wined3d_settings
.logo
, buffer
, len
);
327 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
329 if (!strcmp(buffer
,"enabled"))
331 TRACE("Allow multisampling\n");
332 wined3d_settings
.allow_multisampling
= TRUE
;
335 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
336 && !strcmp(buffer
,"enabled"))
338 TRACE("Enforcing strict draw ordering.\n");
339 wined3d_settings
.strict_draw_ordering
= TRUE
;
342 if (wined3d_settings
.vs_mode
== VS_HW
)
343 TRACE("Allow HW vertex shaders\n");
344 if (wined3d_settings
.ps_mode
== PS_NONE
)
345 TRACE("Disable pixel shaders\n");
346 if (wined3d_settings
.glslRequested
)
347 TRACE("If supported by your system, GL Shading Language will be used\n");
349 if (appkey
) RegCloseKey( appkey
);
350 if (hkey
) RegCloseKey( hkey
);
355 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
357 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
360 if (!TlsFree(wined3d_context_tls_idx
))
362 DWORD err
= GetLastError();
363 ERR("Failed to free context TLS index, err %#x.\n", err
);
366 for (i
= 0; i
< wndproc_table
.count
; ++i
)
368 /* Trying to unregister these would be futile. These entries can only
369 * exist if either we skipped them in wined3d_unregister_window() due
370 * to the application replacing the wndproc after the entry was
371 * registered, or if the application still has an active wined3d
372 * device. In the latter case the application has bigger problems than
374 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
376 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
378 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
379 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
384 void WINAPI
wined3d_mutex_lock(void)
386 EnterCriticalSection(&wined3d_cs
);
389 void WINAPI
wined3d_mutex_unlock(void)
391 LeaveCriticalSection(&wined3d_cs
);
394 static void wined3d_wndproc_mutex_lock(void)
396 EnterCriticalSection(&wined3d_wndproc_cs
);
399 static void wined3d_wndproc_mutex_unlock(void)
401 LeaveCriticalSection(&wined3d_wndproc_cs
);
404 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
408 for (i
= 0; i
< wndproc_table
.count
; ++i
)
410 if (wndproc_table
.entries
[i
].window
== window
)
412 return &wndproc_table
.entries
[i
];
419 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
421 struct wined3d_wndproc
*entry
;
422 struct wined3d_device
*device
;
426 wined3d_wndproc_mutex_lock();
427 entry
= wined3d_find_wndproc(window
);
431 wined3d_wndproc_mutex_unlock();
432 ERR("Window %p is not registered with wined3d.\n", window
);
433 return DefWindowProcW(window
, message
, wparam
, lparam
);
436 device
= entry
->device
;
437 unicode
= entry
->unicode
;
439 wined3d_wndproc_mutex_unlock();
441 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
444 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
446 struct wined3d_wndproc
*entry
;
448 wined3d_wndproc_mutex_lock();
450 if (wined3d_find_wndproc(window
))
452 wined3d_wndproc_mutex_unlock();
453 WARN("Window %p is already registered with wined3d.\n", window
);
457 if (wndproc_table
.size
== wndproc_table
.count
)
459 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
460 struct wined3d_wndproc
*new_entries
;
462 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
463 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
467 wined3d_wndproc_mutex_unlock();
468 ERR("Failed to grow table.\n");
472 wndproc_table
.entries
= new_entries
;
473 wndproc_table
.size
= new_size
;
476 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
477 entry
->window
= window
;
478 entry
->unicode
= IsWindowUnicode(window
);
479 /* Set a window proc that matches the window. Some applications (e.g. NoX)
480 * replace the window proc after we've set ours, and expect to be able to
481 * call the previous one (ours) directly, without using CallWindowProc(). */
483 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
485 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
486 entry
->device
= device
;
488 wined3d_wndproc_mutex_unlock();
493 void wined3d_unregister_window(HWND window
)
495 struct wined3d_wndproc
*entry
, *last
;
498 wined3d_wndproc_mutex_lock();
500 if (!(entry
= wined3d_find_wndproc(window
)))
502 wined3d_wndproc_mutex_unlock();
503 ERR("Window %p is not registered with wined3d.\n", window
);
509 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
510 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
512 wined3d_wndproc_mutex_unlock();
513 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
514 window
, proc
, wined3d_wndproc
);
518 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
522 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
523 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
525 wined3d_wndproc_mutex_unlock();
526 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
527 window
, proc
, wined3d_wndproc
);
531 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
534 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
535 if (entry
!= last
) *entry
= *last
;
537 wined3d_wndproc_mutex_unlock();
540 /* At process attach */
541 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
543 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason
);
547 case DLL_PROCESS_ATTACH
:
548 return wined3d_dll_init(hInstDLL
);
550 case DLL_PROCESS_DETACH
:
551 return wined3d_dll_destroy(hInstDLL
);
553 case DLL_THREAD_DETACH
:
555 if (!context_set_current(NULL
))
557 ERR("Failed to clear current context.\n");