directx.c 12.9 KB
Newer Older
1 2 3
/*
 * IDirect3D8 implementation
 *
4
 * Copyright 2002-2004 Jason Edmeades
5
 * Copyright 2003-2004 Raphael Junqueira
6
 * Copyright 2004 Christian Costa
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22
 */

23 24
#include "config.h"

25 26
#include <stdarg.h>

27 28
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
29 30 31
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
32
#include "winuser.h"
33
#include "wine/debug.h"
34
#include "wine/unicode.h"
35 36 37

#include "d3d8_private.h"

38
WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39

40
static inline struct d3d8 *impl_from_IDirect3D8(IDirect3D8 *iface)
41
{
42
    return CONTAINING_RECORD(iface, struct d3d8, IDirect3D8_iface);
43 44
}

45
static HRESULT WINAPI d3d8_QueryInterface(IDirect3D8 *iface, REFIID riid, void **out)
46
{
47
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
Henri Verbeet's avatar
Henri Verbeet committed
48

49 50 51
    if (IsEqualGUID(riid, &IID_IDirect3D8)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
52
        IUnknown_AddRef(iface);
53
        *out = iface;
H. Verbeet's avatar
H. Verbeet committed
54
        return S_OK;
55 56
    }

57 58
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

59
    *out = NULL;
60 61 62
    return E_NOINTERFACE;
}

63
static ULONG WINAPI d3d8_AddRef(IDirect3D8 *iface)
64
{
65 66
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
    ULONG refcount = InterlockedIncrement(&d3d8->refcount);
67

68
    TRACE("%p increasing refcount to %u.\n", iface, refcount);
69

70
    return refcount;
71 72
}

73
static ULONG WINAPI d3d8_Release(IDirect3D8 *iface)
74
{
75 76
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
    ULONG refcount = InterlockedDecrement(&d3d8->refcount);
77

78
    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
79

80 81
    if (!refcount)
    {
82
        wined3d_mutex_lock();
83
        wined3d_decref(d3d8->wined3d);
84 85
        wined3d_mutex_unlock();

86
        HeapFree(GetProcessHeap(), 0, d3d8);
87
    }
88

89
    return refcount;
90 91
}

92
static HRESULT WINAPI d3d8_RegisterSoftwareDevice(IDirect3D8 *iface, void *init_function)
93
{
94
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
95
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
96

97
    TRACE("iface %p, init_function %p.\n", iface, init_function);
98

99
    wined3d_mutex_lock();
100
    hr = wined3d_register_software_device(d3d8->wined3d, init_function);
101 102
    wined3d_mutex_unlock();

103
    return hr;
104 105
}

106
static UINT WINAPI d3d8_GetAdapterCount(IDirect3D8 *iface)
107
{
108
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
109
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
110 111

    TRACE("iface %p.\n", iface);
112

113
    wined3d_mutex_lock();
114
    hr = wined3d_get_adapter_count(d3d8->wined3d);
115 116
    wined3d_mutex_unlock();

117
    return hr;
118 119
}

120 121
static HRESULT WINAPI d3d8_GetAdapterIdentifier(IDirect3D8 *iface, UINT adapter,
        DWORD flags, D3DADAPTER_IDENTIFIER8 *identifier)
122
{
123
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
124
    struct wined3d_adapter_identifier adapter_id;
125
    HRESULT hr;
126

Henri Verbeet's avatar
Henri Verbeet committed
127
    TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
128
            iface, adapter, flags, identifier);
129

130 131 132 133
    adapter_id.driver = identifier->Driver;
    adapter_id.driver_size = sizeof(identifier->Driver);
    adapter_id.description = identifier->Description;
    adapter_id.description_size = sizeof(identifier->Description);
134 135 136
    adapter_id.device_name = NULL; /* d3d9 only */
    adapter_id.device_name_size = 0; /* d3d9 only */

137
    wined3d_mutex_lock();
138
    hr = wined3d_get_adapter_identifier(d3d8->wined3d, adapter, flags, &adapter_id);
139
    wined3d_mutex_unlock();
140

141 142 143 144 145 146 147
    identifier->DriverVersion = adapter_id.driver_version;
    identifier->VendorId = adapter_id.vendor_id;
    identifier->DeviceId = adapter_id.device_id;
    identifier->SubSysId = adapter_id.subsystem_id;
    identifier->Revision = adapter_id.revision;
    memcpy(&identifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(identifier->DeviceIdentifier));
    identifier->WHQLLevel = adapter_id.whql_level;
148

149
    return hr;
150 151
}

152
static UINT WINAPI d3d8_GetAdapterModeCount(IDirect3D8 *iface, UINT adapter)
153
{
154
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
155
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
156

157
    TRACE("iface %p, adapter %u.\n", iface, adapter);
158

159
    wined3d_mutex_lock();
160 161
    hr = wined3d_get_adapter_mode_count(d3d8->wined3d, adapter,
            WINED3DFMT_UNKNOWN, WINED3D_SCANLINE_ORDERING_UNKNOWN);
162 163
    wined3d_mutex_unlock();

164
    return hr;
165 166
}

167
static HRESULT WINAPI d3d8_EnumAdapterModes(IDirect3D8 *iface, UINT adapter, UINT mode_idx, D3DDISPLAYMODE *mode)
168
{
169
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
170
    struct wined3d_display_mode wined3d_mode;
171
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
172 173

    TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
174
            iface, adapter, mode_idx, mode);
175

176
    wined3d_mutex_lock();
177 178
    hr = wined3d_enum_adapter_modes(d3d8->wined3d, adapter, WINED3DFMT_UNKNOWN,
            WINED3D_SCANLINE_ORDERING_UNKNOWN, mode_idx, &wined3d_mode);
179
    wined3d_mutex_unlock();
180

181
    if (SUCCEEDED(hr))
182 183 184 185 186 187
    {
        mode->Width = wined3d_mode.width;
        mode->Height = wined3d_mode.height;
        mode->RefreshRate = wined3d_mode.refresh_rate;
        mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
    }
188

189
    return hr;
190 191
}

192
static HRESULT WINAPI d3d8_GetAdapterDisplayMode(IDirect3D8 *iface, UINT adapter, D3DDISPLAYMODE *mode)
193
{
194
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
195
    struct wined3d_display_mode wined3d_mode;
196
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
197 198

    TRACE("iface %p, adapter %u, mode %p.\n",
199
            iface, adapter, mode);
200

201
    wined3d_mutex_lock();
202
    hr = wined3d_get_adapter_display_mode(d3d8->wined3d, adapter, &wined3d_mode, NULL);
203
    wined3d_mutex_unlock();
204

205
    if (SUCCEEDED(hr))
206 207 208 209 210 211
    {
        mode->Width = wined3d_mode.width;
        mode->Height = wined3d_mode.height;
        mode->RefreshRate = wined3d_mode.refresh_rate;
        mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
    }
212

213
    return hr;
214 215
}

216 217
static HRESULT WINAPI d3d8_CheckDeviceType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
        D3DFORMAT display_format, D3DFORMAT backbuffer_format, BOOL windowed)
218
{
219
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
220
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
221 222

    TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
223
            iface, adapter, device_type, display_format, backbuffer_format, windowed);
224

225
    wined3d_mutex_lock();
226 227
    hr = wined3d_check_device_type(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(display_format),
            wined3dformat_from_d3dformat(backbuffer_format), windowed);
228 229
    wined3d_mutex_unlock();

230
    return hr;
231 232
}

233 234
static HRESULT WINAPI d3d8_CheckDeviceFormat(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
        D3DFORMAT adapter_format, DWORD usage, D3DRESOURCETYPE resource_type, D3DFORMAT format)
235
{
236
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
237
    enum wined3d_resource_type wined3d_rtype;
238
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
239 240

    TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
241
            iface, adapter, device_type, adapter_format, usage, resource_type, format);
242

243 244
    switch (resource_type)
    {
245 246
        case D3DRTYPE_VERTEXBUFFER:
        case D3DRTYPE_INDEXBUFFER:
247
            wined3d_rtype = WINED3D_RTYPE_BUFFER;
248 249 250
            break;

        default:
251
            wined3d_rtype = resource_type;
252 253 254
            break;
    }

255
    wined3d_mutex_lock();
256 257
    hr = wined3d_check_device_format(d3d8->wined3d, adapter, device_type, wined3dformat_from_d3dformat(adapter_format),
            usage, wined3d_rtype, wined3dformat_from_d3dformat(format), WINED3D_SURFACE_TYPE_OPENGL);
258 259
    wined3d_mutex_unlock();

260
    return hr;
261 262
}

263 264
static HRESULT WINAPI d3d8_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
        D3DFORMAT format, BOOL windowed, D3DMULTISAMPLE_TYPE multisample_type)
Henri Verbeet's avatar
Henri Verbeet committed
265
{
266
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
267
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
268 269

    TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
270
            iface, adapter, device_type, format, windowed, multisample_type);
271

272
    wined3d_mutex_lock();
273 274 275
    hr = wined3d_check_device_multisample_type(d3d8->wined3d, adapter, device_type,
            wined3dformat_from_d3dformat(format), windowed,
            (enum wined3d_multisample_type)multisample_type, NULL);
276 277
    wined3d_mutex_unlock();

278
    return hr;
279 280
}

281 282
static HRESULT WINAPI d3d8_CheckDepthStencilMatch(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type,
        D3DFORMAT adapter_format, D3DFORMAT rt_format, D3DFORMAT ds_format)
Henri Verbeet's avatar
Henri Verbeet committed
283
{
284
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
285
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
286 287

    TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
288
            iface, adapter, device_type, adapter_format, rt_format, ds_format);
289

290
    wined3d_mutex_lock();
291 292 293
    hr = wined3d_check_depth_stencil_match(d3d8->wined3d, adapter, device_type,
            wined3dformat_from_d3dformat(adapter_format), wined3dformat_from_d3dformat(rt_format),
            wined3dformat_from_d3dformat(ds_format));
294 295
    wined3d_mutex_unlock();

296
    return hr;
297 298
}

299 300 301
void fixup_caps(WINED3DCAPS *caps)
{
    /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
302
    if (caps->PixelShaderVersion)
303
        caps->PixelShaderVersion = D3DPS_VERSION(1,4);
304 305 306
    else
        caps->PixelShaderVersion = D3DPS_VERSION(0,0);
    if (caps->VertexShaderVersion)
307
        caps->VertexShaderVersion = D3DVS_VERSION(1,1);
308 309
    else
        caps->VertexShaderVersion = D3DVS_VERSION(0,0);
310 311 312 313 314
    caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);

    caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
}

315
static HRESULT WINAPI d3d8_GetDeviceCaps(IDirect3D8 *iface, UINT adapter, D3DDEVTYPE device_type, D3DCAPS8 *caps)
316
{
317 318 319
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
    WINED3DCAPS *wined3d_caps;
    HRESULT hr;
320

321
    TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, adapter, device_type, caps);
322

323 324 325 326
    if (!caps)
        return D3DERR_INVALIDCALL;

    if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
327
        return D3DERR_INVALIDCALL;
328 329

    wined3d_mutex_lock();
330
    hr = wined3d_get_device_caps(d3d8->wined3d, adapter, device_type, wined3d_caps);
331 332
    wined3d_mutex_unlock();

333 334 335
    fixup_caps(wined3d_caps);
    WINECAPSTOD3D8CAPS(caps, wined3d_caps)
    HeapFree(GetProcessHeap(), 0, wined3d_caps);
336

337
    return hr;
338 339
}

340
static HMONITOR WINAPI d3d8_GetAdapterMonitor(IDirect3D8 *iface, UINT adapter)
341
{
342
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
343
    HMONITOR ret;
Henri Verbeet's avatar
Henri Verbeet committed
344

345
    TRACE("iface %p, adapter %u.\n", iface, adapter);
346

347
    wined3d_mutex_lock();
348
    ret = wined3d_get_adapter_monitor(d3d8->wined3d, adapter);
349 350
    wined3d_mutex_unlock();

351
    return ret;
352 353
}

354
static HRESULT WINAPI d3d8_CreateDevice(IDirect3D8 *iface, UINT adapter,
355 356
        D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
        IDirect3DDevice8 **device)
357
{
358
    struct d3d8 *d3d8 = impl_from_IDirect3D8(iface);
359
    struct d3d8_device *object;
360
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
361 362

    TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
363
            iface, adapter, device_type, focus_window, flags, parameters, device);
364

365 366 367 368 369
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate device memory.\n");
        return E_OUTOFMEMORY;
370
    }
371

372
    hr = device_init(object, d3d8, d3d8->wined3d, adapter, device_type, focus_window, flags, parameters);
373 374 375
    if (FAILED(hr))
    {
        WARN("Failed to initialize device, hr %#x.\n", hr);
376 377 378 379
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

380
    TRACE("Created device %p.\n", object);
381
    *device = &object->IDirect3DDevice8_iface;
382

383
    return D3D_OK;
384 385
}

386
static const struct IDirect3D8Vtbl d3d8_vtbl =
387
{
388
    /* IUnknown */
389 390 391
    d3d8_QueryInterface,
    d3d8_AddRef,
    d3d8_Release,
392
    /* IDirect3D8 */
393 394 395 396 397 398 399 400 401 402 403 404 405
    d3d8_RegisterSoftwareDevice,
    d3d8_GetAdapterCount,
    d3d8_GetAdapterIdentifier,
    d3d8_GetAdapterModeCount,
    d3d8_EnumAdapterModes,
    d3d8_GetAdapterDisplayMode,
    d3d8_CheckDeviceType,
    d3d8_CheckDeviceFormat,
    d3d8_CheckDeviceMultiSampleType,
    d3d8_CheckDepthStencilMatch,
    d3d8_GetDeviceCaps,
    d3d8_GetAdapterMonitor,
    d3d8_CreateDevice,
406
};
407

408
BOOL d3d8_init(struct d3d8 *d3d8)
409
{
410 411
    d3d8->IDirect3D8_iface.lpVtbl = &d3d8_vtbl;
    d3d8->refcount = 1;
412 413

    wined3d_mutex_lock();
414
    d3d8->wined3d = wined3d_create(8, WINED3D_LEGACY_DEPTH_BIAS);
415
    wined3d_mutex_unlock();
416
    if (!d3d8->wined3d)
417 418 419 420
        return FALSE;

    return TRUE;
}