2 * IWineD3DVertexBuffer Implementation
4 * Copyright 2002-2005 Jason Edmeades
6 * Copyright 2004 Christian Costa
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.
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.
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
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29 #define VB_MAXDECLCHANGES 100 /* After that number we stop converting */
30 #define VB_RESETDECLCHANGE 1000 /* Reset the changecount after that number of draws */
32 /* *******************************************
33 IWineD3DVertexBuffer IUnknown parts follow
34 ******************************************* */
35 static HRESULT WINAPI IWineD3DVertexBufferImpl_QueryInterface(IWineD3DVertexBuffer *iface, REFIID riid, LPVOID *ppobj)
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);
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);
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);
66 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
67 checkGLcall("glDeleteBuffersARB");
71 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
72 HeapFree(GetProcessHeap(), 0, This);
77 /* ****************************************************
78 IWineD3DVertexBuffer IWineD3DResource parts follow
79 **************************************************** */
80 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer *iface, IWineD3DDevice** ppDevice) {
81 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
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);
88 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
89 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
92 static HRESULT WINAPI IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer *iface, REFGUID refguid) {
93 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
96 static DWORD WINAPI IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer *iface, DWORD PriorityNew) {
97 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
100 static DWORD WINAPI IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer *iface) {
101 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
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) {
108 for(i = num - 1; i >= 0; i--) {
110 float *p = (float *) (((int) src + (int) pos) + i * stride);
112 /* rhw conversion like in drawStridedSlow */
113 if(p[3] == 1.0 || ((p[3] < eps) && (p[3] > -eps))) {
124 p = (float *) ((int) dst + i * stride + (int) pos);
131 DWORD srcColor, *dstColor = (DWORD *) (dst + i * stride + (int) diffuse);
132 srcColor = * (DWORD *) ( ((int) src + (int) diffuse) + i * stride);
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
137 * 0xff000000: Alpha mask
138 * 0x00ff0000: Blue mask
139 * 0x0000ff00: Green mask
140 * 0x000000ff: Red mask
144 *dstColor |= (srcColor & 0xff00ff00) ; /* Alpha Green */
145 *dstColor |= (srcColor & 0x00ff0000) >> 16; /* Red */
146 *dstColor |= (srcColor & 0x000000ff) << 16; /* Blue */
149 DWORD srcColor, *dstColor = (DWORD *) (dst + i * stride + (int) specular);
150 srcColor = * (DWORD *) ( ((int) src + (int) specular) + i * stride);
152 /* Similar to diffuse
153 * TODO: Write the alpha value out for fog coords
156 *dstColor |= (srcColor & 0xff00ff00) ; /* Alpha Green */
157 *dstColor |= (srcColor & 0x00ff0000) >> 16; /* Red */
158 *dstColor |= (srcColor & 0x000000ff) << 16; /* Blue */
163 inline BOOL WINAPI IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl *This)
165 WineDirect3DVertexStridedData strided;
166 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
168 memset(&strided, 0, sizeof(strided));
169 /* There are certain vertex data types that need to be fixed up. The Vertex Buffers FVF doesn't
170 * help finding them, only the vertex declaration or the device FVF can determine that at drawPrim
171 * time. Rules are as follows:
173 * -> No modification when Vertex Shaders are used
174 * -> Fix up position1 and position 2 if they are XYZRHW
175 * -> Fix up diffuse color
176 * -> Fix up specular color
178 * The Declaration is only known at drawing time, and it can change from draw to draw. If any converted values
179 * are changed, the whole buffer has to be reconverted and reloaded. (Converting is SLOW, so if this happens too
180 * often PreLoad stops converting entirely and falls back to drawStridedSlow).
183 * -> New semantics that have to be converted appear
184 * -> The position of semantics that have to be converted changes
185 * -> The stride of the vertex changed AND there is stuff that needs conversion
186 * -> (If a vertex shader is bound and in use assume that nothing that needs conversion is there)
189 * TRUE: Reload is needed
193 if(device->stateBlock->vertexShader && ((IWineD3DVertexShaderImpl *) device->stateBlock->vertexShader)->baseShader.function) {
194 /* Assume no conversion */
195 memset(&strided, 0, sizeof(strided));
197 /* we need a copy because we modify some params */
198 memcpy(&strided, &device->strided_streams, sizeof(strided));
200 /* Filter out data that does not come from this VBO */
201 if(strided.u.s.position.VBO != This->vbo) memset(&strided.u.s.position, 0, sizeof(strided.u.s.position));
202 if(strided.u.s.diffuse.VBO != This->vbo) memset(&strided.u.s.diffuse, 0, sizeof(strided.u.s.diffuse));
203 if(strided.u.s.specular.VBO != This->vbo) memset(&strided.u.s.specular, 0, sizeof(strided.u.s.specular));
204 if(strided.u.s.position2.VBO != This->vbo) memset(&strided.u.s.position2, 0, sizeof(strided.u.s.position2));
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));
213 /* We have a declaration now in the buffer */
214 This->Flags |= VBFLAG_HASDESC;
216 /* Find out if reload is needed
217 * 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.
219 * 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
220 * and does not exist now all 3 values will be equal(=0).
222 * 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
223 * 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
224 * buffers at the same time and get the position from one and the color from the other buffer.
226 if( /* Position transformed vs untransformed */
227 ((This->strided.u.s.position_transformed || strided.u.s.position_transformed) &&
228 This->strided.u.s.position.lpData != strided.u.s.position.lpData) ||
229 /* Diffuse position and data type */
230 This->strided.u.s.diffuse.lpData != strided.u.s.diffuse.lpData || This->strided.u.s.diffuse.dwStride != strided.u.s.diffuse.dwStride ||
231 This->strided.u.s.diffuse.dwType != strided.u.s.diffuse.dwType ||
232 /* Specular position and data type */
233 This->strided.u.s.specular.lpData != strided.u.s.specular.lpData || This->strided.u.s.specular.dwStride != strided.u.s.specular.dwStride ||
234 This->strided.u.s.specular.dwType != strided.u.s.specular.dwType) {
236 TRACE("Declaration changed, reloading buffer\n");
237 /* Set the new description */
238 memcpy(&This->strided, &strided, sizeof(strided));
245 static void WINAPI IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer *iface) {
246 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
248 UINT start = 0, end = 0, stride = 0;
249 BOOL declChanged = FALSE;
250 TRACE("(%p)->()\n", This);
252 if(This->Flags & VBFLAG_LOAD) {
253 return; /* Already doing that stuff */
257 /* TODO: Make converting independent from VBOs */
258 return; /* Not doing any conversion */
261 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
262 if(This->resource.wineD3DDevice->isInDraw && This->Flags & VBFLAG_STREAM) {
263 declChanged = IWineD3DVertexBufferImpl_FindDecl(This);
264 } else if(This->Flags & VBFLAG_HASDESC) {
265 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
266 * the stream source state handler will call PreLoad again and the change will be cought
269 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
270 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
271 * declaration for the buffer can be found
276 /* If applications change the declaration over and over, reconverting all the time is a huge
277 * performance hit. So count the declaration changes and release the VBO if there are too much
278 * of them(and thus stop converting)
284 if(This->declChanges > VB_MAXDECLCHANGES) {
285 if(This->resource.allocatedMemory) {
286 FIXME("Too much declaration changes, stopping converting\n");
288 GL_EXTCALL(glDeleteBuffersARB(1, &This->vbo));
289 checkGLcall("glDeleteBuffersARB");
293 /* The stream source state handler might have read the memory of the vertex buffer already
294 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
295 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
298 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_STREAMSRC);
302 /* Otherwise do not bother to release the VBO. If we're doing direct locking now,
303 * and the declarations changed the code below will fetch the VBO's contents, convert
304 * and on the next decl change the data will be in sysmem too and we can just release the VBO
308 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
309 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
310 * decl changes and reset the decl change count after a specific number of them
313 if(This->draws > VB_RESETDECLCHANGE) This->declChanges = 0;
317 /* The declaration changed, reload the whole buffer */
318 WARN("Reloading buffer because of decl change\n");
320 end = This->resource.size;
321 } else if(This->Flags & VBFLAG_DIRTY) {
322 /* No decl change, but dirty data, reload the changed stuff */
323 start = This->dirtystart;
324 end = This->dirtyend;
326 /* Desc not changed, buffer not dirty, nothing to do :-) */
330 /* Mark the buffer clean */
331 This->Flags &= ~VBFLAG_DIRTY;
332 This->dirtystart = 0;
335 /* If there was no conversion done before, then resource.allocatedMemory does not exist
336 * because locking was done directly into the VBO. In this case get the data out
338 if(declChanged && !This->resource.allocatedMemory) {
340 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), 0, This->resource.size);
341 if(!This->resource.allocatedMemory) {
342 ERR("Out of memory when allocating memory for a vertex buffer\n");
345 ERR("Was locking directly into the VBO, reading data back because conv is needed\n");
348 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
349 checkGLcall("glBindBufferARB");
350 data = GL_EXTCALL(glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB));
352 ERR("glMapBuffer failed!\n");
356 memcpy(This->resource.allocatedMemory, data, This->resource.size);
357 GL_EXTCALL(glUnmapBufferARB(GL_ARRAY_BUFFER_ARB));
358 checkGLcall("glUnmapBufferARB");
362 if (This->strided.u.s.position.dwStride) stride = This->strided.u.s.position.dwStride;
363 else if(This->strided.u.s.specular.dwStride) stride = This->strided.u.s.specular.dwStride;
364 else if(This->strided.u.s.diffuse.dwStride) stride = This->strided.u.s.diffuse.dwStride;
366 /* That means that there is nothing to fixup. Upload everything into the VBO and
367 * free This->resource.allocatedMemory
369 TRACE("No conversion needed, locking directly into the VBO in future\n");
372 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
373 checkGLcall("glBindBufferARB");
374 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
375 checkGLcall("glBufferSubDataARB");
380 /* OK, we have the original data from the app, the description of the buffer and the dirty area.
381 * so convert the stuff
383 data = HeapAlloc(GetProcessHeap(), 0, end-start);
385 ERR("Out of memory\n");
388 memcpy(data, This->resource.allocatedMemory + start, end - start);
390 fixup_vertices(data, data, stride, ( end - start) / stride,
392 This->strided.u.s.position.lpData, /* Data location */
393 This->strided.u.s.position_transformed, /* Do convert? */
395 This->strided.u.s.diffuse.lpData, /* Location */
396 This->strided.u.s.diffuse.dwType == WINED3DDECLTYPE_SHORT4 || This->strided.u.s.diffuse.dwType == WINED3DDECLTYPE_D3DCOLOR, /* Convert? */
398 This->strided.u.s.specular.lpData, /* location */
399 This->strided.u.s.specular.dwType == WINED3DDECLTYPE_SHORT4 || This->strided.u.s.specular.dwType == WINED3DDECLTYPE_D3DCOLOR);
402 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
403 checkGLcall("glBindBufferARB");
404 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, start, end - start, data));
405 checkGLcall("glBufferSubDataARB");
408 HeapFree(GetProcessHeap(), 0, data);
411 static WINED3DRESOURCETYPE WINAPI IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer *iface) {
412 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
415 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer *iface, IUnknown **pParent) {
416 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
419 /* ******************************************************
420 IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
421 ****************************************************** */
422 static HRESULT WINAPI IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer *iface, UINT OffsetToLock, UINT SizeToLock, BYTE** ppbData, DWORD Flags) {
423 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
425 TRACE("(%p)->%d, %d, %p, %08x\n", This, OffsetToLock, SizeToLock, ppbData, Flags);
427 InterlockedIncrement(&This->lockcount);
429 if(This->Flags & VBFLAG_DIRTY) {
430 if(This->dirtystart > OffsetToLock) This->dirtystart = OffsetToLock;
432 if(This->dirtyend < OffsetToLock + SizeToLock) This->dirtyend = OffsetToLock + SizeToLock;
434 This->dirtyend = This->resource.size;
437 This->dirtystart = OffsetToLock;
439 This->dirtyend = OffsetToLock + SizeToLock;
441 This->dirtyend = This->resource.size;
444 if(This->resource.allocatedMemory) {
445 data = This->resource.allocatedMemory;
446 This->Flags |= VBFLAG_DIRTY;
448 GLenum mode = GL_READ_WRITE_ARB;
449 /* Return data to the VBO */
451 TRACE("Locking directly into the buffer\n");
453 if((This->resource.usage & WINED3DUSAGE_WRITEONLY) || ( Flags & WINED3DLOCK_DISCARD) ) {
454 mode = GL_WRITE_ONLY_ARB;
455 } else if( Flags & (WINED3DLOCK_READONLY | WINED3DLOCK_NO_DIRTY_UPDATE) ) {
456 mode = GL_READ_ONLY_ARB;
460 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
461 checkGLcall("glBindBufferARB");
462 data = GL_EXTCALL(glMapBufferARB(GL_ARRAY_BUFFER_ARB, mode));
465 ERR("glMapBuffer failed\n");
466 return WINED3DERR_INVALIDCALL;
469 *ppbData = data + OffsetToLock;
471 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This, data + OffsetToLock, data, OffsetToLock);
472 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
475 HRESULT WINAPI IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer *iface) {
476 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *) iface;
478 TRACE("(%p)\n", This);
480 lockcount = InterlockedDecrement(&This->lockcount);
482 /* Delay loading the buffer until everything is unlocked */
483 TRACE("Ignoring the unlock\n");
487 if(!This->resource.allocatedMemory) {
489 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB, This->vbo));
490 checkGLcall("glBindBufferARB");
491 GL_EXTCALL(glUnmapBufferARB(GL_ARRAY_BUFFER_ARB));
492 checkGLcall("glUnmapBufferARB");
497 static HRESULT WINAPI IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer *iface, WINED3DVERTEXBUFFER_DESC *pDesc) {
498 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
500 TRACE("(%p)\n", This);
501 pDesc->Format = This->resource.format;
502 pDesc->Type = This->resource.resourceType;
503 pDesc->Usage = This->resource.usage;
504 pDesc->Pool = This->resource.pool;
505 pDesc->Size = This->resource.size;
506 pDesc->FVF = This->fvf;
510 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl =
513 IWineD3DVertexBufferImpl_QueryInterface,
514 IWineD3DVertexBufferImpl_AddRef,
515 IWineD3DVertexBufferImpl_Release,
516 /* IWineD3DResource */
517 IWineD3DVertexBufferImpl_GetParent,
518 IWineD3DVertexBufferImpl_GetDevice,
519 IWineD3DVertexBufferImpl_SetPrivateData,
520 IWineD3DVertexBufferImpl_GetPrivateData,
521 IWineD3DVertexBufferImpl_FreePrivateData,
522 IWineD3DVertexBufferImpl_SetPriority,
523 IWineD3DVertexBufferImpl_GetPriority,
524 IWineD3DVertexBufferImpl_PreLoad,
525 IWineD3DVertexBufferImpl_GetType,
526 /* IWineD3DVertexBuffer */
527 IWineD3DVertexBufferImpl_Lock,
528 IWineD3DVertexBufferImpl_Unlock,
529 IWineD3DVertexBufferImpl_GetDesc
532 BYTE* WINAPI IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer* iface, DWORD iOffset, GLint *vbo) {
533 IWineD3DVertexBufferImpl *This = (IWineD3DVertexBufferImpl *)iface;
537 return This->resource.allocatedMemory + iOffset;
539 return (BYTE *) iOffset;
543 HRESULT WINAPI IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer* iface) {