buffer.c 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2005 Oliver Stieber
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "d3d8_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d8);

24
static inline struct d3d8_vertexbuffer *impl_from_IDirect3DVertexBuffer8(IDirect3DVertexBuffer8 *iface)
25
{
26
    return CONTAINING_RECORD(iface, struct d3d8_vertexbuffer, IDirect3DVertexBuffer8_iface);
27 28
}

29 30 31 32 33 34 35 36
static HRESULT WINAPI d3d8_vertexbuffer_QueryInterface(IDirect3DVertexBuffer8 *iface, REFIID riid, void **object)
{
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

    if (IsEqualGUID(riid, &IID_IDirect3DVertexBuffer8)
            || IsEqualGUID(riid, &IID_IDirect3DResource8)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
37
        IDirect3DVertexBuffer8_AddRef(iface);
38 39 40 41 42 43 44 45 46 47 48 49
        *object = iface;
        return S_OK;
    }

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

    *object = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI d3d8_vertexbuffer_AddRef(IDirect3DVertexBuffer8 *iface)
{
50
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
51
    ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
52 53 54 55 56

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    if (refcount == 1)
    {
57
        IDirect3DDevice8_AddRef(buffer->parent_device);
58
        wined3d_mutex_lock();
59
        wined3d_buffer_incref(buffer->wined3d_buffer);
60 61 62 63 64 65 66 67
        wined3d_mutex_unlock();
    }

    return refcount;
}

static ULONG WINAPI d3d8_vertexbuffer_Release(IDirect3DVertexBuffer8 *iface)
{
68
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
69
    ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
70 71 72 73 74

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
    {
75
        IDirect3DDevice8 *device = buffer->parent_device;
76 77

        wined3d_mutex_lock();
78
        wined3d_buffer_decref(buffer->wined3d_buffer);
79 80 81 82 83 84 85 86 87
        wined3d_mutex_unlock();

        /* Release the device last, as it may cause the device to be destroyed. */
        IDirect3DDevice8_Release(device);
    }

    return refcount;
}

88 89
static HRESULT WINAPI d3d8_vertexbuffer_GetDevice(IDirect3DVertexBuffer8 *iface,
        IDirect3DDevice8 **device)
90
{
91
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
92

93 94
    TRACE("iface %p, device %p.\n", iface, device);

95
    *device = buffer->parent_device;
96 97 98 99 100 101 102 103 104 105
    IDirect3DDevice8_AddRef(*device);

    TRACE("Returning device %p.\n", *device);

    return D3D_OK;
}

static HRESULT WINAPI d3d8_vertexbuffer_SetPrivateData(IDirect3DVertexBuffer8 *iface,
        REFGUID guid, const void *data, DWORD data_size, DWORD flags)
{
106
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
107 108 109
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
            iface, debugstr_guid(guid), data, data_size, flags);

110
    return d3d8_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
111 112 113 114 115
}

static HRESULT WINAPI d3d8_vertexbuffer_GetPrivateData(IDirect3DVertexBuffer8 *iface,
        REFGUID guid, void *data, DWORD *data_size)
{
116
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
117 118 119
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
            iface, debugstr_guid(guid), data, data_size);

120
    return d3d8_resource_get_private_data(&buffer->resource, guid, data, data_size);
121 122 123 124
}

static HRESULT WINAPI d3d8_vertexbuffer_FreePrivateData(IDirect3DVertexBuffer8 *iface, REFGUID guid)
{
125
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
126 127
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

128
    return d3d8_resource_free_private_data(&buffer->resource, guid);
129 130 131 132
}

static DWORD WINAPI d3d8_vertexbuffer_SetPriority(IDirect3DVertexBuffer8 *iface, DWORD priority)
{
133
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
134
    struct wined3d_resource *resource;
135 136 137 138 139
    DWORD previous;

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

    wined3d_mutex_lock();
140 141
    resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
    previous = wined3d_resource_set_priority(resource, priority);
142 143 144 145 146 147 148
    wined3d_mutex_unlock();

    return previous;
}

static DWORD WINAPI d3d8_vertexbuffer_GetPriority(IDirect3DVertexBuffer8 *iface)
{
149
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
150
    const struct wined3d_resource *resource;
151 152 153 154 155
    DWORD priority;

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

    wined3d_mutex_lock();
156 157
    resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
    priority = wined3d_resource_get_priority(resource);
158 159 160 161 162 163 164
    wined3d_mutex_unlock();

    return priority;
}

static void WINAPI d3d8_vertexbuffer_PreLoad(IDirect3DVertexBuffer8 *iface)
{
165
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
166

167 168 169
    TRACE("iface %p.\n", iface);

    wined3d_mutex_lock();
170
    wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
171 172 173 174 175 176 177 178 179 180
    wined3d_mutex_unlock();
}

static D3DRESOURCETYPE WINAPI d3d8_vertexbuffer_GetType(IDirect3DVertexBuffer8 *iface)
{
    TRACE("iface %p.\n", iface);

    return D3DRTYPE_VERTEXBUFFER;
}

181 182
static HRESULT WINAPI d3d8_vertexbuffer_Lock(IDirect3DVertexBuffer8 *iface, UINT offset, UINT size,
        BYTE **data, DWORD flags)
183
{
184
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
185 186
    struct wined3d_map_desc wined3d_map_desc;
    struct wined3d_box wined3d_box = {0};
187 188 189 190 191
    HRESULT hr;

    TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
            iface, offset, size, data, flags);

192 193
    wined3d_box.left = offset;
    wined3d_box.right = offset + size;
194
    wined3d_mutex_lock();
195
    hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
196
            0, &wined3d_map_desc, &wined3d_box, wined3dmapflags_from_d3dmapflags(flags));
197
    wined3d_mutex_unlock();
198
    *data = wined3d_map_desc.data;
199 200 201 202 203 204

    return hr;
}

static HRESULT WINAPI d3d8_vertexbuffer_Unlock(IDirect3DVertexBuffer8 *iface)
{
205
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
206

207 208 209
    TRACE("iface %p.\n", iface);

    wined3d_mutex_lock();
210
    wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
211 212
    wined3d_mutex_unlock();

213
    return D3D_OK;
214 215
}

216 217
static HRESULT WINAPI d3d8_vertexbuffer_GetDesc(IDirect3DVertexBuffer8 *iface,
        D3DVERTEXBUFFER_DESC *desc)
218
{
219
    struct d3d8_vertexbuffer *buffer = impl_from_IDirect3DVertexBuffer8(iface);
220
    struct wined3d_resource_desc wined3d_desc;
221
    struct wined3d_resource *wined3d_resource;
222 223 224 225

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

    wined3d_mutex_lock();
226
    wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
227
    wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
228 229
    wined3d_mutex_unlock();

230
    desc->Format = D3DFMT_VERTEXDATA;
231
    desc->Type = D3DRTYPE_VERTEXBUFFER;
232
    desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
233
    desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
234
    desc->Size = wined3d_desc.size;
235
    desc->FVF = buffer->fvf;
236

237
    return D3D_OK;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

static const IDirect3DVertexBuffer8Vtbl Direct3DVertexBuffer8_Vtbl =
{
    /* IUnknown */
    d3d8_vertexbuffer_QueryInterface,
    d3d8_vertexbuffer_AddRef,
    d3d8_vertexbuffer_Release,
    /* IDirect3DResource8 */
    d3d8_vertexbuffer_GetDevice,
    d3d8_vertexbuffer_SetPrivateData,
    d3d8_vertexbuffer_GetPrivateData,
    d3d8_vertexbuffer_FreePrivateData,
    d3d8_vertexbuffer_SetPriority,
    d3d8_vertexbuffer_GetPriority,
    d3d8_vertexbuffer_PreLoad,
    d3d8_vertexbuffer_GetType,
    /* IDirect3DVertexBuffer8 */
    d3d8_vertexbuffer_Lock,
    d3d8_vertexbuffer_Unlock,
    d3d8_vertexbuffer_GetDesc,
};

static void STDMETHODCALLTYPE d3d8_vertexbuffer_wined3d_object_destroyed(void *parent)
{
263 264
    struct d3d8_vertexbuffer *buffer = parent;
    d3d8_resource_cleanup(&buffer->resource);
265
    heap_free(buffer);
266 267 268 269 270 271 272
}

static const struct wined3d_parent_ops d3d8_vertexbuffer_wined3d_parent_ops =
{
    d3d8_vertexbuffer_wined3d_object_destroyed,
};

273
HRESULT vertexbuffer_init(struct d3d8_vertexbuffer *buffer, struct d3d8_device *device,
274 275
        UINT size, DWORD usage, DWORD fvf, D3DPOOL pool)
{
276
    struct wined3d_buffer_desc desc;
277 278
    HRESULT hr;

279 280
    if (pool == D3DPOOL_SCRATCH)
    {
281 282
        WARN("Vertex buffer with D3DPOOL_SCRATCH requested.\n");
        return D3DERR_INVALIDCALL;
283 284
    }

285
    buffer->IDirect3DVertexBuffer8_iface.lpVtbl = &Direct3DVertexBuffer8_Vtbl;
286
    d3d8_resource_init(&buffer->resource);
287 288
    buffer->fvf = fvf;

289 290 291
    desc.byte_width = size;
    desc.usage = usage & WINED3DUSAGE_MASK;
    desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
292 293
    desc.access = wined3daccess_from_d3dpool(pool, usage)
            | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
294 295 296
    desc.misc_flags = 0;
    desc.structure_byte_stride = 0;

297
    wined3d_mutex_lock();
298 299
    hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
            &d3d8_vertexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
300 301 302 303 304 305 306
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
        return hr;
    }

307
    buffer->parent_device = &device->IDirect3DDevice8_iface;
308
    IDirect3DDevice8_AddRef(buffer->parent_device);
309 310 311 312

    return D3D_OK;
}

313
struct d3d8_vertexbuffer *unsafe_impl_from_IDirect3DVertexBuffer8(IDirect3DVertexBuffer8 *iface)
314 315 316 317 318 319 320 321
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &Direct3DVertexBuffer8_Vtbl);

    return impl_from_IDirect3DVertexBuffer8(iface);
}

322
static inline struct d3d8_indexbuffer *impl_from_IDirect3DIndexBuffer8(IDirect3DIndexBuffer8 *iface)
323
{
324
    return CONTAINING_RECORD(iface, struct d3d8_indexbuffer, IDirect3DIndexBuffer8_iface);
325 326
}

327 328 329 330 331 332 333 334
static HRESULT WINAPI d3d8_indexbuffer_QueryInterface(IDirect3DIndexBuffer8 *iface, REFIID riid, void **object)
{
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

    if (IsEqualGUID(riid, &IID_IDirect3DIndexBuffer8)
            || IsEqualGUID(riid, &IID_IDirect3DResource8)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
335
        IDirect3DIndexBuffer8_AddRef(iface);
336 337 338 339 340 341 342 343 344 345 346 347
        *object = iface;
        return S_OK;
    }

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

    *object = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI d3d8_indexbuffer_AddRef(IDirect3DIndexBuffer8 *iface)
{
348
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
349
    ULONG refcount = InterlockedIncrement(&buffer->resource.refcount);
350 351 352 353 354

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    if (refcount == 1)
    {
355
        IDirect3DDevice8_AddRef(buffer->parent_device);
356
        wined3d_mutex_lock();
357
        wined3d_buffer_incref(buffer->wined3d_buffer);
358 359 360 361 362 363 364 365
        wined3d_mutex_unlock();
    }

    return refcount;
}

static ULONG WINAPI d3d8_indexbuffer_Release(IDirect3DIndexBuffer8 *iface)
{
366
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
367
    ULONG refcount = InterlockedDecrement(&buffer->resource.refcount);
368 369 370 371 372

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
    {
373
        IDirect3DDevice8 *device = buffer->parent_device;
374 375

        wined3d_mutex_lock();
376
        wined3d_buffer_decref(buffer->wined3d_buffer);
377 378 379 380 381 382 383 384 385
        wined3d_mutex_unlock();

        /* Release the device last, as it may cause the device to be destroyed. */
        IDirect3DDevice8_Release(device);
    }

    return refcount;
}

386 387
static HRESULT WINAPI d3d8_indexbuffer_GetDevice(IDirect3DIndexBuffer8 *iface,
        IDirect3DDevice8 **device)
388
{
389
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
390

391 392
    TRACE("iface %p, device %p.\n", iface, device);

393
    *device = buffer->parent_device;
394 395 396 397 398 399 400 401 402 403
    IDirect3DDevice8_AddRef(*device);

    TRACE("Returning device %p.\n", *device);

    return D3D_OK;
}

static HRESULT WINAPI d3d8_indexbuffer_SetPrivateData(IDirect3DIndexBuffer8 *iface,
        REFGUID guid, const void *data, DWORD data_size, DWORD flags)
{
404
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
405 406 407
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
            iface, debugstr_guid(guid), data, data_size, flags);

408
    return d3d8_resource_set_private_data(&buffer->resource, guid, data, data_size, flags);
409 410 411 412 413
}

static HRESULT WINAPI d3d8_indexbuffer_GetPrivateData(IDirect3DIndexBuffer8 *iface,
        REFGUID guid, void *data, DWORD *data_size)
{
414
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
415 416 417
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
            iface, debugstr_guid(guid), data, data_size);

418
    return d3d8_resource_get_private_data(&buffer->resource, guid, data, data_size);
419 420 421 422
}

static HRESULT WINAPI d3d8_indexbuffer_FreePrivateData(IDirect3DIndexBuffer8 *iface, REFGUID guid)
{
423
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
424 425
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

426
    return d3d8_resource_free_private_data(&buffer->resource, guid);
427 428 429 430
}

static DWORD WINAPI d3d8_indexbuffer_SetPriority(IDirect3DIndexBuffer8 *iface, DWORD priority)
{
431
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
432
    struct wined3d_resource *resource;
433 434 435 436 437
    DWORD previous;

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

    wined3d_mutex_lock();
438 439
    resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
    previous = wined3d_resource_set_priority(resource, priority);
440 441 442 443 444 445 446
    wined3d_mutex_unlock();

    return previous;
}

static DWORD WINAPI d3d8_indexbuffer_GetPriority(IDirect3DIndexBuffer8 *iface)
{
447
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
448
    const struct wined3d_resource *resource;
449 450 451 452 453
    DWORD priority;

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

    wined3d_mutex_lock();
454 455
    resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
    priority = wined3d_resource_get_priority(resource);
456 457 458 459 460 461 462
    wined3d_mutex_unlock();

    return priority;
}

static void WINAPI d3d8_indexbuffer_PreLoad(IDirect3DIndexBuffer8 *iface)
{
463
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
464

465 466 467
    TRACE("iface %p.\n", iface);

    wined3d_mutex_lock();
468
    wined3d_resource_preload(wined3d_buffer_get_resource(buffer->wined3d_buffer));
469 470 471 472 473 474 475 476 477 478
    wined3d_mutex_unlock();
}

static D3DRESOURCETYPE WINAPI d3d8_indexbuffer_GetType(IDirect3DIndexBuffer8 *iface)
{
    TRACE("iface %p.\n", iface);

    return D3DRTYPE_INDEXBUFFER;
}

479 480
static HRESULT WINAPI d3d8_indexbuffer_Lock(IDirect3DIndexBuffer8 *iface, UINT offset, UINT size,
        BYTE **data, DWORD flags)
481
{
482
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
483 484
    struct wined3d_map_desc wined3d_map_desc;
    struct wined3d_box wined3d_box = {0};
485 486 487 488 489
    HRESULT hr;

    TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
            iface, offset, size, data, flags);

490 491
    wined3d_box.left = offset;
    wined3d_box.right = offset + size;
492
    wined3d_mutex_lock();
493
    hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer),
494
            0, &wined3d_map_desc, &wined3d_box, wined3dmapflags_from_d3dmapflags(flags));
495
    wined3d_mutex_unlock();
496
    *data = wined3d_map_desc.data;
497 498 499 500 501 502

    return hr;
}

static HRESULT WINAPI d3d8_indexbuffer_Unlock(IDirect3DIndexBuffer8 *iface)
{
503
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
504

505 506 507
    TRACE("iface %p.\n", iface);

    wined3d_mutex_lock();
508
    wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
509 510
    wined3d_mutex_unlock();

511
    return D3D_OK;
512 513
}

514 515
static HRESULT WINAPI d3d8_indexbuffer_GetDesc(IDirect3DIndexBuffer8 *iface,
        D3DINDEXBUFFER_DESC *desc)
516
{
517
    struct d3d8_indexbuffer *buffer = impl_from_IDirect3DIndexBuffer8(iface);
518
    struct wined3d_resource_desc wined3d_desc;
519
    struct wined3d_resource *wined3d_resource;
520 521 522 523

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

    wined3d_mutex_lock();
524
    wined3d_resource = wined3d_buffer_get_resource(buffer->wined3d_buffer);
525
    wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
526 527
    wined3d_mutex_unlock();

528 529
    desc->Format = d3dformat_from_wined3dformat(buffer->format);
    desc->Type = D3DRTYPE_INDEXBUFFER;
530
    desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
531
    desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
532
    desc->Size = wined3d_desc.size;
533

534
    return D3D_OK;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
}

static const IDirect3DIndexBuffer8Vtbl d3d8_indexbuffer_vtbl =
{
    /* IUnknown */
    d3d8_indexbuffer_QueryInterface,
    d3d8_indexbuffer_AddRef,
    d3d8_indexbuffer_Release,
    /* IDirect3DResource8 */
    d3d8_indexbuffer_GetDevice,
    d3d8_indexbuffer_SetPrivateData,
    d3d8_indexbuffer_GetPrivateData,
    d3d8_indexbuffer_FreePrivateData,
    d3d8_indexbuffer_SetPriority,
    d3d8_indexbuffer_GetPriority,
    d3d8_indexbuffer_PreLoad,
    d3d8_indexbuffer_GetType,
    /* IDirect3DIndexBuffer8 */
    d3d8_indexbuffer_Lock,
    d3d8_indexbuffer_Unlock,
    d3d8_indexbuffer_GetDesc,
};

static void STDMETHODCALLTYPE d3d8_indexbuffer_wined3d_object_destroyed(void *parent)
{
560 561
    struct d3d8_indexbuffer *buffer = parent;
    d3d8_resource_cleanup(&buffer->resource);
562
    heap_free(buffer);
563 564 565 566 567 568 569
}

static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
{
    d3d8_indexbuffer_wined3d_object_destroyed,
};

570
HRESULT indexbuffer_init(struct d3d8_indexbuffer *buffer, struct d3d8_device *device,
571 572
        UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
573
    struct wined3d_buffer_desc desc;
574 575
    HRESULT hr;

576 577
    desc.byte_width = size;
    desc.usage = (usage & WINED3DUSAGE_MASK) | WINED3DUSAGE_STATICDECL;
578
    if (pool == D3DPOOL_SCRATCH)
579 580
        desc.usage |= WINED3DUSAGE_SCRATCH;
    desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
581 582
    desc.access = wined3daccess_from_d3dpool(pool, usage)
            | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
583 584
    desc.misc_flags = 0;
    desc.structure_byte_stride = 0;
585

586
    buffer->IDirect3DIndexBuffer8_iface.lpVtbl = &d3d8_indexbuffer_vtbl;
587
    d3d8_resource_init(&buffer->resource);
588 589 590
    buffer->format = wined3dformat_from_d3dformat(format);

    wined3d_mutex_lock();
591 592
    hr = wined3d_buffer_create(device->wined3d_device, &desc, NULL, buffer,
            &d3d8_indexbuffer_wined3d_parent_ops, &buffer->wined3d_buffer);
593 594 595 596 597 598 599
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
        return hr;
    }

600
    buffer->parent_device = &device->IDirect3DDevice8_iface;
601
    IDirect3DDevice8_AddRef(buffer->parent_device);
602 603 604

    return D3D_OK;
}
605

606
struct d3d8_indexbuffer *unsafe_impl_from_IDirect3DIndexBuffer8(IDirect3DIndexBuffer8 *iface)
607 608 609 610 611 612 613
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d8_indexbuffer_vtbl);

    return impl_from_IDirect3DIndexBuffer8(iface);
}