wined3d: Pass a const adapter to the format caps checking functions.
[wine] / dlls / 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     IWineD3DDeviceImpl *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 wined3d_settings_t 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 IWineD3D * WINAPI WineDirect3DCreate(UINT version, void *parent)
82 {
83     IWineD3DImpl *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 (IWineD3D *)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         struct wined3d_wndproc *entry = &wndproc_table.entries[i];
359         SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
360     }
361     HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
362
363     HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
364     UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
365
366     return TRUE;
367 }
368
369 void WINAPI wined3d_mutex_lock(void)
370 {
371     EnterCriticalSection(&wined3d_cs);
372 }
373
374 void WINAPI wined3d_mutex_unlock(void)
375 {
376     LeaveCriticalSection(&wined3d_cs);
377 }
378
379 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
380 {
381     unsigned int i;
382
383     for (i = 0; i < wndproc_table.count; ++i)
384     {
385         if (wndproc_table.entries[i].window == window)
386         {
387             return &wndproc_table.entries[i];
388         }
389     }
390
391     return NULL;
392 }
393
394 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
395 {
396     struct wined3d_wndproc *entry;
397     IWineD3DDeviceImpl *device;
398     BOOL unicode;
399     WNDPROC proc;
400
401     wined3d_mutex_lock();
402     entry = wined3d_find_wndproc(window);
403
404     if (!entry)
405     {
406         wined3d_mutex_unlock();
407         ERR("Window %p is not registered with wined3d.\n", window);
408         return DefWindowProcW(window, message, wparam, lparam);
409     }
410
411     device = entry->device;
412     unicode = entry->unicode;
413     proc = entry->proc;
414     wined3d_mutex_unlock();
415
416     return device_process_message(device, window, unicode, message, wparam, lparam, proc);
417 }
418
419 BOOL wined3d_register_window(HWND window, IWineD3DDeviceImpl *device)
420 {
421     struct wined3d_wndproc *entry;
422
423     wined3d_mutex_lock();
424
425     if (wined3d_find_wndproc(window))
426     {
427         wined3d_mutex_unlock();
428         WARN("Window %p is already registered with wined3d.\n", window);
429         return TRUE;
430     }
431
432     if (wndproc_table.size == wndproc_table.count)
433     {
434         unsigned int new_size = max(1, wndproc_table.size * 2);
435         struct wined3d_wndproc *new_entries;
436
437         if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
438         else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
439
440         if (!new_entries)
441         {
442             wined3d_mutex_unlock();
443             ERR("Failed to grow table.\n");
444             return FALSE;
445         }
446
447         wndproc_table.entries = new_entries;
448         wndproc_table.size = new_size;
449     }
450
451     entry = &wndproc_table.entries[wndproc_table.count++];
452     entry->window = window;
453     entry->unicode = IsWindowUnicode(window);
454     /* Set a window proc that matches the window. Some applications (e.g. NoX)
455      * replace the window proc after we've set ours, and expect to be able to
456      * call the previous one (ours) directly, without using CallWindowProc(). */
457     if (entry->unicode)
458         entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
459     else
460         entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
461     entry->device = device;
462
463     wined3d_mutex_unlock();
464
465     return TRUE;
466 }
467
468 void wined3d_unregister_window(HWND window)
469 {
470     struct wined3d_wndproc *entry, *last;
471     LONG_PTR proc;
472
473     wined3d_mutex_lock();
474
475     if (!(entry = wined3d_find_wndproc(window)))
476     {
477         wined3d_mutex_unlock();
478         ERR("Window %p is not registered with wined3d.\n", window);
479         return;
480     }
481
482     if (entry->unicode)
483     {
484         proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
485         if (proc != (LONG_PTR)wined3d_wndproc)
486         {
487             wined3d_mutex_unlock();
488             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
489                     window, proc, wined3d_wndproc);
490             return;
491         }
492
493         SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
494     }
495     else
496     {
497         proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
498         if (proc != (LONG_PTR)wined3d_wndproc)
499         {
500             wined3d_mutex_unlock();
501             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
502                     window, proc, wined3d_wndproc);
503             return;
504         }
505
506         SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
507     }
508
509     last = &wndproc_table.entries[--wndproc_table.count];
510     if (entry != last) *entry = *last;
511
512     wined3d_mutex_unlock();
513 }
514
515 /* At process attach */
516 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
517 {
518     TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
519
520     switch (fdwReason)
521     {
522         case DLL_PROCESS_ATTACH:
523             return wined3d_dll_init(hInstDLL);
524
525         case DLL_PROCESS_DETACH:
526             return wined3d_dll_destroy(hInstDLL);
527
528         case DLL_THREAD_DETACH:
529         {
530             if (!context_set_current(NULL))
531             {
532                 ERR("Failed to clear current context.\n");
533             }
534             return TRUE;
535         }
536
537         default:
538             return TRUE;
539     }
540 }