d3d9: Fix the shader constant test.
[wine] / dlls / d3d9 / tests / surface.c
1 /*
2  * Copyright 2006-2007 Henri Verbeet
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #define COBJMACROS
19 #include <d3d9.h>
20 #include "wine/test.h"
21
22 static HWND create_window(void)
23 {
24     WNDCLASS wc = {0};
25     wc.lpfnWndProc = DefWindowProc;
26     wc.lpszClassName = "d3d9_test_wc";
27     RegisterClass(&wc);
28
29     return CreateWindow("d3d9_test_wc", "d3d9_test",
30             0, 0, 0, 0, 0, 0, 0, 0, 0);
31 }
32
33 static IDirect3DDevice9 *init_d3d9(HMODULE d3d9_handle)
34 {
35     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
36     IDirect3D9 *d3d9_ptr = 0;
37     IDirect3DDevice9 *device_ptr = 0;
38     D3DPRESENT_PARAMETERS present_parameters;
39     HRESULT hr;
40
41     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
42     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
43     if (!d3d9_create) return NULL;
44
45     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
46     if (!d3d9_ptr)
47     {
48         skip("could not create D3D9\n");
49         return NULL;
50     }
51
52     ZeroMemory(&present_parameters, sizeof(present_parameters));
53     present_parameters.Windowed = TRUE;
54     present_parameters.hDeviceWindow = create_window();
55     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
56
57     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
58             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
59
60     if(FAILED(hr))
61     {
62         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
63         return NULL;
64     }
65
66     return device_ptr;
67 }
68
69 static void test_surface_get_container(IDirect3DDevice9 *device_ptr)
70 {
71     IDirect3DTexture9 *texture_ptr = 0;
72     IDirect3DSurface9 *surface_ptr = 0;
73     void *container_ptr;
74     HRESULT hr;
75
76     hr = IDirect3DDevice9_CreateTexture(device_ptr, 128, 128, 1, 0,
77             D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr, 0);
78     ok(SUCCEEDED(hr) && texture_ptr != NULL, "CreateTexture returned: hr %#x, texture_ptr %p. "
79         "Expected hr %#x, texture_ptr != %p\n", hr, texture_ptr, D3D_OK, NULL);
80     if (!texture_ptr || FAILED(hr)) goto cleanup;
81
82     hr = IDirect3DTexture9_GetSurfaceLevel(texture_ptr, 0, &surface_ptr);
83     ok(SUCCEEDED(hr) && surface_ptr != NULL, "GetSurfaceLevel returned: hr %#x, surface_ptr %p. "
84         "Expected hr %#x, surface_ptr != %p\n", hr, surface_ptr, D3D_OK, NULL);
85     if (!surface_ptr || FAILED(hr)) goto cleanup;
86
87     /* These should work... */
88     container_ptr = (void *)0x1337c0d3;
89     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IUnknown, &container_ptr);
90     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
91         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
92     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
93
94     container_ptr = (void *)0x1337c0d3;
95     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DResource9, &container_ptr);
96     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
97         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
98     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
99
100     container_ptr = (void *)0x1337c0d3;
101     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture9, &container_ptr);
102     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
103         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
104     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
105
106     container_ptr = (void *)0x1337c0d3;
107     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DTexture9, &container_ptr);
108     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
109         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
110     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
111
112     /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
113     container_ptr = (void *)0x1337c0d3;
114     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DSurface9, &container_ptr);
115     ok(hr == E_NOINTERFACE && container_ptr == NULL, "GetContainer returned: hr %#x, container_ptr %p. "
116         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, E_NOINTERFACE, NULL);
117     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
118
119 cleanup:
120     if (texture_ptr) IDirect3DTexture9_Release(texture_ptr);
121     if (surface_ptr) IDirect3DSurface9_Release(surface_ptr);
122 }
123
124 static void test_surface_alignment(IDirect3DDevice9 *device_ptr)
125 {
126     IDirect3DSurface9 *surface_ptr = 0;
127     HRESULT hr;
128     int i;
129
130     /* Test a sysmem surface as those aren't affected by the hardware's np2 restrictions */
131     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 5, 5, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface_ptr, 0);
132     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
133
134     if(surface_ptr)
135     {
136         D3DLOCKED_RECT lockedRect;
137         hr = IDirect3DSurface9_LockRect(surface_ptr, &lockedRect, NULL, 0);
138         ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
139         ok(!(lockedRect.Pitch & 3), "Surface pitch %d is not 32-bit aligned\n", lockedRect.Pitch);
140         /* Some applications also depend on the exact pitch, rather than just
141          * the alignment.
142          */
143         ok(lockedRect.Pitch == 12, "Got pitch %d, expected 12\n", lockedRect.Pitch);
144         hr = IDirect3DSurface9_UnlockRect(surface_ptr);
145         IDirect3DSurface9_Release(surface_ptr);
146     }
147
148     for (i = 0; i < 5; i++)
149     {
150         IDirect3DTexture9 *pTexture;
151         int j, pitch;
152
153         hr = IDirect3DDevice9_CreateTexture(device_ptr, 64, 64, 0, 0, MAKEFOURCC('D', 'X', 'T', '1'+i),
154                                             D3DPOOL_MANAGED, &pTexture, NULL);
155         ok(SUCCEEDED(hr) || hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_CreateTexture: %08x\n", hr);
156         if (FAILED(hr)) {
157             skip("DXT%d surfaces are not supported\n", i + 1);
158             continue;
159         }
160
161         for (j = IDirect3DBaseTexture9_GetLevelCount(pTexture) - 1; j >= 0; j--)
162         {
163             D3DLOCKED_RECT rc;
164             D3DSURFACE_DESC descr;
165
166             IDirect3DTexture9_GetLevelDesc(pTexture, j, &descr);
167             hr = IDirect3DTexture9_LockRect(pTexture, j, &rc, NULL, 0);
168             ok(SUCCEEDED(hr), "IDirect3DTexture9_LockRect: %08x\n", hr);
169             IDirect3DTexture9_UnlockRect(pTexture, j);
170
171             pitch = ((descr.Width + 3) >> 2) << 3;
172             if (i > 0) pitch <<= 1;
173             ok(rc.Pitch == pitch, "Wrong pitch for DXT%d lvl[%d (%dx%d)]: expected %d got %d\n",
174                i + 1, j, descr.Width, descr.Height, pitch, rc.Pitch);
175         }
176         IUnknown_Release( pTexture );
177     }
178 }
179
180 /* Since the DXT formats are based on 4x4 blocks, locking works slightly
181  * different than with regular formats. This patch verifies we return the
182  * correct memory offsets */
183 static void test_lockrect_offset(IDirect3DDevice9 *device)
184 {
185     IDirect3DSurface9 *surface = 0;
186     const RECT rect = {60, 60, 68, 68};
187     D3DLOCKED_RECT locked_rect;
188     unsigned int expected_offset;
189     unsigned int offset;
190     unsigned int i;
191     BYTE *base;
192     HRESULT hr;
193
194     const struct {
195         D3DFORMAT fmt;
196         const char *name;
197         unsigned int block_width;
198         unsigned int block_height;
199         unsigned int block_size;
200     } dxt_formats[] = {
201         {D3DFMT_DXT1, "D3DFMT_DXT1", 4, 4, 8},
202         {D3DFMT_DXT2, "D3DFMT_DXT2", 4, 4, 16},
203         {D3DFMT_DXT3, "D3DFMT_DXT3", 4, 4, 16},
204         {D3DFMT_DXT4, "D3DFMT_DXT4", 4, 4, 16},
205         {D3DFMT_DXT5, "D3DFMT_DXT5", 4, 4, 16},
206     };
207
208     for (i = 0; i < (sizeof(dxt_formats) / sizeof(*dxt_formats)); ++i) {
209         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, dxt_formats[i].fmt, D3DPOOL_SCRATCH, &surface, 0);
210         ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (%08x)\n", hr);
211
212         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
213         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
214
215         base = locked_rect.pBits;
216
217         hr = IDirect3DSurface9_UnlockRect(surface);
218         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
219
220         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &rect, 0);
221         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
222
223         offset = (BYTE *)locked_rect.pBits - base;
224         expected_offset = (rect.top / dxt_formats[i].block_height) * locked_rect.Pitch
225                         + (rect.left / dxt_formats[i].block_width) * dxt_formats[i].block_size;
226         ok(offset == expected_offset, "Got offset %u, expected offset %u for format %s\n", offset, expected_offset, dxt_formats[i].name);
227
228         hr = IDirect3DSurface9_UnlockRect(surface);
229         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
230
231         IDirect3DSurface9_Release(surface);
232     }
233 }
234
235 static void test_lockrect_invalid(IDirect3DDevice9 *device)
236 {
237     IDirect3DSurface9 *surface = 0;
238     D3DLOCKED_RECT locked_rect;
239     unsigned int i;
240     BYTE *base;
241     HRESULT hr;
242
243     const RECT test_rect_2 = { 0, 0, 8, 8 };
244     const RECT test_data[] = {
245         {60, 60, 68, 68},       /* Valid */
246         {60, 60, 60, 68},       /* 0 height */
247         {60, 60, 68, 60},       /* 0 width */
248         {68, 60, 60, 68},       /* left > right */
249         {60, 68, 68, 60},       /* top > bottom */
250         {-8, 60,  0, 68},       /* left < surface */
251         {60, -8, 68,  0},       /* top < surface */
252         {-16, 60, -8, 68},      /* right < surface */
253         {60, -16, 68, -8},      /* bottom < surface */
254         {60, 60, 136, 68},      /* right > surface */
255         {60, 60, 68, 136},      /* bottom > surface */
256         {136, 60, 144, 68},     /* left > surface */
257         {60, 136, 68, 144},     /* top > surface */
258     };
259
260     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
261     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
262
263     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
264     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
265
266     base = locked_rect.pBits;
267
268     hr = IDirect3DSurface9_UnlockRect(surface);
269     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
270
271     for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); ++i)
272     {
273         unsigned int offset, expected_offset;
274         const RECT *rect = &test_data[i];
275
276         locked_rect.pBits = (BYTE *)0xdeadbeef;
277         locked_rect.Pitch = 0xdeadbeef;
278
279         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, rect, 0);
280         ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
281                 hr, rect->left, rect->top, rect->right, rect->bottom);
282
283         offset = (BYTE *)locked_rect.pBits - base;
284         expected_offset = rect->top * locked_rect.Pitch + rect->left * 4;
285         ok(offset == expected_offset, "Got offset %u, expected offset %u for rect [%d, %d]->[%d, %d]\n",
286                 offset, expected_offset, rect->left, rect->top, rect->right, rect->bottom);
287
288         hr = IDirect3DSurface9_UnlockRect(surface);
289         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
290     }
291
292     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
293     ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect NULL\n", hr);
294     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
295     ok(hr == D3DERR_INVALIDCALL, "Double LockRect for rect NULL returned 0x%08x\n", hr);
296     hr = IDirect3DSurface9_UnlockRect(surface);
297     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
298
299     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0], 0);
300     ok(hr == D3D_OK, "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
301             ", expected D3D_OK (0x%08x)\n", hr, test_data[0].left, test_data[0].top,
302             test_data[0].right, test_data[0].bottom, D3D_OK);
303     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0], 0);
304     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
305             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_data[0].left, test_data[0].top,
306             test_data[0].right, test_data[0].bottom, D3DERR_INVALIDCALL);
307     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_rect_2, 0);
308     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
309             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_rect_2.left, test_rect_2.top,
310             test_rect_2.right, test_rect_2.bottom, D3DERR_INVALIDCALL);
311     hr = IDirect3DSurface9_UnlockRect(surface);
312     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
313
314     IDirect3DSurface9_Release(surface);
315 }
316
317 static unsigned long getref(IUnknown *iface)
318 {
319     IUnknown_AddRef(iface);
320     return IUnknown_Release(iface);
321 }
322
323 static void test_private_data(IDirect3DDevice9 *device)
324 {
325     HRESULT hr;
326     IDirect3DSurface9 *surface;
327     ULONG ref, ref2;
328     IUnknown *ptr;
329     DWORD size = sizeof(IUnknown *);
330
331     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
332     ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
333     if(!surface)
334     {
335         return;
336     }
337
338     /* This fails */
339     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 0, D3DSPD_IUNKNOWN);
340     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
341     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 5, D3DSPD_IUNKNOWN);
342     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
343     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *) * 2, D3DSPD_IUNKNOWN);
344     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
345
346     ref = getref((IUnknown *) device);
347     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
348     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
349     ref2 = getref((IUnknown *) device);
350     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
351     hr = IDirect3DSurface9_FreePrivateData(surface, &IID_IDirect3DSurface9);
352     ok(hr == D3D_OK, "IDirect3DSurface9_FreePrivateData returned %08x\n", hr);
353     ref2 = getref((IUnknown *) device);
354     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
355
356     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
357     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
358     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, surface, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
359     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
360     ref2 = getref((IUnknown *) device);
361     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
362
363     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
364     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
365     hr = IDirect3DSurface9_GetPrivateData(surface, &IID_IDirect3DSurface9, &ptr, &size);
366     ok(hr == D3D_OK, "IDirect3DSurface9_GetPrivateData failed with %08x\n", hr);
367     ref2 = getref((IUnknown *) device);
368     /* Object is NOT being addrefed */
369     ok(ptr == (IUnknown *) device, "Returned interface pointer is %p, expected %p\n", ptr, device);
370     ok(ref2 == ref + 2, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 2, ptr, device);
371     IUnknown_Release(ptr);
372
373     IDirect3DSurface9_Release(surface);
374
375     /* Destroying the surface frees the held reference */
376     ref2 = getref((IUnknown *) device);
377     /* -1 because the surface was released and held a reference before */
378     ok(ref2 == (ref - 1), "Object reference is %d, expected %d\n", ref2, (ref - 1));
379 }
380
381 START_TEST(surface)
382 {
383     HMODULE d3d9_handle;
384     IDirect3DDevice9 *device_ptr;
385
386     d3d9_handle = LoadLibraryA("d3d9.dll");
387     if (!d3d9_handle)
388     {
389         skip("Could not load d3d9.dll\n");
390         return;
391     }
392
393     device_ptr = init_d3d9(d3d9_handle);
394     if (!device_ptr) return;
395
396     test_surface_get_container(device_ptr);
397     test_surface_alignment(device_ptr);
398     test_lockrect_offset(device_ptr);
399     test_lockrect_invalid(device_ptr);
400     test_private_data(device_ptr);
401 }