d3d9/tests: Fix NP2 check.
[wine] / dlls / d3d9 / tests / query.c
1 /*
2  * Copyright (C) 2006-2007 Stefan Dösinger(For CodeWeavers)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #include <d3d9.h>
21 #include "wine/test.h"
22
23 static IDirect3D9 *(WINAPI *pDirect3DCreate9)(UINT);
24
25 struct queryInfo
26 {
27     D3DQUERYTYPE type;      /* Query to test */
28     BOOL foundSupported;    /* If at least one windows driver has been found supporting this query */
29     BOOL foundUnsupported;  /* If at least one windows driver has been found which does not support this query */
30 };
31
32 /* When running running this test on windows reveals any differences regarding known supported / unsupported queries,
33  * change this table.
34  *
35  * When marking a query known supported or known unsupported please write one card which supports / does not support
36  * the query.
37  */
38 static struct queryInfo queries[] =
39 {
40     {D3DQUERYTYPE_VCACHE,               TRUE /* geforce 6600 */,    TRUE /* geforce 2 mx */ },
41     {D3DQUERYTYPE_RESOURCEMANAGER,      FALSE,                      TRUE /* geforce 2 mx */ },
42     {D3DQUERYTYPE_VERTEXSTATS,          FALSE,                      TRUE /* geforce 2 mx */ },
43     {D3DQUERYTYPE_EVENT,                TRUE /* geforce 2 mx */,    TRUE /* ati mach64 */   },
44     {D3DQUERYTYPE_OCCLUSION,            TRUE /* radeon M9 */,       TRUE /* geforce 2 mx */ },
45     {D3DQUERYTYPE_TIMESTAMP,            TRUE /* geforce 6600 */,    TRUE /* geforce 2 mx */ },
46     {D3DQUERYTYPE_TIMESTAMPDISJOINT,    TRUE /* geforce 6600 */,    TRUE /* geforce 2 mx */ },
47     {D3DQUERYTYPE_TIMESTAMPFREQ,        TRUE /* geforce 6600 */,    TRUE /* geforce 2 mx */ },
48     {D3DQUERYTYPE_PIPELINETIMINGS,      FALSE,                      TRUE /* geforce 2 mx */ },
49     {D3DQUERYTYPE_INTERFACETIMINGS,     FALSE,                      TRUE /* geforce 2 mx */ },
50     {D3DQUERYTYPE_VERTEXTIMINGS,        FALSE,                      TRUE /* geforce 2 mx */ },
51     {D3DQUERYTYPE_PIXELTIMINGS,         FALSE,                      TRUE /* geforce 2 mx */ },
52     {D3DQUERYTYPE_BANDWIDTHTIMINGS,     FALSE,                      TRUE /* geforce 2 mx */ },
53     {D3DQUERYTYPE_CACHEUTILIZATION,     FALSE,                      TRUE /* geforce 2 mx */ },
54 };
55
56 static const char *queryName(D3DQUERYTYPE type)
57 {
58     switch(type)
59     {
60         case D3DQUERYTYPE_VCACHE:               return "D3DQUERYTYPE_VCACHE";
61         case D3DQUERYTYPE_RESOURCEMANAGER:      return "D3DQUERYTYPE_RESOURCEMANAGER";
62         case D3DQUERYTYPE_VERTEXSTATS:          return "D3DQUERYTYPE_VERTEXSTATS";
63         case D3DQUERYTYPE_EVENT:                return "D3DQUERYTYPE_EVENT";
64         case D3DQUERYTYPE_OCCLUSION:            return "D3DQUERYTYPE_OCCLUSION";
65         case D3DQUERYTYPE_TIMESTAMP:            return "D3DQUERYTYPE_TIMESTAMP";
66         case D3DQUERYTYPE_TIMESTAMPDISJOINT:    return "D3DQUERYTYPE_TIMESTAMPDISJOINT";
67         case D3DQUERYTYPE_TIMESTAMPFREQ:        return "D3DQUERYTYPE_TIMESTAMPFREQ";
68         case D3DQUERYTYPE_PIPELINETIMINGS:      return "D3DQUERYTYPE_PIPELINETIMINGS";
69         case D3DQUERYTYPE_INTERFACETIMINGS:     return "D3DQUERYTYPE_INTERFACETIMINGS";
70         case D3DQUERYTYPE_VERTEXTIMINGS:        return "D3DQUERYTYPE_VERTEXTIMINGS";
71         case D3DQUERYTYPE_PIXELTIMINGS:         return "D3DQUERYTYPE_PIXELTIMINGS";
72         case D3DQUERYTYPE_BANDWIDTHTIMINGS:     return "D3DQUERYTYPE_BANDWIDTHTIMINGS";
73         case D3DQUERYTYPE_CACHEUTILIZATION:     return "D3DQUERYTYPE_CACHEUTILIZATION";
74         default: return "Unexpected query type";
75     }
76 }
77
78 static void test_query_support(IDirect3D9 *pD3d, HWND hwnd)
79 {
80
81     HRESULT               hr;
82
83     IDirect3DDevice9      *pDevice = NULL;
84     D3DPRESENT_PARAMETERS d3dpp;
85     D3DDISPLAYMODE        d3ddm;
86     unsigned int i;
87     IDirect3DQuery9       *pQuery = NULL;
88     BOOL supported;
89
90     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
91     ZeroMemory( &d3dpp, sizeof(d3dpp) );
92     d3dpp.Windowed         = TRUE;
93     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
94     d3dpp.BackBufferFormat = d3ddm.Format;
95
96     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
97                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
98     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
99     if (FAILED(hr))
100     {
101         skip("Failed to create a d3d device\n");
102         goto cleanup;
103     }
104
105     for(i = 0; i < sizeof(queries) / sizeof(queries[0]); i++)
106     {
107         hr = IDirect3DDevice9_CreateQuery(pDevice, queries[i].type, NULL);
108         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE,
109            "IDirect3DDevice9_CreateQuery returned unexpected return value %08x for query %s\n", hr, queryName(queries[i].type));
110
111         supported = (hr == D3D_OK ? TRUE : FALSE);
112         trace("query %s is %s\n", queryName(queries[i].type), supported ? "supported" : "not supported");
113
114         ok(!(supported == TRUE && queries[i].foundSupported == FALSE),
115             "Query %s is supported on this system, but was not found supported before\n",
116             queryName(queries[i].type));
117         ok(!(supported == FALSE && queries[i].foundUnsupported == FALSE),
118             "Query %s is not supported on this system, but was found to be supported on all other systems tested before\n",
119             queryName(queries[i].type));
120
121         hr = IDirect3DDevice9_CreateQuery(pDevice, queries[i].type, &pQuery);
122         ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE,
123            "IDirect3DDevice9_CreateQuery returned unexpected return value %08x for query %s\n", hr, queryName(queries[i].type));
124         ok(!(supported && !pQuery), "Query %s was claimed to be supported, but can't be created\n", queryName(queries[i].type));
125         ok(!(!supported && pQuery), "Query %s was claimed not to be supported, but can be created\n", queryName(queries[i].type));
126         if(pQuery)
127         {
128             IDirect3DQuery9_Release(pQuery);
129             pQuery = NULL;
130         }
131     }
132
133 cleanup:
134     if (pDevice)
135     {
136         UINT refcount = IDirect3DDevice9_Release(pDevice);
137         ok(!refcount, "Device has %u references left.\n", refcount);
138     }
139 }
140
141 static void test_occlusion_query_states(IDirect3D9 *pD3d, HWND hwnd)
142 {
143
144     HRESULT               hr;
145
146     IDirect3DDevice9      *pDevice = NULL;
147     D3DPRESENT_PARAMETERS d3dpp;
148     D3DDISPLAYMODE        d3ddm;
149     IDirect3DQuery9       *pQuery = NULL;
150     BYTE *data = NULL;
151     float point[3] = {0.0, 0.0, 0.0};
152     unsigned int count = 0;
153
154     IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm );
155     ZeroMemory( &d3dpp, sizeof(d3dpp) );
156     d3dpp.Windowed         = TRUE;
157     d3dpp.SwapEffect       = D3DSWAPEFFECT_DISCARD;
158     d3dpp.BackBufferFormat = d3ddm.Format;
159
160     hr = IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
161                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice );
162     ok(SUCCEEDED(hr) || hr == D3DERR_NOTAVAILABLE, "Failed to create IDirect3D9Device (%08x)\n", hr);
163     if (FAILED(hr))
164     {
165         skip("Failed to create a d3d device\n");
166         goto cleanup;
167     }
168
169     hr = IDirect3DDevice9_CreateQuery(pDevice, D3DQUERYTYPE_OCCLUSION, &pQuery);
170     ok(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE,
171        "IDirect3DDevice9_CreateQuery returned unexpected return value %08x\n", hr);
172     if(!pQuery) {
173         skip("Occlusion queries not supported\n");
174         goto cleanup;
175     }
176
177     data = HeapAlloc(GetProcessHeap(), 0, IDirect3DQuery9_GetDataSize(pQuery));
178
179     hr = IDirect3DQuery9_GetData(pQuery, NULL, 0, D3DGETDATA_FLUSH);
180     ok(hr == S_OK, "IDirect3DQuery9_GetData(NULL) on a new query returned %08x\n", hr);
181     hr = IDirect3DQuery9_GetData(pQuery, data, IDirect3DQuery9_GetDataSize(pQuery), D3DGETDATA_FLUSH);
182     ok(hr == S_OK, "IDirect3DQuery9_GetData on a new query returned %08x\n", hr);
183
184     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_END);
185     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_END) on a new not yet started query returned %08x\n", hr);
186
187     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_BEGIN);
188     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_BEGIN) on a new not yet started query returned %08x\n", hr);
189
190     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_BEGIN);
191     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DQUERY_BEGIN) on a started query returned %08x\n", hr);
192
193     *((DWORD *)data) = 0x12345678;
194     hr = IDirect3DQuery9_GetData(pQuery, NULL, 0, D3DGETDATA_FLUSH);
195     ok(hr == S_FALSE || hr == D3D_OK, "IDirect3DQuery9_GetData(NULL) on a started query returned %08x\n", hr);
196     hr = IDirect3DQuery9_GetData(pQuery, data, IDirect3DQuery9_GetDataSize(pQuery), D3DGETDATA_FLUSH);
197     ok(hr == S_FALSE || hr == D3D_OK, "IDirect3DQuery9_GetData on a started query returned %08x\n", hr);
198     if (hr == D3D_OK)
199     {
200         DWORD value = *((DWORD *)data);
201         ok(value == 0, "The unfinished query returned %u, expected 0\n", value);
202     }
203
204     hr = IDirect3DDevice9_SetFVF(pDevice, D3DFVF_XYZ);
205     ok(hr == D3D_OK, "IDirect3DDevice9_SetFVF returned %08x\n", hr);
206     hr = IDirect3DDevice9_BeginScene(pDevice);
207     ok(hr == D3D_OK, "IDirect3DDevice9_BeginScene returned %08x\n", hr);
208     if(SUCCEEDED(hr)) {
209         hr = IDirect3DDevice9_DrawPrimitiveUP(pDevice, D3DPT_POINTLIST, 1, point, 3 * sizeof(float));
210         ok(hr == D3D_OK, "IDirect3DDevice9_DrawPrimitiveUP returned %08x\n", hr);
211         hr = IDirect3DDevice9_EndScene(pDevice);
212         ok(hr == D3D_OK, "IDirect3DDevice9_EndScene returned %08x\n", hr);
213     }
214
215     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_END);
216     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_END) on a started query returned %08x\n", hr);
217
218     hr = S_FALSE;
219     while(hr == S_FALSE && count < 500) {
220         hr = IDirect3DQuery9_GetData(pQuery, NULL, 0, D3DGETDATA_FLUSH);
221         ok(hr == S_OK || hr == S_FALSE, "IDirect3DQuery9_GetData on a ended query returned %08x\n", hr);
222         count++;
223         if(hr == S_FALSE) Sleep(10);
224     }
225     ok(hr == S_OK, "Occlusion query did not finish\n");
226
227     hr = IDirect3DQuery9_GetData(pQuery, data, IDirect3DQuery9_GetDataSize(pQuery), D3DGETDATA_FLUSH);
228     ok(hr == S_OK, "IDirect3DQuery9_GetData on a ended query returned %08x\n", hr);
229     hr = IDirect3DQuery9_GetData(pQuery, data, IDirect3DQuery9_GetDataSize(pQuery), D3DGETDATA_FLUSH);
230     ok(hr == S_OK, "IDirect3DQuery9_GetData a 2nd time on a ended query returned %08x\n", hr);
231
232     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_BEGIN);
233     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_BEGIN) on a new not yet started query returned %08x\n", hr);
234     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_END);
235     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_END) on a started query returned %08x\n", hr);
236     hr = IDirect3DQuery9_Issue(pQuery, D3DISSUE_END);
237     ok(hr == D3D_OK, "IDirect3DQuery9_Issue(D3DISSUE_END) on a ended query returned %08x\n", hr);
238
239 cleanup:
240     HeapFree(GetProcessHeap(), 0, data);
241     if (pQuery) IDirect3DQuery9_Release(pQuery);
242     if (pDevice)
243     {
244         UINT refcount = IDirect3DDevice9_Release(pDevice);
245         ok(!refcount, "Device has %u references left.\n", refcount);
246     }
247 }
248
249 START_TEST(query)
250 {
251     HMODULE d3d9_handle = LoadLibraryA( "d3d9.dll" );
252     if (!d3d9_handle)
253     {
254         skip("Could not load d3d9.dll\n");
255         return;
256     }
257
258     pDirect3DCreate9 = (void *)GetProcAddress( d3d9_handle, "Direct3DCreate9" );
259     ok(pDirect3DCreate9 != NULL, "Failed to get address of Direct3DCreate9\n");
260     if (pDirect3DCreate9)
261     {
262         IDirect3D9            *pD3d = NULL;
263         HWND                  hwnd = NULL;
264         WNDCLASS wc = {0};
265         wc.lpfnWndProc = DefWindowProc;
266         wc.lpszClassName = "d3d9_test_wc";
267         RegisterClass(&wc);
268
269         pD3d = pDirect3DCreate9( D3D_SDK_VERSION );
270         if(!pD3d)
271         {
272             skip("Failed to create Direct3D9 object, not running tests\n");
273             goto out;
274         }
275         hwnd = CreateWindow( wc.lpszClassName, "d3d9_test",
276                 WS_SYSMENU | WS_POPUP, 100, 100, 160, 160, NULL, NULL, NULL, NULL );
277         if(!hwnd)
278         {
279             skip("Failed to create window\n");
280             goto out;
281         }
282
283         test_query_support(pD3d, hwnd);
284         test_occlusion_query_states(pD3d, hwnd);
285
286         out:
287         if(pD3d) IDirect3D9_Release(pD3d);
288         if(hwnd) DestroyWindow(hwnd);
289         UnregisterClassA(wc.lpszClassName, GetModuleHandleA(NULL));
290     }
291 }