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 trace("could not create device, IDirect3D9_CreateDevice returned %#x\n", hres);
73 static int get_refcount(IUnknown *object)
75 IUnknown_AddRef(object);
76 return IUnknown_Release(object);
79 static inline void print_elements(
80 D3DVERTEXELEMENT9 *elements) {
82 D3DVERTEXELEMENT9 last = D3DDECL_END();
83 D3DVERTEXELEMENT9 *ptr = elements;
86 while (memcmp(ptr, &last, sizeof(D3DVERTEXELEMENT9))) {
89 "[Element %d] Stream = %d, Offset = %d, Type = %d, Method = %d, Usage = %d, UsageIndex = %d\n",
90 count, ptr->Stream, ptr->Offset, ptr->Type, ptr->Method, ptr->Usage, ptr->UsageIndex);
97 static int compare_elements(
98 IDirect3DVertexDeclaration9 *decl,
99 const D3DVERTEXELEMENT9 *expected_elements) {
102 unsigned int i, size;
103 D3DVERTEXELEMENT9 last = D3DDECL_END();
104 D3DVERTEXELEMENT9 *elements = NULL;
106 /* How many elements are there? */
107 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, NULL, &size );
108 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
109 if (FAILED(hr)) goto fail;
111 /* Allocate buffer */
112 elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * size);
113 ok (elements != NULL, "Out of memory, aborting test\n");
114 if (elements == NULL) goto fail;
116 /* Get the elements */
117 hr = IDirect3DVertexDeclaration9_GetDeclaration( decl, elements, &size);
118 ok(SUCCEEDED(hr), "GetDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
119 if (FAILED(hr)) goto fail;
121 /* Compare one by one */
122 for (i = 0; i < size; i++) {
126 int end1 = memcmp(&elements[i], &last, sizeof(D3DVERTEXELEMENT9));
127 int end2 = memcmp(&expected_elements[i], &last, sizeof(D3DVERTEXELEMENT9));
128 status = ((end1 && !end2) || (!end1 && end2));
129 ok (!status, "Mismatch in size, test declaration is %s than expected\n",
130 (end1 && !end2) ? "shorter" : "longer");
131 if (status) { print_elements(elements); goto fail; }
133 status = memcmp(&elements[i], &expected_elements[i], sizeof(D3DVERTEXELEMENT9));
134 ok (!status, "Mismatch in element %d\n", i);
135 if (status) { print_elements(elements); goto fail; }
138 HeapFree(GetProcessHeap(), 0, elements);
142 HeapFree(GetProcessHeap(), 0, elements);
146 static IDirect3DVertexDeclaration9 *test_create_vertex_declaration(IDirect3DDevice9 *device_ptr, D3DVERTEXELEMENT9 *vertex_decl)
148 IDirect3DVertexDeclaration9 *decl_ptr = 0;
151 hret = IDirect3DDevice9_CreateVertexDeclaration(device_ptr, vertex_decl, &decl_ptr);
152 ok(hret == D3D_OK && decl_ptr != NULL, "CreateVertexDeclaration returned: hret 0x%x, decl_ptr %p. "
153 "Expected hret 0x%x, decl_ptr != %p. Aborting.\n", hret, decl_ptr, D3D_OK, NULL);
158 static void test_get_set_vertex_declaration(IDirect3DDevice9 *device_ptr, IDirect3DVertexDeclaration9 *decl_ptr)
160 IDirect3DVertexDeclaration9 *current_decl_ptr = 0;
162 int decl_refcount = 0;
165 /* SetVertexDeclaration should not touch the declaration's refcount. */
166 i = get_refcount((IUnknown *)decl_ptr);
167 hret = IDirect3DDevice9_SetVertexDeclaration(device_ptr, decl_ptr);
168 decl_refcount = get_refcount((IUnknown *)decl_ptr);
169 ok(hret == D3D_OK && decl_refcount == i, "SetVertexDeclaration returned: hret 0x%x, refcount %d. "
170 "Expected hret 0x%x, refcount %d.\n", hret, decl_refcount, D3D_OK, i);
172 /* GetVertexDeclaration should increase the declaration's refcount by one. */
174 hret = IDirect3DDevice9_GetVertexDeclaration(device_ptr, ¤t_decl_ptr);
175 decl_refcount = get_refcount((IUnknown *)decl_ptr);
176 ok(hret == D3D_OK && decl_refcount == i && current_decl_ptr == decl_ptr,
177 "GetVertexDeclaration returned: hret 0x%x, current_decl_ptr %p refcount %d. "
178 "Expected hret 0x%x, current_decl_ptr %p, refcount %d.\n", hret, current_decl_ptr, decl_refcount, D3D_OK, decl_ptr, i);
181 static void test_get_declaration(IDirect3DVertexDeclaration9 *decl_ptr, D3DVERTEXELEMENT9 *vertex_decl, UINT expected_num_elements)
184 UINT num_elements = 0;
185 D3DVERTEXELEMENT9 *decl = 0;
188 /* First test only getting the number of elements */
189 num_elements = 0x1337c0de;
190 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
191 ok(hret == D3D_OK && num_elements == expected_num_elements,
192 "GetDeclaration returned: hret 0x%x, num_elements %d. "
193 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
196 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, NULL, &num_elements);
197 ok(hret == D3D_OK && num_elements == expected_num_elements,
198 "GetDeclaration returned: hret 0x%x, num_elements %d. "
199 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
201 /* Also test the returned data */
202 decl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
204 num_elements = 0x1337c0de;
205 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
206 ok(hret == D3D_OK && num_elements == expected_num_elements,
207 "GetDeclaration returned: hret 0x%x, num_elements %d. "
208 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
209 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
210 ok (!i, "Original and returned vertexdeclarations are not the same\n");
211 ZeroMemory(decl, sizeof(D3DVERTEXELEMENT9) * expected_num_elements);
214 hret = IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr, decl, &num_elements);
215 ok(hret == D3D_OK && num_elements == expected_num_elements,
216 "GetDeclaration returned: hret 0x%x, num_elements %d. "
217 "Expected hret 0x%x, num_elements %d.\n", hret, num_elements, D3D_OK, expected_num_elements);
218 i = memcmp(decl, vertex_decl, sizeof(vertex_decl));
219 ok (!i, "Original and returned vertexdeclarations are not the same\n");
221 HeapFree(GetProcessHeap(), 0, decl);
224 /* FIXME: also write a test, which shows that attempting to set
225 * an invalid vertex declaration returns E_FAIL */
227 static HRESULT test_fvf_to_decl(
228 IDirect3DDevice9* device,
229 IDirect3DVertexDeclaration9* default_decl,
231 const D3DVERTEXELEMENT9 expected_elements[],
232 char object_should_change)
236 IDirect3DVertexDeclaration9 *result_decl = NULL;
238 /* Set a default declaration to make sure it is changed */
239 hr = IDirect3DDevice9_SetVertexDeclaration ( device, default_decl );
240 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
241 if (FAILED(hr)) goto fail;
244 hr = IDirect3DDevice9_SetFVF( device, test_fvf);
245 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
246 if (FAILED(hr)) goto fail;
248 /* Check if the declaration object changed underneath */
249 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl);
250 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
251 if (FAILED(hr)) goto fail;
252 if (object_should_change) {
253 ok(result_decl != default_decl, "result declaration matches original\n");
254 if (result_decl == default_decl) goto fail;
256 ok(result_decl == default_decl, "result declaration does not match original\n");
257 if (result_decl != default_decl) goto fail;
260 /* Declaration content/size test */
261 ok(result_decl != NULL, "result declaration was null\n");
262 if (result_decl == NULL)
264 else if (compare_elements(result_decl, expected_elements) != S_OK)
267 if (result_decl) IUnknown_Release( result_decl );
271 if (result_decl) IUnknown_Release( result_decl );
275 static HRESULT test_decl_to_fvf(
276 IDirect3DDevice9* device,
278 CONST D3DVERTEXELEMENT9 test_decl[],
283 IDirect3DVertexDeclaration9 *vdecl = NULL;
285 DWORD result_fvf = 0xdeadbeef;
287 /* Set a default FVF of SPECULAR and DIFFUSE to make sure it is changed back to 0 */
288 hr = IDirect3DDevice9_SetFVF( device, default_fvf);
289 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
290 if (FAILED(hr)) goto fail;
292 /* Create a testing declaration */
293 hr = IDirect3DDevice9_CreateVertexDeclaration( device, test_decl, &vdecl );
294 ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
295 if (FAILED(hr)) goto fail;
297 /* Set the declaration */
298 hr = IDirect3DDevice9_SetVertexDeclaration ( device, vdecl );
299 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
300 if (FAILED(hr)) goto fail;
303 hr = IDirect3DDevice9_GetFVF( device, &result_fvf);
304 ok(SUCCEEDED(hr), "GetFVF returned %#x, expected %#x\n", hr, D3D_OK);
305 if (FAILED(hr)) goto fail;
307 ok(test_fvf == result_fvf, "result FVF was: %#x, expected: %#x\n", result_fvf, test_fvf);
309 if (test_fvf != result_fvf) goto fail;
311 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
312 if (vdecl) IUnknown_Release( vdecl );
316 IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
317 if (vdecl) IUnknown_Release( vdecl );
321 static void test_fvf_decl_conversion(IDirect3DDevice9 *pDevice)
327 IDirect3DVertexDeclaration9* default_decl = NULL;
328 DWORD default_fvf = D3DFVF_SPECULAR | D3DFVF_DIFFUSE;
329 D3DVERTEXELEMENT9 default_elements[] =
330 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
331 { 0, 4, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
333 /* Create a default declaration and FVF that does not match any of the tests */
334 hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, default_elements, &default_decl );
335 ok(SUCCEEDED(hr), "CreateVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
336 if (FAILED(hr)) goto cleanup;
338 /* Test conversions from vertex declaration to an FVF.
339 * For some reason those seem to occur only for POSITION/POSITIONT,
340 * Otherwise the FVF is forced to 0 - maybe this is configuration specific */
342 CONST D3DVERTEXELEMENT9 test_buffer[] =
343 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
344 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZ));
347 CONST D3DVERTEXELEMENT9 test_buffer[] =
348 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
349 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, D3DFVF_XYZRHW));
351 for (i = 0; i < 4; i++) {
352 CONST D3DVERTEXELEMENT9 test_buffer[] =
353 { { 0, 0, D3DDECLTYPE_FLOAT1+i, 0, D3DDECLUSAGE_BLENDWEIGHT, 0}, D3DDECL_END() };
354 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
357 CONST D3DVERTEXELEMENT9 test_buffer[] =
358 { { 0, 0, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0}, D3DDECL_END() };
359 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
362 CONST D3DVERTEXELEMENT9 test_buffer[] =
363 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
364 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
367 CONST D3DVERTEXELEMENT9 test_buffer[] =
368 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
369 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
372 CONST D3DVERTEXELEMENT9 test_buffer[] =
373 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
374 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
377 CONST D3DVERTEXELEMENT9 test_buffer[] =
378 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
379 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
382 /* Make sure textures of different sizes work */
384 CONST D3DVERTEXELEMENT9 test_buffer[] =
385 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
386 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
389 CONST D3DVERTEXELEMENT9 test_buffer[] =
390 { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
391 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
394 CONST D3DVERTEXELEMENT9 test_buffer[] =
395 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
396 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
399 CONST D3DVERTEXELEMENT9 test_buffer[] =
400 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
401 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
404 /* Make sure the TEXCOORD index works correctly - try several textures */
406 CONST D3DVERTEXELEMENT9 test_buffer[] =
407 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
408 { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
409 { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
410 { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
411 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
414 /* No FVF mapping available */
416 CONST D3DVERTEXELEMENT9 test_buffer[] =
417 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 1 }, D3DDECL_END() };
418 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
421 CONST D3DVERTEXELEMENT9 test_buffer[] =
422 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 1 }, D3DDECL_END() };
423 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
426 /* Try empty declaration */
428 CONST D3DVERTEXELEMENT9 test_buffer[] = { D3DDECL_END() };
429 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
432 /* Now try a combination test */
434 CONST D3DVERTEXELEMENT9 test_buffer[] =
435 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITIONT, 0 },
436 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 },
437 { 0, 24, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 },
438 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
439 { 0, 32, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
440 { 0, 44, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
441 VDECL_CHECK(test_decl_to_fvf(pDevice, default_fvf, test_buffer, 0));
444 /* Test conversions from FVF to a vertex declaration
445 * These seem to always occur internally. A new declaration object is created if necessary */
448 CONST D3DVERTEXELEMENT9 test_buffer[] =
449 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() };
450 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZ, test_buffer, 1));
453 CONST D3DVERTEXELEMENT9 test_buffer[] =
454 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
455 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZRHW, test_buffer, 1));
458 CONST D3DVERTEXELEMENT9 test_buffer[] =
459 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
460 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
461 { 0, 28, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
462 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
463 D3DFVF_XYZB5 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
466 CONST D3DVERTEXELEMENT9 test_buffer[] =
467 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
468 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
469 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
470 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
471 D3DFVF_XYZB5 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
474 CONST D3DVERTEXELEMENT9 test_buffer[] =
475 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
476 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
477 { 0, 28, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
478 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB5, test_buffer, 1));
481 CONST D3DVERTEXELEMENT9 test_buffer[] =
482 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
483 { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
484 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB1, test_buffer, 1));
487 CONST D3DVERTEXELEMENT9 test_buffer[] =
488 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
489 { 0, 12, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
490 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
491 D3DFVF_XYZB1 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
494 CONST D3DVERTEXELEMENT9 test_buffer[] =
495 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
496 { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
497 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
498 D3DFVF_XYZB1 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
501 CONST D3DVERTEXELEMENT9 test_buffer[] =
502 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
503 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
504 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB2, test_buffer, 1));
507 CONST D3DVERTEXELEMENT9 test_buffer[] =
508 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
509 { 0, 12, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
510 { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
511 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
512 D3DFVF_XYZB2 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
515 CONST D3DVERTEXELEMENT9 test_buffer[] =
516 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
517 { 0, 12, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
518 { 0, 16, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
519 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
520 D3DFVF_XYZB2 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
523 CONST D3DVERTEXELEMENT9 test_buffer[] =
524 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
525 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
526 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB3, test_buffer, 1));
529 CONST D3DVERTEXELEMENT9 test_buffer[] =
530 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
531 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
532 { 0, 20, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
533 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
534 D3DFVF_XYZB3 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
537 CONST D3DVERTEXELEMENT9 test_buffer[] =
538 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
539 { 0, 12, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
540 { 0, 20, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
541 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
542 D3DFVF_XYZB3 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
545 CONST D3DVERTEXELEMENT9 test_buffer[] =
546 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
547 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 }, D3DDECL_END() };
548 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4, test_buffer, 1));
551 CONST D3DVERTEXELEMENT9 test_buffer[] =
552 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
553 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
554 { 0, 24, D3DDECLTYPE_UBYTE4, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
555 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
556 D3DFVF_XYZB4 | D3DFVF_LASTBETA_UBYTE4, test_buffer, 1));
559 CONST D3DVERTEXELEMENT9 test_buffer[] =
560 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
561 { 0, 12, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
562 { 0, 24, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_BLENDINDICES, 0 }, D3DDECL_END() };
563 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
564 D3DFVF_XYZB4 | D3DFVF_LASTBETA_D3DCOLOR, test_buffer, 1));
567 CONST D3DVERTEXELEMENT9 test_buffer[] =
568 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
569 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_NORMAL, test_buffer, 1));
572 CONST D3DVERTEXELEMENT9 test_buffer[] =
573 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_PSIZE, 0 }, D3DDECL_END() };
574 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_PSIZE, test_buffer, 1));
577 CONST D3DVERTEXELEMENT9 test_buffer[] =
578 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 }, D3DDECL_END() };
579 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_DIFFUSE, test_buffer, 1));
582 CONST D3DVERTEXELEMENT9 test_buffer[] =
583 { { 0, 0, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 }, D3DDECL_END() };
584 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_SPECULAR, test_buffer, 1));
587 /* Make sure textures of different sizes work */
589 CONST D3DVERTEXELEMENT9 test_buffer[] =
590 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
591 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
592 D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEX1, test_buffer, 1));
595 CONST D3DVERTEXELEMENT9 test_buffer[] =
596 { { 0, 0, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
597 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
598 D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEX1, test_buffer, 1));
601 CONST D3DVERTEXELEMENT9 test_buffer[] =
602 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
603 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
604 D3DFVF_TEXCOORDSIZE3(0) | D3DFVF_TEX1, test_buffer, 1));
607 CONST D3DVERTEXELEMENT9 test_buffer[] =
608 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() };
609 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
610 D3DFVF_TEXCOORDSIZE4(0) | D3DFVF_TEX1, test_buffer, 1));
613 /* Make sure the TEXCOORD index works correctly - try several textures */
615 CONST D3DVERTEXELEMENT9 test_buffer[] =
616 { { 0, 0, D3DDECLTYPE_FLOAT1, 0, D3DDECLUSAGE_TEXCOORD, 0 },
617 { 0, 4, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 },
618 { 0, 16, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 2 },
619 { 0, 24, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_TEXCOORD, 3 }, D3DDECL_END() };
620 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl,
621 D3DFVF_TEXCOORDSIZE1(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEXCOORDSIZE2(2) |
622 D3DFVF_TEXCOORDSIZE4(3) | D3DFVF_TEX4, test_buffer, 1));
625 /* Now try a combination test */
627 CONST D3DVERTEXELEMENT9 test_buffer[] =
628 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_POSITION, 0 },
629 { 0, 12, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_BLENDWEIGHT, 0 },
630 { 0, 28, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 0 },
631 { 0, 32, D3DDECLTYPE_D3DCOLOR, 0, D3DDECLUSAGE_COLOR, 1 },
632 { 0, 36, D3DDECLTYPE_FLOAT2, 0, D3DDECLUSAGE_TEXCOORD, 0 },
633 { 0, 44, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_TEXCOORD, 1 }, D3DDECL_END() };
634 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, D3DFVF_XYZB4 | D3DFVF_SPECULAR | D3DFVF_DIFFUSE |
635 D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE3(1) | D3DFVF_TEX2, test_buffer, 1));
638 /* Setting the FVF to 0 should result in no change to the default decl */
639 VDECL_CHECK(test_fvf_to_decl(pDevice, default_decl, 0, default_elements, 0));
642 IDirect3DDevice9_SetVertexDeclaration ( pDevice, NULL );
643 if ( default_decl ) IUnknown_Release (default_decl);
646 /* Check whether a declaration converted from FVF is shared.
647 * Check whether refcounts behave as expected */
648 static void test_fvf_decl_management(
649 IDirect3DDevice9* device) {
652 IDirect3DVertexDeclaration9* result_decl1 = NULL;
653 IDirect3DVertexDeclaration9* result_decl2 = NULL;
654 IDirect3DVertexDeclaration9* result_decl3 = NULL;
655 IDirect3DVertexDeclaration9* result_decl4 = NULL;
656 int ref1, ref2, ref3, ref4;
658 DWORD test_fvf1 = D3DFVF_XYZRHW;
659 DWORD test_fvf2 = D3DFVF_NORMAL;
660 CONST D3DVERTEXELEMENT9 test_elements1[] =
661 { { 0, 0, D3DDECLTYPE_FLOAT4, 0, D3DDECLUSAGE_POSITIONT, 0 }, D3DDECL_END() };
662 CONST D3DVERTEXELEMENT9 test_elements2[] =
663 { { 0, 0, D3DDECLTYPE_FLOAT3, 0, D3DDECLUSAGE_NORMAL, 0 }, D3DDECL_END() };
665 /* Clear down any current vertex declaration */
666 hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
667 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
668 if (FAILED(hr)) return;
671 hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
672 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
673 if (FAILED(hr)) return;
675 /* Get converted decl (#1) */
676 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl1);
677 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
678 if (FAILED(hr)) return;
680 /* Get converted decl again (#2) */
681 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl2);
682 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
683 if (FAILED(hr)) return;
686 hr = IDirect3DDevice9_SetFVF( device, test_fvf2);
687 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
688 if (FAILED(hr)) return;
690 /* The contents should correspond to the first conversion */
691 VDECL_CHECK(compare_elements(result_decl1, test_elements1));
693 /* Get converted decl (#3) */
694 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl3);
695 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
696 if (FAILED(hr)) return;
698 /* The object should be the same */
699 ok (result_decl1 == result_decl2, "Declaration object changes on the second Get() call\n");
700 ok (result_decl2 != result_decl3, "Declaration object did not change during conversion\n");
702 /* The contents should correspond to the second conversion */
703 VDECL_CHECK(compare_elements(result_decl3, test_elements2));
704 /* Re-Check if the first decl was overwritten by the new Get() */
705 VDECL_CHECK(compare_elements(result_decl1, test_elements1));
707 hr = IDirect3DDevice9_SetFVF( device, test_fvf1);
708 ok(SUCCEEDED(hr), "SetFVF returned %#x, expected %#x\n", hr, D3D_OK);
709 if (FAILED(hr)) return;
711 hr = IDirect3DDevice9_GetVertexDeclaration ( device, &result_decl4);
712 ok(SUCCEEDED(hr), "GetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
713 if (FAILED(hr)) return;
715 ok(result_decl4 == result_decl1, "Setting an already used FVF over results in a different vertexdeclaration\n");
717 ref1 = get_refcount((IUnknown*) result_decl1);
718 ref2 = get_refcount((IUnknown*) result_decl2);
719 ref3 = get_refcount((IUnknown*) result_decl3);
720 ref4 = get_refcount((IUnknown*) result_decl4);
721 ok (ref1 == 3, "Refcount #1 is %d, expected 3\n", ref1);
722 ok (ref2 == 3, "Refcount #2 is %d, expected 3\n", ref2);
723 ok (ref3 == 1, "Refcount #3 is %d, expected 1\n", ref3);
724 ok (ref4 == 3, "Refcount #4 is %d, expected 3\n", ref4);
726 /* Clear down any current vertex declaration */
727 hr = IDirect3DDevice9_SetVertexDeclaration ( device, NULL );
728 ok (SUCCEEDED(hr), "SetVertexDeclaration returned %#x, expected %#x\n", hr, D3D_OK);
729 if (FAILED(hr)) return;
731 IDirect3DVertexDeclaration9_Release(result_decl1);
732 IDirect3DVertexDeclaration9_Release(result_decl2);
733 IDirect3DVertexDeclaration9_Release(result_decl3);
734 IDirect3DVertexDeclaration9_Release(result_decl4);
739 START_TEST(vertexdeclaration)
741 static D3DVERTEXELEMENT9 simple_decl[] = {
742 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
744 UINT simple_decl_num_elements = sizeof(simple_decl) / sizeof(*simple_decl);
745 IDirect3DDevice9 *device_ptr = 0;
746 IDirect3DVertexDeclaration9 *decl_ptr = 0;
748 d3d9_handle = LoadLibraryA("d3d9.dll");
751 skip("Could not load d3d9.dll\n");
755 device_ptr = init_d3d9();
758 skip("Failed to initialise d3d9\n");
762 decl_ptr = test_create_vertex_declaration(device_ptr, simple_decl);
765 skip("Failed to create a vertex declaration\n");
769 test_get_set_vertex_declaration(device_ptr, decl_ptr);
770 test_get_declaration(decl_ptr, simple_decl, simple_decl_num_elements);
771 test_fvf_decl_conversion(device_ptr);
772 test_fvf_decl_management(device_ptr);