1 /* Direct3D Vertex Buffer
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DVertexBuffer COM object
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
24 #include "wine/debug.h"
37 #include "wine/exception.h"
42 #include "ddraw_private.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
45 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
47 /*****************************************************************************
49 *****************************************************************************/
51 /*****************************************************************************
52 * IDirect3DVertexBuffer7::QueryInterface
54 * The QueryInterface Method for Vertex Buffers
55 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
58 * riid: Queryied Interface id
59 * obj: Address to return the interface pointer
63 * E_NOINTERFACE if the interface wasn't found
65 *****************************************************************************/
67 IDirect3DVertexBufferImpl_QueryInterface(IDirect3DVertexBuffer7 *iface,
71 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
72 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obj);
74 /* By default, set the object pointer to NULL */
77 if ( IsEqualGUID( &IID_IUnknown, riid ) )
79 IUnknown_AddRef(iface);
81 TRACE(" Creating IUnknown interface at %p.\n", *obj);
84 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
86 IUnknown_AddRef(iface);
87 *obj = &This->IDirect3DVertexBuffer_vtbl;
88 TRACE(" Creating IDirect3DVertexBuffer interface %p\n", *obj);
91 if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
93 IUnknown_AddRef(iface);
95 TRACE(" Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
98 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
102 static HRESULT WINAPI
103 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface(IDirect3DVertexBuffer *iface,
107 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
108 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, debugstr_guid(riid), obj);
110 return IDirect3DVertexBuffer7_QueryInterface((IDirect3DVertexBuffer7 *)This, riid, obj);
113 /*****************************************************************************
114 * IDirect3DVertexBuffer7::AddRef
116 * AddRef for Vertex Buffers
121 *****************************************************************************/
123 IDirect3DVertexBufferImpl_AddRef(IDirect3DVertexBuffer7 *iface)
125 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
126 ULONG ref = InterlockedIncrement(&This->ref);
128 TRACE("(%p/%p)->() incrementing from %u.\n", This, iface, ref - 1);
134 Thunk_IDirect3DVertexBufferImpl_1_AddRef(IDirect3DVertexBuffer *iface)
136 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
137 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
139 return IDirect3DVertexBuffer7_AddRef((IDirect3DVertexBuffer7 *)This);
143 /*****************************************************************************
144 * IDirect3DVertexBuffer7::Release
146 * Release for Vertex Buffers
151 *****************************************************************************/
153 IDirect3DVertexBufferImpl_Release(IDirect3DVertexBuffer7 *iface)
155 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
156 ULONG ref = InterlockedDecrement(&This->ref);
158 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
162 IWineD3DBuffer *curVB = NULL;
165 EnterCriticalSection(&ddraw_cs);
166 /* D3D7 Vertex buffers don't stay bound in the device, they are passed as a parameter
167 * to drawPrimitiveVB. DrawPrimitiveVB sets them as the stream source in wined3d,
168 * and they should get unset there before they are destroyed
170 IWineD3DDevice_GetStreamSource(This->ddraw->wineD3DDevice,
171 0 /* Stream number */,
175 if(curVB == This->wineD3DVertexBuffer)
177 IWineD3DDevice_SetStreamSource(This->ddraw->wineD3DDevice,
178 0 /* Steam number */,
179 NULL /* stream data */,
185 IWineD3DBuffer_Release(curVB); /* For the GetStreamSource */
188 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
189 IWineD3DBuffer_Release(This->wineD3DVertexBuffer);
190 LeaveCriticalSection(&ddraw_cs);
191 HeapFree(GetProcessHeap(), 0, This);
199 Thunk_IDirect3DVertexBufferImpl_1_Release(IDirect3DVertexBuffer *iface)
201 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
202 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
204 return IDirect3DVertexBuffer7_Release((IDirect3DVertexBuffer7 *)This);
207 /*****************************************************************************
208 * IDirect3DVertexBuffer Methods
209 *****************************************************************************/
211 /*****************************************************************************
212 * IDirect3DVertexBuffer7::Lock
214 * Locks the vertex buffer and returns a pointer to the vertex data
215 * Locking vertex buffers is similar to locking surfaces, because Windows
216 * uses surfaces to store vertex data internally (According to the DX sdk)
219 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
220 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
221 * Data: Returns a pointer to the vertex data
222 * Size: Returns the size of the buffer if not NULL
226 * DDERR_INVALIDPARAMS if Data is NULL
227 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
229 *****************************************************************************/
230 static HRESULT WINAPI
231 IDirect3DVertexBufferImpl_Lock(IDirect3DVertexBuffer7 *iface,
236 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
237 WINED3DVERTEXBUFFER_DESC Desc;
239 TRACE("(%p)->(%08x,%p,%p)\n", This, Flags, Data, Size);
241 EnterCriticalSection(&ddraw_cs);
244 /* Get the size, for returning it, and for locking */
245 hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &Desc);
248 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This, hr);
249 LeaveCriticalSection(&ddraw_cs);
255 hr = IWineD3DBuffer_Map(This->wineD3DVertexBuffer, 0 /* OffsetToLock */,
256 0 /* SizeToLock, 0 == Full lock */, (BYTE **)Data, Flags);
257 LeaveCriticalSection(&ddraw_cs);
261 static HRESULT WINAPI
262 Thunk_IDirect3DVertexBufferImpl_1_Lock(IDirect3DVertexBuffer *iface,
267 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
268 TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Flags, Data, Size);
270 return IDirect3DVertexBuffer7_Lock((IDirect3DVertexBuffer7 *)This, Flags, Data, Size);
273 /*****************************************************************************
274 * IDirect3DVertexBuffer7::Unlock
276 * Unlocks a vertex Buffer
281 *****************************************************************************/
282 static HRESULT WINAPI
283 IDirect3DVertexBufferImpl_Unlock(IDirect3DVertexBuffer7 *iface)
285 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
287 TRACE("(%p)->()\n", This);
289 EnterCriticalSection(&ddraw_cs);
290 hr = IWineD3DBuffer_Unmap(This->wineD3DVertexBuffer);
291 LeaveCriticalSection(&ddraw_cs);
296 static HRESULT WINAPI
297 Thunk_IDirect3DVertexBufferImpl_1_Unlock(IDirect3DVertexBuffer *iface)
299 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
300 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DVertexBuffer7 interface.\n", This);
302 return IDirect3DVertexBuffer7_Unlock((IDirect3DVertexBuffer7 *)This);
306 /*****************************************************************************
307 * IDirect3DVertexBuffer7::ProcessVertices
309 * Processes untransformed Vertices into a transformed or optimized vertex
310 * buffer. It can also perform other operations, such as lighting or clipping
313 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
314 * DestIndex: Index in the destination buffer(This), where the vertices are
316 * Count: Number of Vertices in the Source buffer to process
317 * SrcBuffer: Source vertex buffer
318 * SrcIndex: Index of the first vertex in the src buffer to process
319 * D3DDevice: Device to use for transformation
320 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
325 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
327 *****************************************************************************/
328 static HRESULT WINAPI
329 IDirect3DVertexBufferImpl_ProcessVertices(IDirect3DVertexBuffer7 *iface,
333 IDirect3DVertexBuffer7 *SrcBuffer,
335 IDirect3DDevice7 *D3DDevice,
338 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
339 IDirect3DVertexBufferImpl *Src = (IDirect3DVertexBufferImpl *)SrcBuffer;
340 IDirect3DDeviceImpl *D3D = (IDirect3DDeviceImpl *)D3DDevice;
341 BOOL oldClip, doClip;
343 WINED3DVERTEXBUFFER_DESC Desc;
345 TRACE("(%p)->(%08x,%d,%d,%p,%d,%p,%08x)\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
347 /* Vertex operations:
348 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
349 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
350 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
351 * D3DVOP_LIGHT: Lights the vertices
352 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
354 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
355 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
357 if( !(VertexOp & D3DVOP_TRANSFORM) ) return DDERR_INVALIDPARAMS;
359 EnterCriticalSection(&ddraw_cs);
360 /* WineD3D doesn't know d3d7 vertex operation, it uses
361 * render states instead. Set the render states according to
364 doClip = VertexOp & D3DVOP_CLIP ? TRUE : FALSE;
365 IWineD3DDevice_GetRenderState(D3D->wineD3DDevice,
368 if(doClip != oldClip)
370 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
375 IWineD3DBuffer_GetDesc(Src->wineD3DVertexBuffer, &Desc);
376 IWineD3DDevice_SetStreamSource(D3D->wineD3DDevice,
378 Src->wineD3DVertexBuffer,
380 get_flexible_vertex_size(Desc.FVF));
381 IWineD3DDevice_SetVertexDeclaration(D3D->wineD3DDevice,
382 Src->wineD3DVertexDeclaration);
383 hr = IWineD3DDevice_ProcessVertices(D3D->wineD3DDevice,
387 This->wineD3DVertexBuffer,
388 NULL /* Output vdecl */,
391 /* Restore the states if needed */
392 if(doClip != oldClip)
393 IWineD3DDevice_SetRenderState(D3D->wineD3DDevice,
396 LeaveCriticalSection(&ddraw_cs);
400 static HRESULT WINAPI
401 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices(IDirect3DVertexBuffer *iface,
405 IDirect3DVertexBuffer *SrcBuffer,
407 IDirect3DDevice3 *D3DDevice,
410 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
411 IDirect3DVertexBufferImpl *Src = SrcBuffer ? vb_from_vb1(SrcBuffer) : NULL;
412 IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
414 TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, VertexOp, DestIndex, Count, Src, SrcIndex, D3D, Flags);
416 return IDirect3DVertexBuffer7_ProcessVertices((IDirect3DVertexBuffer7 *)This, VertexOp, DestIndex,
417 Count, (IDirect3DVertexBuffer7 *)Src, SrcIndex, (IDirect3DDevice7 *)D3D, Flags);
420 /*****************************************************************************
421 * IDirect3DVertexBuffer7::GetVertexBufferDesc
423 * Returns the description of a vertex buffer
426 * Desc: Address to write the description to
429 * DDERR_INVALIDPARAMS if Desc is NULL
432 *****************************************************************************/
433 static HRESULT WINAPI
434 IDirect3DVertexBufferImpl_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface,
435 D3DVERTEXBUFFERDESC *Desc)
437 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
438 WINED3DVERTEXBUFFER_DESC WDesc;
440 TRACE("(%p)->(%p)\n", This, Desc);
442 if(!Desc) return DDERR_INVALIDPARAMS;
444 EnterCriticalSection(&ddraw_cs);
445 hr = IWineD3DBuffer_GetDesc(This->wineD3DVertexBuffer, &WDesc);
448 ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr=%08x\n", This, hr);
449 LeaveCriticalSection(&ddraw_cs);
453 /* Now fill the Desc structure */
454 Desc->dwCaps = This->Caps;
455 Desc->dwFVF = WDesc.FVF;
456 Desc->dwNumVertices = WDesc.Size / get_flexible_vertex_size(WDesc.FVF);
457 LeaveCriticalSection(&ddraw_cs);
462 static HRESULT WINAPI
463 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface,
464 D3DVERTEXBUFFERDESC *Desc)
466 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
467 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DVertexBuffer7 interface.\n", This, Desc);
469 return IDirect3DVertexBuffer7_GetVertexBufferDesc((IDirect3DVertexBuffer7 *)This, Desc);
473 /*****************************************************************************
474 * IDirect3DVertexBuffer7::Optimize
476 * Converts an unoptimized vertex buffer into an optimized buffer
479 * D3DDevice: Device for which this buffer is optimized
480 * Flags: Not used, should be set to 0
483 * D3D_OK, because it's a stub
485 *****************************************************************************/
486 static HRESULT WINAPI
487 IDirect3DVertexBufferImpl_Optimize(IDirect3DVertexBuffer7 *iface,
488 IDirect3DDevice7 *D3DDevice,
491 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
492 IDirect3DDeviceImpl *D3D = (IDirect3DDeviceImpl *)D3DDevice;
493 static BOOL hide = FALSE;
497 FIXME("(%p)->(%p,%08x): stub!\n", This, D3D, Flags);
501 /* We could forward this call to WineD3D and take advantage
502 * of it once we use OpenGL vertex buffers
504 EnterCriticalSection(&ddraw_cs);
505 This->Caps |= D3DVBCAPS_OPTIMIZED;
506 LeaveCriticalSection(&ddraw_cs);
511 static HRESULT WINAPI
512 Thunk_IDirect3DVertexBufferImpl_1_Optimize(IDirect3DVertexBuffer *iface,
513 IDirect3DDevice3 *D3DDevice,
516 IDirect3DVertexBufferImpl *This = vb_from_vb1(iface);
517 IDirect3DDeviceImpl *D3D = D3DDevice ? device_from_device3(D3DDevice) : NULL;
518 TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DVertexBuffer7 interface.\n", This, D3D, Flags);
520 return IDirect3DVertexBuffer7_Optimize((IDirect3DVertexBuffer7 *)This, (IDirect3DDevice7 *)D3D, Flags);
523 /*****************************************************************************
524 * IDirect3DVertexBuffer7::ProcessVerticesStrided
526 * This method processes untransformed strided vertices into a processed
527 * or optimized vertex buffer.
529 * For more details on the parameters, see
530 * IDirect3DVertexBuffer7::ProcessVertices
533 * VertexOp: Operations to perform
534 * DestIndex: Destination index to write the vertices to
535 * Count: Number of input vertices
536 * StrideData: Array containing the input vertices
537 * VertexTypeDesc: Vertex Description or source index?????????
538 * D3DDevice: IDirect3DDevice7 to use for processing
539 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
542 * D3D_OK on success, or DDERR_*
544 *****************************************************************************/
545 static HRESULT WINAPI
546 IDirect3DVertexBufferImpl_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
550 D3DDRAWPRIMITIVESTRIDEDDATA *StrideData,
551 DWORD VertexTypeDesc,
552 IDirect3DDevice7 *D3DDevice,
555 IDirect3DVertexBufferImpl *This = (IDirect3DVertexBufferImpl *)iface;
556 IDirect3DDeviceImpl *D3D = (IDirect3DDeviceImpl *)D3DDevice;
557 FIXME("(%p)->(%08x,%08x,%08x,%p,%08x,%p,%08x): stub!\n", This, VertexOp, DestIndex, Count, StrideData, VertexTypeDesc, D3D, Flags);
561 /*****************************************************************************
563 *****************************************************************************/
565 const IDirect3DVertexBuffer7Vtbl IDirect3DVertexBuffer7_Vtbl =
567 /*** IUnknown Methods ***/
568 IDirect3DVertexBufferImpl_QueryInterface,
569 IDirect3DVertexBufferImpl_AddRef,
570 IDirect3DVertexBufferImpl_Release,
571 /*** IDirect3DVertexBuffer Methods ***/
572 IDirect3DVertexBufferImpl_Lock,
573 IDirect3DVertexBufferImpl_Unlock,
574 IDirect3DVertexBufferImpl_ProcessVertices,
575 IDirect3DVertexBufferImpl_GetVertexBufferDesc,
576 IDirect3DVertexBufferImpl_Optimize,
577 /*** IDirect3DVertexBuffer7 Methods ***/
578 IDirect3DVertexBufferImpl_ProcessVerticesStrided
581 const IDirect3DVertexBufferVtbl IDirect3DVertexBuffer1_Vtbl =
583 /*** IUnknown Methods ***/
584 Thunk_IDirect3DVertexBufferImpl_1_QueryInterface,
585 Thunk_IDirect3DVertexBufferImpl_1_AddRef,
586 Thunk_IDirect3DVertexBufferImpl_1_Release,
587 /*** IDirect3DVertexBuffer Methods ***/
588 Thunk_IDirect3DVertexBufferImpl_1_Lock,
589 Thunk_IDirect3DVertexBufferImpl_1_Unlock,
590 Thunk_IDirect3DVertexBufferImpl_1_ProcessVertices,
591 Thunk_IDirect3DVertexBufferImpl_1_GetVertexBufferDesc,
592 Thunk_IDirect3DVertexBufferImpl_1_Optimize