vertexbuffer.c 22.7 KB
Newer Older
1
/* Direct3D Vertex Buffer
2
 * Copyright (c) 2002 Lionel ULMER
3
 * Copyright (c) 2006 Stefan DÖSINGER
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This file contains the implementation of Direct3DVertexBuffer COM object
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20 21 22
 */

#include "config.h"
23
#include "wine/port.h"
24

25 26
#include "ddraw_private.h"

27
WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28

29
static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
30
{
31
    return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer_iface);
32 33
}

34
static inline struct d3d_vertex_buffer *impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
35
{
36
    return CONTAINING_RECORD(iface, struct d3d_vertex_buffer, IDirect3DVertexBuffer7_iface);
37 38
}

39 40 41 42 43 44 45 46 47 48 49
/*****************************************************************************
 * IUnknown Methods
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DVertexBuffer7::QueryInterface
 *
 * The QueryInterface Method for Vertex Buffers
 * For a link to QueryInterface rules, see IDirectDraw7::QueryInterface
 *
 * Params
50
 *  riid: Queried Interface id
51 52 53 54 55 56 57
 *  obj: Address to return the interface pointer
 *
 * Returns:
 *  S_OK on success
 *  E_NOINTERFACE if the interface wasn't found
 *
 *****************************************************************************/
58
static HRESULT WINAPI d3d_vertex_buffer7_QueryInterface(IDirect3DVertexBuffer7 *iface, REFIID riid, void  **obj)
59
{
60
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
61 62

    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
63 64

    /* By default, set the object pointer to NULL */
65 66 67 68
    *obj = NULL;

    if ( IsEqualGUID( &IID_IUnknown,  riid ) )
    {
69
        IDirect3DVertexBuffer7_AddRef(iface);
70 71 72
        *obj = iface;
        TRACE("  Creating IUnknown interface at %p.\n", *obj);
        return S_OK;
73
    }
74 75
    if ( IsEqualGUID( &IID_IDirect3DVertexBuffer, riid ) )
    {
76
        IDirect3DVertexBuffer7_AddRef(iface);
77
        *obj = &buffer->IDirect3DVertexBuffer_iface;
78 79
        TRACE("  Creating IDirect3DVertexBuffer interface %p\n", *obj);
        return S_OK;
80
    }
81 82
    if ( IsEqualGUID( &IID_IDirect3DVertexBuffer7, riid ) )
    {
83
        IDirect3DVertexBuffer7_AddRef(iface);
84
        *obj = iface;
85 86
        TRACE("  Creating IDirect3DVertexBuffer7 interface %p\n", *obj);
        return S_OK;
87
    }
88 89 90

    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

91
    return E_NOINTERFACE;
92 93
}

94
static HRESULT WINAPI d3d_vertex_buffer1_QueryInterface(IDirect3DVertexBuffer *iface, REFIID riid, void **obj)
95
{
96
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
97

98
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
99

100
    return d3d_vertex_buffer7_QueryInterface(&buffer->IDirect3DVertexBuffer7_iface, riid, obj);
101 102 103 104 105 106 107 108 109 110 111
}

/*****************************************************************************
 * IDirect3DVertexBuffer7::AddRef
 *
 * AddRef for Vertex Buffers
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
112
static ULONG WINAPI d3d_vertex_buffer7_AddRef(IDirect3DVertexBuffer7 *iface)
113
{
114 115
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
    ULONG ref = InterlockedIncrement(&buffer->ref);
116

117
    TRACE("%p increasing refcount to %u.\n", buffer, ref);
118 119

    return ref;
120 121
}

122
static ULONG WINAPI d3d_vertex_buffer1_AddRef(IDirect3DVertexBuffer *iface)
123
{
124
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
125

126
    TRACE("iface %p.\n", iface);
127

128
    return d3d_vertex_buffer7_AddRef(&buffer->IDirect3DVertexBuffer7_iface);
129 130
}

131 132 133 134 135 136 137 138 139 140

/*****************************************************************************
 * IDirect3DVertexBuffer7::Release
 *
 * Release for Vertex Buffers
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
141
static ULONG WINAPI d3d_vertex_buffer7_Release(IDirect3DVertexBuffer7 *iface)
142
{
143 144
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
    ULONG ref = InterlockedDecrement(&buffer->ref);
145

146
    TRACE("%p decreasing refcount to %u.\n", buffer, ref);
147

148 149
    if (ref == 0)
    {
150
        struct wined3d_buffer *curVB = NULL;
151 152
        UINT offset, stride;

153 154 155 156
        /* D3D7 Vertex buffers don't stay bound in the device, they are passed
         * as a parameter to drawPrimitiveVB. DrawPrimitiveVB sets them as the
         * stream source in wined3d, and they should get unset there before
         * they are destroyed. */
157
        wined3d_mutex_lock();
158
        wined3d_device_get_stream_source(buffer->ddraw->wined3d_device,
159
                0, &curVB, &offset, &stride);
160 161
        if (curVB == buffer->wineD3DVertexBuffer)
            wined3d_device_set_stream_source(buffer->ddraw->wined3d_device, 0, NULL, 0, 0);
162

163 164
        wined3d_vertex_declaration_decref(buffer->wineD3DVertexDeclaration);
        wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
165 166
        wined3d_mutex_unlock();

167
        HeapFree(GetProcessHeap(), 0, buffer);
168
    }
169

170
    return ref;
171 172
}

173
static ULONG WINAPI d3d_vertex_buffer1_Release(IDirect3DVertexBuffer *iface)
174
{
175
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
176

177
    TRACE("iface %p.\n", iface);
178

179
    return d3d_vertex_buffer7_Release(&buffer->IDirect3DVertexBuffer7_iface);
180 181
}

182 183 184
/*****************************************************************************
 * IDirect3DVertexBuffer Methods
 *****************************************************************************/
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static HRESULT d3d_vertex_buffer_create_wined3d_buffer(struct d3d_vertex_buffer *buffer, BOOL dynamic,
        struct wined3d_buffer **wined3d_buffer)
{
    DWORD usage = WINED3DUSAGE_STATICDECL;
    enum wined3d_pool pool;

    if (buffer->Caps & D3DVBCAPS_SYSTEMMEMORY)
        pool = WINED3D_POOL_SYSTEM_MEM;
    else
        pool = WINED3D_POOL_DEFAULT;

    if (buffer->Caps & D3DVBCAPS_WRITEONLY)
        usage |= WINED3DUSAGE_WRITEONLY;
    if (dynamic)
        usage |= WINED3DUSAGE_DYNAMIC;

    return wined3d_buffer_create_vb(buffer->ddraw->wined3d_device,
        buffer->size, usage, pool, buffer, &ddraw_null_wined3d_parent_ops,
        wined3d_buffer);
}

207 208 209 210
/*****************************************************************************
 * IDirect3DVertexBuffer7::Lock
 *
 * Locks the vertex buffer and returns a pointer to the vertex data
211 212
 * Locking vertex buffers is similar to locking surfaces, because Windows
 * uses surfaces to store vertex data internally (According to the DX sdk)
213 214 215 216 217 218 219 220 221 222 223 224 225
 *
 * Params:
 *  Flags: Locking flags. Relevant here are DDLOCK_READONLY, DDLOCK_WRITEONLY,
 *         DDLOCK_DISCARDCONTENTS and DDLOCK_NOOVERWRITE.
 *  Data:  Returns a pointer to the vertex data
 *  Size:  Returns the size of the buffer if not NULL
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Data is NULL
 *  D3DERR_VERTEXBUFFEROPTIMIZED if called on an optimized buffer(WineD3D)
 *
 *****************************************************************************/
226 227
static HRESULT WINAPI d3d_vertex_buffer7_Lock(IDirect3DVertexBuffer7 *iface,
        DWORD flags, void **data, DWORD *data_size)
228
{
229
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
230
    struct wined3d_resource_desc wined3d_desc;
231
    struct wined3d_resource *wined3d_resource;
232
    HRESULT hr;
233
    DWORD wined3d_flags = 0;
234

235
    TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
236

237 238 239
    /* Writeonly: Pointless. Event: Unsupported by native according to the sdk
     * nosyslock: Not applicable
     */
240
    if (!(flags & DDLOCK_WAIT))
241
        wined3d_flags |= WINED3D_MAP_DONOTWAIT;
242
    if (flags & DDLOCK_READONLY)
243
        wined3d_flags |= WINED3D_MAP_READONLY;
244
    if (flags & DDLOCK_NOOVERWRITE)
245
        wined3d_flags |= WINED3D_MAP_NOOVERWRITE;
246
    if (flags & DDLOCK_DISCARDCONTENTS)
247
    {
248
        wined3d_flags |= WINED3D_MAP_DISCARD;
249

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        if (!buffer->dynamic)
        {
            struct wined3d_buffer *new_buffer;
            wined3d_mutex_lock();
            hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, TRUE, &new_buffer);
            if (SUCCEEDED(hr))
            {
                buffer->dynamic = TRUE;
                wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
                buffer->wineD3DVertexBuffer = new_buffer;
            }
            else
            {
                WARN("Failed to create a dynamic buffer\n");
            }
            wined3d_mutex_unlock();
        }
    }

269
    wined3d_mutex_lock();
270
    if (data_size)
271
    {
272
        /* Get the size, for returning it, and for locking */
273
        wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
274
        wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
275
        *data_size = wined3d_desc.size;
276
    }
277

278
    hr = wined3d_buffer_map(buffer->wineD3DVertexBuffer, 0, 0, (BYTE **)data, wined3d_flags);
279

280 281
    wined3d_mutex_unlock();

282
    return hr;
283 284
}

285 286
static HRESULT WINAPI d3d_vertex_buffer1_Lock(IDirect3DVertexBuffer *iface,
        DWORD flags, void **data, DWORD *data_size)
287
{
288
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
289

290
    TRACE("iface %p, flags %#x, data %p, data_size %p.\n", iface, flags, data, data_size);
291

292
    return d3d_vertex_buffer7_Lock(&buffer->IDirect3DVertexBuffer7_iface, flags, data, data_size);
293 294
}

295 296 297 298 299 300 301 302 303
/*****************************************************************************
 * IDirect3DVertexBuffer7::Unlock
 *
 * Unlocks a vertex Buffer
 *
 * Returns:
 *  D3D_OK on success
 *
 *****************************************************************************/
304
static HRESULT WINAPI d3d_vertex_buffer7_Unlock(IDirect3DVertexBuffer7 *iface)
305
{
306
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
307 308

    TRACE("iface %p.\n", iface);
309

310
    wined3d_mutex_lock();
311
    wined3d_buffer_unmap(buffer->wineD3DVertexBuffer);
312
    wined3d_mutex_unlock();
313

314
    return D3D_OK;
315 316
}

317
static HRESULT WINAPI d3d_vertex_buffer1_Unlock(IDirect3DVertexBuffer *iface)
318
{
319
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
320

321
    TRACE("iface %p.\n", iface);
322

323
    return d3d_vertex_buffer7_Unlock(&buffer->IDirect3DVertexBuffer7_iface);
324 325 326
}


327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
/*****************************************************************************
 * IDirect3DVertexBuffer7::ProcessVertices
 *
 * Processes untransformed Vertices into a transformed or optimized vertex
 * buffer. It can also perform other operations, such as lighting or clipping
 *
 * Params
 *  VertexOp: Operation(s) to perform: D3DVOP_CLIP, _EXTENTS, _LIGHT, _TRANSFORM
 *  DestIndex: Index in the destination buffer(This), where the vertices are
 *             placed
 *  Count: Number of Vertices in the Source buffer to process
 *  SrcBuffer: Source vertex buffer
 *  SrcIndex: Index of the first vertex in the src buffer to process
 *  D3DDevice: Device to use for transformation
 *  Flags: 0 for default, D3DPV_DONOTCOPYDATA to prevent copying
 *         unchaned vertices
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS If D3DVOP_TRANSFORM wasn't passed
 *
 *****************************************************************************/
349 350 351
static HRESULT WINAPI d3d_vertex_buffer7_ProcessVertices(IDirect3DVertexBuffer7 *iface,
        DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer7 *src_buffer,
        DWORD src_idx, IDirect3DDevice7 *device, DWORD flags)
352
{
353 354
    struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer7(iface);
    struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer7(src_buffer);
355
    struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice7(device);
356 357 358
    BOOL oldClip, doClip;
    HRESULT hr;

359
    TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
360
            iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
361 362 363 364 365 366 367 368 369 370 371

    /* Vertex operations:
     * D3DVOP_CLIP: Clips vertices outside the viewing frustrum. Needs clipping information
     * in the vertex buffer (Buffer may not be created with D3DVBCAPS_DONOTCLIP)
     * D3DVOP_EXTENTS: Causes the screen extents to be updated when rendering the vertices
     * D3DVOP_LIGHT: Lights the vertices
     * D3DVOP_TRANSFORM: Transform the vertices. This flag is necessary
     *
     * WineD3D only transforms and clips the vertices by now, so EXTENTS and LIGHT
     * are not implemented. Clipping is disabled ATM, because of unsure conditions.
     */
372 373
    if (!(vertex_op & D3DVOP_TRANSFORM))
        return DDERR_INVALIDPARAMS;
374

375 376
    wined3d_mutex_lock();

377 378 379 380
    /* WineD3D doesn't know d3d7 vertex operation, it uses
     * render states instead. Set the render states according to
     * the vertex ops
     */
381
    doClip = !!(vertex_op & D3DVOP_CLIP);
382
    oldClip = wined3d_device_get_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING);
383
    if (doClip != oldClip)
384
        wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, doClip);
385

386
    wined3d_device_set_stream_source(device_impl->wined3d_device,
387 388 389 390
            0, src_buffer_impl->wineD3DVertexBuffer, 0, get_flexible_vertex_size(src_buffer_impl->fvf));
    wined3d_device_set_vertex_declaration(device_impl->wined3d_device, src_buffer_impl->wineD3DVertexDeclaration);
    hr = wined3d_device_process_vertices(device_impl->wined3d_device, src_idx, dst_idx,
            count, dst_buffer_impl->wineD3DVertexBuffer, NULL, flags, dst_buffer_impl->fvf);
391 392

    /* Restore the states if needed */
393
    if (doClip != oldClip)
394
        wined3d_device_set_render_state(device_impl->wined3d_device, WINED3D_RS_CLIPPING, oldClip);
395 396 397

    wined3d_mutex_unlock();

398
    return hr;
399 400
}

401 402 403
static HRESULT WINAPI d3d_vertex_buffer1_ProcessVertices(IDirect3DVertexBuffer *iface,
        DWORD vertex_op, DWORD dst_idx, DWORD count, IDirect3DVertexBuffer *src_buffer,
        DWORD src_idx, IDirect3DDevice3 *device, DWORD flags)
404
{
405 406
    struct d3d_vertex_buffer *dst_buffer_impl = impl_from_IDirect3DVertexBuffer(iface);
    struct d3d_vertex_buffer *src_buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer(src_buffer);
407
    struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
408

409
    TRACE("iface %p, vertex_op %#x, dst_idx %u, count %u, src_buffer %p, src_idx %u, device %p, flags %#x.\n",
410
            iface, vertex_op, dst_idx, count, src_buffer, src_idx, device, flags);
411

412 413 414
    return d3d_vertex_buffer7_ProcessVertices(&dst_buffer_impl->IDirect3DVertexBuffer7_iface, vertex_op,
            dst_idx, count, &src_buffer_impl->IDirect3DVertexBuffer7_iface, src_idx,
            device_impl ? &device_impl->IDirect3DDevice7_iface : NULL, flags);
415 416
}

417 418 419 420 421 422 423 424 425 426 427 428 429
/*****************************************************************************
 * IDirect3DVertexBuffer7::GetVertexBufferDesc
 *
 * Returns the description of a vertex buffer
 *
 * Params:
 *  Desc: Address to write the description to
 *
 * Returns
 *  DDERR_INVALIDPARAMS if Desc is NULL
 *  D3D_OK on success
 *
 *****************************************************************************/
430
static HRESULT WINAPI d3d_vertex_buffer7_GetVertexBufferDesc(IDirect3DVertexBuffer7 *iface, D3DVERTEXBUFFERDESC *desc)
431
{
432
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
433
    struct wined3d_resource_desc wined3d_desc;
434
    struct wined3d_resource *wined3d_resource;
435

436
    TRACE("iface %p, desc %p.\n", iface, desc);
437

438
    if (!desc) return DDERR_INVALIDPARAMS;
439

440
    wined3d_mutex_lock();
441
    wined3d_resource = wined3d_buffer_get_resource(buffer->wineD3DVertexBuffer);
442
    wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
443
    wined3d_mutex_unlock();
444

445 446 447 448
    /* Now fill the desc structure */
    desc->dwCaps = buffer->Caps;
    desc->dwFVF = buffer->fvf;
    desc->dwNumVertices = wined3d_desc.size / get_flexible_vertex_size(buffer->fvf);
449

450
    return D3D_OK;
451 452
}

453
static HRESULT WINAPI d3d_vertex_buffer1_GetVertexBufferDesc(IDirect3DVertexBuffer *iface, D3DVERTEXBUFFERDESC *desc)
454
{
455
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
456

457
    TRACE("iface %p, desc %p.\n", iface, desc);
458

459
    return d3d_vertex_buffer7_GetVertexBufferDesc(&buffer->IDirect3DVertexBuffer7_iface, desc);
460 461 462
}


463 464 465 466 467 468 469 470 471 472 473 474 475
/*****************************************************************************
 * IDirect3DVertexBuffer7::Optimize
 *
 * Converts an unoptimized vertex buffer into an optimized buffer
 *
 * Params:
 *  D3DDevice: Device for which this buffer is optimized
 *  Flags: Not used, should be set to 0
 *
 * Returns
 *  D3D_OK, because it's a stub
 *
 *****************************************************************************/
476 477
static HRESULT WINAPI d3d_vertex_buffer7_Optimize(IDirect3DVertexBuffer7 *iface,
        IDirect3DDevice7 *device, DWORD flags)
478
{
479
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer7(iface);
480 481
    static BOOL hide = FALSE;

482
    TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
483

484 485
    if (!hide)
    {
486
        FIXME("iface %p, device %p, flags %#x stub!\n", iface, device, flags);
487 488
        hide = TRUE;
    }
489

490 491 492
    /* We could forward this call to WineD3D and take advantage
     * of it once we use OpenGL vertex buffers
     */
493
    wined3d_mutex_lock();
494
    buffer->Caps |= D3DVBCAPS_OPTIMIZED;
495
    wined3d_mutex_unlock();
496

497 498 499
    return DD_OK;
}

500 501
static HRESULT WINAPI d3d_vertex_buffer1_Optimize(IDirect3DVertexBuffer *iface,
        IDirect3DDevice3 *device, DWORD flags)
502
{
503
    struct d3d_vertex_buffer *buffer = impl_from_IDirect3DVertexBuffer(iface);
504
    struct d3d_device *device_impl = unsafe_impl_from_IDirect3DDevice3(device);
505

506
    TRACE("iface %p, device %p, flags %#x.\n", iface, device, flags);
507

508 509
    return d3d_vertex_buffer7_Optimize(&buffer->IDirect3DVertexBuffer7_iface,
            device_impl ? &device_impl->IDirect3DDevice7_iface : NULL, flags);
510
}
511

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
/*****************************************************************************
 * IDirect3DVertexBuffer7::ProcessVerticesStrided
 *
 * This method processes untransformed strided vertices into a processed
 * or optimized vertex buffer.
 *
 * For more details on the parameters, see
 * IDirect3DVertexBuffer7::ProcessVertices
 *
 * Params:
 *  VertexOp: Operations to perform
 *  DestIndex: Destination index to write the vertices to
 *  Count: Number of input vertices
 *  StrideData: Array containing the input vertices
 *  VertexTypeDesc: Vertex Description or source index?????????
 *  D3DDevice: IDirect3DDevice7 to use for processing
 *  Flags: Can be D3DPV_DONOTCOPYDATA to avoid copying unmodified vertices
 *
 * Returns
 *  D3D_OK on success, or DDERR_*
 *
 *****************************************************************************/
534 535 536
static HRESULT WINAPI d3d_vertex_buffer7_ProcessVerticesStrided(IDirect3DVertexBuffer7 *iface,
        DWORD vertex_op, DWORD dst_idx, DWORD count, D3DDRAWPRIMITIVESTRIDEDDATA *data,
        DWORD fvf, IDirect3DDevice7 *device, DWORD flags)
537
{
538 539
    FIXME("iface %p, vertex_op %#x, dst_idx %u, count %u, data %p, fvf %#x, device %p, flags %#x stub!\n",
            iface, vertex_op, dst_idx, count, data, fvf, device, flags);
540

541 542
    return DD_OK;
}
543

544 545 546
/*****************************************************************************
 * The VTables
 *****************************************************************************/
547

548
static const struct IDirect3DVertexBuffer7Vtbl d3d_vertex_buffer7_vtbl =
549
{
550 551 552 553 554 555 556 557 558
    d3d_vertex_buffer7_QueryInterface,
    d3d_vertex_buffer7_AddRef,
    d3d_vertex_buffer7_Release,
    d3d_vertex_buffer7_Lock,
    d3d_vertex_buffer7_Unlock,
    d3d_vertex_buffer7_ProcessVertices,
    d3d_vertex_buffer7_GetVertexBufferDesc,
    d3d_vertex_buffer7_Optimize,
    d3d_vertex_buffer7_ProcessVerticesStrided,
559 560
};

561
static const struct IDirect3DVertexBufferVtbl d3d_vertex_buffer1_vtbl =
562
{
563 564 565 566 567 568 569 570
    d3d_vertex_buffer1_QueryInterface,
    d3d_vertex_buffer1_AddRef,
    d3d_vertex_buffer1_Release,
    d3d_vertex_buffer1_Lock,
    d3d_vertex_buffer1_Unlock,
    d3d_vertex_buffer1_ProcessVertices,
    d3d_vertex_buffer1_GetVertexBufferDesc,
    d3d_vertex_buffer1_Optimize,
571
};
572

573 574
HRESULT d3d_vertex_buffer_create(struct d3d_vertex_buffer **vertex_buf,
        struct ddraw *ddraw, D3DVERTEXBUFFERDESC *desc)
575
{
576
    struct d3d_vertex_buffer *buffer;
577 578 579 580 581 582 583 584 585 586 587
    HRESULT hr = D3D_OK;

    TRACE("Vertex buffer description:\n");
    TRACE("    dwSize %u\n", desc->dwSize);
    TRACE("    dwCaps %#x\n", desc->dwCaps);
    TRACE("    FVF %#x\n", desc->dwFVF);
    TRACE("    dwNumVertices %u\n", desc->dwNumVertices);

    buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*buffer));
    if (!buffer)
        return DDERR_OUTOFMEMORY;
588

589
    buffer->IDirect3DVertexBuffer7_iface.lpVtbl = &d3d_vertex_buffer7_vtbl;
590
    buffer->IDirect3DVertexBuffer_iface.lpVtbl = &d3d_vertex_buffer1_vtbl;
591 592 593 594 595
    buffer->ref = 1;

    buffer->ddraw = ddraw;
    buffer->Caps = desc->dwCaps;
    buffer->fvf = desc->dwFVF;
596
    buffer->size = get_flexible_vertex_size(desc->dwFVF) * desc->dwNumVertices;
597

598
    wined3d_mutex_lock();
599

600
    hr = d3d_vertex_buffer_create_wined3d_buffer(buffer, FALSE, &buffer->wineD3DVertexBuffer);
601 602 603 604
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d vertex buffer, hr %#x.\n", hr);
        if (hr == WINED3DERR_INVALIDCALL)
605 606
            hr = DDERR_INVALIDPARAMS;
        goto end;
607 608 609 610 611 612
    }

    buffer->wineD3DVertexDeclaration = ddraw_find_decl(ddraw, desc->dwFVF);
    if (!buffer->wineD3DVertexDeclaration)
    {
        ERR("Failed to find vertex declaration for fvf %#x.\n", desc->dwFVF);
613
        wined3d_buffer_decref(buffer->wineD3DVertexBuffer);
614 615
        hr = DDERR_INVALIDPARAMS;
        goto end;
616
    }
617
    wined3d_vertex_declaration_incref(buffer->wineD3DVertexDeclaration);
618

619
end:
620
    wined3d_mutex_unlock();
621 622 623 624
    if (hr == D3D_OK)
        *vertex_buf = buffer;
    else
        HeapFree(GetProcessHeap(), 0, buffer);
625

626
    return hr;
627
}
628

629
struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer(IDirect3DVertexBuffer *iface)
630 631 632 633 634
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d_vertex_buffer1_vtbl);

635
    return impl_from_IDirect3DVertexBuffer(iface);
636 637
}

638
struct d3d_vertex_buffer *unsafe_impl_from_IDirect3DVertexBuffer7(IDirect3DVertexBuffer7 *iface)
639 640 641 642 643
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d_vertex_buffer7_vtbl);

644
    return impl_from_IDirect3DVertexBuffer7(iface);
645
}