wined3d: Avoid crashing when ProcessVertices is called with NULL vertex declaration.
[wine] / dlls / wined3d / query.c
1 /*
2  * IWineD3DQuery implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  *
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  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/Queries.asp
29  *
30  * Occlusion Queries:
31  * http://www.gris.uni-tuebingen.de/~bartz/Publications/paper/hww98.pdf
32  * http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt
33  */
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
36 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->gl_info
37
38 /* *******************************************
39    IWineD3DQuery IUnknown parts follow
40    ******************************************* */
41 static HRESULT  WINAPI IWineD3DQueryImpl_QueryInterface(IWineD3DQuery *iface, REFIID riid, LPVOID *ppobj)
42 {
43     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
44     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
45     if (IsEqualGUID(riid, &IID_IUnknown)
46         || IsEqualGUID(riid, &IID_IWineD3DBase)
47         || IsEqualGUID(riid, &IID_IWineD3DQuery)) {
48         IUnknown_AddRef(iface);
49         *ppobj = This;
50         return S_OK;
51     }
52     *ppobj = NULL;
53     return E_NOINTERFACE;
54 }
55
56 static ULONG  WINAPI IWineD3DQueryImpl_AddRef(IWineD3DQuery *iface) {
57     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
58     TRACE("(%p) : AddRef increasing from %ld\n", This, This->ref);
59     return InterlockedIncrement(&This->ref);
60 }
61
62 static ULONG  WINAPI IWineD3DQueryImpl_Release(IWineD3DQuery *iface) {
63     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
64     ULONG ref;
65     TRACE("(%p) : Releasing from %ld\n", This, This->ref);
66     ref = InterlockedDecrement(&This->ref);
67     if (ref == 0) {
68         HeapFree(GetProcessHeap(), 0, This->extendedData);
69         HeapFree(GetProcessHeap(), 0, This);
70     }
71     return ref;
72 }
73
74 /* *******************************************
75    IWineD3DQuery IWineD3DQuery parts follow
76    ******************************************* */
77 static HRESULT  WINAPI IWineD3DQueryImpl_GetParent(IWineD3DQuery *iface, IUnknown** parent){
78     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
79
80     *parent= (IUnknown*) parent;
81     IUnknown_AddRef(*parent);
82     TRACE("(%p) : returning %p\n", This, *parent);
83     return WINED3D_OK;
84 }
85
86 static HRESULT  WINAPI IWineD3DQueryImpl_GetDevice(IWineD3DQuery* iface, IWineD3DDevice **pDevice){
87     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
88     IWineD3DDevice_AddRef((IWineD3DDevice *)This->wineD3DDevice);
89     *pDevice = (IWineD3DDevice *)This->wineD3DDevice;
90     TRACE("(%p) returning %p\n", This, *pDevice);
91     return WINED3D_OK;
92 }
93
94
95 static HRESULT  WINAPI IWineD3DQueryImpl_GetData(IWineD3DQuery* iface, void* pData, DWORD dwSize, DWORD dwGetDataFlags){
96     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
97     HRESULT res = S_OK;
98
99     TRACE("(%p) : type %#x, pData %p, dwSize %#lx, dwGetDataFlags %#lx\n", This, This->type, pData, dwSize, dwGetDataFlags);
100
101     if(dwSize == 0){
102         /*you can use this method to poll the resource for the query status*/
103         /*We return success(S_OK) if we support a feature, and faikure(S_FALSE) if we don't, just return success and fluff it for now*/
104         return S_OK;
105     }else{
106     }
107
108     switch (This->type){
109
110     case WINED3DQUERYTYPE_VCACHE:
111     {
112
113         WINED3DDEVINFO_VCACHE *data = (WINED3DDEVINFO_VCACHE *)pData;
114         data->Pattern     = MAKEFOURCC('C','A','C','H');
115         data->OptMethod   = 0; /*0 get longest strips, 1 optimize vertex cache*/
116         data->CacheSize   = 0; /*cache size, only required if OptMethod == 1*/
117         data->MagicNumber = 0; /*only required if OptMethod == 1 (used internally)*/
118
119     }
120     break;
121     case WINED3DQUERYTYPE_RESOURCEMANAGER:
122     {
123         WINED3DDEVINFO_RESOURCEMANAGER *data = (WINED3DDEVINFO_RESOURCEMANAGER *)pData;
124         int i;
125         for(i = 0; i < WINED3DRTYPECOUNT; i++){
126             /*I'm setting the default values to 1 so as to reduce the risk of a div/0 in the caller*/
127             /*  isTextureResident could be used to get some of this infomration  */
128             data->stats[i].bThrashing            = FALSE;
129             data->stats[i].ApproxBytesDownloaded = 1;
130             data->stats[i].NumEvicts             = 1;
131             data->stats[i].NumVidCreates         = 1;
132             data->stats[i].LastPri               = 1;
133             data->stats[i].NumUsed               = 1;
134             data->stats[i].NumUsedInVidMem       = 1;
135             data->stats[i].WorkingSet            = 1;
136             data->stats[i].WorkingSetBytes       = 1;
137             data->stats[i].TotalManaged          = 1;
138             data->stats[i].TotalBytes            = 1;
139         }
140
141     }
142     break;
143     case WINED3DQUERYTYPE_VERTEXSTATS:
144     {
145         WINED3DDEVINFO_VERTEXSTATS *data = (WINED3DDEVINFO_VERTEXSTATS *)pData;
146         data->NumRenderedTriangles      = 1;
147         data->NumExtraClippingTriangles = 1;
148
149     }
150     break;
151     case WINED3DQUERYTYPE_EVENT:
152     {
153         BOOL* data = pData;
154         *data = TRUE; /*Don't know what this is supposed to be*/
155     }
156     break;
157     case WINED3DQUERYTYPE_OCCLUSION:
158     {
159         DWORD* data = pData;
160         if (GL_SUPPORT(ARB_OCCLUSION_QUERY)) {
161             GLuint available;
162             GLuint samples;
163             GLuint queryId = ((WineQueryOcclusionData *)This->extendedData)->queryId;
164
165             GL_EXTCALL(glGetQueryObjectuivARB(queryId, GL_QUERY_RESULT_AVAILABLE_ARB, &available));
166             checkGLcall("glGetQueryObjectiv(GL_QUERY_RESULT_AVAILABLE)\n");
167             TRACE("(%p) : available %d.\n", This, available);
168
169             if (available || dwGetDataFlags & WINED3DGETDATA_FLUSH) {
170                 GL_EXTCALL(glGetQueryObjectuivARB(queryId, GL_QUERY_RESULT_ARB, &samples));
171                 checkGLcall("glGetQueryObjectuiv(GL_QUERY_RESULT)\n");
172                 TRACE("(%p) : Returning %d samples.\n", This, samples);
173                 *data = samples;
174                 res = S_OK;
175             } else {
176                 res = S_FALSE;
177             }
178         } else {
179             FIXME("(%p) : Occlusion queries not supported. Returning 1.\n", This);
180             *data = 1;
181             res = S_OK;
182         }
183     }
184     break;
185     case WINED3DQUERYTYPE_TIMESTAMP:
186     {
187         UINT64* data = pData;
188         *data = 1; /*Don't know what this is supposed to be*/
189     }
190     break;
191     case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
192     {
193         BOOL* data = pData;
194         *data = FALSE; /*Don't know what this is supposed to be*/
195     }
196     break;
197     case WINED3DQUERYTYPE_TIMESTAMPFREQ:
198     {
199         UINT64* data = pData;
200         *data = 1; /*Don't know what this is supposed to be*/
201     }
202     break;
203     case WINED3DQUERYTYPE_PIPELINETIMINGS:
204     {
205         WINED3DDEVINFO_PIPELINETIMINGS *data = (WINED3DDEVINFO_PIPELINETIMINGS *)pData;
206
207         data->VertexProcessingTimePercent    =   1.0f;
208         data->PixelProcessingTimePercent     =   1.0f;
209         data->OtherGPUProcessingTimePercent  =  97.0f;
210         data->GPUIdleTimePercent             =   1.0f;
211     }
212     break;
213     case WINED3DQUERYTYPE_INTERFACETIMINGS:
214     {
215         WINED3DDEVINFO_INTERFACETIMINGS *data = (WINED3DDEVINFO_INTERFACETIMINGS *)pData;
216
217         data->WaitingForGPUToUseApplicationResourceTimePercent =   1.0f;
218         data->WaitingForGPUToAcceptMoreCommandsTimePercent     =   1.0f;
219         data->WaitingForGPUToStayWithinLatencyTimePercent      =   1.0f;
220         data->WaitingForGPUExclusiveResourceTimePercent        =   1.0f;
221         data->WaitingForGPUOtherTimePercent                    =  96.0f;
222     }
223
224     break;
225     case WINED3DQUERYTYPE_VERTEXTIMINGS:
226     {
227         WINED3DDEVINFO_STAGETIMINGS *data = (WINED3DDEVINFO_STAGETIMINGS *)pData;
228         data->MemoryProcessingPercent      = 50.0f;
229         data->ComputationProcessingPercent = 50.0f;
230
231     }
232     break;
233     case WINED3DQUERYTYPE_PIXELTIMINGS:
234     {
235         WINED3DDEVINFO_STAGETIMINGS *data = (WINED3DDEVINFO_STAGETIMINGS *)pData;
236         data->MemoryProcessingPercent      = 50.0f;
237         data->ComputationProcessingPercent = 50.0f;
238     }
239     break;
240     case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
241     {
242         WINED3DDEVINFO_BANDWIDTHTIMINGS *data = (WINED3DDEVINFO_BANDWIDTHTIMINGS *)pData;
243         data->MaxBandwidthUtilized                =  1.0f;
244         data->FrontEndUploadMemoryUtilizedPercent =  1.0f;
245         data->VertexRateUtilizedPercent           =  1.0f;
246         data->TriangleSetupRateUtilizedPercent    =  1.0f;
247         data->FillRateUtilizedPercent             = 97.0f;
248     }
249     break;
250     case WINED3DQUERYTYPE_CACHEUTILIZATION:
251     {
252         WINED3DDEVINFO_CACHEUTILIZATION *data = (WINED3DDEVINFO_CACHEUTILIZATION *)pData;
253
254         data->TextureCacheHitRate             = 1.0f;
255         data->PostTransformVertexCacheHitRate = 1.0f;
256     }
257
258
259     break;
260     default:
261         FIXME("(%p) Unhandled query type %d\n",This , This->type);
262
263     };
264
265     /*dwGetDataFlags = 0 || D3DGETDATA_FLUSH
266     D3DGETDATA_FLUSH may return WINED3DERR_DEVICELOST if the device is lost
267     */
268     FIXME("(%p) : type %#x, Partial stub\n", This, This->type);
269     return res; /* S_OK if the query data is available*/
270 }
271
272
273 static DWORD  WINAPI IWineD3DQueryImpl_GetDataSize(IWineD3DQuery* iface){
274     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
275     int dataSize = 0;
276     TRACE("(%p) : type %#x\n", This, This->type);
277     switch(This->type){
278     case WINED3DQUERYTYPE_VCACHE:
279         dataSize = sizeof(WINED3DDEVINFO_VCACHE);
280         break;
281     case WINED3DQUERYTYPE_RESOURCEMANAGER:
282         dataSize = sizeof(WINED3DDEVINFO_RESOURCEMANAGER);
283         break;
284     case WINED3DQUERYTYPE_VERTEXSTATS:
285         dataSize = sizeof(WINED3DDEVINFO_VERTEXSTATS);
286         break;
287     case WINED3DQUERYTYPE_EVENT:
288         dataSize = sizeof(BOOL);
289         break;
290     case WINED3DQUERYTYPE_OCCLUSION:
291         dataSize = sizeof(DWORD);
292         break;
293     case WINED3DQUERYTYPE_TIMESTAMP:
294         dataSize = sizeof(UINT64);
295         break;
296     case WINED3DQUERYTYPE_TIMESTAMPDISJOINT:
297         dataSize = sizeof(BOOL);
298         break;
299     case WINED3DQUERYTYPE_TIMESTAMPFREQ:
300         dataSize = sizeof(UINT64);
301         break;
302     case WINED3DQUERYTYPE_PIPELINETIMINGS:
303         dataSize = sizeof(WINED3DDEVINFO_PIPELINETIMINGS);
304         break;
305     case WINED3DQUERYTYPE_INTERFACETIMINGS:
306         dataSize = sizeof(WINED3DDEVINFO_INTERFACETIMINGS);
307         break;
308     case WINED3DQUERYTYPE_VERTEXTIMINGS:
309         dataSize = sizeof(WINED3DDEVINFO_STAGETIMINGS);
310         break;
311     case WINED3DQUERYTYPE_PIXELTIMINGS:
312         dataSize = sizeof(WINED3DDEVINFO_STAGETIMINGS);
313         break;
314     case WINED3DQUERYTYPE_BANDWIDTHTIMINGS:
315         dataSize = sizeof(WINED3DQUERYTYPE_BANDWIDTHTIMINGS);
316         break;
317     case WINED3DQUERYTYPE_CACHEUTILIZATION:
318         dataSize = sizeof(WINED3DDEVINFO_CACHEUTILIZATION);
319         break;
320     default:
321        FIXME("(%p) Unhandled query type %d\n",This , This->type);
322        dataSize = 0;
323     }
324     return dataSize;
325 }
326
327
328 static WINED3DQUERYTYPE  WINAPI IWineD3DQueryImpl_GetType(IWineD3DQuery* iface){
329     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
330     return This->type;
331 }
332
333
334 static HRESULT  WINAPI IWineD3DQueryImpl_Issue(IWineD3DQuery* iface,  DWORD dwIssueFlags){
335     IWineD3DQueryImpl *This = (IWineD3DQueryImpl *)iface;
336
337     TRACE("(%p) : dwIssueFlags %#lx, type %#x\n", This, dwIssueFlags, This->type);
338
339     switch (This->type) {
340         case WINED3DQUERYTYPE_OCCLUSION:
341             if (GL_SUPPORT(ARB_OCCLUSION_QUERY)) {
342                 if (dwIssueFlags & D3DISSUE_BEGIN) {
343                     GL_EXTCALL(glBeginQueryARB(GL_SAMPLES_PASSED_ARB, ((WineQueryOcclusionData *)This->extendedData)->queryId));
344                     checkGLcall("glBeginQuery()");
345                 }
346                 if (dwIssueFlags & D3DISSUE_END) {
347                     GL_EXTCALL(glEndQueryARB(GL_SAMPLES_PASSED_ARB));
348                     checkGLcall("glEndQuery()");
349                 }
350             } else {
351                 FIXME("(%p) : Occlusion queries not supported\n", This);
352             }
353             break;
354
355         default:
356             FIXME("(%p) : Unhandled query type %#x\n", This, This->type);
357             break;
358     }
359
360     return WINED3D_OK; /* can be WINED3DERR_INVALIDCALL.    */
361 }
362
363
364 /**********************************************************
365  * IWineD3DQuery VTbl follows
366  **********************************************************/
367
368 const IWineD3DQueryVtbl IWineD3DQuery_Vtbl =
369 {
370     /*** IUnknown methods ***/
371     IWineD3DQueryImpl_QueryInterface,
372     IWineD3DQueryImpl_AddRef,
373     IWineD3DQueryImpl_Release,
374      /*** IWineD3Dquery methods ***/
375     IWineD3DQueryImpl_GetParent,
376     IWineD3DQueryImpl_GetDevice,
377     IWineD3DQueryImpl_GetData,
378     IWineD3DQueryImpl_GetDataSize,
379     IWineD3DQueryImpl_GetType,
380     IWineD3DQueryImpl_Issue
381 };