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