Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / d3dx9_36 / tests / core.c
blobcb5c7ec22c471f37a13c28ad0cddf58f18f0cedc
1 /*
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
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include <dxerr9.h>
24 #include "d3dx9core.h"
26 static inline int get_ref(IUnknown *obj)
28 IUnknown_AddRef(obj);
29 return IUnknown_Release(obj);
32 #define check_ref(obj, exp) _check_ref(__LINE__, obj, exp)
33 static inline void _check_ref(unsigned int line, IUnknown *obj, int exp)
35 int ref = get_ref(obj);
36 ok_(__FILE__, line)(exp == ref, "Invalid refcount. Expected %d, got %d\n", exp, ref);
39 #define check_release(obj, exp) _check_release(__LINE__, obj, exp)
40 static inline void _check_release(unsigned int line, IUnknown *obj, int exp)
42 int ref = IUnknown_Release(obj);
43 ok_(__FILE__, line)(ref == exp, "Invalid refcount. Expected %d, got %d\n", exp, ref);
46 #define admitted_error 0.0001f
47 static inline void check_mat(D3DXMATRIX got, D3DXMATRIX exp)
49 int i, j, equal=1;
50 for (i=0; i<4; i++)
51 for (j=0; j<4; j++)
52 if (fabs(U(exp).m[i][j]-U(got).m[i][j]) > admitted_error)
53 equal=0;
55 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"
56 "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",
57 U(got).m[0][0],U(got).m[0][1],U(got).m[0][2],U(got).m[0][3],
58 U(got).m[1][0],U(got).m[1][1],U(got).m[1][2],U(got).m[1][3],
59 U(got).m[2][0],U(got).m[2][1],U(got).m[2][2],U(got).m[2][3],
60 U(got).m[3][0],U(got).m[3][1],U(got).m[3][2],U(got).m[3][3],
61 U(exp).m[0][0],U(exp).m[0][1],U(exp).m[0][2],U(exp).m[0][3],
62 U(exp).m[1][0],U(exp).m[1][1],U(exp).m[1][2],U(exp).m[1][3],
63 U(exp).m[2][0],U(exp).m[2][1],U(exp).m[2][2],U(exp).m[2][3],
64 U(exp).m[3][0],U(exp).m[3][1],U(exp).m[3][2],U(exp).m[3][3]);
67 static void test_ID3DXBuffer(void)
69 ID3DXBuffer *buffer;
70 HRESULT hr;
71 ULONG count;
72 DWORD size;
74 hr = D3DXCreateBuffer(10, NULL);
75 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
77 hr = D3DXCreateBuffer(0, &buffer);
78 ok(hr == D3D_OK, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3D_OK);
80 size = ID3DXBuffer_GetBufferSize(buffer);
81 ok(!size, "GetBufferSize failed, got %u, expected %u\n", size, 0);
83 count = ID3DXBuffer_Release(buffer);
84 ok(!count, "ID3DBuffer has %u references left\n", count);
86 hr = D3DXCreateBuffer(3, &buffer);
87 ok(hr == D3D_OK, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3D_OK);
89 size = ID3DXBuffer_GetBufferSize(buffer);
90 ok(size == 3, "GetBufferSize failed, got %u, expected %u\n", size, 3);
92 count = ID3DXBuffer_Release(buffer);
93 ok(!count, "ID3DBuffer has %u references left\n", count);
96 static void test_ID3DXSprite(IDirect3DDevice9 *device)
98 ID3DXSprite *sprite;
99 IDirect3D9 *d3d;
100 IDirect3DDevice9 *cmpdev;
101 IDirect3DTexture9 *tex1, *tex2;
102 D3DXMATRIX mat, cmpmat;
103 D3DVIEWPORT9 vp;
104 RECT rect;
105 D3DXVECTOR3 pos, center;
106 HRESULT hr;
108 IDirect3DDevice9_GetDirect3D(device, &d3d);
109 hr = IDirect3D9_CheckDeviceFormat(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DUSAGE_DYNAMIC, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
110 IDirect3D9_Release(d3d);
111 ok (hr == D3D_OK, "D3DFMT_A8R8G8B8 not supported\n");
112 if (FAILED(hr)) return;
114 hr = IDirect3DDevice9_CreateTexture(device, 64, 64, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex1, NULL);
115 ok (hr == D3D_OK, "Failed to create first texture (error code: %#x)\n", hr);
116 if (FAILED(hr)) return;
118 hr = IDirect3DDevice9_CreateTexture(device, 32, 32, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex2, NULL);
119 ok (hr == D3D_OK, "Failed to create second texture (error code: %#x)\n", hr);
120 if (FAILED(hr)) {
121 IDirect3DTexture9_Release(tex1);
122 return;
125 /* Test D3DXCreateSprite */
126 hr = D3DXCreateSprite(device, NULL);
127 ok (hr == D3DERR_INVALIDCALL, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
129 hr = D3DXCreateSprite(NULL, &sprite);
130 ok (hr == D3DERR_INVALIDCALL, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
132 hr = D3DXCreateSprite(device, &sprite);
133 ok (hr == D3D_OK, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3D_OK);
136 /* Test ID3DXSprite_GetDevice */
137 hr = ID3DXSprite_GetDevice(sprite, NULL);
138 ok (hr == D3DERR_INVALIDCALL, "GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
140 hr = ID3DXSprite_GetDevice(sprite, &cmpdev); /* cmpdev == NULL */
141 ok (hr == D3D_OK, "GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
143 hr = ID3DXSprite_GetDevice(sprite, &cmpdev); /* cmpdev != NULL */
144 ok (hr == D3D_OK, "GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
146 IDirect3DDevice9_Release(device);
147 IDirect3DDevice9_Release(device);
150 /* Test ID3DXSprite_GetTransform */
151 hr = ID3DXSprite_GetTransform(sprite, NULL);
152 ok (hr == D3DERR_INVALIDCALL, "GetTransform returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
153 hr = ID3DXSprite_GetTransform(sprite, &mat);
154 ok (hr == D3D_OK, "GetTransform returned %#x, expected %#x\n", hr, D3D_OK);
155 if(SUCCEEDED(hr)) {
156 D3DXMATRIX identity;
157 D3DXMatrixIdentity(&identity);
158 check_mat(mat, identity);
161 /* Test ID3DXSprite_SetTransform */
162 /* Set a transform and test if it gets returned correctly */
163 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;
164 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;
165 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;
166 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;
168 hr = ID3DXSprite_SetTransform(sprite, NULL);
169 ok (hr == D3DERR_INVALIDCALL, "SetTransform returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
171 hr = ID3DXSprite_SetTransform(sprite, &mat);
172 ok (hr == D3D_OK, "SetTransform returned %#x, expected %#x\n", hr, D3D_OK);
173 if(SUCCEEDED(hr)) {
174 hr=ID3DXSprite_GetTransform(sprite, &cmpmat);
175 if(SUCCEEDED(hr)) check_mat(cmpmat, mat);
176 else skip("GetTransform returned %#x\n", hr);
179 /* Test ID3DXSprite_SetWorldViewLH/RH */
180 todo_wine {
181 hr = ID3DXSprite_SetWorldViewLH(sprite, &mat, &mat);
182 ok (hr == D3D_OK, "SetWorldViewLH returned %#x, expected %#x\n", hr, D3D_OK);
183 hr = ID3DXSprite_SetWorldViewLH(sprite, NULL, &mat);
184 ok (hr == D3D_OK, "SetWorldViewLH returned %#x, expected %#x\n", hr, D3D_OK);
185 hr = ID3DXSprite_SetWorldViewLH(sprite, &mat, NULL);
186 ok (hr == D3D_OK, "SetWorldViewLH returned %#x, expected %#x\n", hr, D3D_OK);
187 hr = ID3DXSprite_SetWorldViewLH(sprite, NULL, NULL);
188 ok (hr == D3D_OK, "SetWorldViewLH returned %#x, expected %#x\n", hr, D3D_OK);
190 hr = ID3DXSprite_SetWorldViewRH(sprite, &mat, &mat);
191 ok (hr == D3D_OK, "SetWorldViewRH returned %#x, expected %#x\n", hr, D3D_OK);
192 hr = ID3DXSprite_SetWorldViewRH(sprite, NULL, &mat);
193 ok (hr == D3D_OK, "SetWorldViewRH returned %#x, expected %#x\n", hr, D3D_OK);
194 hr = ID3DXSprite_SetWorldViewRH(sprite, &mat, NULL);
195 ok (hr == D3D_OK, "SetWorldViewRH returned %#x, expected %#x\n", hr, D3D_OK);
196 hr = ID3DXSprite_SetWorldViewRH(sprite, NULL, NULL);
197 ok (hr == D3D_OK, "SetWorldViewRH returned %#x, expected %#x\n", hr, D3D_OK);
199 IDirect3DDevice9_BeginScene(device);
201 /* Test ID3DXSprite_Begin*/
202 hr = ID3DXSprite_Begin(sprite, 0);
203 ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
205 IDirect3DDevice9_GetTransform(device, D3DTS_WORLD, &mat);
206 D3DXMatrixIdentity(&cmpmat);
207 check_mat(mat, cmpmat);
209 IDirect3DDevice9_GetTransform(device, D3DTS_VIEW, &mat);
210 check_mat(mat, cmpmat);
212 IDirect3DDevice9_GetTransform(device, D3DTS_PROJECTION, &mat);
213 IDirect3DDevice9_GetViewport(device, &vp);
214 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);
215 check_mat(mat, cmpmat);
217 /* Test ID3DXSprite_Flush and ID3DXSprite_End */
218 hr = ID3DXSprite_Flush(sprite);
219 ok (hr == D3D_OK, "Flush returned %#x, expected %#x\n", hr, D3D_OK);
221 hr = ID3DXSprite_End(sprite);
222 ok (hr == D3D_OK, "End returned %#x, expected %#x\n", hr, D3D_OK);
224 hr = ID3DXSprite_Flush(sprite); /* May not be called before next Begin */
225 ok (hr == D3DERR_INVALIDCALL, "Flush returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
226 hr = ID3DXSprite_End(sprite);
227 ok (hr == D3DERR_INVALIDCALL, "End returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
229 /* Test ID3DXSprite_Draw */
230 hr = ID3DXSprite_Begin(sprite, 0);
231 ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
233 if(FAILED(hr)) skip("Couldn't ID3DXSprite_Begin, can't test ID3DXSprite_Draw\n");
234 else { /* Feed the sprite batch */
235 int texref1, texref2;
237 SetRect(&rect, 53, 12, 142, 165);
238 pos.x = 2.2f; pos.y = 4.5f; pos.z = 5.1f;
239 center.x = 11.3f; center.y = 3.4f; center.z = 1.2f;
241 texref1 = get_ref((IUnknown*)tex1);
242 texref2 = get_ref((IUnknown*)tex2);
244 hr = ID3DXSprite_Draw(sprite, NULL, &rect, &center, &pos, D3DCOLOR_XRGB(255, 255, 255));
245 ok (hr == D3DERR_INVALIDCALL, "Draw returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
247 hr = ID3DXSprite_Draw(sprite, tex1, &rect, &center, &pos, D3DCOLOR_XRGB(255, 255, 255));
248 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
249 hr = ID3DXSprite_Draw(sprite, tex2, &rect, &center, &pos, D3DCOLOR_XRGB( 3, 45, 66));
250 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
251 hr = ID3DXSprite_Draw(sprite, tex1, NULL, &center, &pos, D3DCOLOR_XRGB(255, 255, 255));
252 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
253 hr = ID3DXSprite_Draw(sprite, tex1, &rect, NULL, &pos, D3DCOLOR_XRGB(255, 255, 255));
254 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
255 hr = ID3DXSprite_Draw(sprite, tex1, &rect, &center, NULL, D3DCOLOR_XRGB(255, 255, 255));
256 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
257 hr = ID3DXSprite_Draw(sprite, tex1, NULL, NULL, NULL, 0);
258 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
260 check_ref((IUnknown*)tex1, texref1+5); check_ref((IUnknown*)tex2, texref2+1);
261 hr = ID3DXSprite_Flush(sprite);
262 ok (hr == D3D_OK, "Flush returned %#x, expected %#x\n", hr, D3D_OK);
263 hr = ID3DXSprite_Flush(sprite); /* Flushing twice should work */
264 ok (hr == D3D_OK, "Flush returned %#x, expected %#x\n", hr, D3D_OK);
265 check_ref((IUnknown*)tex1, texref1); check_ref((IUnknown*)tex2, texref2);
267 hr = ID3DXSprite_End(sprite);
268 ok (hr == D3D_OK, "End returned %#x, expected %#x\n", hr, D3D_OK);
271 /* Test ID3DXSprite_OnLostDevice and ID3DXSprite_OnResetDevice */
272 /* Both can be called twice */
273 hr = ID3DXSprite_OnLostDevice(sprite);
274 ok (hr == D3D_OK, "OnLostDevice returned %#x, expected %#x\n", hr, D3D_OK);
275 hr = ID3DXSprite_OnLostDevice(sprite);
276 ok (hr == D3D_OK, "OnLostDevice returned %#x, expected %#x\n", hr, D3D_OK);
277 hr = ID3DXSprite_OnResetDevice(sprite);
278 ok (hr == D3D_OK, "OnResetDevice returned %#x, expected %#x\n", hr, D3D_OK);
279 hr = ID3DXSprite_OnResetDevice(sprite);
280 ok (hr == D3D_OK, "OnResetDevice returned %#x, expected %#x\n", hr, D3D_OK);
282 /* Make sure everything works like before */
283 hr = ID3DXSprite_Begin(sprite, 0);
284 ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
285 hr = ID3DXSprite_Draw(sprite, tex2, &rect, &center, &pos, D3DCOLOR_XRGB(255, 255, 255));
286 ok (hr == D3D_OK, "Draw returned %#x, expected %#x\n", hr, D3D_OK);
287 hr = ID3DXSprite_Flush(sprite);
288 ok (hr == D3D_OK, "Flush returned %#x, expected %#x\n", hr, D3D_OK);
289 hr = ID3DXSprite_End(sprite);
290 ok (hr == D3D_OK, "End returned %#x, expected %#x\n", hr, D3D_OK);
292 /* OnResetDevice makes the interface "forget" the Begin call */
293 hr = ID3DXSprite_Begin(sprite, 0);
294 ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
295 hr = ID3DXSprite_OnResetDevice(sprite);
296 ok (hr == D3D_OK, "OnResetDevice returned %#x, expected %#x\n", hr, D3D_OK);
297 hr = ID3DXSprite_End(sprite);
298 ok (hr == D3DERR_INVALIDCALL, "End returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
300 IDirect3DDevice9_EndScene(device);
301 check_release((IUnknown*)sprite, 0);
302 check_release((IUnknown*)tex2, 0);
303 check_release((IUnknown*)tex1, 0);
306 static void test_ID3DXFont(IDirect3DDevice9 *device)
308 D3DXFONT_DESC desc;
309 ID3DXFont *font;
310 HRESULT hr;
311 int ref;
312 int i;
313 static const struct {
314 INT font_height;
315 UINT expected_size;
316 DWORD expected_levels;
317 } texture_tests[] = {
318 { 6, 128, 4 },
319 { 8, 128, 4 },
320 { 10, 256, 5 },
321 { 12, 256, 5 },
322 { 72, 256, 8 }
326 /* D3DXCreateFont */
327 ref = get_ref((IUnknown*)device);
328 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
329 ok(hr == D3D_OK, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3D_OK);
330 check_ref((IUnknown*)device, ref + 1);
331 check_release((IUnknown*)font, 0);
332 check_ref((IUnknown*)device, ref);
334 hr = D3DXCreateFontA(device, 0, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
335 ok(hr == D3D_OK, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3D_OK);
336 ID3DXFont_Release(font);
338 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, NULL, &font);
339 ok(hr == D3D_OK, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3D_OK);
340 ID3DXFont_Release(font);
342 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "", &font);
343 ok(hr == D3D_OK, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3D_OK);
344 ID3DXFont_Release(font);
346 hr = D3DXCreateFontA(NULL, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
347 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
349 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", NULL);
350 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
352 hr = D3DXCreateFontA(NULL, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", NULL);
353 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFont returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
356 /* D3DXCreateFontIndirect */
357 desc.Height = 12;
358 desc.Width = 0;
359 desc.Weight = FW_DONTCARE;
360 desc.MipLevels = 0;
361 desc.Italic = FALSE;
362 desc.CharSet = DEFAULT_CHARSET;
363 desc.OutputPrecision = OUT_DEFAULT_PRECIS;
364 desc.Quality = DEFAULT_QUALITY;
365 desc.PitchAndFamily = DEFAULT_PITCH;
366 strcpy(desc.FaceName, "Arial");
367 hr = D3DXCreateFontIndirectA(device, &desc, &font);
368 ok(hr == D3D_OK, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3D_OK);
369 ID3DXFont_Release(font);
371 hr = D3DXCreateFontIndirectA(NULL, &desc, &font);
372 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
374 hr = D3DXCreateFontIndirectA(device, NULL, &font);
375 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
377 hr = D3DXCreateFontIndirectA(device, &desc, NULL);
378 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
381 /* ID3DXFont_GetDevice */
382 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
383 if(SUCCEEDED(hr)) {
384 IDirect3DDevice9 *bufdev;
386 hr = ID3DXFont_GetDevice(font, NULL);
387 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
389 ref = get_ref((IUnknown*)device);
390 hr = ID3DXFont_GetDevice(font, &bufdev);
391 ok(hr == D3D_OK, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
392 check_release((IUnknown*)bufdev, ref);
394 ID3DXFont_Release(font);
395 } else skip("Failed to create a ID3DXFont object\n");
398 /* ID3DXFont_GetDesc */
399 hr = D3DXCreateFontA(device, 12, 8, FW_BOLD, 2, TRUE, ANSI_CHARSET, OUT_RASTER_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, "Arial", &font);
400 if(SUCCEEDED(hr)) {
401 hr = ID3DXFont_GetDescA(font, NULL);
402 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
404 hr = ID3DXFont_GetDescA(font, &desc);
405 ok(hr == D3D_OK, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
407 ok(desc.Height == 12, "ID3DXFont_GetDesc returned font height %d, expected %d\n", desc.Height, 12);
408 ok(desc.Width == 8, "ID3DXFont_GetDesc returned font width %d, expected %d\n", desc.Width, 8);
409 ok(desc.Weight == FW_BOLD, "ID3DXFont_GetDesc returned font weight %d, expected %d\n", desc.Weight, FW_BOLD);
410 ok(desc.MipLevels == 2, "ID3DXFont_GetDesc returned font miplevels %d, expected %d\n", desc.MipLevels, 2);
411 ok(desc.Italic == TRUE, "ID3DXFont_GetDesc says Italic was %d, but Italic should be %d\n", desc.Italic, TRUE);
412 ok(desc.CharSet == ANSI_CHARSET, "ID3DXFont_GetDesc returned font charset %d, expected %d\n", desc.CharSet, ANSI_CHARSET);
413 ok(desc.OutputPrecision == OUT_RASTER_PRECIS, "ID3DXFont_GetDesc returned an output precision of %d, expected %d\n", desc.OutputPrecision, OUT_RASTER_PRECIS);
414 ok(desc.Quality == ANTIALIASED_QUALITY, "ID3DXFont_GetDesc returned font quality %d, expected %d\n", desc.Quality, ANTIALIASED_QUALITY);
415 ok(desc.PitchAndFamily == VARIABLE_PITCH, "ID3DXFont_GetDesc returned pitch and family %d, expected %d\n", desc.PitchAndFamily, VARIABLE_PITCH);
416 ok(strcmp(desc.FaceName, "Arial") == 0, "ID3DXFont_GetDesc returned facename \"%s\", expected \"%s\"\n", desc.FaceName, "Arial");
418 ID3DXFont_Release(font);
419 } else skip("Failed to create a ID3DXFont object\n");
422 /* ID3DXFont_GetDC + ID3DXFont_GetTextMetrics */
423 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
424 if(SUCCEEDED(hr)) {
425 HDC hdc;
427 hdc = ID3DXFont_GetDC(font);
428 ok(hdc != NULL, "ID3DXFont_GetDC returned an invalid handle\n");
429 if(hdc) {
430 TEXTMETRICA metrics, expmetrics;
431 BOOL ret;
433 ret = ID3DXFont_GetTextMetricsA(font, &metrics);
434 ok(ret, "ID3DXFont_GetTextMetricsA failed\n");
435 ret = GetTextMetricsA(hdc, &expmetrics);
436 ok(ret, "GetTextMetricsA failed\n");
438 ok(metrics.tmHeight == expmetrics.tmHeight, "Returned height %d, expected %d\n", metrics.tmHeight, expmetrics.tmHeight);
439 ok(metrics.tmAscent == expmetrics.tmAscent, "Returned ascent %d, expected %d\n", metrics.tmAscent, expmetrics.tmAscent);
440 ok(metrics.tmDescent == expmetrics.tmDescent, "Returned descent %d, expected %d\n", metrics.tmDescent, expmetrics.tmDescent);
441 ok(metrics.tmInternalLeading == expmetrics.tmInternalLeading, "Returned internal leading %d, expected %d\n", metrics.tmInternalLeading, expmetrics.tmInternalLeading);
442 ok(metrics.tmExternalLeading == expmetrics.tmExternalLeading, "Returned external leading %d, expected %d\n", metrics.tmExternalLeading, expmetrics.tmExternalLeading);
443 ok(metrics.tmAveCharWidth == expmetrics.tmAveCharWidth, "Returned average char width %d, expected %d\n", metrics.tmAveCharWidth, expmetrics.tmAveCharWidth);
444 ok(metrics.tmMaxCharWidth == expmetrics.tmMaxCharWidth, "Returned maximum char width %d, expected %d\n", metrics.tmMaxCharWidth, expmetrics.tmMaxCharWidth);
445 ok(metrics.tmWeight == expmetrics.tmWeight, "Returned weight %d, expected %d\n", metrics.tmWeight, expmetrics.tmWeight);
446 ok(metrics.tmOverhang == expmetrics.tmOverhang, "Returned overhang %d, expected %d\n", metrics.tmOverhang, expmetrics.tmOverhang);
447 ok(metrics.tmDigitizedAspectX == expmetrics.tmDigitizedAspectX, "Returned digitized x aspect %d, expected %d\n", metrics.tmDigitizedAspectX, expmetrics.tmDigitizedAspectX);
448 ok(metrics.tmDigitizedAspectY == expmetrics.tmDigitizedAspectY, "Returned digitized y aspect %d, expected %d\n", metrics.tmDigitizedAspectY, expmetrics.tmDigitizedAspectY);
449 ok(metrics.tmFirstChar == expmetrics.tmFirstChar, "Returned first char %d, expected %d\n", metrics.tmFirstChar, expmetrics.tmFirstChar);
450 ok(metrics.tmLastChar == expmetrics.tmLastChar, "Returned last char %d, expected %d\n", metrics.tmLastChar, expmetrics.tmLastChar);
451 ok(metrics.tmDefaultChar == expmetrics.tmDefaultChar, "Returned default char %d, expected %d\n", metrics.tmDefaultChar, expmetrics.tmDefaultChar);
452 ok(metrics.tmBreakChar == expmetrics.tmBreakChar, "Returned break char %d, expected %d\n", metrics.tmBreakChar, expmetrics.tmBreakChar);
453 ok(metrics.tmItalic == expmetrics.tmItalic, "Returned italic %d, expected %d\n", metrics.tmItalic, expmetrics.tmItalic);
454 ok(metrics.tmUnderlined == expmetrics.tmUnderlined, "Returned underlined %d, expected %d\n", metrics.tmUnderlined, expmetrics.tmUnderlined);
455 ok(metrics.tmStruckOut == expmetrics.tmStruckOut, "Returned struck out %d, expected %d\n", metrics.tmStruckOut, expmetrics.tmStruckOut);
456 ok(metrics.tmPitchAndFamily == expmetrics.tmPitchAndFamily, "Returned pitch and family %d, expected %d\n", metrics.tmPitchAndFamily, expmetrics.tmPitchAndFamily);
457 ok(metrics.tmCharSet == expmetrics.tmCharSet, "Returned charset %d, expected %d\n", metrics.tmCharSet, expmetrics.tmCharSet);
459 ID3DXFont_Release(font);
460 } else skip("Failed to create a ID3DXFont object\n");
463 /* ID3DXFont_PreloadText */
464 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
465 if(SUCCEEDED(hr)) {
466 const WCHAR testW[] = {'t','e','s','t',0};
468 todo_wine {
469 hr = ID3DXFont_PreloadTextA(font, NULL, -1);
470 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_PreloadTextA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
471 hr = ID3DXFont_PreloadTextA(font, NULL, 0);
472 ok(hr == D3D_OK, "ID3DXFont_PreloadTextA returned %#x, expected %#x\n", hr, D3D_OK);
473 hr = ID3DXFont_PreloadTextA(font, NULL, 1);
474 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_PreloadTextA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
475 hr = ID3DXFont_PreloadTextA(font, "test", -1);
476 ok(hr == D3D_OK, "ID3DXFont_PreloadTextA returned %#x, expected %#x\n", hr, D3D_OK);
478 hr = ID3DXFont_PreloadTextW(font, NULL, -1);
479 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_PreloadTextW returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
480 hr = ID3DXFont_PreloadTextW(font, NULL, 0);
481 ok(hr == D3D_OK, "ID3DXFont_PreloadTextW returned %#x, expected %#x\n", hr, D3D_OK);
482 hr = ID3DXFont_PreloadTextW(font, NULL, 1);
483 ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_PreloadTextW returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
484 hr = ID3DXFont_PreloadTextW(font, testW, -1);
485 ok(hr == D3D_OK, "ID3DXFont_PreloadTextW returned %#x, expected %#x\n", hr, D3D_OK);
488 check_release((IUnknown*)font, 0);
489 } else skip("Failed to create a ID3DXFont object\n");
492 /* ID3DXFont_GetGlyphData, ID3DXFont_PreloadGlyphs, ID3DXFont_PreloadCharacters */
493 hr = D3DXCreateFontA(device, 12, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
494 if(SUCCEEDED(hr)) {
495 char c;
496 HDC hdc;
497 DWORD ret;
498 HRESULT hr;
499 RECT blackbox;
500 POINT cellinc;
501 IDirect3DTexture9 *texture;
503 hdc = ID3DXFont_GetDC(font);
505 todo_wine {
506 hr = ID3DXFont_GetGlyphData(font, 0, NULL, &blackbox, &cellinc);
507 ok(hr == D3D_OK, "ID3DXFont_GetGlyphData returned %#x, expected %#x\n", hr, D3D_OK);
508 hr = ID3DXFont_GetGlyphData(font, 0, &texture, NULL, &cellinc);
509 if(SUCCEEDED(hr)) check_release((IUnknown*)texture, 1);
510 ok(hr == D3D_OK, "ID3DXFont_GetGlyphData returned %#x, expected %#x\n", hr, D3D_OK);
511 hr = ID3DXFont_GetGlyphData(font, 0, &texture, &blackbox, NULL);
512 if(SUCCEEDED(hr)) check_release((IUnknown*)texture, 1);
513 ok(hr == D3D_OK, "ID3DXFont_GetGlyphData returned %#x, expected %#x\n", hr, D3D_OK);
515 hr = ID3DXFont_PreloadCharacters(font, 'b', 'a');
516 ok(hr == D3D_OK, "ID3DXFont_PreloadCharacters returned %#x, expected %#x\n", hr, D3D_OK);
517 hr = ID3DXFont_PreloadGlyphs(font, 1, 0);
518 ok(hr == D3D_OK, "ID3DXFont_PreloadGlyphs returned %#x, expected %#x\n", hr, D3D_OK);
520 hr = ID3DXFont_PreloadCharacters(font, 'a', 'a');
521 ok(hr == D3D_OK, "ID3DXFont_PreloadCharacters returned %#x, expected %#x\n", hr, D3D_OK);
524 for(c = 'b'; c <= 'z'; c++) {
525 WORD glyph;
527 ret = GetGlyphIndicesA(hdc, &c, 1, &glyph, 0);
528 ok(ret != GDI_ERROR, "GetGlyphIndicesA failed\n");
530 hr = ID3DXFont_GetGlyphData(font, glyph, &texture, &blackbox, &cellinc);
531 todo_wine ok(hr == D3D_OK, "ID3DXFont_GetGlyphData returned %#x, expected %#x\n", hr, D3D_OK);
532 if(SUCCEEDED(hr)) {
533 DWORD levels;
534 D3DSURFACE_DESC desc;
536 levels = IDirect3DTexture9_GetLevelCount(texture);
537 ok(levels == 5, "Got levels %u, expected %u\n", levels, 5);
538 hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
539 ok(hr == D3D_OK, "IDirect3DTexture9_GetLevelDesc failed\n");
540 ok(desc.Format == D3DFMT_A8R8G8B8, "Got format %#x, expected %#x\n", desc.Format, D3DFMT_A8R8G8B8);
541 ok(desc.Usage == 0, "Got usage %#x, expected %#x\n", desc.Usage, 0);
542 ok(desc.Width == 256, "Got width %u, expected %u\n", desc.Width, 256);
543 ok(desc.Height == 256, "Got height %u, expected %u\n", desc.Height, 256);
544 ok(desc.Pool == D3DPOOL_MANAGED, "Got pool %u, expected %u\n", desc.Pool, D3DPOOL_MANAGED);
546 check_release((IUnknown*)texture, 1);
550 hr = ID3DXFont_PreloadCharacters(font, 'a', 'z');
551 todo_wine ok(hr == D3D_OK, "ID3DXFont_PreloadCharacters returned %#x, expected %#x\n", hr, D3D_OK);
553 check_release((IUnknown*)font, 0);
554 } else skip("Failed to create a ID3DXFont object\n");
556 for(i = 0; i < sizeof(texture_tests) / sizeof(texture_tests[0]); i++) {
557 HDC hdc;
558 DWORD ret;
559 HRESULT hr;
560 WORD glyph;
561 char c = 'a';
562 IDirect3DTexture9 *texture;
564 hr = D3DXCreateFontA(device, texture_tests[i].font_height, 0, FW_DONTCARE, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial", &font);
565 if(FAILED(hr)) {
566 skip("Failed to create a ID3DXFont object\n");
567 continue;
570 hdc = ID3DXFont_GetDC(font);
572 ret = GetGlyphIndicesA(hdc, &c, 1, &glyph, 0);
573 ok(ret != GDI_ERROR, "GetGlyphIndicesA failed\n");
575 hr = ID3DXFont_GetGlyphData(font, glyph, &texture, NULL, NULL);
576 todo_wine ok(hr == D3D_OK, "ID3DXFont_GetGlyphData returned %#x, expected %#x\n", hr, D3D_OK);
577 if(SUCCEEDED(hr)) {
578 DWORD levels;
579 D3DSURFACE_DESC desc;
581 levels = IDirect3DTexture9_GetLevelCount(texture);
582 ok(levels == texture_tests[i].expected_levels, "Got levels %u, expected %u\n", levels, texture_tests[i].expected_levels);
583 hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
584 ok(hr == D3D_OK, "IDirect3DTexture9_GetLevelDesc failed\n");
585 ok(desc.Format == D3DFMT_A8R8G8B8, "Got format %#x, expected %#x\n", desc.Format, D3DFMT_A8R8G8B8);
586 ok(desc.Usage == 0, "Got usage %#x, expected %#x\n", desc.Usage, 0);
587 ok(desc.Width == texture_tests[i].expected_size, "Got width %u, expected %u\n", desc.Width, texture_tests[i].expected_size);
588 ok(desc.Height == texture_tests[i].expected_size, "Got height %u, expected %u\n", desc.Height, texture_tests[i].expected_size);
589 ok(desc.Pool == D3DPOOL_MANAGED, "Got pool %u, expected %u\n", desc.Pool, D3DPOOL_MANAGED);
591 IDirect3DTexture9_Release(texture);
593 ID3DXFont_Release(font);
597 static void test_D3DXCreateRenderToSurface(IDirect3DDevice9 *device)
599 int i;
600 HRESULT hr;
601 ULONG ref_count;
602 D3DXRTS_DESC desc;
603 ID3DXRenderToSurface *render = (void *)0xdeadbeef;
604 static const D3DXRTS_DESC tests[] =
606 { 0, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN },
607 { 256, 0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN },
608 { 256, 0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_D24S8 },
609 { 256, 256, D3DFMT_UNKNOWN, FALSE, D3DFMT_R8G8B8 },
610 { 0, 0, D3DFMT_UNKNOWN, FALSE, D3DFMT_UNKNOWN },
611 { -1, -1, MAKEFOURCC('B','A','D','F'), TRUE, MAKEFOURCC('B','A','D','F') }
614 hr = D3DXCreateRenderToSurface(NULL /* device */, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
615 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
616 ok(render == (void *)0xdeadbeef, "Got %p, expected %p\n", render, (void *)0xdeadbeef);
618 hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, NULL /* out */);
619 ok(hr == D3DERR_INVALIDCALL, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
621 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
623 hr = D3DXCreateRenderToSurface(device, tests[i].Width, tests[i].Height, tests[i].Format, tests[i].DepthStencil,
624 tests[i].DepthStencilFormat, &render);
625 ok(hr == D3D_OK, "%d: D3DXCreateRenderToSurface returned %#x, expected %#x\n", i, hr, D3D_OK);
626 if (SUCCEEDED(hr))
628 hr = ID3DXRenderToSurface_GetDesc(render, &desc);
629 ok(hr == D3D_OK, "%d: GetDesc failed %#x\n", i, hr);
630 if (SUCCEEDED(hr))
632 ok(desc.Width == tests[i].Width, "%d: Got width %u, expected %u\n", i, desc.Width, tests[i].Width);
633 ok(desc.Height == tests[i].Height, "%d: Got height %u, expected %u\n", i, desc.Height, tests[i].Height);
634 ok(desc.Format == tests[i].Format, "%d: Got format %#x, expected %#x\n", i, desc.Format, tests[i].Format);
635 ok(desc.DepthStencil == tests[i].DepthStencil, "%d: Got depth stencil %d, expected %d\n",
636 i, desc.DepthStencil, tests[i].DepthStencil);
637 ok(desc.DepthStencilFormat == tests[i].DepthStencilFormat, "%d: Got depth stencil format %#x, expected %#x\n",
638 i, desc.DepthStencilFormat, tests[i].DepthStencilFormat);
640 ID3DXRenderToSurface_Release(render);
644 /* check device ref count */
645 ref_count = get_ref((IUnknown *)device);
646 hr = D3DXCreateRenderToSurface(device, 0, 0, D3DFMT_UNKNOWN, FALSE, D3DFMT_UNKNOWN, &render);
647 check_ref((IUnknown *)device, ref_count + 1);
648 if (SUCCEEDED(hr)) ID3DXRenderToSurface_Release(render);
651 /* runs a set of tests for the ID3DXRenderToSurface interface created with given parameters */
652 static void check_ID3DXRenderToSurface(IDirect3DDevice9 *device, UINT width, UINT height, D3DFORMAT format,
653 BOOL depth_stencil, D3DFORMAT depth_stencil_format, BOOL render_target)
655 HRESULT hr;
656 D3DFORMAT fmt;
657 HRESULT expected_value;
658 IDirect3DSurface9 *surface;
659 ID3DXRenderToSurface *render;
660 D3DVIEWPORT9 viewport = { 0, 0, width, height, 0.0, 1.0 };
662 hr = D3DXCreateRenderToSurface(device, width, height, format, depth_stencil, depth_stencil_format, &render);
663 if (FAILED(hr))
665 skip("Failed to create ID3DXRenderToSurface\n");
666 return;
669 if (render_target)
670 hr = IDirect3DDevice9_CreateRenderTarget(device, width, height, format, D3DMULTISAMPLE_NONE, 0, FALSE, &surface, NULL);
671 else
672 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, format, D3DPOOL_DEFAULT, &surface, NULL);
673 if (FAILED(hr))
675 skip("Failed to create surface\n");
676 ID3DXRenderToSurface_Release(render);
677 return;
680 /* viewport */
681 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
682 ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
683 check_ref((IUnknown *)surface, 2);
684 if (SUCCEEDED(hr)) ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
686 /* invalid viewport */
687 viewport.Width = 2 * width;
688 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
689 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
691 viewport.X = width / 2;
692 viewport.Width = width;
693 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
694 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
696 viewport.X = width;
697 viewport.Width = width;
698 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
699 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
701 /* rendering to a part of a surface is only allowed for render target surfaces */
702 expected_value = render_target ? D3D_OK : D3DERR_INVALIDCALL;
704 viewport.X = 0;
705 viewport.Width = width / 2;
706 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
707 ok(hr == expected_value, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, expected_value);
708 if (SUCCEEDED(hr)) ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
710 viewport.X = width / 2;
711 viewport.Width = width - width / 2;
712 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
713 ok(hr == expected_value, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, expected_value);
714 if (SUCCEEDED(hr)) ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
716 check_release((IUnknown *)surface, 0);
718 /* surfaces with different sizes */
719 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width / 2, width / 2, format, D3DPOOL_DEFAULT, &surface, NULL);
720 if (FAILED(hr))
722 skip("Failed to create surface\n");
723 ID3DXRenderToSurface_Release(render);
724 return;
726 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
727 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
728 check_release((IUnknown *)surface, 0);
730 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2 * width, 2 * height, format, D3DPOOL_DEFAULT, &surface, NULL);
731 if (FAILED(hr))
733 skip("Failed to create surface\n");
734 ID3DXRenderToSurface_Release(render);
735 return;
737 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
738 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
739 viewport.X = 0;
740 viewport.Y = 0;
741 viewport.Width = width;
742 viewport.Height = height;
743 hr = ID3DXRenderToSurface_BeginScene(render, surface, &viewport);
744 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
745 check_release((IUnknown *)surface, 0);
747 /* surfaces with different formats */
748 for (fmt = D3DFMT_A8R8G8B8; fmt <= D3DFMT_X8R8G8B8; fmt++)
750 HRESULT expected_result = (fmt != format) ? D3DERR_INVALIDCALL : D3D_OK;
752 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, fmt, D3DPOOL_DEFAULT, &surface, NULL);
753 if (FAILED(hr))
755 skip("Failed to create surface\n");
756 continue;
759 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
760 ok(hr == expected_result, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, expected_result);
762 if (SUCCEEDED(hr)) ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
763 check_release((IUnknown *)surface, 0);
766 check_release((IUnknown *)render, 0);
769 struct device_state
771 IDirect3DSurface9 *render_target;
772 IDirect3DSurface9 *depth_stencil;
773 D3DVIEWPORT9 viewport;
776 static void release_device_state(struct device_state *state)
778 if (state->render_target) IDirect3DSurface9_Release(state->render_target);
779 if (state->depth_stencil) IDirect3DSurface9_Release(state->depth_stencil);
780 memset(state, 0, sizeof(*state));
783 static HRESULT retrieve_device_state(IDirect3DDevice9 *device, struct device_state *state)
785 HRESULT hr;
787 memset(state, 0, sizeof(*state));
789 hr = IDirect3DDevice9_GetRenderTarget(device, 0, &state->render_target);
790 if (FAILED(hr)) goto cleanup;
792 hr = IDirect3DDevice9_GetDepthStencilSurface(device, &state->depth_stencil);
793 if (hr == D3DERR_NOTFOUND)
794 state->depth_stencil = NULL;
795 else if (FAILED(hr))
796 goto cleanup;
798 hr = IDirect3DDevice9_GetViewport(device, &state->viewport);
799 if (SUCCEEDED(hr)) return hr;
801 cleanup:
802 release_device_state(state);
803 return hr;
806 static HRESULT apply_device_state(IDirect3DDevice9 *device, struct device_state *state)
808 HRESULT hr;
809 HRESULT status = D3D_OK;
811 hr = IDirect3DDevice9_SetRenderTarget(device, 0, state->render_target);
812 if (FAILED(hr)) status = hr;
814 hr = IDirect3DDevice9_SetDepthStencilSurface(device, state->depth_stencil);
815 if (FAILED(hr)) status = hr;
817 hr = IDirect3DDevice9_SetViewport(device, &state->viewport);
818 if (FAILED(hr)) status = hr;
820 return status;
823 static void compare_device_state(struct device_state *state1, struct device_state *state2, BOOL equal)
825 BOOL cmp;
826 const char *message = equal ? "differs" : "is the same";
828 cmp = state1->render_target == state2->render_target;
829 ok(equal ? cmp : !cmp, "Render target %s %p, %p\n", message, state1->render_target, state2->render_target);
831 cmp = state1->depth_stencil == state2->depth_stencil;
832 ok(equal ? cmp : !cmp, "Depth stencil surface %s %p, %p\n", message, state1->depth_stencil, state2->depth_stencil);
834 cmp = state1->viewport.X == state2->viewport.X && state1->viewport.Y == state2->viewport.Y
835 && state1->viewport.Width == state2->viewport.Width && state1->viewport.Height == state2->viewport.Height;
836 ok(equal ? cmp : !cmp, "Viewport %s (%u, %u, %u, %u), (%u, %u, %u, %u)\n", message,
837 state1->viewport.X, state1->viewport.Y, state1->viewport.Width, state1->viewport.Height,
838 state2->viewport.X, state2->viewport.Y, state2->viewport.Width, state2->viewport.Height);
841 static void test_ID3DXRenderToSurface_device_state(IDirect3DDevice9 *device)
843 HRESULT hr;
844 IDirect3DSurface9 *surface = NULL;
845 ID3DXRenderToSurface *render = NULL;
846 struct device_state pre_state;
847 struct device_state current_state;
848 IDirect3DSurface9 *depth_stencil_surface;
850 /* make sure there is a depth stencil surface present */
851 hr = IDirect3DDevice9_GetDepthStencilSurface(device, &depth_stencil_surface);
852 if (SUCCEEDED(hr))
854 IDirect3DSurface9_Release(depth_stencil_surface);
855 depth_stencil_surface = NULL;
857 else if (hr == D3DERR_NOTFOUND)
859 hr = IDirect3DDevice9_CreateDepthStencilSurface(device, 256, 256, D3DFMT_D24X8,
860 D3DMULTISAMPLE_NONE, 0, TRUE, &depth_stencil_surface, NULL);
861 if (SUCCEEDED(hr)) IDirect3DDevice9_SetDepthStencilSurface(device, depth_stencil_surface);
864 if (FAILED(hr))
866 skip("Failed to create depth stencil surface\n");
867 return;
870 hr = IDirect3DDevice9_CreateRenderTarget(device, 256, 256, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0,
871 FALSE, &surface, NULL);
872 if (FAILED(hr))
874 skip("Failed to create render target\n");
875 goto cleanup;
878 hr = retrieve_device_state(device, &pre_state);
880 hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24X8, &render);
881 ok(hr == D3D_OK, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3D_OK);
882 if (SUCCEEDED(hr))
884 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
885 ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
887 hr = retrieve_device_state(device, &current_state);
888 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
889 compare_device_state(&current_state, &pre_state, FALSE);
890 release_device_state(&current_state);
892 hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
893 ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
895 hr = retrieve_device_state(device, &current_state);
896 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
897 compare_device_state(&current_state, &pre_state, TRUE);
898 release_device_state(&current_state);
900 check_release((IUnknown *)render, 0);
903 hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
904 if (SUCCEEDED(hr))
906 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
907 ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
909 hr = retrieve_device_state(device, &current_state);
910 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
911 compare_device_state(&current_state, &pre_state, FALSE);
912 release_device_state(&current_state);
914 hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
915 ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
917 hr = retrieve_device_state(device, &current_state);
918 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
919 compare_device_state(&current_state, &pre_state, TRUE);
920 release_device_state(&current_state);
922 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
923 ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
925 hr = retrieve_device_state(device, &current_state);
926 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
927 compare_device_state(&current_state, &pre_state, FALSE);
928 release_device_state(&current_state);
930 /* if EndScene isn't called, the device state isn't restored */
931 hr = retrieve_device_state(device, &current_state);
932 ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
933 compare_device_state(&current_state, &pre_state, FALSE);
934 release_device_state(&current_state);
936 hr = apply_device_state(device, &pre_state);
937 ok(SUCCEEDED(hr), "Failed to restore previous device state\n");
939 check_release((IUnknown *)render, 0);
942 release_device_state(&pre_state);
944 cleanup:
945 if (depth_stencil_surface)
947 IDirect3DDevice9_SetDepthStencilSurface(device, NULL);
948 IDirect3DSurface9_Release(depth_stencil_surface);
951 if (surface) check_release((IUnknown *)surface, 0);
954 static void test_ID3DXRenderToSurface(IDirect3DDevice9 *device)
956 int i;
957 HRESULT hr;
958 ULONG ref_count;
959 IDirect3DDevice9 *out_device;
960 ID3DXRenderToSurface *render;
961 IDirect3DSurface9 *surface;
962 D3DVIEWPORT9 viewport = { 0, 0, 256, 256, 0.0, 1.0 };
963 D3DXRTS_DESC tests[] = {
964 { 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN },
965 { 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24S8 },
966 { 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24X8 },
967 { 512, 512, D3DFMT_X8R8G8B8, FALSE, D3DFMT_X8R8G8B8 },
968 { 1024, 1024, D3DFMT_X8R8G8B8, TRUE, D3DFMT_D24S8 }
971 hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
972 ok(hr == D3D_OK, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3D_OK);
973 if (FAILED(hr)) return;
975 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
976 if (SUCCEEDED(hr))
978 ID3DXRenderToSurface *render_surface;
980 /* GetDevice */
981 hr = ID3DXRenderToSurface_GetDevice(render, NULL /* device */);
982 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
984 ref_count = get_ref((IUnknown *)device);
985 hr = ID3DXRenderToSurface_GetDevice(render, &out_device);
986 ok(hr == D3D_OK, "ID3DXRenderToSurface::GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
987 check_release((IUnknown *)out_device, ref_count);
989 /* BeginScene and EndScene */
990 hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
991 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
993 hr = ID3DXRenderToSurface_BeginScene(render, NULL /* surface */, &viewport);
994 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
996 ref_count = get_ref((IUnknown *)surface);
997 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
998 ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
999 if (SUCCEEDED(hr))
1001 check_ref((IUnknown *)surface, ref_count + 1);
1003 hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
1004 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1006 hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
1007 ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
1009 check_ref((IUnknown *)surface, ref_count);
1012 /* error handling is deferred to BeginScene */
1013 hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_UNKNOWN, &render_surface);
1014 ok(hr == D3D_OK, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3D_OK);
1015 hr = ID3DXRenderToSurface_BeginScene(render_surface, surface, NULL);
1016 ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1017 check_release((IUnknown *)render_surface, 0);
1019 check_release((IUnknown *)surface, 0);
1021 else skip("Failed to create surface\n");
1023 check_release((IUnknown *)render, 0);
1025 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1027 check_ID3DXRenderToSurface(device, tests[i].Width, tests[i].Height, tests[i].Format, tests[i].DepthStencil, tests[i].DepthStencilFormat, TRUE);
1028 check_ID3DXRenderToSurface(device, tests[i].Width, tests[i].Height, tests[i].Format, tests[i].DepthStencil, tests[i].DepthStencilFormat, FALSE);
1031 test_ID3DXRenderToSurface_device_state(device);
1034 static void test_D3DXCreateRenderToEnvMap(IDirect3DDevice9 *device)
1036 int i;
1037 HRESULT hr;
1038 ULONG ref_count;
1039 D3DXRTE_DESC desc;
1040 ID3DXRenderToEnvMap *render;
1041 static const struct {
1042 D3DXRTE_DESC parameters;
1043 D3DXRTE_DESC expected_values;
1044 } tests[] = {
1045 { { 0, 0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN }, { 1, 1, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN } },
1046 { { 256, 0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN }, { 256, 9, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN } },
1047 { { 256, 4, D3DFMT_A8R8G8B8, FALSE, D3DFMT_D24S8 }, { 256, 4, D3DFMT_A8R8G8B8, FALSE, D3DFMT_D24S8 } },
1048 { { 256, 256, D3DFMT_UNKNOWN, FALSE, D3DFMT_R8G8B8 }, { 256, 9, D3DFMT_A8R8G8B8, FALSE, D3DFMT_R8G8B8 } },
1049 { { -1, -1, D3DFMT_A8R8G8B8, TRUE, D3DFMT_DXT1 }, { 256, 9, D3DFMT_A8R8G8B8, TRUE, D3DFMT_DXT1 } },
1050 { { 256, 1, D3DFMT_X8R8G8B8, TRUE, D3DFMT_UNKNOWN }, { 256, 1, D3DFMT_X8R8G8B8, TRUE, D3DFMT_UNKNOWN } }
1053 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1055 const D3DXRTE_DESC *parameters = &tests[i].parameters;
1056 const D3DXRTE_DESC *expected = &tests[i].expected_values;
1057 hr = D3DXCreateRenderToEnvMap(device, parameters->Size, parameters->MipLevels, parameters->Format,
1058 parameters->DepthStencil, parameters->DepthStencilFormat, &render);
1059 ok(hr == D3D_OK, "%d: D3DXCreateRenderToEnvMap returned %#x, expected %#x\n", i, hr, D3D_OK);
1060 if (SUCCEEDED(hr))
1062 hr = ID3DXRenderToEnvMap_GetDesc(render, &desc);
1063 ok(hr == D3D_OK, "%d: GetDesc failed %#x\n", i, hr);
1064 if (SUCCEEDED(hr))
1066 ok(desc.Size == expected->Size, "%d: Got size %u, expected %u\n", i, desc.Size, expected->Size);
1067 ok(desc.MipLevels == expected->MipLevels, "%d: Got miplevels %u, expected %u\n", i, desc.MipLevels, expected->MipLevels);
1068 ok(desc.Format == expected->Format, "%d: Got format %#x, expected %#x\n", i, desc.Format, expected->Format);
1069 ok(desc.DepthStencil == expected->DepthStencil, "%d: Got depth stencil %d, expected %d\n",
1070 i, expected->DepthStencil, expected->DepthStencil);
1071 ok(desc.DepthStencilFormat == expected->DepthStencilFormat, "%d: Got depth stencil format %#x, expected %#x\n",
1072 i, expected->DepthStencilFormat, expected->DepthStencilFormat);
1074 check_release((IUnknown *)render, 0);
1078 /* check device ref count */
1079 ref_count = get_ref((IUnknown *)device);
1080 hr = D3DXCreateRenderToEnvMap(device, 0, 0, D3DFMT_UNKNOWN, FALSE, D3DFMT_UNKNOWN, &render);
1081 check_ref((IUnknown *)device, ref_count + 1);
1082 if (SUCCEEDED(hr)) ID3DXRenderToEnvMap_Release(render);
1085 START_TEST(core)
1087 HWND wnd;
1088 IDirect3D9 *d3d;
1089 IDirect3DDevice9 *device;
1090 D3DPRESENT_PARAMETERS d3dpp;
1091 HRESULT hr;
1093 wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1094 d3d = Direct3DCreate9(D3D_SDK_VERSION);
1095 if (!wnd) {
1096 skip("Couldn't create application window\n");
1097 return;
1099 if (!d3d) {
1100 skip("Couldn't create IDirect3D9 object\n");
1101 DestroyWindow(wnd);
1102 return;
1105 ZeroMemory(&d3dpp, sizeof(d3dpp));
1106 d3dpp.Windowed = TRUE;
1107 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1108 hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1109 if(FAILED(hr)) {
1110 skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1111 IDirect3D9_Release(d3d);
1112 DestroyWindow(wnd);
1113 return;
1116 test_ID3DXBuffer();
1117 test_ID3DXSprite(device);
1118 test_ID3DXFont(device);
1119 test_D3DXCreateRenderToSurface(device);
1120 test_ID3DXRenderToSurface(device);
1121 test_D3DXCreateRenderToEnvMap(device);
1123 check_release((IUnknown*)device, 0);
1124 check_release((IUnknown*)d3d, 0);
1125 if (wnd) DestroyWindow(wnd);