wined3d: Make shader buffer dynamic.
[wine] / dlls / wined3d / query.c
1 /*
2  * Copyright 2005 Oliver Stieber
3  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
4  * Copyright 2009-2010 Henri Verbeet for CodeWeavers.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27
28 BOOL wined3d_event_query_supported(const struct wined3d_gl_info *gl_info)
29 {
30     return gl_info->supported[ARB_SYNC] || gl_info->supported[NV_FENCE] || gl_info->supported[APPLE_FENCE];
31 }
32
33 void wined3d_event_query_destroy(struct wined3d_event_query *query)
34 {
35     if (query->context) context_free_event_query(query);
36     HeapFree(GetProcessHeap(), 0, query);
37 }
38
39 static enum wined3d_event_query_result wined3d_event_query_test(const struct wined3d_event_query *query,
40         const struct wined3d_device *device)
41 {
42     struct wined3d_context *context;
43     const struct wined3d_gl_info *gl_info;
44     enum wined3d_event_query_result ret;
45     BOOL fence_result;
46
47     TRACE("(%p) : device %p\n", query, device);
48
49     if (!query->context)
50     {
51         TRACE("Query not started\n");
52         return WINED3D_EVENT_QUERY_NOT_STARTED;
53     }
54
55     if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
56     {
57         WARN("Event query tested from wrong thread\n");
58         return WINED3D_EVENT_QUERY_WRONG_THREAD;
59     }
60
61     context = context_acquire(device, query->context->current_rt);
62     gl_info = context->gl_info;
63
64     if (gl_info->supported[ARB_SYNC])
65     {
66         GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
67         checkGLcall("glClientWaitSync");
68
69         switch (gl_ret)
70         {
71             case GL_ALREADY_SIGNALED:
72             case GL_CONDITION_SATISFIED:
73                 ret = WINED3D_EVENT_QUERY_OK;
74                 break;
75
76             case GL_TIMEOUT_EXPIRED:
77                 ret = WINED3D_EVENT_QUERY_WAITING;
78                 break;
79
80             case GL_WAIT_FAILED:
81             default:
82                 ERR("glClientWaitSync returned %#x.\n", gl_ret);
83                 ret = WINED3D_EVENT_QUERY_ERROR;
84         }
85     }
86     else if (gl_info->supported[APPLE_FENCE])
87     {
88         fence_result = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
89         checkGLcall("glTestFenceAPPLE");
90         if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
91         else ret = WINED3D_EVENT_QUERY_WAITING;
92     }
93     else if (gl_info->supported[NV_FENCE])
94     {
95         fence_result = GL_EXTCALL(glTestFenceNV(query->object.id));
96         checkGLcall("glTestFenceNV");
97         if (fence_result) ret = WINED3D_EVENT_QUERY_OK;
98         else ret = WINED3D_EVENT_QUERY_WAITING;
99     }
100     else
101     {
102         ERR("Event query created despite lack of GL support\n");
103         ret = WINED3D_EVENT_QUERY_ERROR;
104     }
105
106     context_release(context);
107     return ret;
108 }
109
110 enum wined3d_event_query_result wined3d_event_query_finish(const struct wined3d_event_query *query,
111         const struct wined3d_device *device)
112 {
113     struct wined3d_context *context;
114     const struct wined3d_gl_info *gl_info;
115     enum wined3d_event_query_result ret;
116
117     TRACE("(%p)\n", query);
118
119     if (!query->context)
120     {
121         TRACE("Query not started\n");
122         return WINED3D_EVENT_QUERY_NOT_STARTED;
123     }
124     gl_info = query->context->gl_info;
125
126     if (query->context->tid != GetCurrentThreadId() && !gl_info->supported[ARB_SYNC])
127     {
128         /* A glFinish does not reliably wait for draws in other contexts. The caller has
129          * to find its own way to cope with the thread switch
130          */
131         WARN("Event query finished from wrong thread\n");
132         return WINED3D_EVENT_QUERY_WRONG_THREAD;
133     }
134
135     context = context_acquire(device, query->context->current_rt);
136
137     if (gl_info->supported[ARB_SYNC])
138     {
139         /* Apple seems to be into arbitrary limits, and timeouts larger than
140          * 0xfffffffffffffbff immediately return GL_TIMEOUT_EXPIRED. We don't
141          * really care and can live with waiting a few μs less. (OS X 10.7.4). */
142         GLenum gl_ret = GL_EXTCALL(glClientWaitSync(query->object.sync, GL_SYNC_FLUSH_COMMANDS_BIT, ~(GLuint64)0xffff));
143         checkGLcall("glClientWaitSync");
144
145         switch (gl_ret)
146         {
147             case GL_ALREADY_SIGNALED:
148             case GL_CONDITION_SATISFIED:
149                 ret = WINED3D_EVENT_QUERY_OK;
150                 break;
151
152                 /* We don't expect a timeout for a ~584 year wait */
153             default:
154                 ERR("glClientWaitSync returned %#x.\n", gl_ret);
155                 ret = WINED3D_EVENT_QUERY_ERROR;
156         }
157     }
158     else if (context->gl_info->supported[APPLE_FENCE])
159     {
160         GL_EXTCALL(glFinishFenceAPPLE(query->object.id));
161         checkGLcall("glFinishFenceAPPLE");
162         ret = WINED3D_EVENT_QUERY_OK;
163     }
164     else if (context->gl_info->supported[NV_FENCE])
165     {
166         GL_EXTCALL(glFinishFenceNV(query->object.id));
167         checkGLcall("glFinishFenceNV");
168         ret = WINED3D_EVENT_QUERY_OK;
169     }
170     else
171     {
172         ERR("Event query created without GL support\n");
173         ret = WINED3D_EVENT_QUERY_ERROR;
174     }
175
176     context_release(context);
177     return ret;
178 }
179
180 void wined3d_event_query_issue(struct wined3d_event_query *query, const struct wined3d_device *device)
181 {
182     const struct wined3d_gl_info *gl_info;
183     struct wined3d_context *context;
184
185     if (query->context)
186     {
187         if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
188         {
189             context_free_event_query(query);
190             context = context_acquire(device, NULL);
191             context_alloc_event_query(context, query);
192         }
193         else
194         {
195             context = context_acquire(device, query->context->current_rt);
196         }
197     }
198     else
199     {
200         context = context_acquire(device, NULL);
201         context_alloc_event_query(context, query);
202     }
203
204     gl_info = context->gl_info;
205
206     if (gl_info->supported[ARB_SYNC])
207     {
208         if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
209         checkGLcall("glDeleteSync");
210         query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
211         checkGLcall("glFenceSync");
212     }
213     else if (gl_info->supported[APPLE_FENCE])
214     {
215         GL_EXTCALL(glSetFenceAPPLE(query->object.id));
216         checkGLcall("glSetFenceAPPLE");
217     }
218     else if (gl_info->supported[NV_FENCE])
219     {
220         GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
221         checkGLcall("glSetFenceNV");
222     }
223
224     context_release(context);
225 }
226
227 ULONG CDECL wined3d_query_incref(struct wined3d_query *query)
228 {
229     ULONG refcount = InterlockedIncrement(&query->ref);
230
231     TRACE("%p increasing refcount to %u.\n", query, refcount);
232
233     return refcount;
234 }
235
236 ULONG CDECL wined3d_query_decref(struct wined3d_query *query)
237 {
238     ULONG refcount = InterlockedDecrement(&query->ref);
239
240     TRACE("%p decreasing refcount to %u.\n", query, refcount);
241
242     if (!refcount)
243     {
244         /* Queries are specific to the GL context that created them. Not
245          * deleting the query will obviously leak it, but that's still better
246          * than potentially deleting a different query with the same id in this
247          * context, and (still) leaking the actual query. */
248         if (query->type == WINED3D_QUERY_TYPE_EVENT)
249         {
250             struct wined3d_event_query *event_query = query->extendedData;
251             if (event_query) wined3d_event_query_destroy(event_query);
252         }
253         else if (query->type == WINED3D_QUERY_TYPE_OCCLUSION)
254         {
255             struct wined3d_occlusion_query *oq = query->extendedData;
256
257             if (oq->context) context_free_occlusion_query(oq);
258             HeapFree(GetProcessHeap(), 0, query->extendedData);
259         }
260
261         HeapFree(GetProcessHeap(), 0, query);
262     }
263
264     return refcount;
265 }
266
267 HRESULT CDECL wined3d_query_get_data(struct wined3d_query *query,
268         void *data, UINT data_size, DWORD flags)
269 {
270     TRACE("query %p, data %p, data_size %u, flags %#x.\n",
271             query, data, data_size, flags);
272
273     return query->query_ops->query_get_data(query, data, data_size, flags);
274 }
275
276 UINT CDECL wined3d_query_get_data_size(const struct wined3d_query *query)
277 {
278     TRACE("query %p.\n", query);
279
280     return query->data_size;
281 }
282
283 HRESULT CDECL wined3d_query_issue(struct wined3d_query *query, DWORD flags)
284 {
285     TRACE("query %p, flags %#x.\n", query, flags);
286
287     return query->query_ops->query_issue(query, flags);
288 }
289
290 static HRESULT wined3d_occlusion_query_ops_get_data(struct wined3d_query *query,
291         void *pData, DWORD dwSize, DWORD flags)
292 {
293     struct wined3d_occlusion_query *oq = query->extendedData;
294     struct wined3d_device *device = query->device;
295     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
296     struct wined3d_context *context;
297     DWORD* data = pData;
298     GLuint available;
299     GLuint samples;
300     HRESULT res;
301
302     TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
303
304     if (!oq->context)
305         query->state = QUERY_CREATED;
306
307     if (query->state == QUERY_CREATED)
308     {
309         /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
310         TRACE("Query wasn't yet started, returning S_OK\n");
311         if(data) *data = 0;
312         return S_OK;
313     }
314
315     if (query->state == QUERY_BUILDING)
316     {
317         /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
318         TRACE("Query is building, returning S_FALSE\n");
319         return S_FALSE;
320     }
321
322     if (!gl_info->supported[ARB_OCCLUSION_QUERY])
323     {
324         WARN("%p Occlusion queries not supported. Returning 1.\n", query);
325         *data = 1;
326         return S_OK;
327     }
328
329     if (oq->context->tid != GetCurrentThreadId())
330     {
331         FIXME("%p Wrong thread, returning 1.\n", query);
332         *data = 1;
333         return S_OK;
334     }
335
336     context = context_acquire(query->device, oq->context->current_rt);
337
338     GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
339     checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
340     TRACE("available %#x.\n", available);
341
342     if (available)
343     {
344         if (data)
345         {
346             GL_EXTCALL(glGetQueryObjectuivARB(oq->id, GL_QUERY_RESULT_ARB, &samples));
347             checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
348             TRACE("Returning %d samples.\n", samples);
349             *data = samples;
350         }
351         res = S_OK;
352     }
353     else
354     {
355         res = S_FALSE;
356     }
357
358     context_release(context);
359
360     return res;
361 }
362
363 static HRESULT wined3d_event_query_ops_get_data(struct wined3d_query *query,
364         void *pData, DWORD dwSize, DWORD flags)
365 {
366     struct wined3d_event_query *event_query = query->extendedData;
367     BOOL *data = pData;
368     enum wined3d_event_query_result ret;
369
370     TRACE("query %p, pData %p, dwSize %#x, flags %#x.\n", query, pData, dwSize, flags);
371
372     if (!pData || !dwSize) return S_OK;
373     if (!event_query)
374     {
375         WARN("Event query not supported by GL, reporting GPU idle.\n");
376         *data = TRUE;
377         return S_OK;
378     }
379
380     ret = wined3d_event_query_test(event_query, query->device);
381     switch(ret)
382     {
383         case WINED3D_EVENT_QUERY_OK:
384         case WINED3D_EVENT_QUERY_NOT_STARTED:
385             *data = TRUE;
386             break;
387
388         case WINED3D_EVENT_QUERY_WAITING:
389             *data = FALSE;
390             break;
391
392         case WINED3D_EVENT_QUERY_WRONG_THREAD:
393             FIXME("(%p) Wrong thread, reporting GPU idle.\n", query);
394             *data = TRUE;
395             break;
396
397         case WINED3D_EVENT_QUERY_ERROR:
398             ERR("The GL event query failed, returning D3DERR_INVALIDCALL\n");
399             return WINED3DERR_INVALIDCALL;
400     }
401
402     return S_OK;
403 }
404
405 enum wined3d_query_type CDECL wined3d_query_get_type(const struct wined3d_query *query)
406 {
407     TRACE("query %p.\n", query);
408
409     return query->type;
410 }
411
412 static HRESULT wined3d_event_query_ops_issue(struct wined3d_query *query, DWORD flags)
413 {
414     TRACE("query %p, flags %#x.\n", query, flags);
415
416     TRACE("(%p) : flags %#x, type D3DQUERY_EVENT\n", query, flags);
417     if (flags & WINED3DISSUE_END)
418     {
419         struct wined3d_event_query *event_query = query->extendedData;
420
421         /* Faked event query support */
422         if (!event_query) return WINED3D_OK;
423
424         wined3d_event_query_issue(event_query, query->device);
425     }
426     else if (flags & WINED3DISSUE_BEGIN)
427     {
428         /* Started implicitly at device creation */
429         ERR("Event query issued with START flag - what to do?\n");
430     }
431
432     if (flags & WINED3DISSUE_BEGIN)
433         query->state = QUERY_BUILDING;
434     else
435         query->state = QUERY_SIGNALLED;
436
437     return WINED3D_OK;
438 }
439
440 static HRESULT wined3d_occlusion_query_ops_issue(struct wined3d_query *query, DWORD flags)
441 {
442     struct wined3d_device *device = query->device;
443     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
444
445     TRACE("query %p, flags %#x.\n", query, flags);
446
447     if (gl_info->supported[ARB_OCCLUSION_QUERY])
448     {
449         struct wined3d_occlusion_query *oq = query->extendedData;
450         struct wined3d_context *context;
451
452         /* This is allowed according to msdn and our tests. Reset the query and restart */
453         if (flags & WINED3DISSUE_BEGIN)
454         {
455             if (query->state == QUERY_BUILDING)
456             {
457                 if (oq->context->tid != GetCurrentThreadId())
458                 {
459                     FIXME("Wrong thread, can't restart query.\n");
460
461                     context_free_occlusion_query(oq);
462                     context = context_acquire(query->device, NULL);
463                     context_alloc_occlusion_query(context, oq);
464                 }
465                 else
466                 {
467                     context = context_acquire(query->device, oq->context->current_rt);
468
469                     GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
470                     checkGLcall("glEndQuery()");
471                 }
472             }
473             else
474             {
475                 if (oq->context) context_free_occlusion_query(oq);
476                 context = context_acquire(query->device, NULL);
477                 context_alloc_occlusion_query(context, oq);
478             }
479
480             GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, oq->id));
481             checkGLcall("glBeginQuery()");
482
483             context_release(context);
484         }
485         if (flags & WINED3DISSUE_END)
486         {
487             /* Msdn says _END on a non-building occlusion query returns an error, but
488              * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
489              * generating an error
490              */
491             if (query->state == QUERY_BUILDING)
492             {
493                 if (oq->context->tid != GetCurrentThreadId())
494                 {
495                     FIXME("Wrong thread, can't end query.\n");
496                 }
497                 else
498                 {
499                     context = context_acquire(query->device, oq->context->current_rt);
500
501                     GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
502                     checkGLcall("glEndQuery()");
503
504                     context_release(context);
505                 }
506             }
507         }
508     }
509     else
510     {
511         FIXME("%p Occlusion queries not supported.\n", query);
512     }
513
514     if (flags & WINED3DISSUE_BEGIN)
515         query->state = QUERY_BUILDING;
516     else
517         query->state = QUERY_SIGNALLED;
518
519     return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL.    */
520 }
521
522 static const struct wined3d_query_ops event_query_ops =
523 {
524     wined3d_event_query_ops_get_data,
525     wined3d_event_query_ops_issue,
526 };
527
528 static const struct wined3d_query_ops occlusion_query_ops =
529 {
530     wined3d_occlusion_query_ops_get_data,
531     wined3d_occlusion_query_ops_issue,
532 };
533
534 static HRESULT query_init(struct wined3d_query *query, struct wined3d_device *device, enum wined3d_query_type type)
535 {
536     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
537
538     switch (type)
539     {
540         case WINED3D_QUERY_TYPE_OCCLUSION:
541             TRACE("Occlusion query.\n");
542             if (!gl_info->supported[ARB_OCCLUSION_QUERY])
543             {
544                 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
545                 return WINED3DERR_NOTAVAILABLE;
546             }
547             query->query_ops = &occlusion_query_ops;
548             query->data_size = sizeof(DWORD);
549             query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
550             if (!query->extendedData)
551             {
552                 ERR("Failed to allocate occlusion query extended data.\n");
553                 return E_OUTOFMEMORY;
554             }
555             ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
556             break;
557
558         case WINED3D_QUERY_TYPE_EVENT:
559             TRACE("Event query.\n");
560             if (!wined3d_event_query_supported(gl_info))
561             {
562                 /* Half-Life 2 needs this query. It does not render the main
563                  * menu correctly otherwise. Pretend to support it, faking
564                  * this query does not do much harm except potentially
565                  * lowering performance. */
566                 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
567             }
568             query->query_ops = &event_query_ops;
569             query->data_size = sizeof(BOOL);
570             query->extendedData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct wined3d_event_query));
571             if (!query->extendedData)
572             {
573                 ERR("Failed to allocate event query memory.\n");
574                 return E_OUTOFMEMORY;
575             }
576             break;
577
578         case WINED3D_QUERY_TYPE_VCACHE:
579         case WINED3D_QUERY_TYPE_RESOURCE_MANAGER:
580         case WINED3D_QUERY_TYPE_VERTEX_STATS:
581         case WINED3D_QUERY_TYPE_TIMESTAMP:
582         case WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT:
583         case WINED3D_QUERY_TYPE_TIMESTAMP_FREQ:
584         case WINED3D_QUERY_TYPE_PIPELINE_TIMINGS:
585         case WINED3D_QUERY_TYPE_INTERFACE_TIMINGS:
586         case WINED3D_QUERY_TYPE_VERTEX_TIMINGS:
587         case WINED3D_QUERY_TYPE_PIXEL_TIMINGS:
588         case WINED3D_QUERY_TYPE_BANDWIDTH_TIMINGS:
589         case WINED3D_QUERY_TYPE_CACHE_UTILIZATION:
590         default:
591             FIXME("Unhandled query type %#x.\n", type);
592             return WINED3DERR_NOTAVAILABLE;
593     }
594
595     query->type = type;
596     query->state = QUERY_CREATED;
597     query->device = device;
598     query->ref = 1;
599
600     return WINED3D_OK;
601 }
602
603 HRESULT CDECL wined3d_query_create(struct wined3d_device *device,
604         enum wined3d_query_type type, struct wined3d_query **query)
605 {
606     struct wined3d_query *object;
607     HRESULT hr;
608
609     TRACE("device %p, type %#x, query %p.\n", device, type, query);
610
611     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
612     if (!object)
613         return E_OUTOFMEMORY;
614
615     hr = query_init(object, device, type);
616     if (FAILED(hr))
617     {
618         WARN("Failed to initialize query, hr %#x.\n", hr);
619         HeapFree(GetProcessHeap(), 0, object);
620         return hr;
621     }
622
623     TRACE("Created query %p.\n", object);
624     *query = object;
625
626     return WINED3D_OK;
627 }