d3d9: Add a separate function for query initialization.
[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     IDirect3DVertexDeclaration9_Release(current_decl_ptr);
187 }
188
189 static void test_get_declaration(IDirect3DVertexDeclaration9 *decl_ptr, D3DVERTEXELEMENT9 *vertex_decl, UINT expected_num_elements)
190 {
191     int i;
192     UINT num_elements = 0;
193     D3DVERTEXELEMENT9 *decl = 0;
194     HRESULT hret = 0;
195
196     /* First test only getting the number of elements */
197     num_elements = 0x1337c0de;
198     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
199     ok(hret == D3D_OK && num_elements == expected_num_elements,
200             "GetDeclaration returned: hret 0x%x, num_elements %d. "
201             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
202
203     num_elements = 0;
204     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
205     ok(hret == D3D_OK && num_elements == expected_num_elements,
206             "GetDeclaration returned: hret 0x%x, num_elements %d. "
207             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
208
209     /* Also test the returned data */
210     decl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
211
212     num_elements = 0x1337c0de;
213     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
214     ok(hret == D3D_OK && num_elements == expected_num_elements,
215             "GetDeclaration returned: hret 0x%x, num_elements %d. "
216             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
217     i = memcmp(decl, vertex_decl, num_elements * sizeof(*vertex_decl));
218     ok (!i, "Original and returned vertexdeclarations are not the same\n");
219     ZeroMemory(decl, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
220
221     num_elements = 0;
222     hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
223     ok(hret == D3D_OK && num_elements == expected_num_elements,
224             "GetDeclaration returned: hret 0x%x, num_elements %d. "
225             "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
226     i = memcmp(decl, vertex_decl, num_elements * sizeof(*vertex_decl));
227     ok (!i, "Original and returned vertexdeclarations are not the same\n");
228
229     HeapFree(GetProcessHeap(), 0, decl);
230 }
231
232 /* FIXME: also write a test, which shows that attempting to set
233  * an invalid vertex declaration returns E_FAIL */
234 static HRESULT test_fvf_to_decl(IDirect3DDevice9 *device, IDirect3DVertexDeclaration9 *default_decl,
235         DWORD test_fvf, const D3DVERTEXELEMENT9 expected_elements[], char object_should_change)
236 {
237
238     HRESULT hr;
239     IDirect3DVertexDeclaration9 *result_decl = NULL;
240
241     /* Set a default declaration to make sure it is changed */
242     hr = IDirect3DDevice9_SetVertexDeclaration ( device, default_decl );
243     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
244     if (FAILED(hr)) goto fail;
245
246     /* Set an FVF */
247     hr = IDirect3DDevice9_SetFVF( device, test_fvf);
248     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
249     if (FAILED(hr)) goto fail;
250
251     /* Check if the declaration object changed underneath */
252     hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl);
253     ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
254     if (FAILED(hr)) goto fail;
255     if (object_should_change) {
256        ok(result_decl != default_decl, "result declaration matches original\n");
257        if (result_decl == default_decl) goto fail;
258     } else {
259        ok(result_decl == default_decl, "result declaration does not match original\n");
260        if (result_decl != default_decl) goto fail;
261     }
262
263     /* Declaration content/size test */
264     ok(result_decl != NULL, "result declaration was null\n");
265     if (result_decl == NULL)
266         goto fail;
267     else if (compare_elements(result_decl, expected_elements) != S_OK)
268         goto fail;
269
270     if (result_decl) IUnknown_Release( result_decl );
271     return S_OK;
272
273     fail:
274     if (result_decl) IUnknown_Release( result_decl );
275     return E_FAIL;
276 }
277
278 static HRESULT test_decl_to_fvf(IDirect3DDevice9* device, DWORD default_fvf,
279         const D3DVERTEXELEMENT9 test_decl[], DWORD test_fvf, BOOL todo)
280 {
281
282     HRESULT hr;
283     IDirect3DVertexDeclaration9 *vdecl = NULL;
284
285     DWORD result_fvf = 0xdeadbeef;
286
287     /* Set a default FVF of SPECULAR and DIFFUSE to make sure it is changed back to 0 */
288     hr = IDirect3DDevice9_SetFVF( device, default_fvf);
289     ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
290     if (FAILED(hr)) goto fail;
291
292     /* Create a testing declaration */
293     hr = IDirect3DDevice9_CreateVertexDeclaration( device, test_decl, &vdecl );
294     ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
295     if (FAILED(hr)) goto fail;
296
297     /* Set the declaration */
298     hr = IDirect3DDevice9_SetVertexDeclaration ( device, vdecl );
299     ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
300     if (FAILED(hr)) goto fail;
301
302     /* Check the FVF */
303     hr = IDirect3DDevice9_GetFVF( device, &result_fvf);
304     ok(SUCCEEDED(hr), "GetFVF returned %#x, expected %#x\n", hr, D3D_OK);
305     if (FAILED(hr)) goto fail;
306     if (todo) todo_wine ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
307     else ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
308     if (test_fvf != result_fvf) goto fail;
309
310     IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
311     if (vdecl) IUnknown_Release( vdecl );
312     return S_OK;
313
314     fail:
315     IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
316     if (vdecl) IUnknown_Release( vdecl );
317     return E_FAIL;
318 }
319
320 static void test_fvf_decl_conversion(IDirect3DDevice9 *pDevice)
321 {
322
323     HRESULT hr;
324     unsigned int i;
325
326     IDirect3DVertexDeclaration9* default_decl = NULL;
327     DWORD default_fvf = D3DFVF_SPECULAR | D3DFVF_DIFFUSE;
328     D3DVERTEXELEMENT9 default_elements[] =
329         { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
330           { 0, 4, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
331
332     /* Create a default declaration and FVF that does not match any of the tests */
333     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, default_elements, &default_decl );
334     ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
335     if (FAILED(hr)) goto cleanup;
336
337     /* Test conversions from vertex declaration to an FVF.
338      * For some reason those seem to occur only for POSITION/POSITIONT,
339      * Otherwise the FVF is forced to 0 - maybe this is configuration specific */
340     {
341         CONST D3DVERTEXELEMENT9 test_buffer[] =
342             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
343         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZ, TRUE));
344     }
345     {
346         CONST D3DVERTEXELEMENT9 test_buffer[] =
347             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
348         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZRHW, TRUE));
349     }
350     for (i = 0; i < 4; i++) {
351         CONST D3DVERTEXELEMENT9 test_buffer[] =
352             { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
353         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
354     }
355     {
356         CONST D3DVERTEXELEMENT9 test_buffer[] =
357             { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
358         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
359     }
360     {
361         CONST D3DVERTEXELEMENT9 test_buffer[] =
362             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
363         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
364     }
365     {
366         CONST D3DVERTEXELEMENT9 test_buffer[] =
367             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
368         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
369     }
370     {
371         CONST D3DVERTEXELEMENT9 test_buffer[] =
372             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
373         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
374     }
375     {
376         CONST D3DVERTEXELEMENT9 test_buffer[] =
377             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
378         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
379     }
380
381     /* Make sure textures of different sizes work */
382     {
383         CONST D3DVERTEXELEMENT9 test_buffer[] =
384             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
385         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
386     }
387     {
388         CONST D3DVERTEXELEMENT9 test_buffer[] =
389             { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
390         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
391     }
392     {
393         CONST D3DVERTEXELEMENT9 test_buffer[] =
394             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
395         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
396     }
397     {
398         CONST D3DVERTEXELEMENT9 test_buffer[] =
399             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
400         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
401     }
402
403     /* Make sure the TEXCOORD index works correctly - try several textures */
404     {
405         CONST D3DVERTEXELEMENT9 test_buffer[] =
406             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
407               { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
408               { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
409               { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
410         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
411     }
412
413     /* No FVF mapping available */
414     {
415         CONST D3DVERTEXELEMENT9 test_buffer[] =
416             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
417         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
418     }
419     {
420         CONST D3DVERTEXELEMENT9 test_buffer[] =
421             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
422         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
423     }
424
425     /* Try empty declaration */
426     {
427         CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
428         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
429     }
430
431     /* Now try a combination test */
432     {
433         CONST D3DVERTEXELEMENT9 test_buffer[] =
434             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
435               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
436               { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
437               { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
438               { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
439               { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
440         VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
441     }
442
443     /* Test conversions from FVF to a vertex declaration
444      * These seem to always occur internally. A new declaration object is created if necessary */
445
446     {
447         CONST D3DVERTEXELEMENT9 test_buffer[] =
448             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
449         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZ, test_buffer, 1));
450     }
451     {
452         CONST D3DVERTEXELEMENT9 test_buffer[] =
453             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
454         VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZW, 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     unsigned 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 static void test_unused_type(
790     IDirect3DDevice9* device) {
791
792     HRESULT hr;
793     IDirect3DVertexDeclaration9* result_decl = NULL;
794     unsigned int i;
795
796     static const D3DVERTEXELEMENT9 test_elements[][3] =
797     {
798         {
799             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
800             { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_COLOR   , 0 },
801             D3DDECL_END()
802         },
803         {
804             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
805             { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 0 },
806             D3DDECL_END()
807         },
808         {
809             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
810             { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 1 },
811             D3DDECL_END()
812         },
813         {
814             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
815             { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
816             D3DDECL_END()
817         },
818         {
819             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
820             { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
821             D3DDECL_END()
822         },
823         {
824             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
825             { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL,   0 },
826             D3DDECL_END()
827         },
828         {
829             { 0, 0,  D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
830             { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL,   0 },
831             D3DDECL_END()
832         },
833     };
834
835     for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
836         result_decl = NULL;
837         hr = IDirect3DDevice9_CreateVertexDeclaration(device, test_elements[i], &result_decl);
838         ok(hr == E_FAIL, "CreateVertexDeclaration for declaration %d returned %#x, expected E_FAIL(%#x)\n",
839                               i, hr, E_FAIL);
840         if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
841     }
842 }
843 START_TEST(vertexdeclaration)
844 {
845     static D3DVERTEXELEMENT9 simple_decl[] = {
846         { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
847         D3DDECL_END()};
848     UINT simple_decl_num_elements = sizeof(simple_decl) / sizeof(*simple_decl);
849     IDirect3DDevice9 *device_ptr = 0;
850     IDirect3DVertexDeclaration9 *decl_ptr = 0;
851     ULONG refcount;
852
853     d3d9_handle = LoadLibraryA("d3d9.dll");
854     if (!d3d9_handle)
855     {
856         skip("Could not load d3d9.dll\n");
857         return;
858     }
859
860     device_ptr = init_d3d9();
861     if (!device_ptr)
862     {
863         skip("Failed to initialise d3d9\n");
864         return;
865     }
866
867     decl_ptr = test_create_vertex_declaration(device_ptr, simple_decl);
868     if (!decl_ptr)
869     {
870         skip("Failed to create a vertex declaration\n");
871         return;
872     }
873
874     test_get_set_vertex_declaration(device_ptr, decl_ptr);
875     test_get_declaration(decl_ptr, simple_decl, simple_decl_num_elements);
876     test_fvf_decl_conversion(device_ptr);
877     test_fvf_decl_management(device_ptr);
878     test_vertex_declaration_alignment(device_ptr);
879     test_unused_type(device_ptr);
880
881     IDirect3DVertexDeclaration9_Release(decl_ptr);
882
883     refcount = IDirect3DDevice9_Release(device_ptr);
884     ok(!refcount, "Device has %u references left\n", refcount);
885 }