1 /*
2  * Tests for the D3DX9 volume functions
3  *
4  * Copyright 2012 Józef Kucia
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include "d3dx9tex.h"
24 
25 /* 4x4x2 volume map dds, 2 mipmaps */
26 static const unsigned char dds_volume_map[] =
27 {
28     0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x8a,0x00,0x04,0x00,0x00,0x00,
29     0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
30     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
31     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
32     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
33     0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
34     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
35     0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
36     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
37     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
38     0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x2f,0x7e,0xcf,0x79,0x01,0x54,0x5c,0x5c,
39     0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x84,0xef,0x7b,0xaa,0xab,0xab,0xab
40 };
41 
42 #define check_pixel_4bpp(box, x, y, z, color) _check_pixel_4bpp(__LINE__, box, x, y, z, color)
43 static inline void _check_pixel_4bpp(unsigned int line, const D3DLOCKED_BOX *box, int x, int y, int z, DWORD expected_color)
44 {
45    DWORD color = ((DWORD *)box->pBits)[x + (y * box->RowPitch + z * box->SlicePitch) / 4];
46    ok_(__FILE__, line)(color == expected_color, "Got color 0x%08x, expected 0x%08x\n", color, expected_color);
47 }
48 
49 static inline void set_box(D3DBOX *box, UINT left, UINT top, UINT right, UINT bottom, UINT front, UINT back)
50 {
51     box->Left = left;
52     box->Top = top;
53     box->Right = right;
54     box->Bottom = bottom;
55     box->Front = front;
56     box->Back = back;
57 }
58 
59 static void test_D3DXLoadVolumeFromMemory(IDirect3DDevice9 *device)
60 {
61     int i, x, y, z;
62     HRESULT hr;
63     D3DBOX src_box, dst_box;
64     D3DLOCKED_BOX locked_box;
65     IDirect3DVolume9 *volume;
66     IDirect3DVolumeTexture9 *volume_texture;
67     const DWORD pixels[] = { 0xc3394cf0, 0x235ae892, 0x09b197fd, 0x8dc32bf6,
68                              0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
69                              0x00000000, 0x00000000, 0x00000000, 0xffffffff,
70                              0xffffffff, 0x00000000, 0xffffffff, 0x00000000 };
71 
72     hr = IDirect3DDevice9_CreateVolumeTexture(device, 256, 256, 4, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,
73             &volume_texture, NULL);
74     if (FAILED(hr))
75     {
76         skip("Failed to create volume texture.\n");
77         return;
78     }
79 
80     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
81 
82     set_box(&src_box, 0, 0, 4, 1, 0, 4);
83     set_box(&dst_box, 0, 0, 4, 1, 0, 4);
84 
85     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
86     if (FAILED(hr))
87     {
88         win_skip("D3DXLoadVolumeFromMemory failed with error %#x, skipping some tests.\n", hr);
89         return;
90     }
91 
92     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
93     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, 0, i / 4, pixels[i]);
94     IDirect3DVolume9_UnlockBox(volume);
95 
96     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_UNKNOWN, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
97     ok(hr == E_FAIL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
98 
99     hr = D3DXLoadVolumeFromMemory(volume, NULL, NULL, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
100     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
101 
102     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, NULL, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
103     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
104 
105     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, NULL, D3DX_DEFAULT, 0);
106     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
107 
108     set_box(&src_box, 0, 0, 4, 4, 0, 1);
109     set_box(&dst_box, 0, 0, 4, 4, 0, 1);
110     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, sizeof(pixels), NULL, &src_box, D3DX_DEFAULT, 0);
111     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
112 
113     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
114     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, i / 4, 0, pixels[i]);
115     IDirect3DVolume9_UnlockBox(volume);
116 
117     hr = D3DXLoadVolumeFromMemory(volume, NULL, NULL, pixels, D3DFMT_A8R8G8B8, 16, sizeof(pixels), NULL, &src_box, D3DX_FILTER_NONE, 0);
118     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
119 
120     IDirect3DVolume9_LockBox(volume, &locked_box, NULL, D3DLOCK_READONLY);
121     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, i / 4, 0, pixels[i]);
122     for (z = 0; z < 4; z++)
123     {
124         for (y = 0; y < 256; y++)
125         {
126             for (x = 0; x < 256; x++)
127                 if (z != 0 || y >= 4 || x >= 4) check_pixel_4bpp(&locked_box, x, y, z, 0);
128         }
129     }
130     IDirect3DVolume9_UnlockBox(volume);
131 
132     set_box(&src_box, 0, 0, 2, 2, 1, 2);
133     set_box(&dst_box, 0, 0, 2, 2, 0, 1);
134     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 8, 16, NULL, &src_box, D3DX_DEFAULT, 0);
135     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
136 
137     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
138     for (i = 0; i < 4; i++) check_pixel_4bpp(&locked_box, i % 2, i / 2, 0, pixels[i + 4]);
139     IDirect3DVolume9_UnlockBox(volume);
140 
141     set_box(&src_box, 0, 0, 2, 2, 2, 3);
142     set_box(&dst_box, 0, 0, 2, 2, 1, 2);
143     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 8, 16, NULL, &src_box, D3DX_DEFAULT, 0);
144     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
145 
146     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
147     for (i = 0; i < 4; i++) check_pixel_4bpp(&locked_box, i % 2, i / 2, 0, pixels[i + 8]);
148     IDirect3DVolume9_UnlockBox(volume);
149 
150     set_box(&src_box, 0, 0, 4, 1, 0, 4);
151 
152     set_box(&dst_box, -1, -1, 3, 0, 0, 4);
153     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
154     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
155 
156     set_box(&dst_box, 254, 254, 258, 255, 0, 4);
157     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
158     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
159 
160     set_box(&dst_box, 4, 1, 0, 0, 0, 4);
161     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
162     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
163 
164     set_box(&dst_box, 0, 0, 0, 0, 0, 0);
165     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
166     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
167 
168     set_box(&dst_box, 0, 0, 0, 0, 0, 1);
169     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
170     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
171 
172     set_box(&dst_box, 300, 300, 300, 300, 0, 0);
173     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
174     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
175 
176     IDirect3DVolume9_Release(volume);
177     IDirect3DVolumeTexture9_Release(volume_texture);
178 
179     hr = IDirect3DDevice9_CreateVolumeTexture(device, 256, 256, 4, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &volume_texture, NULL);
180     if (FAILED(hr))
181     {
182         skip("Failed to create volume texture\n");
183         return;
184     }
185 
186     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
187 
188     set_box(&src_box, 0, 0, 4, 1, 0, 4);
189     set_box(&dst_box, 0, 0, 4, 1, 0, 4);
190     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
191     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
192 
193     IDirect3DVolume9_Release(volume);
194     IDirect3DVolumeTexture9_Release(volume_texture);
195 
196     hr = IDirect3DDevice9_CreateVolumeTexture(device, 8, 8, 1, 1, D3DUSAGE_DYNAMIC, D3DFMT_DXT1, D3DPOOL_DEFAULT, &volume_texture, NULL);
197     if (FAILED(hr))
198     {
199         skip("Failed to create volume texture\n");
200         return;
201     }
202 
203     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
204 
205     set_box(&src_box, 1, 1, 7, 7, 0, 1);
206     set_box(&dst_box, 1, 1, 7, 7, 0, 1);
207     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 32, NULL, &src_box, D3DX_DEFAULT, 0);
208     todo_wine ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
209 
210     IDirect3DVolume9_Release(volume);
211     IDirect3DVolumeTexture9_Release(volume_texture);
212 }
213 
214 static void test_D3DXLoadVolumeFromFileInMemory(IDirect3DDevice9 *device)
215 {
216     HRESULT hr;
217     D3DBOX src_box;
218     IDirect3DVolume9 *volume;
219     IDirect3DVolumeTexture9 *volume_texture;
220 
221     hr = IDirect3DDevice9_CreateVolumeTexture(device, 4, 4, 2, 1, D3DUSAGE_DYNAMIC, D3DFMT_DXT3, D3DPOOL_DEFAULT,
222             &volume_texture, NULL);
223     if (FAILED(hr))
224     {
225         skip("Failed to create volume texture\n");
226         return;
227     }
228 
229     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
230 
231     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
232     ok(hr == D3D_OK, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
233 
234     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, NULL, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
235     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
236 
237     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
238     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
239 
240     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, 0, NULL, D3DX_DEFAULT, 0, NULL);
241     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
242 
243     hr = D3DXLoadVolumeFromFileInMemory(NULL, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
244     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
245 
246     set_box(&src_box, 0, 0, 4, 4, 0, 2);
247     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
248     ok(hr == D3D_OK, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
249 
250     set_box(&src_box, 0, 0, 0, 0, 0, 0);
251     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
252     ok(hr == E_FAIL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, E_FAIL);
253 
254     set_box(&src_box, 0, 0, 5, 4, 0, 2);
255     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
256     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
257 
258     set_box(&src_box, 0, 0, 4, 4, 0, 3);
259     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
260     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
261 
262     IDirect3DVolume9_Release(volume);
263     IDirect3DVolumeTexture9_Release(volume_texture);
264 }
265 
266 START_TEST(volume)
267 {
268     HWND wnd;
269     IDirect3D9 *d3d;
270     IDirect3DDevice9 *device;
271     D3DPRESENT_PARAMETERS d3dpp;
272     HRESULT hr;
273 
274     if (!(wnd = CreateWindowA("static", "d3dx9_test", WS_OVERLAPPEDWINDOW, 0, 0,
275             640, 480, NULL, NULL, NULL, NULL)))
276     {
277         skip("Couldn't create application window\n");
278         return;
279     }
280     d3d = Direct3DCreate9(D3D_SDK_VERSION);
281     if (!d3d)
282     {
283         skip("Couldn't create IDirect3D9 object\n");
284         DestroyWindow(wnd);
285         return;
286     }
287 
288     ZeroMemory(&d3dpp, sizeof(d3dpp));
289     d3dpp.Windowed   = TRUE;
290     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
291     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
292     if(FAILED(hr))
293     {
294         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
295         IDirect3D9_Release(d3d);
296         DestroyWindow(wnd);
297         return;
298     }
299 
300     test_D3DXLoadVolumeFromMemory(device);
301     test_D3DXLoadVolumeFromFileInMemory(device);
302 
303     IDirect3DDevice9_Release(device);
304     IDirect3D9_Release(d3d);
305     DestroyWindow(wnd);
306 }
307