d3d9: Fix test failures on Windows 7.
[wine] / dlls / d3d9 / tests / buffer.c
1 /*
2  * Copyright (C) 2010 Stefan Dösinger(for CodeWeavers)
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
19 #define COBJMACROS
20 #include <d3d9.h>
21 #include "wine/test.h"
22
23 static HMODULE d3d9_handle = 0;
24
25 static HWND create_window(void)
26 {
27     WNDCLASS wc = {0};
28     wc.lpfnWndProc = DefWindowProc;
29     wc.lpszClassName = "d3d9_test_wc";
30     RegisterClass(&wc);
31
32     return CreateWindow("d3d9_test_wc", "d3d9_test",
33             0, 0, 0, 0, 0, 0, 0, 0, 0);
34 }
35
36 static IDirect3DDevice9 *init_d3d9(void)
37 {
38     IDirect3D9 * (__stdcall * d3d9_create)(UINT SDKVersion) = 0;
39     IDirect3D9 *d3d9_ptr = 0;
40     IDirect3DDevice9 *device_ptr = 0;
41     D3DPRESENT_PARAMETERS present_parameters;
42     HRESULT hr;
43
44     d3d9_create = (void *)GetProcAddress(d3d9_handle, "Direct3DCreate9");
45     ok(d3d9_create != NULL, "Failed to get address of Direct3DCreate9\n");
46     if (!d3d9_create) return NULL;
47
48     d3d9_ptr = d3d9_create(D3D_SDK_VERSION);
49     if (!d3d9_ptr)
50     {
51         skip("could not create D3D9\n");
52         return NULL;
53     }
54
55     ZeroMemory(&present_parameters, sizeof(present_parameters));
56     present_parameters.Windowed = TRUE;
57     present_parameters.hDeviceWindow = create_window();
58     present_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
59
60     hr = IDirect3D9_CreateDevice(d3d9_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
61
62     if(FAILED(hr))
63     {
64         skip("could not create device, IDirect3D9_CreateDevice returned 0x%08x\n", hr);
65         return NULL;
66     }
67
68     return device_ptr;
69 }
70
71 static void lock_flag_test(IDirect3DDevice9 *device)
72 {
73     HRESULT hr;
74     IDirect3DVertexBuffer9 *buffer;
75     unsigned int i;
76     void *data;
77     const struct
78     {
79         DWORD flags;
80         const char *debug_string;
81         HRESULT win7_result;
82     }
83     test_data[] =
84     {
85         {D3DLOCK_READONLY,                          "D3DLOCK_READONLY",                         D3D_OK             },
86         {D3DLOCK_DISCARD,                           "D3DLOCK_DISCARD",                          D3D_OK             },
87         {D3DLOCK_NOOVERWRITE,                       "D3DLOCK_NOOVERWRITE",                      D3D_OK             },
88         {D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD,     "D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD",    D3D_OK             },
89         {D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY,    "D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY",   D3D_OK             },
90         {D3DLOCK_READONLY    | D3DLOCK_DISCARD,     "D3DLOCK_READONLY | D3DLOCK_DISCARD",       D3DERR_INVALIDCALL },
91         /* Completely bogous flags aren't an error */
92         {0xdeadbeef,                                "0xdeadbeef",                               D3DERR_INVALIDCALL },
93     };
94
95     hr = IDirect3DDevice9_CreateVertexBuffer(device, 1024, D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &buffer, NULL);
96     ok(hr == D3D_OK, "IDirect3DDevice9_CreateBuffer failed, 0x%08x\n", hr);
97
98     for(i = 0; i < (sizeof(test_data) / sizeof(*test_data)); i++)
99     {
100         hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, &data, test_data[i].flags);
101         /* Windows XP always returns D3D_OK even with flags that don't make sense. Windows 7 returns
102          * an error. At least one game(Shaiya) depends on the Windows XP result, so mark the Windows 7
103          * behavior as broken()
104          */
105         ok(hr == D3D_OK || broken(hr == test_data[i].win7_result), "Lock flags %s returned 0x%08x, expected D3D_OK\n",
106             test_data[i].debug_string, hr);
107
108         if(SUCCEEDED(hr))
109         {
110             ok(data != NULL, "The data pointer returned by Lock is NULL\n");
111             hr = IDirect3DVertexBuffer9_Unlock(buffer);
112             ok(hr == D3D_OK, "IDirect3DVertexBuffer9_Unlock failed, 0x%08x\n", hr);
113         }
114     }
115
116     IDirect3DVertexBuffer9_Release(buffer);
117 }
118
119 static inline const char *debug_d3dpool(D3DPOOL pool)
120 {
121     switch(pool)
122     {
123         case D3DPOOL_DEFAULT: return "D3DPOOL_DEFAULT";
124         case D3DPOOL_SYSTEMMEM: return "D3DPOOL_SYSTEMMEM";
125         case D3DPOOL_SCRATCH: return "D3DPOOL_SCRATCH";
126         case D3DPOOL_MANAGED: return "D3DPOOL_MANAGED";
127         default:
128         return "unknown pool";
129     }
130 }
131
132 static void test_vertex_buffer_alignment(IDirect3DDevice9 *device)
133 {
134     IDirect3DVertexBuffer9 *buffer = NULL;
135     HRESULT hr;
136     D3DPOOL pools[] = {D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEM, D3DPOOL_SCRATCH, D3DPOOL_MANAGED};
137     DWORD sizes[] = {1, 4, 16, 17, 32, 33, 64, 65, 1024, 1025, 1048576, 1048577};
138     unsigned int i, j;
139     void *data;
140
141     for(i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++)
142     {
143         for(j = 0; j < (sizeof(pools) / sizeof(pools[0])); j++)
144         {
145             hr = IDirect3DDevice9_CreateVertexBuffer(device, sizes[i], 0, 0, pools[j], &buffer, NULL);
146             if(pools[j] == D3DPOOL_SCRATCH)
147             {
148                 ok(hr == D3DERR_INVALIDCALL, "Creating a D3DPOOL_SCRATCH buffer returned (0x%08x)\n", hr);
149             }
150             else
151             {
152                 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x). Pool = %s, size %d\n", hr,
153                    debug_d3dpool(pools[j]), sizes[i]);
154             }
155             if(FAILED(hr)) continue;
156
157             hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, &data, 0);
158             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock failed (0x%08x)\n", hr);
159             ok(((DWORD_PTR) data & 31) == 0, "Vertex buffer start address is not 32 byte aligned(size: %d, pool: %s, data: %p)\n",
160                sizes[i], debug_d3dpool(pools[j]), data);
161             hr = IDirect3DVertexBuffer9_Unlock(buffer);
162             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Unlock failed (0x%08x)\n", hr);
163
164             if(buffer) IDirect3DVertexBuffer9_Release(buffer);
165         }
166     }
167 }
168
169 START_TEST(buffer)
170 {
171     IDirect3DDevice9 *device_ptr;
172     ULONG refcount;
173
174     d3d9_handle = LoadLibraryA("d3d9.dll");
175     if (!d3d9_handle)
176     {
177         skip("Could not load d3d9.dll\n");
178         return;
179     }
180
181     device_ptr = init_d3d9();
182     if (!device_ptr) return;
183
184     lock_flag_test(device_ptr);
185     test_vertex_buffer_alignment(device_ptr);
186
187     refcount = IDirect3DDevice9_Release(device_ptr);
188     ok(!refcount, "Device has %u references left\n", refcount);
189 }