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 ~0u, /* Don't force a specific sample count by default. */
84 FALSE
, /* No strict draw ordering. */
85 TRUE
, /* Don't try to render onscreen by default. */
86 FALSE
, /* Don't range check relative addressing indices in float constants. */
87 ~0U, /* No VS shader model limit by default. */
88 ~0U, /* No HS shader model limit by default. */
89 ~0U, /* No DS shader model limit by default. */
90 ~0U, /* No GS shader model limit by default. */
91 ~0U, /* No PS shader model limit by default. */
92 ~0u, /* No CS shader model limit by default. */
93 FALSE
, /* 3D support enabled by default. */
96 struct wined3d
* CDECL
wined3d_create(DWORD flags
)
98 struct wined3d
*object
;
101 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, FIELD_OFFSET(struct wined3d
, adapters
[1]));
104 ERR("Failed to allocate wined3d object memory.\n");
108 if (wined3d_settings
.no_3d
)
109 flags
|= WINED3D_NO3D
;
111 hr
= wined3d_init(object
, flags
);
114 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
115 HeapFree(GetProcessHeap(), 0, object
);
119 TRACE("Created wined3d object %p.\n", object
);
124 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
126 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
127 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
128 return ERROR_FILE_NOT_FOUND
;
131 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*data
)
134 DWORD size
= sizeof(DWORD
);
135 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
136 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)data
, &size
) && (type
== REG_DWORD
)) return 0;
137 return ERROR_FILE_NOT_FOUND
;
140 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
142 DWORD wined3d_context_tls_idx
;
143 char buffer
[MAX_PATH
+10];
144 DWORD size
= sizeof(buffer
);
150 wined3d_context_tls_idx
= TlsAlloc();
151 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
153 DWORD err
= GetLastError();
154 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
157 context_set_tls_idx(wined3d_context_tls_idx
);
159 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
160 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
161 * Various articles/posts about OpenGL problems on Windows recommend this. */
162 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
163 wc
.lpfnWndProc
= DefWindowProcA
;
166 wc
.hInstance
= hInstDLL
;
167 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
168 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
169 wc
.hbrBackground
= NULL
;
170 wc
.lpszMenuName
= NULL
;
171 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
173 if (!RegisterClassA(&wc
))
175 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
176 if (!TlsFree(wined3d_context_tls_idx
))
178 DWORD err
= GetLastError();
179 ERR("Failed to free context TLS index, err %#x.\n", err
);
184 DisableThreadLibraryCalls(hInstDLL
);
186 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
187 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
189 len
= GetModuleFileNameA( 0, buffer
, MAX_PATH
);
190 if (len
&& len
< MAX_PATH
)
193 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
194 if (!RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
196 char *p
, *appname
= buffer
;
197 if ((p
= strrchr( appname
, '/' ))) appname
= p
+ 1;
198 if ((p
= strrchr( appname
, '\\' ))) appname
= p
+ 1;
199 strcat( appname
, "\\Direct3D" );
200 TRACE("appname = [%s]\n", appname
);
201 if (RegOpenKeyA( tmpkey
, appname
, &appkey
)) appkey
= 0;
202 RegCloseKey( tmpkey
);
208 if (!get_config_key_dword(hkey
, appkey
, "MaxVersionGL", &tmpvalue
))
210 if (tmpvalue
!= wined3d_settings
.max_gl_version
)
212 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
213 tmpvalue
>> 16, tmpvalue
& 0xffff);
214 wined3d_settings
.max_gl_version
= tmpvalue
;
217 if ( !get_config_key( hkey
, appkey
, "UseGLSL", buffer
, size
) )
219 if (!strcmp(buffer
,"disabled"))
221 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
222 TRACE("Use of GL Shading Language disabled\n");
223 wined3d_settings
.glslRequested
= FALSE
;
226 if ( !get_config_key( hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
) )
228 if (!strcmp(buffer
,"backbuffer"))
230 TRACE("Using the backbuffer for offscreen rendering\n");
231 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
233 else if (!strcmp(buffer
,"fbo"))
235 TRACE("Using FBOs for offscreen rendering\n");
236 wined3d_settings
.offscreen_rendering_mode
= ORM_FBO
;
239 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
241 int pci_device_id
= tmpvalue
;
243 /* A pci device id is 16-bit */
244 if(pci_device_id
> 0xffff)
246 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
250 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
251 wined3d_settings
.pci_device_id
= pci_device_id
;
254 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
256 int pci_vendor_id
= tmpvalue
;
258 /* A pci device id is 16-bit */
259 if(pci_vendor_id
> 0xffff)
261 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
265 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
266 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
269 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
271 int TmpVideoMemorySize
= atoi(buffer
);
272 if(TmpVideoMemorySize
> 0)
274 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
275 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
277 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
280 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
282 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
284 size_t len
= strlen(buffer
) + 1;
286 wined3d_settings
.logo
= HeapAlloc(GetProcessHeap(), 0, len
);
287 if (!wined3d_settings
.logo
) ERR("Failed to allocate logo path memory.\n");
288 else memcpy(wined3d_settings
.logo
, buffer
, len
);
290 if ( !get_config_key( hkey
, appkey
, "Multisampling", buffer
, size
) )
292 if (!strcmp(buffer
, "disabled"))
294 TRACE("Multisampling disabled.\n");
295 wined3d_settings
.allow_multisampling
= FALSE
;
298 if (!get_config_key_dword(hkey
, appkey
, "SampleCount", &wined3d_settings
.sample_count
))
299 ERR_(winediag
)("Forcing sample count to %u. This may not be compatible with all applications.\n",
300 wined3d_settings
.sample_count
);
301 if (!get_config_key(hkey
, appkey
, "StrictDrawOrdering", buffer
, size
)
302 && !strcmp(buffer
,"enabled"))
304 TRACE("Enforcing strict draw ordering.\n");
305 wined3d_settings
.strict_draw_ordering
= TRUE
;
307 if (!get_config_key(hkey
, appkey
, "AlwaysOffscreen", buffer
, size
)
308 && !strcmp(buffer
,"disabled"))
310 TRACE("Not always rendering backbuffers offscreen.\n");
311 wined3d_settings
.always_offscreen
= FALSE
;
313 if (!get_config_key(hkey
, appkey
, "CheckFloatConstants", buffer
, size
)
314 && !strcmp(buffer
, "enabled"))
316 TRACE("Checking relative addressing indices in float constants.\n");
317 wined3d_settings
.check_float_constants
= TRUE
;
319 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
320 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
321 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelHS", &wined3d_settings
.max_sm_hs
))
322 TRACE("Limiting HS shader model to %u.\n", wined3d_settings
.max_sm_hs
);
323 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelDS", &wined3d_settings
.max_sm_ds
))
324 TRACE("Limiting DS shader model to %u.\n", wined3d_settings
.max_sm_ds
);
325 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
326 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
327 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
328 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
329 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelCS", &wined3d_settings
.max_sm_cs
))
330 TRACE("Limiting CS shader model to %u.\n", wined3d_settings
.max_sm_cs
);
331 if (!get_config_key(hkey
, appkey
, "DirectDrawRenderer", buffer
, size
)
332 && !strcmp(buffer
, "gdi"))
334 TRACE("Disabling 3D support.\n");
335 wined3d_settings
.no_3d
= TRUE
;
339 if (appkey
) RegCloseKey( appkey
);
340 if (hkey
) RegCloseKey( hkey
);
345 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
347 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
350 if (!TlsFree(wined3d_context_tls_idx
))
352 DWORD err
= GetLastError();
353 ERR("Failed to free context TLS index, err %#x.\n", err
);
356 for (i
= 0; i
< wndproc_table
.count
; ++i
)
358 /* Trying to unregister these would be futile. These entries can only
359 * exist if either we skipped them in wined3d_unregister_window() due
360 * to the application replacing the wndproc after the entry was
361 * registered, or if the application still has an active wined3d
362 * device. In the latter case the application has bigger problems than
364 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
366 HeapFree(GetProcessHeap(), 0, wndproc_table
.entries
);
368 HeapFree(GetProcessHeap(), 0, wined3d_settings
.logo
);
369 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
371 DeleteCriticalSection(&wined3d_wndproc_cs
);
372 DeleteCriticalSection(&wined3d_cs
);
376 void WINAPI
wined3d_mutex_lock(void)
378 EnterCriticalSection(&wined3d_cs
);
381 void WINAPI
wined3d_mutex_unlock(void)
383 LeaveCriticalSection(&wined3d_cs
);
386 static void wined3d_wndproc_mutex_lock(void)
388 EnterCriticalSection(&wined3d_wndproc_cs
);
391 static void wined3d_wndproc_mutex_unlock(void)
393 LeaveCriticalSection(&wined3d_wndproc_cs
);
396 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
)
400 for (i
= 0; i
< wndproc_table
.count
; ++i
)
402 if (wndproc_table
.entries
[i
].window
== window
)
404 return &wndproc_table
.entries
[i
];
411 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
413 struct wined3d_wndproc
*entry
;
414 struct wined3d_device
*device
;
418 wined3d_wndproc_mutex_lock();
419 entry
= wined3d_find_wndproc(window
);
423 wined3d_wndproc_mutex_unlock();
424 ERR("Window %p is not registered with wined3d.\n", window
);
425 return DefWindowProcW(window
, message
, wparam
, lparam
);
428 device
= entry
->device
;
429 unicode
= entry
->unicode
;
431 wined3d_wndproc_mutex_unlock();
434 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
436 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
437 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
440 BOOL
wined3d_register_window(HWND window
, struct wined3d_device
*device
)
442 struct wined3d_wndproc
*entry
;
444 wined3d_wndproc_mutex_lock();
446 if (wined3d_find_wndproc(window
))
448 wined3d_wndproc_mutex_unlock();
449 WARN("Window %p is already registered with wined3d.\n", window
);
453 if (wndproc_table
.size
== wndproc_table
.count
)
455 unsigned int new_size
= max(1, wndproc_table
.size
* 2);
456 struct wined3d_wndproc
*new_entries
;
458 if (!wndproc_table
.entries
)
459 new_entries
= wined3d_calloc(new_size
, sizeof(*new_entries
));
461 new_entries
= HeapReAlloc(GetProcessHeap(), 0, wndproc_table
.entries
, new_size
* sizeof(*new_entries
));
465 wined3d_wndproc_mutex_unlock();
466 ERR("Failed to grow table.\n");
470 wndproc_table
.entries
= new_entries
;
471 wndproc_table
.size
= new_size
;
474 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
475 entry
->window
= window
;
476 entry
->unicode
= IsWindowUnicode(window
);
477 /* Set a window proc that matches the window. Some applications (e.g. NoX)
478 * replace the window proc after we've set ours, and expect to be able to
479 * call the previous one (ours) directly, without using CallWindowProc(). */
481 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
483 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
484 entry
->device
= device
;
486 wined3d_wndproc_mutex_unlock();
491 void wined3d_unregister_window(HWND window
)
493 struct wined3d_wndproc
*entry
, *last
;
496 wined3d_wndproc_mutex_lock();
498 if (!(entry
= wined3d_find_wndproc(window
)))
500 wined3d_wndproc_mutex_unlock();
501 ERR("Window %p is not registered with wined3d.\n", window
);
507 proc
= GetWindowLongPtrW(window
, GWLP_WNDPROC
);
508 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
510 entry
->device
= NULL
;
511 wined3d_wndproc_mutex_unlock();
512 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
513 window
, proc
, wined3d_wndproc
);
517 SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
521 proc
= GetWindowLongPtrA(window
, GWLP_WNDPROC
);
522 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
524 entry
->device
= NULL
;
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 inst
, DWORD reason
, void *reserved
)
545 case DLL_PROCESS_ATTACH
:
546 return wined3d_dll_init(inst
);
548 case DLL_PROCESS_DETACH
:
550 return wined3d_dll_destroy(inst
);
553 case DLL_THREAD_DETACH
:
554 if (!context_set_current(NULL
))
556 ERR("Failed to clear current context.\n");