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>
28 //#include "initguid.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 /* Do not call while under the GL lock. */
91 struct wined3d
* CDECL
wined3d_create(UINT version
, 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 (version
== 7 && wined3d_settings
.no_3d
)
104 flags
|= WINED3D_NO3D
;
106 hr
= wined3d_init(object
, version
, flags
);
109 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
110 HeapFree(GetProcessHeap(), 0, object
);
114 TRACE("Created wined3d object %p for d3d%d support.\n", object
, version
);
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
, (LPCSTR
)IDI_WINLOGO
);
163 wc
.hCursor
= LoadCursorA(NULL
, (LPCSTR
)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( hkey
, appkey
, "UseGLSL", buffer
, size
) )
205 if (!strcmp(buffer
,"disabled"))
207 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
208 TRACE("Use of GL Shading Language disabled\n");
209 wined3d_settings
.glslRequested
= FALSE
;
212 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
214 if (!strcmp(buffer
,"backbuffer"))
216 TRACE("Using the backbuffer for offscreen rendering\n");
217 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
219 else if (!strcmp(buffer
,"fbo"))
221 TRACE("Using FBOs for offscreen rendering\n");
222 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
225 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
227 int pci_device_id
= tmpvalue
;
229 /* A pci device id is 16-bit */
230 if(pci_device_id
> 0xffff)
232 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
236 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
237 wined3d_settings
.pci_device_id
= pci_device_id
;
240 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
242 int pci_vendor_id
= tmpvalue
;
244 /* A pci device id is 16-bit */
245 if(pci_vendor_id
> 0xffff)
247 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
251 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
252 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
255 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
257 int TmpVideoMemorySize
= atoi(buffer
);
258 if(TmpVideoMemorySize
> 0)
260 wined3d_settings
.emulated_textureram
= TmpVideoMemorySize
*1024*1024;
261 TRACE("Use %iMB = %d byte for emulated_textureram\n",
263 wined3d_settings
.emulated_textureram
);
266 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
268 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
270 size_t len
= strlen(buffer
) + 1;
272 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
273 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
274 else memcpy(wined3d_settings
.logo
, buffer
, len
);
276 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
278 if (!strcmp(buffer
, "disabled"))
280 TRACE("Multisampling disabled.\n");
281 wined3d_settings
.allow_multisampling
= FALSE
;
284 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
285 && !strcmp(buffer
,"enabled"))
287 TRACE("Enforcing strict draw ordering.\n");
288 wined3d_settings
.strict_draw_ordering
= TRUE
;
290 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
291 && !strcmp(buffer
,"disabled"))
293 TRACE("Not always rendering backbuffers offscreen.\n");
294 wined3d_settings
.always_offscreen
= FALSE
;
296 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
297 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
298 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
299 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
300 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
301 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
302 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
303 && !strcmp(buffer
, "gdi"))
305 TRACE("Disabling 3D support.\n");
306 wined3d_settings
.no_3d
= TRUE
;
310 if (appkey
) RegCloseKey( appkey
);
311 if (hkey
) RegCloseKey( hkey
);
316 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
318 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
321 if (!TlsFree(wined3d_context_tls_idx
))
323 DWORD err
= GetLastError();
324 ERR("Failed to free context TLS index, err %#x.\n", err
);
327 for (i
= 0; i
< wndproc_table
.count
; ++i
)
329 /* Trying to unregister these would be futile. These entries can only
330 * exist if either we skipped them in wined3d_unregister_window() due
331 * to the application replacing the wndproc after the entry was
332 * registered, or if the application still has an active wined3d
333 * device. In the latter case the application has bigger problems than
335 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
337 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
339 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
340 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
342 DeleteCriticalSection(&wined3d_wndproc_cs
);
343 DeleteCriticalSection(&wined3d_cs
);
347 void WINAPI
wined3d_mutex_lock(void)
349 EnterCriticalSection(&wined3d_cs
);
352 void WINAPI
wined3d_mutex_unlock(void)
354 LeaveCriticalSection(&wined3d_cs
);
357 static void wined3d_wndproc_mutex_lock(void)
359 EnterCriticalSection(&wined3d_wndproc_cs
);
362 static void wined3d_wndproc_mutex_unlock(void)
364 LeaveCriticalSection(&wined3d_wndproc_cs
);
367 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
371 for (i
= 0; i
< wndproc_table
.count
; ++i
)
373 if (wndproc_table
.entries
[i
].window
== window
)
375 return &wndproc_table
.entries
[i
];
382 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
384 struct wined3d_wndproc
*entry
;
385 struct wined3d_device
*device
;
389 wined3d_wndproc_mutex_lock();
390 entry
= wined3d_find_wndproc(window
);
394 wined3d_wndproc_mutex_unlock();
395 ERR("Window %p is not registered with wined3d.\n", window
);
396 return DefWindowProcW(window
, message
, wparam
, lparam
);
399 device
= entry
->device
;
400 unicode
= entry
->unicode
;
402 wined3d_wndproc_mutex_unlock();
405 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
407 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
408 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
411 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
413 struct wined3d_wndproc
*entry
;
415 wined3d_wndproc_mutex_lock();
417 if (wined3d_find_wndproc(window
))
419 wined3d_wndproc_mutex_unlock();
420 WARN("Window %p is already registered with wined3d.\n", window
);
424 if (wndproc_table
.size
== wndproc_table
.count
)
426 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
427 struct wined3d_wndproc
*new_entries
;
429 if (!wndproc_table
.entries
) new_entries
= HeapAlloc(GetProcessHeap(), 0, new_size
* sizeof(*new_entries
));
430 else new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
434 wined3d_wndproc_mutex_unlock();
435 ERR("Failed to grow table.\n");
439 wndproc_table
.entries
= new_entries
;
440 wndproc_table
.size
= new_size
;
443 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
444 entry
->window
= window
;
445 entry
->unicode
= IsWindowUnicode(window
);
446 /* Set a window proc that matches the window. Some applications (e.g. NoX)
447 * replace the window proc after we've set ours, and expect to be able to
448 * call the previous one (ours) directly, without using CallWindowProc(). */
450 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
452 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
453 entry
->device
= device
;
455 wined3d_wndproc_mutex_unlock();
460 void wined3d_unregister_window(HWND window
)
462 struct wined3d_wndproc
*entry
, *last
;
465 wined3d_wndproc_mutex_lock();
467 if (!(entry
= wined3d_find_wndproc(window
)))
469 wined3d_wndproc_mutex_unlock();
470 ERR("Window %p is not registered with wined3d.\n", window
);
476 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
477 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
479 entry
->device
= NULL
;
480 wined3d_wndproc_mutex_unlock();
481 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
482 window
, proc
, wined3d_wndproc
);
486 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
490 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
491 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
493 entry
->device
= NULL
;
494 wined3d_wndproc_mutex_unlock();
495 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
496 window
, proc
, wined3d_wndproc
);
500 SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
503 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
504 if (entry
!= last
) *entry
= *last
;
506 wined3d_wndproc_mutex_unlock();
509 /* At process attach */
510 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID lpv
)
512 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason
);
516 case DLL_PROCESS_ATTACH
:
517 return wined3d_dll_init(hInstDLL
);
519 case DLL_PROCESS_DETACH
:
521 return wined3d_dll_destroy(hInstDLL
);
523 case DLL_THREAD_DETACH
:
524 if (!context_set_current(NULL
))
526 ERR("Failed to clear current context.\n");