d3dx9/tests: Remove d3d9 test from d3dx9 tests.
[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 /* 2x2 16-bit dds, no mipmaps */
29 static const unsigned char dds_16bit[] = {
30 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x02,0x00,0x00,0x00,
31 0x02,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
32 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
33 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
34 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
35 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x7c,0x00,0x00,
36 0xe0,0x03,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
37 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
38 0xff,0x7f,0xff,0x7f,0xff,0x7f,0xff,0x7f
39 };
40
41 /* 2x2 24-bit dds, 2 mipmaps */
42 static const unsigned char dds_24bit[] = {
43 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x0a,0x00,0x02,0x00,0x00,0x00,
44 0x02,0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
45 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
46 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
47 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
48 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0xff,0x00,
49 0x00,0xff,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
50 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
51 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
52 };
53
54 /* 4x4 cube map dds */
55 static const unsigned char dds_cube_map[] = {
56 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x04,0x00,0x00,0x00,
57 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
58 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
59 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
60 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
61 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x35,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
62 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x00,0x00,
63 0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
64 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
65 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
66 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
67 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
68 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
69 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50
70 };
71
72 /* 4x4x2 volume map dds, 2 mipmaps */
73 static const unsigned char dds_volume_map[] = {
74 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x8a,0x00,0x04,0x00,0x00,0x00,
75 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
76 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
77 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
78 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
79 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
80 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
81 0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
82 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
83 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
84 0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x2f,0x7e,0xcf,0x79,0x01,0x54,0x5c,0x5c,
85 0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x84,0xef,0x7b,0xaa,0xab,0xab,0xab
86 };
87
88 static BOOL is_autogenmipmap_supported(IDirect3DDevice9 *device, D3DRESOURCETYPE resource_type)
89 {
90     HRESULT hr;
91     D3DCAPS9 caps;
92     IDirect3D9 *d3d9;
93     D3DDISPLAYMODE mode;
94     D3DDEVICE_CREATION_PARAMETERS params;
95
96     IDirect3DDevice9_GetDeviceCaps(device, &caps);
97     IDirect3DDevice9_GetDirect3D(device, &d3d9);
98     IDirect3DDevice9_GetCreationParameters(device, &params);
99     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
100
101     if (!(caps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP))
102         return FALSE;
103
104     hr = IDirect3D9_CheckDeviceFormat(d3d9, params.AdapterOrdinal, params.DeviceType,
105         mode.Format, D3DUSAGE_AUTOGENMIPMAP, resource_type, D3DFMT_A8R8G8B8);
106
107     IDirect3D9_Release(d3d9);
108     return SUCCEEDED(hr);
109 }
110
111 static void test_D3DXCheckTextureRequirements(IDirect3DDevice9 *device)
112 {
113     UINT width, height, mipmaps;
114     D3DFORMAT format, expected;
115     D3DCAPS9 caps;
116     HRESULT hr;
117     IDirect3D9 *d3d;
118     D3DDEVICE_CREATION_PARAMETERS params;
119     D3DDISPLAYMODE mode;
120
121     IDirect3DDevice9_GetDeviceCaps(device, &caps);
122
123     /* general tests */
124     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
125     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
126
127     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
128     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
129
130     hr = D3DXCheckTextureRequirements(NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
131     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
132
133     /* width & height */
134     width = height = D3DX_DEFAULT;
135     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
136     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
137     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
138     ok(height == 256, "Returned height %d, expected %d\n", height, 256);
139
140     width = D3DX_DEFAULT;
141     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
142     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
143     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
144
145     if (caps.TextureCaps & D3DPTEXTURECAPS_POW2)
146         skip("Hardware only supports pow2 textures\n");
147     else
148     {
149         width = 62;
150         hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
151         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
152         ok(width == 62, "Returned width %d, expected %d\n", width, 62);
153
154         width = D3DX_DEFAULT; height = 63;
155         hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
156         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
157         ok(width == height, "Returned width %d, expected %d\n", width, height);
158         ok(height == 63, "Returned height %d, expected %d\n", height, 63);
159     }
160
161     width = D3DX_DEFAULT; height = 0;
162     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
163     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
164     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
165     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
166
167     width = 0; height = 0;
168     hr = D3DXCheckTextureRequirements(device, &width, &height, NULL, 0, NULL, D3DPOOL_DEFAULT);
169     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
170     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
171     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
172
173     width = 0;
174     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
175     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
176     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
177
178     width = 0xFFFFFFFE;
179     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
180     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
181     ok(width == caps.MaxTextureWidth, "Returned width %d, expected %d\n", width, caps.MaxTextureWidth);
182
183     width = caps.MaxTextureWidth-1;
184     hr = D3DXCheckTextureRequirements(device, &width, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
185     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
186     if (caps.TextureCaps & D3DPTEXTURECAPS_POW2)
187         ok(width == caps.MaxTextureWidth, "Returned width %d, expected %d\n", width, caps.MaxTextureWidth);
188     else
189         ok(width == caps.MaxTextureWidth-1, "Returned width %d, expected %d\n", width, caps.MaxTextureWidth-1);
190
191     /* mipmaps */
192     width = 64; height = 63;
193     mipmaps = 9;
194     hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
195     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
196     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
197
198     if (!(caps.TextureCaps & D3DPTEXTURECAPS_POW2))
199     {
200         width = 284; height = 137;
201         mipmaps = 20;
202         hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
203         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
204         ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
205
206         width = height = 63;
207         mipmaps = 9;
208         hr = D3DXCheckTextureRequirements(device, &width, &height, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
209         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
210         ok(mipmaps == 6, "Returned mipmaps %d, expected %d\n", mipmaps, 6);
211     }
212     else
213         skip("Skipping some tests, npot2 textures unsupported\n");
214
215     mipmaps = 20;
216     hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
217     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
218     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
219
220     mipmaps = 0;
221     hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
222     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
223     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
224
225     /* mipmaps when D3DUSAGE_AUTOGENMIPMAP is set */
226     if (is_autogenmipmap_supported(device, D3DRTYPE_TEXTURE))
227     {
228         mipmaps = 0;
229         hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
230         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
231         ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
232         mipmaps = 1;
233         hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
234         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
235         ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
236         mipmaps = 2;
237         hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
238         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
239         ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
240         mipmaps = 6;
241         hr = D3DXCheckTextureRequirements(device, NULL, NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
242         ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
243         ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
244     }
245     else
246         skip("No D3DUSAGE_AUTOGENMIPMAP support for textures\n");
247
248     /* usage */
249     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT);
250     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
251     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_DONOTCLIP, NULL, D3DPOOL_DEFAULT);
252     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
253     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_POINTS, NULL, D3DPOOL_DEFAULT);
254     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
255     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_RTPATCHES, NULL, D3DPOOL_DEFAULT);
256     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
257     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, D3DUSAGE_NPATCHES, NULL, D3DPOOL_DEFAULT);
258     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckTextureRequirements succeeded, but should've failed.\n");
259
260     /* format */
261     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
262     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
263
264     format = D3DFMT_UNKNOWN;
265     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
266     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
267     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
268
269     format = D3DX_DEFAULT;
270     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
271     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
272     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
273
274     format = D3DFMT_R8G8B8;
275     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
276     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
277     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
278
279     IDirect3DDevice9_GetDirect3D(device, &d3d);
280     IDirect3DDevice9_GetCreationParameters(device, &params);
281     IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
282
283     if (SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
284                                                mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_R3G3B2)))
285         expected = D3DFMT_R3G3B2;
286     else
287     {
288         if (SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
289                                                    mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_X4R4G4B4)))
290             expected = D3DFMT_X4R4G4B4;
291         else
292             expected = D3DFMT_X1R5G5B5;
293     }
294
295     format = D3DFMT_R3G3B2;
296     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
297     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
298     ok(format == expected, "Returned format %u, expected %u\n", format, expected);
299
300     if(SUCCEEDED(IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
301                                               mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R3G3B2)))
302         expected = D3DFMT_A8R3G3B2;
303     else
304         expected = D3DFMT_A8R8G8B8;
305
306     format = D3DFMT_A8R3G3B2;
307     hr = D3DXCheckTextureRequirements(device, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
308     ok(hr == D3D_OK, "D3DXCheckTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
309     ok(format == expected, "Returned format %u, expected %u\n", format, expected);
310
311     IDirect3D9_Release(d3d);
312 }
313
314 static void test_D3DXCheckCubeTextureRequirements(IDirect3DDevice9 *device)
315 {
316     UINT size, mipmaps, expected;
317     D3DFORMAT format;
318     D3DCAPS9 caps;
319     HRESULT hr;
320
321     IDirect3DDevice9_GetDeviceCaps(device, &caps);
322
323     if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
324     {
325         skip("No cube textures support\n");
326         return;
327     }
328
329     /* general tests */
330     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
331     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
332
333     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
334     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
335
336     hr = D3DXCheckCubeTextureRequirements(NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
337     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
338
339     /* size */
340     size = D3DX_DEFAULT;
341     hr = D3DXCheckCubeTextureRequirements(device, &size, NULL, 0, NULL, D3DPOOL_DEFAULT);
342     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
343     ok(size == 256, "Returned size %d, expected %d\n", size, 256);
344
345     /* mipmaps */
346     size = 64;
347     mipmaps = 9;
348     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
349     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
350     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
351
352     size = 284;
353     mipmaps = 20;
354     expected = caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2 ? 10 : 9;
355     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
356     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
357     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
358
359     size = 63;
360     mipmaps = 9;
361     expected = caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2 ? 7 : 6;
362     hr = D3DXCheckCubeTextureRequirements(device, &size, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
363     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
364     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
365
366     mipmaps = 0;
367     hr = D3DXCheckCubeTextureRequirements(device, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
368     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
369     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
370
371     if (is_autogenmipmap_supported(device, D3DRTYPE_CUBETEXTURE))
372     {
373         mipmaps = 3;
374         hr = D3DXCheckCubeTextureRequirements(device, NULL,  &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
375         ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
376         ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
377     }
378     else
379         skip("No D3DUSAGE_AUTOGENMIPMAP support for cube textures\n");
380
381     /* usage */
382     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_WRITEONLY, NULL, D3DPOOL_DEFAULT);
383     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
384     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_DONOTCLIP, NULL, D3DPOOL_DEFAULT);
385     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
386     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_POINTS, NULL, D3DPOOL_DEFAULT);
387     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
388     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_RTPATCHES, NULL, D3DPOOL_DEFAULT);
389     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
390     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, D3DUSAGE_NPATCHES, NULL, D3DPOOL_DEFAULT);
391     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckCubeTextureRequirements succeeded, but should've failed.\n");
392
393     /* format */
394     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
395     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
396
397     format = D3DFMT_UNKNOWN;
398     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
399     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
400     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
401
402     format = D3DX_DEFAULT;
403     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
404     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
405     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
406
407     format = D3DFMT_R8G8B8;
408     hr = D3DXCheckCubeTextureRequirements(device, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
409     ok(hr == D3D_OK, "D3DXCheckCubeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
410     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
411 }
412
413 static void test_D3DXCheckVolumeTextureRequirements(IDirect3DDevice9 *device)
414 {
415     UINT width, height, depth, mipmaps, expected;
416     D3DFORMAT format;
417     D3DCAPS9 caps;
418     HRESULT hr;
419
420     IDirect3DDevice9_GetDeviceCaps(device, &caps);
421
422     if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
423     {
424         skip("No volume textures support\n");
425         return;
426     }
427
428     /* general tests */
429     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
430     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
431
432     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
433     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
434
435     hr = D3DXCheckVolumeTextureRequirements(NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, NULL, D3DPOOL_DEFAULT);
436     ok(hr == D3DERR_INVALIDCALL, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
437
438     /* width, height, depth */
439     width = height = depth = D3DX_DEFAULT;
440     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
441     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
442     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
443     ok(height == 256, "Returned height %d, expected %d\n", height, 256);
444     ok(depth == 1, "Returned depth %d, expected %d\n", depth, 1);
445
446     width = D3DX_DEFAULT;
447     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
448     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
449     ok(width == 256, "Returned width %d, expected %d\n", width, 256);
450
451     width = D3DX_DEFAULT; height = 0; depth = 0;
452     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
453     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
454     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
455     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
456     ok(depth == 1, "Returned height %d, expected %d\n", depth, 1);
457
458     width = 0; height = 0; depth = 0;
459     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, NULL, 0, NULL, D3DPOOL_DEFAULT);
460     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
461     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
462     ok(height == 1, "Returned height %d, expected %d\n", height, 1);
463     ok(depth == 1, "Returned height %d, expected %d\n", depth, 1);
464
465     width = 0;
466     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
467     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
468     ok(width == 1, "Returned width %d, expected %d\n", width, 1);
469
470     width = 0xFFFFFFFE;
471     hr = D3DXCheckVolumeTextureRequirements(device, &width, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
472     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
473     ok(width == caps.MaxVolumeExtent, "Returned width %d, expected %d\n", width, caps.MaxVolumeExtent);
474
475     /* format */
476     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, NULL, D3DPOOL_DEFAULT);
477     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
478
479     format = D3DFMT_UNKNOWN;
480     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
481     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
482     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
483
484     format = D3DX_DEFAULT;
485     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
486     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
487     ok(format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_A8R8G8B8);
488
489     format = D3DFMT_R8G8B8;
490     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, NULL, 0, &format, D3DPOOL_DEFAULT);
491     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
492     ok(format == D3DFMT_X8R8G8B8, "Returned format %u, expected %u\n", format, D3DFMT_X8R8G8B8);
493
494     /* mipmaps */
495     if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
496     {
497         skip("No volume textures mipmapping support\n");
498         return;
499     }
500
501     width = height = depth = 64;
502     mipmaps = 9;
503     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
504     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
505     ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
506
507     width = 284;
508     height = 143;
509     depth = 55;
510     mipmaps = 20;
511     expected = caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2 ? 10 : 9;
512     hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
513     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
514     ok(mipmaps == expected, "Returned mipmaps %d, expected %d\n", mipmaps, expected);
515
516     mipmaps = 0;
517     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL, NULL, &mipmaps, 0, NULL, D3DPOOL_DEFAULT);
518     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
519     ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
520
521     if (!is_autogenmipmap_supported(device, D3DRTYPE_VOLUMETEXTURE))
522     {
523         skip("No D3DUSAGE_AUTOGENMIPMAP support for volume textures\n");
524         return;
525     }
526
527     /* mipmaps when D3DUSAGE_AUTOGENMIPMAP is set */
528     mipmaps = 0;
529     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL,NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
530     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
531     ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
532     mipmaps = 1;
533     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL,NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
534     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
535     ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
536     mipmaps = 3;
537     hr = D3DXCheckVolumeTextureRequirements(device, NULL, NULL,NULL, &mipmaps, D3DUSAGE_AUTOGENMIPMAP, NULL, D3DPOOL_DEFAULT);
538     ok(hr == D3D_OK, "D3DXCheckVolumeTextureRequirements returned %#x, expected %#x\n", hr, D3D_OK);
539     ok(mipmaps == 0, "Returned mipmaps %d, expected %d\n", mipmaps, 0);
540 }
541
542 static void test_D3DXCreateTexture(IDirect3DDevice9 *device)
543 {
544     IDirect3DTexture9 *texture;
545     D3DSURFACE_DESC desc;
546     D3DCAPS9 caps;
547     UINT mipmaps;
548     HRESULT hr;
549
550     IDirect3DDevice9_GetDeviceCaps(device, &caps);
551
552     hr = D3DXCreateTexture(NULL, 0, 0, 0, 0, D3DX_DEFAULT, D3DPOOL_DEFAULT, NULL);
553     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
554
555     /* width and height tests */
556
557     hr = D3DXCreateTexture(device, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0, D3DPOOL_DEFAULT, &texture);
558     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
559
560     if (texture)
561     {
562         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
563         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
564         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
565
566         ok(desc.Width == 256, "Returned width %d, expected %d\n", desc.Width, 256);
567         ok(desc.Height == 256, "Returned height %d, expected %d\n", desc.Height, 256);
568
569         IDirect3DTexture9_Release(texture);
570     }
571
572
573     hr = D3DXCreateTexture(device, 0, 0, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
574     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
575
576     if (texture)
577     {
578         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
579         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
580         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
581
582         ok(desc.Width == 1, "Returned width %d, expected %d\n", desc.Width, 1);
583         ok(desc.Height == 1, "Returned height %d, expected %d\n", desc.Height, 1);
584
585         IDirect3DTexture9_Release(texture);
586     }
587
588
589     if (caps.TextureCaps & D3DPTEXTURECAPS_POW2)
590         skip("Hardware only supports pow2 textures\n");
591     else
592     {
593         hr = D3DXCreateTexture(device, D3DX_DEFAULT, 63, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
594         ok((hr == D3D_OK) ||
595            /* may not work with conditional NPOT */
596            ((hr != D3D_OK) && (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)),
597            "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
598
599         if (texture)
600         {
601             hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
602             ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
603             ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
604
605             /* Conditional NPOT may create a texture with different dimensions, so allow those
606                situations instead of returning a fail */
607
608             ok(desc.Width == 63 ||
609                (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL),
610                "Returned width %d, expected %d\n", desc.Width, 63);
611
612             ok(desc.Height == 63 ||
613                (caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL),
614                "Returned height %d, expected %d\n", desc.Height, 63);
615
616             IDirect3DTexture9_Release(texture);
617         }
618     }
619
620     /* mipmaps */
621
622     hr = D3DXCreateTexture(device, 64, 63, 9, 0, 0, D3DPOOL_DEFAULT, &texture);
623     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
624
625     if (texture)
626     {
627         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
628         ok(mipmaps == 7, "Returned mipmaps %d, expected %d\n", mipmaps, 7);
629
630         IDirect3DTexture9_Release(texture);
631     }
632
633
634     hr = D3DXCreateTexture(device, 284, 137, 9, 0, 0, D3DPOOL_DEFAULT, &texture);
635     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
636
637     if (texture)
638     {
639         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
640         ok(mipmaps == 9, "Returned mipmaps %d, expected %d\n", mipmaps, 9);
641
642         IDirect3DTexture9_Release(texture);
643     }
644
645
646     hr = D3DXCreateTexture(device, 0, 0, 20, 0, 0, D3DPOOL_DEFAULT, &texture);
647     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
648
649     if (texture)
650     {
651         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
652         ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
653
654         IDirect3DTexture9_Release(texture);
655     }
656
657
658     hr = D3DXCreateTexture(device, 64, 64, 1, 0, 0, D3DPOOL_DEFAULT, &texture);
659     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
660
661     if (texture)
662     {
663         mipmaps = IDirect3DTexture9_GetLevelCount(texture);
664         ok(mipmaps == 1, "Returned mipmaps %d, expected %d\n", mipmaps, 1);
665
666         IDirect3DTexture9_Release(texture);
667     }
668
669     /* usage */
670
671     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &texture);
672     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
673     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_DONOTCLIP, 0, D3DPOOL_DEFAULT, &texture);
674     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
675     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_POINTS, 0, D3DPOOL_DEFAULT, &texture);
676     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
677     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_RTPATCHES, 0, D3DPOOL_DEFAULT, &texture);
678     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
679     hr = D3DXCreateTexture(device, 0, 0, 0, D3DUSAGE_NPATCHES, 0, D3DPOOL_DEFAULT, &texture);
680     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTexture succeeded, but should have failed.\n");
681
682     /* format */
683
684     hr = D3DXCreateTexture(device, 0, 0, 0, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, &texture);
685     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
686
687     if (texture)
688     {
689         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
690         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
691         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
692
693         IDirect3DTexture9_Release(texture);
694     }
695
696
697     hr = D3DXCreateTexture(device, 0, 0, 0, 0, 0, D3DPOOL_DEFAULT, &texture);
698     ok(hr == D3D_OK, "D3DXCreateTexture returned %#x, expected %#x\n", hr, D3D_OK);
699
700     if (texture)
701     {
702         hr = IDirect3DTexture9_GetLevelDesc(texture, 0, &desc);
703         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
704         ok(desc.Format == D3DFMT_A8R8G8B8, "Returned format %u, expected %u\n", desc.Format, D3DFMT_A8R8G8B8);
705
706         IDirect3DTexture9_Release(texture);
707     }
708
709     /* D3DXCreateTextureFromResource */
710     todo_wine {
711         hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &texture);
712         ok(hr == D3D_OK, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3D_OK);
713         if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
714     }
715     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &texture);
716     ok(hr == D3D_OK, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3D_OK);
717     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
718
719     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDS_STRING), &texture);
720     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
721
722     hr = D3DXCreateTextureFromResourceA(NULL, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &texture);
723     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
724
725     hr = D3DXCreateTextureFromResourceA(device, NULL, NULL, &texture);
726     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
727
728     hr = D3DXCreateTextureFromResourceA(device, NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), NULL);
729     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
730
731
732     /* D3DXCreateTextureFromResourceEx */
733     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);
734     ok(hr == D3D_OK, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3D_OK);
735     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
736
737     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);
738     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
739
740     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);
741     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
742
743     hr = D3DXCreateTextureFromResourceExA(device, NULL, NULL, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
744     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
745
746     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);
747     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateTextureFromResourceEx returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
748 }
749
750 static void test_D3DXFilterTexture(IDirect3DDevice9 *device)
751 {
752     IDirect3DTexture9 *tex;
753     IDirect3DCubeTexture9 *cubetex;
754     HRESULT hr;
755
756     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 5, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &tex, NULL);
757
758     if (SUCCEEDED(hr))
759     {
760         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
761         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
762
763         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_BOX + 1); /* Invalid filter */
764         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
765
766         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 5, D3DX_FILTER_NONE); /* Invalid miplevel */
767         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
768
769         IDirect3DTexture9_Release(tex);
770     }
771     else
772         skip("Failed to create texture\n");
773
774     hr = D3DXFilterTexture(NULL, NULL, 0, D3DX_FILTER_NONE);
775     ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
776
777     /* Test different pools */
778     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &tex, NULL);
779
780     if (SUCCEEDED(hr))
781     {
782         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
783         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
784         IDirect3DTexture9_Release(tex);
785     }
786     else
787         skip("Failed to create texture\n");
788
789     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &tex, NULL);
790
791     if (SUCCEEDED(hr))
792     {
793         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
794         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
795         IDirect3DTexture9_Release(tex);
796     }
797     else
798         skip("Failed to create texture\n");
799
800     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
801     if (SUCCEEDED(hr))
802     {
803         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_POINT);
804         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
805         IDirect3DTexture9_Release(tex);
806     }
807     else
808         skip("Failed to create texture\n");
809
810     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
811     if (SUCCEEDED(hr))
812     {
813         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_POINT);
814         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
815         IDirect3DTexture9_Release(tex);
816     }
817     else
818         skip("Failed to create texture\n");
819
820     /* Cube texture test */
821     hr = IDirect3DDevice9_CreateCubeTexture(device, 256, 5, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &cubetex, NULL);
822
823     if (SUCCEEDED(hr))
824     {
825         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 0, D3DX_FILTER_NONE);
826         ok(hr == D3D_OK, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3D_OK);
827
828         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 0, D3DX_FILTER_BOX + 1); /* Invalid filter */
829         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
830
831         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) cubetex, NULL, 5, D3DX_FILTER_NONE); /* Invalid miplevel */
832         ok(hr == D3DERR_INVALIDCALL, "D3DXFilterTexture returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
833         IDirect3DCubeTexture9_Release(cubetex);
834     }
835     else
836         skip("Failed to create texture\n");
837
838     /* Test textures with D3DUSAGE_AUTOGENMIPMAP usage */
839     if (!is_autogenmipmap_supported(device, D3DRTYPE_TEXTURE))
840     {
841         skip("No D3DUSAGE_AUTOGENMIPMAP supported for textures\n");
842         return;
843     }
844
845     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, D3DUSAGE_DYNAMIC | D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
846     if (SUCCEEDED(hr))
847     {
848         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
849         ok(hr == D3D_OK, "D3dXFilteTexture returned %#x, expected %#x\n", hr, D3D_OK);
850         IDirect3DTexture9_Release(tex);
851     }
852     else
853         skip("Failed to create texture\n");
854
855     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 1, D3DUSAGE_DYNAMIC | D3DUSAGE_AUTOGENMIPMAP, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
856     if (SUCCEEDED(hr))
857     {
858         hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, NULL, 0, D3DX_FILTER_NONE);
859         ok(hr == D3D_OK, "D3dXFilteTexture returned %#x, expected %#x\n", hr, D3D_OK);
860         IDirect3DTexture9_Release(tex);
861     }
862     else
863         skip("Failed to create texture\n");
864 }
865
866 static BOOL color_match(const DWORD *value, const DWORD *expected)
867 {
868     int i;
869
870     for (i = 0; i < 4; i++)
871     {
872         DWORD diff = value[i] > expected[i] ? value[i] - expected[i] : expected[i] - value[i];
873         if (diff > 1) return FALSE;
874     }
875     return TRUE;
876 }
877
878 static void WINAPI fillfunc(D3DXVECTOR4 *value, const D3DXVECTOR2 *texcoord,
879                             const D3DXVECTOR2 *texelsize, void *data)
880 {
881     value->x = texcoord->x;
882     value->y = texcoord->y;
883     value->z = texelsize->x;
884     value->w = 1.0f;
885 }
886
887 static void test_D3DXFillTexture(IDirect3DDevice9 *device)
888 {
889     IDirect3DTexture9 *tex;
890     HRESULT hr;
891     D3DLOCKED_RECT lock_rect;
892     DWORD x, y, m;
893     DWORD v[4], e[4];
894     DWORD value, expected, size, pitch;
895
896     size = 4;
897     hr = IDirect3DDevice9_CreateTexture(device, size, size, 0, 0, D3DFMT_A8R8G8B8,
898                                         D3DPOOL_MANAGED, &tex, NULL);
899
900     if (SUCCEEDED(hr))
901     {
902         hr = D3DXFillTexture(tex, fillfunc, NULL);
903         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
904
905         for (m = 0; m < 3; m++)
906         {
907             hr = IDirect3DTexture9_LockRect(tex, m, &lock_rect, NULL, D3DLOCK_READONLY);
908             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
909             if (SUCCEEDED(hr))
910             {
911                 pitch = lock_rect.Pitch / sizeof(DWORD);
912                 for (y = 0; y < size; y++)
913                 {
914                     for (x = 0; x < size; x++)
915                     {
916                         value = ((DWORD *)lock_rect.pBits)[y * pitch + x];
917                         v[0] = (value >> 24) & 0xff;
918                         v[1] = (value >> 16) & 0xff;
919                         v[2] = (value >> 8) & 0xff;
920                         v[3] = value & 0xff;
921
922                         e[0] = 0xff;
923                         e[1] = (x + 0.5f) / size * 255.0f + 0.5f;
924                         e[2] = (y + 0.5f) / size * 255.0f + 0.5f;
925                         e[3] = 255.0f / size + 0.5f;
926                         expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
927
928                         ok(color_match(v, e),
929                            "Texel at (%u, %u) doesn't match: %#x, expected %#x\n",
930                            x, y, value, expected);
931                     }
932                 }
933                 IDirect3DTexture9_UnlockRect(tex, m);
934             }
935             size >>= 1;
936         }
937     }
938     else
939         skip("Failed to create texture\n");
940
941     IDirect3DTexture9_Release(tex);
942
943     hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_A1R5G5B5,
944                                         D3DPOOL_MANAGED, &tex, NULL);
945
946     if (SUCCEEDED(hr))
947     {
948         hr = D3DXFillTexture(tex, fillfunc, NULL);
949         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
950
951         hr = IDirect3DTexture9_LockRect(tex, 0, &lock_rect, NULL, D3DLOCK_READONLY);
952         ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
953         if (SUCCEEDED(hr))
954         {
955             pitch = lock_rect.Pitch / sizeof(WORD);
956             for (y = 0; y < 4; y++)
957             {
958                 for (x = 0; x < 4; x++)
959                 {
960                     value = ((WORD *)lock_rect.pBits)[y * pitch + x];
961                     v[0] = value >> 15;
962                     v[1] = value >> 10 & 0x1f;
963                     v[2] = value >> 5 & 0x1f;
964                     v[3] = value & 0x1f;
965
966                     e[0] = 1;
967                     e[1] = (x + 0.5f) / 4.0f * 31.0f + 0.5f;
968                     e[2] = (y + 0.5f) / 4.0f * 31.0f + 0.5f;
969                     e[3] = 8;
970                     expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
971
972                     ok(color_match(v, e),
973                        "Texel at (%u, %u) doesn't match: %#x, expected %#x\n",
974                        x, y, value, expected);
975                 }
976             }
977             IDirect3DTexture9_UnlockRect(tex, 0);
978         }
979     }
980     else
981         skip("Failed to create texture\n");
982
983     IDirect3DTexture9_Release(tex);
984 }
985
986 static void WINAPI fillfunc_cube(D3DXVECTOR4 *value, const D3DXVECTOR3 *texcoord,
987                                  const D3DXVECTOR3 *texelsize, void *data)
988 {
989     value->x = (texcoord->x + 1.0f) / 2.0f;
990     value->y = (texcoord->y + 1.0f) / 2.0f;
991     value->z = (texcoord->z + 1.0f) / 2.0f;
992     value->w = texelsize->x;
993 }
994
995 enum cube_coord
996 {
997     XCOORD = 0,
998     XCOORDINV = 1,
999     YCOORD = 2,
1000     YCOORDINV = 3,
1001     ZERO = 4,
1002     ONE = 5
1003 };
1004
1005 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1006 {
1007     switch (coord)
1008     {
1009         case XCOORD:
1010             return x + 0.5f;
1011         case XCOORDINV:
1012             return size - x - 0.5f;
1013         case YCOORD:
1014             return y + 0.5f;
1015         case YCOORDINV:
1016             return size - y - 0.5f;
1017         case ZERO:
1018             return 0.0f;
1019         case ONE:
1020             return size;
1021         default:
1022            trace("Unexpected coordinate value\n");
1023            return 0.0f;
1024     }
1025 }
1026
1027 static void test_D3DXFillCubeTexture(IDirect3DDevice9 *device)
1028 {
1029     IDirect3DCubeTexture9 *tex;
1030     HRESULT hr;
1031     D3DLOCKED_RECT lock_rect;
1032     DWORD x, y, f, m;
1033     DWORD v[4], e[4];
1034     DWORD value, expected, size, pitch;
1035     enum cube_coord coordmap[6][3] =
1036         {
1037             {ONE, YCOORDINV, XCOORDINV},
1038             {ZERO, YCOORDINV, XCOORD},
1039             {XCOORD, ONE, YCOORD},
1040             {XCOORD, ZERO, YCOORDINV},
1041             {XCOORD, YCOORDINV, ONE},
1042             {XCOORDINV, YCOORDINV, ZERO}
1043         };
1044
1045     size = 4;
1046     hr = IDirect3DDevice9_CreateCubeTexture(device, size, 0, 0, D3DFMT_A8R8G8B8,
1047                                             D3DPOOL_MANAGED, &tex, NULL);
1048
1049     if (SUCCEEDED(hr))
1050     {
1051         hr = D3DXFillCubeTexture(tex, fillfunc_cube, NULL);
1052         ok(hr == D3D_OK, "D3DXFillCubeTexture returned %#x, expected %#x\n", hr, D3D_OK);
1053
1054         for (m = 0; m < 3; m++)
1055         {
1056             for (f = 0; f < 6; f++)
1057             {
1058                 hr = IDirect3DCubeTexture9_LockRect(tex, f, m, &lock_rect, NULL, D3DLOCK_READONLY);
1059                 ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
1060                 if (SUCCEEDED(hr))
1061                 {
1062                     pitch = lock_rect.Pitch / sizeof(DWORD);
1063                     for (y = 0; y < size; y++)
1064                     {
1065                         for (x = 0; x < size; x++)
1066                         {
1067                             value = ((DWORD *)lock_rect.pBits)[y * pitch + x];
1068                             v[0] = (value >> 24) & 0xff;
1069                             v[1] = (value >> 16) & 0xff;
1070                             v[2] = (value >> 8) & 0xff;
1071                             v[3] = value & 0xff;
1072
1073                             e[0] = (f == 0) || (f == 1) ?
1074                                 0 : (BYTE)(255.0f / size * 2.0f + 0.5f);
1075                             e[1] = get_cube_coord(coordmap[f][0], x, y, size) / size * 255.0f + 0.5f;
1076                             e[2] = get_cube_coord(coordmap[f][1], x, y, size) / size * 255.0f + 0.5f;
1077                             e[3] = get_cube_coord(coordmap[f][2], x, y, size) / size * 255.0f + 0.5f;
1078                             expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
1079
1080                             ok(color_match(v, e),
1081                                "Texel at face %u (%u, %u) doesn't match: %#x, expected %#x\n",
1082                                f, x, y, value, expected);
1083                         }
1084                     }
1085                     IDirect3DCubeTexture9_UnlockRect(tex, f, m);
1086                 }
1087             }
1088             size >>= 1;
1089         }
1090     }
1091     else
1092         skip("Failed to create texture\n");
1093
1094     IDirect3DCubeTexture9_Release(tex);
1095
1096     hr = IDirect3DDevice9_CreateCubeTexture(device, 4, 1, 0, D3DFMT_A1R5G5B5,
1097                                             D3DPOOL_MANAGED, &tex, NULL);
1098
1099     if (SUCCEEDED(hr))
1100     {
1101         hr = D3DXFillCubeTexture(tex, fillfunc_cube, NULL);
1102         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
1103         for (f = 0; f < 6; f++)
1104         {
1105             hr = IDirect3DCubeTexture9_LockRect(tex, f, 0, &lock_rect, NULL, D3DLOCK_READONLY);
1106             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
1107             if (SUCCEEDED(hr))
1108             {
1109                 pitch = lock_rect.Pitch / sizeof(WORD);
1110                 for (y = 0; y < 4; y++)
1111                 {
1112                     for (x = 0; x < 4; x++)
1113                     {
1114                         value = ((WORD *)lock_rect.pBits)[y * pitch + x];
1115                         v[0] = value >> 15;
1116                         v[1] = value >> 10 & 0x1f;
1117                         v[2] = value >> 5 & 0x1f;
1118                         v[3] = value & 0x1f;
1119
1120                         e[0] = (f == 0) || (f == 1) ?
1121                             0 : (BYTE)(1.0f / size * 2.0f + 0.5f);
1122                         e[1] = get_cube_coord(coordmap[f][0], x, y, 4) / 4 * 31.0f + 0.5f;
1123                         e[2] = get_cube_coord(coordmap[f][1], x, y, 4) / 4 * 31.0f + 0.5f;
1124                         e[3] = get_cube_coord(coordmap[f][2], x, y, 4) / 4 * 31.0f + 0.5f;
1125                         expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
1126
1127                         ok(color_match(v, e),
1128                            "Texel at face %u (%u, %u) doesn't match: %#x, expected %#x\n",
1129                            f, x, y, value, expected);
1130                     }
1131                 }
1132                 IDirect3DCubeTexture9_UnlockRect(tex, f, 0);
1133             }
1134         }
1135     }
1136     else
1137         skip("Failed to create texture\n");
1138
1139     IDirect3DCubeTexture9_Release(tex);
1140 }
1141
1142 static void WINAPI fillfunc_volume(D3DXVECTOR4 *value, const D3DXVECTOR3 *texcoord,
1143                                    const D3DXVECTOR3 *texelsize, void *data)
1144 {
1145     value->x = texcoord->x;
1146     value->y = texcoord->y;
1147     value->z = texcoord->z;
1148     value->w = texelsize->x;
1149 }
1150
1151 static void test_D3DXFillVolumeTexture(IDirect3DDevice9 *device)
1152 {
1153     IDirect3DVolumeTexture9 *tex;
1154     HRESULT hr;
1155     D3DLOCKED_BOX lock_box;
1156     DWORD x, y, z, m;
1157     DWORD v[4], e[4];
1158     DWORD value, expected, size, row_pitch, slice_pitch;
1159
1160     size = 4;
1161     hr = IDirect3DDevice9_CreateVolumeTexture(device, size, size, size, 0, 0, D3DFMT_A8R8G8B8,
1162                                               D3DPOOL_MANAGED, &tex, NULL);
1163
1164     if (SUCCEEDED(hr))
1165     {
1166         hr = D3DXFillVolumeTexture(tex, fillfunc_volume, NULL);
1167         ok(hr == D3D_OK, "D3DXFillVolumeTexture returned %#x, expected %#x\n", hr, D3D_OK);
1168
1169         for (m = 0; m < 3; m++)
1170         {
1171             hr = IDirect3DVolumeTexture9_LockBox(tex, m, &lock_box, NULL, D3DLOCK_READONLY);
1172             ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
1173             if (SUCCEEDED(hr))
1174             {
1175                 row_pitch = lock_box.RowPitch / sizeof(DWORD);
1176                 slice_pitch = lock_box.SlicePitch / sizeof(DWORD);
1177                 for (z = 0; z < size; z++)
1178                 {
1179                     for (y = 0; y < size; y++)
1180                     {
1181                         for (x = 0; x < size; x++)
1182                         {
1183                             value = ((DWORD *)lock_box.pBits)[z * slice_pitch + y * row_pitch + x];
1184                             v[0] = (value >> 24) & 0xff;
1185                             v[1] = (value >> 16) & 0xff;
1186                             v[2] = (value >> 8) & 0xff;
1187                             v[3] = value & 0xff;
1188
1189                             e[0] = 255.0f / size + 0.5f;
1190                             e[1] = (x + 0.5f) / size * 255.0f + 0.5f;
1191                             e[2] = (y + 0.5f) / size * 255.0f + 0.5f;
1192                             e[3] = (z + 0.5f) / size * 255.0f + 0.5f;
1193                             expected = e[0] << 24 | e[1] << 16 | e[2] << 8 | e[3];
1194
1195                             ok(color_match(v, e),
1196                                "Texel at (%u, %u, %u) doesn't match: %#x, expected %#x\n",
1197                                x, y, z, value, expected);
1198                         }
1199                     }
1200                 }
1201                 IDirect3DVolumeTexture9_UnlockBox(tex, m);
1202             }
1203             size >>= 1;
1204         }
1205     }
1206     else
1207         skip("Failed to create texture\n");
1208
1209     IDirect3DVolumeTexture9_Release(tex);
1210
1211     hr = IDirect3DDevice9_CreateVolumeTexture(device, 4, 4, 4, 1, 0, D3DFMT_A1R5G5B5,
1212                                               D3DPOOL_MANAGED, &tex, NULL);
1213
1214     if (SUCCEEDED(hr))
1215     {
1216         hr = D3DXFillVolumeTexture(tex, fillfunc_volume, NULL);
1217         ok(hr == D3D_OK, "D3DXFillTexture returned %#x, expected %#x\n", hr, D3D_OK);
1218         hr = IDirect3DVolumeTexture9_LockBox(tex, 0, &lock_box, NULL, D3DLOCK_READONLY);
1219         ok(hr == D3D_OK, "Couldn't lock the texture, error %#x\n", hr);
1220         if (SUCCEEDED(hr))
1221         {
1222             row_pitch = lock_box.RowPitch / sizeof(WORD);
1223             slice_pitch = lock_box.SlicePitch / sizeof(WORD);
1224             for (z = 0; z < 4; z++)
1225             {
1226                 for (y = 0; y < 4; y++)
1227                 {
1228                     for (x = 0; x < 4; x++)
1229                     {
1230                         value = ((WORD *)lock_box.pBits)[z * slice_pitch + y * row_pitch + x];
1231                         v[0] = value >> 15;
1232                         v[1] = value >> 10 & 0x1f;
1233                         v[2] = value >> 5 & 0x1f;
1234                         v[3] = value & 0x1f;
1235
1236                         e[0] = 1;
1237                         e[1] = (x + 0.5f) / 4 * 31.0f + 0.5f;
1238                         e[2] = (y + 0.5f) / 4 * 31.0f + 0.5f;
1239                         e[3] = (z + 0.5f) / 4 * 31.0f + 0.5f;
1240                         expected = e[0] << 15 | e[1] << 10 | e[2] << 5 | e[3];
1241
1242                         ok(color_match(v, e),
1243                            "Texel at (%u, %u, %u) doesn't match: %#x, expected %#x\n",
1244                            x, y, z, value, expected);
1245                     }
1246                 }
1247             }
1248             IDirect3DVolumeTexture9_UnlockBox(tex, 0);
1249         }
1250     }
1251     else
1252         skip("Failed to create texture\n");
1253
1254     IDirect3DVolumeTexture9_Release(tex);
1255 }
1256
1257 static void test_D3DXCreateTextureFromFileInMemory(IDirect3DDevice9 *device)
1258 {
1259     HRESULT hr;
1260     IDirect3DTexture9 *texture;
1261
1262     hr = D3DXCreateTextureFromFileInMemory(device, dds_16bit, sizeof(dds_16bit), &texture);
1263     ok(hr == D3D_OK, "D3DXCreateTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
1264     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
1265
1266     hr = D3DXCreateTextureFromFileInMemory(device, dds_24bit, sizeof(dds_24bit), &texture);
1267     ok(hr == D3D_OK, "D3DXCreateTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
1268     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
1269
1270     hr = D3DXCreateTextureFromFileInMemory(device, dds_24bit, sizeof(dds_24bit) - 1, &texture);
1271     ok(hr == D3DXERR_INVALIDDATA, "D3DXCreateTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
1272 }
1273
1274 static void test_D3DXCreateTextureFromFileInMemoryEx(IDirect3DDevice9 *device)
1275 {
1276     HRESULT hr;
1277     IDirect3DTexture9 *texture;
1278
1279     hr = D3DXCreateTextureFromFileInMemoryEx(device, dds_16bit, sizeof(dds_16bit), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
1280         0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
1281     ok(hr == D3D_OK, "D3DXCreateTextureFromFileInMemoryEx returned %#x, expected %#x\n", hr, D3D_OK);
1282     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
1283
1284     hr = D3DXCreateTextureFromFileInMemoryEx(device, dds_16bit, sizeof(dds_16bit), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
1285         D3DUSAGE_DYNAMIC, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
1286     ok(hr == D3D_OK, "D3DXCreateTextureFromFileInMemoryEx returned %#x, expected %#x\n", hr, D3D_OK);
1287     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
1288
1289     if (!is_autogenmipmap_supported(device, D3DRTYPE_TEXTURE))
1290     {
1291         skip("No D3DUSAGE_AUTOGENMIPMAP support for textures\n");
1292         return;
1293     }
1294
1295     hr = D3DXCreateTextureFromFileInMemoryEx(device, dds_16bit, sizeof(dds_16bit), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
1296         D3DUSAGE_DYNAMIC | D3DUSAGE_AUTOGENMIPMAP, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &texture);
1297     ok(hr == D3D_OK, "D3DXCreateTextureFromFileInMemoryEx returned %#x, expected %#x\n", hr, D3D_OK);
1298     if (SUCCEEDED(hr)) IDirect3DTexture9_Release(texture);
1299 }
1300
1301 static void test_D3DXCreateCubeTextureFromFileInMemory(IDirect3DDevice9 *device)
1302 {
1303     HRESULT hr;
1304     ULONG ref;
1305     DWORD levelcount;
1306     IDirect3DCubeTexture9 *cube_texture;
1307     D3DSURFACE_DESC surface_desc;
1308
1309     hr = D3DXCreateCubeTextureFromFileInMemory(NULL, dds_cube_map, sizeof(dds_cube_map), &cube_texture);
1310     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateCubeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1311
1312     hr = D3DXCreateCubeTextureFromFileInMemory(device, NULL, sizeof(dds_cube_map), &cube_texture);
1313     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateCubeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1314
1315     hr = D3DXCreateCubeTextureFromFileInMemory(device, dds_cube_map, 0, &cube_texture);
1316     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateCubeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1317
1318     hr = D3DXCreateCubeTextureFromFileInMemory(device, dds_cube_map, sizeof(dds_cube_map), NULL);
1319     ok(hr == D3DERR_INVALIDCALL, "D3DXCreateCubeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1320
1321     hr = D3DXCreateCubeTextureFromFileInMemory(device, dds_cube_map, sizeof(dds_cube_map), &cube_texture);
1322     if (SUCCEEDED(hr))
1323     {
1324         levelcount = IDirect3DCubeTexture9_GetLevelCount(cube_texture);
1325         todo_wine ok(levelcount == 3, "GetLevelCount returned %u, expected 3\n", levelcount);
1326
1327         hr = IDirect3DCubeTexture9_GetLevelDesc(cube_texture, 0, &surface_desc);
1328         ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
1329         ok(surface_desc.Width == 4, "Got width %u, expected 4\n", surface_desc.Width);
1330         ok(surface_desc.Height == 4, "Got height %u, expected 4\n", surface_desc.Height);
1331
1332         ref = IDirect3DCubeTexture9_Release(cube_texture);
1333         ok(ref == 0, "Invalid reference count. Got %u, expected 0\n", ref);
1334     } else skip("Couldn't create cube texture\n");
1335 }
1336
1337 static void test_D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device)
1338 {
1339     HRESULT hr;
1340     IDirect3DCubeTexture9 *cube_texture;
1341
1342     if (!is_autogenmipmap_supported(device, D3DRTYPE_CUBETEXTURE))
1343     {
1344         skip("No D3DUSAGE_AUTOGENMIPMAP support for cube textures\n");
1345         return;
1346     }
1347
1348     hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, dds_cube_map, sizeof(dds_cube_map), D3DX_DEFAULT, D3DX_DEFAULT,
1349         D3DUSAGE_DYNAMIC | D3DUSAGE_AUTOGENMIPMAP, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &cube_texture);
1350     ok(hr == D3D_OK, "D3DXCreateCubeTextureFromFileInMemoryEx returned %#x, expected %#x\n", hr, D3D_OK);
1351     if (SUCCEEDED(hr)) IDirect3DCubeTexture9_Release(cube_texture);
1352 }
1353
1354 static void test_D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device)
1355 {
1356     HRESULT hr;
1357     ULONG ref;
1358     DWORD levelcount;
1359     IDirect3DVolumeTexture9 *volume_texture;
1360     D3DVOLUME_DESC volume_desc;
1361
1362     todo_wine {
1363         hr = D3DXCreateVolumeTextureFromFileInMemory(NULL, dds_volume_map, sizeof(dds_volume_map), &volume_texture);
1364         ok(hr == D3DERR_INVALIDCALL, "D3DXCreateVolumeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1365
1366         hr = D3DXCreateVolumeTextureFromFileInMemory(device, NULL, sizeof(dds_volume_map), &volume_texture);
1367         ok(hr == D3DERR_INVALIDCALL, "D3DXCreateVolumeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1368
1369         hr = D3DXCreateVolumeTextureFromFileInMemory(device, dds_volume_map, 0, &volume_texture);
1370         ok(hr == D3DERR_INVALIDCALL, "D3DXCreateVolumeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1371
1372         hr = D3DXCreateVolumeTextureFromFileInMemory(device, dds_volume_map, sizeof(dds_volume_map), NULL);
1373         ok(hr == D3DERR_INVALIDCALL, "D3DXCreateVolumeTextureFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1374
1375         hr = D3DXCreateVolumeTextureFromFileInMemory(device, dds_volume_map, sizeof(dds_volume_map), &volume_texture);
1376         if (SUCCEEDED(hr))
1377         {
1378             levelcount = IDirect3DVolumeTexture9_GetLevelCount(volume_texture);
1379             ok(levelcount == 3, "GetLevelCount returned %u, expected 3\n", levelcount);
1380
1381             hr = IDirect3DVolumeTexture9_GetLevelDesc(volume_texture, 0, &volume_desc);
1382             ok(hr == D3D_OK, "GetLevelDesc returend %#x, expected %#x\n", hr, D3D_OK);
1383             ok(volume_desc.Width == 4, "Got width %u, expected 4\n", volume_desc.Width);
1384             ok(volume_desc.Height == 4, "Got height %u, expected 4\n", volume_desc.Height);
1385             ok(volume_desc.Depth == 2, "Got depth %u, expected 2\n", volume_desc.Depth);
1386
1387             hr = IDirect3DVolumeTexture9_GetLevelDesc(volume_texture, 1, &volume_desc);
1388             ok(hr == D3D_OK, "GetLevelDesc returned %#x, expected %#x\n", hr, D3D_OK);
1389             ok(volume_desc.Width == 2, "Got width %u, expected 2\n", volume_desc.Width);
1390             ok(volume_desc.Height == 2, "Got height %u, expected 2\n", volume_desc.Height);
1391             ok(volume_desc.Depth == 1, "Got depth %u, expected 1\n", volume_desc.Depth);
1392
1393             ref = IDirect3DVolumeTexture9_Release(volume_texture);
1394             ok(ref == 0, "Invalid reference count. Got %u, expected 0\n", ref);
1395         }
1396     }
1397 }
1398
1399 START_TEST(texture)
1400 {
1401     HWND wnd;
1402     IDirect3D9 *d3d;
1403     IDirect3DDevice9 *device;
1404     D3DPRESENT_PARAMETERS d3dpp;
1405     HRESULT hr;
1406
1407     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1408     if (!wnd) {
1409         skip("Couldn't create application window\n");
1410         return;
1411     }
1412     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1413     if (!d3d) {
1414         skip("Couldn't create IDirect3D9 object\n");
1415         DestroyWindow(wnd);
1416         return;
1417     }
1418
1419     ZeroMemory(&d3dpp, sizeof(d3dpp));
1420     d3dpp.Windowed   = TRUE;
1421     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1422     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1423     if (FAILED(hr)) {
1424         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1425         IDirect3D9_Release(d3d);
1426         DestroyWindow(wnd);
1427         return;
1428     }
1429
1430     test_D3DXCheckTextureRequirements(device);
1431     test_D3DXCheckCubeTextureRequirements(device);
1432     test_D3DXCheckVolumeTextureRequirements(device);
1433     test_D3DXCreateTexture(device);
1434     test_D3DXFilterTexture(device);
1435     test_D3DXFillTexture(device);
1436     test_D3DXFillCubeTexture(device);
1437     test_D3DXFillVolumeTexture(device);
1438     test_D3DXCreateTextureFromFileInMemory(device);
1439     test_D3DXCreateTextureFromFileInMemoryEx(device);
1440     test_D3DXCreateCubeTextureFromFileInMemory(device);
1441     test_D3DXCreateCubeTextureFromFileInMemoryEx(device);
1442     test_D3DXCreateVolumeTextureFromFileInMemory(device);
1443
1444     IDirect3DDevice9_Release(device);
1445     IDirect3D9_Release(d3d);
1446     DestroyWindow(wnd);
1447 }