wined3d: Enable WINED3DFMT_R16G16B16A16_UNORM.
[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  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25
26 #include "initguid.h"
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 int num_lock = 0;
32 void (*CDECL wine_tsx11_lock_ptr)(void) = NULL;
33 void (*CDECL wine_tsx11_unlock_ptr)(void) = NULL;
34
35
36 /* When updating default value here, make sure to update winecfg as well,
37  * where appropriate. */
38 wined3d_settings_t wined3d_settings = 
39 {
40     VS_HW,          /* Hardware by default */
41     PS_HW,          /* Hardware by default */
42     VBO_HW,         /* Hardware by default */
43     TRUE,           /* Use of GLSL enabled by default */
44     ORM_FBO,        /* Use FBOs to do offscreen rendering */
45     RTL_AUTO,       /* Automatically determine best locking method */
46     PCI_VENDOR_NONE,/* PCI Vendor ID */
47     PCI_DEVICE_NONE,/* PCI Device ID */
48     0,              /* The default of memory is set in FillGLCaps */
49     NULL,           /* No wine logo by default */
50     FALSE           /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
51 };
52
53 IWineD3D* WINAPI WineDirect3DCreate(UINT dxVersion, IUnknown *parent) {
54     IWineD3DImpl* object;
55
56     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
57     object->lpVtbl = &IWineD3D_Vtbl;
58     object->dxVersion = dxVersion;
59     object->ref = 1;
60     object->parent = parent;
61
62     if (!InitAdapters(object))
63     {
64         WARN("Failed to initialize direct3d adapters, Direct3D will not be available\n");
65         if (dxVersion > 7)
66         {
67             ERR("Direct3D%d is not available without opengl\n", dxVersion);
68             HeapFree(GetProcessHeap(), 0, object);
69             return NULL;
70         }
71     }
72
73     TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
74
75     return (IWineD3D *)object;
76 }
77
78 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
79 {
80     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
81     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
82     return ERROR_FILE_NOT_FOUND;
83 }
84
85 static inline DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char* name, DWORD *data)
86 {
87     DWORD type;
88     DWORD size = sizeof(DWORD);
89     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
90     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, &type, (LPBYTE) data, &size ) && (type == REG_DWORD)) return 0;
91     return ERROR_FILE_NOT_FOUND;
92 }
93
94 static void CDECL wined3d_do_nothing(void)
95 {
96 }
97
98 static BOOL wined3d_init(HINSTANCE hInstDLL)
99 {
100     DWORD wined3d_context_tls_idx;
101     HMODULE mod;
102     char buffer[MAX_PATH+10];
103     DWORD size = sizeof(buffer);
104     HKEY hkey = 0;
105     HKEY appkey = 0;
106     DWORD len, tmpvalue;
107     WNDCLASSA wc;
108
109     wined3d_context_tls_idx = TlsAlloc();
110     if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
111     {
112         DWORD err = GetLastError();
113         ERR("Failed to allocate context TLS index, err %#x.\n", err);
114         return FALSE;
115     }
116     context_set_tls_idx(wined3d_context_tls_idx);
117
118     /* We need our own window class for a fake window which we use to retrieve GL capabilities */
119     /* We might need CS_OWNDC in the future if we notice strange things on Windows.
120      * Various articles/posts about OpenGL problems on Windows recommend this. */
121     wc.style                = CS_HREDRAW | CS_VREDRAW;
122     wc.lpfnWndProc          = DefWindowProcA;
123     wc.cbClsExtra           = 0;
124     wc.cbWndExtra           = 0;
125     wc.hInstance            = hInstDLL;
126     wc.hIcon                = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
127     wc.hCursor              = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
128     wc.hbrBackground        = NULL;
129     wc.lpszMenuName         = NULL;
130     wc.lpszClassName        = WINED3D_OPENGL_WINDOW_CLASS_NAME;
131
132     if (!RegisterClassA(&wc))
133     {
134         ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
135         if (!TlsFree(wined3d_context_tls_idx))
136         {
137             DWORD err = GetLastError();
138             ERR("Failed to free context TLS index, err %#x.\n", err);
139         }
140         return FALSE;
141     }
142
143     DisableThreadLibraryCalls(hInstDLL);
144
145     mod = GetModuleHandleA( "winex11.drv" );
146     if (mod)
147     {
148         wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
149         wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
150     }
151     else /* We are most likely on Windows */
152     {
153         wine_tsx11_lock_ptr   = wined3d_do_nothing;
154         wine_tsx11_unlock_ptr = wined3d_do_nothing;
155     }
156     /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
157     if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
158
159     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
160     if (len && len < MAX_PATH)
161     {
162         HKEY tmpkey;
163         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
164         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
165         {
166             char *p, *appname = buffer;
167             if ((p = strrchr( appname, '/' ))) appname = p + 1;
168             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
169             strcat( appname, "\\Direct3D" );
170             TRACE("appname = [%s]\n", appname);
171             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
172             RegCloseKey( tmpkey );
173         }
174     }
175
176     if ( 0 != hkey || 0 != appkey )
177     {
178         if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
179         {
180             if (!strcmp(buffer,"none"))
181             {
182                 TRACE("Disable vertex shaders\n");
183                 wined3d_settings.vs_mode = VS_NONE;
184             }
185         }
186         if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
187         {
188             if (!strcmp(buffer,"enabled"))
189             {
190                 TRACE("Allow pixel shaders\n");
191                 wined3d_settings.ps_mode = PS_HW;
192             }
193             if (!strcmp(buffer,"disabled"))
194             {
195                 TRACE("Disable pixel shaders\n");
196                 wined3d_settings.ps_mode = PS_NONE;
197             }
198         }
199         if ( !get_config_key( hkey, appkey, "VertexBufferMode", buffer, size) )
200         {
201             if (!strcmp(buffer,"none"))
202             {
203                 TRACE("Disable Vertex Buffer Hardware support\n");
204                 wined3d_settings.vbo_mode = VBO_NONE;
205             }
206             else if (!strcmp(buffer,"hardware"))
207             {
208                 TRACE("Allow Vertex Buffer Hardware support\n");
209                 wined3d_settings.vbo_mode = VBO_HW;
210             }
211         }
212         if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
213         {
214             if (!strcmp(buffer,"disabled"))
215             {
216                 TRACE("Use of GL Shading Language disabled\n");
217                 wined3d_settings.glslRequested = FALSE;
218             }
219         }
220         if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
221         {
222             if (!strcmp(buffer,"backbuffer"))
223             {
224                 TRACE("Using the backbuffer for offscreen rendering\n");
225                 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
226             }
227             else if (!strcmp(buffer,"pbuffer"))
228             {
229                 TRACE("Using PBuffers for offscreen rendering\n");
230                 wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
231             }
232             else if (!strcmp(buffer,"fbo"))
233             {
234                 TRACE("Using FBOs for offscreen rendering\n");
235                 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
236             }
237         }
238         if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
239         {
240             if (!strcmp(buffer,"disabled"))
241             {
242                 TRACE("Disabling render target locking\n");
243                 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
244             }
245             else if (!strcmp(buffer,"readdraw"))
246             {
247                 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
248                 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
249             }
250             else if (!strcmp(buffer,"readtex"))
251             {
252                 TRACE("Using glReadPixels for render target reading and textures for writing\n");
253                 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
254             }
255             else if (!strcmp(buffer,"texdraw"))
256             {
257                 TRACE("Using textures for render target reading and glDrawPixels for writing\n");
258                 wined3d_settings.rendertargetlock_mode = RTL_TEXDRAW;
259             }
260             else if (!strcmp(buffer,"textex"))
261             {
262                 TRACE("Reading render targets via textures and writing via textures\n");
263                 wined3d_settings.rendertargetlock_mode = RTL_TEXTEX;
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             wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, strlen(buffer) + 1);
312             if(wined3d_settings.logo) strcpy(wined3d_settings.logo, buffer);
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.vbo_mode == VBO_NONE)
328         TRACE("Disable Vertex Buffer Hardware support\n");
329     if (wined3d_settings.glslRequested)
330         TRACE("If supported by your system, GL Shading Language will be used\n");
331
332     if (appkey) RegCloseKey( appkey );
333     if (hkey) RegCloseKey( hkey );
334
335     return TRUE;
336 }
337
338 static BOOL wined3d_destroy(HINSTANCE hInstDLL)
339 {
340     DWORD wined3d_context_tls_idx = context_get_tls_idx();
341
342     if (!TlsFree(wined3d_context_tls_idx))
343     {
344         DWORD err = GetLastError();
345         ERR("Failed to free context TLS index, err %#x.\n", err);
346     }
347
348     HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
349     UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
350
351     return TRUE;
352 }
353
354 /* At process attach */
355 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
356 {
357     TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
358
359     switch (fdwReason)
360     {
361         case DLL_PROCESS_ATTACH:
362             return wined3d_init(hInstDLL);
363
364         case DLL_PROCESS_DETACH:
365             return wined3d_destroy(hInstDLL);
366
367         case DLL_THREAD_DETACH:
368         {
369             if (!context_set_current(NULL))
370             {
371                 ERR("Failed to clear current context.\n");
372             }
373             return TRUE;
374         }
375
376         default:
377             return TRUE;
378     }
379 }