[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / wined3d / wined3d_main.c
1 /*
2 * Direct3D wine internal interface main
3 *
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
9 *
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.
14 *
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.
19 *
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
23 */
24
25 #include "wined3d_private.h"
26
27 #include <winreg.h>
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31
32 struct wined3d_wndproc
33 {
34 HWND window;
35 BOOL unicode;
36 WNDPROC proc;
37 struct wined3d_device *device;
38 };
39
40 struct wined3d_wndproc_table
41 {
42 struct wined3d_wndproc *entries;
43 unsigned int count;
44 SIZE_T size;
45 };
46
47 static struct wined3d_wndproc_table wndproc_table;
48
49 static CRITICAL_SECTION wined3d_cs;
50 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
51 {
52 0, 0, &wined3d_cs,
53 {&wined3d_cs_debug.ProcessLocksList,
54 &wined3d_cs_debug.ProcessLocksList},
55 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
56 };
57 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
58
59 static CRITICAL_SECTION wined3d_wndproc_cs;
60 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
61 {
62 0, 0, &wined3d_wndproc_cs,
63 {&wined3d_wndproc_cs_debug.ProcessLocksList,
64 &wined3d_wndproc_cs_debug.ProcessLocksList},
65 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
66 };
67 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
68
69 /* When updating default value here, make sure to update winecfg as well,
70 * where appropriate. */
71 struct wined3d_settings wined3d_settings =
72 {
73 #if !defined(STAGING_CSMT)
74 FALSE, /* No multithreaded CS by default. */
75 #else /* STAGING_CSMT */
76 TRUE, /* Multithreaded CS by default. */
77 #endif /* STAGING_CSMT */
78 MAKEDWORD_VERSION(1, 0), /* Default to legacy OpenGL */
79 TRUE, /* Use of GLSL enabled by default */
80 ORM_FBO, /* Use FBOs to do offscreen rendering */
81 PCI_VENDOR_NONE,/* PCI Vendor ID */
82 PCI_DEVICE_NONE,/* PCI Device ID */
83 0, /* The default of memory is set in init_driver_info */
84 NULL, /* No wine logo by default */
85 ~0u, /* Don't force a specific sample count by default. */
86 FALSE, /* No strict draw ordering. */
87 FALSE, /* Don't range check relative addressing indices in float constants. */
88 ~0U, /* No VS shader model limit by default. */
89 ~0U, /* No HS shader model limit by default. */
90 ~0U, /* No DS shader model limit by default. */
91 ~0U, /* No GS shader model limit by default. */
92 ~0U, /* No PS shader model limit by default. */
93 ~0u, /* No CS shader model limit by default. */
94 FALSE, /* 3D support enabled by default. */
95 };
96
97 struct wined3d * CDECL wined3d_create(DWORD flags)
98 {
99 struct wined3d *object;
100 HRESULT hr;
101
102 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(struct wined3d, adapters[1]));
103 if (!object)
104 {
105 ERR("Failed to allocate wined3d object memory.\n");
106 return NULL;
107 }
108
109 if (wined3d_settings.no_3d)
110 flags |= WINED3D_NO3D;
111
112 hr = wined3d_init(object, flags);
113 if (FAILED(hr))
114 {
115 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
116 HeapFree(GetProcessHeap(), 0, object);
117 return NULL;
118 }
119
120 TRACE("Created wined3d object %p.\n", object);
121
122 return object;
123 }
124
125 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
126 {
127 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
128 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
129 return ERROR_FILE_NOT_FOUND;
130 }
131
132 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *value)
133 {
134 DWORD type, data, size;
135
136 size = sizeof(data);
137 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
138 size = sizeof(data);
139 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
140
141 return ERROR_FILE_NOT_FOUND;
142
143 success:
144 *value = data;
145 return 0;
146 }
147
148 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
149 {
150 DWORD wined3d_context_tls_idx;
151 char buffer[MAX_PATH+10];
152 DWORD size = sizeof(buffer);
153 HKEY hkey = 0;
154 HKEY appkey = 0;
155 DWORD len, tmpvalue;
156 WNDCLASSA wc;
157
158 wined3d_context_tls_idx = TlsAlloc();
159 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
160 {
161 DWORD err = GetLastError();
162 ERR("Failed to allocate context TLS index, err %#x.\n", err);
163 return FALSE;
164 }
165 context_set_tls_idx(wined3d_context_tls_idx);
166
167 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
168 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
169 * Various articles/posts about OpenGL problems on Windows recommend this. */
170 wc.style = CS_HREDRAW | CS_VREDRAW;
171 wc.lpfnWndProc = DefWindowProcA;
172 wc.cbClsExtra = 0;
173 wc.cbWndExtra = 0;
174 wc.hInstance = hInstDLL;
175 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
176 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
177 wc.hbrBackground = NULL;
178 wc.lpszMenuName = NULL;
179 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
180
181 if (!RegisterClassA(&wc))
182 {
183 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
184 if (!TlsFree(wined3d_context_tls_idx))
185 {
186 DWORD err = GetLastError();
187 ERR("Failed to free context TLS index, err %#x.\n", err);
188 }
189 return FALSE;
190 }
191
192 DisableThreadLibraryCalls(hInstDLL);
193
194 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
195 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
196
197 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
198 if (len && len < MAX_PATH)
199 {
200 HKEY tmpkey;
201 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
202 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
203 {
204 char *p, *appname = buffer;
205 if ((p = strrchr( appname, '/' ))) appname = p + 1;
206 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
207 strcat( appname, "\\Direct3D" );
208 TRACE("appname = [%s]\n", appname);
209 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
210 RegCloseKey( tmpkey );
211 }
212 }
213
214 if (hkey || appkey)
215 {
216 if (!get_config_key_dword(hkey, appkey, "csmt", &wined3d_settings.cs_multithreaded))
217 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
218 if (!get_config_key_dword(hkey, appkey, "MaxVersionGL", &tmpvalue))
219 {
220 if (tmpvalue != wined3d_settings.max_gl_version)
221 {
222 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
223 tmpvalue >> 16, tmpvalue & 0xffff);
224 wined3d_settings.max_gl_version = tmpvalue;
225 }
226 }
227 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
228 {
229 if (!strcmp(buffer,"disabled"))
230 {
231 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
232 TRACE("Use of GL Shading Language disabled\n");
233 wined3d_settings.glslRequested = FALSE;
234 }
235 }
236 if (!get_config_key(hkey, appkey, "OffscreenRenderingMode", buffer, size)
237 && !strcmp(buffer,"backbuffer"))
238 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
239 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
240 {
241 int pci_device_id = tmpvalue;
242
243 /* A pci device id is 16-bit */
244 if(pci_device_id > 0xffff)
245 {
246 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
247 }
248 else
249 {
250 TRACE("Using PCI Device ID %04x\n", pci_device_id);
251 wined3d_settings.pci_device_id = pci_device_id;
252 }
253 }
254 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
255 {
256 int pci_vendor_id = tmpvalue;
257
258 /* A pci device id is 16-bit */
259 if(pci_vendor_id > 0xffff)
260 {
261 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
262 }
263 else
264 {
265 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
266 wined3d_settings.pci_vendor_id = pci_vendor_id;
267 }
268 }
269 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
270 {
271 int TmpVideoMemorySize = atoi(buffer);
272 if(TmpVideoMemorySize > 0)
273 {
274 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
275 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
276 TmpVideoMemorySize,
277 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
278 }
279 else
280 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
281 }
282 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
283 {
284 size_t len = strlen(buffer) + 1;
285
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);
289 }
290 if (!get_config_key_dword(hkey, appkey, "SampleCount", &wined3d_settings.sample_count))
291 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
292 wined3d_settings.sample_count);
293 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
294 && !strcmp(buffer,"enabled"))
295 {
296 ERR_(winediag)("\"StrictDrawOrdering\" is deprecated, please use \"csmt\" instead.\n");
297 TRACE("Enforcing strict draw ordering.\n");
298 wined3d_settings.strict_draw_ordering = TRUE;
299 }
300 if (!get_config_key(hkey, appkey, "CheckFloatConstants", buffer, size)
301 && !strcmp(buffer, "enabled"))
302 {
303 TRACE("Checking relative addressing indices in float constants.\n");
304 wined3d_settings.check_float_constants = TRUE;
305 }
306 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
307 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
308 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
309 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
310 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
311 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
312 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
313 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
314 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
315 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
316 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
317 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
318 if (!get_config_key(hkey, appkey, "DirectDrawRenderer", buffer, size)
319 && !strcmp(buffer, "gdi"))
320 {
321 TRACE("Disabling 3D support.\n");
322 wined3d_settings.no_3d = TRUE;
323 }
324 }
325
326 if (appkey) RegCloseKey( appkey );
327 if (hkey) RegCloseKey( hkey );
328
329 wined3d_dxtn_init();
330
331 return TRUE;
332 }
333
334 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
335 {
336 DWORD wined3d_context_tls_idx = context_get_tls_idx();
337 unsigned int i;
338
339 if (!TlsFree(wined3d_context_tls_idx))
340 {
341 DWORD err = GetLastError();
342 ERR("Failed to free context TLS index, err %#x.\n", err);
343 }
344
345 for (i = 0; i < wndproc_table.count; ++i)
346 {
347 /* Trying to unregister these would be futile. These entries can only
348 * exist if either we skipped them in wined3d_unregister_window() due
349 * to the application replacing the wndproc after the entry was
350 * registered, or if the application still has an active wined3d
351 * device. In the latter case the application has bigger problems than
352 * these entries. */
353 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
354 }
355 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
356
357 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
358 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
359
360 DeleteCriticalSection(&wined3d_wndproc_cs);
361 DeleteCriticalSection(&wined3d_cs);
362
363 wined3d_dxtn_free();
364
365 return TRUE;
366 }
367
368 void WINAPI wined3d_mutex_lock(void)
369 {
370 EnterCriticalSection(&wined3d_cs);
371 }
372
373 void WINAPI wined3d_mutex_unlock(void)
374 {
375 LeaveCriticalSection(&wined3d_cs);
376 }
377
378 static void wined3d_wndproc_mutex_lock(void)
379 {
380 EnterCriticalSection(&wined3d_wndproc_cs);
381 }
382
383 static void wined3d_wndproc_mutex_unlock(void)
384 {
385 LeaveCriticalSection(&wined3d_wndproc_cs);
386 }
387
388 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
389 {
390 unsigned int i;
391
392 for (i = 0; i < wndproc_table.count; ++i)
393 {
394 if (wndproc_table.entries[i].window == window)
395 {
396 return &wndproc_table.entries[i];
397 }
398 }
399
400 return NULL;
401 }
402
403 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
404 {
405 struct wined3d_wndproc *entry;
406 struct wined3d_device *device;
407 BOOL unicode;
408 WNDPROC proc;
409
410 wined3d_wndproc_mutex_lock();
411 entry = wined3d_find_wndproc(window);
412
413 if (!entry)
414 {
415 wined3d_wndproc_mutex_unlock();
416 ERR("Window %p is not registered with wined3d.\n", window);
417 return DefWindowProcW(window, message, wparam, lparam);
418 }
419
420 device = entry->device;
421 unicode = entry->unicode;
422 proc = entry->proc;
423 wined3d_wndproc_mutex_unlock();
424
425 if (device)
426 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
427 if (unicode)
428 return CallWindowProcW(proc, window, message, wparam, lparam);
429 return CallWindowProcA(proc, window, message, wparam, lparam);
430 }
431
432 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
433 {
434 struct wined3d_wndproc *entry;
435
436 wined3d_wndproc_mutex_lock();
437
438 if (wined3d_find_wndproc(window))
439 {
440 wined3d_wndproc_mutex_unlock();
441 WARN("Window %p is already registered with wined3d.\n", window);
442 return TRUE;
443 }
444
445 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
446 wndproc_table.count + 1, sizeof(*entry)))
447 {
448 wined3d_wndproc_mutex_unlock();
449 ERR("Failed to grow table.\n");
450 return FALSE;
451 }
452
453 entry = &wndproc_table.entries[wndproc_table.count++];
454 entry->window = window;
455 entry->unicode = IsWindowUnicode(window);
456 /* Set a window proc that matches the window. Some applications (e.g. NoX)
457 * replace the window proc after we've set ours, and expect to be able to
458 * call the previous one (ours) directly, without using CallWindowProc(). */
459 if (entry->unicode)
460 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
461 else
462 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
463 entry->device = device;
464
465 wined3d_wndproc_mutex_unlock();
466
467 return TRUE;
468 }
469
470 void wined3d_unregister_window(HWND window)
471 {
472 struct wined3d_wndproc *entry, *last;
473 LONG_PTR proc;
474
475 wined3d_wndproc_mutex_lock();
476
477 if (!(entry = wined3d_find_wndproc(window)))
478 {
479 wined3d_wndproc_mutex_unlock();
480 ERR("Window %p is not registered with wined3d.\n", window);
481 return;
482 }
483
484 if (entry->unicode)
485 {
486 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
487 if (proc != (LONG_PTR)wined3d_wndproc)
488 {
489 entry->device = NULL;
490 wined3d_wndproc_mutex_unlock();
491 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
492 window, proc, wined3d_wndproc);
493 return;
494 }
495
496 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
497 }
498 else
499 {
500 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
501 if (proc != (LONG_PTR)wined3d_wndproc)
502 {
503 entry->device = NULL;
504 wined3d_wndproc_mutex_unlock();
505 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
506 window, proc, wined3d_wndproc);
507 return;
508 }
509
510 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
511 }
512
513 last = &wndproc_table.entries[--wndproc_table.count];
514 if (entry != last) *entry = *last;
515
516 wined3d_wndproc_mutex_unlock();
517 }
518
519 void CDECL wined3d_strictdrawing_set(int value)
520 {
521 wined3d_settings.strict_draw_ordering = value;
522 }
523
524 /* At process attach */
525 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
526 {
527 switch (reason)
528 {
529 case DLL_PROCESS_ATTACH:
530 return wined3d_dll_init(inst);
531
532 case DLL_PROCESS_DETACH:
533 if (!reserved)
534 return wined3d_dll_destroy(inst);
535 break;
536
537 case DLL_THREAD_DETACH:
538 if (!context_set_current(NULL))
539 {
540 ERR("Failed to clear current context.\n");
541 }
542 return TRUE;
543 }
544 return TRUE;
545 }