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);
56 skip("could not create D3D9\n");
60 ZeroMemory(&present_parameters, sizeof(present_parameters));
61 present_parameters.Windowed = TRUE;
62 present_parameters.hDeviceWindow = create_window();
63 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
65 hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
69 hres = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
72 trace("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
80 static int get_refcount(IUnknown *object)
82 IUnknown_AddRef(object);
83 return IUnknown_Release(object);
86 static inline void print_elements(
87 D3DVERTEXELEMENT9 *elements) {
89 D3DVERTEXELEMENT9 last = D3DDECL_END();
90 D3DVERTEXELEMENT9 *ptr = elements;
93 while (memcmp(ptr, &last, sizeof(D3DVERTEXELEMENT9))) {
96 "[Element %d] Stream = %d, Offset = %d, Type = %d, Method = %d, Usage = %d, UsageIndex = %d\n",
97 count, ptr->Stream, ptr->Offset, ptr->Type, ptr->Method, ptr->Usage, ptr->UsageIndex);
104 static int compare_elements(
105 IDirect3DVertexDeclaration9 *decl,
106 const D3DVERTEXELEMENT9 *expected_elements) {
109 unsigned int i, size;
110 D3DVERTEXELEMENT9 last = D3DDECL_END();
111 D3DVERTEXELEMENT9 *elements = NULL;
113 /* How many elements are there? */
114 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, NULL, &size );
115 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
116 if (FAILED(hr)) goto fail;
118 /* Allocate buffer */
119 elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * size);
120 ok (elements != NULL, "Out of memory, aborting test\n");
121 if (elements == NULL) goto fail;
123 /* Get the elements */
124 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, elements, &size);
125 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
126 if (FAILED(hr)) goto fail;
128 /* Compare one by one */
129 for (i = 0; i < size; i++) {
133 int end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
134 int end2 = memcmp(&expected_elements[i], &last, sizeof(D3DVERTEXELEMENT9));
135 status = ((end1 && !end2) || (!end1 && end2));
136 ok (!status, "Mismatch in size, test declaration is %s than expected\n",
137 (end1 && !end2) ? "shorter" : "longer");
138 if (status) { print_elements(elements); goto fail; }
140 status = memcmp(&elements[i], &expected_elements[i], sizeof(D3DVERTEXELEMENT9));
141 ok (!status, "Mismatch in element %d\n", i);
142 if (status) { print_elements(elements); goto fail; }
145 HeapFree(GetProcessHeap(), 0, elements);
149 HeapFree(GetProcessHeap(), 0, elements);
153 static IDirect3DVertexDeclaration9 *test_create_vertex_declaration(IDirect3DDevice9 *device_ptr, D3DVERTEXELEMENT9 *vertex_decl)
155 IDirect3DVertexDeclaration9 *decl_ptr = 0;
158 hret = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vertex_decl, &decl_ptr);
159 ok(hret == D3D_OK && decl_ptr != NULL, "CreateVertexDeclaration returned: hret 0x%x, decl_ptr %p. "
160 "Expected hret 0x%x, decl_ptr != %p. Aborting.\n", hret, decl_ptr, D3D_OK, NULL);
165 static void test_get_set_vertex_declaration(IDirect3DDevice9 *device_ptr, IDirect3DVertexDeclaration9 *decl_ptr)
167 IDirect3DVertexDeclaration9 *current_decl_ptr = 0;
169 int decl_refcount = 0;
172 /* SetVertexDeclaration should not touch the declaration's refcount. */
173 i = get_refcount((IUnknown *)decl_ptr);
174 hret = IDirect3DDevice9_SetVertexDeclaration(device_ptr, decl_ptr);
175 decl_refcount = get_refcount((IUnknown *)decl_ptr);
176 ok(hret == D3D_OK && decl_refcount == i, "SetVertexDeclaration returned: hret 0x%x, refcount %d. "
177 "Expected hret 0x%x, refcount %d.\n", hret, decl_refcount, D3D_OK, i);
179 /* GetVertexDeclaration should increase the declaration's refcount by one. */
181 hret = IDirect3DDevice9_GetVertexDeclaration(device_ptr, ¤t_decl_ptr);
182 decl_refcount = get_refcount((IUnknown *)decl_ptr);
183 ok(hret == D3D_OK && decl_refcount == i && current_decl_ptr == decl_ptr,
184 "GetVertexDeclaration returned: hret 0x%x, current_decl_ptr %p refcount %d. "
185 "Expected hret 0x%x, current_decl_ptr %p, refcount %d.\n", hret, current_decl_ptr, decl_refcount, D3D_OK, decl_ptr, i);
188 static void test_get_declaration(IDirect3DVertexDeclaration9 *decl_ptr, D3DVERTEXELEMENT9 *vertex_decl, UINT expected_num_elements)
191 UINT num_elements = 0;
192 D3DVERTEXELEMENT9 *decl = 0;
195 /* First test only getting the number of elements */
196 num_elements = 0x1337c0de;
197 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
198 ok(hret == D3D_OK && num_elements == expected_num_elements,
199 "GetDeclaration returned: hret 0x%x, num_elements %d. "
200 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
203 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
204 ok(hret == D3D_OK && num_elements == expected_num_elements,
205 "GetDeclaration returned: hret 0x%x, num_elements %d. "
206 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
208 /* Also test the returned data */
209 decl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
211 num_elements = 0x1337c0de;
212 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
213 ok(hret == D3D_OK && num_elements == expected_num_elements,
214 "GetDeclaration returned: hret 0x%x, num_elements %d. "
215 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
216 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
217 ok (!i, "Original and returned vertexdeclarations are not the same\n");
218 ZeroMemory(decl, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
221 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
222 ok(hret == D3D_OK && num_elements == expected_num_elements,
223 "GetDeclaration returned: hret 0x%x, num_elements %d. "
224 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
225 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
226 ok (!i, "Original and returned vertexdeclarations are not the same\n");
228 HeapFree(GetProcessHeap(), 0, decl);
231 /* FIXME: also write a test, which shows that attempting to set
232 * an invalid vertex declaration returns E_FAIL */
234 static HRESULT test_fvf_to_decl(
235 IDirect3DDevice9* device,
236 IDirect3DVertexDeclaration9* default_decl,
238 const D3DVERTEXELEMENT9 expected_elements[],
239 char object_should_change)
243 IDirect3DVertexDeclaration9 *result_decl = NULL;
245 /* Set a default declaration to make sure it is changed */
246 hr = IDirect3DDevice9_SetVertexDeclaration ( device, default_decl );
247 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
248 if (FAILED(hr)) goto fail;
251 hr = IDirect3DDevice9_SetFVF( device, test_fvf);
252 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
253 if (FAILED(hr)) goto fail;
255 /* Check if the declaration object changed underneath */
256 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl);
257 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
258 if (FAILED(hr)) goto fail;
259 if (object_should_change) {
260 ok(result_decl != default_decl, "result declaration matches original\n");
261 if (result_decl == default_decl) goto fail;
263 ok(result_decl == default_decl, "result declaration does not match original\n");
264 if (result_decl != default_decl) goto fail;
267 /* Declaration content/size test */
268 ok(result_decl != NULL, "result declaration was null\n");
269 if (result_decl == NULL)
271 else if (compare_elements(result_decl, expected_elements) != S_OK)
274 if (result_decl) IUnknown_Release( result_decl );
278 if (result_decl) IUnknown_Release( result_decl );
282 static HRESULT test_decl_to_fvf(IDirect3DDevice9* device, DWORD default_fvf,
283 const D3DVERTEXELEMENT9 test_decl[], DWORD test_fvf, BOOL todo)
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;
310 if (todo) todo_wine ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
311 else ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
312 if (test_fvf != result_fvf) goto fail;
314 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
315 if (vdecl) IUnknown_Release( vdecl );
319 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
320 if (vdecl) IUnknown_Release( vdecl );
324 static void test_fvf_decl_conversion(IDirect3DDevice9 *pDevice)
330 IDirect3DVertexDeclaration9* default_decl = NULL;
331 DWORD default_fvf = D3DFVF_SPECULAR | D3DFVF_DIFFUSE;
332 D3DVERTEXELEMENT9 default_elements[] =
333 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
334 { 0, 4, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
336 /* Create a default declaration and FVF that does not match any of the tests */
337 hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, default_elements, &default_decl );
338 ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
339 if (FAILED(hr)) goto cleanup;
341 /* Test conversions from vertex declaration to an FVF.
342 * For some reason those seem to occur only for POSITION/POSITIONT,
343 * Otherwise the FVF is forced to 0 - maybe this is configuration specific */
345 CONST D3DVERTEXELEMENT9 test_buffer[] =
346 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
347 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZ, TRUE));
350 CONST D3DVERTEXELEMENT9 test_buffer[] =
351 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
352 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZRHW, TRUE));
354 for (i = 0; i < 4; i++) {
355 CONST D3DVERTEXELEMENT9 test_buffer[] =
356 { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
357 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
360 CONST D3DVERTEXELEMENT9 test_buffer[] =
361 { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
362 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
365 CONST D3DVERTEXELEMENT9 test_buffer[] =
366 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
367 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
370 CONST D3DVERTEXELEMENT9 test_buffer[] =
371 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
372 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
375 CONST D3DVERTEXELEMENT9 test_buffer[] =
376 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
377 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
380 CONST D3DVERTEXELEMENT9 test_buffer[] =
381 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
382 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
385 /* Make sure textures of different sizes work */
387 CONST D3DVERTEXELEMENT9 test_buffer[] =
388 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
389 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
392 CONST D3DVERTEXELEMENT9 test_buffer[] =
393 { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
394 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
397 CONST D3DVERTEXELEMENT9 test_buffer[] =
398 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
399 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
402 CONST D3DVERTEXELEMENT9 test_buffer[] =
403 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
404 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
407 /* Make sure the TEXCOORD index works correctly - try several textures */
409 CONST D3DVERTEXELEMENT9 test_buffer[] =
410 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
411 { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
412 { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
413 { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
414 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
417 /* No FVF mapping available */
419 CONST D3DVERTEXELEMENT9 test_buffer[] =
420 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
421 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
424 CONST D3DVERTEXELEMENT9 test_buffer[] =
425 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
426 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
429 /* Try empty declaration */
431 CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
432 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
435 /* Now try a combination test */
437 CONST D3DVERTEXELEMENT9 test_buffer[] =
438 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
439 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
440 { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
441 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
442 { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
443 { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
444 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0, FALSE));
447 /* Test conversions from FVF to a vertex declaration
448 * These seem to always occur internally. A new declaration object is created if necessary */
451 CONST D3DVERTEXELEMENT9 test_buffer[] =
452 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
453 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZ, test_buffer, 1));
456 CONST D3DVERTEXELEMENT9 test_buffer[] =
457 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
458 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZRHW, test_buffer, 1));
461 CONST D3DVERTEXELEMENT9 test_buffer[] =
462 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
463 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
464 { 0, 28, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
465 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
466 D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
469 CONST D3DVERTEXELEMENT9 test_buffer[] =
470 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
471 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
472 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
473 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
474 D3DFVF_XYZB5 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
477 CONST D3DVERTEXELEMENT9 test_buffer[] =
478 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
479 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
480 { 0, 28, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
481 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB5, test_buffer, 1));
484 CONST D3DVERTEXELEMENT9 test_buffer[] =
485 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
486 { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
487 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB1, test_buffer, 1));
490 CONST D3DVERTEXELEMENT9 test_buffer[] =
491 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
492 { 0, 12, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
493 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
494 D3DFVF_XYZB1 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
497 CONST D3DVERTEXELEMENT9 test_buffer[] =
498 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
499 { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
500 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
501 D3DFVF_XYZB1 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
504 CONST D3DVERTEXELEMENT9 test_buffer[] =
505 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
506 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
507 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB2, test_buffer, 1));
510 CONST D3DVERTEXELEMENT9 test_buffer[] =
511 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
512 { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
513 { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
514 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
515 D3DFVF_XYZB2 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
518 CONST D3DVERTEXELEMENT9 test_buffer[] =
519 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
520 { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
521 { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
522 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
523 D3DFVF_XYZB2 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
526 CONST D3DVERTEXELEMENT9 test_buffer[] =
527 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
528 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
529 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB3, test_buffer, 1));
532 CONST D3DVERTEXELEMENT9 test_buffer[] =
533 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
534 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
535 { 0, 20, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
536 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
537 D3DFVF_XYZB3 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
540 CONST D3DVERTEXELEMENT9 test_buffer[] =
541 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
542 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
543 { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
544 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
545 D3DFVF_XYZB3 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
548 CONST D3DVERTEXELEMENT9 test_buffer[] =
549 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
550 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
551 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4, test_buffer, 1));
554 CONST D3DVERTEXELEMENT9 test_buffer[] =
555 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
556 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
557 { 0, 24, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
558 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
559 D3DFVF_XYZB4 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
562 CONST D3DVERTEXELEMENT9 test_buffer[] =
563 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
564 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
565 { 0, 24, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
566 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
567 D3DFVF_XYZB4 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
570 CONST D3DVERTEXELEMENT9 test_buffer[] =
571 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
572 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_NORMAL, test_buffer, 1));
575 CONST D3DVERTEXELEMENT9 test_buffer[] =
576 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
577 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_PSIZE, test_buffer, 1));
580 CONST D3DVERTEXELEMENT9 test_buffer[] =
581 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
582 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_DIFFUSE, test_buffer, 1));
585 CONST D3DVERTEXELEMENT9 test_buffer[] =
586 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
587 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_SPECULAR, test_buffer, 1));
590 /* Make sure textures of different sizes work */
592 CONST D3DVERTEXELEMENT9 test_buffer[] =
593 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
594 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
595 D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEX1, test_buffer, 1));
598 CONST D3DVERTEXELEMENT9 test_buffer[] =
599 { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
600 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
601 D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEX1, test_buffer, 1));
604 CONST D3DVERTEXELEMENT9 test_buffer[] =
605 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
606 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
607 D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, test_buffer, 1));
610 CONST D3DVERTEXELEMENT9 test_buffer[] =
611 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
612 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
613 D3DFVF_TEXCOORDSIZE4(0) | D3DFVF_TEX1, test_buffer, 1));
616 /* Make sure the TEXCOORD index works correctly - try several textures */
618 CONST D3DVERTEXELEMENT9 test_buffer[] =
619 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
620 { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
621 { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
622 { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
623 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
624 D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEXCOORDSIZE2(2) |
625 D3DFVF_TEXCOORDSIZE4(3) | D3DFVF_TEX4, test_buffer, 1));
628 /* Now try a combination test */
630 CONST D3DVERTEXELEMENT9 test_buffer[] =
631 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
632 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
633 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
634 { 0, 32, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
635 { 0, 36, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 },
636 { 0, 44, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
637 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4 | D3DFVF_SPECULAR | D3DFVF_DIFFUSE |
638 D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEX2, test_buffer, 1));
641 /* Setting the FVF to 0 should result in no change to the default decl */
642 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, 0, default_elements, 0));
645 IDirect3DDevice9_SetVertexDeclaration ( pDevice, NULL );
646 if ( default_decl ) IUnknown_Release (default_decl);
649 /* Check whether a declaration converted from FVF is shared.
650 * Check whether refcounts behave as expected */
651 static void test_fvf_decl_management(
652 IDirect3DDevice9* device) {
655 IDirect3DVertexDeclaration9* result_decl1 = NULL;
656 IDirect3DVertexDeclaration9* result_decl2 = NULL;
657 IDirect3DVertexDeclaration9* result_decl3 = NULL;
658 IDirect3DVertexDeclaration9* result_decl4 = NULL;
659 int ref1, ref2, ref3, ref4;
661 DWORD test_fvf1 = D3DFVF_XYZRHW;
662 DWORD test_fvf2 = D3DFVF_NORMAL;
663 CONST D3DVERTEXELEMENT9 test_elements1[] =
664 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
665 CONST D3DVERTEXELEMENT9 test_elements2[] =
666 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
668 /* Clear down any current vertex declaration */
669 hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
670 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
671 if (FAILED(hr)) return;
674 hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
675 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
676 if (FAILED(hr)) return;
678 /* Get converted decl (#1) */
679 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl1);
680 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
681 if (FAILED(hr)) return;
683 /* Get converted decl again (#2) */
684 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl2);
685 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
686 if (FAILED(hr)) return;
689 hr = IDirect3DDevice9_SetFVF( device, test_fvf2);
690 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
691 if (FAILED(hr)) return;
693 /* The contents should correspond to the first conversion */
694 VDECL_CHECK(compare_elements(result_decl1, test_elements1));
696 /* Get converted decl (#3) */
697 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl3);
698 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
699 if (FAILED(hr)) return;
701 /* The object should be the same */
702 ok (result_decl1 == result_decl2, "Declaration object changes on the second Get() call\n");
703 ok (result_decl2 != result_decl3, "Declaration object did not change during conversion\n");
705 /* The contents should correspond to the second conversion */
706 VDECL_CHECK(compare_elements(result_decl3, test_elements2));
707 /* Re-Check if the first decl was overwritten by the new Get() */
708 VDECL_CHECK(compare_elements(result_decl1, test_elements1));
710 hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
711 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
712 if (FAILED(hr)) return;
714 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl4);
715 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
716 if (FAILED(hr)) return;
718 ok(result_decl4 == result_decl1, "Setting an already used FVF over results in a different vertexdeclaration\n");
720 ref1 = get_refcount((IUnknown*) result_decl1);
721 ref2 = get_refcount((IUnknown*) result_decl2);
722 ref3 = get_refcount((IUnknown*) result_decl3);
723 ref4 = get_refcount((IUnknown*) result_decl4);
724 ok (ref1 == 3, "Refcount #1 is %d, expected 3\n", ref1);
725 ok (ref2 == 3, "Refcount #2 is %d, expected 3\n", ref2);
726 ok (ref3 == 1, "Refcount #3 is %d, expected 1\n", ref3);
727 ok (ref4 == 3, "Refcount #4 is %d, expected 3\n", ref4);
729 /* Clear down any current vertex declaration */
730 hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
731 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
732 if (FAILED(hr)) return;
734 IDirect3DVertexDeclaration9_Release(result_decl1);
735 IDirect3DVertexDeclaration9_Release(result_decl2);
736 IDirect3DVertexDeclaration9_Release(result_decl3);
737 IDirect3DVertexDeclaration9_Release(result_decl4);
742 static void test_vertex_declaration_alignment(
743 IDirect3DDevice9* device) {
746 IDirect3DVertexDeclaration9* result_decl = NULL;
749 CONST D3DVERTEXELEMENT9 test_elements[5][3] =
752 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
753 { 0, 16, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
757 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
758 { 0, 17, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
762 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
763 { 0, 18, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
767 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
768 { 0, 19, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
772 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
773 { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR , 0 },
777 HRESULT results[5] = {D3D_OK, E_FAIL, E_FAIL, E_FAIL, D3D_OK};
779 for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
781 hr = IDirect3DDevice9_CreateVertexDeclaration(device, test_elements[i], &result_decl);
782 ok(hr == results[i], "CreateVertexDeclaration for declaration %d returned %#x, expected %#x\n",
784 if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
788 static void test_unused_type(
789 IDirect3DDevice9* device) {
792 IDirect3DVertexDeclaration9* result_decl = NULL;
795 static const D3DVERTEXELEMENT9 test_elements[][3] =
798 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
799 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_COLOR , 0 },
803 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
804 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 0 },
808 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
809 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 1 },
813 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
814 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
818 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
819 { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_TEXCOORD, 12},
823 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
824 { 0, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL, 0 },
828 { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
829 { 1, 16, D3DDECLTYPE_UNUSED, 0, D3DDECLUSAGE_NORMAL, 0 },
834 for(i = 0; i < sizeof(test_elements) / sizeof(test_elements[0]); i++) {
836 hr = IDirect3DDevice9_CreateVertexDeclaration(device, test_elements[i], &result_decl);
837 ok(hr == E_FAIL, "CreateVertexDeclaration for declaration %d returned %#x, expected E_FAIL(%#x)\n",
839 if(result_decl) IDirect3DVertexDeclaration9_Release(result_decl);
842 START_TEST(vertexdeclaration)
844 static D3DVERTEXELEMENT9 simple_decl[] = {
845 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
847 UINT simple_decl_num_elements = sizeof(simple_decl) / sizeof(*simple_decl);
848 IDirect3DDevice9 *device_ptr = 0;
849 IDirect3DVertexDeclaration9 *decl_ptr = 0;
851 d3d9_handle = LoadLibraryA("d3d9.dll");
854 skip("Could not load d3d9.dll\n");
858 device_ptr = init_d3d9();
861 skip("Failed to initialise d3d9\n");
865 decl_ptr = test_create_vertex_declaration(device_ptr, simple_decl);
868 skip("Failed to create a vertex declaration\n");
872 test_get_set_vertex_declaration(device_ptr, decl_ptr);
873 test_get_declaration(decl_ptr, simple_decl, simple_decl_num_elements);
874 test_fvf_decl_conversion(device_ptr);
875 test_fvf_decl_management(device_ptr);
876 test_vertex_declaration_alignment(device_ptr);
877 test_unused_type(device_ptr);