wined3d_main.c 18.4 KB
Newer Older
1 2 3 4 5
/*
 * Direct3D wine internal interface main
 *
 * Copyright 2002-2003 The wine-d3d team
 * 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 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 26
 */

#include "config.h"

27
#include "initguid.h"
28 29
#include "wined3d_private.h"

30
WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31
WINE_DECLARE_DEBUG_CHANNEL(winediag);
32

33 34 35
struct wined3d_wndproc
{
    HWND window;
36
    BOOL unicode;
37
    WNDPROC proc;
38
    struct wined3d_device *device;
39 40 41 42 43 44 45 46 47 48 49
};

struct wined3d_wndproc_table
{
    struct wined3d_wndproc *entries;
    unsigned int count;
    unsigned int size;
};

static struct wined3d_wndproc_table wndproc_table;

50
int num_lock = 0;
51 52
void (CDECL *wine_tsx11_lock_ptr)(void) = NULL;
void (CDECL *wine_tsx11_unlock_ptr)(void) = NULL;
53

54
static CRITICAL_SECTION wined3d_cs;
55 56 57 58 59 60 61
static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
{
    0, 0, &wined3d_cs,
    {&wined3d_cs_debug.ProcessLocksList,
    &wined3d_cs_debug.ProcessLocksList},
    0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
};
62
static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
63

64 65 66 67 68 69 70 71 72 73
static CRITICAL_SECTION wined3d_wndproc_cs;
static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
{
    0, 0, &wined3d_wndproc_cs,
    {&wined3d_wndproc_cs_debug.ProcessLocksList,
    &wined3d_wndproc_cs_debug.ProcessLocksList},
    0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
};
static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};

74 75
/* When updating default value here, make sure to update winecfg as well,
 * where appropriate. */
76
struct wined3d_settings wined3d_settings =
77
{
78
    VS_HW,          /* Hardware by default */
79
    PS_HW,          /* Hardware by default */
80
    TRUE,           /* Use of GLSL enabled by default */
81
    ORM_FBO,        /* Use FBOs to do offscreen rendering */
82
    RTL_READTEX,    /* Default render target locking method */
83
    PCI_VENDOR_NONE,/* PCI Vendor ID */
84
    PCI_DEVICE_NONE,/* PCI Device ID */
85
    0,              /* The default of memory is set in init_driver_info */
86
    NULL,           /* No wine logo by default */
87 88
    FALSE,          /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
    FALSE,          /* No strict draw ordering. */
89
};
90

91
/* Do not call while under the GL lock. */
92
struct wined3d * CDECL wined3d_create(UINT version, DWORD flags, void *parent)
93
{
94
    struct wined3d *object;
95
    HRESULT hr;
96

97 98 99 100 101 102
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate wined3d object memory.\n");
        return NULL;
    }
103

104
    hr = wined3d_init(object, version, flags, parent);
105
    if (FAILED(hr))
106
    {
107 108 109
        WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return NULL;
110 111
    }

112
    TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
113

114
    return object;
115 116
}

117
static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
118
{
119 120
    if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
    if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
121 122 123
    return ERROR_FILE_NOT_FOUND;
}

124
static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
125 126 127
{
    DWORD type;
    DWORD size = sizeof(DWORD);
128 129
    if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
    if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
130 131 132
    return ERROR_FILE_NOT_FOUND;
}

133
static void CDECL wined3d_do_nothing(void)
134 135 136
{
}

137
static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
138
{
139
    DWORD wined3d_context_tls_idx;
140 141 142 143 144 145 146
    HMODULE mod;
    char buffer[MAX_PATH+10];
    DWORD size = sizeof(buffer);
    HKEY hkey = 0;
    HKEY appkey = 0;
    DWORD len, tmpvalue;
    WNDCLASSA wc;
147

148 149 150 151 152 153 154 155 156
    wined3d_context_tls_idx = TlsAlloc();
    if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
    {
        DWORD err = GetLastError();
        ERR("Failed to allocate context TLS index, err %#x.\n", err);
        return FALSE;
    }
    context_set_tls_idx(wined3d_context_tls_idx);

157 158 159 160 161 162 163 164 165 166 167 168
    /* We need our own window class for a fake window which we use to retrieve GL capabilities */
    /* We might need CS_OWNDC in the future if we notice strange things on Windows.
     * Various articles/posts about OpenGL problems on Windows recommend this. */
    wc.style                = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc          = DefWindowProcA;
    wc.cbClsExtra           = 0;
    wc.cbWndExtra           = 0;
    wc.hInstance            = hInstDLL;
    wc.hIcon                = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
    wc.hCursor              = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
    wc.hbrBackground        = NULL;
    wc.lpszMenuName         = NULL;
169
    wc.lpszClassName        = WINED3D_OPENGL_WINDOW_CLASS_NAME;
170

171
    if (!RegisterClassA(&wc))
172 173
    {
        ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
174 175 176 177 178
        if (!TlsFree(wined3d_context_tls_idx))
        {
            DWORD err = GetLastError();
            ERR("Failed to free context TLS index, err %#x.\n", err);
        }
179 180
        return FALSE;
    }
181

182
    DisableThreadLibraryCalls(hInstDLL);
183

184 185 186 187 188 189 190 191 192 193 194 195 196
    mod = GetModuleHandleA( "winex11.drv" );
    if (mod)
    {
        wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
        wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
    }
    else /* We are most likely on Windows */
    {
        wine_tsx11_lock_ptr   = wined3d_do_nothing;
        wine_tsx11_unlock_ptr = wined3d_do_nothing;
    }
    /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
    if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    len = GetModuleFileNameA( 0, buffer, MAX_PATH );
    if (len && len < MAX_PATH)
    {
        HKEY tmpkey;
        /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
        if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
        {
            char *p, *appname = buffer;
            if ((p = strrchr( appname, '/' ))) appname = p + 1;
            if ((p = strrchr( appname, '\\' ))) appname = p + 1;
            strcat( appname, "\\Direct3D" );
            TRACE("appname = [%s]\n", appname);
            if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
            RegCloseKey( tmpkey );
        }
    }

215
    if (hkey || appkey)
216 217 218 219
    {
        if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
        {
            if (!strcmp(buffer,"none"))
220
            {
221 222
                TRACE("Disable vertex shaders\n");
                wined3d_settings.vs_mode = VS_NONE;
223
            }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        }
        if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
        {
            if (!strcmp(buffer,"enabled"))
            {
                TRACE("Allow pixel shaders\n");
                wined3d_settings.ps_mode = PS_HW;
            }
            if (!strcmp(buffer,"disabled"))
            {
                TRACE("Disable pixel shaders\n");
                wined3d_settings.ps_mode = PS_NONE;
            }
        }
        if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
        {
            if (!strcmp(buffer,"disabled"))
            {
242
                ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
243 244 245 246 247 248 249 250 251 252 253 254
                TRACE("Use of GL Shading Language disabled\n");
                wined3d_settings.glslRequested = FALSE;
            }
        }
        if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
        {
            if (!strcmp(buffer,"backbuffer"))
            {
                TRACE("Using the backbuffer for offscreen rendering\n");
                wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
            }
            else if (!strcmp(buffer,"fbo"))
255
            {
256 257
                TRACE("Using FBOs for offscreen rendering\n");
                wined3d_settings.offscreen_rendering_mode = ORM_FBO;
258
            }
259 260 261 262
        }
        if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
        {
            if (!strcmp(buffer,"disabled"))
263
            {
264 265
                TRACE("Disabling render target locking\n");
                wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
266
            }
267
            else if (!strcmp(buffer,"readdraw"))
268
            {
269 270
                TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
                wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
271
            }
272
            else if (!strcmp(buffer,"readtex"))
273
            {
274 275
                TRACE("Using glReadPixels for render target reading and textures for writing\n");
                wined3d_settings.rendertargetlock_mode = RTL_READTEX;
276
            }
277 278 279 280
        }
        if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
        {
            int pci_device_id = tmpvalue;
281

282 283 284 285
            /* A pci device id is 16-bit */
            if(pci_device_id > 0xffff)
            {
                ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
286
            }
287
            else
288
            {
289 290 291 292 293 294 295
                TRACE("Using PCI Device ID %04x\n", pci_device_id);
                wined3d_settings.pci_device_id = pci_device_id;
            }
        }
        if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
        {
            int pci_vendor_id = tmpvalue;
296

297 298 299 300
            /* A pci device id is 16-bit */
            if(pci_vendor_id > 0xffff)
            {
                ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
301
            }
302
            else
303
            {
304 305
                TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
                wined3d_settings.pci_vendor_id = pci_vendor_id;
306
            }
307 308 309 310 311
        }
        if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
        {
            int TmpVideoMemorySize = atoi(buffer);
            if(TmpVideoMemorySize > 0)
312
            {
313 314 315 316
                wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
                TRACE("Use %iMB = %d byte for emulated_textureram\n",
                        TmpVideoMemorySize,
                        wined3d_settings.emulated_textureram);
317
            }
318 319 320 321 322
            else
                ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
        }
        if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
        {
323 324 325 326 327
            size_t len = strlen(buffer) + 1;

            wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
            if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
            else memcpy(wined3d_settings.logo, buffer, len);
328 329 330 331
        }
        if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
        {
            if (!strcmp(buffer,"enabled"))
332
            {
333 334
                TRACE("Allow multisampling\n");
                wined3d_settings.allow_multisampling = TRUE;
335
            }
336
        }
337 338 339 340 341 342
        if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
                && !strcmp(buffer,"enabled"))
        {
            TRACE("Enforcing strict draw ordering.\n");
            wined3d_settings.strict_draw_ordering = TRUE;
        }
343
    }
344 345 346 347 348 349 350 351 352 353
    if (wined3d_settings.vs_mode == VS_HW)
        TRACE("Allow HW vertex shaders\n");
    if (wined3d_settings.ps_mode == PS_NONE)
        TRACE("Disable pixel shaders\n");
    if (wined3d_settings.glslRequested)
        TRACE("If supported by your system, GL Shading Language will be used\n");

    if (appkey) RegCloseKey( appkey );
    if (hkey) RegCloseKey( hkey );

354 355
    return TRUE;
}
356

357
static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
358
{
359
    DWORD wined3d_context_tls_idx = context_get_tls_idx();
360
    unsigned int i;
361 362 363 364 365 366 367

    if (!TlsFree(wined3d_context_tls_idx))
    {
        DWORD err = GetLastError();
        ERR("Failed to free context TLS index, err %#x.\n", err);
    }

368 369
    for (i = 0; i < wndproc_table.count; ++i)
    {
370 371 372 373 374 375 376
        /* Trying to unregister these would be futile. These entries can only
         * exist if either we skipped them in wined3d_unregister_window() due
         * to the application replacing the wndproc after the entry was
         * registered, or if the application still has an active wined3d
         * device. In the latter case the application has bigger problems than
         * these entries. */
        WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
377 378 379
    }
    HeapFree(GetProcessHeap(), 0, wndproc_table.entries);

380
    HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
381
    UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
382 383 384 385

    return TRUE;
}

386 387 388 389 390 391 392 393 394 395
void WINAPI wined3d_mutex_lock(void)
{
    EnterCriticalSection(&wined3d_cs);
}

void WINAPI wined3d_mutex_unlock(void)
{
    LeaveCriticalSection(&wined3d_cs);
}

396 397 398 399 400 401 402 403 404 405
static void wined3d_wndproc_mutex_lock(void)
{
    EnterCriticalSection(&wined3d_wndproc_cs);
}

static void wined3d_wndproc_mutex_unlock(void)
{
    LeaveCriticalSection(&wined3d_wndproc_cs);
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
{
    unsigned int i;

    for (i = 0; i < wndproc_table.count; ++i)
    {
        if (wndproc_table.entries[i].window == window)
        {
            return &wndproc_table.entries[i];
        }
    }

    return NULL;
}

static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
    struct wined3d_wndproc *entry;
424
    struct wined3d_device *device;
425
    BOOL unicode;
426 427
    WNDPROC proc;

428
    wined3d_wndproc_mutex_lock();
429 430 431 432
    entry = wined3d_find_wndproc(window);

    if (!entry)
    {
433
        wined3d_wndproc_mutex_unlock();
434 435 436 437
        ERR("Window %p is not registered with wined3d.\n", window);
        return DefWindowProcW(window, message, wparam, lparam);
    }

438
    device = entry->device;
439
    unicode = entry->unicode;
440
    proc = entry->proc;
441
    wined3d_wndproc_mutex_unlock();
442

443
    return device_process_message(device, window, unicode, message, wparam, lparam, proc);
444 445
}

446
BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
447 448 449
{
    struct wined3d_wndproc *entry;

450
    wined3d_wndproc_mutex_lock();
451

452 453
    if (wined3d_find_wndproc(window))
    {
454
        wined3d_wndproc_mutex_unlock();
455 456 457 458
        WARN("Window %p is already registered with wined3d.\n", window);
        return TRUE;
    }

459 460 461 462 463 464 465 466 467 468
    if (wndproc_table.size == wndproc_table.count)
    {
        unsigned int new_size = max(1, wndproc_table.size * 2);
        struct wined3d_wndproc *new_entries;

        if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
        else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));

        if (!new_entries)
        {
469
            wined3d_wndproc_mutex_unlock();
470 471 472 473 474 475 476 477 478 479
            ERR("Failed to grow table.\n");
            return FALSE;
        }

        wndproc_table.entries = new_entries;
        wndproc_table.size = new_size;
    }

    entry = &wndproc_table.entries[wndproc_table.count++];
    entry->window = window;
480 481 482 483 484 485 486 487
    entry->unicode = IsWindowUnicode(window);
    /* Set a window proc that matches the window. Some applications (e.g. NoX)
     * replace the window proc after we've set ours, and expect to be able to
     * call the previous one (ours) directly, without using CallWindowProc(). */
    if (entry->unicode)
        entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
    else
        entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
488
    entry->device = device;
489

490
    wined3d_wndproc_mutex_unlock();
491 492 493 494 495 496

    return TRUE;
}

void wined3d_unregister_window(HWND window)
{
497 498
    struct wined3d_wndproc *entry, *last;
    LONG_PTR proc;
499

500
    wined3d_wndproc_mutex_lock();
501 502

    if (!(entry = wined3d_find_wndproc(window)))
503
    {
504
        wined3d_wndproc_mutex_unlock();
505 506 507
        ERR("Window %p is not registered with wined3d.\n", window);
        return;
    }
508

509 510 511 512 513
    if (entry->unicode)
    {
        proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
        if (proc != (LONG_PTR)wined3d_wndproc)
        {
514
            wined3d_wndproc_mutex_unlock();
515 516 517 518
            WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
                    window, proc, wined3d_wndproc);
            return;
        }
519

520 521 522 523 524 525 526
        SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
    }
    else
    {
        proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
        if (proc != (LONG_PTR)wined3d_wndproc)
        {
527
            wined3d_wndproc_mutex_unlock();
528 529
            WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
                    window, proc, wined3d_wndproc);
530 531
            return;
        }
532 533

        SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
534 535
    }

536 537 538
    last = &wndproc_table.entries[--wndproc_table.count];
    if (entry != last) *entry = *last;

539
    wined3d_wndproc_mutex_unlock();
540 541
}

542 543 544 545 546 547 548 549
/* At process attach */
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
{
    TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
550
            return wined3d_dll_init(hInstDLL);
551 552

        case DLL_PROCESS_DETACH:
553
            return wined3d_dll_destroy(hInstDLL);
554

555 556 557 558 559 560 561 562 563
        case DLL_THREAD_DETACH:
        {
            if (!context_set_current(NULL))
            {
                ERR("Failed to clear current context.\n");
            }
            return TRUE;
        }

564 565 566 567
        default:
            return TRUE;
    }
}