wined3d: Add a test for the vFace register.
[wine] / dlls / wined3d / indexbuffer.c
1 /*
2  * IWineD3DIndexBuffer Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
29
30 /* *******************************************
31    IWineD3DIndexBuffer IUnknown parts follow
32    ******************************************* */
33 static HRESULT WINAPI IWineD3DIndexBufferImpl_QueryInterface(IWineD3DIndexBuffer *iface, REFIID riid, LPVOID *ppobj)
34 {
35     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
36     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
37     if (IsEqualGUID(riid, &IID_IUnknown)
38         || IsEqualGUID(riid, &IID_IWineD3DBase)
39         || IsEqualGUID(riid, &IID_IWineD3DResource)
40         || IsEqualGUID(riid, &IID_IWineD3DIndexBuffer)){
41         IUnknown_AddRef(iface);
42         *ppobj = This;
43         return S_OK;
44     }
45     *ppobj = NULL;
46     return E_NOINTERFACE;
47 }
48
49 static ULONG WINAPI IWineD3DIndexBufferImpl_AddRef(IWineD3DIndexBuffer *iface) {
50     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
51     ULONG ref = InterlockedIncrement(&This->resource.ref);
52     TRACE("(%p) : AddRef increasing from %d\n", This, ref - 1);
53     return ref;
54 }
55
56 static ULONG WINAPI IWineD3DIndexBufferImpl_Release(IWineD3DIndexBuffer *iface) {
57     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
58     ULONG ref = InterlockedDecrement(&This->resource.ref);
59     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
60     if (ref == 0) {
61         if(This->vbo) {
62             IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
63
64             ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
65             ENTER_GL();
66             /* No need to manually unset the buffer. glDeleteBuffers unsets it for the current context,
67              * but not for other contexts. However, because the d3d buffer is destroyed the app has to
68              * unset it before doing the next draw, thus dirtifying the index buffer state and forcing
69              * binding a new buffer
70              */
71             GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
72             checkGLcall("glDeleteBuffersARB");
73             LEAVE_GL();
74         }
75
76         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
77         HeapFree(GetProcessHeap(), 0, This);
78     }
79     return ref;
80 }
81
82 /* ****************************************************
83    IWineD3DIndexBuffer IWineD3DResource parts follow
84    **************************************************** */
85 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDevice(IWineD3DIndexBuffer *iface, IWineD3DDevice** ppDevice) {
86     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
87 }
88
89 static HRESULT WINAPI IWineD3DIndexBufferImpl_SetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
90     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
91 }
92
93 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
94     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
95 }
96
97 static HRESULT WINAPI IWineD3DIndexBufferImpl_FreePrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid) {
98     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
99 }
100
101 static DWORD WINAPI IWineD3DIndexBufferImpl_SetPriority(IWineD3DIndexBuffer *iface, DWORD PriorityNew) {
102     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
103 }
104
105 static DWORD WINAPI IWineD3DIndexBufferImpl_GetPriority(IWineD3DIndexBuffer *iface) {
106     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
107 }
108
109 static void WINAPI IWineD3DIndexBufferImpl_PreLoad(IWineD3DIndexBuffer *iface) {
110     IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
111 }
112
113 static void WINAPI IWineD3DIndexBufferImpl_UnLoad(IWineD3DIndexBuffer *iface) {
114     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *) iface;
115     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
116     TRACE("(%p)\n", This);
117
118     /* This is easy: The whole content is shadowed in This->resource.allocatedMemory,
119      * so we only have to destroy the vbo. Only do it if we have a vbo, which implies
120      * that vbos are supported.
121      * (TODO: Make a IWineD3DBuffer base class for index and vertex buffers and share
122      * this code. Also needed for D3D10)
123      */
124     if(This->vbo) {
125         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
126         ENTER_GL();
127         GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
128         checkGLcall("glDeleteBuffersARB");
129         LEAVE_GL();
130         This->vbo = 0;
131     }
132 }
133
134 static WINED3DRESOURCETYPE WINAPI IWineD3DIndexBufferImpl_GetType(IWineD3DIndexBuffer *iface) {
135     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
136 }
137
138 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetParent(IWineD3DIndexBuffer *iface, IUnknown **pParent) {
139     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
140 }
141
142 /* ******************************************************
143    IWineD3DIndexBuffer IWineD3DIndexBuffer parts follow
144    ****************************************************** */
145 static HRESULT WINAPI IWineD3DIndexBufferImpl_Lock(IWineD3DIndexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
146     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
147     TRACE("(%p) : offset %d, size %d, Flags=%x\n", This, OffsetToLock, SizeToLock, Flags);
148
149     InterlockedIncrement(&This->lockcount);
150     *ppbData = This->resource.allocatedMemory + OffsetToLock;
151
152     if(Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) || This->vbo == 0) {
153         return WINED3D_OK;
154     }
155
156     if(This->dirtystart != This->dirtyend) {
157         if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
158         if(SizeToLock) {
159             if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
160         } else {
161             This->dirtyend = This->resource.size;
162         }
163     } else {
164         This->dirtystart = OffsetToLock;
165         if(SizeToLock)
166             This->dirtyend = OffsetToLock + SizeToLock;
167         else
168             This->dirtyend = This->resource.size;
169     }
170
171     return WINED3D_OK;
172 }
173 static HRESULT WINAPI IWineD3DIndexBufferImpl_Unlock(IWineD3DIndexBuffer *iface) {
174     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
175     unsigned long locks = InterlockedDecrement(&This->lockcount);
176     TRACE("(%p)\n", This);
177
178     /* For now load in unlock */
179     if(locks == 0 && This->vbo && (This->dirtyend - This->dirtystart) > 0) {
180         IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
181
182         ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
183
184         ENTER_GL();
185         GL_EXTCALL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, This->vbo));
186         checkGLcall("glBindBufferARB");
187         GL_EXTCALL(glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
188                 This->dirtystart, This->dirtyend - This->dirtystart, This->resource.allocatedMemory + This->dirtystart));
189         checkGLcall("glBufferSubDataARB");
190         LEAVE_GL();
191         This->dirtystart = 0;
192         This->dirtyend = 0;
193         /* TODO: Move loading into preload when the buffer is used, that avoids dirtifying the state */
194         IWineD3DDeviceImpl_MarkStateDirty(device, STATE_INDEXBUFFER);
195     }
196     return WINED3D_OK;
197 }
198 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDesc(IWineD3DIndexBuffer *iface, WINED3DINDEXBUFFER_DESC *pDesc) {
199     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
200
201     TRACE("(%p)\n", This);
202     pDesc->Format = This->resource.format;
203     pDesc->Type   = This->resource.resourceType;
204     pDesc->Usage  = This->resource.usage;
205     pDesc->Pool   = This->resource.pool;
206     pDesc->Size   = This->resource.size;
207     return WINED3D_OK;
208 }
209
210 const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl =
211 {
212     /* IUnknown */
213     IWineD3DIndexBufferImpl_QueryInterface,
214     IWineD3DIndexBufferImpl_AddRef,
215     IWineD3DIndexBufferImpl_Release,
216     /* IWineD3DResource */
217     IWineD3DIndexBufferImpl_GetParent,
218     IWineD3DIndexBufferImpl_GetDevice,
219     IWineD3DIndexBufferImpl_SetPrivateData,
220     IWineD3DIndexBufferImpl_GetPrivateData,
221     IWineD3DIndexBufferImpl_FreePrivateData,
222     IWineD3DIndexBufferImpl_SetPriority,
223     IWineD3DIndexBufferImpl_GetPriority,
224     IWineD3DIndexBufferImpl_PreLoad,
225     IWineD3DIndexBufferImpl_UnLoad,
226     IWineD3DIndexBufferImpl_GetType,
227     /* IWineD3DIndexBuffer */
228     IWineD3DIndexBufferImpl_Lock,
229     IWineD3DIndexBufferImpl_Unlock,
230     IWineD3DIndexBufferImpl_GetDesc
231 };