d3d8/tests: Mark a failure of GetLightEnable on Win9x as broken().
[wine] / dlls / d3d8 / indexbuffer.c
1 /*
2  * IDirect3DIndexBuffer8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DIndexBuffer8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_QueryInterface(LPDIRECT3DINDEXBUFFER8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
29
30     if (IsEqualGUID(riid, &IID_IUnknown)
31         || IsEqualGUID(riid, &IID_IDirect3DResource8)
32         || IsEqualGUID(riid, &IID_IDirect3DIndexBuffer8)) {
33         IUnknown_AddRef(iface);
34         *ppobj = This;
35         return S_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39     *ppobj = NULL;
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI IDirect3DIndexBuffer8Impl_AddRef(LPDIRECT3DINDEXBUFFER8 iface) {
44     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
45     ULONG ref = InterlockedIncrement(&This->ref);
46
47     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
48
49     if (ref == 1)
50     {
51         IDirect3DDevice8_AddRef(This->parentDevice);
52         wined3d_mutex_lock();
53         IWineD3DBuffer_AddRef(This->wineD3DIndexBuffer);
54         wined3d_mutex_unlock();
55     }
56
57     return ref;
58 }
59
60 static ULONG WINAPI IDirect3DIndexBuffer8Impl_Release(LPDIRECT3DINDEXBUFFER8 iface) {
61     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
62     ULONG ref = InterlockedDecrement(&This->ref);
63
64     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
65
66     if (ref == 0) {
67         IDirect3DDevice8 *parentDevice = This->parentDevice;
68
69         wined3d_mutex_lock();
70         IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
71         wined3d_mutex_unlock();
72
73         /* Release the device last, as it may cause the device to be destroyed. */
74         IDirect3DDevice8_Release(parentDevice);
75     }
76     return ref;
77 }
78
79 /* IDirect3DIndexBuffer8 IDirect3DResource8 Interface follow: */
80 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDevice(LPDIRECT3DINDEXBUFFER8 iface, IDirect3DDevice8 **ppDevice) {
81     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
82     IWineD3DDevice *wined3d_device;
83     HRESULT hr;
84     TRACE("(%p) Relay\n", This);
85
86     wined3d_mutex_lock();
87     hr = IWineD3DBuffer_GetDevice(This->wineD3DIndexBuffer, &wined3d_device);
88     if (SUCCEEDED(hr))
89     {
90         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
91         IWineD3DDevice_Release(wined3d_device);
92     }
93     wined3d_mutex_unlock();
94
95     return hr;
96 }
97
98 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
99     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
100     HRESULT hr;
101     TRACE("(%p) Relay\n", This);
102
103     wined3d_mutex_lock();
104     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
105     wined3d_mutex_unlock();
106
107     return hr;
108 }
109
110 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
111     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
112     HRESULT hr;
113     TRACE("(%p) Relay\n", This);
114
115     wined3d_mutex_lock();
116     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
117     wined3d_mutex_unlock();
118
119     return hr;
120 }
121
122 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid) {
123     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
124     HRESULT hr;
125     TRACE("(%p) Relay\n", This);
126
127     wined3d_mutex_lock();
128     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
129     wined3d_mutex_unlock();
130
131     return hr;
132 }
133
134 static DWORD WINAPI IDirect3DIndexBuffer8Impl_SetPriority(LPDIRECT3DINDEXBUFFER8 iface, DWORD PriorityNew) {
135     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
136     DWORD ret;
137     TRACE("(%p) Relay\n", This);
138
139     wined3d_mutex_lock();
140     ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
141     wined3d_mutex_unlock();
142
143     return ret;
144 }
145
146 static DWORD WINAPI IDirect3DIndexBuffer8Impl_GetPriority(LPDIRECT3DINDEXBUFFER8 iface) {
147     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
148     DWORD ret;
149     TRACE("(%p) Relay\n", This);
150
151     wined3d_mutex_lock();
152     ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
153     wined3d_mutex_unlock();
154
155     return ret;
156 }
157
158 static void WINAPI IDirect3DIndexBuffer8Impl_PreLoad(LPDIRECT3DINDEXBUFFER8 iface) {
159     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
160     TRACE("(%p) Relay\n", This);
161
162     wined3d_mutex_lock();
163     IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
164     wined3d_mutex_unlock();
165 }
166
167 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer8Impl_GetType(LPDIRECT3DINDEXBUFFER8 iface) {
168     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
169     TRACE("(%p)\n", This);
170
171     return D3DRTYPE_INDEXBUFFER;
172 }
173
174 /* IDirect3DIndexBuffer8 Interface follow: */
175 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Lock(LPDIRECT3DINDEXBUFFER8 iface, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags) {
176     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
177     HRESULT hr;
178     TRACE("(%p) Relay\n", This);
179
180     wined3d_mutex_lock();
181     hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, ppbData, Flags);
182     wined3d_mutex_unlock();
183
184     return hr;
185 }
186
187 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Unlock(LPDIRECT3DINDEXBUFFER8 iface) {
188     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
189     HRESULT hr;
190     TRACE("(%p) Relay\n", This);
191
192     wined3d_mutex_lock();
193     hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
194     wined3d_mutex_unlock();
195
196     return hr;
197 }
198
199 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDesc(LPDIRECT3DINDEXBUFFER8 iface, D3DINDEXBUFFER_DESC *pDesc) {
200     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
201     HRESULT hr;
202     WINED3DBUFFER_DESC desc;
203     TRACE("(%p) Relay\n", This);
204
205     wined3d_mutex_lock();
206     hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
207     wined3d_mutex_unlock();
208
209     if (SUCCEEDED(hr)) {
210         pDesc->Format = d3dformat_from_wined3dformat(This->format);
211         pDesc->Type = D3DRTYPE_INDEXBUFFER;
212         pDesc->Usage = desc.Usage;
213         pDesc->Pool = desc.Pool;
214         pDesc->Size = desc.Size;
215     }
216
217     return hr;
218 }
219
220 static const IDirect3DIndexBuffer8Vtbl Direct3DIndexBuffer8_Vtbl =
221 {
222     /* IUnknown */
223     IDirect3DIndexBuffer8Impl_QueryInterface,
224     IDirect3DIndexBuffer8Impl_AddRef,
225     IDirect3DIndexBuffer8Impl_Release,
226     /* IDirect3DResource8 */
227     IDirect3DIndexBuffer8Impl_GetDevice,
228     IDirect3DIndexBuffer8Impl_SetPrivateData,
229     IDirect3DIndexBuffer8Impl_GetPrivateData,
230     IDirect3DIndexBuffer8Impl_FreePrivateData,
231     IDirect3DIndexBuffer8Impl_SetPriority,
232     IDirect3DIndexBuffer8Impl_GetPriority,
233     IDirect3DIndexBuffer8Impl_PreLoad,
234     IDirect3DIndexBuffer8Impl_GetType,
235     /* IDirect3DIndexBuffer8 */
236     IDirect3DIndexBuffer8Impl_Lock,
237     IDirect3DIndexBuffer8Impl_Unlock,
238     IDirect3DIndexBuffer8Impl_GetDesc
239 };
240
241 static void STDMETHODCALLTYPE d3d8_indexbuffer_wined3d_object_destroyed(void *parent)
242 {
243     HeapFree(GetProcessHeap(), 0, parent);
244 }
245
246 static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
247 {
248     d3d8_indexbuffer_wined3d_object_destroyed,
249 };
250
251 HRESULT indexbuffer_init(IDirect3DIndexBuffer8Impl *buffer, IDirect3DDevice8Impl *device,
252         UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
253 {
254     HRESULT hr;
255
256     buffer->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
257     buffer->ref = 1;
258     buffer->format = wined3dformat_from_d3dformat(format);
259
260     wined3d_mutex_lock();
261     hr = IWineD3DDevice_CreateIndexBuffer(device->WineD3DDevice, size,
262             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DIndexBuffer,
263             (IUnknown *)buffer, &d3d8_indexbuffer_wined3d_parent_ops);
264     wined3d_mutex_unlock();
265     if (FAILED(hr))
266     {
267         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
268         return hr;
269     }
270
271     buffer->parentDevice = (IDirect3DDevice8 *)device;
272     IUnknown_AddRef(buffer->parentDevice);
273
274     return D3D_OK;
275 }