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