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