wined3d: Catch invalid buffer map parameters.
[wine] / dlls / wined3d / query.c
1 /*
2  * IWineD3DQuery implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6  * Copyright 2009 Henri Verbeet for CodeWeavers.
7  *
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.
12  *
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.
17  *
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
21  */
22
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 /*
28  * Occlusion Queries:
29  * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
30  * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
31  */
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 #define GLINFO_LOCATION (*gl_info)
35
36 /* *******************************************
37    IWineD3DQuery IUnknown parts follow
38    ******************************************* */
39 static HRESULT  WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, LPVOID *ppobj)
40 {
41     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
42     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
43     if (IsEqualGUID(riid, &IID_IUnknown)
44         || IsEqualGUID(riid, &IID_IWineD3DBase)
45         || IsEqualGUID(riid, &IID_IWineD3DQuery)) {
46         IUnknown_AddRef(iface);
47         *ppobj = This;
48         return S_OK;
49     }
50     *ppobj = NULL;
51     return E_NOINTERFACE;
52 }
53
54 static ULONG  WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
55     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
56     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
57     return InterlockedIncrement(&This->ref);
58 }
59
60 static ULONG  WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
61     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
62     ULONG ref;
63     TRACE("(%p) : Releasing from %d\n", This, This->ref);
64     ref = InterlockedDecrement(&This->ref);
65     if (ref == 0) {
66         /* Queries are specific to the GL context that created them. Not
67          * deleting the query will obviously leak it, but that's still better
68          * than potentially deleting a different query with the same id in this
69          * context, and (still) leaking the actual query. */
70         if (This->type == WINED3DQUERYTYPE_EVENT)
71         {
72             struct wined3d_event_query *query = This->extendedData;
73
74             if (query->context) context_free_event_query(query);
75         }
76         else if (This->type == WINED3DQUERYTYPE_OCCLUSION)
77         {
78             struct wined3d_occlusion_query *query = This->extendedData;
79
80             if (query->context) context_free_occlusion_query(query);
81         }
82
83         HeapFree(GetProcessHeap(), 0, This->extendedData);
84         HeapFree(GetProcessHeap(), 0, This);
85     }
86     return ref;
87 }
88
89 /* *******************************************
90    IWineD3DQuery IWineD3DQuery parts follow
91    ******************************************* */
92 static HRESULT WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown **parent)
93 {
94     TRACE("iface %p, parent %p.\n", iface, parent);
95
96     *parent = (IUnknown *)parent;
97     IUnknown_AddRef(*parent);
98
99     TRACE("Returning %p.\n", *parent);
100
101     return WINED3D_OK;
102 }
103
104 static HRESULT  WINAPI IWineD3DOcclusionQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
105     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
106     struct wined3d_occlusion_query *query = This->extendedData;
107     IWineD3DDeviceImpl *device = This->device;
108     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
109     struct wined3d_context *context;
110     DWORD* data = pData;
111     GLuint available;
112     GLuint samples;
113     HRESULT res;
114
115     TRACE("(%p) : type D3DQUERY_OCCLUSION, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
116
117     if (!query->context) This->state = QUERY_CREATED;
118
119     if (This->state == QUERY_CREATED)
120     {
121         /* D3D allows GetData on a new query, OpenGL doesn't. So just invent the data ourselves */
122         TRACE("Query wasn't yet started, returning S_OK\n");
123         if(data) *data = 0;
124         return S_OK;
125     }
126
127     if (This->state == QUERY_BUILDING)
128     {
129         /* Msdn says this returns an error, but our tests show that S_FALSE is returned */
130         TRACE("Query is building, returning S_FALSE\n");
131         return S_FALSE;
132     }
133
134     if (!gl_info->supported[ARB_OCCLUSION_QUERY])
135     {
136         WARN("(%p) : Occlusion queries not supported. Returning 1.\n", This);
137         *data = 1;
138         return S_OK;
139     }
140
141     if (query->context->tid != GetCurrentThreadId())
142     {
143         FIXME("%p Wrong thread, returning 1.\n", This);
144         *data = 1;
145         return S_OK;
146     }
147
148     context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
149
150     ENTER_GL();
151
152     GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
153     checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT_AVAILABLE)");
154     TRACE("(%p) : available %d.\n", This, available);
155
156     if (available)
157     {
158         if (data)
159         {
160             GL_EXTCALL(glGetQueryObjectuivARB(query->id, GL_QUERY_RESULT_ARB, &samples));
161             checkGLcall("glGetQueryObjectuivARB(GL_QUERY_RESULT)");
162             TRACE("(%p) : Returning %d samples.\n", This, samples);
163             *data = samples;
164         }
165         res = S_OK;
166     }
167     else
168     {
169         res = S_FALSE;
170     }
171
172     LEAVE_GL();
173
174     context_release(context);
175
176     return res;
177 }
178
179 static HRESULT  WINAPI IWineD3DEventQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags) {
180     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *) iface;
181     struct wined3d_event_query *query = This->extendedData;
182     const struct wined3d_gl_info *gl_info;
183     struct wined3d_context *context;
184     BOOL *data = pData;
185
186     TRACE("(%p) : type D3DQUERY_EVENT, pData %p, dwSize %#x, dwGetDataFlags %#x\n", This, pData, dwSize, dwGetDataFlags);
187
188     if (!pData || !dwSize) return S_OK;
189
190     if (!query->context)
191     {
192         TRACE("Query not started, returning TRUE.\n");
193         *data = TRUE;
194
195         return S_OK;
196     }
197
198     if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
199     {
200         /* See comment in IWineD3DQuery::Issue, event query codeblock */
201         FIXME("Wrong thread, reporting GPU idle.\n");
202         *data = TRUE;
203
204         return S_OK;
205     }
206
207     context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
208     gl_info = context->gl_info;
209
210     ENTER_GL();
211
212     if (gl_info->supported[ARB_SYNC])
213     {
214         GLenum ret = GL_EXTCALL(glClientWaitSync(query->object.sync, 0, 0));
215         checkGLcall("glClientWaitSync");
216
217         switch (ret)
218         {
219             case GL_ALREADY_SIGNALED:
220             case GL_CONDITION_SATISFIED:
221                 *data = TRUE;
222                 break;
223
224             case GL_TIMEOUT_EXPIRED:
225                 *data = FALSE;
226                 break;
227
228             case GL_WAIT_FAILED:
229             default:
230                 ERR("glClientWaitSync returned %#x.\n", ret);
231                 *data = FALSE;
232                 break;
233         }
234     }
235     else if (gl_info->supported[APPLE_FENCE])
236     {
237         *data = GL_EXTCALL(glTestFenceAPPLE(query->object.id));
238         checkGLcall("glTestFenceAPPLE");
239     }
240     else if (gl_info->supported[NV_FENCE])
241     {
242         *data = GL_EXTCALL(glTestFenceNV(query->object.id));
243         checkGLcall("glTestFenceNV");
244     }
245     else
246     {
247         WARN("(%p): reporting GPU idle\n", This);
248         *data = TRUE;
249     }
250
251     LEAVE_GL();
252
253     context_release(context);
254
255     return S_OK;
256 }
257
258 static DWORD  WINAPI IWineD3DEventQueryImpl_GetDataSize(IWineD3DQuery* iface){
259     TRACE("(%p) : type D3DQUERY_EVENT\n", iface);
260
261     return sizeof(BOOL);
262 }
263
264 static DWORD  WINAPI IWineD3DOcclusionQueryImpl_GetDataSize(IWineD3DQuery* iface){
265     TRACE("(%p) : type D3DQUERY_OCCLUSION\n", iface);
266
267     return sizeof(DWORD);
268 }
269
270 static WINED3DQUERYTYPE  WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
271     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
272     return This->type;
273 }
274
275
276 static HRESULT  WINAPI IWineD3DEventQueryImpl_Issue(IWineD3DQuery* iface,  DWORD dwIssueFlags) {
277     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
278
279     TRACE("(%p) : dwIssueFlags %#x, type D3DQUERY_EVENT\n", This, dwIssueFlags);
280     if (dwIssueFlags & WINED3DISSUE_END)
281     {
282         struct wined3d_event_query *query = This->extendedData;
283         const struct wined3d_gl_info *gl_info;
284         struct wined3d_context *context;
285
286         if (query->context)
287         {
288             if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
289             {
290                 context_free_event_query(query);
291                 context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
292                 context_alloc_event_query(context, query);
293             }
294             else
295             {
296                 context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
297             }
298         }
299         else
300         {
301             context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
302             context_alloc_event_query(context, query);
303         }
304
305         gl_info = context->gl_info;
306
307         ENTER_GL();
308
309         if (gl_info->supported[ARB_SYNC])
310         {
311             if (query->object.sync) GL_EXTCALL(glDeleteSync(query->object.sync));
312             checkGLcall("glDeleteSync");
313             query->object.sync = GL_EXTCALL(glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0));
314             checkGLcall("glFenceSync");
315         }
316         else if (gl_info->supported[APPLE_FENCE])
317         {
318             GL_EXTCALL(glSetFenceAPPLE(query->object.id));
319             checkGLcall("glSetFenceAPPLE");
320         }
321         else if (gl_info->supported[NV_FENCE])
322         {
323             GL_EXTCALL(glSetFenceNV(query->object.id, GL_ALL_COMPLETED_NV));
324             checkGLcall("glSetFenceNV");
325         }
326
327         LEAVE_GL();
328
329         context_release(context);
330     }
331     else if(dwIssueFlags & WINED3DISSUE_BEGIN)
332     {
333         /* Started implicitly at device creation */
334         ERR("Event query issued with START flag - what to do?\n");
335     }
336
337     if(dwIssueFlags & WINED3DISSUE_BEGIN) {
338         This->state = QUERY_BUILDING;
339     } else {
340         This->state = QUERY_SIGNALLED;
341     }
342
343     return WINED3D_OK;
344 }
345
346 static HRESULT  WINAPI IWineD3DOcclusionQueryImpl_Issue(IWineD3DQuery* iface,  DWORD dwIssueFlags) {
347     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
348     IWineD3DDeviceImpl *device = This->device;
349     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
350
351     if (gl_info->supported[ARB_OCCLUSION_QUERY])
352     {
353         struct wined3d_occlusion_query *query = This->extendedData;
354         struct wined3d_context *context;
355
356         /* This is allowed according to msdn and our tests. Reset the query and restart */
357         if (dwIssueFlags & WINED3DISSUE_BEGIN)
358         {
359             if (This->state == QUERY_BUILDING)
360             {
361                 if (query->context->tid != GetCurrentThreadId())
362                 {
363                     FIXME("Wrong thread, can't restart query.\n");
364
365                     context_free_occlusion_query(query);
366                     context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
367                     context_alloc_occlusion_query(context, query);
368                 }
369                 else
370                 {
371                     context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
372
373                     ENTER_GL();
374                     GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
375                     checkGLcall("glEndQuery()");
376                     LEAVE_GL();
377                 }
378             }
379             else
380             {
381                 if (query->context) context_free_occlusion_query(query);
382                 context = context_acquire(This->device, NULL, CTXUSAGE_RESOURCELOAD);
383                 context_alloc_occlusion_query(context, query);
384             }
385
386             ENTER_GL();
387             GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, query->id));
388             checkGLcall("glBeginQuery()");
389             LEAVE_GL();
390
391             context_release(context);
392         }
393         if (dwIssueFlags & WINED3DISSUE_END) {
394             /* Msdn says _END on a non-building occlusion query returns an error, but
395              * our tests show that it returns OK. But OpenGL doesn't like it, so avoid
396              * generating an error
397              */
398             if (This->state == QUERY_BUILDING)
399             {
400                 if (query->context->tid != GetCurrentThreadId())
401                 {
402                     FIXME("Wrong thread, can't end query.\n");
403                 }
404                 else
405                 {
406                     context = context_acquire(This->device, query->context->current_rt, CTXUSAGE_RESOURCELOAD);
407
408                     ENTER_GL();
409                     GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
410                     checkGLcall("glEndQuery()");
411                     LEAVE_GL();
412
413                     context_release(context);
414                 }
415             }
416         }
417     } else {
418         FIXME("(%p) : Occlusion queries not supported\n", This);
419     }
420
421     if(dwIssueFlags & WINED3DISSUE_BEGIN) {
422         This->state = QUERY_BUILDING;
423     } else {
424         This->state = QUERY_SIGNALLED;
425     }
426     return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL.    */
427 }
428
429 static const IWineD3DQueryVtbl IWineD3DEventQuery_Vtbl =
430 {
431     /*** IUnknown methods ***/
432     IWineD3DQueryImpl_QueryInterface,
433     IWineD3DQueryImpl_AddRef,
434     IWineD3DQueryImpl_Release,
435     /*** IWineD3Dquery methods ***/
436     IWineD3DQueryImpl_GetParent,
437     IWineD3DEventQueryImpl_GetData,
438     IWineD3DEventQueryImpl_GetDataSize,
439     IWineD3DQueryImpl_GetType,
440     IWineD3DEventQueryImpl_Issue
441 };
442
443 static const IWineD3DQueryVtbl IWineD3DOcclusionQuery_Vtbl =
444 {
445     /*** IUnknown methods ***/
446     IWineD3DQueryImpl_QueryInterface,
447     IWineD3DQueryImpl_AddRef,
448     IWineD3DQueryImpl_Release,
449     /*** IWineD3Dquery methods ***/
450     IWineD3DQueryImpl_GetParent,
451     IWineD3DOcclusionQueryImpl_GetData,
452     IWineD3DOcclusionQueryImpl_GetDataSize,
453     IWineD3DQueryImpl_GetType,
454     IWineD3DOcclusionQueryImpl_Issue
455 };
456
457 HRESULT query_init(IWineD3DQueryImpl *query, IWineD3DDeviceImpl *device,
458         WINED3DQUERYTYPE type, IUnknown *parent)
459 {
460     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
461
462     switch (type)
463     {
464         case WINED3DQUERYTYPE_OCCLUSION:
465             TRACE("Occlusion query.\n");
466             if (!gl_info->supported[ARB_OCCLUSION_QUERY])
467             {
468                 WARN("Unsupported in local OpenGL implementation: ARB_OCCLUSION_QUERY.\n");
469                 return WINED3DERR_NOTAVAILABLE;
470             }
471             query->lpVtbl = &IWineD3DOcclusionQuery_Vtbl;
472             query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_occlusion_query));
473             if (!query->extendedData)
474             {
475                 ERR("Failed to allocate occlusion query extended data.\n");
476                 return E_OUTOFMEMORY;
477             }
478             ((struct wined3d_occlusion_query *)query->extendedData)->context = NULL;
479             break;
480
481         case WINED3DQUERYTYPE_EVENT:
482             TRACE("Event query.\n");
483             if (!gl_info->supported[ARB_SYNC] && !gl_info->supported[NV_FENCE]
484                     && !gl_info->supported[APPLE_FENCE])
485             {
486                 /* Half-Life 2 needs this query. It does not render the main
487                  * menu correctly otherwise. Pretend to support it, faking
488                  * this query does not do much harm except potentially
489                  * lowering performance. */
490                 FIXME("Event query: Unimplemented, but pretending to be supported.\n");
491             }
492             query->lpVtbl = &IWineD3DEventQuery_Vtbl;
493             query->extendedData = HeapAlloc(GetProcessHeap(), 0, sizeof(struct wined3d_event_query));
494             if (!query->extendedData)
495             {
496                 ERR("Failed to allocate event query extended data.\n");
497                 return E_OUTOFMEMORY;
498             }
499             ((struct wined3d_event_query *)query->extendedData)->context = NULL;
500             break;
501
502         case WINED3DQUERYTYPE_VCACHE:
503         case WINED3DQUERYTYPE_RESOURCEMANAGER:
504         case WINED3DQUERYTYPE_VERTEXSTATS:
505         case WINED3DQUERYTYPE_TIMESTAMP:
506         case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
507         case WINED3DQUERYTYPE_TIMESTAMPFREQ:
508         case WINED3DQUERYTYPE_PIPELINETIMINGS:
509         case WINED3DQUERYTYPE_INTERFACETIMINGS:
510         case WINED3DQUERYTYPE_VERTEXTIMINGS:
511         case WINED3DQUERYTYPE_PIXELTIMINGS:
512         case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
513         case WINED3DQUERYTYPE_CACHEUTILIZATION:
514         default:
515             FIXME("Unhandled query type %#x.\n", type);
516             return WINED3DERR_NOTAVAILABLE;
517     }
518
519     query->type = type;
520     query->state = QUERY_CREATED;
521     query->device = device;
522     query->parent = parent;
523     query->ref = 1;
524
525     return WINED3D_OK;
526 }