2 * Copyright (C) 2005 Henri Verbeet
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
23 static HMODULE d3d9_handle
= 0;
25 static HWND
create_window(void)
28 wc
.lpfnWndProc
= &DefWindowProc
;
29 wc
.lpszClassName
= "d3d9_test_wc";
32 return CreateWindow("d3d9_test_wc", "d3d9_test",
33 0, 0, 0, 0, 0, 0, 0, 0, 0);
36 static IDirect3DDevice9
*init_d3d9(void)
38 IDirect3D9
* (__stdcall
* d3d9_create
)(UINT SDKVersion
) = 0;
39 IDirect3D9
*d3d9_ptr
= 0;
40 IDirect3DDevice9
*device_ptr
= 0;
41 D3DPRESENT_PARAMETERS present_parameters
;
44 d3d9_create
= (void *)GetProcAddress(d3d9_handle
, "Direct3DCreate9");
45 ok(d3d9_create
!= NULL
, "Failed to get address of Direct3DCreate9\n");
46 if (!d3d9_create
) return NULL
;
48 d3d9_ptr
= d3d9_create(D3D_SDK_VERSION
);
49 ok(d3d9_ptr
!= NULL
, "Failed to create IDirect3D9 object\n");
50 if (!d3d9_ptr
) return NULL
;
52 ZeroMemory(&present_parameters
, sizeof(present_parameters
));
53 present_parameters
.Windowed
= TRUE
;
54 present_parameters
.hDeviceWindow
= create_window();
55 present_parameters
.SwapEffect
= D3DSWAPEFFECT_DISCARD
;
57 hres
= IDirect3D9_CreateDevice(d3d9_ptr
, D3DADAPTER_DEFAULT
, D3DDEVTYPE_HAL
, NULL
, D3DCREATE_SOFTWARE_VERTEXPROCESSING
, &present_parameters
, &device_ptr
);
58 ok(hres
== D3D_OK
, "IDirect3D_CreateDevice returned: 0x%lx\n", hres
);
63 static int get_refcount(IUnknown
*object
)
65 IUnknown_AddRef(object
);
66 return IUnknown_Release(object
);
69 static IDirect3DVertexDeclaration9
*test_create_vertex_declaration(IDirect3DDevice9
*device_ptr
, D3DVERTEXELEMENT9
*vertex_decl
)
71 IDirect3DVertexDeclaration9
*decl_ptr
= 0;
74 hret
= IDirect3DDevice9_CreateVertexDeclaration(device_ptr
, vertex_decl
, &decl_ptr
);
75 ok(hret
== D3D_OK
&& decl_ptr
!= NULL
, "CreateVertexDeclaration returned: hret 0x%lx, decl_ptr %p. "
76 "Expected hret 0x%lx, decl_ptr != %p. Aborting.\n", hret
, decl_ptr
, D3D_OK
, NULL
);
81 static void test_get_set_vertex_declaration(IDirect3DDevice9
*device_ptr
, IDirect3DVertexDeclaration9
*decl_ptr
)
83 IDirect3DVertexDeclaration9
*current_decl_ptr
= 0;
85 int decl_refcount
= 0;
88 /* SetVertexDeclaration should not touch the declaration's refcount. */
89 i
= get_refcount((IUnknown
*)decl_ptr
);
90 hret
= IDirect3DDevice9_SetVertexDeclaration(device_ptr
, decl_ptr
);
91 decl_refcount
= get_refcount((IUnknown
*)decl_ptr
);
92 ok(hret
== D3D_OK
&& decl_refcount
== i
, "SetVertexDeclaration returned: hret 0x%lx, refcount %d. "
93 "Expected hret 0x%lx, refcount %d.\n", hret
, decl_refcount
, D3D_OK
, i
);
95 /* GetVertexDeclaration should increase the declaration's refcount by one. */
97 hret
= IDirect3DDevice9_GetVertexDeclaration(device_ptr
, ¤t_decl_ptr
);
98 decl_refcount
= get_refcount((IUnknown
*)decl_ptr
);
99 ok(hret
== D3D_OK
&& decl_refcount
== i
&& current_decl_ptr
== decl_ptr
,
100 "GetVertexDeclaration returned: hret 0x%lx, current_decl_ptr %p refcount %d. "
101 "Expected hret 0x%lx, current_decl_ptr %p, refcount %d.\n", hret
, current_decl_ptr
, decl_refcount
, D3D_OK
, decl_ptr
, i
);
104 static void test_get_declaration(IDirect3DVertexDeclaration9
*decl_ptr
, D3DVERTEXELEMENT9
*vertex_decl
, UINT expected_num_elements
)
107 UINT num_elements
= 0;
108 D3DVERTEXELEMENT9
*decl
= 0;
111 /* First test only getting the number of elements */
112 num_elements
= 0x1337c0de;
113 hret
= IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr
, NULL
, &num_elements
);
114 ok(hret
== D3D_OK
&& num_elements
== expected_num_elements
,
115 "GetDeclaration returned: hret 0x%lx, num_elements %d. "
116 "Expected hret 0x%lx, num_elements %d.\n", hret
, num_elements
, D3D_OK
, expected_num_elements
);
119 hret
= IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr
, NULL
, &num_elements
);
120 ok(hret
== D3D_OK
&& num_elements
== expected_num_elements
,
121 "GetDeclaration returned: hret 0x%lx, num_elements %d. "
122 "Expected hret 0x%lx, num_elements %d.\n", hret
, num_elements
, D3D_OK
, expected_num_elements
);
124 /* Also test the returned data */
125 decl
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(D3DVERTEXELEMENT9
) * expected_num_elements
);
127 num_elements
= 0x1337c0de;
128 hret
= IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr
, decl
, &num_elements
);
129 ok(hret
== D3D_OK
&& num_elements
== expected_num_elements
,
130 "GetDeclaration returned: hret 0x%lx, num_elements %d. "
131 "Expected hret 0x%lx, num_elements %d.\n", hret
, num_elements
, D3D_OK
, expected_num_elements
);
132 i
= memcmp(decl
, vertex_decl
, sizeof(vertex_decl
));
133 ok (!i
, "Original and returned vertexdeclarations are not the same\n");
134 ZeroMemory(decl
, sizeof(D3DVERTEXELEMENT9
) * expected_num_elements
);
137 hret
= IDirect3DVertexDeclaration9_GetDeclaration(decl_ptr
, decl
, &num_elements
);
138 ok(hret
== D3D_OK
&& num_elements
== expected_num_elements
,
139 "GetDeclaration returned: hret 0x%lx, num_elements %d. "
140 "Expected hret 0x%lx, num_elements %d.\n", hret
, num_elements
, D3D_OK
, expected_num_elements
);
141 i
= memcmp(decl
, vertex_decl
, sizeof(vertex_decl
));
142 ok (!i
, "Original and returned vertexdeclarations are not the same\n");
144 HeapFree(GetProcessHeap(), 0, decl
);
147 START_TEST(vertexdeclaration
)
149 static D3DVERTEXELEMENT9 simple_decl
[] = {
150 { 0, 0, D3DDECLTYPE_FLOAT3
, D3DDECLMETHOD_DEFAULT
, D3DDECLUSAGE_POSITION
, 0 },
152 UINT simple_decl_num_elements
= sizeof(simple_decl
) / sizeof(*simple_decl
);
153 IDirect3DDevice9
*device_ptr
= 0;
154 IDirect3DVertexDeclaration9
*decl_ptr
= 0;
156 d3d9_handle
= LoadLibraryA("d3d9.dll");
159 trace("Could not load d3d9.dll, skipping tests\n");
163 device_ptr
= init_d3d9();
166 trace("Failed to initialise d3d9, aborting test.\n");
170 decl_ptr
= test_create_vertex_declaration(device_ptr
, simple_decl
);
173 trace("Failed to create a vertex declaration, aborting test.\n");
177 test_get_set_vertex_declaration(device_ptr
, decl_ptr
);
179 test_get_declaration(decl_ptr
, simple_decl
, simple_decl_num_elements
);