d3dx9: Implement pixel format conversion for volume textures.
[wine] / dlls / d3dx9_36 / tests / volume.c
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     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
87
88     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
89     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, 0, i / 4, pixels[i]);
90     IDirect3DVolume9_UnlockBox(volume);
91
92     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_UNKNOWN, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
93     ok(hr == E_FAIL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
94
95     hr = D3DXLoadVolumeFromMemory(volume, NULL, NULL, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
96     todo_wine ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
97
98     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, NULL, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
99     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
100
101     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, NULL, D3DX_DEFAULT, 0);
102     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
103
104     set_box(&src_box, 0, 0, 4, 4, 0, 1);
105     set_box(&dst_box, 0, 0, 4, 4, 0, 1);
106     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, sizeof(pixels), NULL, &src_box, D3DX_DEFAULT, 0);
107     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
108
109     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
110     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, i / 4, 0, pixels[i]);
111     IDirect3DVolume9_UnlockBox(volume);
112
113     hr = D3DXLoadVolumeFromMemory(volume, NULL, NULL, pixels, D3DFMT_A8R8G8B8, 16, sizeof(pixels), NULL, &src_box, D3DX_FILTER_NONE, 0);
114     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
115
116     IDirect3DVolume9_LockBox(volume, &locked_box, NULL, D3DLOCK_READONLY);
117     for (i = 0; i < 16; i++) check_pixel_4bpp(&locked_box, i % 4, i / 4, 0, pixels[i]);
118     for (z = 0; z < 4; z++)
119     {
120         for (y = 0; y < 256; y++)
121         {
122             for (x = 0; x < 256; x++)
123                 if (z != 0 || y >= 4 || x >= 4) check_pixel_4bpp(&locked_box, x, y, z, 0);
124         }
125     }
126     IDirect3DVolume9_UnlockBox(volume);
127
128     set_box(&src_box, 0, 0, 2, 2, 1, 2);
129     set_box(&dst_box, 0, 0, 2, 2, 0, 1);
130     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 8, 16, NULL, &src_box, D3DX_DEFAULT, 0);
131     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
132
133     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
134     for (i = 0; i < 4; i++) check_pixel_4bpp(&locked_box, i % 2, i / 2, 0, pixels[i + 4]);
135     IDirect3DVolume9_UnlockBox(volume);
136
137     set_box(&src_box, 0, 0, 2, 2, 2, 3);
138     set_box(&dst_box, 0, 0, 2, 2, 1, 2);
139     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 8, 16, NULL, &src_box, D3DX_DEFAULT, 0);
140     ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
141
142     IDirect3DVolume9_LockBox(volume, &locked_box, &dst_box, D3DLOCK_READONLY);
143     for (i = 0; i < 4; i++) check_pixel_4bpp(&locked_box, i % 2, i / 2, 0, pixels[i + 8]);
144     IDirect3DVolume9_UnlockBox(volume);
145
146     set_box(&src_box, 0, 0, 4, 1, 0, 4);
147
148     set_box(&dst_box, -1, -1, 3, 0, 0, 4);
149     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
150     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
151
152     set_box(&dst_box, 254, 254, 258, 255, 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, 4, 1, 0, 0, 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, 0, 0, 0, 0, 0, 0);
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, 1);
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, 300, 300, 300, 300, 0, 0);
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     IDirect3DVolume9_Release(volume);
173     IDirect3DVolumeTexture9_Release(volume_texture);
174
175     hr = IDirect3DDevice9_CreateVolumeTexture(device, 256, 256, 4, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &volume_texture, NULL);
176     if (FAILED(hr))
177     {
178         skip("Failed to create volume texture\n");
179         return;
180     }
181
182     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
183
184     set_box(&src_box, 0, 0, 4, 1, 0, 4);
185     set_box(&dst_box, 0, 0, 4, 1, 0, 4);
186     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 16, NULL, &src_box, D3DX_DEFAULT, 0);
187     todo_wine ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
188
189     IDirect3DVolume9_Release(volume);
190     IDirect3DVolumeTexture9_Release(volume_texture);
191
192     hr = IDirect3DDevice9_CreateVolumeTexture(device, 8, 8, 1, 1, D3DUSAGE_DYNAMIC, D3DFMT_DXT1, D3DPOOL_DEFAULT, &volume_texture, NULL);
193     if (FAILED(hr))
194     {
195         skip("Failed to create volume texture\n");
196         return;
197     }
198
199     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
200
201     set_box(&src_box, 1, 1, 7, 7, 0, 1);
202     set_box(&dst_box, 1, 1, 7, 7, 0, 1);
203     hr = D3DXLoadVolumeFromMemory(volume, NULL, &dst_box, pixels, D3DFMT_A8R8G8B8, 16, 32, NULL, &src_box, D3DX_DEFAULT, 0);
204     todo_wine ok(hr == D3D_OK, "D3DXLoadVolumeFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
205
206     IDirect3DVolume9_Release(volume);
207     IDirect3DVolumeTexture9_Release(volume_texture);
208 }
209
210 static void test_D3DXLoadVolumeFromFileInMemory(IDirect3DDevice9 *device)
211 {
212     HRESULT hr;
213     D3DBOX src_box;
214     IDirect3DVolume9 *volume;
215     IDirect3DVolumeTexture9 *volume_texture;
216
217     hr = IDirect3DDevice9_CreateVolumeTexture(device, 4, 4, 2, 1, D3DUSAGE_DYNAMIC, D3DFMT_DXT3, D3DPOOL_DEFAULT,
218             &volume_texture, NULL);
219     if (FAILED(hr))
220     {
221         skip("Failed to create volume texture\n");
222         return;
223     }
224
225     IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, 0, &volume);
226
227     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
228     ok(hr == D3D_OK, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
229
230     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, NULL, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
231     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
232
233     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
234     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
235
236     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, 0, NULL, D3DX_DEFAULT, 0, NULL);
237     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
238
239     hr = D3DXLoadVolumeFromFileInMemory(NULL, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), NULL, D3DX_DEFAULT, 0, NULL);
240     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
241
242     set_box(&src_box, 0, 0, 4, 4, 0, 2);
243     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
244     ok(hr == D3D_OK, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
245
246     set_box(&src_box, 0, 0, 0, 0, 0, 0);
247     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
248     ok(hr == E_FAIL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, E_FAIL);
249
250     set_box(&src_box, 0, 0, 5, 4, 0, 2);
251     hr = D3DXLoadVolumeFromFileInMemory(volume, NULL, NULL, dds_volume_map, sizeof(dds_volume_map), &src_box, D3DX_DEFAULT, 0, NULL);
252     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadVolumeFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
253
254     set_box(&src_box, 0, 0, 4, 4, 0, 3);
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     IDirect3DVolume9_Release(volume);
259     IDirect3DVolumeTexture9_Release(volume_texture);
260 }
261
262 START_TEST(volume)
263 {
264     HWND wnd;
265     IDirect3D9 *d3d;
266     IDirect3DDevice9 *device;
267     D3DPRESENT_PARAMETERS d3dpp;
268     HRESULT hr;
269
270     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
271     if (!wnd)
272     {
273         skip("Couldn't create application window\n");
274         return;
275     }
276     d3d = Direct3DCreate9(D3D_SDK_VERSION);
277     if (!d3d)
278     {
279         skip("Couldn't create IDirect3D9 object\n");
280         DestroyWindow(wnd);
281         return;
282     }
283
284     ZeroMemory(&d3dpp, sizeof(d3dpp));
285     d3dpp.Windowed   = TRUE;
286     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
287     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
288     if(FAILED(hr))
289     {
290         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
291         IDirect3D9_Release(d3d);
292         DestroyWindow(wnd);
293         return;
294     }
295
296     test_D3DXLoadVolumeFromMemory(device);
297     test_D3DXLoadVolumeFromFileInMemory(device);
298
299     IDirect3DDevice9_Release(device);
300     IDirect3D9_Release(d3d);
301     DestroyWindow(wnd);
302 }