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