async.c 12.4 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
/*
 * Copyright 2009 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"

23
#include "d3d11_private.h"
24

25
WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
26

27 28 29
/* ID3D11Query methods */

static inline struct d3d_query *impl_from_ID3D11Query(ID3D11Query *iface)
30
{
31
    return CONTAINING_RECORD(iface, struct d3d_query, ID3D11Query_iface);
32 33
}

34
static HRESULT STDMETHODCALLTYPE d3d11_query_QueryInterface(ID3D11Query *iface, REFIID riid, void **object)
35
{
36
    struct d3d_query *query = impl_from_ID3D11Query(iface);
37

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

40 41 42 43 44 45 46 47 48 49 50
    if ((IsEqualGUID(riid, &IID_ID3D11Predicate) && query->predicate)
            || IsEqualGUID(riid, &IID_ID3D11Query)
            || IsEqualGUID(riid, &IID_ID3D11Asynchronous)
            || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
        ID3D11Query_AddRef(iface);
        *object = iface;
        return S_OK;
    }

51 52
    if ((IsEqualGUID(riid, &IID_ID3D10Predicate) && query->predicate)
            || IsEqualGUID(riid, &IID_ID3D10Query)
53
            || IsEqualGUID(riid, &IID_ID3D10Asynchronous)
54
            || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
55
    {
56 57
        ID3D10Query_AddRef(&query->ID3D10Query_iface);
        *object = &query->ID3D10Query_iface;
58 59 60 61 62 63 64 65 66
        return S_OK;
    }

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

    *object = NULL;
    return E_NOINTERFACE;
}

67
static ULONG STDMETHODCALLTYPE d3d11_query_AddRef(ID3D11Query *iface)
68
{
69 70
    struct d3d_query *query = impl_from_ID3D11Query(iface);
    ULONG refcount = InterlockedIncrement(&query->refcount);
71

72
    TRACE("%p increasing refcount to %u.\n", query, refcount);
73 74 75 76

    return refcount;
}

77
static ULONG STDMETHODCALLTYPE d3d11_query_Release(ID3D11Query *iface)
78
{
79
    struct d3d_query *query = impl_from_ID3D11Query(iface);
80
    ULONG refcount = InterlockedDecrement(&query->refcount);
81

82
    TRACE("%p decreasing refcount to %u.\n", query, refcount);
83 84 85

    if (!refcount)
    {
86
        ID3D11Device_Release(query->device);
87
        wined3d_mutex_lock();
88 89
        wined3d_query_decref(query->wined3d_query);
        wined3d_private_store_cleanup(&query->private_store);
90
        wined3d_mutex_unlock();
91
        HeapFree(GetProcessHeap(), 0, query);
92 93 94 95 96
    }

    return refcount;
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static void STDMETHODCALLTYPE d3d11_query_GetDevice(ID3D11Query *iface, ID3D11Device **device)
{
    struct d3d_query *query = impl_from_ID3D11Query(iface);

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

    *device = query->device;
    ID3D11Device_AddRef(*device);
}

static HRESULT STDMETHODCALLTYPE d3d11_query_GetPrivateData(ID3D11Query *iface,
        REFGUID guid, UINT *data_size, void *data)
{
    struct d3d_query *query = impl_from_ID3D11Query(iface);

    TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_get_private_data(&query->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_query_SetPrivateData(ID3D11Query *iface,
        REFGUID guid, UINT data_size, const void *data)
{
    struct d3d_query *query = impl_from_ID3D11Query(iface);

    TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_set_private_data(&query->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_query_SetPrivateDataInterface(ID3D11Query *iface,
        REFGUID guid, const IUnknown *data)
{
    struct d3d_query *query = impl_from_ID3D11Query(iface);

    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

    return d3d_set_private_data_interface(&query->private_store, guid, data);
}

static UINT STDMETHODCALLTYPE d3d11_query_GetDataSize(ID3D11Query *iface)
{
    FIXME("iface %p stub!\n", iface);

    return 0;
}

static void STDMETHODCALLTYPE d3d11_query_GetDesc(ID3D11Query *iface, D3D11_QUERY_DESC *desc)
{
    FIXME("iface %p, desc %p stub!\n", iface, desc);
}

static const struct ID3D11QueryVtbl d3d11_query_vtbl =
{
    /* IUnknown methods */
    d3d11_query_QueryInterface,
    d3d11_query_AddRef,
    d3d11_query_Release,
    /* ID3D11DeviceChild methods */
    d3d11_query_GetDevice,
    d3d11_query_GetPrivateData,
    d3d11_query_SetPrivateData,
    d3d11_query_SetPrivateDataInterface,
    /* ID3D11Asynchronous methods */
    d3d11_query_GetDataSize,
    /* ID3D11Query methods */
    d3d11_query_GetDesc,
};

166 167 168 169 170 171 172 173
struct d3d_query *unsafe_impl_from_ID3D11Query(ID3D11Query *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d11_query_vtbl);
    return CONTAINING_RECORD(iface, struct d3d_query, ID3D11Query_iface);
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/* ID3D10Query methods */

static inline struct d3d_query *impl_from_ID3D10Query(ID3D10Query *iface)
{
    return CONTAINING_RECORD(iface, struct d3d_query, ID3D10Query_iface);
}

/* IUnknown methods */

static HRESULT STDMETHODCALLTYPE d3d10_query_QueryInterface(ID3D10Query *iface, REFIID riid, void **object)
{
    struct d3d_query *query = impl_from_ID3D10Query(iface);

    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

    return d3d11_query_QueryInterface(&query->ID3D11Query_iface, riid, object);
}

static ULONG STDMETHODCALLTYPE d3d10_query_AddRef(ID3D10Query *iface)
{
    struct d3d_query *query = impl_from_ID3D10Query(iface);

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

    return d3d11_query_AddRef(&query->ID3D11Query_iface);
}

static ULONG STDMETHODCALLTYPE d3d10_query_Release(ID3D10Query *iface)
{
    struct d3d_query *query = impl_from_ID3D10Query(iface);

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

    return d3d11_query_Release(&query->ID3D11Query_iface);
}

210 211 212 213
/* ID3D10DeviceChild methods */

static void STDMETHODCALLTYPE d3d10_query_GetDevice(ID3D10Query *iface, ID3D10Device **device)
{
214
    struct d3d_query *query = impl_from_ID3D10Query(iface);
215 216 217

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

218
    ID3D11Device_QueryInterface(query->device, &IID_ID3D10Device, (void **)device);
219 220 221 222 223
}

static HRESULT STDMETHODCALLTYPE d3d10_query_GetPrivateData(ID3D10Query *iface,
        REFGUID guid, UINT *data_size, void *data)
{
224
    struct d3d_query *query = impl_from_ID3D10Query(iface);
225 226

    TRACE("iface %p, guid %s, data_size %p, data %p.\n",
227 228
            iface, debugstr_guid(guid), data_size, data);

229
    return d3d_get_private_data(&query->private_store, guid, data_size, data);
230 231 232 233 234
}

static HRESULT STDMETHODCALLTYPE d3d10_query_SetPrivateData(ID3D10Query *iface,
        REFGUID guid, UINT data_size, const void *data)
{
235
    struct d3d_query *query = impl_from_ID3D10Query(iface);
236 237

    TRACE("iface %p, guid %s, data_size %u, data %p.\n",
238 239
            iface, debugstr_guid(guid), data_size, data);

240
    return d3d_set_private_data(&query->private_store, guid, data_size, data);
241 242 243 244 245
}

static HRESULT STDMETHODCALLTYPE d3d10_query_SetPrivateDataInterface(ID3D10Query *iface,
        REFGUID guid, const IUnknown *data)
{
246
    struct d3d_query *query = impl_from_ID3D10Query(iface);
247

248 249
    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

250
    return d3d_set_private_data_interface(&query->private_store, guid, data);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

/* ID3D10Asynchronous methods */

static void STDMETHODCALLTYPE d3d10_query_Begin(ID3D10Query *iface)
{
    FIXME("iface %p stub!\n", iface);
}

static void STDMETHODCALLTYPE d3d10_query_End(ID3D10Query *iface)
{
    FIXME("iface %p stub!\n", iface);
}

static HRESULT STDMETHODCALLTYPE d3d10_query_GetData(ID3D10Query *iface, void *data, UINT data_size, UINT flags)
{
    FIXME("iface %p, data %p, data_size %u, flags %#x stub!\n", iface, data, data_size, flags);

    return E_NOTIMPL;
}

static UINT STDMETHODCALLTYPE d3d10_query_GetDataSize(ID3D10Query *iface)
{
    FIXME("iface %p stub!\n", iface);

    return 0;
}

/* ID3D10Query methods */

static void STDMETHODCALLTYPE d3d10_query_GetDesc(ID3D10Query *iface, D3D10_QUERY_DESC *desc)
{
    FIXME("iface %p, desc %p stub!\n", iface, desc);
}

static const struct ID3D10QueryVtbl d3d10_query_vtbl =
{
    /* IUnknown methods */
    d3d10_query_QueryInterface,
    d3d10_query_AddRef,
    d3d10_query_Release,
    /* ID3D10DeviceChild methods */
    d3d10_query_GetDevice,
    d3d10_query_GetPrivateData,
    d3d10_query_SetPrivateData,
    d3d10_query_SetPrivateDataInterface,
    /* ID3D10Asynchronous methods */
    d3d10_query_Begin,
    d3d10_query_End,
    d3d10_query_GetData,
    d3d10_query_GetDataSize,
    /* ID3D10Query methods */
    d3d10_query_GetDesc,
};

306
struct d3d_query *unsafe_impl_from_ID3D10Query(ID3D10Query *iface)
307 308 309 310
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d10_query_vtbl);
311
    return CONTAINING_RECORD(iface, struct d3d_query, ID3D10Query_iface);
312 313
}

314
static HRESULT d3d_query_init(struct d3d_query *query, struct d3d_device *device,
315
        const D3D11_QUERY_DESC *desc, BOOL predicate)
316
{
317 318 319 320
    HRESULT hr;

    static const enum wined3d_query_type query_type_map[] =
    {
321 322 323 324 325 326 327 328
        /* D3D11_QUERY_EVENT                    */  WINED3D_QUERY_TYPE_EVENT,
        /* D3D11_QUERY_OCCLUSION                */  WINED3D_QUERY_TYPE_OCCLUSION,
        /* D3D11_QUERY_TIMESTAMP                */  WINED3D_QUERY_TYPE_TIMESTAMP,
        /* D3D11_QUERY_TIMESTAMP_DISJOINT       */  WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT,
        /* D3D11_QUERY_PIPELINE_STATISTICS      */  WINED3D_QUERY_TYPE_PIPELINE_STATISTICS,
        /* D3D11_QUERY_OCCLUSION_PREDICATE      */  WINED3D_QUERY_TYPE_OCCLUSION,
        /* D3D11_QUERY_SO_STATISTICS            */  WINED3D_QUERY_TYPE_SO_STATISTICS,
        /* D3D11_QUERY_SO_OVERFLOW_PREDICATE    */  WINED3D_QUERY_TYPE_SO_OVERFLOW,
329 330
    };

331
    if (desc->Query >= sizeof(query_type_map) / sizeof(*query_type_map))
332 333 334 335 336 337 338 339
    {
        FIXME("Unhandled query type %#x.\n", desc->Query);
        return E_INVALIDARG;
    }

    if (desc->MiscFlags)
        FIXME("Ignoring MiscFlags %#x.\n", desc->MiscFlags);

340
    query->ID3D11Query_iface.lpVtbl = &d3d11_query_vtbl;
341
    query->ID3D10Query_iface.lpVtbl = &d3d10_query_vtbl;
342
    query->refcount = 1;
343
    wined3d_mutex_lock();
344
    wined3d_private_store_init(&query->private_store);
345 346

    if (FAILED(hr = wined3d_query_create(device->wined3d_device,
347
            query_type_map[desc->Query], query, &query->wined3d_query)))
348 349
    {
        WARN("Failed to create wined3d query, hr %#x.\n", hr);
350
        wined3d_private_store_cleanup(&query->private_store);
351
        wined3d_mutex_unlock();
352 353
        return hr;
    }
354
    wined3d_mutex_unlock();
355

356
    query->predicate = predicate;
357 358
    query->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(query->device);
359 360 361

    return S_OK;
}
362 363 364 365 366

HRESULT d3d_query_create(struct d3d_device *device, const D3D11_QUERY_DESC *desc, BOOL predicate,
        struct d3d_query **query)
{
    struct d3d_query *object;
367
    BOOL is_predicate_type;
368 369 370 371 372
    HRESULT hr;

    if (!desc)
        return E_INVALIDARG;

373 374 375 376 377 378 379 380
    is_predicate_type = desc->Query == D3D11_QUERY_OCCLUSION_PREDICATE
            || desc->Query == D3D11_QUERY_SO_OVERFLOW_PREDICATE
            || desc->Query == D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM0
            || desc->Query == D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM1
            || desc->Query == D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM2
            || desc->Query == D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM3;

    if (!is_predicate_type && predicate)
381 382 383 384 385
    {
        WARN("Query type %u is not a predicate.\n", desc->Query);
        return E_INVALIDARG;
    }

386 387 388
    if (is_predicate_type)
        predicate = TRUE;

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;

    if (FAILED(hr = d3d_query_init(object, device, desc, predicate)))
    {
        WARN("Failed to initialize predicate, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created predicate %p.\n", object);
    *query = object;

    return S_OK;
}