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 This->resource.wineD3DDevice->adapter->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);
65 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
68 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
69 GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
70 checkGLcall("glDeleteBuffersARB");
74 IWineD3DResourceImpl_CleanUp((IWineD3DResource
*)iface
);
75 HeapFree(GetProcessHeap(), 0, This
);
80 /* ****************************************************
81 IWineD3DVertexBuffer IWineD3DResource parts follow
82 **************************************************** */
83 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetDevice(IWineD3DVertexBuffer
*iface
, IWineD3DDevice
** ppDevice
) {
84 return IWineD3DResourceImpl_GetDevice((IWineD3DResource
*)iface
, ppDevice
);
87 static HRESULT WINAPI
IWineD3DVertexBufferImpl_SetPrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
, CONST
void* pData
, DWORD SizeOfData
, DWORD Flags
) {
88 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, SizeOfData
, Flags
);
91 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetPrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
, void* pData
, DWORD
* pSizeOfData
) {
92 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, pSizeOfData
);
95 static HRESULT WINAPI
IWineD3DVertexBufferImpl_FreePrivateData(IWineD3DVertexBuffer
*iface
, REFGUID refguid
) {
96 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource
*)iface
, refguid
);
99 static DWORD WINAPI
IWineD3DVertexBufferImpl_SetPriority(IWineD3DVertexBuffer
*iface
, DWORD PriorityNew
) {
100 return IWineD3DResourceImpl_SetPriority((IWineD3DResource
*)iface
, PriorityNew
);
103 static DWORD WINAPI
IWineD3DVertexBufferImpl_GetPriority(IWineD3DVertexBuffer
*iface
) {
104 return IWineD3DResourceImpl_GetPriority((IWineD3DResource
*)iface
);
107 static void fixup_vertices(BYTE
*src
, BYTE
*dst
, int stride
, int num
, BYTE
*pos
, BOOL haspos
, BYTE
*diffuse
, BOOL hasdiffuse
, BYTE
*specular
, BOOL hasspecular
) {
111 for(i
= num
- 1; i
>= 0; i
--) {
113 float *p
= (float *) (((int) src
+ (int) pos
) + i
* stride
);
115 /* rhw conversion like in drawStridedSlow */
116 if(p
[3] == 1.0 || ((p
[3] < eps
) && (p
[3] > -eps
))) {
127 p
= (float *) ((int) dst
+ i
* stride
+ (int) pos
);
134 DWORD srcColor
, *dstColor
= (DWORD
*) (dst
+ i
* stride
+ (int) diffuse
);
135 srcColor
= * (DWORD
*) ( ((int) src
+ (int) diffuse
) + i
* stride
);
137 /* Color conversion like in drawStridedSlow. watch out for little endianity
138 * If we want that stuff to work on big endian machines too we have to consider more things
140 * 0xff000000: Alpha mask
141 * 0x00ff0000: Blue mask
142 * 0x0000ff00: Green mask
143 * 0x000000ff: Red mask
147 *dstColor
|= (srcColor
& 0xff00ff00) ; /* Alpha Green */
148 *dstColor
|= (srcColor
& 0x00ff0000) >> 16; /* Red */
149 *dstColor
|= (srcColor
& 0x000000ff) << 16; /* Blue */
152 DWORD srcColor
, *dstColor
= (DWORD
*) (dst
+ i
* stride
+ (int) specular
);
153 srcColor
= * (DWORD
*) ( ((int) src
+ (int) specular
) + i
* stride
);
155 /* Similar to diffuse
156 * TODO: Write the alpha value out for fog coords
159 *dstColor
|= (srcColor
& 0xff00ff00) ; /* Alpha Green */
160 *dstColor
|= (srcColor
& 0x00ff0000) >> 16; /* Red */
161 *dstColor
|= (srcColor
& 0x000000ff) << 16; /* Blue */
166 inline BOOL WINAPI
IWineD3DVertexBufferImpl_FindDecl(IWineD3DVertexBufferImpl
*This
)
168 WineDirect3DVertexStridedData strided
;
169 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
171 /* In d3d7 the vertex buffer declaration NEVER changes because it is stored in the d3d7 vertex buffer.
172 * Once we have our declaration there is no need to look it up again.
174 if(((IWineD3DImpl
*)device
->wineD3D
)->dxVersion
== 7 && This
->Flags
& VBFLAG_HASDESC
) {
178 /* There are certain vertex data types that need to be fixed up. The Vertex Buffers FVF doesn't
179 * help finding them, only the vertex declaration or the device FVF can determine that at drawPrim
180 * time. Rules are as follows:
182 * -> No modification when Vertex Shaders are used
183 * -> Fix up position1 and position 2 if they are XYZRHW
184 * -> Fix up diffuse color
185 * -> Fix up specular color
187 * The Declaration is only known at drawing time, and it can change from draw to draw. If any converted values
188 * are changed, the whole buffer has to be reconverted and reloaded. (Converting is SLOW, so if this happens too
189 * often PreLoad stops converting entirely and falls back to drawStridedSlow).
192 * -> New semantics that have to be converted appear
193 * -> The position of semantics that have to be converted changes
194 * -> The stride of the vertex changed AND there is stuff that needs conversion
195 * -> (If a vertex shader is bound and in use assume that nothing that needs conversion is there)
198 * TRUE: Reload is needed
202 if (use_vs(device
)) {
203 /* Assume no conversion */
204 memset(&strided
, 0, sizeof(strided
));
206 /* we need a copy because we modify some params */
207 memcpy(&strided
, &device
->strided_streams
, sizeof(strided
));
209 /* Filter out data that does not come from this VBO */
210 if(strided
.u
.s
.position
.VBO
!= This
->vbo
) memset(&strided
.u
.s
.position
, 0, sizeof(strided
.u
.s
.position
));
211 if(strided
.u
.s
.diffuse
.VBO
!= This
->vbo
) memset(&strided
.u
.s
.diffuse
, 0, sizeof(strided
.u
.s
.diffuse
));
212 if(strided
.u
.s
.specular
.VBO
!= This
->vbo
) memset(&strided
.u
.s
.specular
, 0, sizeof(strided
.u
.s
.specular
));
213 if(strided
.u
.s
.position2
.VBO
!= This
->vbo
) memset(&strided
.u
.s
.position2
, 0, sizeof(strided
.u
.s
.position2
));
216 /* We have a declaration now in the buffer */
217 This
->Flags
|= VBFLAG_HASDESC
;
219 /* Find out if reload is needed
220 * 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.
222 * 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
223 * and does not exist now all 3 values will be equal(=0).
225 * 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
226 * 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
227 * buffers at the same time and get the position from one and the color from the other buffer.
229 if( /* Position transformed vs untransformed */
230 ((This
->strided
.u
.s
.position_transformed
|| strided
.u
.s
.position_transformed
) &&
231 This
->strided
.u
.s
.position
.lpData
!= strided
.u
.s
.position
.lpData
) ||
232 /* Diffuse position and data type */
233 This
->strided
.u
.s
.diffuse
.lpData
!= strided
.u
.s
.diffuse
.lpData
|| This
->strided
.u
.s
.diffuse
.dwStride
!= strided
.u
.s
.diffuse
.dwStride
||
234 This
->strided
.u
.s
.diffuse
.dwType
!= strided
.u
.s
.diffuse
.dwType
||
235 /* Specular position and data type */
236 This
->strided
.u
.s
.specular
.lpData
!= strided
.u
.s
.specular
.lpData
|| This
->strided
.u
.s
.specular
.dwStride
!= strided
.u
.s
.specular
.dwStride
||
237 This
->strided
.u
.s
.specular
.dwType
!= strided
.u
.s
.specular
.dwType
) {
239 TRACE("Declaration changed, reloading buffer\n");
240 /* Set the new description */
241 memcpy(&This
->strided
, &strided
, sizeof(strided
));
248 static void WINAPI
IWineD3DVertexBufferImpl_PreLoad(IWineD3DVertexBuffer
*iface
) {
249 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*) iface
;
250 IWineD3DDeviceImpl
*device
= This
->resource
.wineD3DDevice
;
252 UINT start
= 0, end
= 0, stride
= 0;
253 BOOL declChanged
= FALSE
;
254 TRACE("(%p)->()\n", This
);
256 if(This
->Flags
& VBFLAG_LOAD
) {
257 return; /* Already doing that stuff */
261 /* TODO: Make converting independent from VBOs */
262 return; /* Not doing any conversion */
265 /* Reading the declaration makes only sense if the stateblock is finalized and the buffer bound to a stream */
266 if(device
->isInDraw
&& This
->bindCount
> 0) {
267 declChanged
= IWineD3DVertexBufferImpl_FindDecl(This
);
268 } else if(This
->Flags
& VBFLAG_HASDESC
) {
269 /* Reuse the declaration stored in the buffer. It will most likely not change, and if it does
270 * the stream source state handler will call PreLoad again and the change will be cought
273 /* Cannot get a declaration, and no declaration is stored in the buffer. It is pointless to preload
274 * now. When the buffer is used, PreLoad will be called by the stream source state handler and a valid
275 * declaration for the buffer can be found
280 /* If applications change the declaration over and over, reconverting all the time is a huge
281 * performance hit. So count the declaration changes and release the VBO if there are too much
282 * of them(and thus stop converting)
288 if(This
->declChanges
> VB_MAXDECLCHANGES
) {
289 FIXME("Too much declaration changes, stopping converting\n");
291 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
292 GL_EXTCALL(glDeleteBuffersARB(1, &This
->vbo
));
293 checkGLcall("glDeleteBuffersARB");
297 /* The stream source state handler might have read the memory of the vertex buffer already
298 * and got the memory in the vbo which is not valid any longer. Dirtify the stream source
299 * to force a reload. This happens only once per changed vertexbuffer and should occur rather
302 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_STREAMSRC
);
307 /* However, it is perfectly fine to change the declaration every now and then. We don't want a game that
308 * changes it every minute drop the VBO after VB_MAX_DECL_CHANGES minutes. So count draws without
309 * decl changes and reset the decl change count after a specific number of them
312 if(This
->draws
> VB_RESETDECLCHANGE
) This
->declChanges
= 0;
316 /* The declaration changed, reload the whole buffer */
317 WARN("Reloading buffer because of decl change\n");
319 end
= This
->resource
.size
;
320 } else if(This
->Flags
& VBFLAG_DIRTY
) {
321 /* No decl change, but dirty data, reload the changed stuff */
322 start
= This
->dirtystart
;
323 end
= This
->dirtyend
;
325 /* Desc not changed, buffer not dirty, nothing to do :-) */
329 /* Mark the buffer clean */
330 This
->Flags
&= ~VBFLAG_DIRTY
;
331 This
->dirtystart
= 0;
334 if (This
->strided
.u
.s
.position
.dwStride
) stride
= This
->strided
.u
.s
.position
.dwStride
;
335 else if(This
->strided
.u
.s
.specular
.dwStride
) stride
= This
->strided
.u
.s
.specular
.dwStride
;
336 else if(This
->strided
.u
.s
.diffuse
.dwStride
) stride
= This
->strided
.u
.s
.diffuse
.dwStride
;
338 /* That means that there is nothing to fixup. Just upload from This->resource.allocatedMemory
339 * directly into the vbo. Do not free the system memory copy because drawPrimitive may need it if
340 * the stride is 0, for instancing emulation, vertex blending emulation or shader emulation.
342 TRACE("No conversion needed, locking directly into the VBO in future\n");
345 if(!device
->isInDraw
) {
346 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
348 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
349 checkGLcall("glBindBufferARB");
350 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
, start
, end
-start
, This
->resource
.allocatedMemory
+ start
));
351 checkGLcall("glBufferSubDataARB");
356 /* OK, we have the original data from the app, the description of the buffer and the dirty area.
357 * so convert the stuff
359 data
= HeapAlloc(GetProcessHeap(), 0, end
-start
);
361 ERR("Out of memory\n");
364 memcpy(data
, This
->resource
.allocatedMemory
+ start
, end
- start
);
366 fixup_vertices(data
, data
, stride
, ( end
- start
) / stride
,
368 This
->strided
.u
.s
.position
.lpData
, /* Data location */
369 This
->strided
.u
.s
.position_transformed
, /* Do convert? */
371 This
->strided
.u
.s
.diffuse
.lpData
, /* Location */
372 This
->strided
.u
.s
.diffuse
.dwType
== WINED3DDECLTYPE_SHORT4
|| This
->strided
.u
.s
.diffuse
.dwType
== WINED3DDECLTYPE_D3DCOLOR
, /* Convert? */
374 This
->strided
.u
.s
.specular
.lpData
, /* location */
375 This
->strided
.u
.s
.specular
.dwType
== WINED3DDECLTYPE_SHORT4
|| This
->strided
.u
.s
.specular
.dwType
== WINED3DDECLTYPE_D3DCOLOR
);
378 if(!device
->isInDraw
) {
379 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
381 GL_EXTCALL(glBindBufferARB(GL_ARRAY_BUFFER_ARB
, This
->vbo
));
382 checkGLcall("glBindBufferARB");
383 GL_EXTCALL(glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
, start
, end
- start
, data
));
384 checkGLcall("glBufferSubDataARB");
387 HeapFree(GetProcessHeap(), 0, data
);
390 static WINED3DRESOURCETYPE WINAPI
IWineD3DVertexBufferImpl_GetType(IWineD3DVertexBuffer
*iface
) {
391 return IWineD3DResourceImpl_GetType((IWineD3DResource
*)iface
);
394 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetParent(IWineD3DVertexBuffer
*iface
, IUnknown
**pParent
) {
395 return IWineD3DResourceImpl_GetParent((IWineD3DResource
*)iface
, pParent
);
398 /* ******************************************************
399 IWineD3DVertexBuffer IWineD3DVertexBuffer parts follow
400 ****************************************************** */
401 static HRESULT WINAPI
IWineD3DVertexBufferImpl_Lock(IWineD3DVertexBuffer
*iface
, UINT OffsetToLock
, UINT SizeToLock
, BYTE
** ppbData
, DWORD Flags
) {
402 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
404 TRACE("(%p)->%d, %d, %p, %08x\n", This
, OffsetToLock
, SizeToLock
, ppbData
, Flags
);
406 InterlockedIncrement(&This
->lockcount
);
408 if(This
->Flags
& VBFLAG_DIRTY
) {
409 if(This
->dirtystart
> OffsetToLock
) This
->dirtystart
= OffsetToLock
;
411 if(This
->dirtyend
< OffsetToLock
+ SizeToLock
) This
->dirtyend
= OffsetToLock
+ SizeToLock
;
413 This
->dirtyend
= This
->resource
.size
;
416 This
->dirtystart
= OffsetToLock
;
418 This
->dirtyend
= OffsetToLock
+ SizeToLock
;
420 This
->dirtyend
= This
->resource
.size
;
423 data
= This
->resource
.allocatedMemory
;
424 This
->Flags
|= VBFLAG_DIRTY
;
425 *ppbData
= data
+ OffsetToLock
;
427 TRACE("(%p) : returning memory of %p (base:%p,offset:%u)\n", This
, data
+ OffsetToLock
, data
, OffsetToLock
);
428 /* TODO: check Flags compatibility with This->currentDesc.Usage (see MSDN) */
431 HRESULT WINAPI
IWineD3DVertexBufferImpl_Unlock(IWineD3DVertexBuffer
*iface
) {
432 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*) iface
;
434 TRACE("(%p)\n", This
);
436 lockcount
= InterlockedDecrement(&This
->lockcount
);
438 /* Delay loading the buffer until everything is unlocked */
439 TRACE("Ignoring the unlock\n");
443 if(This
->Flags
& VBFLAG_HASDESC
) {
444 IWineD3DVertexBufferImpl_PreLoad(iface
);
448 static HRESULT WINAPI
IWineD3DVertexBufferImpl_GetDesc(IWineD3DVertexBuffer
*iface
, WINED3DVERTEXBUFFER_DESC
*pDesc
) {
449 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
451 TRACE("(%p)\n", This
);
452 pDesc
->Format
= This
->resource
.format
;
453 pDesc
->Type
= This
->resource
.resourceType
;
454 pDesc
->Usage
= This
->resource
.usage
;
455 pDesc
->Pool
= This
->resource
.pool
;
456 pDesc
->Size
= This
->resource
.size
;
457 pDesc
->FVF
= This
->fvf
;
461 const IWineD3DVertexBufferVtbl IWineD3DVertexBuffer_Vtbl
=
464 IWineD3DVertexBufferImpl_QueryInterface
,
465 IWineD3DVertexBufferImpl_AddRef
,
466 IWineD3DVertexBufferImpl_Release
,
467 /* IWineD3DResource */
468 IWineD3DVertexBufferImpl_GetParent
,
469 IWineD3DVertexBufferImpl_GetDevice
,
470 IWineD3DVertexBufferImpl_SetPrivateData
,
471 IWineD3DVertexBufferImpl_GetPrivateData
,
472 IWineD3DVertexBufferImpl_FreePrivateData
,
473 IWineD3DVertexBufferImpl_SetPriority
,
474 IWineD3DVertexBufferImpl_GetPriority
,
475 IWineD3DVertexBufferImpl_PreLoad
,
476 IWineD3DVertexBufferImpl_GetType
,
477 /* IWineD3DVertexBuffer */
478 IWineD3DVertexBufferImpl_Lock
,
479 IWineD3DVertexBufferImpl_Unlock
,
480 IWineD3DVertexBufferImpl_GetDesc
483 BYTE
* WINAPI
IWineD3DVertexBufferImpl_GetMemory(IWineD3DVertexBuffer
* iface
, DWORD iOffset
, GLint
*vbo
) {
484 IWineD3DVertexBufferImpl
*This
= (IWineD3DVertexBufferImpl
*)iface
;
488 return This
->resource
.allocatedMemory
+ iOffset
;
490 return (BYTE
*) iOffset
;
494 HRESULT WINAPI
IWineD3DVertexBufferImpl_ReleaseMemory(IWineD3DVertexBuffer
* iface
) {