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 FALSE
, /* No multithreaded CS by default. */
76 MAKEDWORD_VERSION(1, 0), /* Default to legacy OpenGL */
77 TRUE
, /* Use of GLSL enabled by default */
78 ORM_FBO
, /* Use FBOs to do offscreen rendering */
79 PCI_VENDOR_NONE
,/* PCI Vendor ID */
80 PCI_DEVICE_NONE
,/* PCI Device ID */
81 0, /* The default of memory is set in init_driver_info */
82 NULL
, /* No wine logo by default */
83 ~0u, /* Don't force a specific sample count by default. */
84 FALSE
, /* No strict draw ordering. */
85 FALSE
, /* Don't range check relative addressing indices in float constants. */
86 ~0U, /* No VS shader model limit by default. */
87 ~0U, /* No HS shader model limit by default. */
88 ~0U, /* No DS shader model limit by default. */
89 ~0U, /* No GS shader model limit by default. */
90 ~0U, /* No PS shader model limit by default. */
91 ~0u, /* No CS shader model limit by default. */
92 FALSE
, /* 3D support enabled by default. */
95 struct wined3d
* CDECL
wined3d_create(DWORD flags
)
97 struct wined3d
*object
;
100 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
103 ERR("Failed to allocate wined3d object memory.\n");
107 if (wined3d_settings
.no_3d
)
108 flags
|= WINED3D_NO3D
;
110 hr
= wined3d_init(object
, flags
);
113 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
114 HeapFree(GetProcessHeap(), 0, object
);
118 TRACE("Created wined3d object %p.\n", object
);
123 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
125 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
126 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
127 return ERROR_FILE_NOT_FOUND
;
130 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*value
)
132 DWORD type
, data
, size
;
135 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
137 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
139 return ERROR_FILE_NOT_FOUND
;
146 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
148 DWORD wined3d_context_tls_idx
;
149 char buffer
[MAX_PATH
+10];
150 DWORD size
= sizeof(buffer
);
156 wined3d_context_tls_idx
= TlsAlloc();
157 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
159 DWORD err
= GetLastError();
160 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
163 context_set_tls_idx(wined3d_context_tls_idx
);
165 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
166 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
167 * Various articles/posts about OpenGL problems on Windows recommend this. */
168 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
169 wc
.lpfnWndProc
= DefWindowProcA
;
172 wc
.hInstance
= hInstDLL
;
173 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
174 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
175 wc
.hbrBackground
= NULL
;
176 wc
.lpszMenuName
= NULL
;
177 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
179 if (!RegisterClassA(&wc
))
181 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
182 if (!TlsFree(wined3d_context_tls_idx
))
184 DWORD err
= GetLastError();
185 ERR("Failed to free context TLS index, err %#x.\n", err
);
190 DisableThreadLibraryCalls(hInstDLL
);
192 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
193 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
195 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
196 if (len
&& len
< MAX_PATH
)
199 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
200 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
202 char *p
, *appname
= buffer
;
203 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
204 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
205 strcat( appname
, "\\Direct3D" );
206 TRACE("appname = [%s]\n", appname
);
207 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
208 RegCloseKey( tmpkey
);
214 if (!get_config_key_dword(hkey
, appkey
, "csmt", &wined3d_settings
.cs_multithreaded
))
215 ERR_(winediag
)("Setting multithreaded command stream to %#x.\n", wined3d_settings
.cs_multithreaded
);
216 if (!get_config_key_dword(hkey
, appkey
, "MaxVersionGL", &tmpvalue
))
218 if (tmpvalue
!= wined3d_settings
.max_gl_version
)
220 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
221 tmpvalue
>> 16, tmpvalue
& 0xffff);
222 wined3d_settings
.max_gl_version
= tmpvalue
;
225 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
227 if (!strcmp(buffer
,"disabled"))
229 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
230 TRACE("Use of GL Shading Language disabled\n");
231 wined3d_settings
.glslRequested
= FALSE
;
234 if (!get_config_key(hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
)
235 && !strcmp(buffer
,"backbuffer"))
236 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
237 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
239 int pci_device_id
= tmpvalue
;
241 /* A pci device id is 16-bit */
242 if(pci_device_id
> 0xffff)
244 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
248 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
249 wined3d_settings
.pci_device_id
= pci_device_id
;
252 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
254 int pci_vendor_id
= tmpvalue
;
256 /* A pci device id is 16-bit */
257 if(pci_vendor_id
> 0xffff)
259 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
263 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
264 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
267 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
269 int TmpVideoMemorySize
= atoi(buffer
);
270 if(TmpVideoMemorySize
> 0)
272 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
273 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
275 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
278 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
280 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
282 size_t len
= strlen(buffer
) + 1;
284 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
285 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
286 else memcpy(wined3d_settings
.logo
, buffer
, len
);
288 if (!get_config_key_dword(hkey
, appkey
, "SampleCount", &wined3d_settings
.sample_count
))
289 ERR_(winediag
)("Forcing sample count to %u. This may not be compatible with all applications.\n",
290 wined3d_settings
.sample_count
);
291 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
292 && !strcmp(buffer
,"enabled"))
294 ERR_(winediag
)("\"StrictDrawOrdering\" is deprecated, please use \"csmt\" instead.\n");
295 TRACE("Enforcing strict draw ordering.\n");
296 wined3d_settings
.strict_draw_ordering
= TRUE
;
298 if (!get_config_key(hkey
, appkey
, "CheckFloatConstants", buffer
, size
)
299 && !strcmp(buffer
, "enabled"))
301 TRACE("Checking relative addressing indices in float constants.\n");
302 wined3d_settings
.check_float_constants
= TRUE
;
304 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
305 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
306 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelHS", &wined3d_settings
.max_sm_hs
))
307 TRACE("Limiting HS shader model to %u.\n", wined3d_settings
.max_sm_hs
);
308 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelDS", &wined3d_settings
.max_sm_ds
))
309 TRACE("Limiting DS shader model to %u.\n", wined3d_settings
.max_sm_ds
);
310 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
311 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
312 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
313 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
314 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelCS", &wined3d_settings
.max_sm_cs
))
315 TRACE("Limiting CS shader model to %u.\n", wined3d_settings
.max_sm_cs
);
316 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
317 && !strcmp(buffer
, "gdi"))
319 TRACE("Disabling 3D support.\n");
320 wined3d_settings
.no_3d
= TRUE
;
324 if (appkey
) RegCloseKey( appkey
);
325 if (hkey
) RegCloseKey( hkey
);
330 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
332 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
335 if (!TlsFree(wined3d_context_tls_idx
))
337 DWORD err
= GetLastError();
338 ERR("Failed to free context TLS index, err %#x.\n", err
);
341 for (i
= 0; i
< wndproc_table
.count
; ++i
)
343 /* Trying to unregister these would be futile. These entries can only
344 * exist if either we skipped them in wined3d_unregister_window() due
345 * to the application replacing the wndproc after the entry was
346 * registered, or if the application still has an active wined3d
347 * device. In the latter case the application has bigger problems than
349 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
351 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
353 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
354 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
356 DeleteCriticalSection(&wined3d_wndproc_cs
);
357 DeleteCriticalSection(&wined3d_cs
);
361 void WINAPI
wined3d_mutex_lock(void)
363 EnterCriticalSection(&wined3d_cs
);
366 void WINAPI
wined3d_mutex_unlock(void)
368 LeaveCriticalSection(&wined3d_cs
);
371 static void wined3d_wndproc_mutex_lock(void)
373 EnterCriticalSection(&wined3d_wndproc_cs
);
376 static void wined3d_wndproc_mutex_unlock(void)
378 LeaveCriticalSection(&wined3d_wndproc_cs
);
381 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
385 for (i
= 0; i
< wndproc_table
.count
; ++i
)
387 if (wndproc_table
.entries
[i
].window
== window
)
389 return &wndproc_table
.entries
[i
];
396 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
398 struct wined3d_wndproc
*entry
;
399 struct wined3d_device
*device
;
403 wined3d_wndproc_mutex_lock();
404 entry
= wined3d_find_wndproc(window
);
408 wined3d_wndproc_mutex_unlock();
409 ERR("Window %p is not registered with wined3d.\n", window
);
410 return DefWindowProcW(window
, message
, wparam
, lparam
);
413 device
= entry
->device
;
414 unicode
= entry
->unicode
;
416 wined3d_wndproc_mutex_unlock();
419 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
421 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
422 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
425 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
427 struct wined3d_wndproc
*entry
;
429 wined3d_wndproc_mutex_lock();
431 if (wined3d_find_wndproc(window
))
433 wined3d_wndproc_mutex_unlock();
434 WARN("Window %p is already registered with wined3d.\n", window
);
438 if (!wined3d_array_reserve((void **)&wndproc_table
.entries
, &wndproc_table
.size
,
439 wndproc_table
.count
+ 1, sizeof(*entry
)))
441 wined3d_wndproc_mutex_unlock();
442 ERR("Failed to grow table.\n");
446 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
447 entry
->window
= window
;
448 entry
->unicode
= IsWindowUnicode(window
);
449 /* Set a window proc that matches the window. Some applications (e.g. NoX)
450 * replace the window proc after we've set ours, and expect to be able to
451 * call the previous one (ours) directly, without using CallWindowProc(). */
453 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
455 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
456 entry
->device
= device
;
458 wined3d_wndproc_mutex_unlock();
463 void wined3d_unregister_window(HWND window
)
465 struct wined3d_wndproc
*entry
, *last
;
468 wined3d_wndproc_mutex_lock();
470 if (!(entry
= wined3d_find_wndproc(window
)))
472 wined3d_wndproc_mutex_unlock();
473 ERR("Window %p is not registered with wined3d.\n", window
);
479 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
480 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
482 entry
->device
= NULL
;
483 wined3d_wndproc_mutex_unlock();
484 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
485 window
, proc
, wined3d_wndproc
);
489 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
493 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
494 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
496 entry
->device
= NULL
;
497 wined3d_wndproc_mutex_unlock();
498 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
499 window
, proc
, wined3d_wndproc
);
503 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
506 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
507 if (entry
!= last
) *entry
= *last
;
509 wined3d_wndproc_mutex_unlock();
512 /* At process attach */
513 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
517 case DLL_PROCESS_ATTACH
:
518 return wined3d_dll_init(inst
);
520 case DLL_PROCESS_DETACH
:
522 return wined3d_dll_destroy(inst
);
525 case DLL_THREAD_DETACH
:
526 if (!context_set_current(NULL
))
528 ERR("Failed to clear current context.\n");