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
26 #include "wine/port.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
34 struct wined3d_wndproc
39 struct wined3d_device
*device
;
42 struct wined3d_wndproc_table
44 struct wined3d_wndproc
*entries
;
49 static struct wined3d_wndproc_table wndproc_table
;
51 static CRITICAL_SECTION wined3d_cs
;
52 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
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 TRUE
, /* Use of GLSL enabled by default */
76 ORM_FBO
, /* Use FBOs to do offscreen rendering */
77 PCI_VENDOR_NONE
,/* PCI Vendor ID */
78 PCI_DEVICE_NONE
,/* PCI Device ID */
79 0, /* The default of memory is set in init_driver_info */
80 NULL
, /* No wine logo by default */
81 TRUE
, /* Multisampling enabled by default. */
82 FALSE
, /* No strict draw ordering. */
83 TRUE
, /* Don't try to render onscreen by default. */
84 ~0U, /* No VS shader model limit by default. */
85 ~0U, /* No GS shader model limit by default. */
86 ~0U, /* No PS shader model limit by default. */
87 FALSE
, /* 3D support enabled by default. */
90 struct wined3d
* CDECL
wined3d_create(DWORD flags
)
92 struct wined3d
*object
;
95 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
98 ERR("Failed to allocate wined3d object memory.\n");
102 if (wined3d_settings
.no_3d
)
103 flags
|= WINED3D_NO3D
;
105 hr
= wined3d_init(object
, flags
);
108 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
109 HeapFree(GetProcessHeap(), 0, object
);
113 TRACE("Created wined3d object %p.\n", object
);
118 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
120 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
121 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
122 return ERROR_FILE_NOT_FOUND
;
125 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
128 DWORD size
= sizeof(DWORD
);
129 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
130 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
131 return ERROR_FILE_NOT_FOUND
;
134 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
136 DWORD wined3d_context_tls_idx
;
137 char buffer
[MAX_PATH
+10];
138 DWORD size
= sizeof(buffer
);
144 wined3d_context_tls_idx
= TlsAlloc();
145 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
147 DWORD err
= GetLastError();
148 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
151 context_set_tls_idx(wined3d_context_tls_idx
);
153 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
154 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
155 * Various articles/posts about OpenGL problems on Windows recommend this. */
156 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
157 wc
.lpfnWndProc
= DefWindowProcA
;
160 wc
.hInstance
= hInstDLL
;
161 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
162 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
163 wc
.hbrBackground
= NULL
;
164 wc
.lpszMenuName
= NULL
;
165 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
167 if (!RegisterClassA(&wc
))
169 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
170 if (!TlsFree(wined3d_context_tls_idx
))
172 DWORD err
= GetLastError();
173 ERR("Failed to free context TLS index, err %#x.\n", err
);
178 DisableThreadLibraryCalls(hInstDLL
);
180 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
181 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
183 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
184 if (len
&& len
< MAX_PATH
)
187 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
188 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
190 char *p
, *appname
= buffer
;
191 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
192 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
193 strcat( appname
, "\\Direct3D" );
194 TRACE("appname = [%s]\n", appname
);
195 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
196 RegCloseKey( tmpkey
);
202 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
204 if (!strcmp(buffer
,"disabled"))
206 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
207 TRACE("Use of GL Shading Language disabled\n");
208 wined3d_settings
.glslRequested
= FALSE
;
211 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
213 if (!strcmp(buffer
,"backbuffer"))
215 TRACE("Using the backbuffer for offscreen rendering\n");
216 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
218 else if (!strcmp(buffer
,"fbo"))
220 TRACE("Using FBOs for offscreen rendering\n");
221 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
224 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
226 int pci_device_id
= tmpvalue
;
228 /* A pci device id is 16-bit */
229 if(pci_device_id
> 0xffff)
231 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
235 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
236 wined3d_settings
.pci_device_id
= pci_device_id
;
239 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
241 int pci_vendor_id
= tmpvalue
;
243 /* A pci device id is 16-bit */
244 if(pci_vendor_id
> 0xffff)
246 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
250 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
251 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
254 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
256 int TmpVideoMemorySize
= atoi(buffer
);
257 if(TmpVideoMemorySize
> 0)
259 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
260 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
262 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
265 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
267 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
269 size_t len
= strlen(buffer
) + 1;
271 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
272 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
273 else memcpy(wined3d_settings
.logo
, buffer
, len
);
275 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
277 if (!strcmp(buffer
, "disabled"))
279 TRACE("Multisampling disabled.\n");
280 wined3d_settings
.allow_multisampling
= FALSE
;
283 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
284 && !strcmp(buffer
,"enabled"))
286 TRACE("Enforcing strict draw ordering.\n");
287 wined3d_settings
.strict_draw_ordering
= TRUE
;
289 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
290 && !strcmp(buffer
,"disabled"))
292 TRACE("Not always rendering backbuffers offscreen.\n");
293 wined3d_settings
.always_offscreen
= FALSE
;
295 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
296 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
297 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
298 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
299 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
300 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
301 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
302 && !strcmp(buffer
, "gdi"))
304 TRACE("Disabling 3D support.\n");
305 wined3d_settings
.no_3d
= TRUE
;
309 if (appkey
) RegCloseKey( appkey
);
310 if (hkey
) RegCloseKey( hkey
);
315 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
317 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
320 if (!TlsFree(wined3d_context_tls_idx
))
322 DWORD err
= GetLastError();
323 ERR("Failed to free context TLS index, err %#x.\n", err
);
326 for (i
= 0; i
< wndproc_table
.count
; ++i
)
328 /* Trying to unregister these would be futile. These entries can only
329 * exist if either we skipped them in wined3d_unregister_window() due
330 * to the application replacing the wndproc after the entry was
331 * registered, or if the application still has an active wined3d
332 * device. In the latter case the application has bigger problems than
334 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
336 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
338 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
339 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
341 DeleteCriticalSection(&wined3d_wndproc_cs
);
342 DeleteCriticalSection(&wined3d_cs
);
346 void WINAPI
wined3d_mutex_lock(void)
348 EnterCriticalSection(&wined3d_cs
);
351 void WINAPI
wined3d_mutex_unlock(void)
353 LeaveCriticalSection(&wined3d_cs
);
356 static void wined3d_wndproc_mutex_lock(void)
358 EnterCriticalSection(&wined3d_wndproc_cs
);
361 static void wined3d_wndproc_mutex_unlock(void)
363 LeaveCriticalSection(&wined3d_wndproc_cs
);
366 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
370 for (i
= 0; i
< wndproc_table
.count
; ++i
)
372 if (wndproc_table
.entries
[i
].window
== window
)
374 return &wndproc_table
.entries
[i
];
381 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
383 struct wined3d_wndproc
*entry
;
384 struct wined3d_device
*device
;
388 wined3d_wndproc_mutex_lock();
389 entry
= wined3d_find_wndproc(window
);
393 wined3d_wndproc_mutex_unlock();
394 ERR("Window %p is not registered with wined3d.\n", window
);
395 return DefWindowProcW(window
, message
, wparam
, lparam
);
398 device
= entry
->device
;
399 unicode
= entry
->unicode
;
401 wined3d_wndproc_mutex_unlock();
404 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
406 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
407 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
410 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
412 struct wined3d_wndproc
*entry
;
414 wined3d_wndproc_mutex_lock();
416 if (wined3d_find_wndproc(window
))
418 wined3d_wndproc_mutex_unlock();
419 WARN("Window %p is already registered with wined3d.\n", window
);
423 if (wndproc_table
.size
== wndproc_table
.count
)
425 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
426 struct wined3d_wndproc
*new_entries
;
428 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
429 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
433 wined3d_wndproc_mutex_unlock();
434 ERR("Failed to grow table.\n");
438 wndproc_table
.entries
= new_entries
;
439 wndproc_table
.size
= new_size
;
442 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
443 entry
->window
= window
;
444 entry
->unicode
= IsWindowUnicode(window
);
445 /* Set a window proc that matches the window. Some applications (e.g. NoX)
446 * replace the window proc after we've set ours, and expect to be able to
447 * call the previous one (ours) directly, without using CallWindowProc(). */
449 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
451 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
452 entry
->device
= device
;
454 wined3d_wndproc_mutex_unlock();
459 void wined3d_unregister_window(HWND window
)
461 struct wined3d_wndproc
*entry
, *last
;
464 wined3d_wndproc_mutex_lock();
466 if (!(entry
= wined3d_find_wndproc(window
)))
468 wined3d_wndproc_mutex_unlock();
469 ERR("Window %p is not registered with wined3d.\n", window
);
475 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
476 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
478 entry
->device
= NULL
;
479 wined3d_wndproc_mutex_unlock();
480 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
481 window
, proc
, wined3d_wndproc
);
485 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
489 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
490 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
492 entry
->device
= NULL
;
493 wined3d_wndproc_mutex_unlock();
494 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
495 window
, proc
, wined3d_wndproc
);
499 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
502 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
503 if (entry
!= last
) *entry
= *last
;
505 wined3d_wndproc_mutex_unlock();
508 /* At process attach */
509 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
513 case DLL_PROCESS_ATTACH
:
514 return wined3d_dll_init(inst
);
516 case DLL_PROCESS_DETACH
:
518 return wined3d_dll_destroy(inst
);
521 case DLL_THREAD_DETACH
:
522 if (!context_set_current(NULL
))
524 ERR("Failed to clear current context.\n");