d3dx9_36/tests: Remove a (mostly) redundant test.
[wine] / dlls / d3dx9_36 / tests / mesh.c
1 /*
2  * Copyright 2008 David Adam
3  * Copyright 2008 Luis Busquets
4  * Copyright 2009 Henri Verbeet for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22 #include "wine/test.h"
23 #include "d3dx9.h"
24
25 #define admitted_error 0.0001f
26
27 #define compare_vertex_sizes(type, exp) \
28     got=D3DXGetFVFVertexSize(type); \
29     ok(got==exp, "Expected: %d, Got: %d\n", exp, got);
30
31 static BOOL compare(FLOAT u, FLOAT v)
32 {
33     return (fabs(u-v) < admitted_error);
34 }
35
36 static BOOL compare_vec3(D3DXVECTOR3 u, D3DXVECTOR3 v)
37 {
38     return ( compare(u.x, v.x) && compare(u.y, v.y) && compare(u.z, v.z) );
39 }
40
41 struct vertex
42 {
43     D3DXVECTOR3 position;
44     D3DXVECTOR3 normal;
45 };
46
47 typedef WORD face[3];
48
49 static BOOL compare_face(face a, face b)
50 {
51     return (a[0]==b[0] && a[1] == b[1] && a[2] == b[2]);
52 }
53
54 struct mesh
55 {
56     DWORD number_of_vertices;
57     struct vertex *vertices;
58
59     DWORD number_of_faces;
60     face *faces;
61 };
62
63 static void free_mesh(struct mesh *mesh)
64 {
65     HeapFree(GetProcessHeap(), 0, mesh->faces);
66     HeapFree(GetProcessHeap(), 0, mesh->vertices);
67 }
68
69 static BOOL new_mesh(struct mesh *mesh, DWORD number_of_vertices, DWORD number_of_faces)
70 {
71     mesh->vertices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, number_of_vertices * sizeof(*mesh->vertices));
72     if (!mesh->vertices)
73     {
74         return FALSE;
75     }
76     mesh->number_of_vertices = number_of_vertices;
77
78     mesh->faces = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, number_of_faces * sizeof(*mesh->faces));
79     if (!mesh->faces)
80     {
81         HeapFree(GetProcessHeap(), 0, mesh->vertices);
82         return FALSE;
83     }
84     mesh->number_of_faces = number_of_faces;
85
86     return TRUE;
87 }
88
89 static void compare_mesh(const char *name, ID3DXMesh *d3dxmesh, struct mesh *mesh)
90 {
91     HRESULT hr;
92     DWORD number_of_vertices, number_of_faces;
93     IDirect3DVertexBuffer9 *vertex_buffer;
94     IDirect3DIndexBuffer9 *index_buffer;
95     D3DVERTEXBUFFER_DESC vertex_buffer_description;
96     D3DINDEXBUFFER_DESC index_buffer_description;
97     struct vertex *vertices;
98     face *faces;
99     int expected, i;
100
101     number_of_vertices = d3dxmesh->lpVtbl->GetNumVertices(d3dxmesh);
102     ok(number_of_vertices == mesh->number_of_vertices, "Test %s, result %u, expected %d\n",
103        name, number_of_vertices, mesh->number_of_vertices);
104
105     number_of_faces = d3dxmesh->lpVtbl->GetNumFaces(d3dxmesh);
106     ok(number_of_faces == mesh->number_of_faces, "Test %s, result %u, expected %d\n",
107        name, number_of_faces, mesh->number_of_faces);
108
109     /* vertex buffer */
110     hr = d3dxmesh->lpVtbl->GetVertexBuffer(d3dxmesh, &vertex_buffer);
111     ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
112
113     if (hr != D3D_OK)
114     {
115         skip("Couldn't get vertex buffer\n");
116     }
117     else
118     {
119         hr = IDirect3DVertexBuffer9_GetDesc(vertex_buffer, &vertex_buffer_description);
120         ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
121
122         if (hr != D3D_OK)
123         {
124             skip("Couldn't get vertex buffer description\n");
125         }
126         else
127         {
128             ok(vertex_buffer_description.Format == D3DFMT_VERTEXDATA, "Test %s, result %x, expected %x (D3DFMT_VERTEXDATA)\n",
129                name, vertex_buffer_description.Format, D3DFMT_VERTEXDATA);
130             ok(vertex_buffer_description.Type == D3DRTYPE_VERTEXBUFFER, "Test %s, result %x, expected %x (D3DRTYPE_VERTEXBUFFER)\n",
131                name, vertex_buffer_description.Type, D3DRTYPE_VERTEXBUFFER);
132             ok(vertex_buffer_description.Usage == 0, "Test %s, result %x, expected %x\n", name, vertex_buffer_description.Usage, 0);
133             ok(vertex_buffer_description.Pool == D3DPOOL_MANAGED, "Test %s, result %x, expected %x (D3DPOOL_DEFAULT)\n",
134                name, vertex_buffer_description.Pool, D3DPOOL_DEFAULT);
135             expected = number_of_vertices * sizeof(D3DXVECTOR3) * 2;
136             ok(vertex_buffer_description.Size == expected, "Test %s, result %x, expected %x\n",
137                name, vertex_buffer_description.Size, expected);
138             ok(vertex_buffer_description.FVF == (D3DFVF_XYZ | D3DFVF_NORMAL), "Test %s, result %x, expected %x (D3DFVF_XYZ | D3DFVF_NORMAL)\n",
139                name, vertex_buffer_description.FVF, D3DFVF_XYZ | D3DFVF_NORMAL);
140         }
141
142         /* specify offset and size to avoid potential overruns */
143         hr = IDirect3DVertexBuffer9_Lock(vertex_buffer, 0, number_of_vertices * sizeof(D3DXVECTOR3) * 2,
144                                          (LPVOID *)&vertices, D3DLOCK_DISCARD);
145         ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
146
147         if (hr != D3D_OK)
148         {
149             skip("Couldn't lock vertex buffer\n");
150         }
151         else
152         {
153             for (i = 0; i < number_of_vertices; i++)
154             {
155                 ok(compare_vec3(vertices[i].position, mesh->vertices[i].position),
156                    "Test %s, vertex position %d, result (%g, %g, %g), expected (%g, %g, %g)\n", name, i,
157                    vertices[i].position.x, vertices[i].position.y, vertices[i].position.z,
158                    mesh->vertices[i].position.x, mesh->vertices[i].position.y, mesh->vertices[i].position.z);
159                 ok(compare_vec3(vertices[i].normal, mesh->vertices[i].normal),
160                    "Test %s, vertex normal %d, result (%g, %g, %g), expected (%g, %g, %g)\n", name, i,
161                    vertices[i].normal.x, vertices[i].normal.y, vertices[i].normal.z,
162                    mesh->vertices[i].normal.x, mesh->vertices[i].normal.y, mesh->vertices[i].normal.z);
163             }
164
165             IDirect3DVertexBuffer9_Unlock(vertex_buffer);
166         }
167
168         IDirect3DVertexBuffer9_Release(vertex_buffer);
169     }
170
171     /* index buffer */
172     hr = d3dxmesh->lpVtbl->GetIndexBuffer(d3dxmesh, &index_buffer);
173     ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
174
175     if (!index_buffer)
176     {
177         skip("Couldn't get index buffer\n");
178     }
179     else
180     {
181         hr = IDirect3DIndexBuffer9_GetDesc(index_buffer, &index_buffer_description);
182         ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
183
184         if (hr != D3D_OK)
185         {
186             skip("Couldn't get index buffer description\n");
187         }
188         else
189         {
190             ok(index_buffer_description.Format == D3DFMT_INDEX16, "Test %s, result %x, expected %x (D3DFMT_INDEX16)\n",
191                name, index_buffer_description.Format, D3DFMT_INDEX16);
192             ok(index_buffer_description.Type == D3DRTYPE_INDEXBUFFER, "Test %s, result %x, expected %x (D3DRTYPE_INDEXBUFFER)\n",
193                name, index_buffer_description.Type, D3DRTYPE_INDEXBUFFER);
194             ok(index_buffer_description.Usage == 0, "Test %s, result %x, expected %x\n", name, index_buffer_description.Usage, 0);
195             ok(index_buffer_description.Pool == D3DPOOL_MANAGED, "Test %s, result %x, expected %x (D3DPOOL_DEFAULT)\n",
196                name, index_buffer_description.Pool, D3DPOOL_DEFAULT);
197             expected = number_of_faces * sizeof(WORD) * 3;
198             ok(index_buffer_description.Size == expected, "Test %s, result %x, expected %x\n",
199                name, index_buffer_description.Size, expected);
200         }
201
202         /* specify offset and size to avoid potential overruns */
203         hr = IDirect3DIndexBuffer9_Lock(index_buffer, 0, number_of_faces * sizeof(WORD) * 3,
204                                         (LPVOID *)&faces, D3DLOCK_DISCARD);
205         ok(hr == D3D_OK, "Test %s, result %x, expected 0 (D3D_OK)\n", name, hr);
206
207         if (hr != D3D_OK)
208         {
209             skip("Couldn't lock index buffer\n");
210         }
211         else
212         {
213             for (i = 0; i < number_of_faces; i++)
214             {
215                 ok(compare_face(faces[i], mesh->faces[i]),
216                    "Test %s, face %d, result (%u, %u, %u), expected (%u, %u, %u)\n", name, i,
217                    faces[i][0], faces[i][1], faces[i][2],
218                    mesh->faces[i][0], mesh->faces[i][1], mesh->faces[i][2]);
219             }
220
221             IDirect3DIndexBuffer9_Unlock(index_buffer);
222         }
223
224         IDirect3DIndexBuffer9_Release(index_buffer);
225     }
226 }
227
228 static void D3DXBoundProbeTest(void)
229 {
230     BOOL result;
231     D3DXVECTOR3 bottom_point, center, top_point, raydirection, rayposition;
232     FLOAT radius;
233
234 /*____________Test the Box case___________________________*/
235     bottom_point.x = -3.0f; bottom_point.y = -2.0f; bottom_point.z = -1.0f;
236     top_point.x = 7.0f; top_point.y = 8.0f; top_point.z = 9.0f;
237
238     raydirection.x = -4.0f; raydirection.y = -5.0f; raydirection.z = -6.0f;
239     rayposition.x = 5.0f; rayposition.y = 5.0f; rayposition.z = 11.0f;
240     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
241     ok(result == TRUE, "expected TRUE, received FALSE\n");
242
243     raydirection.x = 4.0f; raydirection.y = 5.0f; raydirection.z = 6.0f;
244     rayposition.x = 5.0f; rayposition.y = 5.0f; rayposition.z = 11.0f;
245     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
246     ok(result == FALSE, "expected FALSE, received TRUE\n");
247
248     rayposition.x = -4.0f; rayposition.y = 1.0f; rayposition.z = -2.0f;
249     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
250     ok(result == TRUE, "expected TRUE, received FALSE\n");
251
252     bottom_point.x = 1.0f; bottom_point.y = 0.0f; bottom_point.z = 0.0f;
253     top_point.x = 1.0f; top_point.y = 0.0f; top_point.z = 0.0f;
254     rayposition.x = 0.0f; rayposition.y = 1.0f; rayposition.z = 0.0f;
255     raydirection.x = 0.0f; raydirection.y = 3.0f; raydirection.z = 0.0f;
256     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
257     ok(result == FALSE, "expected FALSE, received TRUE\n");
258
259     bottom_point.x = 1.0f; bottom_point.y = 2.0f; bottom_point.z = 3.0f;
260     top_point.x = 10.0f; top_point.y = 15.0f; top_point.z = 20.0f;
261
262     raydirection.x = 7.0f; raydirection.y = 8.0f; raydirection.z = 9.0f;
263     rayposition.x = 3.0f; rayposition.y = 7.0f; rayposition.z = -6.0f;
264     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
265     ok(result == TRUE, "expected TRUE, received FALSE\n");
266
267     bottom_point.x = 0.0f; bottom_point.y = 0.0f; bottom_point.z = 0.0f;
268     top_point.x = 1.0f; top_point.y = 1.0f; top_point.z = 1.0f;
269
270     raydirection.x = 0.0f; raydirection.y = 1.0f; raydirection.z = .0f;
271     rayposition.x = -3.0f; rayposition.y = 0.0f; rayposition.z = 0.0f;
272     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
273     ok(result == FALSE, "expected FALSE, received TRUE\n");
274
275     raydirection.x = 1.0f; raydirection.y = 0.0f; raydirection.z = .0f;
276     rayposition.x = -3.0f; rayposition.y = 0.0f; rayposition.z = 0.0f;
277     result = D3DXBoxBoundProbe(&bottom_point, &top_point, &rayposition, &raydirection);
278     ok(result == TRUE, "expected TRUE, received FALSE\n");
279
280 /*____________Test the Sphere case________________________*/
281     radius = sqrt(77.0f);
282     center.x = 1.0f; center.y = 2.0f; center.z = 3.0f;
283     raydirection.x = 2.0f; raydirection.y = -4.0f; raydirection.z = 2.0f;
284
285     rayposition.x = 5.0f; rayposition.y = 5.0f; rayposition.z = 9.0f;
286     result = D3DXSphereBoundProbe(&center, radius, &rayposition, &raydirection);
287     ok(result == TRUE, "expected TRUE, received FALSE\n");
288
289     rayposition.x = 45.0f; rayposition.y = -75.0f; rayposition.z = 49.0f;
290     result = D3DXSphereBoundProbe(&center, radius, &rayposition, &raydirection);
291     ok(result == FALSE, "expected FALSE, received TRUE\n");
292
293     rayposition.x = 5.0f; rayposition.y = 11.0f; rayposition.z = 9.0f;
294     result = D3DXSphereBoundProbe(&center, radius, &rayposition, &raydirection);
295     ok(result == FALSE, "expected FALSE, received TRUE\n");
296 }
297
298 static void D3DXComputeBoundingBoxTest(void)
299 {
300     D3DXVECTOR3 exp_max, exp_min, got_max, got_min, vertex[5];
301     HRESULT hr;
302
303     vertex[0].x = 1.0f; vertex[0].y = 1.0f; vertex[0].z = 1.0f;
304     vertex[1].x = 1.0f; vertex[1].y = 1.0f; vertex[1].z = 1.0f;
305     vertex[2].x = 1.0f; vertex[2].y = 1.0f; vertex[2].z = 1.0f;
306     vertex[3].x = 1.0f; vertex[3].y = 1.0f; vertex[3].z = 1.0f;
307     vertex[4].x = 9.0f; vertex[4].y = 9.0f; vertex[4].z = 9.0f;
308
309     exp_min.x = 1.0f; exp_min.y = 1.0f; exp_min.z = 1.0f;
310     exp_max.x = 9.0f; exp_max.y = 9.0f; exp_max.z = 9.0f;
311
312     hr = D3DXComputeBoundingBox(&vertex[3],2,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_min,&got_max);
313
314     ok( hr == D3D_OK, "Expected D3D_OK, got %#x\n", hr);
315     ok( compare_vec3(exp_min,got_min), "Expected min: (%f, %f, %f), got: (%f, %f, %f)\n", exp_min.x,exp_min.y,exp_min.z,got_min.x,got_min.y,got_min.z);
316     ok( compare_vec3(exp_max,got_max), "Expected max: (%f, %f, %f), got: (%f, %f, %f)\n", exp_max.x,exp_max.y,exp_max.z,got_max.x,got_max.y,got_max.z);
317
318 /*________________________*/
319
320     vertex[0].x = 2.0f; vertex[0].y = 5.9f; vertex[0].z = -1.2f;
321     vertex[1].x = -1.87f; vertex[1].y = 7.9f; vertex[1].z = 7.4f;
322     vertex[2].x = 7.43f; vertex[2].y = -0.9f; vertex[2].z = 11.9f;
323     vertex[3].x = -6.92f; vertex[3].y = 6.3f; vertex[3].z = -3.8f;
324     vertex[4].x = 11.4f; vertex[4].y = -8.1f; vertex[4].z = 4.5f;
325
326     exp_min.x = -6.92f; exp_min.y = -8.1f; exp_min.z = -3.80f;
327     exp_max.x = 11.4f; exp_max.y = 7.90f; exp_max.z = 11.9f;
328
329     hr = D3DXComputeBoundingBox(&vertex[0],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_min,&got_max);
330
331     ok( hr == D3D_OK, "Expected D3D_OK, got %#x\n", hr);
332     ok( compare_vec3(exp_min,got_min), "Expected min: (%f, %f, %f), got: (%f, %f, %f)\n", exp_min.x,exp_min.y,exp_min.z,got_min.x,got_min.y,got_min.z);
333     ok( compare_vec3(exp_max,got_max), "Expected max: (%f, %f, %f), got: (%f, %f, %f)\n", exp_max.x,exp_max.y,exp_max.z,got_max.x,got_max.y,got_max.z);
334
335 /*________________________*/
336
337     vertex[0].x = 2.0f; vertex[0].y = 5.9f; vertex[0].z = -1.2f;
338     vertex[1].x = -1.87f; vertex[1].y = 7.9f; vertex[1].z = 7.4f;
339     vertex[2].x = 7.43f; vertex[2].y = -0.9f; vertex[2].z = 11.9f;
340     vertex[3].x = -6.92f; vertex[3].y = 6.3f; vertex[3].z = -3.8f;
341     vertex[4].x = 11.4f; vertex[4].y = -8.1f; vertex[4].z = 4.5f;
342
343     exp_min.x = -6.92f; exp_min.y = -0.9f; exp_min.z = -3.8f;
344     exp_max.x = 7.43f; exp_max.y = 7.90f; exp_max.z = 11.9f;
345
346     hr = D3DXComputeBoundingBox(&vertex[0],4,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_min,&got_max);
347
348     ok( hr == D3D_OK, "Expected D3D_OK, got %#x\n", hr);
349     ok( compare_vec3(exp_min,got_min), "Expected min: (%f, %f, %f), got: (%f, %f, %f)\n", exp_min.x,exp_min.y,exp_min.z,got_min.x,got_min.y,got_min.z);
350     ok( compare_vec3(exp_max,got_max), "Expected max: (%f, %f, %f), got: (%f, %f, %f)\n", exp_max.x,exp_max.y,exp_max.z,got_max.x,got_max.y,got_max.z);
351
352 /*________________________*/
353     hr = D3DXComputeBoundingBox(NULL,5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_min,&got_max);
354     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
355
356 /*________________________*/
357     hr = D3DXComputeBoundingBox(&vertex[3],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),NULL,&got_max);
358     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
359
360 /*________________________*/
361     hr = D3DXComputeBoundingBox(&vertex[3],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_min,NULL);
362     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
363 }
364
365 static void D3DXComputeBoundingSphereTest(void)
366 {
367     D3DXVECTOR3 exp_cen, got_cen, vertex[5];
368     FLOAT exp_rad, got_rad;
369     HRESULT hr;
370
371     vertex[0].x = 1.0f; vertex[0].y = 1.0f; vertex[0].z = 1.0f;
372     vertex[1].x = 1.0f; vertex[1].y = 1.0f; vertex[1].z = 1.0f;
373     vertex[2].x = 1.0f; vertex[2].y = 1.0f; vertex[2].z = 1.0f;
374     vertex[3].x = 1.0f; vertex[3].y = 1.0f; vertex[3].z = 1.0f;
375     vertex[4].x = 9.0f; vertex[4].y = 9.0f; vertex[4].z = 9.0f;
376
377     exp_rad = 6.928203f;
378     exp_cen.x = 5.0; exp_cen.y = 5.0; exp_cen.z = 5.0;
379
380     hr = D3DXComputeBoundingSphere(&vertex[3],2,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_cen,&got_rad);
381
382     ok( hr == D3D_OK, "Expected D3D_OK, got %#x\n", hr);
383     ok( compare(exp_rad, got_rad), "Expected radius: %f, got radius: %f\n", exp_rad, got_rad);
384     ok( compare_vec3(exp_cen,got_cen), "Expected center: (%f, %f, %f), got center: (%f, %f, %f)\n", exp_cen.x,exp_cen.y,exp_cen.z,got_cen.x,got_cen.y,got_cen.z);
385
386 /*________________________*/
387
388     vertex[0].x = 2.0f; vertex[0].y = 5.9f; vertex[0].z = -1.2f;
389     vertex[1].x = -1.87f; vertex[1].y = 7.9f; vertex[1].z = 7.4f;
390     vertex[2].x = 7.43f; vertex[2].y = -0.9f; vertex[2].z = 11.9f;
391     vertex[3].x = -6.92f; vertex[3].y = 6.3f; vertex[3].z = -3.8f;
392     vertex[4].x = 11.4f; vertex[4].y = -8.1f; vertex[4].z = 4.5f;
393
394     exp_rad = 13.707883f;
395     exp_cen.x = 2.408f; exp_cen.y = 2.22f; exp_cen.z = 3.76f;
396
397     hr = D3DXComputeBoundingSphere(&vertex[0],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_cen,&got_rad);
398
399     ok( hr == D3D_OK, "Expected D3D_OK, got %#x\n", hr);
400     ok( compare(exp_rad, got_rad), "Expected radius: %f, got radius: %f\n", exp_rad, got_rad);
401     ok( compare_vec3(exp_cen,got_cen), "Expected center: (%f, %f, %f), got center: (%f, %f, %f)\n", exp_cen.x,exp_cen.y,exp_cen.z,got_cen.x,got_cen.y,got_cen.z);
402
403 /*________________________*/
404     hr = D3DXComputeBoundingSphere(NULL,5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_cen,&got_rad);
405     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
406
407 /*________________________*/
408     hr = D3DXComputeBoundingSphere(&vertex[3],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),NULL,&got_rad);
409     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
410
411 /*________________________*/
412     hr = D3DXComputeBoundingSphere(&vertex[3],5,D3DXGetFVFVertexSize(D3DFVF_XYZ),&got_cen,NULL);
413     ok( hr == D3DERR_INVALIDCALL, "Expected D3DERR_INVALIDCALL, got %#x\n", hr);
414 }
415
416 static inline void print_elements(const D3DVERTEXELEMENT9 *elements)
417 {
418     D3DVERTEXELEMENT9 last = D3DDECL_END();
419     const D3DVERTEXELEMENT9 *ptr = elements;
420     int count = 0;
421
422     while (memcmp(ptr, &last, sizeof(D3DVERTEXELEMENT9)))
423     {
424         trace(
425             "[Element %d] Stream = %d, Offset = %d, Type = %d, Method = %d, Usage = %d, UsageIndex = %d\n",
426              count, ptr->Stream, ptr->Offset, ptr->Type, ptr->Method, ptr->Usage, ptr->UsageIndex);
427         ptr++;
428         count++;
429     }
430 }
431
432 static inline void copy_elements(D3DVERTEXELEMENT9 *decl, const D3DVERTEXELEMENT9 *elements)
433 {
434     unsigned int i;
435     D3DVERTEXELEMENT9 last = D3DDECL_END();
436     int end1;
437
438     for (i = 0; i < MAX_FVF_DECL_SIZE; i++)
439     {
440         memcpy(&decl[i], &elements[i], sizeof(D3DVERTEXELEMENT9));
441         end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
442         if (!end1) break;
443     }
444 }
445
446 static void compare_elements(const D3DVERTEXELEMENT9 *elements, const D3DVERTEXELEMENT9 *expected_elements,
447                              unsigned int line)
448 {
449     unsigned int i;
450     D3DVERTEXELEMENT9 last = D3DDECL_END();
451     int status, end1, end2;
452
453     for (i = 0; i < MAX_FVF_DECL_SIZE; i++)
454     {
455         end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
456         end2 = memcmp(&expected_elements[i], &last, sizeof(D3DVERTEXELEMENT9));
457
458         if (!end1 && !end2) break;
459
460         status = ((end1 && !end2) || (!end1 && end2));
461         ok (!status, "Mismatch in size, test declaration is %s than expected, line #%u\n",
462             (end1 && !end2) ? "shorter" : "longer", line);
463         if (status) { print_elements(elements); break; }
464
465         status = memcmp(&elements[i], &expected_elements[i], sizeof(D3DVERTEXELEMENT9));
466         ok (!status, "Mismatch in element %d, line #%u\n", i, line);
467         if (status) { print_elements(elements); break; }
468     }
469 }
470
471 static void test_fvf_to_decl(DWORD test_fvf, const D3DVERTEXELEMENT9 expected_elements[], HRESULT expected_hr,
472                              BOOL todo, unsigned int line)
473 {
474     HRESULT hr;
475     D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
476
477     hr = D3DXDeclaratorFromFVF(test_fvf, decl);
478     if (todo) todo_wine ok(hr == expected_hr, "D3DXDeclaratorFromFVF returned %#x, expected %#x, line #%u\n",
479                            hr, expected_hr, line);
480     else ok(hr == expected_hr, "D3DXDeclaratorFromFVF returned %#x, expected %#x, line #%u\n",
481             hr, expected_hr, line);
482     if (SUCCEEDED(hr)) { compare_elements(decl, expected_elements, line); }
483 }
484
485 static void test_decl_to_fvf(const D3DVERTEXELEMENT9 test_decl[], DWORD expected_fvf, HRESULT expected_hr,
486                              BOOL todo, unsigned int line)
487 {
488     HRESULT hr;
489     DWORD result_fvf = 0xdeadbeef;
490     D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
491
492     copy_elements(decl, test_decl);
493
494     hr = D3DXFVFFromDeclarator((D3DVERTEXELEMENT9 **)&decl, &result_fvf);
495     if (todo) todo_wine ok(hr == expected_hr, "D3DXFVFFromDeclarator returned %#x, expected %#x, line #%u\n",
496                            hr, expected_hr, line);
497     else ok(hr == expected_hr, "D3DXFVFFromDeclarator returned %#x, expected %#x, line #%u\n",
498             hr, expected_hr, line);
499     if (SUCCEEDED(hr))
500     {
501         ok(expected_fvf == result_fvf, "result FVF was %#x, expected %#x, line #%u\n",
502            result_fvf, expected_fvf, line);
503     }
504 }
505
506 #define D3DFVF_RESERVED1 0x020 /* in d3dtypes.h */
507
508 static void test_fvf_decl_conversion(void)
509 {
510     int i;
511
512     /* Test conversions from vertex declaration to an FVF */
513     {
514         CONST D3DVERTEXELEMENT9 test_buffer[] =
515             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
516         test_decl_to_fvf(test_buffer, D3DFVF_XYZ, D3D_OK, TRUE, __LINE__);
517     }
518     {
519         CONST D3DVERTEXELEMENT9 test_buffer[] =
520             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
521         test_decl_to_fvf(test_buffer, D3DFVF_XYZRHW, D3D_OK, TRUE, __LINE__);
522     }
523     for (i = 0; i < 4; i++) {
524         CONST D3DVERTEXELEMENT9 test_buffer[] =
525             { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
526         test_decl_to_fvf(test_buffer, 0, D3DERR_INVALIDCALL, TRUE, __LINE__);
527     }
528     {
529         CONST D3DVERTEXELEMENT9 test_buffer[] =
530             { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
531         test_decl_to_fvf(test_buffer, 0, D3DERR_INVALIDCALL, TRUE, __LINE__);
532     }
533     {
534         CONST D3DVERTEXELEMENT9 test_buffer[] =
535             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
536         test_decl_to_fvf(test_buffer, D3DFVF_NORMAL, D3D_OK, TRUE, __LINE__);
537     }
538     {
539         CONST D3DVERTEXELEMENT9 test_buffer[] =
540             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
541         test_decl_to_fvf(test_buffer, D3DFVF_RESERVED1, D3D_OK, TRUE, __LINE__);
542     }
543     {
544         CONST D3DVERTEXELEMENT9 test_buffer[] =
545             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
546         test_decl_to_fvf(test_buffer, D3DFVF_DIFFUSE, D3D_OK, TRUE, __LINE__);
547     }
548     {
549         CONST D3DVERTEXELEMENT9 test_buffer[] =
550             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
551         test_decl_to_fvf(test_buffer, D3DFVF_SPECULAR, D3D_OK, TRUE, __LINE__);
552     }
553
554     /* Make sure textures of different sizes work */
555     {
556         CONST D3DVERTEXELEMENT9 test_buffer[] =
557             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
558         test_decl_to_fvf(test_buffer, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE1(0),
559                          D3D_OK, TRUE, __LINE__);
560     }
561     {
562         CONST D3DVERTEXELEMENT9 test_buffer[] =
563             { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
564         test_decl_to_fvf(test_buffer, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0),
565                          D3D_OK, TRUE, __LINE__);
566     }
567     {
568         CONST D3DVERTEXELEMENT9 test_buffer[] =
569             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
570         test_decl_to_fvf(test_buffer, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0),
571                          D3D_OK, TRUE, __LINE__);
572     }
573     {
574         CONST D3DVERTEXELEMENT9 test_buffer[] =
575             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
576         test_decl_to_fvf(test_buffer, D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE4(0),
577                          D3D_OK, TRUE, __LINE__);
578     }
579
580     /* Make sure the TEXCOORD index works correctly - try several textures */
581     {
582         CONST D3DVERTEXELEMENT9 test_buffer[] =
583             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
584               { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
585               { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
586               { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
587         test_decl_to_fvf(test_buffer, D3DFVF_TEX4 |
588                          D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEXCOORDSIZE2(2) |
589                          D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEXCOORDSIZE4(3),
590                          D3D_OK, TRUE, __LINE__);
591     }
592
593     /* Vary usage index */
594     {
595         CONST D3DVERTEXELEMENT9 test_buffer[] =
596             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
597         test_decl_to_fvf(test_buffer, D3DFVF_XYZ, D3D_OK, TRUE, __LINE__);
598     }
599     {
600         CONST D3DVERTEXELEMENT9 test_buffer[] =
601             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
602         test_decl_to_fvf(test_buffer, D3DFVF_NORMAL, D3D_OK, TRUE, __LINE__);
603     }
604
605     /* Try empty declaration */
606     {
607         CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
608         test_decl_to_fvf(test_buffer, 0, D3D_OK, TRUE, __LINE__);
609     }
610
611     /* Now try a combination test */
612     {
613         CONST D3DVERTEXELEMENT9 test_buffer[] =
614             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
615               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
616               { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
617               { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
618               { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
619               { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
620         test_decl_to_fvf(test_buffer, 0, D3DERR_INVALIDCALL, TRUE, __LINE__);
621     }
622
623     /* Test conversions from FVF to a vertex declaration */
624     {
625         CONST D3DVERTEXELEMENT9 test_buffer[] =
626             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
627         test_fvf_to_decl(D3DFVF_XYZ, test_buffer, D3D_OK, TRUE, __LINE__);
628     }
629     {
630         CONST D3DVERTEXELEMENT9 test_buffer[] =
631             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
632         test_fvf_to_decl(D3DFVF_XYZW, test_buffer, D3DERR_INVALIDCALL, TRUE, __LINE__);
633     }
634     {
635         CONST D3DVERTEXELEMENT9 test_buffer[] =
636           { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
637         test_fvf_to_decl(D3DFVF_XYZRHW, test_buffer, D3D_OK, TRUE, __LINE__);
638     }
639     {
640         CONST D3DVERTEXELEMENT9 test_buffer[] =
641             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
642               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
643               { 0, 28, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
644         test_fvf_to_decl(D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4, test_buffer, D3D_OK, TRUE, __LINE__);
645     }
646     {
647         CONST D3DVERTEXELEMENT9 test_buffer[] =
648             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
649               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
650               { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
651         test_fvf_to_decl(D3DFVF_XYZB5 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer,
652                          D3D_OK, TRUE, __LINE__);
653     }
654     {
655         CONST D3DVERTEXELEMENT9 test_buffer[] =
656             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
657               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
658               { 0, 28, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
659         test_fvf_to_decl(D3DFVF_XYZB5, test_buffer, D3DERR_INVALIDCALL, TRUE, __LINE__);
660     }
661     {
662         CONST D3DVERTEXELEMENT9 test_buffer[] =
663             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
664               { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
665         test_fvf_to_decl(D3DFVF_XYZB1, test_buffer, D3D_OK, TRUE, __LINE__);
666     }
667     {
668         CONST D3DVERTEXELEMENT9 test_buffer[] =
669             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
670               { 0, 12, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
671         test_fvf_to_decl(D3DFVF_XYZB1 | D3DFVF_LASTBETA_UBYTE4, test_buffer, D3D_OK, TRUE, __LINE__);
672     }
673     {
674         CONST D3DVERTEXELEMENT9 test_buffer[] =
675             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
676               { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
677         test_fvf_to_decl(D3DFVF_XYZB1 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, D3D_OK, TRUE, __LINE__);
678     }
679     {
680          CONST D3DVERTEXELEMENT9 test_buffer[] =
681              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
682                { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
683          test_fvf_to_decl(D3DFVF_XYZB2, test_buffer, D3D_OK, TRUE, __LINE__);
684     }
685     {
686          CONST D3DVERTEXELEMENT9 test_buffer[] =
687              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
688                { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
689                { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
690          test_fvf_to_decl(D3DFVF_XYZB2 | D3DFVF_LASTBETA_UBYTE4, test_buffer,
691                           D3D_OK, TRUE, __LINE__);
692      }
693      {
694          CONST D3DVERTEXELEMENT9 test_buffer[] =
695              { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
696                { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
697                { 0, 16, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
698          test_fvf_to_decl(D3DFVF_XYZB2 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer,
699                           D3D_OK, TRUE, __LINE__);
700      }
701      {
702         CONST D3DVERTEXELEMENT9 test_buffer[] =
703             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
704               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
705         test_fvf_to_decl(D3DFVF_XYZB3, test_buffer, D3D_OK, TRUE, __LINE__);
706     }
707     {
708         CONST D3DVERTEXELEMENT9 test_buffer[] =
709             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
710               { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
711               { 0, 20, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
712         test_fvf_to_decl(D3DFVF_XYZB3 | D3DFVF_LASTBETA_UBYTE4, test_buffer,
713                          D3D_OK, TRUE, __LINE__);
714     }
715     {
716         CONST D3DVERTEXELEMENT9 test_buffer[] =
717             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
718               { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
719               { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
720         test_fvf_to_decl(D3DFVF_XYZB3 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer,
721                          D3D_OK, TRUE, __LINE__);
722     }
723     {
724         CONST D3DVERTEXELEMENT9 test_buffer[] =
725             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
726               { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
727         test_fvf_to_decl(D3DFVF_XYZB4, test_buffer, D3D_OK, TRUE, __LINE__);
728     }
729     {
730         CONST D3DVERTEXELEMENT9 test_buffer[] =
731             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
732               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
733               { 0, 24, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
734         test_fvf_to_decl(D3DFVF_XYZB4 | D3DFVF_LASTBETA_UBYTE4, test_buffer,
735                          D3D_OK, TRUE, __LINE__);
736     }
737     {
738         CONST D3DVERTEXELEMENT9 test_buffer[] =
739             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
740               { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
741               { 0, 24, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
742         test_fvf_to_decl(D3DFVF_XYZB4 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, D3D_OK, TRUE, __LINE__);
743     }
744     {
745         CONST D3DVERTEXELEMENT9 test_buffer[] =
746             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
747         test_fvf_to_decl(D3DFVF_NORMAL, test_buffer, D3D_OK, TRUE, __LINE__);
748     }
749     {
750         CONST D3DVERTEXELEMENT9 test_buffer[] =
751             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
752         test_fvf_to_decl(D3DFVF_PSIZE, test_buffer, D3D_OK, TRUE, __LINE__);
753     }
754     {
755         CONST D3DVERTEXELEMENT9 test_buffer[] =
756             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
757         test_fvf_to_decl(D3DFVF_DIFFUSE, test_buffer, D3D_OK, TRUE, __LINE__);
758     }
759     {
760         CONST D3DVERTEXELEMENT9 test_buffer[] =
761             { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
762         test_fvf_to_decl(D3DFVF_SPECULAR, test_buffer, D3D_OK, TRUE, __LINE__);
763     }
764
765     /* Make sure textures of different sizes work */
766     {
767         CONST D3DVERTEXELEMENT9 test_buffer[] =
768             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
769         test_fvf_to_decl(D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEX1, test_buffer,
770                          D3D_OK, TRUE, __LINE__);
771     }
772     {
773         CONST D3DVERTEXELEMENT9 test_buffer[] =
774             { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
775         test_fvf_to_decl(D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEX1, test_buffer, D3D_OK, TRUE, __LINE__);
776     }
777     {
778         CONST D3DVERTEXELEMENT9 test_buffer[] =
779             { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
780         test_fvf_to_decl(D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, test_buffer, D3D_OK, TRUE, __LINE__);
781     }
782     {
783         CONST D3DVERTEXELEMENT9 test_buffer[] =
784             { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
785         test_fvf_to_decl(D3DFVF_TEXCOORDSIZE4(0) | D3DFVF_TEX1, test_buffer, D3D_OK, TRUE, __LINE__);
786     }
787
788     /* Make sure the TEXCOORD index works correctly - try several textures */
789     {
790         CONST D3DVERTEXELEMENT9 test_buffer[] =
791             { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
792               { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
793               { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
794               { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
795         test_fvf_to_decl(D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEXCOORDSIZE3(1) |
796                          D3DFVF_TEXCOORDSIZE2(2) | D3DFVF_TEXCOORDSIZE4(3) | D3DFVF_TEX4,
797                          test_buffer, D3D_OK, TRUE, __LINE__);
798     }
799
800     /* Now try a combination test  */
801     {
802        CONST D3DVERTEXELEMENT9 test_buffer[] =
803                 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
804                   { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
805                   { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
806                   { 0, 32, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
807                   { 0, 36, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 },
808                   { 0, 44, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
809        test_fvf_to_decl(D3DFVF_XYZB4 | D3DFVF_SPECULAR | D3DFVF_DIFFUSE |
810                         D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEX2,
811                         test_buffer, D3D_OK, TRUE, __LINE__);
812     }
813 }
814
815 static void D3DXGetFVFVertexSizeTest(void)
816 {
817     UINT got;
818
819     compare_vertex_sizes (D3DFVF_XYZ, 12);
820
821     compare_vertex_sizes (D3DFVF_XYZB3, 24);
822
823     compare_vertex_sizes (D3DFVF_XYZB5, 32);
824
825     compare_vertex_sizes (D3DFVF_XYZ | D3DFVF_NORMAL, 24);
826
827     compare_vertex_sizes (D3DFVF_XYZ | D3DFVF_DIFFUSE, 16);
828
829     compare_vertex_sizes (
830         D3DFVF_XYZ |
831         D3DFVF_TEX1 |
832         D3DFVF_TEXCOORDSIZE1(0), 16);
833     compare_vertex_sizes (
834         D3DFVF_XYZ |
835         D3DFVF_TEX2 |
836         D3DFVF_TEXCOORDSIZE1(0) |
837         D3DFVF_TEXCOORDSIZE1(1), 20);
838
839     compare_vertex_sizes (
840         D3DFVF_XYZ |
841         D3DFVF_TEX1 |
842         D3DFVF_TEXCOORDSIZE2(0), 20);
843
844     compare_vertex_sizes (
845         D3DFVF_XYZ |
846         D3DFVF_TEX2 |
847         D3DFVF_TEXCOORDSIZE2(0) |
848         D3DFVF_TEXCOORDSIZE2(1), 28);
849
850     compare_vertex_sizes (
851         D3DFVF_XYZ |
852         D3DFVF_TEX6 |
853         D3DFVF_TEXCOORDSIZE2(0) |
854         D3DFVF_TEXCOORDSIZE2(1) |
855         D3DFVF_TEXCOORDSIZE2(2) |
856         D3DFVF_TEXCOORDSIZE2(3) |
857         D3DFVF_TEXCOORDSIZE2(4) |
858         D3DFVF_TEXCOORDSIZE2(5), 60);
859
860     compare_vertex_sizes (
861         D3DFVF_XYZ |
862         D3DFVF_TEX8 |
863         D3DFVF_TEXCOORDSIZE2(0) |
864         D3DFVF_TEXCOORDSIZE2(1) |
865         D3DFVF_TEXCOORDSIZE2(2) |
866         D3DFVF_TEXCOORDSIZE2(3) |
867         D3DFVF_TEXCOORDSIZE2(4) |
868         D3DFVF_TEXCOORDSIZE2(5) |
869         D3DFVF_TEXCOORDSIZE2(6) |
870         D3DFVF_TEXCOORDSIZE2(7), 76);
871
872     compare_vertex_sizes (
873         D3DFVF_XYZ |
874         D3DFVF_TEX1 |
875         D3DFVF_TEXCOORDSIZE3(0), 24);
876
877     compare_vertex_sizes (
878         D3DFVF_XYZ |
879         D3DFVF_TEX4 |
880         D3DFVF_TEXCOORDSIZE3(0) |
881         D3DFVF_TEXCOORDSIZE3(1) |
882         D3DFVF_TEXCOORDSIZE3(2) |
883         D3DFVF_TEXCOORDSIZE3(3), 60);
884
885     compare_vertex_sizes (
886         D3DFVF_XYZ |
887         D3DFVF_TEX1 |
888         D3DFVF_TEXCOORDSIZE4(0), 28);
889
890     compare_vertex_sizes (
891         D3DFVF_XYZ |
892         D3DFVF_TEX2 |
893         D3DFVF_TEXCOORDSIZE4(0) |
894         D3DFVF_TEXCOORDSIZE4(1), 44);
895
896     compare_vertex_sizes (
897         D3DFVF_XYZ |
898         D3DFVF_TEX3 |
899         D3DFVF_TEXCOORDSIZE4(0) |
900         D3DFVF_TEXCOORDSIZE4(1) |
901         D3DFVF_TEXCOORDSIZE4(2), 60);
902
903     compare_vertex_sizes (
904         D3DFVF_XYZB5 |
905         D3DFVF_NORMAL |
906         D3DFVF_DIFFUSE |
907         D3DFVF_SPECULAR |
908         D3DFVF_TEX8 |
909         D3DFVF_TEXCOORDSIZE4(0) |
910         D3DFVF_TEXCOORDSIZE4(1) |
911         D3DFVF_TEXCOORDSIZE4(2) |
912         D3DFVF_TEXCOORDSIZE4(3) |
913         D3DFVF_TEXCOORDSIZE4(4) |
914         D3DFVF_TEXCOORDSIZE4(5) |
915         D3DFVF_TEXCOORDSIZE4(6) |
916         D3DFVF_TEXCOORDSIZE4(7), 180);
917 }
918
919 static void D3DXIntersectTriTest(void)
920 {
921     BOOL exp_res, got_res;
922     D3DXVECTOR3 position, ray, vertex[3];
923     FLOAT exp_dist, got_dist, exp_u, got_u, exp_v, got_v;
924
925     vertex[0].x = 1.0f; vertex[0].y = 0.0f; vertex[0].z = 0.0f;
926     vertex[1].x = 2.0f; vertex[1].y = 0.0f; vertex[1].z = 0.0f;
927     vertex[2].x = 1.0f; vertex[2].y = 1.0f; vertex[2].z = 0.0f;
928
929     position.x = -14.5f; position.y = -23.75f; position.z = -32.0f;
930
931     ray.x = 2.0f; ray.y = 3.0f; ray.z = 4.0f;
932
933     exp_res = TRUE; exp_u = 0.5f; exp_v = 0.25f; exp_dist = 8.0f;
934
935     got_res = D3DXIntersectTri(&vertex[0],&vertex[1],&vertex[2],&position,&ray,&got_u,&got_v,&got_dist);
936     ok( got_res == exp_res, "Expected result = %d, got %d\n",exp_res,got_res);
937     ok( compare(exp_u,got_u), "Expected u = %f, got %f\n",exp_u,got_u);
938     ok( compare(exp_v,got_v), "Expected v = %f, got %f\n",exp_v,got_v);
939     ok( compare(exp_dist,got_dist), "Expected distance = %f, got %f\n",exp_dist,got_dist);
940
941 /*Only positive ray is taken in account*/
942
943     vertex[0].x = 1.0f; vertex[0].y = 0.0f; vertex[0].z = 0.0f;
944     vertex[1].x = 2.0f; vertex[1].y = 0.0f; vertex[1].z = 0.0f;
945     vertex[2].x = 1.0f; vertex[2].y = 1.0f; vertex[2].z = 0.0f;
946
947     position.x = 17.5f; position.y = 24.25f; position.z = 32.0f;
948
949     ray.x = 2.0f; ray.y = 3.0f; ray.z = 4.0f;
950
951     exp_res = FALSE;
952
953     got_res = D3DXIntersectTri(&vertex[0],&vertex[1],&vertex[2],&position,&ray,&got_u,&got_v,&got_dist);
954     ok( got_res == exp_res, "Expected result = %d, got %d\n",exp_res,got_res);
955
956 /*Intersection between ray and triangle in a same plane is considered as empty*/
957
958     vertex[0].x = 4.0f; vertex[0].y = 0.0f; vertex[0].z = 0.0f;
959     vertex[1].x = 6.0f; vertex[1].y = 0.0f; vertex[1].z = 0.0f;
960     vertex[2].x = 4.0f; vertex[2].y = 2.0f; vertex[2].z = 0.0f;
961
962     position.x = 1.0f; position.y = 1.0f; position.z = 0.0f;
963
964     ray.x = 1.0f; ray.y = 0.0f; ray.z = 0.0f;
965
966     exp_res = FALSE;
967
968     got_res = D3DXIntersectTri(&vertex[0],&vertex[1],&vertex[2],&position,&ray,&got_u,&got_v,&got_dist);
969     ok( got_res == exp_res, "Expected result = %d, got %d\n",exp_res,got_res);
970 }
971
972 static void D3DXCreateMeshTest(void)
973 {
974     HRESULT hr;
975     HWND wnd;
976     IDirect3D9 *d3d;
977     IDirect3DDevice9 *device, *test_device;
978     D3DPRESENT_PARAMETERS d3dpp;
979     ID3DXMesh *d3dxmesh;
980     int i, size;
981     D3DVERTEXELEMENT9 test_decl[MAX_FVF_DECL_SIZE];
982     DWORD fvf, options;
983     struct mesh mesh;
984
985     static const D3DVERTEXELEMENT9 decl[3] = {
986         {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
987         {0, 0xC, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
988         D3DDECL_END(), };
989
990     hr = D3DXCreateMesh(0, 0, 0, NULL, NULL, NULL);
991     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
992
993     hr = D3DXCreateMesh(1, 3, D3DXMESH_MANAGED, (LPD3DVERTEXELEMENT9 *)&decl, NULL, &d3dxmesh);
994     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
995
996     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
997     if (!wnd)
998     {
999         skip("Couldn't create application window\n");
1000         return;
1001     }
1002     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1003     if (!d3d)
1004     {
1005         skip("Couldn't create IDirect3D9 object\n");
1006         DestroyWindow(wnd);
1007         return;
1008     }
1009
1010     ZeroMemory(&d3dpp, sizeof(d3dpp));
1011     d3dpp.Windowed = TRUE;
1012     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1013     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1014     if (FAILED(hr))
1015     {
1016         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1017         IDirect3D9_Release(d3d);
1018         DestroyWindow(wnd);
1019         return;
1020     }
1021
1022     hr = D3DXCreateMesh(0, 3, D3DXMESH_MANAGED, (LPD3DVERTEXELEMENT9 *)&decl, device, &d3dxmesh);
1023     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1024
1025     hr = D3DXCreateMesh(1, 0, D3DXMESH_MANAGED, (LPD3DVERTEXELEMENT9 *)&decl, device, &d3dxmesh);
1026     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1027
1028     hr = D3DXCreateMesh(1, 3, 0, (LPD3DVERTEXELEMENT9 *)&decl, device, &d3dxmesh);
1029     todo_wine ok(hr == D3D_OK, "Got result %x, expected %x (D3D_OK)\n", hr, D3D_OK);
1030
1031     if (hr == D3D_OK)
1032     {
1033         d3dxmesh->lpVtbl->Release(d3dxmesh);
1034     }
1035
1036     hr = D3DXCreateMesh(1, 3, D3DXMESH_MANAGED, 0, device, &d3dxmesh);
1037     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1038
1039     hr = D3DXCreateMesh(1, 3, D3DXMESH_MANAGED, (LPD3DVERTEXELEMENT9 *)&decl, device, NULL);
1040     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1041
1042     hr = D3DXCreateMesh(1, 3, D3DXMESH_MANAGED, (LPD3DVERTEXELEMENT9 *)&decl, device, &d3dxmesh);
1043     todo_wine ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
1044
1045     if (hr == D3D_OK)
1046     {
1047         /* device */
1048         hr = d3dxmesh->lpVtbl->GetDevice(d3dxmesh, NULL);
1049         ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1050
1051         hr = d3dxmesh->lpVtbl->GetDevice(d3dxmesh, &test_device);
1052         ok(hr == D3D_OK, "Got result %x, expected %x (D3D_OK)\n", hr, D3D_OK);
1053         ok(test_device == device, "Got result %p, expected %p\n", test_device, device);
1054
1055         if (hr == D3D_OK)
1056         {
1057             IDirect3DDevice9_Release(device);
1058         }
1059
1060         /* declaration */
1061         hr = d3dxmesh->lpVtbl->GetDeclaration(d3dxmesh, test_decl);
1062         ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
1063
1064         if (hr == D3D_OK)
1065         {
1066             size = sizeof(decl) / sizeof(decl[0]);
1067             for (i = 0; i < size - 1; i++)
1068             {
1069                 ok(test_decl[i].Stream == decl[i].Stream, "Returned stream %d, expected %d\n", test_decl[i].Stream, decl[i].Stream);
1070                 ok(test_decl[i].Type == decl[i].Type, "Returned type %d, expected %d\n", test_decl[i].Type, decl[i].Type);
1071                 ok(test_decl[i].Method == decl[i].Method, "Returned method %d, expected %d\n", test_decl[i].Method, decl[i].Method);
1072                 ok(test_decl[i].Usage == decl[i].Usage, "Returned usage %d, expected %d\n", test_decl[i].Usage, decl[i].Usage);
1073                 ok(test_decl[i].UsageIndex == decl[i].UsageIndex, "Returned usage index %d, expected %d\n", test_decl[i].UsageIndex, decl[i].UsageIndex);
1074                 ok(test_decl[i].Offset == decl[i].Offset, "Returned offset %d, expected %d\n", decl[1].Offset, decl[i].Offset);
1075             }
1076             ok(decl[size-1].Stream == 0xFF, "Returned too long vertex declaration\n"); /* end element */
1077         }
1078
1079         /* FVF */
1080         fvf = d3dxmesh->lpVtbl->GetFVF(d3dxmesh);
1081         ok(fvf == (D3DFVF_XYZ | D3DFVF_NORMAL), "Got result %x, expected %x (D3DFVF_XYZ | D3DFVF_NORMAL)\n", fvf, D3DFVF_XYZ | D3DFVF_NORMAL);
1082
1083         /* options */
1084         options = d3dxmesh->lpVtbl->GetOptions(d3dxmesh);
1085         ok(options == D3DXMESH_MANAGED, "Got result %x, expected %x (D3DXMESH_MANAGED)\n", options, D3DXMESH_MANAGED);
1086
1087         /* rest */
1088         if (!new_mesh(&mesh, 3, 1))
1089         {
1090             skip("Couldn't create mesh\n");
1091         }
1092         else
1093         {
1094             memset(mesh.vertices, 0, mesh.number_of_vertices * sizeof(*mesh.vertices));
1095             memset(mesh.faces, 0, mesh.number_of_faces * sizeof(*mesh.faces));
1096
1097             compare_mesh("createmeshfvf", d3dxmesh, &mesh);
1098
1099             free_mesh(&mesh);
1100         }
1101
1102         d3dxmesh->lpVtbl->Release(d3dxmesh);
1103     }
1104
1105     IDirect3DDevice9_Release(device);
1106     IDirect3D9_Release(d3d);
1107     DestroyWindow(wnd);
1108 }
1109
1110 struct sincos_table
1111 {
1112     float *sin;
1113     float *cos;
1114 };
1115
1116 static void free_sincos_table(struct sincos_table *sincos_table)
1117 {
1118     HeapFree(GetProcessHeap(), 0, sincos_table->cos);
1119     HeapFree(GetProcessHeap(), 0, sincos_table->sin);
1120 }
1121
1122 /* pre compute sine and cosine tables; caller must free */
1123 static BOOL compute_sincos_table(struct sincos_table *sincos_table, float angle_start, float angle_step, int n)
1124 {
1125     float angle;
1126     int i;
1127
1128     sincos_table->sin = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*sincos_table->sin));
1129     if (!sincos_table->sin)
1130     {
1131         return FALSE;
1132     }
1133     sincos_table->cos = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*sincos_table->cos));
1134     if (!sincos_table->cos)
1135     {
1136         HeapFree(GetProcessHeap(), 0, sincos_table->sin);
1137         return FALSE;
1138     }
1139
1140     angle = angle_start;
1141     for (i = 0; i < n; i++)
1142     {
1143         sincos_table->sin[i] = sin(angle);
1144         sincos_table->cos[i] = cos(angle);
1145         angle += angle_step;
1146     }
1147
1148     return TRUE;
1149 }
1150
1151 static WORD sphere_vertex(UINT slices, int slice, int stack)
1152 {
1153     return stack*slices+slice+1;
1154 }
1155
1156 /* slices = subdivisions along xy plane, stacks = subdivisions along z axis */
1157 static BOOL compute_sphere(struct mesh *mesh, FLOAT radius, UINT slices, UINT stacks)
1158 {
1159     float theta_step, theta_start;
1160     struct sincos_table theta;
1161     float phi_step, phi_start;
1162     struct sincos_table phi;
1163     DWORD number_of_vertices, number_of_faces;
1164     DWORD vertex, face;
1165     int slice, stack;
1166
1167     /* theta = angle on xy plane wrt x axis */
1168     theta_step = M_PI / stacks;
1169     theta_start = theta_step;
1170
1171     /* phi = angle on xz plane wrt z axis */
1172     phi_step = -2 * M_PI / slices;
1173     phi_start = M_PI / 2;
1174
1175     if (!compute_sincos_table(&theta, theta_start, theta_step, stacks))
1176     {
1177         return FALSE;
1178     }
1179     if (!compute_sincos_table(&phi, phi_start, phi_step, slices))
1180     {
1181         free_sincos_table(&theta);
1182         return FALSE;
1183     }
1184
1185     number_of_vertices = 2 + slices * (stacks-1);
1186     number_of_faces = 2 * slices + (stacks - 2) * (2 * slices);
1187
1188     if (!new_mesh(mesh, number_of_vertices, number_of_faces))
1189     {
1190         free_sincos_table(&phi);
1191         free_sincos_table(&theta);
1192         return FALSE;
1193     }
1194
1195     vertex = 0;
1196     face = 0;
1197     stack = 0;
1198
1199     mesh->vertices[vertex].normal.x = 0.0f;
1200     mesh->vertices[vertex].normal.y = 0.0f;
1201     mesh->vertices[vertex].normal.z = 1.0f;
1202     mesh->vertices[vertex].position.x = 0.0f;
1203     mesh->vertices[vertex].position.y = 0.0f;
1204     mesh->vertices[vertex].position.z = radius;
1205     vertex++;
1206
1207     for (stack = 0; stack < stacks - 1; stack++)
1208     {
1209         for (slice = 0; slice < slices; slice++)
1210         {
1211             mesh->vertices[vertex].normal.x = theta.sin[stack] * phi.cos[slice];
1212             mesh->vertices[vertex].normal.y = theta.sin[stack] * phi.sin[slice];
1213             mesh->vertices[vertex].normal.z = theta.cos[stack];
1214             mesh->vertices[vertex].position.x = radius * theta.sin[stack] * phi.cos[slice];
1215             mesh->vertices[vertex].position.y = radius * theta.sin[stack] * phi.sin[slice];
1216             mesh->vertices[vertex].position.z = radius * theta.cos[stack];
1217             vertex++;
1218
1219             if (slice > 0)
1220             {
1221                 if (stack == 0)
1222                 {
1223                     /* top stack is triangle fan */
1224                     mesh->faces[face][0] = 0;
1225                     mesh->faces[face][1] = slice + 1;
1226                     mesh->faces[face][2] = slice;
1227                     face++;
1228                 }
1229                 else
1230                 {
1231                     /* stacks in between top and bottom are quad strips */
1232                     mesh->faces[face][0] = sphere_vertex(slices, slice-1, stack-1);
1233                     mesh->faces[face][1] = sphere_vertex(slices, slice, stack-1);
1234                     mesh->faces[face][2] = sphere_vertex(slices, slice-1, stack);
1235                     face++;
1236
1237                     mesh->faces[face][0] = sphere_vertex(slices, slice, stack-1);
1238                     mesh->faces[face][1] = sphere_vertex(slices, slice, stack);
1239                     mesh->faces[face][2] = sphere_vertex(slices, slice-1, stack);
1240                     face++;
1241                 }
1242             }
1243         }
1244
1245         if (stack == 0)
1246         {
1247             mesh->faces[face][0] = 0;
1248             mesh->faces[face][1] = 1;
1249             mesh->faces[face][2] = slice;
1250             face++;
1251         }
1252         else
1253         {
1254             mesh->faces[face][0] = sphere_vertex(slices, slice-1, stack-1);
1255             mesh->faces[face][1] = sphere_vertex(slices, 0, stack-1);
1256             mesh->faces[face][2] = sphere_vertex(slices, slice-1, stack);
1257             face++;
1258
1259             mesh->faces[face][0] = sphere_vertex(slices, 0, stack-1);
1260             mesh->faces[face][1] = sphere_vertex(slices, 0, stack);
1261             mesh->faces[face][2] = sphere_vertex(slices, slice-1, stack);
1262             face++;
1263         }
1264     }
1265
1266     mesh->vertices[vertex].position.x = 0.0f;
1267     mesh->vertices[vertex].position.y = 0.0f;
1268     mesh->vertices[vertex].position.z = -radius;
1269     mesh->vertices[vertex].normal.x = 0.0f;
1270     mesh->vertices[vertex].normal.y = 0.0f;
1271     mesh->vertices[vertex].normal.z = -1.0f;
1272
1273     /* bottom stack is triangle fan */
1274     for (slice = 1; slice < slices; slice++)
1275     {
1276         mesh->faces[face][0] = sphere_vertex(slices, slice-1, stack-1);
1277         mesh->faces[face][1] = sphere_vertex(slices, slice, stack-1);
1278         mesh->faces[face][2] = vertex;
1279         face++;
1280     }
1281
1282     mesh->faces[face][0] = sphere_vertex(slices, slice-1, stack-1);
1283     mesh->faces[face][1] = sphere_vertex(slices, 0, stack-1);
1284     mesh->faces[face][2] = vertex;
1285
1286     free_sincos_table(&phi);
1287     free_sincos_table(&theta);
1288
1289     return TRUE;
1290 }
1291
1292 static void test_sphere(IDirect3DDevice9 *device, FLOAT radius, UINT slices, UINT stacks)
1293 {
1294     HRESULT hr;
1295     ID3DXMesh *sphere;
1296     struct mesh mesh;
1297     char name[256];
1298
1299     hr = D3DXCreateSphere(device, radius, slices, stacks, &sphere, NULL);
1300     todo_wine ok(hr == D3D_OK, "Got result %x, expected 0 (D3D_OK)\n", hr);
1301     if (hr != D3D_OK)
1302     {
1303         skip("Couldn't create sphere\n");
1304         return;
1305     }
1306
1307     if (!compute_sphere(&mesh, radius, slices, stacks))
1308     {
1309         skip("Couldn't create mesh\n");
1310         sphere->lpVtbl->Release(sphere);
1311         return;
1312     }
1313
1314     sprintf(name, "sphere (%g, %u, %u)", radius, slices, stacks);
1315     compare_mesh(name, sphere, &mesh);
1316
1317     free_mesh(&mesh);
1318
1319     sphere->lpVtbl->Release(sphere);
1320 }
1321
1322 static void D3DXCreateSphereTest(void)
1323 {
1324     HRESULT hr;
1325     HWND wnd;
1326     IDirect3D9* d3d;
1327     IDirect3DDevice9* device;
1328     D3DPRESENT_PARAMETERS d3dpp;
1329     ID3DXMesh* sphere = NULL;
1330
1331     hr = D3DXCreateSphere(NULL, 0.0f, 0, 0, NULL, NULL);
1332     todo_wine ok( hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n",hr,D3DERR_INVALIDCALL);
1333
1334     hr = D3DXCreateSphere(NULL, 0.1f, 0, 0, NULL, NULL);
1335     todo_wine ok( hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n",hr,D3DERR_INVALIDCALL);
1336
1337     hr = D3DXCreateSphere(NULL, 0.0f, 1, 0, NULL, NULL);
1338     todo_wine ok( hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n",hr,D3DERR_INVALIDCALL);
1339
1340     hr = D3DXCreateSphere(NULL, 0.0f, 0, 1, NULL, NULL);
1341     todo_wine ok( hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n",hr,D3DERR_INVALIDCALL);
1342
1343     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1344     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1345     if (!wnd)
1346     {
1347         skip("Couldn't create application window\n");
1348         return;
1349     }
1350     if (!d3d)
1351     {
1352         skip("Couldn't create IDirect3D9 object\n");
1353         DestroyWindow(wnd);
1354         return;
1355     }
1356
1357     ZeroMemory(&d3dpp, sizeof(d3dpp));
1358     d3dpp.Windowed = TRUE;
1359     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1360     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1361     if (FAILED(hr))
1362     {
1363         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1364         IDirect3D9_Release(d3d);
1365         DestroyWindow(wnd);
1366         return;
1367     }
1368
1369     hr = D3DXCreateSphere(device, 1.0f, 1, 1, &sphere, NULL);
1370     todo_wine ok( hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n",hr,D3DERR_INVALIDCALL);
1371
1372     hr = D3DXCreateSphere(device, 1.0f, 2, 1, &sphere, NULL);
1373     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1374
1375     hr = D3DXCreateSphere(device, 1.0f, 1, 2, &sphere, NULL);
1376     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1377
1378     hr = D3DXCreateSphere(device, -0.1f, 1, 2, &sphere, NULL);
1379     todo_wine ok(hr == D3DERR_INVALIDCALL, "Got result %x, expected %x (D3DERR_INVALIDCALL)\n", hr, D3DERR_INVALIDCALL);
1380
1381     test_sphere(device, 0.0f, 2, 2);
1382     test_sphere(device, 1.0f, 2, 2);
1383     test_sphere(device, 1.0f, 3, 2);
1384     test_sphere(device, 1.0f, 4, 4);
1385     test_sphere(device, 1.0f, 3, 4);
1386     test_sphere(device, 5.0f, 6, 7);
1387     test_sphere(device, 10.0f, 11, 12);
1388
1389     IDirect3DDevice9_Release(device);
1390     IDirect3D9_Release(d3d);
1391     DestroyWindow(wnd);
1392 }
1393
1394 static void test_get_decl_vertex_size(void)
1395 {
1396     static const D3DVERTEXELEMENT9 declaration1[] =
1397     {
1398         {0, 0, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1399         {1, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1400         {2, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1401         {3, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1402         {4, 0, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1403         {5, 0, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1404         {6, 0, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1405         {7, 0, D3DDECLTYPE_SHORT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1406         {8, 0, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1407         {9, 0, D3DDECLTYPE_SHORT2N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1408         {10, 0, D3DDECLTYPE_SHORT4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1409         {11, 0, D3DDECLTYPE_UDEC3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1410         {12, 0, D3DDECLTYPE_DEC3N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1411         {13, 0, D3DDECLTYPE_FLOAT16_2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1412         {14, 0, D3DDECLTYPE_FLOAT16_4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1413         D3DDECL_END(),
1414     };
1415     static const D3DVERTEXELEMENT9 declaration2[] =
1416     {
1417         {0, 8, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1418         {1, 8, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1419         {2, 8, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1420         {3, 8, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1421         {4, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1422         {5, 8, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1423         {6, 8, D3DDECLTYPE_SHORT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1424         {7, 8, D3DDECLTYPE_SHORT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1425         {0, 8, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1426         {1, 8, D3DDECLTYPE_SHORT2N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1427         {2, 8, D3DDECLTYPE_SHORT4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1428         {3, 8, D3DDECLTYPE_UDEC3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1429         {4, 8, D3DDECLTYPE_DEC3N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1430         {5, 8, D3DDECLTYPE_FLOAT16_2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1431         {6, 8, D3DDECLTYPE_FLOAT16_4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1432         {7, 8, D3DDECLTYPE_FLOAT1, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
1433         D3DDECL_END(),
1434     };
1435     static const UINT sizes1[] =
1436     {
1437         4,  8,  12, 16,
1438         4,  4,  4,  8,
1439         4,  4,  8,  4,
1440         4,  4,  8,  0,
1441     };
1442     static const UINT sizes2[] =
1443     {
1444         12, 16, 20, 24,
1445         12, 12, 16, 16,
1446     };
1447     unsigned int i;
1448     UINT size;
1449
1450     size = D3DXGetDeclVertexSize(NULL, 0);
1451     ok(size == 0, "Got size %#x, expected 0.\n", size);
1452
1453     for (i = 0; i < 16; ++i)
1454     {
1455         size = D3DXGetDeclVertexSize(declaration1, i);
1456         ok(size == sizes1[i], "Got size %u for stream %u, expected %u.\n", size, i, sizes1[i]);
1457     }
1458
1459     for (i = 0; i < 8; ++i)
1460     {
1461         size = D3DXGetDeclVertexSize(declaration2, i);
1462         ok(size == sizes2[i], "Got size %u for stream %u, expected %u.\n", size, i, sizes2[i]);
1463     }
1464 }
1465
1466 START_TEST(mesh)
1467 {
1468     D3DXBoundProbeTest();
1469     D3DXComputeBoundingBoxTest();
1470     D3DXComputeBoundingSphereTest();
1471     D3DXGetFVFVertexSizeTest();
1472     D3DXIntersectTriTest();
1473     D3DXCreateMeshTest();
1474     D3DXCreateSphereTest();
1475     test_get_decl_vertex_size();
1476     test_fvf_decl_conversion();
1477 }