wined3d: Don't clamp vertex depth values.
[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     present_parameters.EnableAutoDepthStencil = TRUE;
57     present_parameters.AutoDepthStencilFormat = D3DFMT_D24S8;
58
59     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
60             NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
61
62     if(FAILED(hr))
63     {
64         skip("could not create device, IDirect3D9_CreateDevice returned %#x\n", hr);
65         return NULL;
66     }
67
68     return device_ptr;
69 }
70
71 static void test_surface_get_container(IDirect3DDevice9 *device_ptr)
72 {
73     IDirect3DTexture9 *texture_ptr = 0;
74     IDirect3DSurface9 *surface_ptr = 0;
75     void *container_ptr;
76     HRESULT hr;
77
78     hr = IDirect3DDevice9_CreateTexture(device_ptr, 128, 128, 1, 0,
79             D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr, 0);
80     ok(SUCCEEDED(hr) && texture_ptr != NULL, "CreateTexture returned: hr %#x, texture_ptr %p. "
81         "Expected hr %#x, texture_ptr != %p\n", hr, texture_ptr, D3D_OK, NULL);
82     if (!texture_ptr || FAILED(hr)) goto cleanup;
83
84     hr = IDirect3DTexture9_GetSurfaceLevel(texture_ptr, 0, &surface_ptr);
85     ok(SUCCEEDED(hr) && surface_ptr != NULL, "GetSurfaceLevel returned: hr %#x, surface_ptr %p. "
86         "Expected hr %#x, surface_ptr != %p\n", hr, surface_ptr, D3D_OK, NULL);
87     if (!surface_ptr || FAILED(hr)) goto cleanup;
88
89     /* These should work... */
90     container_ptr = (void *)0x1337c0d3;
91     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IUnknown, &container_ptr);
92     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
93         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
94     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
95
96     container_ptr = (void *)0x1337c0d3;
97     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DResource9, &container_ptr);
98     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
99         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
100     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
101
102     container_ptr = (void *)0x1337c0d3;
103     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture9, &container_ptr);
104     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
105         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
106     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
107
108     container_ptr = (void *)0x1337c0d3;
109     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DTexture9, &container_ptr);
110     ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
111         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
112     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
113
114     /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
115     container_ptr = (void *)0x1337c0d3;
116     hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DSurface9, &container_ptr);
117     ok(hr == E_NOINTERFACE && container_ptr == NULL, "GetContainer returned: hr %#x, container_ptr %p. "
118         "Expected hr %#x, container_ptr %p\n", hr, container_ptr, E_NOINTERFACE, NULL);
119     if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
120
121 cleanup:
122     if (texture_ptr) IDirect3DTexture9_Release(texture_ptr);
123     if (surface_ptr) IDirect3DSurface9_Release(surface_ptr);
124 }
125
126 static void test_surface_alignment(IDirect3DDevice9 *device_ptr)
127 {
128     IDirect3DSurface9 *surface_ptr = 0;
129     HRESULT hr;
130     int i;
131
132     /* Test a sysmem surface as those aren't affected by the hardware's np2 restrictions */
133     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 5, 5, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface_ptr, 0);
134     ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
135
136     if(surface_ptr)
137     {
138         D3DLOCKED_RECT lockedRect;
139         hr = IDirect3DSurface9_LockRect(surface_ptr, &lockedRect, NULL, 0);
140         ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
141         ok(!(lockedRect.Pitch & 3), "Surface pitch %d is not 32-bit aligned\n", lockedRect.Pitch);
142         /* Some applications also depend on the exact pitch, rather than just
143          * the alignment.
144          */
145         ok(lockedRect.Pitch == 12, "Got pitch %d, expected 12\n", lockedRect.Pitch);
146         hr = IDirect3DSurface9_UnlockRect(surface_ptr);
147         ok(SUCCEEDED(hr), "IDirect3DSurface9_UnlockRect returned %#x.\n", hr);
148         IDirect3DSurface9_Release(surface_ptr);
149     }
150
151     for (i = 0; i < 5; i++)
152     {
153         IDirect3DTexture9 *pTexture;
154         int j, pitch;
155
156         hr = IDirect3DDevice9_CreateTexture(device_ptr, 64, 64, 0, 0, MAKEFOURCC('D', 'X', 'T', '1'+i),
157                                             D3DPOOL_MANAGED, &pTexture, NULL);
158         ok(SUCCEEDED(hr) || hr == D3DERR_INVALIDCALL, "IDirect3DDevice9_CreateTexture: %08x\n", hr);
159         if (FAILED(hr)) {
160             skip("DXT%d surfaces are not supported\n", i + 1);
161             continue;
162         }
163
164         for (j = IDirect3DBaseTexture9_GetLevelCount(pTexture) - 1; j >= 0; j--)
165         {
166             D3DLOCKED_RECT rc;
167             D3DSURFACE_DESC descr;
168
169             IDirect3DTexture9_GetLevelDesc(pTexture, j, &descr);
170             hr = IDirect3DTexture9_LockRect(pTexture, j, &rc, NULL, 0);
171             ok(SUCCEEDED(hr), "IDirect3DTexture9_LockRect: %08x\n", hr);
172             hr = IDirect3DTexture9_UnlockRect(pTexture, j);
173             ok(SUCCEEDED(hr), "IDirect3DTexture9_UnLockRect: %08x\n", hr);
174             /* Windows XP returns D3D_OK when calling UnlockRect on an unlocked surface,
175              * windows 7 returns an error.
176              */
177
178             pitch = ((descr.Width + 3) >> 2) << 3;
179             if (i > 0) pitch <<= 1;
180             ok(rc.Pitch == pitch, "Wrong pitch for DXT%d lvl[%d (%dx%d)]: expected %d got %d\n",
181                i + 1, j, descr.Width, descr.Height, pitch, rc.Pitch);
182         }
183         IUnknown_Release( pTexture );
184     }
185 }
186
187 /* Since the DXT formats are based on 4x4 blocks, locking works slightly
188  * different than with regular formats. This patch verifies we return the
189  * correct memory offsets */
190 static void test_lockrect_offset(IDirect3DDevice9 *device)
191 {
192     IDirect3DSurface9 *surface = 0;
193     IDirect3D9 *d3d;
194     const RECT rect = {60, 60, 68, 68};
195     D3DLOCKED_RECT locked_rect;
196     int expected_pitch;
197     unsigned int expected_offset;
198     unsigned int offset;
199     unsigned int i;
200     BYTE *base;
201     HRESULT hr;
202
203     const struct {
204         D3DFORMAT fmt;
205         const char *name;
206         unsigned int block_width;
207         unsigned int block_height;
208         unsigned int block_size;
209     } dxt_formats[] = {
210         {D3DFMT_DXT1,                 "D3DFMT_DXT1", 4, 4, 8},
211         {D3DFMT_DXT2,                 "D3DFMT_DXT2", 4, 4, 16},
212         {D3DFMT_DXT3,                 "D3DFMT_DXT3", 4, 4, 16},
213         {D3DFMT_DXT4,                 "D3DFMT_DXT4", 4, 4, 16},
214         {D3DFMT_DXT5,                 "D3DFMT_DXT5", 4, 4, 16},
215         {MAKEFOURCC('A','T','I','2'), "ATI2N",       1, 1,  1},
216     };
217     hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
218     ok(SUCCEEDED(hr), "IDirect3DDevice9_GetDirect3D failed (%08x)\n", hr);
219
220     for (i = 0; i < (sizeof(dxt_formats) / sizeof(*dxt_formats)); ++i) {
221         hr = IDirect3D9_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, 0, D3DRTYPE_TEXTURE, dxt_formats[i].fmt);
222         if(FAILED(hr))
223         {
224             skip("Format %s not supported, skipping lockrect offset test\n", dxt_formats[i].name);
225             continue;
226         }
227
228         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, dxt_formats[i].fmt, D3DPOOL_SCRATCH, &surface, 0);
229         ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (%08x)\n", hr);
230
231         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
232         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
233
234         base = locked_rect.pBits;
235         expected_pitch = (128 + dxt_formats[i].block_height - 1) / dxt_formats[i].block_width
236                          * dxt_formats[i].block_size;
237         ok(locked_rect.Pitch == expected_pitch, "Got pitch %d, expected pitch %d for format %s\n", locked_rect.Pitch, expected_pitch, dxt_formats[i].name);
238
239         hr = IDirect3DSurface9_UnlockRect(surface);
240         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
241         hr = IDirect3DSurface9_UnlockRect(surface);
242         ok(hr == D3DERR_INVALIDCALL, "Double UnlockRect returned %08x, expected D3DERR_INVALIDCALL\n", hr);
243
244         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &rect, 0);
245         ok(SUCCEEDED(hr), "LockRect failed (%08x)\n", hr);
246
247         offset = (BYTE *)locked_rect.pBits - base;
248         expected_offset = (rect.top / dxt_formats[i].block_height) * expected_pitch
249                         + (rect.left / dxt_formats[i].block_width) * dxt_formats[i].block_size;
250         ok(offset == expected_offset, "Got offset %u, expected offset %u for format %s\n", offset, expected_offset, dxt_formats[i].name);
251
252         hr = IDirect3DSurface9_UnlockRect(surface);
253         ok(SUCCEEDED(hr), "UnlockRect failed (%08x)\n", hr);
254
255         IDirect3DSurface9_Release(surface);
256     }
257     IDirect3D9_Release(d3d);
258 }
259
260 struct rect_test
261 {
262     RECT rect;
263     HRESULT win7_result;
264 };
265
266 static void test_lockrect_invalid(IDirect3DDevice9 *device)
267 {
268     IDirect3DSurface9 *surface = 0;
269     D3DLOCKED_RECT locked_rect;
270     unsigned int i;
271     BYTE *base;
272     HRESULT hr;
273
274     const RECT test_rect_2 = { 0, 0, 8, 8 };
275     const struct rect_test test_data[] = {
276         {{60, 60, 68, 68},      D3D_OK},                /* Valid */
277         {{60, 60, 60, 68},      D3DERR_INVALIDCALL},    /* 0 height */
278         {{60, 60, 68, 60},      D3DERR_INVALIDCALL},    /* 0 width */
279         {{68, 60, 60, 68},      D3DERR_INVALIDCALL},    /* left > right */
280         {{60, 68, 68, 60},      D3DERR_INVALIDCALL},    /* top > bottom */
281         {{-8, 60,  0, 68},      D3DERR_INVALIDCALL},    /* left < surface */
282         {{60, -8, 68,  0},      D3DERR_INVALIDCALL},    /* top < surface */
283         {{-16, 60, -8, 68},     D3DERR_INVALIDCALL},    /* right < surface */
284         {{60, -16, 68, -8},     D3DERR_INVALIDCALL},    /* bottom < surface */
285         {{60, 60, 136, 68},     D3DERR_INVALIDCALL},    /* right > surface */
286         {{60, 60, 68, 136},     D3DERR_INVALIDCALL},    /* bottom > surface */
287         {{136, 60, 144, 68},    D3DERR_INVALIDCALL},    /* left > surface */
288         {{60, 136, 68, 144},    D3DERR_INVALIDCALL},    /* top > surface */
289     };
290
291     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 128, 128, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
292     ok(SUCCEEDED(hr), "CreateOffscreenPlainSurface failed (0x%08x)\n", hr);
293
294     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
295     ok(SUCCEEDED(hr), "LockRect failed (0x%08x)\n", hr);
296
297     base = locked_rect.pBits;
298
299     hr = IDirect3DSurface9_UnlockRect(surface);
300     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
301
302     for (i = 0; i < (sizeof(test_data) / sizeof(*test_data)); ++i)
303     {
304         unsigned int offset, expected_offset;
305         const RECT *rect = &test_data[i].rect;
306
307         locked_rect.pBits = (BYTE *)0xdeadbeef;
308         locked_rect.Pitch = 0xdeadbeef;
309
310         hr = IDirect3DSurface9_LockRect(surface, &locked_rect, rect, 0);
311         /* Windows XP accepts invalid locking rectangles, windows 7 rejects them.
312          * Some games(C&C3) depend on the XP behavior, mark the Win7 one broken */
313         ok(SUCCEEDED(hr) || broken(hr == test_data[i].win7_result),
314                 "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]\n",
315                 hr, rect->left, rect->top, rect->right, rect->bottom);
316         if(FAILED(hr)) continue;
317
318         offset = (BYTE *)locked_rect.pBits - base;
319         expected_offset = rect->top * locked_rect.Pitch + rect->left * 4;
320         ok(offset == expected_offset, "Got offset %u, expected offset %u for rect [%d, %d]->[%d, %d]\n",
321                 offset, expected_offset, rect->left, rect->top, rect->right, rect->bottom);
322
323         hr = IDirect3DSurface9_UnlockRect(surface);
324         ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
325     }
326
327     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
328     ok(SUCCEEDED(hr), "LockRect failed (0x%08x) for rect NULL\n", hr);
329     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
330     ok(hr == D3DERR_INVALIDCALL, "Double LockRect for rect NULL returned 0x%08x\n", hr);
331     hr = IDirect3DSurface9_UnlockRect(surface);
332     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
333
334     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0].rect, 0);
335     ok(hr == D3D_OK, "LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
336             ", expected D3D_OK (0x%08x)\n", hr, test_data[0].rect.left, test_data[0].rect.top,
337             test_data[0].rect.right, test_data[0].rect.bottom, D3D_OK);
338     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_data[0].rect, 0);
339     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
340             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_data[0].rect.left, test_data[0].rect.top,
341             test_data[0].rect.right, test_data[0].rect.bottom, D3DERR_INVALIDCALL);
342     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, &test_rect_2, 0);
343     ok(hr == D3DERR_INVALIDCALL, "Double LockRect failed (0x%08x) for rect [%d, %d]->[%d, %d]"
344             ", expected D3DERR_INVALIDCALL (0x%08x)\n", hr, test_rect_2.left, test_rect_2.top,
345             test_rect_2.right, test_rect_2.bottom, D3DERR_INVALIDCALL);
346     hr = IDirect3DSurface9_UnlockRect(surface);
347     ok(SUCCEEDED(hr), "UnlockRect failed (0x%08x)\n", hr);
348
349     IDirect3DSurface9_Release(surface);
350 }
351
352 static ULONG getref(IUnknown *iface)
353 {
354     IUnknown_AddRef(iface);
355     return IUnknown_Release(iface);
356 }
357
358 static void test_private_data(IDirect3DDevice9 *device)
359 {
360     HRESULT hr;
361     IDirect3DSurface9 *surface;
362     ULONG ref, ref2;
363     IUnknown *ptr;
364     DWORD size = sizeof(IUnknown *);
365
366     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, 0);
367     ok(SUCCEEDED(hr), "CreateImageSurface failed (0x%08x)\n", hr);
368     if(!surface)
369     {
370         return;
371     }
372
373     /* This fails */
374     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 0, D3DSPD_IUNKNOWN);
375     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
376     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, 5, D3DSPD_IUNKNOWN);
377     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
378     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *) * 2, D3DSPD_IUNKNOWN);
379     ok(hr == D3DERR_INVALIDCALL, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
380
381     ref = getref((IUnknown *) device);
382     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9 /* Abuse this tag */, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
383     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
384     ref2 = getref((IUnknown *) device);
385     ok(ref2 == ref + 1, "Object reference is %d, expected %d\n", ref2, ref + 1);
386     hr = IDirect3DSurface9_FreePrivateData(surface, &IID_IDirect3DSurface9);
387     ok(hr == D3D_OK, "IDirect3DSurface9_FreePrivateData returned %08x\n", hr);
388     ref2 = getref((IUnknown *) device);
389     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
390
391     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
392     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
393     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, surface, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
394     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
395     ref2 = getref((IUnknown *) device);
396     ok(ref2 == ref, "Object reference is %d, expected %d\n", ref2, ref);
397
398     hr = IDirect3DSurface9_SetPrivateData(surface, &IID_IDirect3DSurface9, device, sizeof(IUnknown *), D3DSPD_IUNKNOWN);
399     ok(hr == D3D_OK, "IDirect3DSurface9_SetPrivateData failed with %08x\n", hr);
400     hr = IDirect3DSurface9_GetPrivateData(surface, &IID_IDirect3DSurface9, &ptr, &size);
401     ok(hr == D3D_OK, "IDirect3DSurface9_GetPrivateData failed with %08x\n", hr);
402     ref2 = getref((IUnknown *) device);
403     /* Object is addrefed */
404     ok(ptr == (IUnknown *) device, "Returned interface pointer is %p, expected %p\n", ptr, device);
405     ok(ref2 == ref + 2, "Object reference is %d, expected %d. ptr at %p, orig at %p\n", ref2, ref + 2, ptr, device);
406     IUnknown_Release(ptr);
407
408     IDirect3DSurface9_Release(surface);
409
410     /* Destroying the surface frees the held reference */
411     ref2 = getref((IUnknown *) device);
412     /* -1 because the surface was released and held a reference before */
413     ok(ref2 == (ref - 1), "Object reference is %d, expected %d\n", ref2, (ref - 1));
414 }
415
416 static void test_getdc(IDirect3DDevice9 *device)
417 {
418     IDirect3DSurface9 *surface;
419     IDirect3DTexture9 *texture;
420     HRESULT hr;
421     unsigned int i;
422     HDC dc;
423
424     struct
425     {
426         const char *name;
427         D3DFORMAT fmt;
428         BOOL getdc_supported;
429     } testdata[] = {
430         { "D3DFMT_A8R8G8B8",    D3DFMT_A8R8G8B8,    TRUE    },
431         { "D3DFMT_X8R8G8B8",    D3DFMT_X8R8G8B8,    TRUE    },
432         { "D3DFMT_R5G6B5",      D3DFMT_R5G6B5,      TRUE    },
433         { "D3DFMT_X1R5G5B5",    D3DFMT_X1R5G5B5,    TRUE    },
434         { "D3DFMT_A1R5G5B5",    D3DFMT_A1R5G5B5,    TRUE    },
435         { "D3DFMT_R8G8B8",      D3DFMT_R8G8B8,      TRUE    },
436         { "D3DFMT_A2R10G10B10", D3DFMT_A2R10G10B10, FALSE   }, /* Untested, card on windows didn't support it */
437         { "D3DFMT_V8U8",        D3DFMT_V8U8,        FALSE   },
438         { "D3DFMT_Q8W8V8U8",    D3DFMT_Q8W8V8U8,    FALSE   },
439         { "D3DFMT_A8B8G8R8",    D3DFMT_A8B8G8R8,    FALSE   },
440         { "D3DFMT_X8B8G8R8",    D3DFMT_A8B8G8R8,    FALSE   },
441         { "D3DFMT_R3G3B2",      D3DFMT_R3G3B2,      FALSE   },
442         { "D3DFMT_P8",          D3DFMT_P8,          FALSE   },
443         { "D3DFMT_L8",          D3DFMT_L8,          FALSE   },
444         { "D3DFMT_A8L8",        D3DFMT_A8L8,        FALSE   },
445         { "D3DFMT_DXT1",        D3DFMT_DXT1,        FALSE   },
446         { "D3DFMT_DXT2",        D3DFMT_DXT2,        FALSE   },
447         { "D3DFMT_DXT3",        D3DFMT_DXT3,        FALSE   },
448         { "D3DFMT_DXT4",        D3DFMT_DXT4,        FALSE   },
449         { "D3DFMT_DXT5",        D3DFMT_DXT5,        FALSE   },
450     };
451
452     for(i = 0; i < (sizeof(testdata) / sizeof(testdata[0])); i++)
453     {
454         texture = NULL;
455         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 64, 64, testdata[i].fmt, D3DPOOL_SYSTEMMEM, &surface, NULL);
456         if(FAILED(hr))
457         {
458             hr = IDirect3DDevice9_CreateTexture(device, 64, 64, 1, 0, testdata[i].fmt, D3DPOOL_MANAGED, &texture, NULL);
459             if(FAILED(hr))
460             {
461                 skip("IDirect3DDevice9_CreateOffscreenPlainSurface failed, hr = 0x%08x, fmt %s\n", hr, testdata[i].name);
462                 continue;
463             }
464             IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
465         }
466
467         dc = (void *) 0x1234;
468         hr = IDirect3DSurface9_GetDC(surface, &dc);
469
470         if(testdata[i].getdc_supported)
471         {
472             ok(SUCCEEDED(hr), "GetDC on format %s failed(hr=0x%08x), but was expected to work\n",
473                testdata[i].name, hr);
474         }
475         else
476         {
477             ok(FAILED(hr), "GetDC on format %s worked(hr=0x%08x), but was expected to fail\n",
478                testdata[i].name, hr);
479         }
480
481         if(SUCCEEDED(hr))
482         {
483             hr = IDirect3DSurface9_ReleaseDC(surface, dc);
484             ok(hr == D3D_OK, "IDirect3DSurface9_ReleaseDC failed, hr = 0x%08x\n", hr);
485         }
486         else
487         {
488             ok(dc == (void *) 0x1234, "After failed getDC dc is %p\n", dc);
489         }
490
491         IDirect3DSurface9_Release(surface);
492         if(texture) IDirect3DTexture9_Release(texture);
493     }
494 }
495
496 static void test_surface_dimensions(IDirect3DDevice9 *device)
497 {
498     IDirect3DSurface9 *surface;
499     HRESULT hr;
500
501     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 0, 1, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
502     ok(hr == D3DERR_INVALIDCALL, "CreateOffscreenPlainSurface returned %#x, expected D3DERR_INVALIDCALL.\n", hr);
503     if (SUCCEEDED(hr)) IDirect3DSurface9_Release(surface);
504
505     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
506     ok(hr == D3DERR_INVALIDCALL, "CreateOffscreenPlainSurface returned %#x, expected D3DERR_INVALIDCALL.\n", hr);
507     if (SUCCEEDED(hr)) IDirect3DSurface9_Release(surface);
508 }
509
510 static void test_surface_format_null(IDirect3DDevice9 *device)
511 {
512     static const D3DFORMAT D3DFMT_NULL = MAKEFOURCC('N','U','L','L');
513     IDirect3DTexture9 *texture;
514     IDirect3DSurface9 *surface;
515     IDirect3DSurface9 *rt, *ds;
516     D3DLOCKED_RECT locked_rect;
517     D3DSURFACE_DESC desc;
518     IDirect3D9 *d3d;
519     HRESULT hr;
520
521     IDirect3DDevice9_GetDirect3D(device, &d3d);
522
523     hr = IDirect3D9_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
524             D3DUSAGE_RENDERTARGET,  D3DRTYPE_SURFACE, D3DFMT_NULL);
525     if (hr != D3D_OK)
526     {
527         skip("No D3DFMT_NULL support, skipping test.\n");
528         IDirect3D9_Release(d3d);
529         return;
530     }
531
532     hr = IDirect3D9_CheckDeviceFormat(d3d, 0, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
533             D3DUSAGE_RENDERTARGET,  D3DRTYPE_TEXTURE, D3DFMT_NULL);
534     ok(hr == D3D_OK, "D3DFMT_NULL should be supported for render target textures, hr %#x.\n", hr);
535
536     hr = IDirect3D9_CheckDepthStencilMatch(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8,
537             D3DFMT_NULL, D3DFMT_D24S8);
538     ok(SUCCEEDED(hr), "Depth stencil match failed for D3DFMT_NULL, hr %#x.\n", hr);
539
540     IDirect3D9_Release(d3d);
541
542     hr = IDirect3DDevice9_CreateRenderTarget(device, 128, 128, D3DFMT_NULL, 0, 0, TRUE, &surface, NULL);
543     ok(SUCCEEDED(hr), "Failed to create render target, hr %#x.\n", hr);
544
545     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &rt);
546     ok(SUCCEEDED(hr), "Failed to get original render target, hr %#x.\n", hr);
547
548     hr = IDirect3DDevice9_GetDepthStencilSurface(device, &ds);
549     ok(SUCCEEDED(hr), "Failed to get original depth/stencil, hr %#x.\n", hr);
550
551     hr = IDirect3DDevice9_SetRenderTarget(device, 0, NULL);
552     ok(FAILED(hr), "Succeeded in setting render target 0 to NULL, should fail.\n");
553
554     hr = IDirect3DDevice9_SetRenderTarget(device, 0, surface);
555     ok(SUCCEEDED(hr), "Failed to set render target, hr %#x.\n", hr);
556
557     hr = IDirect3DDevice9_SetDepthStencilSurface(device, NULL);
558     ok(SUCCEEDED(hr), "Failed to set depth/stencil, hr %#x.\n", hr);
559
560     hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, 0x00000000, 0.0f, 0);
561     ok(SUCCEEDED(hr), "Clear failed, hr %#x.\n", hr);
562
563     hr = IDirect3DDevice9_SetRenderTarget(device, 0, rt);
564     ok(SUCCEEDED(hr), "Failed to set render target, hr %#x.\n", hr);
565
566     hr = IDirect3DDevice9_SetDepthStencilSurface(device, ds);
567     ok(SUCCEEDED(hr), "Failed to set depth/stencil, hr %#x.\n", hr);
568
569     IDirect3DSurface9_Release(rt);
570     IDirect3DSurface9_Release(ds);
571
572     hr = IDirect3DSurface9_GetDesc(surface, &desc);
573     ok(SUCCEEDED(hr), "Failed to get surface desc, hr %#x.\n", hr);
574     ok(desc.Width == 128, "Expected width 128, got %u.\n", desc.Width);
575     ok(desc.Height == 128, "Expected height 128, got %u.\n", desc.Height);
576
577     hr = IDirect3DSurface9_LockRect(surface, &locked_rect, NULL, 0);
578     ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
579     ok(locked_rect.Pitch, "Expected non-zero pitch, got %u.\n", locked_rect.Pitch);
580     ok(!!locked_rect.pBits, "Expected non-NULL pBits, got %p.\n", locked_rect.pBits);
581
582     hr = IDirect3DSurface9_UnlockRect(surface);
583     ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
584
585     IDirect3DSurface9_Release(surface);
586
587     hr = IDirect3DDevice9_CreateTexture(device, 128, 128, 0, D3DUSAGE_RENDERTARGET,
588             D3DFMT_NULL, D3DPOOL_DEFAULT, &texture, NULL);
589     ok(SUCCEEDED(hr), "Failed to create texture, hr %#x.\n", hr);
590     IDirect3DTexture9_Release(texture);
591 }
592
593 START_TEST(surface)
594 {
595     HMODULE d3d9_handle;
596     IDirect3DDevice9 *device_ptr;
597     ULONG refcount;
598
599     d3d9_handle = LoadLibraryA("d3d9.dll");
600     if (!d3d9_handle)
601     {
602         skip("Could not load d3d9.dll\n");
603         return;
604     }
605
606     device_ptr = init_d3d9(d3d9_handle);
607     if (!device_ptr) return;
608
609     test_surface_get_container(device_ptr);
610     test_surface_alignment(device_ptr);
611     test_lockrect_offset(device_ptr);
612     test_lockrect_invalid(device_ptr);
613     test_private_data(device_ptr);
614     test_getdc(device_ptr);
615     test_surface_dimensions(device_ptr);
616     test_surface_format_null(device_ptr);
617
618     refcount = IDirect3DDevice9_Release(device_ptr);
619     ok(!refcount, "Device has %u references left\n", refcount);
620 }