d3dx9: A simple copy isn't enough when a color key is provided.
[wine] / dlls / d3dx9_36 / tests / core.c
1 /*
2  * Tests for the D3DX9 core interfaces
3  *
4  * Copyright 2009 Tony Wasserka
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include <dxerr9.h>
24 #include "d3dx9core.h"
25
26 static inline int get_ref(IUnknown *obj)
27 {
28     IUnknown_AddRef(obj);
29     return IUnknown_Release(obj);
30 }
31
32 #define check_ref(obj, exp) _check_ref(__LINE__, obj, exp)
33 static inline void _check_ref(unsigned int line, IUnknown *obj, int exp)
34 {
35     int ref = get_ref(obj);
36     ok_(__FILE__, line)(exp == ref, "Invalid refcount. Expected %d, got %d\n", exp, ref);
37 }
38
39 #define check_release(obj, exp) _check_release(__LINE__, obj, exp)
40 static inline void _check_release(unsigned int line, IUnknown *obj, int exp)
41 {
42     int ref = IUnknown_Release(obj);
43     ok_(__FILE__, line)(ref == exp, "Invalid refcount. Expected %d, got %d\n", exp, ref);
44 }
45
46 #define admitted_error 0.0001f
47 static inline void check_mat(D3DXMATRIX got, D3DXMATRIX exp)
48 {
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;
54
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]);
65 }
66
67 static void test_ID3DXBuffer(void)
68 {
69     ID3DXBuffer *buffer;
70     HRESULT hr;
71     ULONG count;
72     DWORD size;
73
74     hr = D3DXCreateBuffer(10, NULL);
75     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
76
77     hr = D3DXCreateBuffer(0, &buffer);
78     ok(hr == D3D_OK, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3D_OK);
79
80     size = ID3DXBuffer_GetBufferSize(buffer);
81     ok(!size, "GetBufferSize failed, got %u, expected %u\n", size, 0);
82
83     count = ID3DXBuffer_Release(buffer);
84     ok(!count, "ID3DBuffer has %u references left\n", count);
85
86     hr = D3DXCreateBuffer(3, &buffer);
87     ok(hr == D3D_OK, "D3DXCreateBuffer failed, got %#x, expected %#x\n", hr, D3D_OK);
88
89     size = ID3DXBuffer_GetBufferSize(buffer);
90     ok(size == 3, "GetBufferSize failed, got %u, expected %u\n", size, 3);
91
92     count = ID3DXBuffer_Release(buffer);
93     ok(!count, "ID3DBuffer has %u references left\n", count);
94 }
95
96 static void test_ID3DXSprite(IDirect3DDevice9 *device)
97 {
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;
107
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;
113
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;
117
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;
123     }
124
125     /* Test D3DXCreateSprite */
126     hr = D3DXCreateSprite(device, NULL);
127     ok (hr == D3DERR_INVALIDCALL, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
128
129     hr = D3DXCreateSprite(NULL, &sprite);
130     ok (hr == D3DERR_INVALIDCALL, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
131
132     hr = D3DXCreateSprite(device, &sprite);
133     ok (hr == D3D_OK, "D3DXCreateSprite returned %#x, expected %#x\n", hr, D3D_OK);
134
135
136     /* Test ID3DXSprite_GetDevice */
137     hr = ID3DXSprite_GetDevice(sprite, NULL);
138     ok (hr == D3DERR_INVALIDCALL, "GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
139
140     hr = ID3DXSprite_GetDevice(sprite, &cmpdev);  /* cmpdev == NULL */
141     ok (hr == D3D_OK, "GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
142
143     hr = ID3DXSprite_GetDevice(sprite, &cmpdev);  /* cmpdev != NULL */
144     ok (hr == D3D_OK, "GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
145
146     IDirect3DDevice9_Release(device);
147     IDirect3DDevice9_Release(device);
148
149
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);
159     }
160
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;
167
168     hr = ID3DXSprite_SetTransform(sprite, NULL);
169     ok (hr == D3DERR_INVALIDCALL, "SetTransform returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
170
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);
177     }
178
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);
189
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);
198     }
199     IDirect3DDevice9_BeginScene(device);
200
201     /* Test ID3DXSprite_Begin*/
202     hr = ID3DXSprite_Begin(sprite, 0);
203     ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
204
205     IDirect3DDevice9_GetTransform(device, D3DTS_WORLD, &mat);
206     D3DXMatrixIdentity(&cmpmat);
207     check_mat(mat, cmpmat);
208
209     IDirect3DDevice9_GetTransform(device, D3DTS_VIEW, &mat);
210     check_mat(mat, cmpmat);
211
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);
216
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);
220
221     hr = ID3DXSprite_End(sprite);
222     ok (hr == D3D_OK, "End returned %#x, expected %#x\n", hr, D3D_OK);
223
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);
228
229     /* Test ID3DXSprite_Draw */
230     hr = ID3DXSprite_Begin(sprite, 0);
231     ok (hr == D3D_OK, "Begin returned %#x, expected %#x\n", hr, D3D_OK);
232
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;
236
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;
240
241         texref1 = get_ref((IUnknown*)tex1);
242         texref2 = get_ref((IUnknown*)tex2);
243
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);
246
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);
259
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);
266
267         hr = ID3DXSprite_End(sprite);
268         ok (hr == D3D_OK, "End returned %#x, expected %#x\n", hr, D3D_OK);
269     }
270
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);
281
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);
291
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);
299
300     IDirect3DDevice9_EndScene(device);
301     check_release((IUnknown*)sprite, 0);
302     check_release((IUnknown*)tex2, 0);
303     check_release((IUnknown*)tex1, 0);
304 }
305
306 static void test_ID3DXFont(IDirect3DDevice9 *device)
307 {
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 }
323     };
324
325
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);
333
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);
337
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);
341
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);
345
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);
348
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);
351
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);
354
355
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);
370
371     hr = D3DXCreateFontIndirectA(NULL, &desc, &font);
372     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
373
374     hr = D3DXCreateFontIndirectA(device, NULL, &font);
375     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
376
377     hr = D3DXCreateFontIndirectA(device, &desc, NULL);
378     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateFontIndirect returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
379
380
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;
385
386         hr = ID3DXFont_GetDevice(font, NULL);
387         ok(hr == D3DERR_INVALIDCALL, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
388
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);
393
394         ID3DXFont_Release(font);
395     } else skip("Failed to create a ID3DXFont object\n");
396
397
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);
403
404         hr = ID3DXFont_GetDescA(font, &desc);
405         ok(hr == D3D_OK, "ID3DXFont_GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
406
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");
417
418         ID3DXFont_Release(font);
419     } else skip("Failed to create a ID3DXFont object\n");
420
421
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;
426
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;
432
433             ret = ID3DXFont_GetTextMetricsA(font, &metrics);
434             ok(ret, "ID3DXFont_GetTextMetricsA failed\n");
435             ret = GetTextMetricsA(hdc, &expmetrics);
436             ok(ret, "GetTextMetricsA failed\n");
437
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);
458         }
459         ID3DXFont_Release(font);
460     } else skip("Failed to create a ID3DXFont object\n");
461
462
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};
467
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);
477
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);
486         }
487
488         check_release((IUnknown*)font, 0);
489     } else skip("Failed to create a ID3DXFont object\n");
490
491
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;
502
503         hdc = ID3DXFont_GetDC(font);
504
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);
514
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);
519
520         hr = ID3DXFont_PreloadCharacters(font, 'a', 'a');
521         ok(hr == D3D_OK, "ID3DXFont_PreloadCharacters returned %#x, expected %#x\n", hr, D3D_OK);
522         }
523
524         for(c = 'b'; c <= 'z'; c++) {
525             WORD glyph;
526
527             ret = GetGlyphIndicesA(hdc, &c, 1, &glyph, 0);
528             ok(ret != GDI_ERROR, "GetGlyphIndicesA failed\n");
529
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;
535
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);
545
546                 check_release((IUnknown*)texture, 1);
547             }
548         }
549
550         hr = ID3DXFont_PreloadCharacters(font, 'a', 'z');
551         todo_wine ok(hr == D3D_OK, "ID3DXFont_PreloadCharacters returned %#x, expected %#x\n", hr, D3D_OK);
552
553         check_release((IUnknown*)font, 0);
554     } else skip("Failed to create a ID3DXFont object\n");
555
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;
563
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;
568         }
569
570         hdc = ID3DXFont_GetDC(font);
571
572         ret = GetGlyphIndicesA(hdc, &c, 1, &glyph, 0);
573         ok(ret != GDI_ERROR, "GetGlyphIndicesA failed\n");
574
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;
580
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);
590
591             IDirect3DTexture9_Release(texture);
592         }
593         ID3DXFont_Release(font);
594     }
595 }
596
597 static void test_D3DXCreateRenderToSurface(IDirect3DDevice9 *device)
598 {
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[] =
605     {
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') }
612     };
613
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);
617
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);
620
621     for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
622     {
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))
627         {
628             hr = ID3DXRenderToSurface_GetDesc(render, &desc);
629             ok(hr == D3D_OK, "%d: GetDesc failed %#x\n", i, hr);
630             if (SUCCEEDED(hr))
631             {
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);
639             }
640             ID3DXRenderToSurface_Release(render);
641         }
642     }
643
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);
649 }
650
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)
654 {
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 };
661
662     hr = D3DXCreateRenderToSurface(device, width, height, format, depth_stencil, depth_stencil_format, &render);
663     if (FAILED(hr))
664     {
665         skip("Failed to create ID3DXRenderToSurface\n");
666         return;
667     }
668
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))
674     {
675         skip("Failed to create surface\n");
676         ID3DXRenderToSurface_Release(render);
677         return;
678     }
679
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);
685
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);
690
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);
695
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);
700
701     /* rendering to a part of a surface is only allowed for render target surfaces */
702     expected_value = render_target ? D3D_OK : D3DERR_INVALIDCALL;
703
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);
709
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);
715
716     check_release((IUnknown *)surface, 0);
717
718     /* surfaces with different sizes */
719     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width / 2, width / 2, format, D3DPOOL_DEFAULT, &surface, NULL);
720     if (FAILED(hr))
721     {
722         skip("Failed to create surface\n");
723         ID3DXRenderToSurface_Release(render);
724         return;
725     }
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);
729
730     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2 * width, 2 * height, format, D3DPOOL_DEFAULT, &surface, NULL);
731     if (FAILED(hr))
732     {
733         skip("Failed to create surface\n");
734         ID3DXRenderToSurface_Release(render);
735         return;
736     }
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);
746
747     /* surfaces with different formats */
748     for (fmt = D3DFMT_A8R8G8B8; fmt <= D3DFMT_X8R8G8B8; fmt++)
749     {
750         HRESULT expected_result = (fmt != format) ? D3DERR_INVALIDCALL : D3D_OK;
751
752         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height, fmt, D3DPOOL_DEFAULT, &surface, NULL);
753         if (FAILED(hr))
754         {
755             skip("Failed to create surface\n");
756             continue;
757         }
758
759         hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
760         ok(hr == expected_result, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, expected_result);
761
762         if (SUCCEEDED(hr)) ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
763         check_release((IUnknown *)surface, 0);
764     }
765
766     check_release((IUnknown *)render, 0);
767 }
768
769 struct device_state
770 {
771     IDirect3DSurface9 *render_target;
772     IDirect3DSurface9 *depth_stencil;
773     D3DVIEWPORT9 viewport;
774 };
775
776 static void release_device_state(struct device_state *state)
777 {
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));
781 }
782
783 static HRESULT retrieve_device_state(IDirect3DDevice9 *device, struct device_state *state)
784 {
785     HRESULT hr;
786
787     memset(state, 0, sizeof(*state));
788
789     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &state->render_target);
790     if (FAILED(hr)) goto cleanup;
791
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;
797
798     hr = IDirect3DDevice9_GetViewport(device, &state->viewport);
799     if (SUCCEEDED(hr)) return hr;
800
801 cleanup:
802     release_device_state(state);
803     return hr;
804 }
805
806 static HRESULT apply_device_state(IDirect3DDevice9 *device, struct device_state *state)
807 {
808     HRESULT hr;
809     HRESULT status = D3D_OK;
810
811     hr = IDirect3DDevice9_SetRenderTarget(device, 0, state->render_target);
812     if (FAILED(hr)) status = hr;
813
814     hr = IDirect3DDevice9_SetDepthStencilSurface(device, state->depth_stencil);
815     if (FAILED(hr)) status = hr;
816
817     hr = IDirect3DDevice9_SetViewport(device, &state->viewport);
818     if (FAILED(hr)) status = hr;
819
820     return status;
821 }
822
823 static void compare_device_state(struct device_state *state1, struct device_state *state2, BOOL equal)
824 {
825     BOOL cmp;
826     const char *message = equal ? "differs" : "is the same";
827
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);
830
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);
833
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);
839 }
840
841 static void test_ID3DXRenderToSurface_device_state(IDirect3DDevice9 *device)
842 {
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;
849
850     /* make sure there is a depth stencil surface present */
851     hr = IDirect3DDevice9_GetDepthStencilSurface(device, &depth_stencil_surface);
852     if (SUCCEEDED(hr))
853     {
854         IDirect3DSurface9_Release(depth_stencil_surface);
855         depth_stencil_surface = NULL;
856     }
857     else if (hr == D3DERR_NOTFOUND)
858     {
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);
862     }
863
864     if (FAILED(hr))
865     {
866         skip("Failed to create depth stencil surface\n");
867         return;
868     }
869
870     hr = IDirect3DDevice9_CreateRenderTarget(device, 256, 256, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0,
871         FALSE, &surface, NULL);
872     if (FAILED(hr))
873     {
874         skip("Failed to create render target\n");
875         goto cleanup;
876     }
877
878     hr = retrieve_device_state(device, &pre_state);
879
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))
883     {
884         hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
885         ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
886
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);
891
892         hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
893         ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
894
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);
899
900         check_release((IUnknown *)render, 0);
901     }
902
903     hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
904     if (SUCCEEDED(hr))
905     {
906         hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
907         ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
908
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);
913
914         hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
915         ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
916
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);
921
922         hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
923         ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
924
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);
929
930         check_release((IUnknown *)render, 0);
931
932         /* if EndScene isn't called, the device state isn't restored */
933         hr = retrieve_device_state(device, &current_state);
934         ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
935         compare_device_state(&current_state, &pre_state, FALSE);
936         release_device_state(&current_state);
937
938         hr = apply_device_state(device, &pre_state);
939         ok(SUCCEEDED(hr), "Failed to restore previous device state\n");
940
941         IDirect3DDevice9_EndScene(device);
942     }
943
944     release_device_state(&pre_state);
945
946 cleanup:
947     if (depth_stencil_surface)
948     {
949         IDirect3DDevice9_SetDepthStencilSurface(device, NULL);
950         IDirect3DSurface9_Release(depth_stencil_surface);
951     }
952
953     if (surface) check_release((IUnknown *)surface, 0);
954 }
955
956 static void test_ID3DXRenderToSurface(IDirect3DDevice9 *device)
957 {
958     int i;
959     HRESULT hr;
960     ULONG ref_count;
961     IDirect3DDevice9 *out_device;
962     ID3DXRenderToSurface *render;
963     IDirect3DSurface9 *surface;
964     D3DVIEWPORT9 viewport = { 0, 0, 256, 256, 0.0, 1.0 };
965     D3DXRTS_DESC tests[] = {
966         { 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN },
967         { 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24S8 },
968         { 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24X8 },
969         { 512, 512, D3DFMT_X8R8G8B8, FALSE, D3DFMT_X8R8G8B8 },
970         { 1024, 1024, D3DFMT_X8R8G8B8, TRUE, D3DFMT_D24S8 }
971     };
972
973     hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
974     ok(hr == D3D_OK, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3D_OK);
975     if (FAILED(hr)) return;
976
977     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
978     if (SUCCEEDED(hr))
979     {
980         ID3DXRenderToSurface *render_surface;
981
982         /* GetDevice */
983         hr = ID3DXRenderToSurface_GetDevice(render, NULL /* device */);
984         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
985
986         ref_count = get_ref((IUnknown *)device);
987         hr = ID3DXRenderToSurface_GetDevice(render, &out_device);
988         ok(hr == D3D_OK, "ID3DXRenderToSurface::GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
989         check_release((IUnknown *)out_device, ref_count);
990
991         /* BeginScene and EndScene */
992         hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
993         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
994
995         hr = ID3DXRenderToSurface_BeginScene(render, NULL /* surface */, &viewport);
996         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
997
998         ref_count = get_ref((IUnknown *)surface);
999         hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
1000         ok(hr == D3D_OK, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3D_OK);
1001         if (SUCCEEDED(hr))
1002         {
1003             check_ref((IUnknown *)surface, ref_count + 1);
1004
1005             hr = ID3DXRenderToSurface_BeginScene(render, surface, NULL);
1006             ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1007
1008             hr = ID3DXRenderToSurface_EndScene(render, D3DX_FILTER_NONE);
1009             ok(hr == D3D_OK, "ID3DXRenderToSurface::EndScene returned %#x, expected %#x\n", hr, D3D_OK);
1010
1011             check_ref((IUnknown *)surface, ref_count);
1012         }
1013
1014         /* error handling is deferred to BeginScene */
1015         hr = D3DXCreateRenderToSurface(device, 256, 256, D3DFMT_A8R8G8B8, TRUE, D3DFMT_UNKNOWN, &render_surface);
1016         ok(hr == D3D_OK, "D3DXCreateRenderToSurface returned %#x, expected %#x\n", hr, D3D_OK);
1017         hr = ID3DXRenderToSurface_BeginScene(render_surface, surface, NULL);
1018         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToSurface::BeginScene returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1019         check_release((IUnknown *)render_surface, 0);
1020
1021         check_release((IUnknown *)surface, 0);
1022     }
1023     else skip("Failed to create surface\n");
1024
1025     check_release((IUnknown *)render, 0);
1026
1027     for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1028     {
1029         check_ID3DXRenderToSurface(device, tests[i].Width, tests[i].Height, tests[i].Format, tests[i].DepthStencil, tests[i].DepthStencilFormat, TRUE);
1030         check_ID3DXRenderToSurface(device, tests[i].Width, tests[i].Height, tests[i].Format, tests[i].DepthStencil, tests[i].DepthStencilFormat, FALSE);
1031     }
1032
1033     test_ID3DXRenderToSurface_device_state(device);
1034 }
1035
1036 static void test_D3DXCreateRenderToEnvMap(IDirect3DDevice9 *device)
1037 {
1038     int i;
1039     HRESULT hr;
1040     ULONG ref_count;
1041     D3DXRTE_DESC desc;
1042     ID3DXRenderToEnvMap *render;
1043     static const struct {
1044         D3DXRTE_DESC parameters;
1045         D3DXRTE_DESC expected_values;
1046     } tests[] = {
1047         { {   0,   0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN }, {   1, 1, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN } },
1048         { { 256,   0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN }, { 256, 9, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN } },
1049         { { 256,   4, D3DFMT_A8R8G8B8, FALSE, D3DFMT_D24S8   }, { 256, 4, D3DFMT_A8R8G8B8, FALSE, D3DFMT_D24S8   } },
1050         { { 256, 256, D3DFMT_UNKNOWN,  FALSE, D3DFMT_R8G8B8  }, { 256, 9, D3DFMT_A8R8G8B8, FALSE, D3DFMT_R8G8B8  } },
1051         { {  -1,  -1, D3DFMT_A8R8G8B8, TRUE,  D3DFMT_DXT1    }, { 256, 9, D3DFMT_A8R8G8B8, TRUE,  D3DFMT_DXT1    } },
1052         { { 256,   1, D3DFMT_X8R8G8B8, TRUE,  D3DFMT_UNKNOWN }, { 256, 1, D3DFMT_X8R8G8B8, TRUE,  D3DFMT_UNKNOWN } }
1053     };
1054
1055     for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1056     {
1057         const D3DXRTE_DESC *parameters = &tests[i].parameters;
1058         const D3DXRTE_DESC *expected  = &tests[i].expected_values;
1059         hr = D3DXCreateRenderToEnvMap(device, parameters->Size, parameters->MipLevels, parameters->Format,
1060                 parameters->DepthStencil, parameters->DepthStencilFormat, &render);
1061         ok(hr == D3D_OK, "%d: D3DXCreateRenderToEnvMap returned %#x, expected %#x\n", i, hr, D3D_OK);
1062         if (SUCCEEDED(hr))
1063         {
1064             hr = ID3DXRenderToEnvMap_GetDesc(render, &desc);
1065             ok(hr == D3D_OK, "%d: GetDesc failed %#x\n", i, hr);
1066             if (SUCCEEDED(hr))
1067             {
1068                 ok(desc.Size == expected->Size, "%d: Got size %u, expected %u\n", i, desc.Size, expected->Size);
1069                 ok(desc.MipLevels == expected->MipLevels, "%d: Got miplevels %u, expected %u\n", i, desc.MipLevels, expected->MipLevels);
1070                 ok(desc.Format == expected->Format, "%d: Got format %#x, expected %#x\n", i, desc.Format, expected->Format);
1071                 ok(desc.DepthStencil == expected->DepthStencil, "%d: Got depth stencil %d, expected %d\n",
1072                         i, expected->DepthStencil, expected->DepthStencil);
1073                 ok(desc.DepthStencilFormat == expected->DepthStencilFormat, "%d: Got depth stencil format %#x, expected %#x\n",
1074                         i, expected->DepthStencilFormat, expected->DepthStencilFormat);
1075             }
1076             check_release((IUnknown *)render, 0);
1077         }
1078     }
1079
1080     /* check device ref count */
1081     ref_count = get_ref((IUnknown *)device);
1082     hr = D3DXCreateRenderToEnvMap(device, 0, 0, D3DFMT_UNKNOWN, FALSE, D3DFMT_UNKNOWN, &render);
1083     check_ref((IUnknown *)device, ref_count + 1);
1084     if (SUCCEEDED(hr)) ID3DXRenderToEnvMap_Release(render);
1085 }
1086
1087 static void test_ID3DXRenderToEnvMap_cube_map(IDirect3DDevice9 *device)
1088 {
1089     HRESULT hr;
1090     IDirect3DCubeTexture9 *cube_texture = NULL;
1091     ID3DXRenderToEnvMap *render = NULL;
1092     struct device_state pre_state;
1093     struct device_state current_state;
1094
1095     hr = IDirect3DDevice9_CreateCubeTexture(device, 256, 0, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
1096         &cube_texture, NULL);
1097     if (FAILED(hr))
1098     {
1099         skip("Failed to create cube texture\n");
1100         return;
1101     }
1102
1103     hr = retrieve_device_state(device, &pre_state);
1104
1105     hr = D3DXCreateRenderToEnvMap(device, 256, 0, D3DFMT_A8R8G8B8, TRUE, D3DFMT_D24X8, &render);
1106     ok(hr == D3D_OK, "D3DCreateRenderToEnvMap returned %#x, expected %#x\n", hr, D3D_OK);
1107     if (SUCCEEDED(hr))
1108     {
1109         DWORD face;
1110
1111         hr = ID3DXRenderToEnvMap_End(render, D3DX_FILTER_NONE);
1112         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::End returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1113
1114         hr = ID3DXRenderToEnvMap_BeginCube(render, cube_texture);
1115         ok(hr == D3D_OK, "ID3DXRenderToEnvMap::BeginCube returned %#x, expected %#x\n", hr, D3D_OK);
1116
1117         hr = retrieve_device_state(device, &current_state);
1118         ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
1119         compare_device_state(&current_state, &pre_state, TRUE);
1120         release_device_state(&current_state);
1121
1122         for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
1123         {
1124             hr = ID3DXRenderToEnvMap_Face(render, face, D3DX_FILTER_POINT);
1125             ok(hr == D3D_OK, "ID3DXRenderToEnvMap::Face returned %#x, expected %#x\n", hr, D3D_OK);
1126
1127             hr = retrieve_device_state(device, &current_state);
1128             ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
1129             compare_device_state(&current_state, &pre_state, FALSE);
1130             release_device_state(&current_state);
1131         }
1132
1133         hr = ID3DXRenderToEnvMap_End(render, D3DX_FILTER_POINT);
1134         ok(hr == D3D_OK, "ID3DXRenderToEnvMap::End returned %#x, expected %#x\n", hr, D3D_OK);
1135
1136         hr = retrieve_device_state(device, &current_state);
1137         ok(SUCCEEDED(hr), "Failed to retrieve device state\n");
1138         compare_device_state(&current_state, &pre_state, TRUE);
1139         release_device_state(&current_state);
1140
1141         check_release((IUnknown *)render, 0);
1142     }
1143
1144     release_device_state(&pre_state);
1145
1146     check_release((IUnknown *)cube_texture, 0);
1147 }
1148
1149 static void test_ID3DXRenderToEnvMap(IDirect3DDevice9 *device)
1150 {
1151     HRESULT hr;
1152     ID3DXRenderToEnvMap *render;
1153     IDirect3DSurface9 *depth_stencil_surface;
1154
1155     hr = D3DXCreateRenderToEnvMap(device, 256, 0, D3DFMT_A8R8G8B8, FALSE, D3DFMT_UNKNOWN, &render);
1156     if (SUCCEEDED(hr))
1157     {
1158         ULONG ref_count;
1159         IDirect3DDevice9 *out_device;
1160
1161         hr = ID3DXRenderToEnvMap_GetDesc(render, NULL);
1162         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::GetDesc returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1163
1164         hr = ID3DXRenderToEnvMap_GetDevice(render, NULL);
1165         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::GetDevice returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1166
1167         ref_count = get_ref((IUnknown *)device);
1168         hr = ID3DXRenderToEnvMap_GetDevice(render, &out_device);
1169         ok(hr == D3D_OK, "ID3DXRenderToEnvMap::GetDevice returned %#x, expected %#x\n", hr, D3D_OK);
1170         ok(out_device == device, "ID3DXRenderToEnvMap::GetDevice returned different device\n");
1171         check_release((IUnknown *)device, ref_count);
1172
1173         hr = ID3DXRenderToEnvMap_End(render, D3DX_FILTER_NONE);
1174         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::End returned %#x, expected %#x\n", hr, D3D_OK);
1175
1176         hr = ID3DXRenderToEnvMap_BeginCube(render, NULL);
1177         ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::BeginCube returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1178
1179         hr = ID3DXRenderToEnvMap_BeginHemisphere(render, NULL, NULL);
1180         todo_wine ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::BeginHemisphere returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1181
1182         hr = ID3DXRenderToEnvMap_BeginParabolic(render, NULL, NULL);
1183         todo_wine ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::BeginParabolic returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1184
1185         hr = ID3DXRenderToEnvMap_BeginSphere(render, NULL);
1186         todo_wine ok(hr == D3DERR_INVALIDCALL, "ID3DXRenderToEnvMap::BeginSphere returned %#x, exected %#x\n", hr, D3DERR_INVALIDCALL);
1187
1188         check_release((IUnknown *)render, 0);
1189     } else skip("Failed to create ID3DXRenderToEnvMap\n");
1190
1191     /* make sure there is a depth stencil surface present */
1192     hr = IDirect3DDevice9_GetDepthStencilSurface(device, &depth_stencil_surface);
1193     if (SUCCEEDED(hr))
1194     {
1195         IDirect3DSurface9_Release(depth_stencil_surface);
1196         depth_stencil_surface = NULL;
1197     }
1198     else if (hr == D3DERR_NOTFOUND)
1199     {
1200         hr = IDirect3DDevice9_CreateDepthStencilSurface(device, 256, 256, D3DFMT_D24X8,
1201                 D3DMULTISAMPLE_NONE, 0, TRUE, &depth_stencil_surface, NULL);
1202         if (SUCCEEDED(hr)) IDirect3DDevice9_SetDepthStencilSurface(device, depth_stencil_surface);
1203     }
1204
1205     if (FAILED(hr))
1206     {
1207         skip("Failed to create depth stencil surface\n");
1208         return;
1209     }
1210
1211     test_ID3DXRenderToEnvMap_cube_map(device);
1212
1213     if (depth_stencil_surface)
1214     {
1215         IDirect3DDevice9_SetDepthStencilSurface(device, NULL);
1216         IDirect3DSurface9_Release(depth_stencil_surface);
1217     }
1218 }
1219
1220 START_TEST(core)
1221 {
1222     HWND wnd;
1223     IDirect3D9 *d3d;
1224     IDirect3DDevice9 *device;
1225     D3DPRESENT_PARAMETERS d3dpp;
1226     HRESULT hr;
1227
1228     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1229     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1230     if (!wnd) {
1231         skip("Couldn't create application window\n");
1232         return;
1233     }
1234     if (!d3d) {
1235         skip("Couldn't create IDirect3D9 object\n");
1236         DestroyWindow(wnd);
1237         return;
1238     }
1239
1240     ZeroMemory(&d3dpp, sizeof(d3dpp));
1241     d3dpp.Windowed   = TRUE;
1242     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1243     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1244     if(FAILED(hr)) {
1245         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1246         IDirect3D9_Release(d3d);
1247         DestroyWindow(wnd);
1248         return;
1249     }
1250
1251     test_ID3DXBuffer();
1252     test_ID3DXSprite(device);
1253     test_ID3DXFont(device);
1254     test_D3DXCreateRenderToSurface(device);
1255     test_ID3DXRenderToSurface(device);
1256     test_D3DXCreateRenderToEnvMap(device);
1257     test_ID3DXRenderToEnvMap(device);
1258
1259     check_release((IUnknown*)device, 0);
1260     check_release((IUnknown*)d3d, 0);
1261     if (wnd) DestroyWindow(wnd);
1262 }