wined3d: Don't use WINED3DSHADERDECLUSAGE_PSIZE / WINED3DSHADERDECLUSAGE_FOG.
[wine] / dlls / wined3d / vertexbuffer.c
1 /*
2  * IWineD3DVertexBuffer Implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
28
29 #define VB_MAXDECLCHANGES     100     /* After that number we stop converting */
30 #define VB_RESETDECLCHANGE    1000    /* Reset the changecount after that number of draws */
31
32 /* *******************************************
33    IWineD3DVertexBuffer IUnknown parts follow
34    ******************************************* */
35 static HRESULT WINAPI IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer *iface, REFIID riid, LPVOID *ppobj)
36 {
37     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
38     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
39     if (IsEqualGUID(riid, &IID_IUnknown)
40         || IsEqualGUID(riid, &IID_IWineD3DBase)
41         || IsEqualGUID(riid, &IID_IWineD3DResource)
42         || IsEqualGUID(riid, &IID_IWineD3DVertexBuffer)){
43         IUnknown_AddRef(iface);
44         *ppobj = This;
45         return S_OK;
46     }
47     *ppobj = NULL;
48     return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI IWineD3DVertexBufferImpl_AddRef(IWineD3DVertexBuffer *iface) {
52     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
53     ULONG ref = InterlockedIncrement(&This->resource.ref);
54     TRACE("(%p) : AddRef increasing from %d\n", This, ref - 1);
55     return ref;
56 }
57
58 static ULONG WINAPI IWineD3DVertexBufferImpl_Release(IWineD3DVertexBuffer *iface) {
59     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
60     ULONG ref = InterlockedDecrement(&This->resource.ref);
61     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
62     if (ref == 0) {
63
64         if(This->vbo) {
65             ENTER_GL();
66             GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
67             checkGLcall("glDeleteBuffersARB");
68             LEAVE_GL();
69         }
70
71         IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
72         HeapFree(GetProcessHeap(), 0, This);
73     }
74     return ref;
75 }
76
77 /* ****************************************************
78    IWineD3DVertexBuffer IWineD3DResource parts follow
79    **************************************************** */
80 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer *iface, IWineD3DDevice** ppDevice) {
81     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
82 }
83
84 static HRESULT WINAPI IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
85     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
86 }
87
88 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
89     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
90 }
91
92 static HRESULT WINAPI IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid) {
93     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
94 }
95
96 static DWORD    WINAPI IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer *iface, DWORD PriorityNew) {
97     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
98 }
99
100 static DWORD    WINAPI IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer *iface) {
101     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
102 }
103
104 static void fixup_vertices(BYTE *src, BYTE *dst, int stride, int num, BYTE *pos, BOOL haspos, BYTE *diffuse, BOOL hasdiffuse, BYTE *specular, BOOL hasspecular) {
105     int i;
106     float x, y, z, w;
107
108     for(i = num - 1; i >= 0; i--) {
109         if(haspos) {
110             float *p = (float *) (((int) src + (int) pos) + i * stride);
111
112             /* rhw conversion like in drawStridedSlow */
113             if(p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps))) {
114                 x = p[0];
115                 y = p[1];
116                 z = p[2];
117                 w = 1.0;
118             } else {
119                 w = 1.0 / p[3];
120                 x = p[0] * w;
121                 y = p[1] * w;
122                 z = p[2] * w;
123             }
124             p = (float *) ((int) dst + i * stride + (int) pos);
125             p[0] = x;
126             p[1] = y;
127             p[2] = z;
128             p[3] = w;
129         }
130         if(hasdiffuse) {
131             DWORD srcColor, *dstColor = (DWORD *) (dst + i * stride + (int) diffuse);
132             srcColor = * (DWORD *) ( ((int) src + (int) diffuse) + i * stride);
133
134             /* Color conversion like in drawStridedSlow. watch out for little endianity
135             * If we want that stuff to work on big endian machines too we have to consider more things
136             *
137             * 0xff000000: Alpha mask
138             * 0x00ff0000: Blue mask
139             * 0x0000ff00: Green mask
140             * 0x000000ff: Red mask
141             */
142
143             *dstColor = 0;
144             *dstColor |= (srcColor & 0xff00ff00)      ;   /* Alpha Green */
145             *dstColor |= (srcColor & 0x00ff0000) >> 16;   /* Red */
146             *dstColor |= (srcColor & 0x000000ff) << 16;   /* Blue */
147         }
148         if(hasspecular) {
149             DWORD srcColor, *dstColor = (DWORD *) (dst + i * stride + (int) specular);
150             srcColor = * (DWORD *) ( ((int) src + (int) specular) + i * stride);
151
152             /* Similar to diffuse
153              * TODO: Write the alpha value out for fog coords
154              */
155             *dstColor = 0;
156             *dstColor |= (srcColor & 0xff00ff00)      ;   /* Alpha Green */
157             *dstColor |= (srcColor & 0x00ff0000) >> 16;   /* Red */
158             *dstColor |= (srcColor & 0x000000ff) << 16;   /* Blue */
159         }
160     }
161 }
162
163 inline BOOL WINAPI IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl *This)
164 {
165     WineDirect3DVertexStridedData strided;
166     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
167
168     /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
169      * Once we have our declaration there is no need to look it up again.
170      */
171     if(((IWineD3DImpl *)device->wineD3D)->dxVersion == 7 && This->Flags & VBFLAG_HASDESC) {
172         return FALSE;
173     }
174
175     memset(&strided, 0, sizeof(strided));
176     /* There are certain vertex data types that need to be fixed up. The Vertex Buffers FVF doesn't
177      * help finding them, only the vertex declaration or the device FVF can determine that at drawPrim
178      * time. Rules are as follows:
179      *
180      * -> No modification when Vertex Shaders are used
181      * -> Fix up position1 and position 2 if they are XYZRHW
182      * -> Fix up diffuse color
183      * -> Fix up specular color
184      *
185      * The Declaration is only known at drawing time, and it can change from draw to draw. If any converted values
186      * are changed, the whole buffer has to be reconverted and reloaded. (Converting is SLOW, so if this happens too
187      * often PreLoad stops converting entirely and falls back to drawStridedSlow).
188      *
189      * Reconvert if:
190      * -> New semantics that have to be converted appear
191      * -> The position of semantics that have to be converted changes
192      * -> The stride of the vertex changed AND there is stuff that needs conversion
193      * -> (If a vertex shader is bound and in use assume that nothing that needs conversion is there)
194      *
195      * Return values:
196      *  TRUE: Reload is needed
197      *  FALSE: otherwise
198      */
199
200     if(device->stateBlock->vertexShader && ((IWineD3DVertexShaderImpl *) device->stateBlock->vertexShader)->baseShader.function) {
201         /* Assume no conversion */
202         memset(&strided, 0, sizeof(strided));
203     } else {
204         /* we need a copy because we modify some params */
205         memcpy(&strided, &device->strided_streams, sizeof(strided));
206
207         /* Filter out data that does not come from this VBO */
208         if(strided.u.s.position.VBO != This->vbo)    memset(&strided.u.s.position, 0, sizeof(strided.u.s.position));
209         if(strided.u.s.diffuse.VBO != This->vbo)     memset(&strided.u.s.diffuse, 0, sizeof(strided.u.s.diffuse));
210         if(strided.u.s.specular.VBO != This->vbo)    memset(&strided.u.s.specular, 0, sizeof(strided.u.s.specular));
211         if(strided.u.s.position2.VBO != This->vbo)   memset(&strided.u.s.position2, 0, sizeof(strided.u.s.position2));
212     }
213
214     /* Filter out data that does not come from this VBO */
215     if(strided.u.s.position.VBO != This->vbo)    memset(&strided.u.s.position, 0, sizeof(strided.u.s.position));
216     if(strided.u.s.diffuse.VBO != This->vbo)     memset(&strided.u.s.diffuse, 0, sizeof(strided.u.s.diffuse));
217     if(strided.u.s.specular.VBO != This->vbo)    memset(&strided.u.s.specular, 0, sizeof(strided.u.s.specular));
218     if(strided.u.s.position2.VBO != This->vbo)   memset(&strided.u.s.position2, 0, sizeof(strided.u.s.position2));
219
220     /* We have a declaration now in the buffer */
221     This->Flags |= VBFLAG_HASDESC;
222
223     /* Find out if reload is needed
224      * Position of the semantic in the vertex and the stride must be equal to the stored type. Don't mind if only unconverted stuff changed.
225      *
226      * If some stuff does not exist in the buffer, then lpData, dwStride and dwType are memsetted to 0. So if the semantic didn't exist before
227      * and does not exist now all 3 values will be equal(=0).
228      *
229      * Checking the lpData field alone is not enough, because data may appear at offset 0 in the buffer. This is the same location as nonexistent
230      * data uses, so we have to check the type and stride too. Colors can be at offset 0 too, because it is perfectly fine to render from 2 or more
231      * buffers at the same time and get the position from one and the color from the other buffer.
232      */
233     if( /* Position transformed vs untransformed */
234         ((This->strided.u.s.position_transformed || strided.u.s.position_transformed) &&
235           This->strided.u.s.position.lpData != strided.u.s.position.lpData) ||
236         /* Diffuse position and data type */
237         This->strided.u.s.diffuse.lpData != strided.u.s.diffuse.lpData || This->strided.u.s.diffuse.dwStride != strided.u.s.diffuse.dwStride ||
238          This->strided.u.s.diffuse.dwType != strided.u.s.diffuse.dwType ||
239         /* Specular position and data type */
240         This->strided.u.s.specular.lpData != strided.u.s.specular.lpData || This->strided.u.s.specular.dwStride != strided.u.s.specular.dwStride ||
241          This->strided.u.s.specular.dwType != strided.u.s.specular.dwType) {
242
243         TRACE("Declaration changed, reloading buffer\n");
244         /* Set the new description */
245         memcpy(&This->strided, &strided, sizeof(strided));
246         return TRUE;
247     }
248
249     return FALSE;
250 }
251
252 static void     WINAPI IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer *iface) {
253     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
254     BYTE *data;
255     UINT start = 0, end = 0, stride = 0;
256     BOOL declChanged = FALSE;
257     TRACE("(%p)->()\n", This);
258
259     if(This->Flags & VBFLAG_LOAD) {
260         return; /* Already doing that stuff */
261     }
262
263     if(!This->vbo) {
264         /* TODO: Make converting independent from VBOs */
265         return; /* Not doing any conversion */
266     }
267
268     /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
269     if(This->resource.wineD3DDevice->isInDraw && This->bindCount > 0) {
270         declChanged = IWineD3DVertexBufferImpl_FindDecl(This);
271     } else if(This->Flags & VBFLAG_HASDESC) {
272         /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
273          * the stream source state handler will call PreLoad again and the change will be cought
274          */
275     } else {
276         /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
277          * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
278          * declaration for the buffer can be found
279          */
280         return;
281     }
282
283     /* If applications change the declaration over and over, reconverting all the time is a huge
284      * performance hit. So count the declaration changes and release the VBO if there are too much
285      * of them(and thus stop converting)
286      */
287     if(declChanged) {
288         This->declChanges++;
289         This->draws = 0;
290
291         if(This->declChanges > VB_MAXDECLCHANGES) {
292             if(This->resource.allocatedMemory) {
293                 FIXME("Too much declaration changes, stopping converting\n");
294                 ENTER_GL();
295                 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
296                 checkGLcall("glDeleteBuffersARB");
297                 LEAVE_GL();
298                 This->vbo = 0;
299
300                 /* The stream source state handler might have read the memory of the vertex buffer already
301                  * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
302                  * to force a reload. This happens only once per changed vertexbuffer and should occur rather
303                  * rarely
304                  */
305                 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_STREAMSRC);
306
307                 return;
308             }
309             /* Otherwise do not bother to release the VBO. If we're doing direct locking now,
310              * and the declarations changed the code below will fetch the VBO's contents, convert
311              * and on the next decl change the data will be in sysmem too and we can just release the VBO
312              */
313         }
314     } else {
315         /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
316          * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
317          * decl changes and reset the decl change count after a specific number of them
318          */
319         This->draws++;
320         if(This->draws > VB_RESETDECLCHANGE) This->declChanges = 0;
321     }
322
323     if(declChanged) {
324         /* The declaration changed, reload the whole buffer */
325         WARN("Reloading buffer because of decl change\n");
326         start = 0;
327         end = This->resource.size;
328     } else if(This->Flags & VBFLAG_DIRTY) {
329         /* No decl change, but dirty data, reload the changed stuff */
330         start = This->dirtystart;
331         end = This->dirtyend;
332     } else {
333         /* Desc not changed, buffer not dirty, nothing to do :-) */
334         return;
335     }
336
337     /* Mark the buffer clean */
338     This->Flags &= ~VBFLAG_DIRTY;
339     This->dirtystart = 0;
340     This->dirtyend = 0;
341
342     /* If there was no conversion done before, then resource.allocatedMemory does not exist
343      * because locking was done directly into the VBO. In this case get the data out
344      */
345     if(declChanged && !This->resource.allocatedMemory) {
346
347         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
348         if(!This->resource.allocatedMemory) {
349             ERR("Out of memory when allocating memory for a vertex buffer\n");
350             return;
351         }
352         ERR("Was locking directly into the VBO, reading data back because conv is needed\n");
353
354         ENTER_GL();
355         GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
356         checkGLcall("glBindBufferARB");
357         data = GL_EXTCALL(glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB));
358         if(!data) {
359             ERR("glMapBuffer failed!\n");
360             LEAVE_GL();
361             return;
362         }
363         memcpy(This->resource.allocatedMemory, data, This->resource.size);
364         GL_EXTCALL(glUnmapBufferARB(GL_ARRAY_BUFFER_ARB));
365         checkGLcall("glUnmapBufferARB");
366         LEAVE_GL();
367     }
368
369     if     (This->strided.u.s.position.dwStride) stride = This->strided.u.s.position.dwStride;
370     else if(This->strided.u.s.specular.dwStride) stride = This->strided.u.s.specular.dwStride;
371     else if(This->strided.u.s.diffuse.dwStride)  stride = This->strided.u.s.diffuse.dwStride;
372     else {
373         /* That means that there is nothing to fixup. Upload everything into the VBO and
374          * free This->resource.allocatedMemory
375          */
376         TRACE("No conversion needed, locking directly into the VBO in future\n");
377
378         ENTER_GL();
379         GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
380         checkGLcall("glBindBufferARB");
381         GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
382         checkGLcall("glBufferSubDataARB");
383         LEAVE_GL();
384         return;
385     }
386
387     /* OK, we have the original data from the app, the description of the buffer and the dirty area.
388      * so convert the stuff
389      */
390     data = HeapAlloc(GetProcessHeap(), 0, end-start);
391     if(!data) {
392         ERR("Out of memory\n");
393         return;
394     }
395     memcpy(data, This->resource.allocatedMemory + start, end - start);
396
397     fixup_vertices(data, data, stride, ( end - start) / stride,
398                    /* Position */
399                    This->strided.u.s.position.lpData, /* Data location */
400                    This->strided.u.s.position_transformed, /* Do convert? */
401                    /* Diffuse color */
402                    This->strided.u.s.diffuse.lpData, /* Location */
403                    This->strided.u.s.diffuse.dwType == WINED3DDECLTYPE_SHORT4 || This->strided.u.s.diffuse.dwType == WINED3DDECLTYPE_D3DCOLOR, /* Convert? */
404                    /* specular color */
405                    This->strided.u.s.specular.lpData, /* location */
406                    This->strided.u.s.specular.dwType == WINED3DDECLTYPE_SHORT4 || This->strided.u.s.specular.dwType == WINED3DDECLTYPE_D3DCOLOR);
407
408     ENTER_GL();
409     GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
410     checkGLcall("glBindBufferARB");
411     GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data));
412     checkGLcall("glBufferSubDataARB");
413     LEAVE_GL();
414
415     HeapFree(GetProcessHeap(), 0, data);
416 }
417
418 static WINED3DRESOURCETYPE WINAPI IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer *iface) {
419     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
420 }
421
422 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer *iface, IUnknown **pParent) {
423     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
424 }
425
426 /* ******************************************************
427    IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
428    ****************************************************** */
429 static HRESULT  WINAPI IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
430     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
431     BYTE *data;
432     TRACE("(%p)->%d, %d, %p, %08x\n", This, OffsetToLock, SizeToLock, ppbData, Flags);
433
434     InterlockedIncrement(&This->lockcount);
435
436     if(This->Flags & VBFLAG_DIRTY) {
437         if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
438         if(SizeToLock) {
439             if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
440         } else {
441             This->dirtyend = This->resource.size;
442         }
443     } else {
444         This->dirtystart = OffsetToLock;
445         if(SizeToLock)
446             This->dirtyend = OffsetToLock + SizeToLock;
447         else
448             This->dirtyend = This->resource.size;
449     }
450
451     if(This->resource.allocatedMemory) {
452           data = This->resource.allocatedMemory;
453           This->Flags |= VBFLAG_DIRTY;
454     } else {
455         GLenum mode = GL_READ_WRITE_ARB;
456         /* Return data to the VBO */
457
458         TRACE("Locking directly into the buffer\n");
459
460         if((This->resource.usage & WINED3DUSAGE_WRITEONLY) || ( Flags & WINED3DLOCK_DISCARD) ) {
461             mode = GL_WRITE_ONLY_ARB;
462         } else if( Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) ) {
463             mode = GL_READ_ONLY_ARB;
464         }
465
466         ENTER_GL();
467         GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
468         checkGLcall("glBindBufferARB");
469         data = GL_EXTCALL(glMapBufferARB(GL_ARRAY_BUFFER_ARB, mode));
470         LEAVE_GL();
471         if(!data) {
472             ERR("glMapBuffer failed\n");
473             return WINED3DERR_INVALIDCALL;
474         }
475     }
476     *ppbData = data + OffsetToLock;
477
478     TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This, data + OffsetToLock, data, OffsetToLock);
479     /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
480     return WINED3D_OK;
481 }
482 HRESULT  WINAPI IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer *iface) {
483     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
484     LONG lockcount;
485     TRACE("(%p)\n", This);
486
487     lockcount = InterlockedDecrement(&This->lockcount);
488     if(lockcount > 0) {
489         /* Delay loading the buffer until everything is unlocked */
490         TRACE("Ignoring the unlock\n");
491         return D3D_OK;
492     }
493
494     if(!This->resource.allocatedMemory) {
495         ENTER_GL();
496         GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
497         checkGLcall("glBindBufferARB");
498         GL_EXTCALL(glUnmapBufferARB(GL_ARRAY_BUFFER_ARB));
499         checkGLcall("glUnmapBufferARB");
500         LEAVE_GL();
501     } else if(This->Flags & VBFLAG_HASDESC){
502         IWineD3DVertexBufferImpl_PreLoad(iface);
503     }
504     return WINED3D_OK;
505 }
506 static HRESULT  WINAPI IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer *iface, WINED3DVERTEXBUFFER_DESC *pDesc) {
507     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
508
509     TRACE("(%p)\n", This);
510     pDesc->Format = This->resource.format;
511     pDesc->Type   = This->resource.resourceType;
512     pDesc->Usage  = This->resource.usage;
513     pDesc->Pool   = This->resource.pool;
514     pDesc->Size   = This->resource.size;
515     pDesc->FVF    = This->fvf;
516     return WINED3D_OK;
517 }
518
519 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl =
520 {
521     /* IUnknown */
522     IWineD3DVertexBufferImpl_QueryInterface,
523     IWineD3DVertexBufferImpl_AddRef,
524     IWineD3DVertexBufferImpl_Release,
525     /* IWineD3DResource */
526     IWineD3DVertexBufferImpl_GetParent,
527     IWineD3DVertexBufferImpl_GetDevice,
528     IWineD3DVertexBufferImpl_SetPrivateData,
529     IWineD3DVertexBufferImpl_GetPrivateData,
530     IWineD3DVertexBufferImpl_FreePrivateData,
531     IWineD3DVertexBufferImpl_SetPriority,
532     IWineD3DVertexBufferImpl_GetPriority,
533     IWineD3DVertexBufferImpl_PreLoad,
534     IWineD3DVertexBufferImpl_GetType,
535     /* IWineD3DVertexBuffer */
536     IWineD3DVertexBufferImpl_Lock,
537     IWineD3DVertexBufferImpl_Unlock,
538     IWineD3DVertexBufferImpl_GetDesc
539 };
540
541 BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo) {
542     IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
543
544     *vbo = This->vbo;
545     if(This->vbo == 0) {
546         return This->resource.allocatedMemory + iOffset;
547     } else {
548         return (BYTE *) iOffset;
549     }
550 }
551
552 HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface) {
553   return WINED3D_OK;
554 }