wined3d: Get rid of some pointers in WINED3DADAPTER_IDENTIFIER.
[wine] / dlls / d3d9 / indexbuffer.c
1 /*
2  * IDirect3DIndexBuffer9 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DIndexBuffer9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_QueryInterface(LPDIRECT3DINDEXBUFFER9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DResource9)
33         || IsEqualGUID(riid, &IID_IDirect3DIndexBuffer9)) {
34         IDirect3DIndexBuffer9_AddRef(iface);
35         *ppobj = This;
36         return S_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     *ppobj = NULL;
41     return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DIndexBuffer9Impl_AddRef(LPDIRECT3DINDEXBUFFER9 iface) {
45     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
46     ULONG ref = InterlockedIncrement(&This->ref);
47
48     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
49
50     return ref;
51 }
52
53 static ULONG WINAPI IDirect3DIndexBuffer9Impl_Release(LPDIRECT3DINDEXBUFFER9 iface) {
54     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
55     ULONG ref = InterlockedDecrement(&This->ref);
56
57     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
58
59     if (ref == 0) {
60         EnterCriticalSection(&d3d9_cs);
61         IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
62         LeaveCriticalSection(&d3d9_cs);
63         IDirect3DDevice9Ex_Release(This->parentDevice);
64         HeapFree(GetProcessHeap(), 0, This);
65     }
66     return ref;
67 }
68
69 /* IDirect3DIndexBuffer9 IDirect3DResource9 Interface follow: */
70 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetDevice(LPDIRECT3DINDEXBUFFER9 iface, IDirect3DDevice9** ppDevice) {
71     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
72     IWineD3DDevice *wined3d_device;
73     HRESULT hr;
74     TRACE("(%p) Relay\n", This);
75
76     EnterCriticalSection(&d3d9_cs);
77     hr = IWineD3DBuffer_GetDevice(This->wineD3DIndexBuffer, &wined3d_device);
78     if (SUCCEEDED(hr))
79     {
80         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
81         IWineD3DDevice_Release(wined3d_device);
82     }
83     LeaveCriticalSection(&d3d9_cs);
84     return hr;
85 }
86
87 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
88     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
89     HRESULT hr;
90     TRACE("(%p) Relay\n", This);
91
92     EnterCriticalSection(&d3d9_cs);
93     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
94     LeaveCriticalSection(&d3d9_cs);
95     return hr;
96 }
97
98 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
99     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
100     HRESULT hr;
101     TRACE("(%p) Relay\n", This);
102
103     EnterCriticalSection(&d3d9_cs);
104     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
105     LeaveCriticalSection(&d3d9_cs);
106     return hr;
107 }
108
109 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER9 iface, REFGUID refguid) {
110     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
111     HRESULT hr;
112     TRACE("(%p) Relay\n", This);
113
114     EnterCriticalSection(&d3d9_cs);
115     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
116     LeaveCriticalSection(&d3d9_cs);
117     return hr;
118 }
119
120 static DWORD WINAPI IDirect3DIndexBuffer9Impl_SetPriority(LPDIRECT3DINDEXBUFFER9 iface, DWORD PriorityNew) {
121     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
122     DWORD ret;
123     TRACE("(%p) Relay\n", This);
124
125     EnterCriticalSection(&d3d9_cs);
126     ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
127     LeaveCriticalSection(&d3d9_cs);
128     return ret;
129 }
130
131 static DWORD WINAPI IDirect3DIndexBuffer9Impl_GetPriority(LPDIRECT3DINDEXBUFFER9 iface) {
132     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
133     DWORD ret;
134     TRACE("(%p) Relay\n", This);
135
136     EnterCriticalSection(&d3d9_cs);
137     ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
138     LeaveCriticalSection(&d3d9_cs);
139     return ret;
140 }
141
142 static void WINAPI IDirect3DIndexBuffer9Impl_PreLoad(LPDIRECT3DINDEXBUFFER9 iface) {
143     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
144     TRACE("(%p) Relay\n", This);
145
146     EnterCriticalSection(&d3d9_cs);
147     IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
148     LeaveCriticalSection(&d3d9_cs);
149 }
150
151 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer9Impl_GetType(LPDIRECT3DINDEXBUFFER9 iface) {
152     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
153     TRACE("(%p)\n", This);
154
155     return D3DRTYPE_INDEXBUFFER;
156 }
157
158 /* IDirect3DIndexBuffer9 Interface follow: */
159 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Lock(LPDIRECT3DINDEXBUFFER9 iface, UINT OffsetToLock, UINT SizeToLock, void** ppbData, DWORD Flags) {
160     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
161     HRESULT hr;
162     TRACE("(%p) Relay\n", This);
163
164     EnterCriticalSection(&d3d9_cs);
165     hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, (BYTE **)ppbData, Flags);
166     LeaveCriticalSection(&d3d9_cs);
167     return hr;
168 }
169
170 static HRESULT WINAPI IDirect3DIndexBuffer9Impl_Unlock(LPDIRECT3DINDEXBUFFER9 iface) {
171     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
172     HRESULT hr;
173     TRACE("(%p) Relay\n", This);
174
175     EnterCriticalSection(&d3d9_cs);
176     hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
177     LeaveCriticalSection(&d3d9_cs);
178     return hr;
179 }
180
181 static HRESULT  WINAPI        IDirect3DIndexBuffer9Impl_GetDesc(LPDIRECT3DINDEXBUFFER9 iface, D3DINDEXBUFFER_DESC *pDesc) {
182     IDirect3DIndexBuffer9Impl *This = (IDirect3DIndexBuffer9Impl *)iface;
183     HRESULT hr;
184     WINED3DBUFFER_DESC desc;
185     TRACE("(%p) Relay\n", This);
186
187     EnterCriticalSection(&d3d9_cs);
188     hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
189     LeaveCriticalSection(&d3d9_cs);
190
191     if (SUCCEEDED(hr)) {
192         pDesc->Format = d3dformat_from_wined3dformat(This->format);
193         pDesc->Usage = desc.Usage;
194         pDesc->Pool = desc.Pool;
195         pDesc->Size = desc.Size;
196         pDesc->Type = D3DRTYPE_INDEXBUFFER;
197     }
198
199     return hr;
200 }
201
202
203 static const IDirect3DIndexBuffer9Vtbl Direct3DIndexBuffer9_Vtbl =
204 {
205     /* IUnknown */
206     IDirect3DIndexBuffer9Impl_QueryInterface,
207     IDirect3DIndexBuffer9Impl_AddRef,
208     IDirect3DIndexBuffer9Impl_Release,
209     /* IDirect3DResource9 */
210     IDirect3DIndexBuffer9Impl_GetDevice,
211     IDirect3DIndexBuffer9Impl_SetPrivateData,
212     IDirect3DIndexBuffer9Impl_GetPrivateData,
213     IDirect3DIndexBuffer9Impl_FreePrivateData,
214     IDirect3DIndexBuffer9Impl_SetPriority,
215     IDirect3DIndexBuffer9Impl_GetPriority,
216     IDirect3DIndexBuffer9Impl_PreLoad,
217     IDirect3DIndexBuffer9Impl_GetType,
218     /* IDirect3DIndexBuffer9 */
219     IDirect3DIndexBuffer9Impl_Lock,
220     IDirect3DIndexBuffer9Impl_Unlock,
221     IDirect3DIndexBuffer9Impl_GetDesc
222 };
223
224
225 /* IDirect3DDevice9 IDirect3DIndexBuffer9 Methods follow: */
226 HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(LPDIRECT3DDEVICE9EX iface,
227                               UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool,
228                               IDirect3DIndexBuffer9** ppIndexBuffer, HANDLE* pSharedHandle) {
229     
230     IDirect3DIndexBuffer9Impl *object;
231     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
232     HRESULT hrc = D3D_OK;
233     
234     TRACE("(%p) Relay\n", This);
235     /* Allocate the storage for the device */
236     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
237     if (NULL == object) {
238         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
239         return D3DERR_OUTOFVIDEOMEMORY;
240     }
241
242     object->lpVtbl = &Direct3DIndexBuffer9_Vtbl;
243     object->ref = 1;
244     object->format = wined3dformat_from_d3dformat(Format);
245     TRACE("Calling wined3d create index buffer\n");
246     EnterCriticalSection(&d3d9_cs);
247     hrc = IWineD3DDevice_CreateIndexBuffer(This->WineD3DDevice, Length, Usage & WINED3DUSAGE_MASK,
248             (WINED3DPOOL)Pool, &object->wineD3DIndexBuffer, (IUnknown *)object);
249     LeaveCriticalSection(&d3d9_cs);
250     if (hrc != D3D_OK) {
251
252         /* free up object */
253         FIXME("(%p) call to IWineD3DDevice_CreateIndexBuffer failed\n", This);
254         HeapFree(GetProcessHeap(), 0, object);
255     } else {
256         IDirect3DDevice9Ex_AddRef(iface);
257         object->parentDevice = iface;
258         *ppIndexBuffer = (LPDIRECT3DINDEXBUFFER9) object;
259         TRACE("(%p) : Created index buffer %p\n", This, object);
260     }
261     return hrc;
262 }