2 * Tests for the D3DX9 core interfaces
4 * Copyright 2009 Tony Wasserka
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/test.h"
24 #include "d3dx9core.h"
26 static inline int get_ref(IUnknown
*obj
)
29 return IUnknown_Release(obj
);
32 static inline void check_ref(IUnknown
*obj
, int exp
)
34 int ref
= get_ref(obj
);
35 ok (exp
== ref
, "Invalid refcount. Expected %d, got %d\n", exp
, ref
);
38 static inline void check_release(IUnknown
*obj
, int exp
)
40 int ref
= IUnknown_Release(obj
);
41 ok (ref
== exp
, "Invalid refcount. Expected %d, got %d\n", exp
, ref
);
44 #define admitted_error 0.0001f
45 static inline void check_mat(D3DXMATRIX got
, D3DXMATRIX exp
)
50 if (fabs(U(exp
).m
[i
][j
]-U(got
).m
[i
][j
]) > admitted_error
)
53 ok(equal
, "Got matrix\n\t(%f,%f,%f,%f\n\t %f,%f,%f,%f\n\t %f,%f,%f,%f\n\t %f,%f,%f,%f)\n"
54 "Expected matrix=\n\t(%f,%f,%f,%f\n\t %f,%f,%f,%f\n\t %f,%f,%f,%f\n\t %f,%f,%f,%f)\n",
55 U(got
).m
[0][0],U(got
).m
[0][1],U(got
).m
[0][2],U(got
).m
[0][3],
56 U(got
).m
[1][0],U(got
).m
[1][1],U(got
).m
[1][2],U(got
).m
[1][3],
57 U(got
).m
[2][0],U(got
).m
[2][1],U(got
).m
[2][2],U(got
).m
[2][3],
58 U(got
).m
[3][0],U(got
).m
[3][1],U(got
).m
[3][2],U(got
).m
[3][3],
59 U(exp
).m
[0][0],U(exp
).m
[0][1],U(exp
).m
[0][2],U(exp
).m
[0][3],
60 U(exp
).m
[1][0],U(exp
).m
[1][1],U(exp
).m
[1][2],U(exp
).m
[1][3],
61 U(exp
).m
[2][0],U(exp
).m
[2][1],U(exp
).m
[2][2],U(exp
).m
[2][3],
62 U(exp
).m
[3][0],U(exp
).m
[3][1],U(exp
).m
[3][2],U(exp
).m
[3][3]);
65 static void test_ID3DXSprite(IDirect3DDevice9
*device
)
69 IDirect3DDevice9
*cmpdev
;
70 IDirect3DTexture9
*tex1
, *tex2
;
71 D3DXMATRIX mat
, cmpmat
;
74 D3DXVECTOR3 pos
, center
;
77 IDirect3DDevice9_GetDirect3D(device
, &d3d
);
78 hr
= IDirect3D9_CheckDeviceFormat(d3d
, 0, D3DDEVTYPE_HAL
, D3DFMT_UNKNOWN
, 0, D3DRTYPE_TEXTURE
, D3DFMT_A8R8G8B8
);
79 IDirect3D9_Release(d3d
);
80 ok (hr
== D3D_OK
, "D3DFMT_A8R8G8B8 not supported\n");
81 if (FAILED(hr
)) return;
83 hr
= IDirect3DDevice9_CreateTexture(device
, 64, 64, 1, 0, D3DFMT_A8R8G8B8
, D3DPOOL_DEFAULT
, &tex1
, NULL
);
84 ok (hr
== D3D_OK
, "Failed to create first texture (error code: %#x)\n", hr
);
85 if (FAILED(hr
)) return;
87 hr
= IDirect3DDevice9_CreateTexture(device
, 32, 32, 1, 0, D3DFMT_A8R8G8B8
, D3DPOOL_DEFAULT
, &tex2
, NULL
);
88 ok (hr
== D3D_OK
, "Failed to create second texture (error code: %#x)\n", hr
);
90 IDirect3DTexture9_Release(tex1
);
94 /* Test D3DXCreateSprite */
95 hr
= D3DXCreateSprite(device
, NULL
);
96 ok (hr
== D3DERR_INVALIDCALL
, "D3DXCreateSprite returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
98 hr
= D3DXCreateSprite(NULL
, &sprite
);
99 ok (hr
== D3DERR_INVALIDCALL
, "D3DXCreateSprite returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
101 hr
= D3DXCreateSprite(device
, &sprite
);
102 ok (hr
== D3D_OK
, "D3DXCreateSprite returned %#x, expected %#x\n", hr
, D3D_OK
);
105 /* Test ID3DXSprite_GetDevice */
106 hr
= ID3DXSprite_GetDevice(sprite
, NULL
);
107 ok (hr
== D3DERR_INVALIDCALL
, "GetDevice returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
109 hr
= ID3DXSprite_GetDevice(sprite
, &cmpdev
); /* cmpdev == NULL */
110 ok (hr
== D3D_OK
, "GetDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
112 hr
= ID3DXSprite_GetDevice(sprite
, &cmpdev
); /* cmpdev != NULL */
113 ok (hr
== D3D_OK
, "GetDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
115 IDirect3DDevice9_Release(device
);
116 IDirect3DDevice9_Release(device
);
119 /* Test ID3DXSprite_GetTransform */
120 hr
= ID3DXSprite_GetTransform(sprite
, NULL
);
121 ok (hr
== D3DERR_INVALIDCALL
, "GetTransform returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
122 hr
= ID3DXSprite_GetTransform(sprite
, &mat
);
123 ok (hr
== D3D_OK
, "GetTransform returned %#x, expected %#x\n", hr
, D3D_OK
);
126 D3DXMatrixIdentity(&identity
);
127 check_mat(mat
, identity
);
130 /* Test ID3DXSprite_SetTransform */
131 /* Set a transform and test if it gets returned correctly */
132 U(mat
).m
[0][0]=2.1f
; U(mat
).m
[0][1]=6.5f
; U(mat
).m
[0][2]=-9.6f
; U(mat
).m
[0][3]=1.7f
;
133 U(mat
).m
[1][0]=4.2f
; U(mat
).m
[1][1]=-2.5f
; U(mat
).m
[1][2]=2.1f
; U(mat
).m
[1][3]=5.5f
;
134 U(mat
).m
[2][0]=-2.6f
; U(mat
).m
[2][1]=0.3f
; U(mat
).m
[2][2]=8.6f
; U(mat
).m
[2][3]=8.4f
;
135 U(mat
).m
[3][0]=6.7f
; U(mat
).m
[3][1]=-5.1f
; U(mat
).m
[3][2]=6.1f
; U(mat
).m
[3][3]=2.2f
;
137 hr
= ID3DXSprite_SetTransform(sprite
, NULL
);
138 ok (hr
== D3DERR_INVALIDCALL
, "SetTransform returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
140 hr
= ID3DXSprite_SetTransform(sprite
, &mat
);
141 ok (hr
== D3D_OK
, "SetTransform returned %#x, expected %#x\n", hr
, D3D_OK
);
143 hr
=ID3DXSprite_GetTransform(sprite
, &cmpmat
);
144 if(SUCCEEDED(hr
)) check_mat(cmpmat
, mat
);
145 else skip("GetTransform returned %#x\n", hr
);
148 /* Test ID3DXSprite_SetWorldViewLH/RH */
150 hr
= ID3DXSprite_SetWorldViewLH(sprite
, &mat
, &mat
);
151 ok (hr
== D3D_OK
, "SetWorldViewLH returned %#x, expected %#x\n", hr
, D3D_OK
);
152 hr
= ID3DXSprite_SetWorldViewLH(sprite
, NULL
, &mat
);
153 ok (hr
== D3D_OK
, "SetWorldViewLH returned %#x, expected %#x\n", hr
, D3D_OK
);
154 hr
= ID3DXSprite_SetWorldViewLH(sprite
, &mat
, NULL
);
155 ok (hr
== D3D_OK
, "SetWorldViewLH returned %#x, expected %#x\n", hr
, D3D_OK
);
156 hr
= ID3DXSprite_SetWorldViewLH(sprite
, NULL
, NULL
);
157 ok (hr
== D3D_OK
, "SetWorldViewLH returned %#x, expected %#x\n", hr
, D3D_OK
);
159 hr
= ID3DXSprite_SetWorldViewRH(sprite
, &mat
, &mat
);
160 ok (hr
== D3D_OK
, "SetWorldViewRH returned %#x, expected %#x\n", hr
, D3D_OK
);
161 hr
= ID3DXSprite_SetWorldViewRH(sprite
, NULL
, &mat
);
162 ok (hr
== D3D_OK
, "SetWorldViewRH returned %#x, expected %#x\n", hr
, D3D_OK
);
163 hr
= ID3DXSprite_SetWorldViewRH(sprite
, &mat
, NULL
);
164 ok (hr
== D3D_OK
, "SetWorldViewRH returned %#x, expected %#x\n", hr
, D3D_OK
);
165 hr
= ID3DXSprite_SetWorldViewRH(sprite
, NULL
, NULL
);
166 ok (hr
== D3D_OK
, "SetWorldViewRH returned %#x, expected %#x\n", hr
, D3D_OK
);
168 IDirect3DDevice9_BeginScene(device
);
170 /* Test ID3DXSprite_Begin*/
171 hr
= ID3DXSprite_Begin(sprite
, 0);
172 ok (hr
== D3D_OK
, "Begin returned %#x, expected %#x\n", hr
, D3D_OK
);
174 IDirect3DDevice9_GetTransform(device
, D3DTS_WORLD
, &mat
);
175 D3DXMatrixIdentity(&cmpmat
);
176 check_mat(mat
, cmpmat
);
178 IDirect3DDevice9_GetTransform(device
, D3DTS_VIEW
, &mat
);
179 check_mat(mat
, cmpmat
);
181 IDirect3DDevice9_GetTransform(device
, D3DTS_PROJECTION
, &mat
);
182 IDirect3DDevice9_GetViewport(device
, &vp
);
183 D3DXMatrixOrthoOffCenterLH(&cmpmat
, vp
.X
+0.5f
, (float)vp
.Width
+vp
.X
+0.5f
, (float)vp
.Height
+vp
.Y
+0.5f
, vp
.Y
+0.5f
, vp
.MinZ
, vp
.MaxZ
);
184 check_mat(mat
, cmpmat
);
186 /* Test ID3DXSprite_Flush and ID3DXSprite_End */
187 hr
= ID3DXSprite_Flush(sprite
);
188 ok (hr
== D3D_OK
, "Flush returned %#x, expected %#x\n", hr
, D3D_OK
);
190 hr
= ID3DXSprite_End(sprite
);
191 ok (hr
== D3D_OK
, "End returned %#x, expected %#x\n", hr
, D3D_OK
);
193 hr
= ID3DXSprite_Flush(sprite
); /* May not be called before next Begin */
194 ok (hr
== D3DERR_INVALIDCALL
, "Flush returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
195 hr
= ID3DXSprite_End(sprite
);
196 ok (hr
== D3DERR_INVALIDCALL
, "End returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
198 /* Test ID3DXSprite_Draw */
199 hr
= ID3DXSprite_Begin(sprite
, 0);
200 ok (hr
== D3D_OK
, "Begin returned %#x, expected %#x\n", hr
, D3D_OK
);
202 if(FAILED(hr
)) skip("Couldn't ID3DXSprite_Begin, can't test ID3DXSprite_Draw\n");
203 else { /* Feed the sprite batch */
204 int texref1
, texref2
;
206 SetRect(&rect
, 53, 12, 142, 165);
207 pos
.x
= 2.2f
; pos
.y
= 4.5f
; pos
.z
= 5.1f
;
208 center
.x
= 11.3f
; center
.y
= 3.4f
; center
.z
= 1.2f
;
210 texref1
= get_ref((IUnknown
*)tex1
);
211 texref2
= get_ref((IUnknown
*)tex2
);
213 hr
= ID3DXSprite_Draw(sprite
, NULL
, &rect
, ¢er
, &pos
, D3DCOLOR_XRGB(255, 255, 255));
214 ok (hr
== D3DERR_INVALIDCALL
, "Draw returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
216 hr
= ID3DXSprite_Draw(sprite
, tex1
, &rect
, ¢er
, &pos
, D3DCOLOR_XRGB(255, 255, 255));
217 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
218 hr
= ID3DXSprite_Draw(sprite
, tex2
, &rect
, ¢er
, &pos
, D3DCOLOR_XRGB( 3, 45, 66));
219 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
220 hr
= ID3DXSprite_Draw(sprite
, tex1
, NULL
, ¢er
, &pos
, D3DCOLOR_XRGB(255, 255, 255));
221 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
222 hr
= ID3DXSprite_Draw(sprite
, tex1
, &rect
, NULL
, &pos
, D3DCOLOR_XRGB(255, 255, 255));
223 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
224 hr
= ID3DXSprite_Draw(sprite
, tex1
, &rect
, ¢er
, NULL
, D3DCOLOR_XRGB(255, 255, 255));
225 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
226 hr
= ID3DXSprite_Draw(sprite
, tex1
, NULL
, NULL
, NULL
, 0);
227 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
229 check_ref((IUnknown
*)tex1
, texref1
+5); check_ref((IUnknown
*)tex2
, texref2
+1);
230 hr
= ID3DXSprite_Flush(sprite
);
231 ok (hr
== D3D_OK
, "Flush returned %#x, expected %#x\n", hr
, D3D_OK
);
232 hr
= ID3DXSprite_Flush(sprite
); /* Flushing twice should work */
233 ok (hr
== D3D_OK
, "Flush returned %#x, expected %#x\n", hr
, D3D_OK
);
234 check_ref((IUnknown
*)tex1
, texref1
); check_ref((IUnknown
*)tex2
, texref2
);
236 hr
= ID3DXSprite_End(sprite
);
237 ok (hr
== D3D_OK
, "End returned %#x, expected %#x\n", hr
, D3D_OK
);
240 /* Test ID3DXSprite_OnLostDevice and ID3DXSprite_OnResetDevice */
241 /* Both can be called twice */
242 hr
= ID3DXSprite_OnLostDevice(sprite
);
243 ok (hr
== D3D_OK
, "OnLostDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
244 hr
= ID3DXSprite_OnLostDevice(sprite
);
245 ok (hr
== D3D_OK
, "OnLostDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
246 hr
= ID3DXSprite_OnResetDevice(sprite
);
247 ok (hr
== D3D_OK
, "OnResetDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
248 hr
= ID3DXSprite_OnResetDevice(sprite
);
249 ok (hr
== D3D_OK
, "OnResetDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
251 /* Make sure everything works like before */
252 hr
= ID3DXSprite_Begin(sprite
, 0);
253 ok (hr
== D3D_OK
, "Begin returned %#x, expected %#x\n", hr
, D3D_OK
);
254 hr
= ID3DXSprite_Draw(sprite
, tex2
, &rect
, ¢er
, &pos
, D3DCOLOR_XRGB(255, 255, 255));
255 ok (hr
== D3D_OK
, "Draw returned %#x, expected %#x\n", hr
, D3D_OK
);
256 hr
= ID3DXSprite_Flush(sprite
);
257 ok (hr
== D3D_OK
, "Flush returned %#x, expected %#x\n", hr
, D3D_OK
);
258 hr
= ID3DXSprite_End(sprite
);
259 ok (hr
== D3D_OK
, "End returned %#x, expected %#x\n", hr
, D3D_OK
);
261 /* OnResetDevice makes the interface "forget" the Begin call */
262 hr
= ID3DXSprite_Begin(sprite
, 0);
263 ok (hr
== D3D_OK
, "Begin returned %#x, expected %#x\n", hr
, D3D_OK
);
264 hr
= ID3DXSprite_OnResetDevice(sprite
);
265 ok (hr
== D3D_OK
, "OnResetDevice returned %#x, expected %#x\n", hr
, D3D_OK
);
266 hr
= ID3DXSprite_End(sprite
);
267 ok (hr
== D3DERR_INVALIDCALL
, "End returned %#x, expected %#x\n", hr
, D3DERR_INVALIDCALL
);
269 IDirect3DDevice9_EndScene(device
);
270 check_release((IUnknown
*)sprite
, 0);
271 check_release((IUnknown
*)tex2
, 0);
272 check_release((IUnknown
*)tex1
, 0);
279 IDirect3DDevice9
*device
;
280 D3DPRESENT_PARAMETERS d3dpp
;
283 wnd
= CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL
, NULL
, NULL
, NULL
);
284 d3d
= Direct3DCreate9(D3D_SDK_VERSION
);
286 skip("Couldn't create application window\n");
290 skip("Couldn't create IDirect3D9 object\n");
295 ZeroMemory(&d3dpp
, sizeof(d3dpp
));
296 d3dpp
.Windowed
= TRUE
;
297 d3dpp
.SwapEffect
= D3DSWAPEFFECT_DISCARD
;
298 hr
= IDirect3D9_CreateDevice(d3d
, D3DADAPTER_DEFAULT
, D3DDEVTYPE_HAL
, wnd
, D3DCREATE_MIXED_VERTEXPROCESSING
, &d3dpp
, &device
);
300 skip("Failed to create IDirect3DDevice9 object %#x\n", hr
);
301 IDirect3D9_Release(d3d
);
306 test_ID3DXSprite(device
);
308 check_release((IUnknown
*)device
, 0);
309 check_release((IUnknown
*)d3d
, 0);
310 if (wnd
) DestroyWindow(wnd
);