d3dx9/tests: Avoid using FP_NAN for portability.
[wine] / dlls / d3dx9_36 / tests / texture.c
1 /*
2  * Tests for the D3DX9 texture functions
3  *
4  * Copyright 2009 Tony Wasserka
5  * Copyright 2010 Owen Rudge for CodeWeavers
6  * Copyright 2010 Matteo Bruni for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #define COBJMACROS
24 #include "wine/test.h"
25 #include "d3dx9tex.h"
26 #include "resources.h"
27
28 static void test_D3DXCheckTextureRequirements(IDirect3DDevice9 *device)
29 {
30     UINT width, height, mipmaps;
31     D3DFORMAT format, expected;
32     D3DCAPS9 caps;
33     HRESULT hr;
34     IDirect3D9 *d3d;
35     D3DDEVICE_CREATION_PARAMETERS params;
36     D3DDISPLAYMODE mode;
37
38     IDirect3DDevice9_GetDeviceCaps(device, &caps);
39
40     /* general tests */
41     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
42     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
43
44     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
45     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
46
47     hr = D3DXCheckTextureRequirements(NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
48     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
49
50     /* width & height */
51     width = height = D3DX_DEFAULT;
52     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
53     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
54     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
55     ok(height == 256, "Returned height %d, expected %d\n", height, 256);
56
57     width = D3DX_DEFAULT;
58     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
59     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
60     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
61
62     if (caps.TextureCaps & D3DPTEXTURECAPS_POW2)
63         skip("Hardware only supports pow2 textures\n");
64     else
65     {
66         width = 62;
67         hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
68         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
69         ok(width == 62, "Returned width %d, expected %d\n", width, 62);
70
71         width = D3DX_DEFAULT; height = 63;
72         hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
73         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
74         ok(width == height, "Returned width %d, expected %d\n", width, height);
75         ok(height == 63, "Returned height %d, expected %d\n", height, 63);
76     }
77
78     width = D3DX_DEFAULT; height = 0;
79     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
80     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
81     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
82     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
83
84     width = 0; height = 0;
85     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
86     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
87     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
88     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
89
90     width = 0;
91     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
92     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
93     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
94
95     width = 0xFFFFFFFE;
96     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
97     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
98     ok(width == caps.MaxTextureWidth, "Returned width %d, expected %d\n", width, caps.MaxTextureWidth);
99
100     width = caps.MaxTextureWidth-1;
101     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
102     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
103     ok(width == caps.MaxTextureWidth-1, "Returned width %d, expected %d\n", width, caps.MaxTextureWidth-1);
104
105     /* mipmaps */
106     width = 64; height = 63;
107     mipmaps = 9;
108     hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
109     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
110     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
111
112     if (!(caps.TextureCaps & D3DPTEXTURECAPS_POW2))
113     {
114         width = 284; height = 137;
115         mipmaps = 20;
116         hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
117         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
118         ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
119
120         width = height = 63;
121         mipmaps = 9;
122         hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
123         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
124         ok(mipmaps == 6, "Returned mipmaps %d, expected %d\n", mipmaps, 6);
125     }
126     else
127         skip("Skipping some tests, npot2 textures unsupported\n");
128
129     mipmaps = 20;
130     hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
131     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
132     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
133
134     mipmaps = 0;
135     hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
136     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
137     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
138
139     /* usage */
140     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT);
141     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
142     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_DONOTCLIP, NULL, D3DPOOL_DEFAULT);
143     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
144     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_POINTS, NULL, D3DPOOL_DEFAULT);
145     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
146     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_RTPATCHES, NULL, D3DPOOL_DEFAULT);
147     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
148     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_NPATCHES, NULL, D3DPOOL_DEFAULT);
149     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
150
151     /* format */
152     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
153     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
154
155     format = D3DFMT_UNKNOWN;
156     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
157     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
158     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
159
160     format = D3DX_DEFAULT;
161     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
162     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
163     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
164
165     format = D3DFMT_R8G8B8;
166     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
167     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
168     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
169
170     IDirect3DDevice9_GetDirect3D(device, &d3d);
171     IDirect3DDevice9_GetCreationParameters(device, &params);
172     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
173
174     if (SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
175                                                mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_R3G3B2)))
176         expected = D3DFMT_R3G3B2;
177     else
178     {
179         if (SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
180                                                    mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_X4R4G4B4)))
181             expected = D3DFMT_X4R4G4B4;
182         else
183             expected = D3DFMT_X1R5G5B5;
184     }
185
186     format = D3DFMT_R3G3B2;
187     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
188     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
189     ok(format == expected, "Returned format %u, expected %u\n", format, expected);
190
191     if(SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
192                                               mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R3G3B2)))
193         expected = D3DFMT_A8R3G3B2;
194     else
195         expected = D3DFMT_A8R8G8B8;
196
197     format = D3DFMT_A8R3G3B2;
198     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
199     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
200     ok(format == expected, "Returned format %u, expected %u\n", format, expected);
201
202     IDirect3D9_Release(d3d);
203 }
204
205 static void test_D3DXCheckCubeTextureRequirements(IDirect3DDevice9 *device)
206 {
207     UINT size, mipmaps, expected;
208     D3DFORMAT format;
209     D3DCAPS9 caps;
210     HRESULT hr;
211
212     IDirect3DDevice9_GetDeviceCaps(device, &caps);
213
214     if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
215     {
216         skip("No cube textures support\n");
217         return;
218     }
219
220     /* general tests */
221     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
222     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
223
224     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
225     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
226
227     hr = D3DXCheckCubeTextureRequirements(NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
228     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
229
230     /* size */
231     size = D3DX_DEFAULT;
232     hr = D3DXCheckCubeTextureRequirements(device, &size, NULL, 0, NULL, D3DPOOL_DEFAULT);
233     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
234     ok(size == 256, "Returned size %d, expected %d\n", size, 256);
235
236     /* mipmaps */
237     size = 64;
238     mipmaps = 9;
239     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
240     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
241     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
242
243     size = 284;
244     mipmaps = 20;
245     expected = caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2 ? 10 : 9;
246     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
247     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
248     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
249
250     size = 63;
251     mipmaps = 9;
252     expected = caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2 ? 7 : 6;
253     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
254     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
255     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
256
257     mipmaps = 0;
258     hr = D3DXCheckCubeTextureRequirements(device, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
259     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
260     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
261
262     /* usage */
263     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT);
264     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
265     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_DONOTCLIP, NULL, D3DPOOL_DEFAULT);
266     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
267     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_POINTS, NULL, D3DPOOL_DEFAULT);
268     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
269     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_RTPATCHES, NULL, D3DPOOL_DEFAULT);
270     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
271     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_NPATCHES, NULL, D3DPOOL_DEFAULT);
272     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
273
274     /* format */
275     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
276     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
277
278     format = D3DFMT_UNKNOWN;
279     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
280     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
281     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
282
283     format = D3DX_DEFAULT;
284     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
285     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
286     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
287
288     format = D3DFMT_R8G8B8;
289     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
290     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
291     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
292 }
293
294 static void test_D3DXCheckVolumeTextureRequirements(IDirect3DDevice9 *device)
295 {
296     UINT width, height, depth, mipmaps, expected;
297     D3DFORMAT format;
298     D3DCAPS9 caps;
299     HRESULT hr;
300
301     IDirect3DDevice9_GetDeviceCaps(device, &caps);
302
303     if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
304     {
305         skip("No volume textures support\n");
306         return;
307     }
308
309     /* general tests */
310     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
311     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
312
313     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
314     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
315
316     hr = D3DXCheckVolumeTextureRequirements(NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
317     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
318
319     /* width, height, depth */
320     width = height = depth = D3DX_DEFAULT;
321     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
322     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
323     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
324     ok(height == 256, "Returned height %d, expected %d\n", height, 256);
325     ok(depth == 1, "Returned depth %d, expected %d\n", depth, 1);
326
327     width = D3DX_DEFAULT;
328     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
329     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
330     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
331
332     width = D3DX_DEFAULT; height = 0; depth = 0;
333     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
334     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
335     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
336     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
337     ok(depth == 1, "Returned height %d, expected %d\n", depth, 1);
338
339     width = 0; height = 0; depth = 0;
340     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
341     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
342     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
343     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
344     ok(depth == 1, "Returned height %d, expected %d\n", depth, 1);
345
346     width = 0;
347     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
348     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
349     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
350
351     width = 0xFFFFFFFE;
352     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
353     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
354     ok(width == caps.MaxVolumeExtent, "Returned width %d, expected %d\n", width, caps.MaxVolumeExtent);
355
356     /* format */
357     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
358     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
359
360     format = D3DFMT_UNKNOWN;
361     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
362     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
363     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
364
365     format = D3DX_DEFAULT;
366     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
367     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
368     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
369
370     format = D3DFMT_R8G8B8;
371     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
372     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
373     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
374
375     /* mipmaps */
376     if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
377     {
378         skip("No volume textures mipmapping support\n");
379         return;
380     }
381
382     width = height = depth = 64;
383     mipmaps = 9;
384     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
385     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
386     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
387
388     width = 284;
389     height = 143;
390     depth = 55;
391     mipmaps = 20;
392     expected = caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2 ? 10 : 9;
393     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
394     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
395     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
396
397     mipmaps = 0;
398     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
399     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
400     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
401 }
402
403 static void test_D3DXCreateTexture(IDirect3DDevice9 *device)
404 {
405     IDirect3DTexture9 *texture;
406     D3DSURFACE_DESC desc;
407     D3DCAPS9 caps;
408     UINT mipmaps;
409     HRESULT hr;
410
411     IDirect3DDevice9_GetDeviceCaps(device, &caps);
412
413     hr = D3DXCreateTexture(NULL, 0, 0, 0, 0, D3DX_DEFAULT, 0, D3DPOOL_DEFAULT);
414     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
415
416     /* width and height tests */
417
418     hr = D3DXCreateTexture(device, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, D3DPOOL_DEFAULT, &texture);
419     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
420
421     if (texture)
422     {
423         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
424         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
425         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
426
427         ok(desc.Width == 256, "Returned width %d, expected %d\n", desc.Width, 256);
428         ok(desc.Height == 256, "Returned height %d, expected %d\n", desc.Height, 256);
429
430         IDirect3DTexture9_Release(texture);
431     }
432
433
434     hr = D3DXCreateTexture(device, 0, 0, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
435     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
436
437     if (texture)
438     {
439         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
440         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
441         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
442
443         ok(desc.Width == 1, "Returned width %d, expected %d\n", desc.Width, 1);
444         ok(desc.Height == 1, "Returned height %d, expected %d\n", desc.Height, 1);
445
446         IDirect3DTexture9_Release(texture);
447     }
448
449
450     if (caps.TextureCaps & D3DPTEXTURECAPS_POW2)
451         skip("Hardware only supports pow2 textures\n");
452     else
453     {
454         hr = D3DXCreateTexture(device, D3DX_DEFAULT, 63, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
455         ok((hr == D3D_OK) ||
456            /* may not work with conditional NPOT */
457            ((hr != D3D_OK) && (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)),
458            "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
459
460         if (texture)
461         {
462             hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
463             ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
464             ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
465
466             /* Conditional NPOT may create a texture with different dimensions, so allow those
467                situations instead of returning a fail */
468
469             ok(desc.Width == 63 ||
470                (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL),
471                "Returned width %d, expected %d\n", desc.Width, 63);
472
473             ok(desc.Height == 63 ||
474                (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL),
475                "Returned height %d, expected %d\n", desc.Height, 63);
476
477             IDirect3DTexture9_Release(texture);
478         }
479     }
480
481     /* mipmaps */
482
483     hr = D3DXCreateTexture(device, 64, 63, 9, 0, 0, D3DPOOL_DEFAULT, &texture);
484     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
485
486     if (texture)
487     {
488         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
489         ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
490
491         IDirect3DTexture9_Release(texture);
492     }
493
494
495     hr = D3DXCreateTexture(device, 284, 137, 9, 0, 0, D3DPOOL_DEFAULT, &texture);
496     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
497
498     if (texture)
499     {
500         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
501         ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
502
503         IDirect3DTexture9_Release(texture);
504     }
505
506
507     hr = D3DXCreateTexture(device, 0, 0, 20, 0, 0, D3DPOOL_DEFAULT, &texture);
508     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
509
510     if (texture)
511     {
512         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
513         ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
514
515         IDirect3DTexture9_Release(texture);
516     }
517
518
519     hr = D3DXCreateTexture(device, 64, 64, 1, 0, 0, D3DPOOL_DEFAULT, &texture);
520     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
521
522     if (texture)
523     {
524         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
525         ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
526
527         IDirect3DTexture9_Release(texture);
528     }
529
530     /* usage */
531
532     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &texture);
533     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
534     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_DONOTCLIP, 0, D3DPOOL_DEFAULT, &texture);
535     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
536     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_POINTS, 0, D3DPOOL_DEFAULT, &texture);
537     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
538     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_RTPATCHES, 0, D3DPOOL_DEFAULT, &texture);
539     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
540     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_NPATCHES, 0, D3DPOOL_DEFAULT, &texture);
541     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
542
543     /* format */
544
545     hr = D3DXCreateTexture(device, 0, 0, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, &texture);
546     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
547
548     if (texture)
549     {
550         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
551         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
552         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
553
554         IDirect3DTexture9_Release(texture);
555     }
556
557
558     hr = D3DXCreateTexture(device, 0, 0, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
559     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
560
561     if (texture)
562     {
563         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
564         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
565         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
566
567         IDirect3DTexture9_Release(texture);
568     }
569
570     /* D3DXCreateTextureFromResource */
571     todo_wine {
572         hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &texture);
573         ok(hr == D3D_OK, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3D_OK);
574         if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
575     }
576     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &texture);
577     ok(hr == D3D_OK, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3D_OK);
578     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
579
580     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDS_STRING), &texture);
581     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
582
583     hr = D3DXCreateTextureFromResourceA(NULL, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &texture);
584     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
585
586     hr = D3DXCreateTextureFromResourceA(device, NULL, NULL, &texture);
587     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
588
589     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), NULL);
590     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
591
592
593     /* D3DXCreateTextureFromResourceEx */
594     hr = D3DXCreateTextureFromResourceExA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
595     ok(hr == D3D_OK, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3D_OK);
596     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
597
598     hr = D3DXCreateTextureFromResourceExA(device, NULL, MAKEINTRESOURCEA(IDS_STRING), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
599     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
600
601     hr = D3DXCreateTextureFromResourceExA(NULL, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
602     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
603
604     hr = D3DXCreateTextureFromResourceExA(device, NULL, NULL, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
605     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
606
607     hr = D3DXCreateTextureFromResourceExA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, NULL);
608     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
609 }
610
611 static void test_D3DXFilterTexture(IDirect3DDevice9 *device)
612 {
613     IDirect3DTexture9 *tex;
614     IDirect3DCubeTexture9 *cubetex;
615     HRESULT hr;
616
617     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 5, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &tex, NULL);
618
619     if (SUCCEEDED(hr))
620     {
621         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
622         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
623
624         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_BOX + 1); /* Invalid filter */
625         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
626
627         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 5, D3DX_FILTER_NONE); /* Invalid miplevel */
628         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
629     }
630     else
631         skip("Failed to create texture\n");
632
633     IDirect3DTexture9_Release(tex);
634
635     hr = D3DXFilterTexture(NULL, NULL, 0, D3DX_FILTER_NONE);
636     ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
637
638     /* Test different pools */
639     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &tex, NULL);
640
641     if (SUCCEEDED(hr))
642     {
643         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
644         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
645         IDirect3DTexture9_Release(tex);
646     }
647     else
648         skip("Failed to create texture\n");
649
650     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &tex, NULL);
651
652     if (SUCCEEDED(hr))
653     {
654         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
655         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
656         IDirect3DTexture9_Release(tex);
657     }
658     else
659         skip("Failed to create texture\n");
660
661     /* Cube texture test */
662     hr = IDirect3DDevice9_CreateCubeTexture(device, 256, 5, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &cubetex, NULL);
663
664     if (SUCCEEDED(hr))
665     {
666         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 0, D3DX_FILTER_NONE);
667         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
668
669         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 0, D3DX_FILTER_BOX + 1); /* Invalid filter */
670         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
671
672         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 5, D3DX_FILTER_NONE); /* Invalid miplevel */
673         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
674     }
675     else
676         skip("Failed to create texture\n");
677
678     IDirect3DCubeTexture9_Release(cubetex);
679 }
680
681 static BOOL color_match(const DWORD *value, const DWORD *expected)
682 {
683     int i;
684
685     for (i = 0; i < 4; i++)
686     {
687         DWORD diff = value[i] > expected[i] ? value[i] - expected[i] : expected[i] - value[i];
688         if (diff > 1) return FALSE;
689     }
690     return TRUE;
691 }
692
693 static void WINAPI fillfunc(D3DXVECTOR4 *value, const D3DXVECTOR2 *texcoord,
694                             const D3DXVECTOR2 *texelsize, void *data)
695 {
696     value->x = texcoord->x;
697     value->y = texcoord->y;
698     value->z = texelsize->x;
699     value->w = 1.0f;
700 }
701
702 static void test_D3DXFillTexture(IDirect3DDevice9 *device)
703 {
704     IDirect3DTexture9 *tex;
705     HRESULT hr;
706     D3DLOCKED_RECT lock_rect;
707     DWORD x, y, m;
708     DWORD v[4], e[4];
709     DWORD value, expected, size, pitch;
710
711     size = 4;
712     hr = IDirect3DDevice9_CreateTexture(device, size, size, 0, 0, D3DFMT_A8R8G8B8,
713                                         D3DPOOL_MANAGED, &tex, NULL);
714
715     if (SUCCEEDED(hr))
716     {
717         hr = D3DXFillTexture(tex, fillfunc, NULL);
718         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
719
720         for (m = 0; m < 3; m++)
721         {
722             hr = IDirect3DTexture9_LockRect(tex, m, &lock_rect, NULL, D3DLOCK_READONLY);
723             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
724             if (SUCCEEDED(hr))
725             {
726                 pitch = lock_rect.Pitch / sizeof(DWORD);
727                 for (y = 0; y < size; y++)
728                 {
729                     for (x = 0; x < size; x++)
730                     {
731                         value = ((DWORD *)lock_rect.pBits)[y * pitch + x];
732                         v[0] = (value >> 24) & 0xff;
733                         v[1] = (value >> 16) & 0xff;
734                         v[2] = (value >> 8) & 0xff;
735                         v[3] = value & 0xff;
736
737                         e[0] = 0xff;
738                         e[1] = (x + 0.5f) / size * 255.0f + 0.5f;
739                         e[2] = (y + 0.5f) / size * 255.0f + 0.5f;
740                         e[3] = 255.0f / size + 0.5f;
741                         expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
742
743                         ok(color_match(v, e),
744                            "Texel at (%u, %u) doesn't match: %#x, expected %#x\n",
745                            x, y, value, expected);
746                     }
747                 }
748                 IDirect3DTexture9_UnlockRect(tex, m);
749             }
750             size >>= 1;
751         }
752     }
753     else
754         skip("Failed to create texture\n");
755
756     IDirect3DTexture9_Release(tex);
757
758     hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_A1R5G5B5,
759                                         D3DPOOL_MANAGED, &tex, NULL);
760
761     if (SUCCEEDED(hr))
762     {
763         hr = D3DXFillTexture(tex, fillfunc, NULL);
764         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
765
766         hr = IDirect3DTexture9_LockRect(tex, 0, &lock_rect, NULL, D3DLOCK_READONLY);
767         ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
768         if (SUCCEEDED(hr))
769         {
770             pitch = lock_rect.Pitch / sizeof(WORD);
771             for (y = 0; y < 4; y++)
772             {
773                 for (x = 0; x < 4; x++)
774                 {
775                     value = ((WORD *)lock_rect.pBits)[y * pitch + x];
776                     v[0] = value >> 15;
777                     v[1] = value >> 10 & 0x1f;
778                     v[2] = value >> 5 & 0x1f;
779                     v[3] = value & 0x1f;
780
781                     e[0] = 1;
782                     e[1] = (x + 0.5f) / 4.0f * 31.0f + 0.5f;
783                     e[2] = (y + 0.5f) / 4.0f * 31.0f + 0.5f;
784                     e[3] = 8;
785                     expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
786
787                     ok(color_match(v, e),
788                        "Texel at (%u, %u) doesn't match: %#x, expected %#x\n",
789                        x, y, value, expected);
790                 }
791             }
792             IDirect3DTexture9_UnlockRect(tex, 0);
793         }
794     }
795     else
796         skip("Failed to create texture\n");
797
798     IDirect3DTexture9_Release(tex);
799 }
800
801 static void WINAPI fillfunc_cube(D3DXVECTOR4 *value, const D3DXVECTOR3 *texcoord,
802                                  const D3DXVECTOR3 *texelsize, void *data)
803 {
804     value->x = (texcoord->x + 1.0f) / 2.0f;
805     value->y = (texcoord->y + 1.0f) / 2.0f;
806     value->z = (texcoord->z + 1.0f) / 2.0f;
807     value->w = texelsize->x;
808 }
809
810 enum cube_coord
811 {
812     XCOORD = 0,
813     XCOORDINV = 1,
814     YCOORD = 2,
815     YCOORDINV = 3,
816     ZERO = 4,
817     ONE = 5
818 };
819
820 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
821 {
822     switch (coord)
823     {
824         case XCOORD:
825             return x + 0.5f;
826         case XCOORDINV:
827             return size - x - 0.5f;
828         case YCOORD:
829             return y + 0.5f;
830         case YCOORDINV:
831             return size - y - 0.5f;
832         case ZERO:
833             return 0.0f;
834         case ONE:
835             return size;
836         default:
837            trace("Unexpected coordinate value\n");
838            return 0.0f;
839     }
840 }
841
842 static void test_D3DXFillCubeTexture(IDirect3DDevice9 *device)
843 {
844     IDirect3DCubeTexture9 *tex;
845     HRESULT hr;
846     D3DLOCKED_RECT lock_rect;
847     DWORD x, y, f, m;
848     DWORD v[4], e[4];
849     DWORD value, expected, size, pitch;
850     enum cube_coord coordmap[6][3] =
851         {
852             {ONE, YCOORDINV, XCOORDINV},
853             {ZERO, YCOORDINV, XCOORD},
854             {XCOORD, ONE, YCOORD},
855             {XCOORD, ZERO, YCOORDINV},
856             {XCOORD, YCOORDINV, ONE},
857             {XCOORDINV, YCOORDINV, ZERO}
858         };
859
860     size = 4;
861     hr = IDirect3DDevice9_CreateCubeTexture(device, size, 0, 0, D3DFMT_A8R8G8B8,
862                                             D3DPOOL_MANAGED, &tex, NULL);
863
864     if (SUCCEEDED(hr))
865     {
866         hr = D3DXFillCubeTexture(tex, fillfunc_cube, NULL);
867         ok(hr == D3D_OK, "D3DXFillCubeTexture returned %#x, expected %#x\n", hr, D3D_OK);
868
869         for (m = 0; m < 3; m++)
870         {
871             for (f = 0; f < 6; f++)
872             {
873                 hr = IDirect3DCubeTexture9_LockRect(tex, f, m, &lock_rect, NULL, D3DLOCK_READONLY);
874                 ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
875                 if (SUCCEEDED(hr))
876                 {
877                     pitch = lock_rect.Pitch / sizeof(DWORD);
878                     for (y = 0; y < size; y++)
879                     {
880                         for (x = 0; x < size; x++)
881                         {
882                             value = ((DWORD *)lock_rect.pBits)[y * pitch + x];
883                             v[0] = (value >> 24) & 0xff;
884                             v[1] = (value >> 16) & 0xff;
885                             v[2] = (value >> 8) & 0xff;
886                             v[3] = value & 0xff;
887
888                             e[0] = (f == 0) || (f == 1) ?
889                                 0 : (BYTE)(255.0f / size * 2.0f + 0.5f);
890                             e[1] = get_cube_coord(coordmap[f][0], x, y, size) / size * 255.0f + 0.5f;
891                             e[2] = get_cube_coord(coordmap[f][1], x, y, size) / size * 255.0f + 0.5f;
892                             e[3] = get_cube_coord(coordmap[f][2], x, y, size) / size * 255.0f + 0.5f;
893                             expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
894
895                             ok(color_match(v, e),
896                                "Texel at face %u (%u, %u) doesn't match: %#x, expected %#x\n",
897                                f, x, y, value, expected);
898                         }
899                     }
900                     IDirect3DCubeTexture9_UnlockRect(tex, f, m);
901                 }
902             }
903             size >>= 1;
904         }
905     }
906     else
907         skip("Failed to create texture\n");
908
909     IDirect3DCubeTexture9_Release(tex);
910
911     hr = IDirect3DDevice9_CreateCubeTexture(device, 4, 1, 0, D3DFMT_A1R5G5B5,
912                                             D3DPOOL_MANAGED, &tex, NULL);
913
914     if (SUCCEEDED(hr))
915     {
916         hr = D3DXFillCubeTexture(tex, fillfunc_cube, NULL);
917         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
918         for (f = 0; f < 6; f++)
919         {
920             hr = IDirect3DCubeTexture9_LockRect(tex, f, 0, &lock_rect, NULL, D3DLOCK_READONLY);
921             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
922             if (SUCCEEDED(hr))
923             {
924                 pitch = lock_rect.Pitch / sizeof(WORD);
925                 for (y = 0; y < 4; y++)
926                 {
927                     for (x = 0; x < 4; x++)
928                     {
929                         value = ((WORD *)lock_rect.pBits)[y * pitch + x];
930                         v[0] = value >> 15;
931                         v[1] = value >> 10 & 0x1f;
932                         v[2] = value >> 5 & 0x1f;
933                         v[3] = value & 0x1f;
934
935                         e[0] = (f == 0) || (f == 1) ?
936                             0 : (BYTE)(1.0f / size * 2.0f + 0.5f);
937                         e[1] = get_cube_coord(coordmap[f][0], x, y, 4) / 4 * 31.0f + 0.5f;
938                         e[2] = get_cube_coord(coordmap[f][1], x, y, 4) / 4 * 31.0f + 0.5f;
939                         e[3] = get_cube_coord(coordmap[f][2], x, y, 4) / 4 * 31.0f + 0.5f;
940                         expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
941
942                         ok(color_match(v, e),
943                            "Texel at face %u (%u, %u) doesn't match: %#x, expected %#x\n",
944                            f, x, y, value, expected);
945                     }
946                 }
947                 IDirect3DCubeTexture9_UnlockRect(tex, f, 0);
948             }
949         }
950     }
951     else
952         skip("Failed to create texture\n");
953
954     IDirect3DCubeTexture9_Release(tex);
955 }
956
957 static void WINAPI fillfunc_volume(D3DXVECTOR4 *value, const D3DXVECTOR3 *texcoord,
958                                    const D3DXVECTOR3 *texelsize, void *data)
959 {
960     value->x = texcoord->x;
961     value->y = texcoord->y;
962     value->z = texcoord->z;
963     value->w = texelsize->x;
964 }
965
966 static void test_D3DXFillVolumeTexture(IDirect3DDevice9 *device)
967 {
968     IDirect3DVolumeTexture9 *tex;
969     HRESULT hr;
970     D3DLOCKED_BOX lock_box;
971     DWORD x, y, z, m;
972     DWORD v[4], e[4];
973     DWORD value, expected, size, row_pitch, slice_pitch;
974
975     size = 4;
976     hr = IDirect3DDevice9_CreateVolumeTexture(device, size, size, size, 0, 0, D3DFMT_A8R8G8B8,
977                                               D3DPOOL_MANAGED, &tex, NULL);
978
979     if (SUCCEEDED(hr))
980     {
981         hr = D3DXFillVolumeTexture(tex, fillfunc_volume, NULL);
982         ok(hr == D3D_OK, "D3DXFillVolumeTexture returned %#x, expected %#x\n", hr, D3D_OK);
983
984         for (m = 0; m < 3; m++)
985         {
986             hr = IDirect3DVolumeTexture9_LockBox(tex, m, &lock_box, NULL, D3DLOCK_READONLY);
987             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
988             if (SUCCEEDED(hr))
989             {
990                 row_pitch = lock_box.RowPitch / sizeof(DWORD);
991                 slice_pitch = lock_box.SlicePitch / sizeof(DWORD);
992                 for (z = 0; z < size; z++)
993                 {
994                     for (y = 0; y < size; y++)
995                     {
996                         for (x = 0; x < size; x++)
997                         {
998                             value = ((DWORD *)lock_box.pBits)[z * slice_pitch + y * row_pitch + x];
999                             v[0] = (value >> 24) & 0xff;
1000                             v[1] = (value >> 16) & 0xff;
1001                             v[2] = (value >> 8) & 0xff;
1002                             v[3] = value & 0xff;
1003
1004                             e[0] = 255.0f / size + 0.5f;
1005                             e[1] = (x + 0.5f) / size * 255.0f + 0.5f;
1006                             e[2] = (y + 0.5f) / size * 255.0f + 0.5f;
1007                             e[3] = (z + 0.5f) / size * 255.0f + 0.5f;
1008                             expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
1009
1010                             ok(color_match(v, e),
1011                                "Texel at (%u, %u, %u) doesn't match: %#x, expected %#x\n",
1012                                x, y, z, value, expected);
1013                         }
1014                     }
1015                 }
1016                 IDirect3DVolumeTexture9_UnlockBox(tex, m);
1017             }
1018             size >>= 1;
1019         }
1020     }
1021     else
1022         skip("Failed to create texture\n");
1023
1024     IDirect3DVolumeTexture9_Release(tex);
1025
1026     hr = IDirect3DDevice9_CreateVolumeTexture(device, 4, 4, 4, 1, 0, D3DFMT_A1R5G5B5,
1027                                               D3DPOOL_MANAGED, &tex, NULL);
1028
1029     if (SUCCEEDED(hr))
1030     {
1031         hr = D3DXFillVolumeTexture(tex, fillfunc_volume, NULL);
1032         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
1033         hr = IDirect3DVolumeTexture9_LockBox(tex, 0, &lock_box, NULL, D3DLOCK_READONLY);
1034         ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
1035         if (SUCCEEDED(hr))
1036         {
1037             row_pitch = lock_box.RowPitch / sizeof(WORD);
1038             slice_pitch = lock_box.SlicePitch / sizeof(WORD);
1039             for (z = 0; z < 4; z++)
1040             {
1041                 for (y = 0; y < 4; y++)
1042                 {
1043                     for (x = 0; x < 4; x++)
1044                     {
1045                         value = ((WORD *)lock_box.pBits)[z * slice_pitch + y * row_pitch + x];
1046                         v[0] = value >> 15;
1047                         v[1] = value >> 10 & 0x1f;
1048                         v[2] = value >> 5 & 0x1f;
1049                         v[3] = value & 0x1f;
1050
1051                         e[0] = 1;
1052                         e[1] = (x + 0.5f) / 4 * 31.0f + 0.5f;
1053                         e[2] = (y + 0.5f) / 4 * 31.0f + 0.5f;
1054                         e[3] = (z + 0.5f) / 4 * 31.0f + 0.5f;
1055                         expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
1056
1057                         ok(color_match(v, e),
1058                            "Texel at (%u, %u, %u) doesn't match: %#x, expected %#x\n",
1059                            x, y, z, value, expected);
1060                     }
1061                 }
1062             }
1063             IDirect3DVolumeTexture9_UnlockBox(tex, 0);
1064         }
1065     }
1066     else
1067         skip("Failed to create texture\n");
1068
1069     IDirect3DVolumeTexture9_Release(tex);
1070 }
1071
1072 START_TEST(texture)
1073 {
1074     HWND wnd;
1075     IDirect3D9 *d3d;
1076     IDirect3DDevice9 *device;
1077     D3DPRESENT_PARAMETERS d3dpp;
1078     HRESULT hr;
1079
1080     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1081     if (!wnd) {
1082         skip("Couldn't create application window\n");
1083         return;
1084     }
1085     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1086     if (!d3d) {
1087         skip("Couldn't create IDirect3D9 object\n");
1088         DestroyWindow(wnd);
1089         return;
1090     }
1091
1092     ZeroMemory(&d3dpp, sizeof(d3dpp));
1093     d3dpp.Windowed   = TRUE;
1094     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1095     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1096     if (FAILED(hr)) {
1097         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1098         IDirect3D9_Release(d3d);
1099         DestroyWindow(wnd);
1100         return;
1101     }
1102
1103     test_D3DXCheckTextureRequirements(device);
1104     test_D3DXCheckCubeTextureRequirements(device);
1105     test_D3DXCheckVolumeTextureRequirements(device);
1106     test_D3DXCreateTexture(device);
1107     test_D3DXFilterTexture(device);
1108     test_D3DXFillTexture(device);
1109     test_D3DXFillCubeTexture(device);
1110     test_D3DXFillVolumeTexture(device);
1111
1112     IDirect3DDevice9_Release(device);
1113     IDirect3D9_Release(d3d);
1114     DestroyWindow(wnd);
1115 }