device.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2008 Henri Verbeet for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include "config.h"
#include "wine/port.h"

#include "dxgi_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(dxgi);

27 28 29 30 31
static inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
{
    return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
}

32 33
/* IUnknown methods */

34
static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
35
{
36
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
37

38 39 40 41
    TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);

    if (IsEqualGUID(riid, &IID_IUnknown)
            || IsEqualGUID(riid, &IID_IDXGIObject)
42 43
            || IsEqualGUID(riid, &IID_IDXGIDevice)
            || IsEqualGUID(riid, &IID_IWineDXGIDevice))
44 45 46 47 48 49
    {
        IUnknown_AddRef(iface);
        *object = iface;
        return S_OK;
    }

50 51 52 53 54 55
    if (This->child_layer)
    {
        TRACE("forwarding to child layer %p\n", This->child_layer);
        return IUnknown_QueryInterface(This->child_layer, riid, object);
    }

56 57 58 59 60 61
    WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));

    *object = NULL;
    return E_NOINTERFACE;
}

62
static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
63
{
64
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
65 66 67 68 69 70 71
    ULONG refcount = InterlockedIncrement(&This->refcount);

    TRACE("%p increasing refcount to %u\n", This, refcount);

    return refcount;
}

72
static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
73
{
74
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
75 76 77 78 79 80
    ULONG refcount = InterlockedDecrement(&This->refcount);

    TRACE("%p decreasing refcount to %u\n", This, refcount);

    if (!refcount)
    {
81
        if (This->child_layer) IUnknown_Release(This->child_layer);
82
        EnterCriticalSection(&dxgi_cs);
83
        wined3d_device_decref(This->wined3d_device);
84 85
        LeaveCriticalSection(&dxgi_cs);
        IWineDXGIFactory_Release(This->factory);
86 87 88 89 90 91 92 93
        HeapFree(GetProcessHeap(), 0, This);
    }

    return refcount;
}

/* IDXGIObject methods */

94 95
static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
        REFGUID guid, UINT data_size, const void *data)
96 97 98 99 100 101
{
    FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);

    return E_NOTIMPL;
}

102 103
static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
        REFGUID guid, const IUnknown *object)
104 105 106 107 108 109
{
    FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);

    return E_NOTIMPL;
}

110 111
static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
        REFGUID guid, UINT *data_size, void *data)
112 113 114 115 116 117
{
    FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);

    return E_NOTIMPL;
}

118
static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
119
{
120 121
    IDXGIAdapter *adapter;
    HRESULT hr;
122

123 124 125 126 127 128 129 130 131 132 133 134 135
    TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);

    hr = IDXGIDevice_GetAdapter(iface, &adapter);
    if (FAILED(hr))
    {
        ERR("Failed to get adapter, hr %#x.\n", hr);
        return hr;
    }

    hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
    IDXGIAdapter_Release(adapter);

    return hr;
136 137 138 139
}

/* IDXGIDevice methods */

140
static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
141
{
142
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
143
    struct wined3d_device_creation_parameters create_parameters;
144
    HRESULT hr;
145

146 147 148 149
    TRACE("iface %p, adapter %p\n", iface, adapter);

    EnterCriticalSection(&dxgi_cs);

150
    hr = wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
151 152 153 154 155 156 157 158
    if (FAILED(hr))
    {
        LeaveCriticalSection(&dxgi_cs);
        return hr;
    }

    LeaveCriticalSection(&dxgi_cs);

159
    return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.adapter_idx, adapter);
160 161
}

162
static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
163 164 165
        const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
        const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
{
166 167
    struct wined3d_device_parent *device_parent;
    IWineDXGIDeviceParent *dxgi_device_parent;
168 169
    HRESULT hr;
    UINT i;
170
    UINT j;
171

172
    TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
173 174
            iface, desc, surface_count, usage, shared_resource, surface);

175
    hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
176 177 178 179 180 181
    if (FAILED(hr))
    {
        ERR("Device should implement IWineD3DDeviceParent\n");
        return E_FAIL;
    }

182 183
    device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);

184
    FIXME("Implement DXGI<->wined3d usage conversion\n");
185

186 187 188
    memset(surface, 0, surface_count * sizeof(*surface));
    for (i = 0; i < surface_count; ++i)
    {
189
        struct wined3d_surface *wined3d_surface;
190 191
        IUnknown *parent;

192
        hr = device_parent->ops->create_surface(device_parent, NULL, desc->Width, desc->Height,
193
                wined3dformat_from_dxgi_format(desc->Format), usage, WINED3D_POOL_DEFAULT, 0,
194
                WINED3D_CUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
195
        if (FAILED(hr))
196
        {
197
            ERR("CreateSurface failed, returning %#x\n", hr);
198 199 200
            goto fail;
        }

201
        parent = wined3d_surface_get_parent(wined3d_surface);
202
        hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
203
        wined3d_surface_decref(wined3d_surface);
204 205 206 207 208
        if (FAILED(hr))
        {
            ERR("Surface should implement IDXGISurface\n");
            goto fail;
        }
209

210
        TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
211
    }
212
    IWineDXGIDeviceParent_Release(dxgi_device_parent);
213 214 215 216

    return S_OK;

fail:
217
    for (j = 0; j < i; ++j)
218
    {
219
        IDXGISurface_Release(surface[i]);
220
    }
221
    IWineDXGIDeviceParent_Release(dxgi_device_parent);
222
    return hr;
223 224
}

225
static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
226 227 228 229 230 231 232 233
        IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
{
    FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
            iface, resources, residency, resource_count);

    return E_NOTIMPL;
}

234
static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
235 236 237 238 239 240
{
    FIXME("iface %p, priority %d stub!\n", iface, priority);

    return E_NOTIMPL;
}

241
static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
242 243 244 245 246 247
{
    FIXME("iface %p, priority %p stub!\n", iface, priority);

    return E_NOTIMPL;
}

248 249
/* IWineDXGIDevice methods */

250
static struct wined3d_device * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
251
{
252
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
253 254 255 256

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

    EnterCriticalSection(&dxgi_cs);
257
    wined3d_device_incref(This->wined3d_device);
258 259 260 261
    LeaveCriticalSection(&dxgi_cs);
    return This->wined3d_device;
}

262 263 264 265
static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
        DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
{
    struct dxgi_surface *object;
266
    HRESULT hr;
267 268 269 270 271 272 273 274 275 276 277

    FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
            iface, desc, usage, shared_resource, outer, surface);

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate DXGI surface object memory\n");
        return E_OUTOFMEMORY;
    }

278
    hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
279
    if (FAILED(hr))
280
    {
281 282 283
        WARN("Failed to initialize surface, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
284 285 286
    }

    TRACE("Created IDXGISurface %p\n", object);
287
    *surface = outer ? (void *)&object->inner_unknown_vtbl : object;
288 289 290 291

    return S_OK;
}

292
static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
293
        struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **wined3d_swapchain)
294
{
295
    struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
296 297 298
    struct dxgi_swapchain *object;
    HRESULT hr;

299 300
    TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
            iface, desc, wined3d_swapchain);
301

302 303 304 305 306 307 308
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        ERR("Failed to allocate DXGI swapchain object memory\n");
        return E_OUTOFMEMORY;
    }

309
    hr = dxgi_swapchain_init(object, This, desc);
310 311
    if (FAILED(hr))
    {
312
        WARN("Failed to initialize swapchain, hr %#x.\n", hr);
313 314 315 316 317
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created IDXGISwapChain %p\n", object);
318
    *wined3d_swapchain = object->wined3d_swapchain;
319 320 321 322

    return S_OK;
}

323
static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
    /* IUnknown methods */
    dxgi_device_QueryInterface,
    dxgi_device_AddRef,
    dxgi_device_Release,
    /* IDXGIObject methods */
    dxgi_device_SetPrivateData,
    dxgi_device_SetPrivateDataInterface,
    dxgi_device_GetPrivateData,
    dxgi_device_GetParent,
    /* IDXGIDevice methods */
    dxgi_device_GetAdapter,
    dxgi_device_CreateSurface,
    dxgi_device_QueryResourceResidency,
    dxgi_device_SetGPUThreadPriority,
    dxgi_device_GetGPUThreadPriority,
340 341
    /* IWineDXGIAdapter methods */
    dxgi_device_get_wined3d_device,
342
    dxgi_device_create_surface,
343
    dxgi_device_create_swapchain,
344
};
345 346 347 348

HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
        IDXGIFactory *factory, IDXGIAdapter *adapter)
{
349 350
    struct wined3d_device_parent *wined3d_device_parent;
    IWineDXGIDeviceParent *dxgi_device_parent;
351 352
    IWineDXGIAdapter *wine_adapter;
    UINT adapter_ordinal;
353
    struct wined3d *wined3d;
354 355
    void *layer_base;
    HRESULT hr;
356
    WINED3DCAPS caps;
357

358
    device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    device->refcount = 1;

    layer_base = device + 1;

    hr = layer->create(layer->id, &layer_base, 0,
            device, &IID_IUnknown, (void **)&device->child_layer);
    if (FAILED(hr))
    {
        WARN("Failed to create device, returning %#x.\n", hr);
        goto fail;
    }

    hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
    if (FAILED(hr))
    {
        WARN("This is not the factory we're looking for, returning %#x.\n", hr);
        goto fail;
    }
    wined3d = IWineDXGIFactory_get_wined3d(device->factory);

    hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
    if (FAILED(hr))
    {
        WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
        EnterCriticalSection(&dxgi_cs);
384
        wined3d_decref(wined3d);
385 386 387 388 389 390
        LeaveCriticalSection(&dxgi_cs);
        goto fail;
    }
    adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
    IWineDXGIAdapter_Release(wine_adapter);

391 392
    hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface, &IID_IWineDXGIDeviceParent,
            (void **)&dxgi_device_parent);
393 394 395 396 397 398
    if (FAILED(hr))
    {
        ERR("DXGI device should implement IWineD3DDeviceParent.\n");
        goto fail;
    }

399 400
    wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);

401
    FIXME("Ignoring adapter type.\n");
402

403
    hr = wined3d_get_device_caps(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, &caps);
404 405 406 407 408 409 410 411
    if (FAILED(hr) || caps.VertexShaderVersion < 4 || caps.PixelShaderVersion < 4)
    {
        WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
        if (SUCCEEDED(hr))
            hr = E_FAIL;
        goto fail;
    }

412
    EnterCriticalSection(&dxgi_cs);
413
    hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, NULL, 0, 4,
414
            wined3d_device_parent, &device->wined3d_device);
415
    IWineDXGIDeviceParent_Release(dxgi_device_parent);
416
    wined3d_decref(wined3d);
417 418 419 420 421 422 423 424 425 426 427 428 429
    LeaveCriticalSection(&dxgi_cs);
    if (FAILED(hr))
    {
        WARN("Failed to create a wined3d device, returning %#x.\n", hr);
        goto fail;
    }

    return S_OK;

fail:
    if (device->wined3d_device)
    {
        EnterCriticalSection(&dxgi_cs);
430
        wined3d_device_decref(device->wined3d_device);
431 432 433 434 435 436
        LeaveCriticalSection(&dxgi_cs);
    }
    if (device->factory) IWineDXGIFactory_Release(device->factory);
    if (device->child_layer) IUnknown_Release(device->child_layer);
    return hr;
}