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 "nine_state.h"
25 #include "query9.h"
26 #include "nine_helpers.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_context.h"
29 #include "util/u_math.h"
30 #include "nine_dump.h"
31 
32 #define DBG_CHANNEL DBG_QUERY
33 
34 static inline unsigned
d3dquerytype_to_pipe_query(struct pipe_screen * screen,D3DQUERYTYPE type)35 d3dquerytype_to_pipe_query(struct pipe_screen *screen, D3DQUERYTYPE type)
36 {
37     switch (type) {
38     case D3DQUERYTYPE_EVENT:
39         return PIPE_QUERY_GPU_FINISHED;
40     case D3DQUERYTYPE_OCCLUSION:
41         return screen->get_param(screen, PIPE_CAP_OCCLUSION_QUERY) ?
42                PIPE_QUERY_OCCLUSION_COUNTER : PIPE_QUERY_TYPES;
43     case D3DQUERYTYPE_TIMESTAMP:
44         return screen->get_param(screen, PIPE_CAP_QUERY_TIMESTAMP) ?
45                PIPE_QUERY_TIMESTAMP : PIPE_QUERY_TYPES;
46     case D3DQUERYTYPE_TIMESTAMPDISJOINT:
47     case D3DQUERYTYPE_TIMESTAMPFREQ:
48         return screen->get_param(screen, PIPE_CAP_QUERY_TIMESTAMP) ?
49                PIPE_QUERY_TIMESTAMP_DISJOINT : PIPE_QUERY_TYPES;
50     case D3DQUERYTYPE_VERTEXSTATS:
51         return screen->get_param(screen,
52                                  PIPE_CAP_QUERY_PIPELINE_STATISTICS) ?
53                PIPE_QUERY_PIPELINE_STATISTICS : PIPE_QUERY_TYPES;
54     default:
55         return PIPE_QUERY_TYPES; /* Query not supported */
56     }
57 }
58 
59 #define GET_DATA_SIZE_CASE2(a, b) case D3DQUERYTYPE_##a: return sizeof(D3DDEVINFO_##b)
60 #define GET_DATA_SIZE_CASET(a, b) case D3DQUERYTYPE_##a: return sizeof(b)
61 static inline DWORD
nine_query_result_size(D3DQUERYTYPE type)62 nine_query_result_size(D3DQUERYTYPE type)
63 {
64     switch (type) {
65     GET_DATA_SIZE_CASE2(VERTEXSTATS, D3DVERTEXSTATS);
66     GET_DATA_SIZE_CASET(EVENT, BOOL);
67     GET_DATA_SIZE_CASET(OCCLUSION, DWORD);
68     GET_DATA_SIZE_CASET(TIMESTAMP, UINT64);
69     GET_DATA_SIZE_CASET(TIMESTAMPDISJOINT, BOOL);
70     GET_DATA_SIZE_CASET(TIMESTAMPFREQ, UINT64);
71     default:
72         assert(0);
73         return 0;
74     }
75 }
76 
77 HRESULT
nine_is_query_supported(struct pipe_screen * screen,D3DQUERYTYPE type)78 nine_is_query_supported(struct pipe_screen *screen, D3DQUERYTYPE type)
79 {
80     const unsigned ptype = d3dquerytype_to_pipe_query(screen, type);
81 
82     user_assert(ptype != ~0, D3DERR_INVALIDCALL);
83 
84     if (ptype == PIPE_QUERY_TYPES) {
85         DBG("Query type %u (%s) not supported.\n",
86             type, nine_D3DQUERYTYPE_to_str(type));
87         return D3DERR_NOTAVAILABLE;
88     }
89     return D3D_OK;
90 }
91 
92 HRESULT
NineQuery9_ctor(struct NineQuery9 * This,struct NineUnknownParams * pParams,D3DQUERYTYPE Type)93 NineQuery9_ctor( struct NineQuery9 *This,
94                  struct NineUnknownParams *pParams,
95                  D3DQUERYTYPE Type )
96 {
97     struct NineDevice9 *device = pParams->device;
98     const unsigned ptype = d3dquerytype_to_pipe_query(device->screen, Type);
99     HRESULT hr;
100 
101     DBG("This=%p pParams=%p Type=%d\n", This, pParams, Type);
102 
103     hr = NineUnknown_ctor(&This->base, pParams);
104     if (FAILED(hr))
105         return hr;
106 
107     This->state = NINE_QUERY_STATE_FRESH;
108     This->type = Type;
109 
110     user_assert(ptype != ~0, D3DERR_INVALIDCALL);
111 
112     if (ptype < PIPE_QUERY_TYPES) {
113         This->pq = nine_context_create_query(device, ptype);
114         if (!This->pq)
115             return E_OUTOFMEMORY;
116     } else {
117         assert(0); /* we have checked this case before */
118     }
119 
120     This->instant =
121         Type == D3DQUERYTYPE_EVENT ||
122         Type == D3DQUERYTYPE_RESOURCEMANAGER ||
123         Type == D3DQUERYTYPE_TIMESTAMP ||
124         Type == D3DQUERYTYPE_TIMESTAMPFREQ ||
125         Type == D3DQUERYTYPE_VCACHE ||
126         Type == D3DQUERYTYPE_VERTEXSTATS;
127 
128     This->result_size = nine_query_result_size(Type);
129 
130     return D3D_OK;
131 }
132 
133 void
NineQuery9_dtor(struct NineQuery9 * This)134 NineQuery9_dtor( struct NineQuery9 *This )
135 {
136     struct NineDevice9 *device = This->base.device;
137 
138     DBG("This=%p\n", This);
139 
140     if (This->pq) {
141         if (This->state == NINE_QUERY_STATE_RUNNING)
142             nine_context_end_query(device, &This->counter, This->pq);
143         nine_context_destroy_query(device, This->pq);
144     }
145 
146     NineUnknown_dtor(&This->base);
147 }
148 
149 D3DQUERYTYPE NINE_WINAPI
NineQuery9_GetType(struct NineQuery9 * This)150 NineQuery9_GetType( struct NineQuery9 *This )
151 {
152     return This->type;
153 }
154 
155 DWORD NINE_WINAPI
NineQuery9_GetDataSize(struct NineQuery9 * This)156 NineQuery9_GetDataSize( struct NineQuery9 *This )
157 {
158     return This->result_size;
159 }
160 
161 HRESULT NINE_WINAPI
NineQuery9_Issue(struct NineQuery9 * This,DWORD dwIssueFlags)162 NineQuery9_Issue( struct NineQuery9 *This,
163                   DWORD dwIssueFlags )
164 {
165     struct NineDevice9 *device = This->base.device;
166 
167     DBG("This=%p dwIssueFlags=%d\n", This, dwIssueFlags);
168 
169     user_assert((dwIssueFlags == D3DISSUE_BEGIN) ||
170                 (dwIssueFlags == 0) ||
171                 (dwIssueFlags == D3DISSUE_END), D3DERR_INVALIDCALL);
172 
173     /* Wine tests: always return D3D_OK on D3DISSUE_BEGIN
174      * even when the call is supposed to be forbidden */
175     if (dwIssueFlags == D3DISSUE_BEGIN && This->instant)
176         return D3D_OK;
177 
178     if (dwIssueFlags == D3DISSUE_BEGIN) {
179         if (This->state == NINE_QUERY_STATE_RUNNING)
180             nine_context_end_query(device, &This->counter, This->pq);
181         nine_context_begin_query(device, &This->counter, 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             nine_context_begin_query(device, &This->counter, This->pq);
188         nine_context_end_query(device, &This->counter, 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
NineQuery9_GetData(struct NineQuery9 * This,void * pData,DWORD dwSize,DWORD dwGetDataFlags)203 NineQuery9_GetData( struct NineQuery9 *This,
204                     void *pData,
205                     DWORD dwSize,
206                     DWORD dwGetDataFlags )
207 {
208     struct NineDevice9 *device = This->base.device;
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 = nine_context_get_query_result(device, This->pq, &This->counter,
244                                        !!(dwGetDataFlags & D3DGETDATA_FLUSH),
245                                        wait_query_result, &presult);
246 
247     if (!ok) return S_FALSE;
248 
249     if (!dwSize)
250         return S_OK;
251 
252     switch (This->type) {
253     case D3DQUERYTYPE_EVENT:
254         nresult.b = presult.b;
255         break;
256     case D3DQUERYTYPE_OCCLUSION:
257         nresult.dw = presult.u64;
258         break;
259     case D3DQUERYTYPE_TIMESTAMP:
260         nresult.u64 = presult.u64;
261         break;
262     case D3DQUERYTYPE_TIMESTAMPDISJOINT:
263         nresult.b = presult.timestamp_disjoint.disjoint;
264         break;
265     case D3DQUERYTYPE_TIMESTAMPFREQ:
266         /* Applications use it to convert the TIMESTAMP value to time.
267            AMD drivers on win seem to return the actual hardware clock
268            resolution and corresponding values in TIMESTAMP.
269            However, this behaviour is not easy to replicate here.
270            So instead we do what wine and opengl do, and use
271            nanoseconds TIMESTAMPs.
272            (Which is also the unit used by PIPE_QUERY_TIMESTAMP.)
273         */
274         nresult.u64 = 1000000000;
275         break;
276     case D3DQUERYTYPE_VERTEXSTATS:
277         nresult.vertexstats.NumRenderedTriangles =
278             presult.pipeline_statistics.c_invocations;
279         nresult.vertexstats.NumExtraClippingTriangles =
280             presult.pipeline_statistics.c_primitives;
281         break;
282     default:
283         assert(0);
284         break;
285     }
286     memcpy(pData, &nresult, MIN2(sizeof(nresult), dwSize));
287 
288     return S_OK;
289 }
290 
291 IDirect3DQuery9Vtbl NineQuery9_vtable = {
292     (void *)NineUnknown_QueryInterface,
293     (void *)NineUnknown_AddRef,
294     (void *)NineUnknown_Release,
295     (void *)NineUnknown_GetDevice, /* actually part of Query9 iface */
296     (void *)NineQuery9_GetType,
297     (void *)NineQuery9_GetDataSize,
298     (void *)NineQuery9_Issue,
299     (void *)NineQuery9_GetData
300 };
301 
302 static const GUID *NineQuery9_IIDs[] = {
303     &IID_IDirect3DQuery9,
304     &IID_IUnknown,
305     NULL
306 };
307 
308 HRESULT
NineQuery9_new(struct NineDevice9 * pDevice,struct NineQuery9 ** ppOut,D3DQUERYTYPE Type)309 NineQuery9_new( struct NineDevice9 *pDevice,
310                 struct NineQuery9 **ppOut,
311                 D3DQUERYTYPE Type )
312 {
313     NINE_DEVICE_CHILD_NEW(Query9, ppOut, pDevice, Type);
314 }
315