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