st/nine: Move core of device clear to nine_state
[mesa.git] / src / gallium / state_trackers / nine / query9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "device9.h"
24 #include "query9.h"
25 #include "nine_helpers.h"
26 #include "pipe/p_screen.h"
27 #include "pipe/p_context.h"
28 #include "util/u_math.h"
29 #include "nine_dump.h"
30
31 #define DBG_CHANNEL DBG_QUERY
32
33 static inline unsigned
34 d3dquerytype_to_pipe_query(struct pipe_screen *screen, D3DQUERYTYPE type)
35 {
36 switch (type) {
37 case D3DQUERYTYPE_EVENT:
38 return PIPE_QUERY_GPU_FINISHED;
39 case D3DQUERYTYPE_OCCLUSION:
40 return screen->get_param(screen, PIPE_CAP_OCCLUSION_QUERY) ?
41 PIPE_QUERY_OCCLUSION_COUNTER : PIPE_QUERY_TYPES;
42 case D3DQUERYTYPE_TIMESTAMP:
43 return screen->get_param(screen, PIPE_CAP_QUERY_TIMESTAMP) ?
44 PIPE_QUERY_TIMESTAMP : PIPE_QUERY_TYPES;
45 case D3DQUERYTYPE_TIMESTAMPDISJOINT:
46 case D3DQUERYTYPE_TIMESTAMPFREQ:
47 return screen->get_param(screen, PIPE_CAP_QUERY_TIMESTAMP) ?
48 PIPE_QUERY_TIMESTAMP_DISJOINT : PIPE_QUERY_TYPES;
49 case D3DQUERYTYPE_VERTEXSTATS:
50 return screen->get_param(screen,
51 PIPE_CAP_QUERY_PIPELINE_STATISTICS) ?
52 PIPE_QUERY_PIPELINE_STATISTICS : PIPE_QUERY_TYPES;
53 default:
54 return PIPE_QUERY_TYPES; /* Query not supported */
55 }
56 }
57
58 #define GET_DATA_SIZE_CASE2(a, b) case D3DQUERYTYPE_##a: return sizeof(D3DDEVINFO_##b)
59 #define GET_DATA_SIZE_CASET(a, b) case D3DQUERYTYPE_##a: return sizeof(b)
60 static inline DWORD
61 nine_query_result_size(D3DQUERYTYPE type)
62 {
63 switch (type) {
64 GET_DATA_SIZE_CASE2(VERTEXSTATS, D3DVERTEXSTATS);
65 GET_DATA_SIZE_CASET(EVENT, BOOL);
66 GET_DATA_SIZE_CASET(OCCLUSION, DWORD);
67 GET_DATA_SIZE_CASET(TIMESTAMP, UINT64);
68 GET_DATA_SIZE_CASET(TIMESTAMPDISJOINT, BOOL);
69 GET_DATA_SIZE_CASET(TIMESTAMPFREQ, UINT64);
70 default:
71 assert(0);
72 return 0;
73 }
74 }
75
76 HRESULT
77 nine_is_query_supported(struct pipe_screen *screen, D3DQUERYTYPE type)
78 {
79 const unsigned ptype = d3dquerytype_to_pipe_query(screen, type);
80
81 user_assert(ptype != ~0, D3DERR_INVALIDCALL);
82
83 if (ptype == PIPE_QUERY_TYPES) {
84 DBG("Query type %u (%s) not supported.\n",
85 type, nine_D3DQUERYTYPE_to_str(type));
86 return D3DERR_NOTAVAILABLE;
87 }
88 return D3D_OK;
89 }
90
91 HRESULT
92 NineQuery9_ctor( struct NineQuery9 *This,
93 struct NineUnknownParams *pParams,
94 D3DQUERYTYPE Type )
95 {
96 struct pipe_context *pipe = pParams->device->pipe;
97 const unsigned ptype = d3dquerytype_to_pipe_query(pParams->device->screen, Type);
98 HRESULT hr;
99
100 DBG("This=%p pParams=%p Type=%d\n", This, pParams, Type);
101
102 hr = NineUnknown_ctor(&This->base, pParams);
103 if (FAILED(hr))
104 return hr;
105
106 This->state = NINE_QUERY_STATE_FRESH;
107 This->type = Type;
108
109 user_assert(ptype != ~0, D3DERR_INVALIDCALL);
110
111 if (ptype < PIPE_QUERY_TYPES) {
112 This->pq = pipe->create_query(pipe, ptype, 0);
113 if (!This->pq)
114 return E_OUTOFMEMORY;
115 } else {
116 assert(0); /* we have checked this case before */
117 }
118
119 This->instant =
120 Type == D3DQUERYTYPE_EVENT ||
121 Type == D3DQUERYTYPE_RESOURCEMANAGER ||
122 Type == D3DQUERYTYPE_TIMESTAMP ||
123 Type == D3DQUERYTYPE_TIMESTAMPFREQ ||
124 Type == D3DQUERYTYPE_VCACHE ||
125 Type == D3DQUERYTYPE_VERTEXSTATS;
126
127 This->result_size = nine_query_result_size(Type);
128
129 return D3D_OK;
130 }
131
132 void
133 NineQuery9_dtor( struct NineQuery9 *This )
134 {
135 struct pipe_context *pipe = This->base.device->pipe;
136
137 DBG("This=%p\n", This);
138
139 if (This->pq) {
140 if (This->state == NINE_QUERY_STATE_RUNNING)
141 pipe->end_query(pipe, This->pq);
142 pipe->destroy_query(pipe, This->pq);
143 }
144
145 NineUnknown_dtor(&This->base);
146 }
147
148 D3DQUERYTYPE NINE_WINAPI
149 NineQuery9_GetType( struct NineQuery9 *This )
150 {
151 return This->type;
152 }
153
154 DWORD NINE_WINAPI
155 NineQuery9_GetDataSize( struct NineQuery9 *This )
156 {
157 return This->result_size;
158 }
159
160 HRESULT NINE_WINAPI
161 NineQuery9_Issue( struct NineQuery9 *This,
162 DWORD dwIssueFlags )
163 {
164 struct pipe_context *pipe = This->base.device->pipe;
165
166 DBG("This=%p dwIssueFlags=%d\n", This, dwIssueFlags);
167
168 user_assert((dwIssueFlags == D3DISSUE_BEGIN) ||
169 (dwIssueFlags == 0) ||
170 (dwIssueFlags == D3DISSUE_END), D3DERR_INVALIDCALL);
171
172 /* Wine tests: always return D3D_OK on D3DISSUE_BEGIN
173 * even when the call is supposed to be forbidden */
174 if (dwIssueFlags == D3DISSUE_BEGIN && This->instant)
175 return D3D_OK;
176
177 if (dwIssueFlags == D3DISSUE_BEGIN) {
178 if (This->state == NINE_QUERY_STATE_RUNNING) {
179 pipe->end_query(pipe, This->pq);
180 }
181 pipe->begin_query(pipe, This->pq);
182 This->state = NINE_QUERY_STATE_RUNNING;
183 } else {
184 if (This->state != NINE_QUERY_STATE_RUNNING &&
185 This->type != D3DQUERYTYPE_EVENT &&
186 This->type != D3DQUERYTYPE_TIMESTAMP)
187 pipe->begin_query(pipe, This->pq);
188 pipe->end_query(pipe, This->pq);
189 This->state = NINE_QUERY_STATE_ENDED;
190 }
191 return D3D_OK;
192 }
193
194 union nine_query_result
195 {
196 D3DDEVINFO_D3DVERTEXSTATS vertexstats;
197 DWORD dw;
198 BOOL b;
199 UINT64 u64;
200 };
201
202 HRESULT NINE_WINAPI
203 NineQuery9_GetData( struct NineQuery9 *This,
204 void *pData,
205 DWORD dwSize,
206 DWORD dwGetDataFlags )
207 {
208 struct pipe_context *pipe = This->base.device->pipe;
209 boolean ok, wait_query_result = FALSE;
210 union pipe_query_result presult;
211 union nine_query_result nresult;
212
213 DBG("This=%p pData=%p dwSize=%d dwGetDataFlags=%d\n",
214 This, pData, dwSize, dwGetDataFlags);
215
216 /* according to spec we should return D3DERR_INVALIDCALL here, but
217 * wine returns S_FALSE because it is apparently the behaviour
218 * on windows */
219 user_assert(This->state != NINE_QUERY_STATE_RUNNING, S_FALSE);
220 user_assert(dwSize == 0 || pData, D3DERR_INVALIDCALL);
221 user_assert(dwGetDataFlags == 0 ||
222 dwGetDataFlags == D3DGETDATA_FLUSH, D3DERR_INVALIDCALL);
223
224 if (This->state == NINE_QUERY_STATE_FRESH) {
225 /* App forgot calling Issue. call it for it.
226 * However Wine states that return value should
227 * be S_OK, so wait for the result to return S_OK. */
228 NineQuery9_Issue(This, D3DISSUE_END);
229 wait_query_result = TRUE;
230 }
231
232 /* The documention mentions no special case for D3DQUERYTYPE_TIMESTAMP.
233 * However Windows tests show that the query always succeeds when
234 * D3DGETDATA_FLUSH is specified. */
235 if (This->type == D3DQUERYTYPE_TIMESTAMP &&
236 (dwGetDataFlags & D3DGETDATA_FLUSH))
237 wait_query_result = TRUE;
238
239
240 /* Note: We ignore dwGetDataFlags, because get_query_result will
241 * flush automatically if needed */
242
243 ok = pipe->get_query_result(pipe, This->pq, wait_query_result, &presult);
244
245 if (!ok) return S_FALSE;
246
247 if (!dwSize)
248 return S_OK;
249
250 switch (This->type) {
251 case D3DQUERYTYPE_EVENT:
252 nresult.b = presult.b;
253 break;
254 case D3DQUERYTYPE_OCCLUSION:
255 nresult.dw = presult.u64;
256 break;
257 case D3DQUERYTYPE_TIMESTAMP:
258 nresult.u64 = presult.u64;
259 break;
260 case D3DQUERYTYPE_TIMESTAMPDISJOINT:
261 nresult.b = presult.timestamp_disjoint.disjoint;
262 break;
263 case D3DQUERYTYPE_TIMESTAMPFREQ:
264 /* Applications use it to convert the TIMESTAMP value to time.
265 AMD drivers on win seem to return the actual hardware clock
266 resolution and corresponding values in TIMESTAMP.
267 However, this behaviour is not easy to replicate here.
268 So instead we do what wine and opengl do, and use
269 nanoseconds TIMESTAMPs.
270 (Which is also the unit used by PIPE_QUERY_TIMESTAMP.)
271 */
272 nresult.u64 = 1000000000;
273 break;
274 case D3DQUERYTYPE_VERTEXSTATS:
275 nresult.vertexstats.NumRenderedTriangles =
276 presult.pipeline_statistics.c_invocations;
277 nresult.vertexstats.NumExtraClippingTriangles =
278 presult.pipeline_statistics.c_primitives;
279 break;
280 default:
281 assert(0);
282 break;
283 }
284 memcpy(pData, &nresult, MIN2(sizeof(nresult), dwSize));
285
286 return S_OK;
287 }
288
289 IDirect3DQuery9Vtbl NineQuery9_vtable = {
290 (void *)NineUnknown_QueryInterface,
291 (void *)NineUnknown_AddRef,
292 (void *)NineUnknown_Release,
293 (void *)NineUnknown_GetDevice, /* actually part of Query9 iface */
294 (void *)NineQuery9_GetType,
295 (void *)NineQuery9_GetDataSize,
296 (void *)NineQuery9_Issue,
297 (void *)NineQuery9_GetData
298 };
299
300 static const GUID *NineQuery9_IIDs[] = {
301 &IID_IDirect3DQuery9,
302 &IID_IUnknown,
303 NULL
304 };
305
306 HRESULT
307 NineQuery9_new( struct NineDevice9 *pDevice,
308 struct NineQuery9 **ppOut,
309 D3DQUERYTYPE Type )
310 {
311 NINE_DEVICE_CHILD_NEW(Query9, ppOut, pDevice, Type);
312 }