2 * IWineD3DIndexBuffer Implementation
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
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.
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.
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
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
31 /* *******************************************
32 IWineD3DIndexBuffer IUnknown parts follow
33 ******************************************* */
34 static HRESULT WINAPI IWineD3DIndexBufferImpl_QueryInterface(IWineD3DIndexBuffer *iface, REFIID riid, LPVOID *ppobj)
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);
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);
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);
63 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
65 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
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
72 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
73 checkGLcall("glDeleteBuffersARB");
77 resource_cleanup((IWineD3DResource *)iface);
78 HeapFree(GetProcessHeap(), 0, This);
83 /* ****************************************************
84 IWineD3DIndexBuffer IWineD3DResource parts follow
85 **************************************************** */
86 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDevice(IWineD3DIndexBuffer *iface, IWineD3DDevice** ppDevice) {
87 return resource_get_device((IWineD3DResource *)iface, ppDevice);
90 static HRESULT WINAPI IWineD3DIndexBufferImpl_SetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
91 return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
94 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95 return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
98 static HRESULT WINAPI IWineD3DIndexBufferImpl_FreePrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid) {
99 return resource_free_private_data((IWineD3DResource *)iface, refguid);
102 static DWORD WINAPI IWineD3DIndexBufferImpl_SetPriority(IWineD3DIndexBuffer *iface, DWORD PriorityNew) {
103 return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
106 static DWORD WINAPI IWineD3DIndexBufferImpl_GetPriority(IWineD3DIndexBuffer *iface) {
107 return resource_get_priority((IWineD3DResource *)iface);
110 static void WINAPI IWineD3DIndexBufferImpl_PreLoad(IWineD3DIndexBuffer *iface) {
111 FIXME("iface %p stub!\n", iface);
114 static void WINAPI IWineD3DIndexBufferImpl_UnLoad(IWineD3DIndexBuffer *iface) {
115 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *) iface;
116 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
117 TRACE("(%p)\n", This);
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)
126 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
128 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
129 checkGLcall("glDeleteBuffersARB");
135 static WINED3DRESOURCETYPE WINAPI IWineD3DIndexBufferImpl_GetType(IWineD3DIndexBuffer *iface) {
136 return resource_get_type((IWineD3DResource *)iface);
139 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetParent(IWineD3DIndexBuffer *iface, IUnknown **pParent) {
140 return resource_get_parent((IWineD3DResource *)iface, pParent);
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);
150 InterlockedIncrement(&This->lockcount);
151 *ppbData = This->resource.allocatedMemory + OffsetToLock;
153 if(Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) || This->vbo == 0) {
157 if(This->dirtystart != This->dirtyend) {
158 if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
160 if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
162 This->dirtyend = This->resource.size;
165 This->dirtystart = OffsetToLock;
167 This->dirtyend = OffsetToLock + SizeToLock;
169 This->dirtyend = This->resource.size;
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);
179 /* For now load in unlock */
180 if(locks == 0 && This->vbo && (This->dirtyend - This->dirtystart) > 0) {
181 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
183 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
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");
192 This->dirtystart = 0;
194 /* TODO: Move loading into preload when the buffer is used, that avoids dirtifying the state */
195 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_INDEXBUFFER);
199 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDesc(IWineD3DIndexBuffer *iface, WINED3DINDEXBUFFER_DESC *pDesc) {
200 IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
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;
211 const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl =
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