wined3d: Start of some surface cleanup.
[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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24
25 #include "initguid.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(wine_d3d);
29
30 int num_lock = 0;
31 void (*wine_tsx11_lock_ptr)(void) = NULL;
32 void (*wine_tsx11_unlock_ptr)(void) = NULL;
33
34
35 /* When updating default value here, make sure to update winecfg as well,
36  * where appropriate. */
37 wined3d_settings_t wined3d_settings = 
38 {
39     VS_HW,          /* Hardware by default */
40     PS_HW,          /* Hardware by default */
41     VBO_HW,         /* Hardware by default */
42     FALSE,          /* Use of GLSL disabled by default */
43     ORM_BACKBUFFER, /* Use the backbuffer to do offscreen rendering */
44     RTL_AUTO,       /* Automatically determine best locking method */
45     64*1024*1024,   /* 64MB texture memory by default */
46     NULL            /* No wine logo by default */
47 };
48
49 WineD3DGlobalStatistics *wineD3DGlobalStatistics = NULL;
50
51 long globalChangeGlRam(long glram){
52     /* FIXME: replace this function with object tracking */
53     int result;
54
55     wineD3DGlobalStatistics->glsurfaceram     += glram;
56     TRACE("Adjusted gl ram by %ld to %d\n", glram, wineD3DGlobalStatistics->glsurfaceram);
57     result = wineD3DGlobalStatistics->glsurfaceram;
58     return result;
59
60 }
61
62 IWineD3D* WINAPI WineDirect3DCreate(UINT SDKVersion, UINT dxVersion, IUnknown *parent) {
63     IWineD3DImpl* object;
64
65     if (!InitAdapters()) {
66         WARN("Failed to initialize direct3d adapters, Direct3D will not be available\n");
67         if(dxVersion > 7) {
68             ERR("Direct3D%d is not available without opengl\n", dxVersion);
69             return NULL;
70         }
71     }
72
73     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DImpl));
74     object->lpVtbl = &IWineD3D_Vtbl;
75     object->dxVersion = dxVersion;
76     object->ref = 1;
77     object->parent = parent;
78
79     /*Create a structure for storing global data in*/
80     if(wineD3DGlobalStatistics == NULL){
81         TRACE("Creating global statistics store\n");
82         wineD3DGlobalStatistics = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wineD3DGlobalStatistics));
83
84     }
85
86     TRACE("Created WineD3D object @ %p for d3d%d support\n", object, dxVersion);
87
88     return (IWineD3D *)object;
89 }
90
91 static inline DWORD get_config_key(HKEY defkey, HKEY appkey, const char* name, char* buffer, DWORD size)
92 {
93     if (0 != appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
94     if (0 != defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE) buffer, &size )) return 0;
95     return ERROR_FILE_NOT_FOUND;
96 }
97
98 static void wined3d_do_nothing(void)
99 {
100 }
101
102 /* At process attach */
103 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
104 {
105     TRACE("WineD3D DLLMain Reason=%d\n", fdwReason);
106     if (fdwReason == DLL_PROCESS_ATTACH)
107     {
108        HMODULE mod;
109        char buffer[MAX_PATH+10];
110        DWORD size = sizeof(buffer);
111        HKEY hkey = 0;
112        HKEY appkey = 0;
113        DWORD len;
114        WNDCLASSA wc;
115
116        wined3d_settings.emulated_textureram = 64*1024*1024;
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";
131
132        if (!RegisterClassA(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
133        {
134            ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
135            return FALSE;
136        }
137
138        DisableThreadLibraryCalls(hInstDLL);
139
140        mod = GetModuleHandleA( "winex11.drv" );
141        if (mod)
142        {
143            wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
144            wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
145        }
146        else /* We are most likely on Windows */
147        {
148            wine_tsx11_lock_ptr   = wined3d_do_nothing;
149            wine_tsx11_unlock_ptr = wined3d_do_nothing;
150        }
151        /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
152        if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
153
154        len = GetModuleFileNameA( 0, buffer, MAX_PATH );
155        if (len && len < MAX_PATH)
156        {
157             HKEY tmpkey;
158             /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
159             if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
160             {
161                 char *p, *appname = buffer;
162                 if ((p = strrchr( appname, '/' ))) appname = p + 1;
163                 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
164                 strcat( appname, "\\Direct3D" );
165                 TRACE("appname = [%s]\n", appname);
166                 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
167                 RegCloseKey( tmpkey );
168             }
169        }
170
171        if ( 0 != hkey || 0 != appkey )
172        {
173             if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
174             {
175                 if (!strcmp(buffer,"none"))
176                 {
177                     TRACE("Disable vertex shaders\n");
178                     wined3d_settings.vs_mode = VS_NONE;
179                 }
180             }
181             if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
182             {
183                 if (!strcmp(buffer,"enabled"))
184                 {
185                     TRACE("Allow pixel shaders\n");
186                     wined3d_settings.ps_mode = PS_HW;
187                 }
188                 if (!strcmp(buffer,"disabled"))
189                 {
190                     TRACE("Disable pixel shaders\n");
191                     wined3d_settings.ps_mode = PS_NONE;
192                 }
193             }
194             if ( !get_config_key( hkey, appkey, "VertexBufferMode", buffer, size) )
195             {
196                 if (!strcmp(buffer,"none"))
197                 {
198                     TRACE("Disable Vertex Buffer Hardware support\n");
199                     wined3d_settings.vbo_mode = VBO_NONE;
200                 }
201                 else if (!strcmp(buffer,"hardware"))
202                 {
203                     TRACE("Allow Vertex Buffer Hardware support\n");
204                     wined3d_settings.vbo_mode = VBO_HW;
205                 }
206             }
207             if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
208             {
209                 if (!strcmp(buffer,"enabled"))
210                 {
211                     TRACE("Use of GL Shading Language enabled for systems that support it\n");
212                     wined3d_settings.glslRequested = TRUE;
213                 }
214                 else
215                 {
216                     TRACE("Use of GL Shading Language disabled\n");
217                 }
218             }
219             if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
220             {
221                 if (!strcmp(buffer,"backbuffer"))
222                 {
223                     TRACE("Using the backbuffer for offscreen rendering\n");
224                     wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
225                 }
226                 else if (!strcmp(buffer,"pbuffer"))
227                 {
228                     TRACE("Using PBuffers for offscreen rendering\n");
229                     wined3d_settings.offscreen_rendering_mode = ORM_PBUFFER;
230                 }
231                 else if (!strcmp(buffer,"fbo"))
232                 {
233                     TRACE("Using FBOs for offscreen rendering\n");
234                     wined3d_settings.offscreen_rendering_mode = ORM_FBO;
235                 }
236             }
237             if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
238             {
239                 if (!strcmp(buffer,"disabled"))
240                 {
241                     TRACE("Disabling render target locking\n");
242                     wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
243                 }
244                 else if (!strcmp(buffer,"readdraw"))
245                 {
246                     TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
247                     wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
248                 }
249                 else if (!strcmp(buffer,"readtex"))
250                 {
251                     TRACE("Using glReadPixels for render target reading and textures for writing\n");
252                     wined3d_settings.rendertargetlock_mode = RTL_READTEX;
253                 }
254                 else if (!strcmp(buffer,"texdraw"))
255                 {
256                     TRACE("Using textures for render target reading and glDrawPixels for writing\n");
257                     wined3d_settings.rendertargetlock_mode = RTL_TEXDRAW;
258                 }
259                 else if (!strcmp(buffer,"textex"))
260                 {
261                     TRACE("Reading render targets via textures and writing via textures\n");
262                     wined3d_settings.rendertargetlock_mode = RTL_TEXTEX;
263                 }
264             }
265             if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
266             {
267                 int TmpVideoMemorySize = atoi(buffer);
268                 if(TmpVideoMemorySize > 0)
269                 {
270                     wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
271                     TRACE("Use %iMB = %d byte for emulated_textureram\n",
272                             TmpVideoMemorySize,
273                             wined3d_settings.emulated_textureram);
274                 }
275                 else
276                     ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
277             }
278             if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
279             {
280                 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, strlen(buffer) + 1);
281                 if(wined3d_settings.logo) strcpy(wined3d_settings.logo, buffer);
282             }
283        }
284        if (wined3d_settings.vs_mode == VS_HW)
285            TRACE("Allow HW vertex shaders\n");
286        if (wined3d_settings.ps_mode == PS_NONE)
287            TRACE("Disable pixel shaders\n");
288        if (wined3d_settings.vbo_mode == VBO_NONE)
289            TRACE("Disable Vertex Buffer Hardware support\n");
290        if (wined3d_settings.glslRequested)
291            TRACE("If supported by your system, GL Shading Language will be used\n");
292
293        if (appkey) RegCloseKey( appkey );
294        if (hkey) RegCloseKey( hkey );
295     }
296     return TRUE;
297 }