wined3d: Remove a redundant check in handleStreams().
[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 ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->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             ENTER_GL();
63             /* No need to manually unset the buffer. glDeleteBuffers unsets it for the current context,
64              * but not for other contexts. However, because the d3d buffer is destroyed the app has to
65              * unset it before doing the next draw, thus dirtifying the index buffer state and forcing
66              * binding a new buffer
67              */
68             GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
69             checkGLcall("glDeleteBuffersARB");
70             LEAVE_GL();
71         }
72
73         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
74         HeapFree(GetProcessHeap(), 0, This);
75     }
76     return ref;
77 }
78
79 /* ****************************************************
80    IWineD3DIndexBuffer IWineD3DResource parts follow
81    **************************************************** */
82 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDevice(IWineD3DIndexBuffer *iface, IWineD3DDevice** ppDevice) {
83     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
84 }
85
86 static HRESULT WINAPI IWineD3DIndexBufferImpl_SetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
87     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
88 }
89
90 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetPrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
91     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
92 }
93
94 static HRESULT WINAPI IWineD3DIndexBufferImpl_FreePrivateData(IWineD3DIndexBuffer *iface, REFGUID refguid) {
95     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
96 }
97
98 static DWORD WINAPI IWineD3DIndexBufferImpl_SetPriority(IWineD3DIndexBuffer *iface, DWORD PriorityNew) {
99     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
100 }
101
102 static DWORD WINAPI IWineD3DIndexBufferImpl_GetPriority(IWineD3DIndexBuffer *iface) {
103     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
104 }
105
106 static void WINAPI IWineD3DIndexBufferImpl_PreLoad(IWineD3DIndexBuffer *iface) {
107     return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
108 }
109
110 static WINED3DRESOURCETYPE WINAPI IWineD3DIndexBufferImpl_GetType(IWineD3DIndexBuffer *iface) {
111     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
112 }
113
114 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetParent(IWineD3DIndexBuffer *iface, IUnknown **pParent) {
115     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
116 }
117
118 /* ******************************************************
119    IWineD3DIndexBuffer IWineD3DIndexBuffer parts follow
120    ****************************************************** */
121 static HRESULT WINAPI IWineD3DIndexBufferImpl_Lock(IWineD3DIndexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
122     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
123     TRACE("(%p) : offset %d, size %d, Flags=%x\n", This, OffsetToLock, SizeToLock, Flags);
124
125     InterlockedIncrement(&This->lockcount);
126     *ppbData = (BYTE *)This->resource.allocatedMemory + OffsetToLock;
127
128     if(Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) || This->vbo == 0) {
129         return WINED3D_OK;
130     }
131
132     if(This->dirtystart != This->dirtyend) {
133         if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
134         if(SizeToLock) {
135             if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
136         } else {
137             This->dirtyend = This->resource.size;
138         }
139     } else {
140         This->dirtystart = OffsetToLock;
141         if(SizeToLock)
142             This->dirtyend = OffsetToLock + SizeToLock;
143         else
144             This->dirtyend = This->resource.size;
145     }
146
147     return WINED3D_OK;
148 }
149 static HRESULT WINAPI IWineD3DIndexBufferImpl_Unlock(IWineD3DIndexBuffer *iface) {
150     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
151     unsigned long locks = InterlockedDecrement(&This->lockcount);
152     TRACE("(%p)\n", This);
153
154     /* For now load in unlock */
155     if(locks == 0 && This->vbo) {
156         ENTER_GL();
157         GL_EXTCALL(glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, This->vbo));
158         checkGLcall("glBindBufferARB");
159         GL_EXTCALL(glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
160                 This->dirtystart, This->dirtyend - This->dirtystart, This->resource.allocatedMemory + This->dirtystart));
161         checkGLcall("glBufferSubDataARB");
162         LEAVE_GL();
163         This->dirtystart = 0;
164         This->dirtyend = 0;
165         /* TODO: Move loading into preload when the buffer is used, that avoids dirtifying the state */
166         IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_INDEXBUFFER);
167     }
168     return WINED3D_OK;
169 }
170 static HRESULT WINAPI IWineD3DIndexBufferImpl_GetDesc(IWineD3DIndexBuffer *iface, WINED3DINDEXBUFFER_DESC *pDesc) {
171     IWineD3DIndexBufferImpl *This = (IWineD3DIndexBufferImpl *)iface;
172
173     TRACE("(%p)\n", This);
174     pDesc->Format = This->resource.format;
175     pDesc->Type   = This->resource.resourceType;
176     pDesc->Usage  = This->resource.usage;
177     pDesc->Pool   = This->resource.pool;
178     pDesc->Size   = This->resource.size;
179     return WINED3D_OK;
180 }
181
182 const IWineD3DIndexBufferVtbl IWineD3DIndexBuffer_Vtbl =
183 {
184     /* IUnknown */
185     IWineD3DIndexBufferImpl_QueryInterface,
186     IWineD3DIndexBufferImpl_AddRef,
187     IWineD3DIndexBufferImpl_Release,
188     /* IWineD3DResource */
189     IWineD3DIndexBufferImpl_GetParent,
190     IWineD3DIndexBufferImpl_GetDevice,
191     IWineD3DIndexBufferImpl_SetPrivateData,
192     IWineD3DIndexBufferImpl_GetPrivateData,
193     IWineD3DIndexBufferImpl_FreePrivateData,
194     IWineD3DIndexBufferImpl_SetPriority,
195     IWineD3DIndexBufferImpl_GetPriority,
196     IWineD3DIndexBufferImpl_PreLoad,
197     IWineD3DIndexBufferImpl_GetType,
198     /* IWineD3DIndexBuffer */
199     IWineD3DIndexBufferImpl_Lock,
200     IWineD3DIndexBufferImpl_Unlock,
201     IWineD3DIndexBufferImpl_GetDesc
202 };