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