user32/tests: Reorganize the DPI tests.
[wine.git] / dlls / ddraw / vertexbuffer.c
blob3ad8f7e1bdd976f129d800ebbeae50ba7cc50924
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 heap_free(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 struct wined3d_buffer_desc desc;
120 desc.byte_width = buffer->size;
121 desc.usage = WINED3DUSAGE_STATICDECL;
122 if (buffer->Caps & D3DVBCAPS_WRITEONLY)
123 desc.usage |= WINED3DUSAGE_WRITEONLY;
124 if (dynamic)
125 desc.usage |= WINED3DUSAGE_DYNAMIC;
126 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
127 if (buffer->Caps & D3DVBCAPS_SYSTEMMEMORY)
128 desc.access = WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
129 else
130 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
131 desc.misc_flags = 0;
132 desc.structure_byte_stride = 0;
134 return wined3d_buffer_create(buffer->ddraw->wined3d_device, &desc,
135 NULL, buffer, &ddraw_null_wined3d_parent_ops, wined3d_buffer);
138 /*****************************************************************************
139 * IDirect3DVertexBuffer7::Lock
141 * Locks the vertex buffer and returns a pointer to the vertex data
142 * Locking vertex buffers is similar to locking surfaces, because Windows
143 * uses surfaces to store vertex data internally (According to the DX sdk)
145 * Params:
146 * Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
147 * DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
148 * Data: Returns a pointer to the vertex data
149 * Size: Returns the size of the buffer if not NULL
151 * Returns:
152 * D3D_OK on success
153 * DDERR_INVALIDPARAMS if Data is NULL
154 * D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
156 *****************************************************************************/
157 static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface,
158 DWORD flags, void **data, DWORD *data_size)
160 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
161 struct wined3d_resource_desc wined3d_desc;
162 struct wined3d_resource *wined3d_resource;
163 struct wined3d_map_desc wined3d_map_desc;
164 HRESULT hr;
166 TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
168 if (buffer->version != 7)
169 flags &= ~(DDLOCK_NOOVERWRITE | DDLOCK_DISCARDCONTENTS);
171 if (!(flags & DDLOCK_WAIT))
172 flags |= DDLOCK_DONOTWAIT;
173 if (flags & DDLOCK_DISCARDCONTENTS)
175 if (!buffer->dynamic)
177 struct wined3d_buffer *new_buffer;
178 wined3d_mutex_lock();
179 hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer);
180 if (SUCCEEDED(hr))
182 buffer->dynamic = TRUE;
183 wined3d_buffer_decref(buffer->wined3d_buffer);
184 buffer->wined3d_buffer = new_buffer;
186 else
188 WARN("Failed to create a dynamic buffer\n");
190 wined3d_mutex_unlock();
194 wined3d_mutex_lock();
195 if (data_size)
197 /* Get the size, for returning it, and for locking */
198 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
199 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
200 *data_size = wined3d_desc.size;
203 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
204 0, &wined3d_map_desc, NULL, wined3dmapflags_from_ddrawmapflags(flags));
205 *data = wined3d_map_desc.data;
207 wined3d_mutex_unlock();
209 return hr;
212 /*****************************************************************************
213 * IDirect3DVertexBuffer7::Unlock
215 * Unlocks a vertex Buffer
217 * Returns:
218 * D3D_OK on success
220 *****************************************************************************/
221 static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface)
223 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
225 TRACE("iface %p.\n", iface);
227 wined3d_mutex_lock();
228 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
229 wined3d_mutex_unlock();
231 return D3D_OK;
234 /*****************************************************************************
235 * IDirect3DVertexBuffer7::ProcessVertices
237 * Processes untransformed Vertices into a transformed or optimized vertex
238 * buffer. It can also perform other operations, such as lighting or clipping
240 * Params
241 * VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
242 * DestIndex: Index in the destination buffer(This), where the vertices are
243 * placed
244 * Count: Number of Vertices in the Source buffer to process
245 * SrcBuffer: Source vertex buffer
246 * SrcIndex: Index of the first vertex in the src buffer to process
247 * D3DDevice: Device to use for transformation
248 * Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
249 * unchanged vertices
251 * Returns:
252 * D3D_OK on success
253 * DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
255 *****************************************************************************/
256 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7 *iface,
257 DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer7 *src_buffer,
258 DWORD src_idx, IDirect3DDevice7 *device, DWORD flags)
260 struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer7(iface);
261 struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer);
262 struct d3d_device *device_impl = dst_buffer_impl->version == 7
263 ? unsafe_impl_from_IDirect3DDevice7(device)
264 : unsafe_impl_from_IDirect3DDevice3((IDirect3DDevice3 *)device);
265 BOOL oldClip, doClip;
266 HRESULT hr;
268 TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
269 iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
271 /* Vertex operations:
272 * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
273 * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
274 * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
275 * D3DVOP_LIGHT: Lights the vertices
276 * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
278 * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
279 * are not implemented. Clipping is disabled ATM, because of unsure conditions.
281 if (!(vertex_op & D3DVOP_TRANSFORM))
282 return DDERR_INVALIDPARAMS;
284 wined3d_mutex_lock();
286 /* WineD3D doesn't know d3d7 vertex operation, it uses
287 * render states instead. Set the render states according to
288 * the vertex ops
290 doClip = !!(vertex_op & D3DVOP_CLIP);
291 oldClip = wined3d_device_get_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING);
292 if (doClip != oldClip)
293 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, doClip);
295 wined3d_device_set_stream_source(device_impl->wined3d_device,
296 0, src_buffer_impl->wined3d_buffer, 0, get_flexible_vertex_size(src_buffer_impl->fvf));
297 wined3d_device_set_vertex_declaration(device_impl->wined3d_device, src_buffer_impl->wined3d_declaration);
298 hr = wined3d_device_process_vertices(device_impl->wined3d_device, src_idx, dst_idx,
299 count, dst_buffer_impl->wined3d_buffer, NULL, flags, dst_buffer_impl->fvf);
301 /* Restore the states if needed */
302 if (doClip != oldClip)
303 wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip);
305 wined3d_mutex_unlock();
307 return hr;
310 /*****************************************************************************
311 * IDirect3DVertexBuffer7::GetVertexBufferDesc
313 * Returns the description of a vertex buffer
315 * Params:
316 * Desc: Address to write the description to
318 * Returns
319 * DDERR_INVALIDPARAMS if Desc is NULL
320 * D3D_OK on success
322 *****************************************************************************/
323 static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc)
325 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
326 struct wined3d_resource_desc wined3d_desc;
327 struct wined3d_resource *wined3d_resource;
329 TRACE("iface %p, desc %p.\n", iface, desc);
331 if (!desc) return DDERR_INVALIDPARAMS;
333 wined3d_mutex_lock();
334 wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
335 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
336 wined3d_mutex_unlock();
338 /* Now fill the desc structure */
339 desc->dwCaps = buffer->Caps;
340 desc->dwFVF = buffer->fvf;
341 desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(buffer->fvf);
343 return D3D_OK;
346 /*****************************************************************************
347 * IDirect3DVertexBuffer7::Optimize
349 * Converts an unoptimized vertex buffer into an optimized buffer
351 * Params:
352 * D3DDevice: Device for which this buffer is optimized
353 * Flags: Not used, should be set to 0
355 * Returns
356 * D3D_OK, because it's a stub
358 *****************************************************************************/
359 static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface,
360 IDirect3DDevice7 *device, DWORD flags)
362 struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
363 static BOOL hide = FALSE;
365 TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
367 if (!hide)
369 FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags);
370 hide = TRUE;
373 /* We could forward this call to WineD3D and take advantage
374 * of it once we use OpenGL vertex buffers
376 wined3d_mutex_lock();
377 buffer->Caps |= D3DVBCAPS_OPTIMIZED;
378 wined3d_mutex_unlock();
380 return DD_OK;
383 /*****************************************************************************
384 * IDirect3DVertexBuffer7::ProcessVerticesStrided
386 * This method processes untransformed strided vertices into a processed
387 * or optimized vertex buffer.
389 * For more details on the parameters, see
390 * IDirect3DVertexBuffer7::ProcessVertices
392 * Params:
393 * VertexOp: Operations to perform
394 * DestIndex: Destination index to write the vertices to
395 * Count: Number of input vertices
396 * StrideData: Array containing the input vertices
397 * VertexTypeDesc: Vertex Description or source index?????????
398 * D3DDevice: IDirect3DDevice7 to use for processing
399 * Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
401 * Returns
402 * D3D_OK on success, or DDERR_*
404 *****************************************************************************/
405 static HRESULT WINAPI d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
406 DWORD vertex_op, DWORD dst_idx, DWORD count, D3DDRAWPRIMITIVESTRIDEDDATA *data,
407 DWORD fvf, IDirect3DDevice7 *device, DWORD flags)
409 FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
410 iface, vertex_op, dst_idx, count, data, fvf, device, flags);
412 return DD_OK;
415 /*****************************************************************************
416 * The VTables
417 *****************************************************************************/
419 static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
421 d3d_vertex_buffer7_QueryInterface,
422 d3d_vertex_buffer7_AddRef,
423 d3d_vertex_buffer7_Release,
424 d3d_vertex_buffer7_Lock,
425 d3d_vertex_buffer7_Unlock,
426 d3d_vertex_buffer7_ProcessVertices,
427 d3d_vertex_buffer7_GetVertexBufferDesc,
428 d3d_vertex_buffer7_Optimize,
429 d3d_vertex_buffer7_ProcessVerticesStrided,
432 HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf,
433 struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc)
435 struct d3d_vertex_buffer *buffer;
436 HRESULT hr = D3D_OK;
438 TRACE("Vertex buffer description:\n");
439 TRACE(" dwSize %u\n", desc->dwSize);
440 TRACE(" dwCaps %#x\n", desc->dwCaps);
441 TRACE(" FVF %#x\n", desc->dwFVF);
442 TRACE(" dwNumVertices %u\n", desc->dwNumVertices);
444 if (!(buffer = heap_alloc_zero(sizeof(*buffer))))
445 return DDERR_OUTOFMEMORY;
447 buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
448 buffer->ref = 1;
449 buffer->version = ddraw->d3dversion;
450 if (buffer->version == 7)
451 IDirectDraw7_AddRef(&ddraw->IDirectDraw7_iface);
452 buffer->ddraw = ddraw;
453 buffer->Caps = desc->dwCaps;
454 buffer->fvf = desc->dwFVF;
455 buffer->size = get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices;
457 wined3d_mutex_lock();
459 if (FAILED(hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wined3d_buffer)))
461 WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
462 if (hr == WINED3DERR_INVALIDCALL)
463 hr = DDERR_INVALIDPARAMS;
464 goto end;
467 if (!(buffer->wined3d_declaration = ddraw_find_decl(ddraw, desc->dwFVF)))
469 ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
470 wined3d_buffer_decref(buffer->wined3d_buffer);
471 hr = DDERR_INVALIDPARAMS;
472 goto end;
474 wined3d_vertex_declaration_incref(buffer->wined3d_declaration);
476 end:
477 wined3d_mutex_unlock();
478 if (hr == D3D_OK)
479 *vertex_buf = buffer;
480 else
481 heap_free(buffer);
483 return hr;
486 struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
488 if (!iface)
489 return NULL;
490 assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);
492 return impl_from_IDirect3DVertexBuffer7(iface);