2 * Copyright 2009 Henri Verbeet for CodeWeavers
3 * Copyright 2015-2017 Józef Kucia for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "d3d11_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d11
);
25 /* ID3D11Query methods */
27 static inline struct d3d_query
*impl_from_ID3D11Query(ID3D11Query
*iface
)
29 return CONTAINING_RECORD(iface
, struct d3d_query
, ID3D11Query_iface
);
32 static HRESULT STDMETHODCALLTYPE
d3d11_query_QueryInterface(ID3D11Query
*iface
, REFIID riid
, void **object
)
34 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
36 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
38 if ((IsEqualGUID(riid
, &IID_ID3D11Predicate
) && query
->predicate
)
39 || IsEqualGUID(riid
, &IID_ID3D11Query
)
40 || IsEqualGUID(riid
, &IID_ID3D11Asynchronous
)
41 || IsEqualGUID(riid
, &IID_ID3D11DeviceChild
)
42 || IsEqualGUID(riid
, &IID_IUnknown
))
44 ID3D11Query_AddRef(iface
);
49 if ((IsEqualGUID(riid
, &IID_ID3D10Predicate
) && query
->predicate
)
50 || IsEqualGUID(riid
, &IID_ID3D10Query
)
51 || IsEqualGUID(riid
, &IID_ID3D10Asynchronous
)
52 || IsEqualGUID(riid
, &IID_ID3D10DeviceChild
))
54 ID3D10Query_AddRef(&query
->ID3D10Query_iface
);
55 *object
= &query
->ID3D10Query_iface
;
59 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
65 static ULONG STDMETHODCALLTYPE
d3d11_query_AddRef(ID3D11Query
*iface
)
67 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
68 ULONG refcount
= InterlockedIncrement(&query
->refcount
);
70 TRACE("%p increasing refcount to %u.\n", query
, refcount
);
74 ID3D11Device2_AddRef(query
->device
);
76 wined3d_query_incref(query
->wined3d_query
);
77 wined3d_mutex_unlock();
83 static ULONG STDMETHODCALLTYPE
d3d11_query_Release(ID3D11Query
*iface
)
85 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
86 ULONG refcount
= InterlockedDecrement(&query
->refcount
);
88 TRACE("%p decreasing refcount to %u.\n", query
, refcount
);
92 ID3D11Device2
*device
= query
->device
;
95 wined3d_query_decref(query
->wined3d_query
);
96 wined3d_mutex_unlock();
98 ID3D11Device2_Release(device
);
104 static void STDMETHODCALLTYPE
d3d11_query_GetDevice(ID3D11Query
*iface
, ID3D11Device
**device
)
106 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
108 TRACE("iface %p, device %p.\n", iface
, device
);
110 *device
= (ID3D11Device
*)query
->device
;
111 ID3D11Device_AddRef(*device
);
114 static HRESULT STDMETHODCALLTYPE
d3d11_query_GetPrivateData(ID3D11Query
*iface
,
115 REFGUID guid
, UINT
*data_size
, void *data
)
117 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
119 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
121 return d3d_get_private_data(&query
->private_store
, guid
, data_size
, data
);
124 static HRESULT STDMETHODCALLTYPE
d3d11_query_SetPrivateData(ID3D11Query
*iface
,
125 REFGUID guid
, UINT data_size
, const void *data
)
127 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
129 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
131 return d3d_set_private_data(&query
->private_store
, guid
, data_size
, data
);
134 static HRESULT STDMETHODCALLTYPE
d3d11_query_SetPrivateDataInterface(ID3D11Query
*iface
,
135 REFGUID guid
, const IUnknown
*data
)
137 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
139 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
141 return d3d_set_private_data_interface(&query
->private_store
, guid
, data
);
144 static UINT STDMETHODCALLTYPE
d3d11_query_GetDataSize(ID3D11Query
*iface
)
146 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
147 unsigned int data_size
;
149 TRACE("iface %p.\n", iface
);
151 wined3d_mutex_lock();
152 data_size
= wined3d_query_get_data_size(query
->wined3d_query
);
153 wined3d_mutex_unlock();
158 static void STDMETHODCALLTYPE
d3d11_query_GetDesc(ID3D11Query
*iface
, D3D11_QUERY_DESC
*desc
)
160 struct d3d_query
*query
= impl_from_ID3D11Query(iface
);
162 TRACE("iface %p, desc %p.\n", iface
, desc
);
167 static const struct ID3D11QueryVtbl d3d11_query_vtbl
=
169 /* IUnknown methods */
170 d3d11_query_QueryInterface
,
173 /* ID3D11DeviceChild methods */
174 d3d11_query_GetDevice
,
175 d3d11_query_GetPrivateData
,
176 d3d11_query_SetPrivateData
,
177 d3d11_query_SetPrivateDataInterface
,
178 /* ID3D11Asynchronous methods */
179 d3d11_query_GetDataSize
,
180 /* ID3D11Query methods */
184 static void STDMETHODCALLTYPE
d3d_query_wined3d_object_destroyed(void *parent
)
186 struct d3d_query
*query
= parent
;
188 wined3d_private_store_cleanup(&query
->private_store
);
192 static const struct wined3d_parent_ops d3d_query_wined3d_parent_ops
=
194 d3d_query_wined3d_object_destroyed
,
197 struct d3d_query
*unsafe_impl_from_ID3D11Query(ID3D11Query
*iface
)
201 assert(iface
->lpVtbl
== &d3d11_query_vtbl
);
202 return CONTAINING_RECORD(iface
, struct d3d_query
, ID3D11Query_iface
);
205 struct d3d_query
*unsafe_impl_from_ID3D11Asynchronous(ID3D11Asynchronous
*iface
)
207 return unsafe_impl_from_ID3D11Query((ID3D11Query
*)iface
);
210 /* ID3D10Query methods */
212 static inline struct d3d_query
*impl_from_ID3D10Query(ID3D10Query
*iface
)
214 return CONTAINING_RECORD(iface
, struct d3d_query
, ID3D10Query_iface
);
217 /* IUnknown methods */
219 static HRESULT STDMETHODCALLTYPE
d3d10_query_QueryInterface(ID3D10Query
*iface
, REFIID riid
, void **object
)
221 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
223 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
225 return d3d11_query_QueryInterface(&query
->ID3D11Query_iface
, riid
, object
);
228 static ULONG STDMETHODCALLTYPE
d3d10_query_AddRef(ID3D10Query
*iface
)
230 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
232 TRACE("iface %p.\n", iface
);
234 return d3d11_query_AddRef(&query
->ID3D11Query_iface
);
237 static ULONG STDMETHODCALLTYPE
d3d10_query_Release(ID3D10Query
*iface
)
239 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
241 TRACE("iface %p.\n", iface
);
243 return d3d11_query_Release(&query
->ID3D11Query_iface
);
246 /* ID3D10DeviceChild methods */
248 static void STDMETHODCALLTYPE
d3d10_query_GetDevice(ID3D10Query
*iface
, ID3D10Device
**device
)
250 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
252 TRACE("iface %p, device %p.\n", iface
, device
);
254 ID3D11Device2_QueryInterface(query
->device
, &IID_ID3D10Device
, (void **)device
);
257 static HRESULT STDMETHODCALLTYPE
d3d10_query_GetPrivateData(ID3D10Query
*iface
,
258 REFGUID guid
, UINT
*data_size
, void *data
)
260 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
262 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
263 iface
, debugstr_guid(guid
), data_size
, data
);
265 return d3d_get_private_data(&query
->private_store
, guid
, data_size
, data
);
268 static HRESULT STDMETHODCALLTYPE
d3d10_query_SetPrivateData(ID3D10Query
*iface
,
269 REFGUID guid
, UINT data_size
, const void *data
)
271 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
273 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
274 iface
, debugstr_guid(guid
), data_size
, data
);
276 return d3d_set_private_data(&query
->private_store
, guid
, data_size
, data
);
279 static HRESULT STDMETHODCALLTYPE
d3d10_query_SetPrivateDataInterface(ID3D10Query
*iface
,
280 REFGUID guid
, const IUnknown
*data
)
282 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
284 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
286 return d3d_set_private_data_interface(&query
->private_store
, guid
, data
);
289 /* ID3D10Asynchronous methods */
291 static void STDMETHODCALLTYPE
d3d10_query_Begin(ID3D10Query
*iface
)
293 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
296 TRACE("iface %p.\n", iface
);
298 wined3d_mutex_lock();
299 if (FAILED(hr
= wined3d_query_issue(query
->wined3d_query
, WINED3DISSUE_BEGIN
)))
300 ERR("Failed to issue query, hr %#x.\n", hr
);
301 wined3d_mutex_unlock();
304 static void STDMETHODCALLTYPE
d3d10_query_End(ID3D10Query
*iface
)
306 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
309 TRACE("iface %p.\n", iface
);
311 wined3d_mutex_lock();
312 if (FAILED(hr
= wined3d_query_issue(query
->wined3d_query
, WINED3DISSUE_END
)))
313 ERR("Failed to issue query, hr %#x.\n", hr
);
314 wined3d_mutex_unlock();
317 static HRESULT STDMETHODCALLTYPE
d3d10_query_GetData(ID3D10Query
*iface
, void *data
, UINT data_size
, UINT flags
)
319 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
320 D3D11_QUERY_DATA_PIPELINE_STATISTICS d3d11_data
;
321 void *d3d10_data_pointer
= NULL
;
322 unsigned int wined3d_flags
;
325 TRACE("iface %p, data %p, data_size %u, flags %#x.\n", iface
, data
, data_size
, flags
);
327 if (!data
&& data_size
)
330 if (query
->desc
.Query
== D3D11_QUERY_PIPELINE_STATISTICS
331 && data_size
== sizeof(D3D10_QUERY_DATA_PIPELINE_STATISTICS
))
333 data_size
= sizeof(D3D11_QUERY_DATA_PIPELINE_STATISTICS
);
334 d3d10_data_pointer
= data
;
338 wined3d_flags
= wined3d_getdata_flags_from_d3d11_async_getdata_flags(flags
);
340 wined3d_mutex_lock();
341 if (!data_size
|| wined3d_query_get_data_size(query
->wined3d_query
) == data_size
)
343 hr
= wined3d_query_get_data(query
->wined3d_query
, data
, data_size
, wined3d_flags
);
344 if (hr
== WINED3DERR_INVALIDCALL
)
345 hr
= DXGI_ERROR_INVALID_CALL
;
349 WARN("Invalid data size %u.\n", data_size
);
352 wined3d_mutex_unlock();
354 if (d3d10_data_pointer
&& hr
== S_OK
)
355 memcpy(d3d10_data_pointer
, data
, sizeof(D3D10_QUERY_DATA_PIPELINE_STATISTICS
));
360 static UINT STDMETHODCALLTYPE
d3d10_query_GetDataSize(ID3D10Query
*iface
)
362 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
363 unsigned int data_size
;
365 TRACE("iface %p.\n", iface
);
367 wined3d_mutex_lock();
368 data_size
= wined3d_query_get_data_size(query
->wined3d_query
);
369 wined3d_mutex_unlock();
371 if (query
->desc
.Query
== D3D11_QUERY_PIPELINE_STATISTICS
)
372 data_size
= sizeof(D3D10_QUERY_DATA_PIPELINE_STATISTICS
);
377 /* ID3D10Query methods */
379 static void STDMETHODCALLTYPE
d3d10_query_GetDesc(ID3D10Query
*iface
, D3D10_QUERY_DESC
*desc
)
381 struct d3d_query
*query
= impl_from_ID3D10Query(iface
);
383 TRACE("iface %p, desc %p.\n", iface
, desc
);
385 memcpy(desc
, &query
->desc
, sizeof(*desc
));
388 static const struct ID3D10QueryVtbl d3d10_query_vtbl
=
390 /* IUnknown methods */
391 d3d10_query_QueryInterface
,
394 /* ID3D10DeviceChild methods */
395 d3d10_query_GetDevice
,
396 d3d10_query_GetPrivateData
,
397 d3d10_query_SetPrivateData
,
398 d3d10_query_SetPrivateDataInterface
,
399 /* ID3D10Asynchronous methods */
403 d3d10_query_GetDataSize
,
404 /* ID3D10Query methods */
408 struct d3d_query
*unsafe_impl_from_ID3D10Query(ID3D10Query
*iface
)
412 assert(iface
->lpVtbl
== &d3d10_query_vtbl
);
413 return CONTAINING_RECORD(iface
, struct d3d_query
, ID3D10Query_iface
);
416 static HRESULT
d3d_query_init(struct d3d_query
*query
, struct d3d_device
*device
,
417 const D3D11_QUERY_DESC
*desc
, BOOL predicate
)
421 static const enum wined3d_query_type query_type_map
[] =
423 /* D3D11_QUERY_EVENT */ WINED3D_QUERY_TYPE_EVENT
,
424 /* D3D11_QUERY_OCCLUSION */ WINED3D_QUERY_TYPE_OCCLUSION
,
425 /* D3D11_QUERY_TIMESTAMP */ WINED3D_QUERY_TYPE_TIMESTAMP
,
426 /* D3D11_QUERY_TIMESTAMP_DISJOINT */ WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT
,
427 /* D3D11_QUERY_PIPELINE_STATISTICS */ WINED3D_QUERY_TYPE_PIPELINE_STATISTICS
,
428 /* D3D11_QUERY_OCCLUSION_PREDICATE */ WINED3D_QUERY_TYPE_OCCLUSION
,
429 /* D3D11_QUERY_SO_STATISTICS */ WINED3D_QUERY_TYPE_SO_STATISTICS
,
430 /* D3D11_QUERY_SO_OVERFLOW_PREDICATE */ WINED3D_QUERY_TYPE_SO_OVERFLOW
,
431 /* D3D11_QUERY_SO_STATISTICS_STREAM0 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0
,
432 /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM0 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM0
,
433 /* D3D11_QUERY_SO_STATISTICS_STREAM1 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1
,
434 /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM1 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM1
,
435 /* D3D11_QUERY_SO_STATISTICS_STREAM2 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2
,
436 /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM2 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM2
,
437 /* D3D11_QUERY_SO_STATISTICS_STREAM3 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3
,
438 /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM3 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM3
,
441 if (desc
->Query
>= ARRAY_SIZE(query_type_map
))
443 FIXME("Unhandled query type %#x.\n", desc
->Query
);
448 FIXME("Ignoring MiscFlags %#x.\n", desc
->MiscFlags
);
450 query
->ID3D11Query_iface
.lpVtbl
= &d3d11_query_vtbl
;
451 query
->ID3D10Query_iface
.lpVtbl
= &d3d10_query_vtbl
;
456 wined3d_mutex_lock();
457 wined3d_private_store_init(&query
->private_store
);
459 if (FAILED(hr
= wined3d_query_create(device
->wined3d_device
, query_type_map
[desc
->Query
],
460 query
, &d3d_query_wined3d_parent_ops
, &query
->wined3d_query
)))
462 WARN("Failed to create wined3d query, hr %#x.\n", hr
);
463 wined3d_private_store_cleanup(&query
->private_store
);
464 wined3d_mutex_unlock();
467 wined3d_mutex_unlock();
469 query
->predicate
= predicate
;
470 ID3D11Device2_AddRef(query
->device
= &device
->ID3D11Device2_iface
);
475 HRESULT
d3d_query_create(struct d3d_device
*device
, const D3D11_QUERY_DESC
*desc
, BOOL predicate
,
476 struct d3d_query
**query
)
478 struct d3d_query
*object
;
479 BOOL is_predicate_type
;
485 is_predicate_type
= desc
->Query
== D3D11_QUERY_OCCLUSION_PREDICATE
486 || desc
->Query
== D3D11_QUERY_SO_OVERFLOW_PREDICATE
487 || desc
->Query
== D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM0
488 || desc
->Query
== D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM1
489 || desc
->Query
== D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM2
490 || desc
->Query
== D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM3
;
492 if (!is_predicate_type
&& predicate
)
494 WARN("Query type %u is not a predicate.\n", desc
->Query
);
498 if (is_predicate_type
)
501 if (!(object
= heap_alloc_zero(sizeof(*object
))))
502 return E_OUTOFMEMORY
;
504 if (FAILED(hr
= d3d_query_init(object
, device
, desc
, predicate
)))
506 WARN("Failed to initialize predicate, hr %#x.\n", hr
);
511 TRACE("Created query %p.\n", object
);