2 * Copyright (C) 2005 Henri Verbeet
3 * Copyright (C) 2006 Ivan Gyurdiev
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.
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.
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
22 #include "wine/test.h"
24 static HMODULE d3d9_handle = 0;
26 #define VDECL_CHECK(fcall) \
28 trace(" Test failed on line #%d\n", __LINE__);
30 static HWND create_window(void)
33 wc.lpfnWndProc = DefWindowProc;
34 wc.lpszClassName = "d3d9_test_wc";
37 return CreateWindow("d3d9_test_wc", "d3d9_test",
38 0, 0, 0, 0, 0, 0, 0, 0, 0);
41 static IDirect3DDevice9 *init_d3d9(void)
43 IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
44 IDirect3D9 *d3d9_ptr = 0;
45 IDirect3DDevice9 *device_ptr = 0;
46 D3DPRESENT_PARAMETERS present_parameters;
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;
53 d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
54 ok(d3d9_ptr != NULL, "Failed to create IDirect3D9 object\n");
55 if (!d3d9_ptr) return NULL;
57 ZeroMemory(&present_parameters, sizeof(present_parameters));
58 present_parameters.Windowed = TRUE;
59 present_parameters.hDeviceWindow = create_window();
60 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
62 hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
66 hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
69 trace("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
77 static int get_refcount(IUnknown *object)
79 IUnknown_AddRef(object);
80 return IUnknown_Release(object);
83 static inline void print_elements(
84 D3DVERTEXELEMENT9 *elements) {
86 D3DVERTEXELEMENT9 last = D3DDECL_END();
87 D3DVERTEXELEMENT9 *ptr = elements;
90 while (memcmp(ptr, &last, sizeof(D3DVERTEXELEMENT9))) {
93 "[Element %d] Stream = %d, Offset = %d, Type = %d, Method = %d, Usage = %d, UsageIndex = %d\n",
94 count, ptr->Stream, ptr->Offset, ptr->Type, ptr->Method, ptr->Usage, ptr->UsageIndex);
101 static int compare_elements(
102 IDirect3DVertexDeclaration9 *decl,
103 const D3DVERTEXELEMENT9 *expected_elements) {
106 unsigned int i, size;
107 D3DVERTEXELEMENT9 last = D3DDECL_END();
108 D3DVERTEXELEMENT9 *elements = NULL;
110 /* How many elements are there? */
111 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, NULL, &size );
112 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
113 if (FAILED(hr)) goto fail;
115 /* Allocate buffer */
116 elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * size);
117 ok (elements != NULL, "Out of memory, aborting test\n");
118 if (elements == NULL) goto fail;
120 /* Get the elements */
121 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, elements, &size);
122 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
123 if (FAILED(hr)) goto fail;
125 /* Compare one by one */
126 for (i = 0; i < size; i++) {
130 int end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
131 int end2 = memcmp(&expected_elements[i], &last, sizeof(D3DVERTEXELEMENT9));
132 status = ((end1 && !end2) || (!end1 && end2));
133 ok (!status, "Mismatch in size, test declaration is %s than expected\n",
134 (end1 && !end2) ? "shorter" : "longer");
135 if (status) { print_elements(elements); goto fail; }
137 status = memcmp(&elements[i], &expected_elements[i], sizeof(D3DVERTEXELEMENT9));
138 ok (!status, "Mismatch in element %d\n", i);
139 if (status) { print_elements(elements); goto fail; }
142 HeapFree(GetProcessHeap(), 0, elements);
146 HeapFree(GetProcessHeap(), 0, elements);
150 static IDirect3DVertexDeclaration9 *test_create_vertex_declaration(IDirect3DDevice9 *device_ptr, D3DVERTEXELEMENT9 *vertex_decl)
152 IDirect3DVertexDeclaration9 *decl_ptr = 0;
155 hret = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vertex_decl, &decl_ptr);
156 ok(hret == D3D_OK && decl_ptr != NULL, "CreateVertexDeclaration returned: hret 0x%x, decl_ptr %p. "
157 "Expected hret 0x%x, decl_ptr != %p. Aborting.\n", hret, decl_ptr, D3D_OK, NULL);
162 static void test_get_set_vertex_declaration(IDirect3DDevice9 *device_ptr, IDirect3DVertexDeclaration9 *decl_ptr)
164 IDirect3DVertexDeclaration9 *current_decl_ptr = 0;
166 int decl_refcount = 0;
169 /* SetVertexDeclaration should not touch the declaration's refcount. */
170 i = get_refcount((IUnknown *)decl_ptr);
171 hret = IDirect3DDevice9_SetVertexDeclaration(device_ptr, decl_ptr);
172 decl_refcount = get_refcount((IUnknown *)decl_ptr);
173 ok(hret == D3D_OK && decl_refcount == i, "SetVertexDeclaration returned: hret 0x%x, refcount %d. "
174 "Expected hret 0x%x, refcount %d.\n", hret, decl_refcount, D3D_OK, i);
176 /* GetVertexDeclaration should increase the declaration's refcount by one. */
178 hret = IDirect3DDevice9_GetVertexDeclaration(device_ptr, ¤t_decl_ptr);
179 decl_refcount = get_refcount((IUnknown *)decl_ptr);
180 ok(hret == D3D_OK && decl_refcount == i && current_decl_ptr == decl_ptr,
181 "GetVertexDeclaration returned: hret 0x%x, current_decl_ptr %p refcount %d. "
182 "Expected hret 0x%x, current_decl_ptr %p, refcount %d.\n", hret, current_decl_ptr, decl_refcount, D3D_OK, decl_ptr, i);
185 static void test_get_declaration(IDirect3DVertexDeclaration9 *decl_ptr, D3DVERTEXELEMENT9 *vertex_decl, UINT expected_num_elements)
188 UINT num_elements = 0;
189 D3DVERTEXELEMENT9 *decl = 0;
192 /* First test only getting the number of elements */
193 num_elements = 0x1337c0de;
194 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
195 ok(hret == D3D_OK && num_elements == expected_num_elements,
196 "GetDeclaration returned: hret 0x%x, num_elements %d. "
197 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
200 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
201 ok(hret == D3D_OK && num_elements == expected_num_elements,
202 "GetDeclaration returned: hret 0x%x, num_elements %d. "
203 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
205 /* Also test the returned data */
206 decl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
208 num_elements = 0x1337c0de;
209 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
210 ok(hret == D3D_OK && num_elements == expected_num_elements,
211 "GetDeclaration returned: hret 0x%x, num_elements %d. "
212 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
213 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
214 ok (!i, "Original and returned vertexdeclarations are not the same\n");
215 ZeroMemory(decl, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
218 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
219 ok(hret == D3D_OK && num_elements == expected_num_elements,
220 "GetDeclaration returned: hret 0x%x, num_elements %d. "
221 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
222 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
223 ok (!i, "Original and returned vertexdeclarations are not the same\n");
225 HeapFree(GetProcessHeap(), 0, decl);
228 /* FIXME: also write a test, which shows that attempting to set
229 * an invalid vertex declaration returns E_FAIL */
231 static HRESULT test_fvf_to_decl(
232 IDirect3DDevice9* device,
233 IDirect3DVertexDeclaration9* default_decl,
235 const D3DVERTEXELEMENT9 expected_elements[],
236 char object_should_change)
240 IDirect3DVertexDeclaration9 *result_decl = NULL;
242 /* Set a default declaration to make sure it is changed */
243 hr = IDirect3DDevice9_SetVertexDeclaration ( device, default_decl );
244 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
245 if (FAILED(hr)) goto fail;
248 hr = IDirect3DDevice9_SetFVF( device, test_fvf);
249 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
250 if (FAILED(hr)) goto fail;
252 /* Check if the declaration object changed underneath */
253 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl);
254 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
255 if (FAILED(hr)) goto fail;
256 if (object_should_change) {
257 ok(result_decl != default_decl, "result declaration matches original\n");
258 if (result_decl == default_decl) goto fail;
260 ok(result_decl == default_decl, "result declaration does not match original\n");
261 if (result_decl != default_decl) goto fail;
264 /* Declaration content/size test */
265 ok(result_decl != NULL, "result declaration was null\n");
266 if (result_decl == NULL)
268 else if (compare_elements(result_decl, expected_elements) != S_OK)
271 if (result_decl) IUnknown_Release( result_decl );
275 if (result_decl) IUnknown_Release( result_decl );
279 static HRESULT test_decl_to_fvf(
280 IDirect3DDevice9* device,
282 CONST D3DVERTEXELEMENT9 test_decl[],
287 IDirect3DVertexDeclaration9 *vdecl = NULL;
289 DWORD result_fvf = 0xdeadbeef;
291 /* Set a default FVF of SPECULAR and DIFFUSE to make sure it is changed back to 0 */
292 hr = IDirect3DDevice9_SetFVF( device, default_fvf);
293 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
294 if (FAILED(hr)) goto fail;
296 /* Create a testing declaration */
297 hr = IDirect3DDevice9_CreateVertexDeclaration( device, test_decl, &vdecl );
298 ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
299 if (FAILED(hr)) goto fail;
301 /* Set the declaration */
302 hr = IDirect3DDevice9_SetVertexDeclaration ( device, vdecl );
303 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
304 if (FAILED(hr)) goto fail;
307 hr = IDirect3DDevice9_GetFVF( device, &result_fvf);
308 ok(SUCCEEDED(hr), "GetFVF returned %#x, expected %#x\n", hr, D3D_OK);
309 if (FAILED(hr)) goto fail;
311 ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
313 if (test_fvf != result_fvf) goto fail;
315 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
316 if (vdecl) IUnknown_Release( vdecl );
320 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
321 if (vdecl) IUnknown_Release( vdecl );
325 static void test_fvf_decl_conversion(IDirect3DDevice9 *pDevice)
331 IDirect3DVertexDeclaration9* default_decl = NULL;
332 DWORD default_fvf = D3DFVF_SPECULAR | D3DFVF_DIFFUSE;
333 D3DVERTEXELEMENT9 default_elements[] =
334 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
335 { 0, 4, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
337 /* Create a default declaration and FVF that does not match any of the tests */
338 hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, default_elements, &default_decl );
339 ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
340 if (FAILED(hr)) goto cleanup;
342 /* Test conversions from vertex declaration to an FVF.
343 * For some reason those seem to occur only for POSITION/POSITIONT,
344 * Otherwise the FVF is forced to 0 - maybe this is configuration specific */
346 CONST D3DVERTEXELEMENT9 test_buffer[] =
347 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
348 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZ));
351 CONST D3DVERTEXELEMENT9 test_buffer[] =
352 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
353 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZRHW));
355 for (i = 0; i < 4; i++) {
356 CONST D3DVERTEXELEMENT9 test_buffer[] =
357 { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
358 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
361 CONST D3DVERTEXELEMENT9 test_buffer[] =
362 { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
363 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
366 CONST D3DVERTEXELEMENT9 test_buffer[] =
367 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
368 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
371 CONST D3DVERTEXELEMENT9 test_buffer[] =
372 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
373 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
376 CONST D3DVERTEXELEMENT9 test_buffer[] =
377 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
378 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
381 CONST D3DVERTEXELEMENT9 test_buffer[] =
382 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
383 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
386 /* Make sure textures of different sizes work */
388 CONST D3DVERTEXELEMENT9 test_buffer[] =
389 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
390 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
393 CONST D3DVERTEXELEMENT9 test_buffer[] =
394 { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
395 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
398 CONST D3DVERTEXELEMENT9 test_buffer[] =
399 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
400 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
403 CONST D3DVERTEXELEMENT9 test_buffer[] =
404 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
405 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
408 /* Make sure the TEXCOORD index works correctly - try several textures */
410 CONST D3DVERTEXELEMENT9 test_buffer[] =
411 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
412 { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
413 { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
414 { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
415 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
418 /* No FVF mapping available */
420 CONST D3DVERTEXELEMENT9 test_buffer[] =
421 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
422 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
425 CONST D3DVERTEXELEMENT9 test_buffer[] =
426 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
427 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
430 /* Try empty declaration */
432 CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
433 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
436 /* Now try a combination test */
438 CONST D3DVERTEXELEMENT9 test_buffer[] =
439 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
440 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
441 { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
442 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
443 { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
444 { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
445 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
448 /* Test conversions from FVF to a vertex declaration
449 * These seem to always occur internally. A new declaration object is created if necessary */
452 CONST D3DVERTEXELEMENT9 test_buffer[] =
453 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
454 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZ, test_buffer, 1));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
591 /* Make sure textures of different sizes work */
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));
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));
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));
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));
617 /* Make sure the TEXCOORD index works correctly - try several textures */
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));
629 /* Now try a combination test */
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));
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));
646 IDirect3DDevice9_SetVertexDeclaration ( pDevice, NULL );
647 if ( default_decl ) IUnknown_Release (default_decl);
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) {
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;
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() };
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;
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;
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;
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;
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;
694 /* The contents should correspond to the first conversion */
695 VDECL_CHECK(compare_elements(result_decl1, test_elements1));
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;
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");
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));
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;
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;
719 ok(result_decl4 == result_decl1, "Setting an already used FVF over results in a different vertexdeclaration\n");
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);
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;
735 IDirect3DVertexDeclaration9_Release(result_decl1);
736 IDirect3DVertexDeclaration9_Release(result_decl2);
737 IDirect3DVertexDeclaration9_Release(result_decl3);
738 IDirect3DVertexDeclaration9_Release(result_decl4);
743 static void test_vertex_declaration_alignment(
744 IDirect3DDevice9* device) {
747 IDirect3DVertexDeclaration9* result_decl = NULL;
750 CONST D3DVERTEXELEMENT9 test_elements[5][3] =
753 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
754 { 0, 16, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
758 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
759 { 0, 17, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
763 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
764 { 0, 18, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
768 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
769 { 0, 19, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
773 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
774 { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
778 HRESULT results[5] = {D3D_OK, E_FAIL, E_FAIL, E_FAIL, D3D_OK};
780 for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
782 hr = IDirect3DDevice9_CreateVertexDeclaration(device, test_elements[i], &result_decl);
783 ok(hr == results[i], "CreateVertexDeclaration for declaration %d returned %#x, expected %#x\n",
785 if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
789 static void test_unused_type(
790 IDirect3DDevice9* device) {
793 IDirect3DVertexDeclaration9* result_decl = NULL;
796 static const D3DVERTEXELEMENT9 test_elements[][3] =
799 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
800 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_COLOR , 0 },
804 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
805 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 0 },
809 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
810 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 1 },
814 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
815 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
819 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
820 { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
824 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
825 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL, 0 },
829 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
830 { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL, 0 },
835 for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
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",
840 if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
843 START_TEST(vertexdeclaration)
845 static D3DVERTEXELEMENT9 simple_decl[] = {
846 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
848 UINT simple_decl_num_elements = sizeof(simple_decl) / sizeof(*simple_decl);
849 IDirect3DDevice9 *device_ptr = 0;
850 IDirect3DVertexDeclaration9 *decl_ptr = 0;
852 d3d9_handle = LoadLibraryA("d3d9.dll");
855 skip("Could not load d3d9.dll\n");
859 device_ptr = init_d3d9();
862 skip("Failed to initialise d3d9\n");
866 decl_ptr = test_create_vertex_declaration(device_ptr, simple_decl);
869 skip("Failed to create a vertex declaration\n");
873 test_get_set_vertex_declaration(device_ptr, decl_ptr);
874 test_get_declaration(decl_ptr, simple_decl, simple_decl_num_elements);
875 test_fvf_decl_conversion(device_ptr);
876 test_fvf_decl_management(device_ptr);
877 test_vertex_declaration_alignment(device_ptr);
878 test_unused_type(device_ptr);