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