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