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 MAKEDWORD_VERSION(1, 0), /* Default to legacy OpenGL */
76 TRUE
, /* Use of GLSL enabled by default */
77 ORM_FBO
, /* Use FBOs to do offscreen rendering */
78 PCI_VENDOR_NONE
,/* PCI Vendor ID */
79 PCI_DEVICE_NONE
,/* PCI Device ID */
80 0, /* The default of memory is set in init_driver_info */
81 NULL
, /* No wine logo by default */
82 TRUE
, /* Multisampling enabled by default. */
83 FALSE
, /* No strict draw ordering. */
84 TRUE
, /* Don't try to render onscreen by default. */
85 ~0U, /* No VS shader model limit by default. */
86 ~0U, /* No GS shader model limit by default. */
87 ~0U, /* No PS shader model limit by default. */
88 FALSE
, /* 3D support enabled by default. */
91 struct wined3d
* CDECL
wined3d_create(DWORD flags
)
93 struct wined3d
*object
;
96 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
99 ERR("Failed to allocate wined3d object memory.\n");
103 if (wined3d_settings
.no_3d
)
104 flags
|= WINED3D_NO3D
;
106 hr
= wined3d_init(object
, flags
);
109 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
110 HeapFree(GetProcessHeap(), 0, object
);
114 TRACE("Created wined3d object %p.\n", object
);
119 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
121 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
122 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
123 return ERROR_FILE_NOT_FOUND
;
126 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
129 DWORD size
= sizeof(DWORD
);
130 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
131 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
132 return ERROR_FILE_NOT_FOUND
;
135 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
137 DWORD wined3d_context_tls_idx
;
138 char buffer
[MAX_PATH
+10];
139 DWORD size
= sizeof(buffer
);
145 wined3d_context_tls_idx
= TlsAlloc();
146 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
148 DWORD err
= GetLastError();
149 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
152 context_set_tls_idx(wined3d_context_tls_idx
);
154 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
155 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
156 * Various articles/posts about OpenGL problems on Windows recommend this. */
157 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
158 wc
.lpfnWndProc
= DefWindowProcA
;
161 wc
.hInstance
= hInstDLL
;
162 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
163 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
164 wc
.hbrBackground
= NULL
;
165 wc
.lpszMenuName
= NULL
;
166 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
168 if (!RegisterClassA(&wc
))
170 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
171 if (!TlsFree(wined3d_context_tls_idx
))
173 DWORD err
= GetLastError();
174 ERR("Failed to free context TLS index, err %#x.\n", err
);
179 DisableThreadLibraryCalls(hInstDLL
);
181 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
182 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
184 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
185 if (len
&& len
< MAX_PATH
)
188 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
189 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
191 char *p
, *appname
= buffer
;
192 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
193 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
194 strcat( appname
, "\\Direct3D" );
195 TRACE("appname = [%s]\n", appname
);
196 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
197 RegCloseKey( tmpkey
);
203 if (!get_config_key_dword(hkey
, appkey
, "MaxVersionGL", &tmpvalue
))
205 if (tmpvalue
!= wined3d_settings
.max_gl_version
)
207 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
208 tmpvalue
>> 16, tmpvalue
& 0xffff);
209 wined3d_settings
.max_gl_version
= tmpvalue
;
212 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
214 if (!strcmp(buffer
,"disabled"))
216 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
217 TRACE("Use of GL Shading Language disabled\n");
218 wined3d_settings
.glslRequested
= FALSE
;
221 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
223 if (!strcmp(buffer
,"backbuffer"))
225 TRACE("Using the backbuffer for offscreen rendering\n");
226 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
228 else if (!strcmp(buffer
,"fbo"))
230 TRACE("Using FBOs for offscreen rendering\n");
231 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
234 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
236 int pci_device_id
= tmpvalue
;
238 /* A pci device id is 16-bit */
239 if(pci_device_id
> 0xffff)
241 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
245 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
246 wined3d_settings
.pci_device_id
= pci_device_id
;
249 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
251 int pci_vendor_id
= tmpvalue
;
253 /* A pci device id is 16-bit */
254 if(pci_vendor_id
> 0xffff)
256 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
260 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
261 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
264 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
266 int TmpVideoMemorySize
= atoi(buffer
);
267 if(TmpVideoMemorySize
> 0)
269 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
270 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
272 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
275 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
277 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
279 size_t len
= strlen(buffer
) + 1;
281 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
282 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
283 else memcpy(wined3d_settings
.logo
, buffer
, len
);
285 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
287 if (!strcmp(buffer
, "disabled"))
289 TRACE("Multisampling disabled.\n");
290 wined3d_settings
.allow_multisampling
= FALSE
;
293 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
294 && !strcmp(buffer
,"enabled"))
296 TRACE("Enforcing strict draw ordering.\n");
297 wined3d_settings
.strict_draw_ordering
= TRUE
;
299 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
300 && !strcmp(buffer
,"disabled"))
302 TRACE("Not always rendering backbuffers offscreen.\n");
303 wined3d_settings
.always_offscreen
= FALSE
;
305 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
306 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
307 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
308 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
309 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
310 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
311 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
312 && !strcmp(buffer
, "gdi"))
314 TRACE("Disabling 3D support.\n");
315 wined3d_settings
.no_3d
= TRUE
;
319 if (appkey
) RegCloseKey( appkey
);
320 if (hkey
) RegCloseKey( hkey
);
325 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
327 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
330 if (!TlsFree(wined3d_context_tls_idx
))
332 DWORD err
= GetLastError();
333 ERR("Failed to free context TLS index, err %#x.\n", err
);
336 for (i
= 0; i
< wndproc_table
.count
; ++i
)
338 /* Trying to unregister these would be futile. These entries can only
339 * exist if either we skipped them in wined3d_unregister_window() due
340 * to the application replacing the wndproc after the entry was
341 * registered, or if the application still has an active wined3d
342 * device. In the latter case the application has bigger problems than
344 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
346 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
348 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
349 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
351 DeleteCriticalSection(&wined3d_wndproc_cs
);
352 DeleteCriticalSection(&wined3d_cs
);
356 void WINAPI
wined3d_mutex_lock(void)
358 EnterCriticalSection(&wined3d_cs
);
361 void WINAPI
wined3d_mutex_unlock(void)
363 LeaveCriticalSection(&wined3d_cs
);
366 static void wined3d_wndproc_mutex_lock(void)
368 EnterCriticalSection(&wined3d_wndproc_cs
);
371 static void wined3d_wndproc_mutex_unlock(void)
373 LeaveCriticalSection(&wined3d_wndproc_cs
);
376 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
380 for (i
= 0; i
< wndproc_table
.count
; ++i
)
382 if (wndproc_table
.entries
[i
].window
== window
)
384 return &wndproc_table
.entries
[i
];
391 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
393 struct wined3d_wndproc
*entry
;
394 struct wined3d_device
*device
;
398 wined3d_wndproc_mutex_lock();
399 entry
= wined3d_find_wndproc(window
);
403 wined3d_wndproc_mutex_unlock();
404 ERR("Window %p is not registered with wined3d.\n", window
);
405 return DefWindowProcW(window
, message
, wparam
, lparam
);
408 device
= entry
->device
;
409 unicode
= entry
->unicode
;
411 wined3d_wndproc_mutex_unlock();
414 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
416 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
417 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
420 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
422 struct wined3d_wndproc
*entry
;
424 wined3d_wndproc_mutex_lock();
426 if (wined3d_find_wndproc(window
))
428 wined3d_wndproc_mutex_unlock();
429 WARN("Window %p is already registered with wined3d.\n", window
);
433 if (wndproc_table
.size
== wndproc_table
.count
)
435 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
436 struct wined3d_wndproc
*new_entries
;
438 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
439 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
443 wined3d_wndproc_mutex_unlock();
444 ERR("Failed to grow table.\n");
448 wndproc_table
.entries
= new_entries
;
449 wndproc_table
.size
= new_size
;
452 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
453 entry
->window
= window
;
454 entry
->unicode
= IsWindowUnicode(window
);
455 /* Set a window proc that matches the window. Some applications (e.g. NoX)
456 * replace the window proc after we've set ours, and expect to be able to
457 * call the previous one (ours) directly, without using CallWindowProc(). */
459 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
461 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
462 entry
->device
= device
;
464 wined3d_wndproc_mutex_unlock();
469 void wined3d_unregister_window(HWND window
)
471 struct wined3d_wndproc
*entry
, *last
;
474 wined3d_wndproc_mutex_lock();
476 if (!(entry
= wined3d_find_wndproc(window
)))
478 wined3d_wndproc_mutex_unlock();
479 ERR("Window %p is not registered with wined3d.\n", window
);
485 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
486 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
488 entry
->device
= NULL
;
489 wined3d_wndproc_mutex_unlock();
490 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
491 window
, proc
, wined3d_wndproc
);
495 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
499 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
500 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
502 entry
->device
= NULL
;
503 wined3d_wndproc_mutex_unlock();
504 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
505 window
, proc
, wined3d_wndproc
);
509 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
512 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
513 if (entry
!= last
) *entry
= *last
;
515 wined3d_wndproc_mutex_unlock();
518 /* At process attach */
519 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
523 case DLL_PROCESS_ATTACH
:
524 return wined3d_dll_init(inst
);
526 case DLL_PROCESS_DETACH
:
528 return wined3d_dll_destroy(inst
);
531 case DLL_THREAD_DETACH
:
532 if (!context_set_current(NULL
))
534 ERR("Failed to clear current context.\n");