include: Add SetDefaultDllDirectories.
[wine.git] / dlls / ddraw / vertexbuffer.c
blob07f1257cf9cf9b726d8e356f9a9f01ea527a93e6
1 /* Direct3D Vertex Buffer
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include "ddraw_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
27 static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
29 return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer7_iface);
32 /*****************************************************************************
33 * IUnknown Methods
34 *****************************************************************************/
36 static HRESULT WINAPI d3d_vertex_buffer7_QueryInterface(IDirect3DVertexBuffer7 *iface, REFIID riid, void **obj)
38 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
40 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
42 *obj = NULL;
44 if (IsEqualGUID(&IID_IUnknown, riid))
46 IDirect3DVertexBuffer7_AddRef(iface);
47 *obj = iface;
48 return S_OK;
50 if (IsEqualGUID(&IID_IDirect3DVertexBuffer7, riid) && buffer->version == 7)
52 IDirect3DVertexBuffer7_AddRef(iface);
53 *obj = iface;
54 return S_OK;
56 if (IsEqualGUID(&IID_IDirect3DVertexBuffer, riid) && buffer->version == 3)
58 IDirect3DVertexBuffer7_AddRef(iface);
59 *obj = iface;
60 return S_OK;
63 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
64 return E_NOINTERFACE;
67 static ULONG WINAPI d3d_vertex_buffer7_AddRef(IDirect3DVertexBuffer7 *iface)
69 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
70 ULONG ref = InterlockedIncrement(&buffer->ref);
72 TRACE("%p increasing refcount to %u.\n", buffer, ref);
74 return ref;
77 static ULONG WINAPI d3d_vertex_buffer7_Release(IDirect3DVertexBuffer7 *iface)
79 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
80 ULONG ref = InterlockedDecrement(&buffer->ref);
82 TRACE("%p decreasing refcount to %u.\n", buffer, ref);
84 if (!ref)
86 struct wined3d_buffer *vb = NULL;
87 UINT offset, stride;
89 /* D3D7 vertex buffers don't stay bound in the device, they are passed
90 * as a parameter to DrawPrimitiveVB. DrawPrimitiveVB sets them as the
91 * stream source in wined3d and they should get unset there before
92 * they are destroyed. */
93 wined3d_mutex_lock();
94 wined3d_device_get_stream_source(buffer->ddraw->wined3d_device, 0, &vb, &offset, &stride);
95 if (vb == buffer->wined3d_buffer)
96 wined3d_device_set_stream_source(buffer->ddraw->wined3d_device, 0, NULL, 0, 0);
98 wined3d_vertex_declaration_decref(buffer->wined3d_declaration);
99 wined3d_buffer_decref(buffer->wined3d_buffer);
100 wined3d_mutex_unlock();
102 if (buffer->version == 7)
103 IDirectDraw7_Release(&buffer->ddraw->IDirectDraw7_iface);
105 HeapFree(GetProcessHeap(), 0, buffer);
108 return ref;
111 /*****************************************************************************
112 * IDirect3DVertexBuffer Methods
113 *****************************************************************************/
115 static HRESULT d3d_vertex_buffer_create_wined3d_buffer(struct d3d_vertex_buffer *buffer, BOOL dynamic,
116 struct wined3d_buffer **wined3d_buffer)
118 DWORD usage = WINED3DUSAGE_STATICDECL;
119 enum wined3d_pool pool;
121 if (buffer->Caps & D3DVBCAPS_SYSTEMMEMORY)
122 pool = WINED3D_POOL_SYSTEM_MEM;
123 else
124 pool = WINED3D_POOL_DEFAULT;
126 if (buffer->Caps & D3DVBCAPS_WRITEONLY)
127 usage |= WINED3DUSAGE_WRITEONLY;
128 if (dynamic)
129 usage |= WINED3DUSAGE_DYNAMIC;
131 return wined3d_buffer_create_vb(buffer->ddraw->wined3d_device,
132 buffer->size, usage, pool, buffer, &ddraw_null_wined3d_parent_ops,
133 wined3d_buffer);
136 /*****************************************************************************
137 * IDirect3DVertexBuffer7::Lock
139 * Locks the vertex buffer and returns a pointer to the vertex data
140 * Locking vertex buffers is similar to locking surfaces, because Windows
141 * uses surfaces to store vertex data internally (According to the DX sdk)
143 * Params:
144 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
145 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
146 * Data: Returns a pointer to the vertex data
147 * Size: Returns the size of the buffer if not NULL
149 * Returns:
150 * D3D_OK on success
151 * DDERR_INVALIDPARAMS if Data is NULL
152 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
154 *****************************************************************************/
155 static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface,
156 DWORD flags, void **data, DWORD *data_size)
158 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
159 struct wined3d_resource_desc wined3d_desc;
160 struct wined3d_resource *wined3d_resource;
161 struct wined3d_map_desc wined3d_map_desc;
162 HRESULT hr;
163 DWORD wined3d_flags = 0;
165 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
167 /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
168 * nosyslock: Not applicable
170 if (!(flags & DDLOCK_WAIT))
171 wined3d_flags |= WINED3D_MAP_DONOTWAIT;
172 if (flags & DDLOCK_READONLY)
173 wined3d_flags |= WINED3D_MAP_READONLY;
174 if (flags & DDLOCK_NOOVERWRITE)
175 wined3d_flags |= WINED3D_MAP_NOOVERWRITE;
176 if (flags & DDLOCK_DISCARDCONTENTS)
178 wined3d_flags |= WINED3D_MAP_DISCARD;
180 if (!buffer->dynamic)
182 struct wined3d_buffer *new_buffer;
183 wined3d_mutex_lock();
184 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer);
185 if (SUCCEEDED(hr))
187 buffer->dynamic = TRUE;
188 wined3d_buffer_decref(buffer->wined3d_buffer);
189 buffer->wined3d_buffer = new_buffer;
191 else
193 WARN("Failed to create a dynamic buffer\n");
195 wined3d_mutex_unlock();
199 wined3d_mutex_lock();
200 if (data_size)
202 /* Get the size, for returning it, and for locking */
203 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
204 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
205 *data_size = wined3d_desc.size;
208 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
209 0, &wined3d_map_desc, NULL, wined3d_flags);
210 *data = wined3d_map_desc.data;
212 wined3d_mutex_unlock();
214 return hr;
217 /*****************************************************************************
218 * IDirect3DVertexBuffer7::Unlock
220 * Unlocks a vertex Buffer
222 * Returns:
223 * D3D_OK on success
225 *****************************************************************************/
226 static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface)
228 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
230 TRACE("iface %p.\n", iface);
232 wined3d_mutex_lock();
233 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
234 wined3d_mutex_unlock();
236 return D3D_OK;
239 /*****************************************************************************
240 * IDirect3DVertexBuffer7::ProcessVertices
242 * Processes untransformed Vertices into a transformed or optimized vertex
243 * buffer. It can also perform other operations, such as lighting or clipping
245 * Params
246 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
247 * DestIndex: Index in the destination buffer(This), where the vertices are
248 * placed
249 * Count: Number of Vertices in the Source buffer to process
250 * SrcBuffer: Source vertex buffer
251 * SrcIndex: Index of the first vertex in the src buffer to process
252 * D3DDevice: Device to use for transformation
253 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
254 * unchaned vertices
256 * Returns:
257 * D3D_OK on success
258 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
260 *****************************************************************************/
261 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7 *iface,
262 DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer7 *src_buffer,
263 DWORD src_idx, IDirect3DDevice7 *device, DWORD flags)
265 struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer7(iface);
266 struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer);
267 struct d3d_device *device_impl = dst_buffer_impl->version == 7
268 ? unsafe_impl_from_IDirect3DDevice7(device)
269 : unsafe_impl_from_IDirect3DDevice3((IDirect3DDevice3 *)device);
270 BOOL oldClip, doClip;
271 HRESULT hr;
273 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
274 iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
276 /* Vertex operations:
277 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
278 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
279 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
280 * D3DVOP_LIGHT: Lights the vertices
281 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
283 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
284 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
286 if (!(vertex_op & D3DVOP_TRANSFORM))
287 return DDERR_INVALIDPARAMS;
289 wined3d_mutex_lock();
291 /* WineD3D doesn't know d3d7 vertex operation, it uses
292 * render states instead. Set the render states according to
293 * the vertex ops
295 doClip = !!(vertex_op & D3DVOP_CLIP);
296 oldClip = wined3d_device_get_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING);
297 if (doClip != oldClip)
298 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, doClip);
300 wined3d_device_set_stream_source(device_impl->wined3d_device,
301 0, src_buffer_impl->wined3d_buffer, 0, get_flexible_vertex_size(src_buffer_impl->fvf));
302 wined3d_device_set_vertex_declaration(device_impl->wined3d_device, src_buffer_impl->wined3d_declaration);
303 hr = wined3d_device_process_vertices(device_impl->wined3d_device, src_idx, dst_idx,
304 count, dst_buffer_impl->wined3d_buffer, NULL, flags, dst_buffer_impl->fvf);
306 /* Restore the states if needed */
307 if (doClip != oldClip)
308 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip);
310 wined3d_mutex_unlock();
312 return hr;
315 /*****************************************************************************
316 * IDirect3DVertexBuffer7::GetVertexBufferDesc
318 * Returns the description of a vertex buffer
320 * Params:
321 * Desc: Address to write the description to
323 * Returns
324 * DDERR_INVALIDPARAMS if Desc is NULL
325 * D3D_OK on success
327 *****************************************************************************/
328 static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc)
330 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
331 struct wined3d_resource_desc wined3d_desc;
332 struct wined3d_resource *wined3d_resource;
334 TRACE("iface %p, desc %p.\n", iface, desc);
336 if (!desc) return DDERR_INVALIDPARAMS;
338 wined3d_mutex_lock();
339 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
340 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
341 wined3d_mutex_unlock();
343 /* Now fill the desc structure */
344 desc->dwCaps = buffer->Caps;
345 desc->dwFVF = buffer->fvf;
346 desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(buffer->fvf);
348 return D3D_OK;
351 /*****************************************************************************
352 * IDirect3DVertexBuffer7::Optimize
354 * Converts an unoptimized vertex buffer into an optimized buffer
356 * Params:
357 * D3DDevice: Device for which this buffer is optimized
358 * Flags: Not used, should be set to 0
360 * Returns
361 * D3D_OK, because it's a stub
363 *****************************************************************************/
364 static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface,
365 IDirect3DDevice7 *device, DWORD flags)
367 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
368 static BOOL hide = FALSE;
370 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
372 if (!hide)
374 FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags);
375 hide = TRUE;
378 /* We could forward this call to WineD3D and take advantage
379 * of it once we use OpenGL vertex buffers
381 wined3d_mutex_lock();
382 buffer->Caps |= D3DVBCAPS_OPTIMIZED;
383 wined3d_mutex_unlock();
385 return DD_OK;
388 /*****************************************************************************
389 * IDirect3DVertexBuffer7::ProcessVerticesStrided
391 * This method processes untransformed strided vertices into a processed
392 * or optimized vertex buffer.
394 * For more details on the parameters, see
395 * IDirect3DVertexBuffer7::ProcessVertices
397 * Params:
398 * VertexOp: Operations to perform
399 * DestIndex: Destination index to write the vertices to
400 * Count: Number of input vertices
401 * StrideData: Array containing the input vertices
402 * VertexTypeDesc: Vertex Description or source index?????????
403 * D3DDevice: IDirect3DDevice7 to use for processing
404 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
406 * Returns
407 * D3D_OK on success, or DDERR_*
409 *****************************************************************************/
410 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
411 DWORD vertex_op, DWORD dst_idx, DWORD count, D3DDRAWPRIMITIVESTRIDEDDATA *data,
412 DWORD fvf, IDirect3DDevice7 *device, DWORD flags)
414 FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
415 iface, vertex_op, dst_idx, count, data, fvf, device, flags);
417 return DD_OK;
420 /*****************************************************************************
421 * The VTables
422 *****************************************************************************/
424 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
426 d3d_vertex_buffer7_QueryInterface,
427 d3d_vertex_buffer7_AddRef,
428 d3d_vertex_buffer7_Release,
429 d3d_vertex_buffer7_Lock,
430 d3d_vertex_buffer7_Unlock,
431 d3d_vertex_buffer7_ProcessVertices,
432 d3d_vertex_buffer7_GetVertexBufferDesc,
433 d3d_vertex_buffer7_Optimize,
434 d3d_vertex_buffer7_ProcessVerticesStrided,
437 HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf,
438 struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc)
440 struct d3d_vertex_buffer *buffer;
441 HRESULT hr = D3D_OK;
443 TRACE("Vertex buffer description:\n");
444 TRACE(" dwSize %u\n", desc->dwSize);
445 TRACE(" dwCaps %#x\n", desc->dwCaps);
446 TRACE(" FVF %#x\n", desc->dwFVF);
447 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
449 buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buffer));
450 if (!buffer)
451 return DDERR_OUTOFMEMORY;
453 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
454 buffer->ref = 1;
455 buffer->version = ddraw->d3dversion;
456 if (buffer->version == 7)
457 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
458 buffer->ddraw = ddraw;
459 buffer->Caps = desc->dwCaps;
460 buffer->fvf = desc->dwFVF;
461 buffer->size = get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices;
463 wined3d_mutex_lock();
465 if (FAILED(hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wined3d_buffer)))
467 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
468 if (hr == WINED3DERR_INVALIDCALL)
469 hr = DDERR_INVALIDPARAMS;
470 goto end;
473 if (!(buffer->wined3d_declaration = ddraw_find_decl(ddraw, desc->dwFVF)))
475 ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
476 wined3d_buffer_decref(buffer->wined3d_buffer);
477 hr = DDERR_INVALIDPARAMS;
478 goto end;
480 wined3d_vertex_declaration_incref(buffer->wined3d_declaration);
482 end:
483 wined3d_mutex_unlock();
484 if (hr == D3D_OK)
485 *vertex_buf = buffer;
486 else
487 HeapFree(GetProcessHeap(), 0, buffer);
489 return hr;
492 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
494 if (!iface)
495 return NULL;
496 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);
498 return impl_from_IDirect3DVertexBuffer7(iface);