msdaps: Add server side stubs for IAccessor.
[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 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",       D3D_OK             },
91         /* Completely bogous flags aren't an error */
92         {0xdeadbeef,                                "0xdeadbeef",                               D3D_OK             },
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         ok(hr == test_data[i].result, "Lock flags %s returned 0x%08x, expected 0x%08x\n",
102             test_data[i].debug_string, hr, test_data[i].result);
103
104         if(SUCCEEDED(hr))
105         {
106             hr = IDirect3DVertexBuffer9_Unlock(buffer);
107             ok(hr == D3D_OK, "IDirect3DVertexBuffer9_Unlock failed, 0x%08x\n", hr);
108         }
109     }
110
111     IDirect3DVertexBuffer9_Release(buffer);
112 }
113
114 static inline const char *debug_d3dpool(D3DPOOL pool)
115 {
116     switch(pool)
117     {
118         case D3DPOOL_DEFAULT: return "D3DPOOL_DEFAULT";
119         case D3DPOOL_SYSTEMMEM: return "D3DPOOL_SYSTEMMEM";
120         case D3DPOOL_SCRATCH: return "D3DPOOL_SCRATCH";
121         case D3DPOOL_MANAGED: return "D3DPOOL_MANAGED";
122         default:
123         return "unknown pool";
124     }
125 }
126
127 static void test_vertex_buffer_alignment(IDirect3DDevice9 *device)
128 {
129     IDirect3DVertexBuffer9 *buffer = NULL;
130     HRESULT hr;
131     D3DPOOL pools[] = {D3DPOOL_DEFAULT, D3DPOOL_SYSTEMMEM, D3DPOOL_SCRATCH, D3DPOOL_MANAGED};
132     DWORD sizes[] = {1, 4, 16, 17, 32, 33, 64, 65, 1024, 1025, 1048576, 1048577};
133     unsigned int i, j;
134     void *data;
135
136     for(i = 0; i < (sizeof(sizes) / sizeof(sizes[0])); i++)
137     {
138         for(j = 0; j < (sizeof(pools) / sizeof(pools[0])); j++)
139         {
140             hr = IDirect3DDevice9_CreateVertexBuffer(device, sizes[i], 0, 0, pools[j], &buffer, NULL);
141             if(pools[j] == D3DPOOL_SCRATCH)
142             {
143                 ok(hr == D3DERR_INVALIDCALL, "Creating a D3DPOOL_SCRATCH buffer returned (0x%08x)\n", hr);
144             }
145             else
146             {
147                 ok(SUCCEEDED(hr), "IDirect3DDevice9_CreateVertexBuffer failed (0x%08x). Pool = %s, size %d\n", hr,
148                    debug_d3dpool(pools[j]), sizes[i]);
149             }
150             if(FAILED(hr)) continue;
151
152             hr = IDirect3DVertexBuffer9_Lock(buffer, 0, 0, &data, 0);
153             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Lock failed (0x%08x)\n", hr);
154             ok(((DWORD_PTR) data & 31) == 0, "Vertex buffer start address is not 32 byte aligned(size: %d, pool: %s, data: %p)\n",
155                sizes[i], debug_d3dpool(pools[j]), data);
156             hr = IDirect3DVertexBuffer9_Unlock(buffer);
157             ok(SUCCEEDED(hr), "IDirect3DVertexBuffer9_Unlock failed (0x%08x)\n", hr);
158
159             if(buffer) IDirect3DVertexBuffer9_Release(buffer);
160         }
161     }
162 }
163
164 START_TEST(buffer)
165 {
166     IDirect3DDevice9 *device_ptr;
167     ULONG refcount;
168
169     d3d9_handle = LoadLibraryA("d3d9.dll");
170     if (!d3d9_handle)
171     {
172         skip("Could not load d3d9.dll\n");
173         return;
174     }
175
176     device_ptr = init_d3d9();
177     if (!device_ptr) return;
178
179     lock_flag_test(device_ptr);
180     test_vertex_buffer_alignment(device_ptr);
181
182     refcount = IDirect3DDevice9_Release(device_ptr);
183     ok(!refcount, "Device has %u references left\n", refcount);
184 }