2 * Copyright (C) 2006 Henri Verbeet
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.
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.
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
21 #include "wine/test.h"
23 static HWND create_window(void)
26 wc.lpfnWndProc = &DefWindowProc;
27 wc.lpszClassName = "d3d9_test_wc";
30 return CreateWindow("d3d9_test_wc", "d3d9_test",
31 0, 0, 0, 0, 0, 0, 0, 0, 0);
34 static IDirect3DDevice9 *init_d3d9(HMODULE d3d9_handle)
36 IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
37 IDirect3D9 *d3d9_ptr = 0;
38 IDirect3DDevice9 *device_ptr = 0;
39 D3DPRESENT_PARAMETERS present_parameters;
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;
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;
50 ZeroMemory(&present_parameters, sizeof(present_parameters));
51 present_parameters.Windowed = TRUE;
52 present_parameters.hDeviceWindow = create_window();
53 present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
55 hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
56 NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
57 ok(SUCCEEDED(hr), "IDirect3D_CreateDevice returned %#x\n", hr);
62 static void test_surface_get_container(IDirect3DDevice9 *device_ptr)
64 IDirect3DTexture9 *texture_ptr = 0;
65 IDirect3DSurface9 *surface_ptr = 0;
69 hr = IDirect3DDevice9_CreateTexture(device_ptr, 128, 128, 1, 0,
70 D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture_ptr, 0);
71 ok(SUCCEEDED(hr) && texture_ptr != NULL, "CreateTexture returned: hr %#x, texture_ptr %p. "
72 "Expected hr %#x, texture_ptr != %p\n", hr, texture_ptr, D3D_OK, NULL);
73 if (!texture_ptr || FAILED(hr)) goto cleanup;
75 hr = IDirect3DTexture9_GetSurfaceLevel(texture_ptr, 0, &surface_ptr);
76 ok(SUCCEEDED(hr) && surface_ptr != NULL, "GetSurfaceLevel returned: hr %#x, surface_ptr %p. "
77 "Expected hr %#x, surface_ptr != %p\n", hr, surface_ptr, D3D_OK, NULL);
78 if (!surface_ptr || FAILED(hr)) goto cleanup;
80 /* These should work... */
81 container_ptr = (void *)0x1337c0d3;
82 hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IUnknown, &container_ptr);
83 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
84 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
85 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
87 container_ptr = (void *)0x1337c0d3;
88 hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DResource9, &container_ptr);
89 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
90 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
91 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
93 container_ptr = (void *)0x1337c0d3;
94 hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DBaseTexture9, &container_ptr);
95 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
96 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
97 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
99 container_ptr = (void *)0x1337c0d3;
100 hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DTexture9, &container_ptr);
101 ok(SUCCEEDED(hr) && container_ptr == texture_ptr, "GetContainer returned: hr %#x, container_ptr %p. "
102 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, S_OK, texture_ptr);
103 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
105 /* ...and this one shouldn't. This should return E_NOINTERFACE and set container_ptr to NULL */
106 container_ptr = (void *)0x1337c0d3;
107 hr = IDirect3DSurface9_GetContainer(surface_ptr, &IID_IDirect3DSurface9, &container_ptr);
108 ok(hr == E_NOINTERFACE && container_ptr == NULL, "GetContainer returned: hr %#x, container_ptr %p. "
109 "Expected hr %#x, container_ptr %p\n", hr, container_ptr, E_NOINTERFACE, NULL);
110 if (container_ptr && container_ptr != (void *)0x1337c0d3) IUnknown_Release((IUnknown *)container_ptr);
113 if (texture_ptr) IDirect3DTexture9_Release(texture_ptr);
114 if (surface_ptr) IDirect3DSurface9_Release(surface_ptr);
117 static void test_surface_alignment(IDirect3DDevice9 *device_ptr)
119 IDirect3DSurface9 *surface_ptr = 0;
123 /* Test a sysmem surface as those aren't affected by the hardware's np2 restrictions */
124 hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device_ptr, 5, 5, D3DFMT_R5G6B5, D3DPOOL_SYSTEMMEM, &surface_ptr, 0);
125 ok(hr == D3D_OK, "IDirect3DDevice9_CreateOffscreenPlainSurface returned %08x\n", hr);
129 D3DLOCKED_RECT lockedRect;
130 hr = IDirect3DSurface9_LockRect(surface_ptr, &lockedRect, NULL, 0);
131 ok(hr == D3D_OK, "IDirect3DSurface9_LockRect returned %08x\n", hr);
132 ok(!(lockedRect.Pitch & 3), "Surface pitch %d is not 32-bit aligned\n", lockedRect.Pitch);
134 /* Some applications also depend on the exact pitch, rather than just
135 * the alignment. However, this test will fail or succeed depending
136 * on the NP2 mode we're using. */
137 ok(lockedRect.Pitch == 12, "Got pitch %d, expected 12\n", lockedRect.Pitch);
139 hr = IDirect3DSurface9_UnlockRect(surface_ptr);
140 IDirect3DSurface9_Release(surface_ptr);
143 for (i = 0; i < 5; i++)
145 IDirect3DTexture9 *pTexture;
148 hr = IDirect3DDevice9_CreateTexture(device_ptr, 64, 64, 0, 0, MAKEFOURCC('D', 'X', 'T', '1'+i),
149 D3DPOOL_MANAGED, &pTexture, NULL);
150 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateTexture: %s\n", DXGetErrorString9(hr));
151 if (FAILED(hr)) continue;
153 for (j = IDirect3DBaseTexture9_GetLevelCount(pTexture) - 1; j >= 0; j--)
156 D3DSURFACE_DESC descr;
158 IDirect3DTexture9_GetLevelDesc(pTexture, j, &descr);
159 hr = IDirect3DTexture9_LockRect(pTexture, j, &rc, NULL, 0);
160 ok(SUCCEEDED(hr), "IDirect3DTexture9_LockRect: %s\n", DXGetErrorString9(hr));
161 IDirect3DTexture9_UnlockRect(pTexture, j);
163 pitch = ((descr.Width + 3) >> 2) << 3;
164 if (i > 0) pitch <<= 1;
165 ok(rc.Pitch == pitch, "Wrong pitch for DXT%d lvl[%d (%dx%d)]: expected %d got %d\n",
166 i + 1, j, descr.Width, descr.Height, pitch, rc.Pitch);
168 IUnknown_Release( pTexture );
175 IDirect3DDevice9 *device_ptr;
177 d3d9_handle = LoadLibraryA("d3d9.dll");
180 trace("Could not load d3d9.dll, skipping tests\n");
184 device_ptr = init_d3d9(d3d9_handle);
185 if (!device_ptr) return;
187 test_surface_get_container(device_ptr);
188 test_surface_alignment(device_ptr);