wined3d: Check for SRV/RTV binding conflicts per wined3d_state.
[wine.git] / dlls / d3d9 / query.c
blob207ad55e5a19874ea62e63e832567780e719370f
1 /*
2 * IDirect3DQuery9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Copyright 2002-2003 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * 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
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static D3DQUERYTYPE d3dquerytype_from_wined3d(enum wined3d_query_type type)
29 return (D3DQUERYTYPE)type;
32 static enum wined3d_query_type wined3d_query_type_from_d3d(D3DQUERYTYPE type)
34 return (enum wined3d_query_type)type;
37 static inline struct d3d9_query *impl_from_IDirect3DQuery9(IDirect3DQuery9 *iface)
39 return CONTAINING_RECORD(iface, struct d3d9_query, IDirect3DQuery9_iface);
42 static HRESULT WINAPI d3d9_query_QueryInterface(IDirect3DQuery9 *iface, REFIID riid, void **out)
44 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
46 if (IsEqualGUID(riid, &IID_IDirect3DQuery9)
47 || IsEqualGUID(riid, &IID_IUnknown))
49 IDirect3DQuery9_AddRef(iface);
50 *out = iface;
51 return S_OK;
54 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
56 *out = NULL;
57 return E_NOINTERFACE;
60 static ULONG WINAPI d3d9_query_AddRef(IDirect3DQuery9 *iface)
62 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
63 ULONG refcount = InterlockedIncrement(&query->refcount);
65 TRACE("%p increasing refcount to %u.\n", iface, refcount);
67 return refcount;
70 static ULONG WINAPI d3d9_query_Release(IDirect3DQuery9 *iface)
72 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
73 ULONG refcount = InterlockedDecrement(&query->refcount);
75 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
77 if (!refcount)
79 wined3d_mutex_lock();
80 wined3d_query_decref(query->wined3d_query);
81 wined3d_mutex_unlock();
83 IDirect3DDevice9Ex_Release(query->parent_device);
84 heap_free(query);
86 return refcount;
89 static HRESULT WINAPI d3d9_query_GetDevice(IDirect3DQuery9 *iface, IDirect3DDevice9 **device)
91 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
93 TRACE("iface %p, device %p.\n", iface, device);
95 *device = (IDirect3DDevice9 *)query->parent_device;
96 IDirect3DDevice9_AddRef(*device);
98 TRACE("Returning device %p.\n", *device);
100 return D3D_OK;
103 static D3DQUERYTYPE WINAPI d3d9_query_GetType(IDirect3DQuery9 *iface)
105 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
106 D3DQUERYTYPE type;
108 TRACE("iface %p.\n", iface);
110 wined3d_mutex_lock();
111 type = d3dquerytype_from_wined3d(wined3d_query_get_type(query->wined3d_query));
112 wined3d_mutex_unlock();
114 return type;
117 static DWORD WINAPI d3d9_query_GetDataSize(IDirect3DQuery9 *iface)
119 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
121 TRACE("iface %p.\n", iface);
123 return query->data_size;
126 static HRESULT WINAPI d3d9_query_Issue(IDirect3DQuery9 *iface, DWORD flags)
128 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
129 HRESULT hr;
131 TRACE("iface %p, flags %#x.\n", iface, flags);
133 wined3d_mutex_lock();
134 hr = wined3d_query_issue(query->wined3d_query, flags);
135 wined3d_mutex_unlock();
137 return hr;
140 static HRESULT WINAPI d3d9_query_GetData(IDirect3DQuery9 *iface, void *data, DWORD size, DWORD flags)
142 struct d3d9_query *query = impl_from_IDirect3DQuery9(iface);
143 enum wined3d_query_type type;
144 HRESULT hr;
146 TRACE("iface %p, data %p, size %u, flags %#x.\n",
147 iface, data, size, flags);
149 wined3d_mutex_lock();
150 type = wined3d_query_get_type(query->wined3d_query);
151 if (type == WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT && data)
153 struct wined3d_query_data_timestamp_disjoint data_disjoint;
155 if (size > sizeof(data_disjoint.disjoint))
156 size = sizeof(data_disjoint.disjoint);
158 hr = wined3d_query_get_data(query->wined3d_query, &data_disjoint, sizeof(data_disjoint), flags);
159 if (SUCCEEDED(hr))
160 memcpy(data, &data_disjoint.disjoint, size);
162 else
164 hr = wined3d_query_get_data(query->wined3d_query, data, size, flags);
166 wined3d_mutex_unlock();
168 if (hr == D3DERR_INVALIDCALL)
170 if (data)
172 memset(data, 0, size);
173 memset(data, 0xdd, min(size, query->data_size));
175 return S_OK;
177 return hr;
181 static const struct IDirect3DQuery9Vtbl d3d9_query_vtbl =
183 d3d9_query_QueryInterface,
184 d3d9_query_AddRef,
185 d3d9_query_Release,
186 d3d9_query_GetDevice,
187 d3d9_query_GetType,
188 d3d9_query_GetDataSize,
189 d3d9_query_Issue,
190 d3d9_query_GetData,
193 HRESULT query_init(struct d3d9_query *query, struct d3d9_device *device, D3DQUERYTYPE type)
195 HRESULT hr;
197 if (type > D3DQUERYTYPE_MEMORYPRESSURE)
199 if (type == 0x16)
200 FIXME("Undocumented query %#x created.\n", type);
201 else
202 WARN("Invalid query type %#x.\n", type);
204 return D3DERR_NOTAVAILABLE;
207 query->IDirect3DQuery9_iface.lpVtbl = &d3d9_query_vtbl;
208 query->refcount = 1;
210 wined3d_mutex_lock();
211 if (FAILED(hr = wined3d_query_create(device->wined3d_device, wined3d_query_type_from_d3d(type),
212 query, &d3d9_null_wined3d_parent_ops, &query->wined3d_query)))
214 wined3d_mutex_unlock();
215 WARN("Failed to create wined3d query, hr %#x.\n", hr);
216 return hr;
219 if (type == D3DQUERYTYPE_OCCLUSION)
220 query->data_size = sizeof(DWORD);
221 else if (type == D3DQUERYTYPE_TIMESTAMPDISJOINT)
222 query->data_size = sizeof(BOOL);
223 else
224 query->data_size = wined3d_query_get_data_size(query->wined3d_query);
225 wined3d_mutex_unlock();
227 query->parent_device = &device->IDirect3DDevice9Ex_iface;
228 IDirect3DDevice9Ex_AddRef(query->parent_device);
230 return D3D_OK;