d3d: Make sure vertexbuffer lock doesn't return a NULL pointer.
[wine] / dlls / d3d8 / 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 <d3d8.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 = "d3d8_test_wc";
28     RegisterClass(&wc);
29
30     return CreateWindow("d3d8_test_wc", "d3d8_test",
31             0, 0, 0, 0, 0, 0, 0, 0, 0);
32 }
33
34 static IDirect3DDevice8 *init_d3d8(HMODULE d3d8_handle)
35 {
36     IDirect3D8 * (__stdcall * d3d8_create)(UINT SDKVersion) = 0;
37     IDirect3D8 *d3d8_ptr = 0;
38     IDirect3DDevice8 *device_ptr = 0;
39     D3DPRESENT_PARAMETERS present_parameters;
40     D3DDISPLAYMODE               d3ddm;
41     HRESULT hr;
42
43     d3d8_create = (void *)GetProcAddress(d3d8_handle, "Direct3DCreate8");
44     ok(d3d8_create != NULL, "Failed to get address of Direct3DCreate8\n");
45     if (!d3d8_create) return NULL;
46
47     d3d8_ptr = d3d8_create(D3D_SDK_VERSION);
48     if (!d3d8_ptr)
49     {
50         skip("could not create D3D8\n");
51         return NULL;
52     }
53
54     IDirect3D8_GetAdapterDisplayMode(d3d8_ptr, D3DADAPTER_DEFAULT, &d3ddm );
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     present_parameters.BackBufferFormat = d3ddm.Format;
60
61     hr = IDirect3D8_CreateDevice(d3d8_ptr, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &present_parameters, &device_ptr);
62
63     if(FAILED(hr))
64     {
65         skip("could not create device, IDirect3D8_CreateDevice returned %#x\n", hr);
66         return NULL;
67     }
68
69     return device_ptr;
70 }
71
72 static void lock_flag_test(IDirect3DDevice8 *device)
73 {
74     HRESULT hr;
75     IDirect3DVertexBuffer8 *buffer;
76     unsigned int i;
77     BYTE *data;
78     const struct
79     {
80         DWORD flags;
81         const char *debug_string;
82         HRESULT result;
83     }
84     test_data[] =
85     {
86         {D3DLOCK_READONLY,                          "D3DLOCK_READONLY",                         D3D_OK             },
87         {D3DLOCK_DISCARD,                           "D3DLOCK_DISCARD",                          D3D_OK             },
88         {D3DLOCK_NOOVERWRITE,                       "D3DLOCK_NOOVERWRITE",                      D3D_OK             },
89         {D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD,     "D3DLOCK_NOOVERWRITE | D3DLOCK_DISCARD",    D3D_OK             },
90         {D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY,    "D3DLOCK_NOOVERWRITE | D3DLOCK_READONLY",   D3D_OK             },
91         {D3DLOCK_READONLY    | D3DLOCK_DISCARD,     "D3DLOCK_READONLY | D3DLOCK_DISCARD",       D3D_OK             },
92         /* Completely bogous flags aren't an error */
93         {0xdeadbeef,                                "0xdeadbeef",                               D3D_OK             },
94     };
95
96     hr = IDirect3DDevice8_CreateVertexBuffer(device, 1024, D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &buffer);
97     ok(hr == D3D_OK, "IDirect3DDevice8_CreateBuffer failed, 0x%08x\n", hr);
98
99     for(i = 0; i < (sizeof(test_data) / sizeof(*test_data)); i++)
100     {
101         hr = IDirect3DVertexBuffer8_Lock(buffer, 0, 0, &data, test_data[i].flags);
102         ok(hr == test_data[i].result, "Lock flags %s returned 0x%08x, expected 0x%08x\n",
103             test_data[i].debug_string, hr, test_data[i].result);
104
105         if(SUCCEEDED(hr))
106         {
107             ok(data != NULL, "The data pointer returned by Lock is NULL\n");
108             hr = IDirect3DVertexBuffer8_Unlock(buffer);
109             ok(hr == D3D_OK, "IDirect3DVertexBuffer8_Unlock failed, 0x%08x\n", hr);
110         }
111     }
112
113     IDirect3DVertexBuffer8_Release(buffer);
114 }
115
116 START_TEST(buffer)
117 {
118     IDirect3DDevice8 *device_ptr;
119     ULONG refcount;
120     HMODULE d3d8_handle = 0;
121
122     d3d8_handle = LoadLibraryA("d3d8.dll");
123     if (!d3d8_handle)
124     {
125         skip("Could not load d3d8.dll\n");
126         return;
127     }
128
129     device_ptr = init_d3d8(d3d8_handle);
130     if (!device_ptr) return;
131
132     lock_flag_test(device_ptr);
133
134     refcount = IDirect3DDevice8_Release(device_ptr);
135     ok(!refcount, "Device has %u references left\n", refcount);
136 }