d3d9: shademode_test: Increase slop in color comparison.
[wine] / dlls / d3d9 / tests / vertexdeclaration.c
1 /*
2  * Copyright (C) 2005 Henri Verbeet
3  * Copyright (C) 2006 Ivan Gyurdiev
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define COBJMACROS
21 #include <d3d9.h>
22 #include "wine/test.h"
23
24 static HMODULE d3d9_handle = 0;
25
26 #define VDECL_CHECK(fcall) \
27     if(fcall != S_OK) \
28         trace(" Test failed on line #%d\n", __LINE__);
29
30 static HWND create_window(void)
31 {
32     WNDCLASS wc = {0};
33     wc.lpfnWndProc = &DefWindowProc;
34     wc.lpszClassName = "d3d9_test_wc";
35     RegisterClass(&wc);
36
37     return CreateWindow("d3d9_test_wc", "d3d9_test",
38             0, 0, 0, 0, 0, 0, 0, 0, 0);
39 }
40
41 static IDirect3DDevice9 *init_d3d9(void)
42 {
43     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
44     IDirect3D9 *d3d9_ptr = 0;
45     IDirect3DDevice9 *device_ptr = 0;
46     D3DPRESENT_PARAMETERS present_parameters;
47     HRESULT hres;
48
49     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
50     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
51     if (!d3d9_create) return NULL;
52     
53     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
54     ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
55     if (!d3d9_ptr) return NULL;
56
57     ZeroMemory(&present_parameters, sizeof(present_parameters));
58     present_parameters.Windowed = TRUE;
59     present_parameters.hDeviceWindow = create_window();
60     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
61
62     hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
63
64     if(FAILED(hres))
65     {
66         hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
67         if(FAILED(hres))
68         {
69             trace("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
70             return NULL;
71         }
72     }
73
74     return device_ptr;
75 }
76
77 static int get_refcount(IUnknown *object)
78 {
79     IUnknown_AddRef(object);
80     return IUnknown_Release(object);
81 }
82
83 static inline void print_elements(
84     D3DVERTEXELEMENT9 *elements) {
85
86     D3DVERTEXELEMENT9 last = D3DDECL_END();
87     D3DVERTEXELEMENT9 *ptr = elements;
88     int count = 0;
89
90     while (memcmp(ptr, &last, sizeof(D3DVERTEXELEMENT9))) {
91
92         trace(
93             "[Element %d] Stream = %d, Offset = %d, Type = %d, Method = %d, Usage = %d, UsageIndex = %d\n",
94              count, ptr->Stream, ptr->Offset, ptr->Type, ptr->Method, ptr->Usage, ptr->UsageIndex);
95
96         ptr++;
97         count++;
98     }
99 }
100
101 static int compare_elements(
102     IDirect3DVertexDeclaration9 *decl,
103     const D3DVERTEXELEMENT9 *expected_elements) {
104
105     HRESULT hr;
106     unsigned int i, size;
107     D3DVERTEXELEMENT9 last = D3DDECL_END();
108     D3DVERTEXELEMENT9 *elements = NULL;
109
110     /* How many elements are there? */
111     hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, NULL, &size );
112     ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
113     if (FAILED(hr)) goto fail;
114
115     /* Allocate buffer */
116     elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * size);
117     ok (elements != NULL, "Out of memory, aborting test\n");
118     if (elements == NULL) goto fail;
119
120     /* Get the elements */
121     hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, elements, &size);
122     ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
123     if (FAILED(hr)) goto fail;
124
125     /* Compare one by one */
126     for (i = 0; i < size; i++) {
127
128         int status;
129
130         int end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
131         int end2 = memcmp(&expected_elements[i], &last, sizeof(D3DVERTEXELEMENT9));
132         status = ((end1 && !end2) || (!end1 && end2));
133         ok (!status, "Mismatch in size, test declaration is %s than expected\n",
134             (end1 && !end2) ? "shorter" : "longer");
135         if (status) { print_elements(elements); goto fail; }
136
137         status = memcmp(&elements[i], &expected_elements[i], sizeof(D3DVERTEXELEMENT9));
138         ok (!status, "Mismatch in element %d\n", i);
139         if (status) { print_elements(elements); goto fail; }
140     }
141
142     HeapFree(GetProcessHeap(), 0, elements);
143     return S_OK;
144
145     fail:
146     HeapFree(GetProcessHeap(), 0, elements);
147     return E_FAIL;
148 }
149
150 static IDirect3DVertexDeclaration9 *test_create_vertex_declaration(IDirect3DDevice9 *device_ptr, D3DVERTEXELEMENT9 *vertex_decl)
151 {
152     IDirect3DVertexDeclaration9 *decl_ptr = 0;
153     HRESULT hret = 0;
154
155     hret = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vertex_decl, &decl_ptr);
156     ok(hret == D3D_OK && decl_ptr != NULL, "CreateVertexDeclaration returned: hret 0x%x, decl_ptr %p. "
157         "Expected hret 0x%x, decl_ptr != %p. Aborting.\n", hret, decl_ptr, D3D_OK, NULL);
158
159     return decl_ptr;
160 }
161
162 static void test_get_set_vertex_declaration(IDirect3DDevice9 *device_ptr, IDirect3DVertexDeclaration9 *decl_ptr)
163 {
164     IDirect3DVertexDeclaration9 *current_decl_ptr = 0;
165     HRESULT hret = 0;
166     int decl_refcount = 0;
167     int i = 0;
168     
169     /* SetVertexDeclaration should not touch the declaration's refcount. */
170     i = get_refcount((IUnknown *)decl_ptr);
171     hret = IDirect3DDevice9_SetVertexDeclaration(device_ptr, decl_ptr);
172     decl_refcount = get_refcount((IUnknown *)decl_ptr);
173     ok(hret == D3D_OK && decl_refcount == i, "SetVertexDeclaration returned: hret 0x%x, refcount %d. "
174         "Expected hret 0x%x, refcount %d.\n", hret, decl_refcount, D3D_OK, i);
175     
176     /* GetVertexDeclaration should increase the declaration's refcount by one. */
177     i = decl_refcount+1;
178     hret = IDirect3DDevice9_GetVertexDeclaration(device_ptr, &current_decl_ptr);
179     decl_refcount = get_refcount((IUnknown *)decl_ptr);
180     ok(hret == D3D_OK && decl_refcount == i && current_decl_ptr == decl_ptr, 
181         "GetVertexDeclaration returned: hret 0x%x, current_decl_ptr %p refcount %d. "
182         "Expected hret 0x%x, current_decl_ptr %p, refcount %d.\n", hret, current_decl_ptr, decl_refcount, D3D_OK, decl_ptr, i);
183 }
184
185 static void test_get_declaration(IDirect3DVertexDeclaration9 *decl_ptr, D3DVERTEXELEMENT9 *vertex_decl, UINT expected_num_elements)
186 {
187     int i;
188     UINT num_elements = 0;
189     D3DVERTEXELEMENT9 *decl = 0;
190     HRESULT hret = 0;
191
192     /* First test only getting the number of elements */
193     num_elements = 0x1337c0de;
194     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
195     ok(hret == D3D_OK && num_elements == expected_num_elements,
196             "GetDeclaration returned: hret 0x%x, num_elements %d. "
197             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
198
199     num_elements = 0;
200     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
201     ok(hret == D3D_OK && num_elements == expected_num_elements,
202             "GetDeclaration returned: hret 0x%x, num_elements %d. "
203             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
204
205     /* Also test the returned data */
206     decl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
207
208     num_elements = 0x1337c0de;
209     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
210     ok(hret == D3D_OK && num_elements == expected_num_elements,
211             "GetDeclaration returned: hret 0x%x, num_elements %d. "
212             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
213     i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
214     ok (!i, "Original and returned vertexdeclarations are not the same\n");
215     ZeroMemory(decl, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
216
217     num_elements = 0;
218     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
219     ok(hret == D3D_OK && num_elements == expected_num_elements,
220             "GetDeclaration returned: hret 0x%x, num_elements %d. "
221             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
222     i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
223     ok (!i, "Original and returned vertexdeclarations are not the same\n");
224
225     HeapFree(GetProcessHeap(), 0, decl);
226 }
227
228 /* FIXME: also write a test, which shows that attempting to set
229  * an invalid vertex declaration returns E_FAIL */
230
231 static HRESULT test_fvf_to_decl(
232     IDirect3DDevice9* device,
233     IDirect3DVertexDeclaration9* default_decl,
234     DWORD test_fvf,
235     const D3DVERTEXELEMENT9 expected_elements[],
236     char object_should_change) 
237 {
238
239     HRESULT hr;
240     IDirect3DVertexDeclaration9 *result_decl = NULL;
241
242     /* Set a default declaration to make sure it is changed */
243     hr = IDirect3DDevice9_SetVertexDeclaration ( device, default_decl );
244     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
245     if (FAILED(hr)) goto fail;
246
247     /* Set an FVF */
248     hr = IDirect3DDevice9_SetFVF( device, test_fvf);
249     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
250     if (FAILED(hr)) goto fail;
251
252     /* Check if the declaration object changed underneath */
253     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl);
254     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
255     if (FAILED(hr)) goto fail;
256     if (object_should_change) {
257        ok(result_decl != default_decl, "result declaration matches original\n");
258        if (result_decl == default_decl) goto fail;
259     } else {
260        ok(result_decl == default_decl, "result declaration does not match original\n");
261        if (result_decl != default_decl) goto fail;
262     }
263
264     /* Declaration content/size test */
265     ok(result_decl != NULL, "result declaration was null\n");
266     if (result_decl == NULL) 
267         goto fail;
268     else if (compare_elements(result_decl, expected_elements) != S_OK)
269         goto fail;
270
271     if (result_decl) IUnknown_Release( result_decl );
272     return S_OK;    
273
274     fail:
275     if (result_decl) IUnknown_Release( result_decl );
276     return E_FAIL;
277 }
278
279 static HRESULT test_decl_to_fvf(
280     IDirect3DDevice9* device,
281     DWORD default_fvf,
282     CONST D3DVERTEXELEMENT9 test_decl[],
283     DWORD test_fvf)
284 {
285
286     HRESULT hr;
287     IDirect3DVertexDeclaration9 *vdecl = NULL;
288
289     DWORD result_fvf = 0xdeadbeef;
290
291     /* Set a default FVF of SPECULAR and DIFFUSE to make sure it is changed back to 0 */
292     hr = IDirect3DDevice9_SetFVF( device, default_fvf);
293     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
294     if (FAILED(hr)) goto fail;
295
296     /* Create a testing declaration */
297     hr = IDirect3DDevice9_CreateVertexDeclaration( device, test_decl, &vdecl );
298     ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
299     if (FAILED(hr)) goto fail;
300
301     /* Set the declaration */
302     hr = IDirect3DDevice9_SetVertexDeclaration ( device, vdecl );
303     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
304     if (FAILED(hr)) goto fail;
305
306     /* Check the FVF */
307     hr = IDirect3DDevice9_GetFVF( device, &result_fvf);
308     ok(SUCCEEDED(hr), "GetFVF returned %#x, expected %#x\n", hr, D3D_OK);
309     if (FAILED(hr)) goto fail;
310     todo_wine {
311        ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
312     }
313     if (test_fvf != result_fvf) goto fail;
314
315     IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
316     if (vdecl) IUnknown_Release( vdecl );
317     return S_OK;
318
319     fail:
320     IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
321     if (vdecl) IUnknown_Release( vdecl );
322     return E_FAIL;
323 }
324
325 static void test_fvf_decl_conversion(IDirect3DDevice9 *pDevice)
326 {
327
328     HRESULT hr;
329     unsigned int i;
330
331     IDirect3DVertexDeclaration9* default_decl = NULL;
332     DWORD default_fvf = D3DFVF_SPECULAR | D3DFVF_DIFFUSE;
333     D3DVERTEXELEMENT9 default_elements[] =
334         { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
335           { 0, 4, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
336
337     /* Create a default declaration and FVF that does not match any of the tests */
338     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, default_elements, &default_decl );
339     ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
340     if (FAILED(hr)) goto cleanup;
341
342     /* Test conversions from vertex declaration to an FVF.
343      * For some reason those seem to occur only for POSITION/POSITIONT,
344      * Otherwise the FVF is forced to 0 - maybe this is configuration specific */
345     {
346         CONST D3DVERTEXELEMENT9 test_buffer[] =
347             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
348         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZ));
349     }
350     {
351         CONST D3DVERTEXELEMENT9 test_buffer[] =
352             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
353         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZRHW));
354     }
355     for (i = 0; i < 4; i++) {
356         CONST D3DVERTEXELEMENT9 test_buffer[] =
357             { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
358         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
359     }
360     {
361         CONST D3DVERTEXELEMENT9 test_buffer[] = 
362             { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
363         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
364     }
365     {
366         CONST D3DVERTEXELEMENT9 test_buffer[] =
367             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
368         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
369     }
370     {
371         CONST D3DVERTEXELEMENT9 test_buffer[] =
372             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
373         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
374     }
375     {
376         CONST D3DVERTEXELEMENT9 test_buffer[] =
377             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
378         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
379     }
380     {
381         CONST D3DVERTEXELEMENT9 test_buffer[] =
382             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
383         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
384     }
385
386     /* Make sure textures of different sizes work */
387     {
388         CONST D3DVERTEXELEMENT9 test_buffer[] =
389             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
390         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
391     }
392     {
393         CONST D3DVERTEXELEMENT9 test_buffer[] =
394             { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
395         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
396     }
397     {
398         CONST D3DVERTEXELEMENT9 test_buffer[] =
399             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
400         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
401     }
402     {
403         CONST D3DVERTEXELEMENT9 test_buffer[] =
404             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
405         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
406     }
407
408     /* Make sure the TEXCOORD index works correctly - try several textures */
409     {
410         CONST D3DVERTEXELEMENT9 test_buffer[] =
411             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
412               { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
413               { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
414               { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
415         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
416     }
417
418     /* No FVF mapping available */
419     {
420         CONST D3DVERTEXELEMENT9 test_buffer[] =
421             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
422         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
423     }
424     {
425         CONST D3DVERTEXELEMENT9 test_buffer[] =
426             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
427         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
428     }
429
430     /* Try empty declaration */
431     {
432         CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
433         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
434     }
435
436     /* Now try a combination test */
437     {
438         CONST D3DVERTEXELEMENT9 test_buffer[] =
439             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
440               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
441               { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
442               { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
443               { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
444               { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
445         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
446     }
447
448     /* Test conversions from FVF to a vertex declaration 
449      * These seem to always occur internally. A new declaration object is created if necessary */
450
451     {
452         CONST D3DVERTEXELEMENT9 test_buffer[] =
453             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
454         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZ, test_buffer, 1));
455     }
456     {
457         CONST D3DVERTEXELEMENT9 test_buffer[] =
458           { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
459         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZRHW, test_buffer, 1));
460     }
461     {
462         CONST D3DVERTEXELEMENT9 test_buffer[] =
463             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
464               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
465               { 0, 28, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
466         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
467             D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
468     }
469     {
470         CONST D3DVERTEXELEMENT9 test_buffer[] =
471             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
472               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
473               { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
474         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
475            D3DFVF_XYZB5 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
476     }
477     {
478         CONST D3DVERTEXELEMENT9 test_buffer[] =
479             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
480               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
481               { 0, 28, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
482         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB5, test_buffer, 1));
483     }
484     {
485         CONST D3DVERTEXELEMENT9 test_buffer[] =
486             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
487               { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
488         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB1, test_buffer, 1));
489     }
490     {
491         CONST D3DVERTEXELEMENT9 test_buffer[] =
492             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
493               { 0, 12, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
494         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
495             D3DFVF_XYZB1 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
496     }
497     {
498         CONST D3DVERTEXELEMENT9 test_buffer[] =
499             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
500               { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
501         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
502             D3DFVF_XYZB1 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
503     }
504     {
505          CONST D3DVERTEXELEMENT9 test_buffer[] =
506              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
507                { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
508          VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB2, test_buffer, 1));
509     }
510     {
511          CONST D3DVERTEXELEMENT9 test_buffer[] =
512              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
513                { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
514                { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
515          VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
516              D3DFVF_XYZB2 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
517      }
518      {
519          CONST D3DVERTEXELEMENT9 test_buffer[] =
520              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
521                { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
522                { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
523          VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
524              D3DFVF_XYZB2 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
525      }
526      {
527         CONST D3DVERTEXELEMENT9 test_buffer[] =
528             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
529               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
530         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB3, test_buffer, 1));
531     }
532     {
533         CONST D3DVERTEXELEMENT9 test_buffer[] =
534             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
535               { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
536               { 0, 20, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
537         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
538             D3DFVF_XYZB3 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
539     }
540     {
541         CONST D3DVERTEXELEMENT9 test_buffer[] =
542             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
543               { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
544               { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
545         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
546             D3DFVF_XYZB3 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
547     }
548     {
549         CONST D3DVERTEXELEMENT9 test_buffer[] =
550             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
551               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
552         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4, test_buffer, 1));
553     }
554     {
555         CONST D3DVERTEXELEMENT9 test_buffer[] =
556             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
557               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
558               { 0, 24, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
559         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
560             D3DFVF_XYZB4 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
561     }
562     {
563         CONST D3DVERTEXELEMENT9 test_buffer[] =
564             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
565               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
566               { 0, 24, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
567         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
568             D3DFVF_XYZB4 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
569     }
570     {
571         CONST D3DVERTEXELEMENT9 test_buffer[] =
572             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
573         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_NORMAL, test_buffer, 1));
574     }
575     {
576         CONST D3DVERTEXELEMENT9 test_buffer[] =
577             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
578         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_PSIZE, test_buffer, 1));
579     }
580     {
581         CONST D3DVERTEXELEMENT9 test_buffer[] =
582             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
583         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_DIFFUSE, test_buffer, 1));
584     }
585     {
586         CONST D3DVERTEXELEMENT9 test_buffer[] =
587             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
588         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_SPECULAR, test_buffer, 1));
589     }
590
591     /* Make sure textures of different sizes work */
592     {
593         CONST D3DVERTEXELEMENT9 test_buffer[] =
594             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
595         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
596             D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEX1, test_buffer, 1));
597     }
598     {
599         CONST D3DVERTEXELEMENT9 test_buffer[] =
600             { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
601         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
602             D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEX1, test_buffer, 1));
603     }
604     {
605         CONST D3DVERTEXELEMENT9 test_buffer[] =
606             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
607         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
608             D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, test_buffer, 1));
609     }
610     {
611         CONST D3DVERTEXELEMENT9 test_buffer[] =
612             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
613         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
614             D3DFVF_TEXCOORDSIZE4(0) | D3DFVF_TEX1, test_buffer, 1));
615     }
616
617     /* Make sure the TEXCOORD index works correctly - try several textures */
618     {
619         CONST D3DVERTEXELEMENT9 test_buffer[] =
620             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
621               { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
622               { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
623               { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
624         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
625             D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEXCOORDSIZE2(2) |
626             D3DFVF_TEXCOORDSIZE4(3) | D3DFVF_TEX4, test_buffer, 1));
627     }
628
629     /* Now try a combination test  */
630     {
631        CONST D3DVERTEXELEMENT9 test_buffer[] =
632                 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
633                   { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
634                   { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
635                   { 0, 32, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
636                   { 0, 36, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 },
637                   { 0, 44, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
638        VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4 | D3DFVF_SPECULAR | D3DFVF_DIFFUSE |
639            D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEX2, test_buffer, 1));
640     }
641
642     /* Setting the FVF to 0 should result in no change to the default decl */
643     VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, 0, default_elements, 0));
644
645     cleanup:
646     IDirect3DDevice9_SetVertexDeclaration ( pDevice, NULL );
647     if ( default_decl ) IUnknown_Release (default_decl);
648 }
649
650 /* Check whether a declaration converted from FVF is shared.
651  * Check whether refcounts behave as expected */
652 static void test_fvf_decl_management(
653     IDirect3DDevice9* device) {
654
655     HRESULT hr;
656     IDirect3DVertexDeclaration9* result_decl1 = NULL;
657     IDirect3DVertexDeclaration9* result_decl2 = NULL;
658     IDirect3DVertexDeclaration9* result_decl3 = NULL;
659     IDirect3DVertexDeclaration9* result_decl4 = NULL;
660     int ref1, ref2, ref3, ref4;
661
662     DWORD test_fvf1 = D3DFVF_XYZRHW;
663     DWORD test_fvf2 = D3DFVF_NORMAL;
664     CONST D3DVERTEXELEMENT9 test_elements1[] =
665         { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
666     CONST D3DVERTEXELEMENT9 test_elements2[] =
667         { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
668
669     /* Clear down any current vertex declaration */
670     hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
671     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
672     if (FAILED(hr)) return;
673
674     /* Conversion */
675     hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
676     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
677     if (FAILED(hr)) return;
678
679     /* Get converted decl (#1) */
680     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl1);
681     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
682     if (FAILED(hr)) return;
683
684     /* Get converted decl again (#2) */
685     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl2);
686     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
687     if (FAILED(hr)) return;
688
689     /* Conversion */
690     hr = IDirect3DDevice9_SetFVF( device, test_fvf2);
691     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
692     if (FAILED(hr)) return;
693
694     /* The contents should correspond to the first conversion */
695     VDECL_CHECK(compare_elements(result_decl1, test_elements1));
696
697     /* Get converted decl (#3) */
698     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl3);
699     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
700     if (FAILED(hr)) return;
701
702     /* The object should be the same */
703     ok (result_decl1 == result_decl2, "Declaration object changes on the second Get() call\n");
704     ok (result_decl2 != result_decl3, "Declaration object did not change during conversion\n");
705
706     /* The contents should correspond to the second conversion */
707     VDECL_CHECK(compare_elements(result_decl3, test_elements2));
708     /* Re-Check if the first decl was overwritten by the new Get() */
709     VDECL_CHECK(compare_elements(result_decl1, test_elements1));
710
711     hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
712     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
713     if (FAILED(hr)) return;
714
715     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl4);
716     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
717     if (FAILED(hr)) return;
718
719     ok(result_decl4 == result_decl1, "Setting an already used FVF over results in a different vertexdeclaration\n");
720
721     ref1 = get_refcount((IUnknown*) result_decl1);
722     ref2 = get_refcount((IUnknown*) result_decl2);
723     ref3 = get_refcount((IUnknown*) result_decl3);
724     ref4 = get_refcount((IUnknown*) result_decl4);
725     ok (ref1 == 3, "Refcount #1 is %d, expected 3\n", ref1);
726     ok (ref2 == 3, "Refcount #2 is %d, expected 3\n", ref2);
727     ok (ref3 == 1, "Refcount #3 is %d, expected 1\n", ref3);
728     ok (ref4 == 3, "Refcount #4 is %d, expected 3\n", ref4);
729
730     /* Clear down any current vertex declaration */
731     hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
732     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
733     if (FAILED(hr)) return;
734
735     IDirect3DVertexDeclaration9_Release(result_decl1);
736     IDirect3DVertexDeclaration9_Release(result_decl2);
737     IDirect3DVertexDeclaration9_Release(result_decl3);
738     IDirect3DVertexDeclaration9_Release(result_decl4);
739
740     return;
741 }
742
743 static void test_vertex_declaration_alignment(
744     IDirect3DDevice9* device) {
745
746     HRESULT hr;
747     IDirect3DVertexDeclaration9* result_decl = NULL;
748     int i;
749
750     CONST D3DVERTEXELEMENT9 test_elements[5][3] =
751     {
752         {
753             { 0, 0, D3DDECLTYPE_FLOAT3,   0, D3DDECLUSAGE_POSITION, 0 },
754             { 0, 16, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR  , 0 },
755             D3DDECL_END()
756         },
757         {
758             { 0, 0, D3DDECLTYPE_FLOAT3,   0, D3DDECLUSAGE_POSITION, 0 },
759             { 0, 17, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR  , 0 },
760             D3DDECL_END()
761         },
762         {
763             { 0, 0, D3DDECLTYPE_FLOAT3,   0, D3DDECLUSAGE_POSITION, 0 },
764             { 0, 18, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR  , 0 },
765             D3DDECL_END()
766         },
767         {
768             { 0, 0, D3DDECLTYPE_FLOAT3,   0, D3DDECLUSAGE_POSITION, 0 },
769             { 0, 19, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR  , 0 },
770             D3DDECL_END()
771         },
772         {
773             { 0, 0, D3DDECLTYPE_FLOAT3,   0, D3DDECLUSAGE_POSITION, 0 },
774             { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR  , 0 },
775             D3DDECL_END()
776         }
777     };
778     HRESULT results[5] = {D3D_OK, E_FAIL, E_FAIL, E_FAIL, D3D_OK};
779
780     for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
781         result_decl = NULL;
782         hr = IDirect3DDevice9_CreateVertexDeclaration(device, test_elements[i], &result_decl);
783         ok(hr == results[i], "CreateVertexDeclaration for declaration %d returned %#x, expected %#x\n",
784                               i, hr, results[i]);
785         if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
786     }
787 }
788
789 START_TEST(vertexdeclaration)
790 {
791     static D3DVERTEXELEMENT9 simple_decl[] = {
792         { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
793         D3DDECL_END()};
794     UINT simple_decl_num_elements = sizeof(simple_decl) / sizeof(*simple_decl);
795     IDirect3DDevice9 *device_ptr = 0;
796     IDirect3DVertexDeclaration9 *decl_ptr = 0;
797
798     d3d9_handle = LoadLibraryA("d3d9.dll");
799     if (!d3d9_handle)
800     {
801         skip("Could not load d3d9.dll\n");
802         return;
803     }
804
805     device_ptr = init_d3d9();
806     if (!device_ptr)
807     {
808         skip("Failed to initialise d3d9\n");
809         return;
810     }
811
812     decl_ptr = test_create_vertex_declaration(device_ptr, simple_decl);
813     if (!decl_ptr)
814     {
815         skip("Failed to create a vertex declaration\n");
816         return;
817     }
818
819     test_get_set_vertex_declaration(device_ptr, decl_ptr);
820     test_get_declaration(decl_ptr, simple_decl, simple_decl_num_elements);
821     test_fvf_decl_conversion(device_ptr);
822     test_fvf_decl_management(device_ptr);
823     test_vertex_declaration_alignment(device_ptr);
824 }