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

#include "config.h"
#include "d3d9_private.h"

24
WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
25

26
static inline struct d3d9_texture *impl_from_IDirect3DTexture9(IDirect3DTexture9 *iface)
27
{
28
    return CONTAINING_RECORD(iface, struct d3d9_texture, IDirect3DBaseTexture9_iface);
29 30
}

31 32 33 34 35
static inline struct d3d9_texture *impl_from_IDirect3DCubeTexture9(IDirect3DCubeTexture9 *iface)
{
    return CONTAINING_RECORD(iface, struct d3d9_texture, IDirect3DBaseTexture9_iface);
}

36 37 38 39 40
static inline struct d3d9_texture *impl_from_IDirect3DVolumeTexture9(IDirect3DVolumeTexture9 *iface)
{
    return CONTAINING_RECORD(iface, struct d3d9_texture, IDirect3DBaseTexture9_iface);
}

41
static HRESULT WINAPI d3d9_texture_2d_QueryInterface(IDirect3DTexture9 *iface, REFIID riid, void **out)
42
{
43
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
Henri Verbeet's avatar
Henri Verbeet committed
44

45 46 47 48 49
    if (IsEqualGUID(riid, &IID_IDirect3DTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DResource9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
50
        IDirect3DTexture9_AddRef(iface);
51
        *out = iface;
H. Verbeet's avatar
H. Verbeet committed
52
        return S_OK;
53 54
    }

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

57
    *out = NULL;
58 59 60
    return E_NOINTERFACE;
}

61
static ULONG WINAPI d3d9_texture_2d_AddRef(IDirect3DTexture9 *iface)
62
{
63
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
64
    ULONG ref = InterlockedIncrement(&texture->resource.refcount);
65

Henri Verbeet's avatar
Henri Verbeet committed
66
    TRACE("%p increasing refcount to %u.\n", iface, ref);
67

68 69
    if (ref == 1)
    {
70 71
        struct d3d9_surface *surface;

72
        IDirect3DDevice9Ex_AddRef(texture->parent_device);
73
        wined3d_mutex_lock();
74 75 76 77
        LIST_FOR_EACH_ENTRY(surface, &texture->rtv_list, struct d3d9_surface, rtv_entry)
        {
            wined3d_rendertarget_view_incref(surface->wined3d_rtv);
        }
78
        wined3d_texture_incref(texture->wined3d_texture);
79 80 81
        wined3d_mutex_unlock();
    }

82
    return ref;
83 84
}

85
static ULONG WINAPI d3d9_texture_2d_Release(IDirect3DTexture9 *iface)
86
{
87
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
88
    ULONG ref = InterlockedDecrement(&texture->resource.refcount);
89

Henri Verbeet's avatar
Henri Verbeet committed
90
    TRACE("%p decreasing refcount to %u.\n", iface, ref);
91

92 93 94
    if (!ref)
    {
        IDirect3DDevice9Ex *parent_device = texture->parent_device;
95
        struct d3d9_surface *surface;
96

97
        wined3d_mutex_lock();
98 99 100 101
        LIST_FOR_EACH_ENTRY(surface, &texture->rtv_list, struct d3d9_surface, rtv_entry)
        {
            wined3d_rendertarget_view_decref(surface->wined3d_rtv);
        }
102
        wined3d_texture_decref(texture->wined3d_texture);
103
        wined3d_mutex_unlock();
104 105

        /* Release the device last, as it may cause the device to be destroyed. */
106
        IDirect3DDevice9Ex_Release(parent_device);
107 108 109 110
    }
    return ref;
}

111
static HRESULT WINAPI d3d9_texture_2d_GetDevice(IDirect3DTexture9 *iface, IDirect3DDevice9 **device)
112
{
113
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
114

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

117
    *device = (IDirect3DDevice9 *)texture->parent_device;
118
    IDirect3DDevice9_AddRef(*device);
119

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

    return D3D_OK;
123 124
}

125 126
static HRESULT WINAPI d3d9_texture_2d_SetPrivateData(IDirect3DTexture9 *iface,
        REFGUID guid, const void *data, DWORD data_size, DWORD flags)
127
{
128
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
129
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
130
            iface, debugstr_guid(guid), data, data_size, flags);
131

132
    return d3d9_resource_set_private_data(&texture->resource, guid, data, data_size, flags);
133 134
}

135 136
static HRESULT WINAPI d3d9_texture_2d_GetPrivateData(IDirect3DTexture9 *iface,
        REFGUID guid, void *data, DWORD *data_size)
137
{
138
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
139
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
140
            iface, debugstr_guid(guid), data, data_size);
141

142
    return d3d9_resource_get_private_data(&texture->resource, guid, data, data_size);
143 144
}

145
static HRESULT WINAPI d3d9_texture_2d_FreePrivateData(IDirect3DTexture9 *iface, REFGUID guid)
146
{
147 148
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
149

150
    return d3d9_resource_free_private_data(&texture->resource, guid);
151 152
}

153
static DWORD WINAPI d3d9_texture_2d_SetPriority(IDirect3DTexture9 *iface, DWORD priority)
154
{
155
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
156
    struct wined3d_resource *resource;
157
    DWORD ret;
Henri Verbeet's avatar
Henri Verbeet committed
158

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

161
    wined3d_mutex_lock();
162 163
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_set_priority(resource, priority);
164 165
    wined3d_mutex_unlock();

166
    return ret;
167 168
}

169
static DWORD WINAPI d3d9_texture_2d_GetPriority(IDirect3DTexture9 *iface)
170
{
171
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
172
    const struct wined3d_resource *resource;
173
    DWORD ret;
Henri Verbeet's avatar
Henri Verbeet committed
174 175

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

177
    wined3d_mutex_lock();
178 179
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_get_priority(resource);
180 181
    wined3d_mutex_unlock();

182
    return ret;
183 184
}

185
static void WINAPI d3d9_texture_2d_PreLoad(IDirect3DTexture9 *iface)
186
{
187
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
188 189

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

191
    wined3d_mutex_lock();
192
    wined3d_texture_preload(texture->wined3d_texture);
193
    wined3d_mutex_unlock();
194 195
}

196
static D3DRESOURCETYPE WINAPI d3d9_texture_2d_GetType(IDirect3DTexture9 *iface)
197
{
Henri Verbeet's avatar
Henri Verbeet committed
198
    TRACE("iface %p.\n", iface);
199

200
    return D3DRTYPE_TEXTURE;
201 202
}

203
static DWORD WINAPI d3d9_texture_2d_SetLOD(IDirect3DTexture9 *iface, DWORD lod)
204
{
205
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
206
    DWORD ret;
Henri Verbeet's avatar
Henri Verbeet committed
207

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

210
    wined3d_mutex_lock();
211
    ret = wined3d_texture_set_lod(texture->wined3d_texture, lod);
212 213
    wined3d_mutex_unlock();

214
    return ret;
215 216
}

217
static DWORD WINAPI d3d9_texture_2d_GetLOD(IDirect3DTexture9 *iface)
218
{
219
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
220
    DWORD ret;
Henri Verbeet's avatar
Henri Verbeet committed
221 222

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

224
    wined3d_mutex_lock();
225
    ret = wined3d_texture_get_lod(texture->wined3d_texture);
226 227
    wined3d_mutex_unlock();

228
    return ret;
229 230
}

231
static DWORD WINAPI d3d9_texture_2d_GetLevelCount(IDirect3DTexture9 *iface)
232
{
233
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
234
    DWORD ret;
Henri Verbeet's avatar
Henri Verbeet committed
235 236

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

238
    wined3d_mutex_lock();
239
    ret = wined3d_texture_get_level_count(texture->wined3d_texture);
240 241
    wined3d_mutex_unlock();

242
    return ret;
243 244
}

245
static HRESULT WINAPI d3d9_texture_2d_SetAutoGenFilterType(IDirect3DTexture9 *iface, D3DTEXTUREFILTERTYPE filter_type)
246
{
247
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
248
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
249

250
    TRACE("iface %p, filter_type %#x.\n", iface, filter_type);
251

252
    wined3d_mutex_lock();
253 254
    hr = wined3d_texture_set_autogen_filter_type(texture->wined3d_texture,
            (enum wined3d_texture_filter_type)filter_type);
255 256
    wined3d_mutex_unlock();

257
    return hr;
258 259
}

260
static D3DTEXTUREFILTERTYPE WINAPI d3d9_texture_2d_GetAutoGenFilterType(IDirect3DTexture9 *iface)
261
{
262
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
263
    D3DTEXTUREFILTERTYPE ret;
Henri Verbeet's avatar
Henri Verbeet committed
264 265

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

267
    wined3d_mutex_lock();
268
    ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(texture->wined3d_texture);
269 270
    wined3d_mutex_unlock();

271
    return ret;
272 273
}

274
static void WINAPI d3d9_texture_2d_GenerateMipSubLevels(IDirect3DTexture9 *iface)
275
{
276
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
Henri Verbeet's avatar
Henri Verbeet committed
277 278

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

280
    wined3d_mutex_lock();
281
    wined3d_texture_generate_mipmaps(texture->wined3d_texture);
282
    wined3d_mutex_unlock();
283 284
}

285
static HRESULT WINAPI d3d9_texture_2d_GetLevelDesc(IDirect3DTexture9 *iface, UINT level, D3DSURFACE_DESC *desc)
286
{
287
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
288 289
    struct wined3d_resource *sub_resource;
    HRESULT hr = D3D_OK;
290

291
    TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
292

293
    wined3d_mutex_lock();
294
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
295 296
        hr = D3DERR_INVALIDCALL;
    else
297
    {
298 299 300
        struct wined3d_resource_desc wined3d_desc;

        wined3d_resource_get_desc(sub_resource, &wined3d_desc);
301 302
        desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
        desc->Type = wined3d_desc.resource_type;
303
        desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
304 305 306 307 308
        desc->Pool = wined3d_desc.pool;
        desc->MultiSampleType = wined3d_desc.multisample_type;
        desc->MultiSampleQuality = wined3d_desc.multisample_quality;
        desc->Width = wined3d_desc.width;
        desc->Height = wined3d_desc.height;
309
    }
310
    wined3d_mutex_unlock();
311

312
    return hr;
313 314
}

315
static HRESULT WINAPI d3d9_texture_2d_GetSurfaceLevel(IDirect3DTexture9 *iface,
316
        UINT level, IDirect3DSurface9 **surface)
317
{
318
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
319
    struct wined3d_resource *sub_resource;
320
    struct d3d9_surface *surface_impl;
321

322
    TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
323 324

    wined3d_mutex_lock();
325
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
326
    {
327 328
        wined3d_mutex_unlock();
        return D3DERR_INVALIDCALL;
329
    }
330

331 332
    surface_impl = wined3d_resource_get_parent(sub_resource);
    *surface = &surface_impl->IDirect3DSurface9_iface;
333
    IDirect3DSurface9_AddRef(*surface);
334 335
    wined3d_mutex_unlock();

336
    return D3D_OK;
337 338
}

339
static HRESULT WINAPI d3d9_texture_2d_LockRect(IDirect3DTexture9 *iface,
340 341
        UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
{
342
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
343
    struct wined3d_resource *sub_resource;
344
    struct d3d9_surface *surface_impl;
345
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
346 347

    TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
348
            iface, level, locked_rect, rect, flags);
349

350
    wined3d_mutex_lock();
351
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
352 353
        hr = D3DERR_INVALIDCALL;
    else
354 355 356 357
    {
        surface_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DSurface9_LockRect(&surface_impl->IDirect3DSurface9_iface, locked_rect, rect, flags);
    }
358 359
    wined3d_mutex_unlock();

360
    return hr;
361 362
}

363
static HRESULT WINAPI d3d9_texture_2d_UnlockRect(IDirect3DTexture9 *iface, UINT level)
364
{
365
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
366
    struct wined3d_resource *sub_resource;
367
    struct d3d9_surface *surface_impl;
368
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
369

370
    TRACE("iface %p, level %u.\n", iface, level);
371

372
    wined3d_mutex_lock();
373
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
374 375
        hr = D3DERR_INVALIDCALL;
    else
376 377 378 379
    {
        surface_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DSurface9_UnlockRect(&surface_impl->IDirect3DSurface9_iface);
    }
380 381
    wined3d_mutex_unlock();

382
    return hr;
383 384
}

385
static HRESULT WINAPI d3d9_texture_2d_AddDirtyRect(IDirect3DTexture9 *iface, const RECT *dirty_rect)
386
{
387
    struct d3d9_texture *texture = impl_from_IDirect3DTexture9(iface);
388
    HRESULT hr;
Henri Verbeet's avatar
Henri Verbeet committed
389

390 391
    TRACE("iface %p, dirty_rect %s.\n",
            iface, wine_dbgstr_rect(dirty_rect));
392

393
    wined3d_mutex_lock();
394
    if (!dirty_rect)
395
        hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, NULL);
396 397
    else
    {
398 399 400 401 402 403 404 405
        struct wined3d_box dirty_region;

        dirty_region.left = dirty_rect->left;
        dirty_region.top = dirty_rect->top;
        dirty_region.right = dirty_rect->right;
        dirty_region.bottom = dirty_rect->bottom;
        dirty_region.front = 0;
        dirty_region.back = 1;
406
        hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, &dirty_region);
407
    }
408 409
    wined3d_mutex_unlock();

410
    return hr;
411 412
}

413
static const IDirect3DTexture9Vtbl d3d9_texture_2d_vtbl =
414
{
415
    /* IUnknown */
416 417 418
    d3d9_texture_2d_QueryInterface,
    d3d9_texture_2d_AddRef,
    d3d9_texture_2d_Release,
419
     /* IDirect3DResource9 */
420 421 422 423 424 425 426 427
    d3d9_texture_2d_GetDevice,
    d3d9_texture_2d_SetPrivateData,
    d3d9_texture_2d_GetPrivateData,
    d3d9_texture_2d_FreePrivateData,
    d3d9_texture_2d_SetPriority,
    d3d9_texture_2d_GetPriority,
    d3d9_texture_2d_PreLoad,
    d3d9_texture_2d_GetType,
428
    /* IDirect3dBaseTexture9 */
429 430 431 432 433 434
    d3d9_texture_2d_SetLOD,
    d3d9_texture_2d_GetLOD,
    d3d9_texture_2d_GetLevelCount,
    d3d9_texture_2d_SetAutoGenFilterType,
    d3d9_texture_2d_GetAutoGenFilterType,
    d3d9_texture_2d_GenerateMipSubLevels,
435
    /* IDirect3DTexture9 */
436 437 438 439 440
    d3d9_texture_2d_GetLevelDesc,
    d3d9_texture_2d_GetSurfaceLevel,
    d3d9_texture_2d_LockRect,
    d3d9_texture_2d_UnlockRect,
    d3d9_texture_2d_AddDirtyRect,
441 442
};

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
static HRESULT WINAPI d3d9_texture_cube_QueryInterface(IDirect3DCubeTexture9 *iface, REFIID riid, void **out)
{
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    if (IsEqualGUID(riid, &IID_IDirect3DCubeTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DResource9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
        IDirect3DCubeTexture9_AddRef(iface);
        *out = iface;
        return S_OK;
    }

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

    *out = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI d3d9_texture_cube_AddRef(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
466
    ULONG ref = InterlockedIncrement(&texture->resource.refcount);
467 468 469 470 471

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

    if (ref == 1)
    {
472 473
        struct d3d9_surface *surface;

474 475
        IDirect3DDevice9Ex_AddRef(texture->parent_device);
        wined3d_mutex_lock();
476 477 478 479
        LIST_FOR_EACH_ENTRY(surface, &texture->rtv_list, struct d3d9_surface, rtv_entry)
        {
            wined3d_rendertarget_view_decref(surface->wined3d_rtv);
        }
480 481 482 483 484 485 486 487 488 489
        wined3d_texture_incref(texture->wined3d_texture);
        wined3d_mutex_unlock();
    }

    return ref;
}

static ULONG WINAPI d3d9_texture_cube_Release(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
490
    ULONG ref = InterlockedDecrement(&texture->resource.refcount);
491 492 493 494 495 496

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

    if (!ref)
    {
        IDirect3DDevice9Ex *parent_device = texture->parent_device;
497
        struct d3d9_surface *surface;
498 499 500 501

        TRACE("Releasing child %p.\n", texture->wined3d_texture);

        wined3d_mutex_lock();
502 503 504 505
        LIST_FOR_EACH_ENTRY(surface, &texture->rtv_list, struct d3d9_surface, rtv_entry)
        {
            wined3d_rendertarget_view_decref(surface->wined3d_rtv);
        }
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
        wined3d_texture_decref(texture->wined3d_texture);
        wined3d_mutex_unlock();

        /* Release the device last, as it may cause the device to be destroyed. */
        IDirect3DDevice9Ex_Release(parent_device);
    }
    return ref;
}

static HRESULT WINAPI d3d9_texture_cube_GetDevice(IDirect3DCubeTexture9 *iface, IDirect3DDevice9 **device)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);

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

    *device = (IDirect3DDevice9 *)texture->parent_device;
    IDirect3DDevice9_AddRef(*device);

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

    return D3D_OK;
}

static HRESULT WINAPI d3d9_texture_cube_SetPrivateData(IDirect3DCubeTexture9 *iface,
        REFGUID guid, const void *data, DWORD data_size, DWORD flags)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
            iface, debugstr_guid(guid), data, data_size, flags);

536
    return d3d9_resource_set_private_data(&texture->resource, guid, data, data_size, flags);
537 538 539 540 541 542 543 544 545
}

static HRESULT WINAPI d3d9_texture_cube_GetPrivateData(IDirect3DCubeTexture9 *iface,
        REFGUID guid, void *data, DWORD *data_size)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
            iface, debugstr_guid(guid), data, data_size);

546
    return d3d9_resource_get_private_data(&texture->resource, guid, data, data_size);
547 548 549 550 551 552 553
}

static HRESULT WINAPI d3d9_texture_cube_FreePrivateData(IDirect3DCubeTexture9 *iface, REFGUID guid)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

554
    return d3d9_resource_free_private_data(&texture->resource, guid);
555 556 557 558 559
}

static DWORD WINAPI d3d9_texture_cube_SetPriority(IDirect3DCubeTexture9 *iface, DWORD priority)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
560
    struct wined3d_resource *resource;
561 562 563 564 565
    DWORD ret;

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

    wined3d_mutex_lock();
566 567
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_set_priority(resource, priority);
568 569 570 571 572 573 574 575
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_cube_GetPriority(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
576
    const struct wined3d_resource *resource;
577 578 579 580 581
    DWORD ret;

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

    wined3d_mutex_lock();
582 583
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_get_priority(resource);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    wined3d_mutex_unlock();

    return ret;
}

static void WINAPI d3d9_texture_cube_PreLoad(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);

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

    wined3d_mutex_lock();
    wined3d_texture_preload(texture->wined3d_texture);
    wined3d_mutex_unlock();
}

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

    return D3DRTYPE_CUBETEXTURE;
}

static DWORD WINAPI d3d9_texture_cube_SetLOD(IDirect3DCubeTexture9 *iface, DWORD lod)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_set_lod(texture->wined3d_texture, lod);
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_cube_GetLOD(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_get_lod(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_cube_GetLevelCount(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_get_level_count(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return ret;
}

static HRESULT WINAPI d3d9_texture_cube_SetAutoGenFilterType(IDirect3DCubeTexture9 *iface,
        D3DTEXTUREFILTERTYPE filter_type)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    HRESULT hr;

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

    wined3d_mutex_lock();
    hr = wined3d_texture_set_autogen_filter_type(texture->wined3d_texture,
            (enum wined3d_texture_filter_type)filter_type);
    wined3d_mutex_unlock();

    return hr;
}

static D3DTEXTUREFILTERTYPE WINAPI d3d9_texture_cube_GetAutoGenFilterType(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    D3DTEXTUREFILTERTYPE ret;

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

    wined3d_mutex_lock();
    ret = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return ret;
}

static void WINAPI d3d9_texture_cube_GenerateMipSubLevels(IDirect3DCubeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);

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

    wined3d_mutex_lock();
    wined3d_texture_generate_mipmaps(texture->wined3d_texture);
    wined3d_mutex_unlock();
}

static HRESULT WINAPI d3d9_texture_cube_GetLevelDesc(IDirect3DCubeTexture9 *iface, UINT level, D3DSURFACE_DESC *desc)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    struct wined3d_resource *sub_resource;
    HRESULT hr = D3D_OK;
695
    DWORD level_count;
696 697 698 699

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

    wined3d_mutex_lock();
700 701 702 703 704 705 706
    level_count = wined3d_texture_get_level_count(texture->wined3d_texture);
    if (level >= level_count)
    {
        wined3d_mutex_unlock();
        return D3DERR_INVALIDCALL;
    }

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
        hr = D3DERR_INVALIDCALL;
    else
    {
        struct wined3d_resource_desc wined3d_desc;

        wined3d_resource_get_desc(sub_resource, &wined3d_desc);
        desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
        desc->Type = wined3d_desc.resource_type;
        desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
        desc->Pool = wined3d_desc.pool;
        desc->MultiSampleType = wined3d_desc.multisample_type;
        desc->MultiSampleQuality = wined3d_desc.multisample_quality;
        desc->Width = wined3d_desc.width;
        desc->Height = wined3d_desc.height;
    }
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT WINAPI d3d9_texture_cube_GetCubeMapSurface(IDirect3DCubeTexture9 *iface,
        D3DCUBEMAP_FACES face, UINT level, IDirect3DSurface9 **surface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    struct wined3d_resource *sub_resource;
733
    struct d3d9_surface *surface_impl;
734
    UINT sub_resource_idx;
735
    DWORD level_count;
736 737 738 739

    TRACE("iface %p, face %#x, level %u, surface %p.\n", iface, face, level, surface);

    wined3d_mutex_lock();
740 741 742 743 744 745 746 747
    level_count = wined3d_texture_get_level_count(texture->wined3d_texture);
    if (level >= level_count)
    {
        wined3d_mutex_unlock();
        return D3DERR_INVALIDCALL;
    }

    sub_resource_idx = level_count * face + level;
748 749 750 751 752 753
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
    {
        wined3d_mutex_unlock();
        return D3DERR_INVALIDCALL;
    }

754 755
    surface_impl = wined3d_resource_get_parent(sub_resource);
    *surface = &surface_impl->IDirect3DSurface9_iface;
756 757 758 759 760 761 762 763 764 765 766 767
    IDirect3DSurface9_AddRef(*surface);
    wined3d_mutex_unlock();

    return D3D_OK;
}

static HRESULT WINAPI d3d9_texture_cube_LockRect(IDirect3DCubeTexture9 *iface,
        D3DCUBEMAP_FACES face, UINT level, D3DLOCKED_RECT *locked_rect, const RECT *rect,
        DWORD flags)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    struct wined3d_resource *sub_resource;
768
    struct d3d9_surface *surface_impl;
769 770 771 772 773 774 775 776 777 778 779
    UINT sub_resource_idx;
    HRESULT hr;

    TRACE("iface %p, face %#x, level %u, locked_rect %p, rect %p, flags %#x.\n",
            iface, face, level, locked_rect, rect, flags);

    wined3d_mutex_lock();
    sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
        hr = D3DERR_INVALIDCALL;
    else
780 781 782 783
    {
        surface_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DSurface9_LockRect(&surface_impl->IDirect3DSurface9_iface, locked_rect, rect, flags);
    }
784 785 786 787 788 789 790 791 792 793
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT WINAPI d3d9_texture_cube_UnlockRect(IDirect3DCubeTexture9 *iface,
        D3DCUBEMAP_FACES face, UINT level)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    struct wined3d_resource *sub_resource;
794
    struct d3d9_surface *surface_impl;
795 796 797 798 799 800 801 802 803 804
    UINT sub_resource_idx;
    HRESULT hr;

    TRACE("iface %p, face %#x, level %u.\n", iface, face, level);

    wined3d_mutex_lock();
    sub_resource_idx = wined3d_texture_get_level_count(texture->wined3d_texture) * face + level;
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, sub_resource_idx)))
        hr = D3DERR_INVALIDCALL;
    else
805 806 807 808
    {
        surface_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DSurface9_UnlockRect(&surface_impl->IDirect3DSurface9_iface);
    }
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT  WINAPI d3d9_texture_cube_AddDirtyRect(IDirect3DCubeTexture9 *iface,
        D3DCUBEMAP_FACES face, const RECT *dirty_rect)
{
    struct d3d9_texture *texture = impl_from_IDirect3DCubeTexture9(iface);
    HRESULT hr;

    TRACE("iface %p, face %#x, dirty_rect %s.\n",
            iface, face, wine_dbgstr_rect(dirty_rect));

    wined3d_mutex_lock();
    if (!dirty_rect)
        hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, NULL);
    else
    {
        struct wined3d_box dirty_region;

        dirty_region.left = dirty_rect->left;
        dirty_region.top = dirty_rect->top;
        dirty_region.right = dirty_rect->right;
        dirty_region.bottom = dirty_rect->bottom;
        dirty_region.front = 0;
        dirty_region.back = 1;
        hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, face, &dirty_region);
    }
    wined3d_mutex_unlock();

    return hr;
}

static const IDirect3DCubeTexture9Vtbl d3d9_texture_cube_vtbl =
{
    /* IUnknown */
    d3d9_texture_cube_QueryInterface,
    d3d9_texture_cube_AddRef,
    d3d9_texture_cube_Release,
    /* IDirect3DResource9 */
    d3d9_texture_cube_GetDevice,
    d3d9_texture_cube_SetPrivateData,
    d3d9_texture_cube_GetPrivateData,
    d3d9_texture_cube_FreePrivateData,
    d3d9_texture_cube_SetPriority,
    d3d9_texture_cube_GetPriority,
    d3d9_texture_cube_PreLoad,
    d3d9_texture_cube_GetType,
    /* IDirect3DBaseTexture9 */
    d3d9_texture_cube_SetLOD,
    d3d9_texture_cube_GetLOD,
    d3d9_texture_cube_GetLevelCount,
    d3d9_texture_cube_SetAutoGenFilterType,
    d3d9_texture_cube_GetAutoGenFilterType,
    d3d9_texture_cube_GenerateMipSubLevels,
    /* IDirect3DCubeTexture9 */
    d3d9_texture_cube_GetLevelDesc,
    d3d9_texture_cube_GetCubeMapSurface,
    d3d9_texture_cube_LockRect,
    d3d9_texture_cube_UnlockRect,
    d3d9_texture_cube_AddDirtyRect,
};

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
static HRESULT WINAPI d3d9_texture_3d_QueryInterface(IDirect3DVolumeTexture9 *iface, REFIID riid, void **out)
{
    TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);

    if (IsEqualGUID(riid, &IID_IDirect3DVolumeTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
            || IsEqualGUID(riid, &IID_IDirect3DResource9)
            || IsEqualGUID(riid, &IID_IUnknown))
    {
        IDirect3DVolumeTexture9_AddRef(iface);
        *out = iface;
        return S_OK;
    }

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

    *out = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI d3d9_texture_3d_AddRef(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
896
    ULONG ref = InterlockedIncrement(&texture->resource.refcount);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

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

    if (ref == 1)
    {
        IDirect3DDevice9Ex_AddRef(texture->parent_device);
        wined3d_mutex_lock();
        wined3d_texture_incref(texture->wined3d_texture);
        wined3d_mutex_unlock();
    }

    return ref;
}

static ULONG WINAPI d3d9_texture_3d_Release(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
914
    ULONG ref = InterlockedDecrement(&texture->resource.refcount);
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

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

    if (!ref)
    {
        IDirect3DDevice9Ex *parent_device = texture->parent_device;

        wined3d_mutex_lock();
        wined3d_texture_decref(texture->wined3d_texture);
        wined3d_mutex_unlock();

        /* Release the device last, as it may cause the device to be destroyed. */
        IDirect3DDevice9Ex_Release(parent_device);
    }
    return ref;
}

static HRESULT WINAPI d3d9_texture_3d_GetDevice(IDirect3DVolumeTexture9 *iface, IDirect3DDevice9 **device)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);

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

    *device = (IDirect3DDevice9 *)texture->parent_device;
    IDirect3DDevice9_AddRef(*device);

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

    return D3D_OK;
}

static HRESULT WINAPI d3d9_texture_3d_SetPrivateData(IDirect3DVolumeTexture9 *iface,
        REFGUID guid, const void *data, DWORD data_size, DWORD flags)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
            iface, debugstr_guid(guid), data, data_size, flags);

953
    return d3d9_resource_set_private_data(&texture->resource, guid, data, data_size, flags);
954 955 956 957 958 959 960 961 962
}

static HRESULT WINAPI d3d9_texture_3d_GetPrivateData(IDirect3DVolumeTexture9 *iface,
        REFGUID guid, void *data, DWORD *data_size)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    TRACE("iface %p, guid %s, data %p, data_size %p.\n",
            iface, debugstr_guid(guid), data, data_size);

963
    return d3d9_resource_get_private_data(&texture->resource, guid, data, data_size);
964 965 966 967 968 969 970
}

static HRESULT WINAPI d3d9_texture_3d_FreePrivateData(IDirect3DVolumeTexture9 *iface, REFGUID guid)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));

971
    return d3d9_resource_free_private_data(&texture->resource, guid);
972 973 974 975 976
}

static DWORD WINAPI d3d9_texture_3d_SetPriority(IDirect3DVolumeTexture9 *iface, DWORD priority)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
977
    struct wined3d_resource *resource;
978 979 980 981 982
    DWORD ret;

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

    wined3d_mutex_lock();
983 984
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_set_priority(resource, priority);
985 986 987 988 989 990 991 992
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_3d_GetPriority(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
993
    const struct wined3d_resource *resource;
994 995 996 997 998
    DWORD ret;

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

    wined3d_mutex_lock();
999 1000
    resource = wined3d_texture_get_resource(texture->wined3d_texture);
    ret = wined3d_resource_get_priority(resource);
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    wined3d_mutex_unlock();

    return ret;
}

static void WINAPI d3d9_texture_3d_PreLoad(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);

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

    wined3d_mutex_lock();
    wined3d_texture_preload(texture->wined3d_texture);
    wined3d_mutex_unlock();
}

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

    return D3DRTYPE_VOLUMETEXTURE;
}

static DWORD WINAPI d3d9_texture_3d_SetLOD(IDirect3DVolumeTexture9 *iface, DWORD lod)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_set_lod(texture->wined3d_texture, lod);
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_3d_GetLOD(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_get_lod(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return ret;
}

static DWORD WINAPI d3d9_texture_3d_GetLevelCount(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    DWORD ret;

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

    wined3d_mutex_lock();
    ret = wined3d_texture_get_level_count(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return ret;
}

static HRESULT WINAPI d3d9_texture_3d_SetAutoGenFilterType(IDirect3DVolumeTexture9 *iface,
        D3DTEXTUREFILTERTYPE filter_type)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    HRESULT hr;

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

    wined3d_mutex_lock();
    hr = wined3d_texture_set_autogen_filter_type(texture->wined3d_texture,
            (enum wined3d_texture_filter_type)filter_type);
    wined3d_mutex_unlock();

    return hr;
}

static D3DTEXTUREFILTERTYPE WINAPI d3d9_texture_3d_GetAutoGenFilterType(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    D3DTEXTUREFILTERTYPE filter_type;

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

    wined3d_mutex_lock();
    filter_type = (D3DTEXTUREFILTERTYPE)wined3d_texture_get_autogen_filter_type(texture->wined3d_texture);
    wined3d_mutex_unlock();

    return filter_type;
}

static void WINAPI d3d9_texture_3d_GenerateMipSubLevels(IDirect3DVolumeTexture9 *iface)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);

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

    wined3d_mutex_lock();
    wined3d_texture_generate_mipmaps(texture->wined3d_texture);
    wined3d_mutex_unlock();
}

static HRESULT WINAPI d3d9_texture_3d_GetLevelDesc(IDirect3DVolumeTexture9 *iface, UINT level, D3DVOLUME_DESC *desc)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    struct wined3d_resource *sub_resource;
    HRESULT hr = D3D_OK;

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

    wined3d_mutex_lock();
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
        hr = D3DERR_INVALIDCALL;
    else
    {
        struct wined3d_resource_desc wined3d_desc;

        wined3d_resource_get_desc(sub_resource, &wined3d_desc);
        desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
        desc->Type = wined3d_desc.resource_type;
        desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
        desc->Pool = wined3d_desc.pool;
        desc->Width = wined3d_desc.width;
        desc->Height = wined3d_desc.height;
        desc->Depth = wined3d_desc.depth;
    }
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT WINAPI d3d9_texture_3d_GetVolumeLevel(IDirect3DVolumeTexture9 *iface,
        UINT level, IDirect3DVolume9 **volume)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    struct wined3d_resource *sub_resource;
1141
    struct d3d9_volume *volume_impl;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

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

    wined3d_mutex_lock();
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
    {
        wined3d_mutex_unlock();
        return D3DERR_INVALIDCALL;
    }

1152 1153
    volume_impl = wined3d_resource_get_parent(sub_resource);
    *volume = &volume_impl->IDirect3DVolume9_iface;
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    IDirect3DVolume9_AddRef(*volume);
    wined3d_mutex_unlock();

    return D3D_OK;
}

static HRESULT WINAPI d3d9_texture_3d_LockBox(IDirect3DVolumeTexture9 *iface,
        UINT level, D3DLOCKED_BOX *locked_box, const D3DBOX *box, DWORD flags)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    struct wined3d_resource *sub_resource;
1165
    struct d3d9_volume *volume_impl;
1166 1167 1168 1169 1170 1171 1172 1173 1174
    HRESULT hr;

    TRACE("iface %p, level %u, locked_box %p, box %p, flags %#x.\n",
            iface, level, locked_box, box, flags);

    wined3d_mutex_lock();
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
        hr = D3DERR_INVALIDCALL;
    else
1175 1176 1177 1178
    {
        volume_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DVolume9_LockBox(&volume_impl->IDirect3DVolume9_iface, locked_box, box, flags);
    }
1179 1180 1181 1182 1183 1184 1185 1186 1187
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT WINAPI d3d9_texture_3d_UnlockBox(IDirect3DVolumeTexture9 *iface, UINT level)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    struct wined3d_resource *sub_resource;
1188
    struct d3d9_volume *volume_impl;
1189 1190 1191 1192 1193 1194 1195 1196
    HRESULT hr;

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

    wined3d_mutex_lock();
    if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
        hr = D3DERR_INVALIDCALL;
    else
1197 1198 1199 1200
    {
        volume_impl = wined3d_resource_get_parent(sub_resource);
        hr = IDirect3DVolume9_UnlockBox(&volume_impl->IDirect3DVolume9_iface);
    }
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    wined3d_mutex_unlock();

    return hr;
}

static HRESULT WINAPI d3d9_texture_3d_AddDirtyBox(IDirect3DVolumeTexture9 *iface, const D3DBOX *dirty_box)
{
    struct d3d9_texture *texture = impl_from_IDirect3DVolumeTexture9(iface);
    HRESULT hr;

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

    wined3d_mutex_lock();
    hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, (const struct wined3d_box *)dirty_box);
    wined3d_mutex_unlock();

    return hr;
}


static const IDirect3DVolumeTexture9Vtbl d3d9_texture_3d_vtbl =
{
    /* IUnknown */
    d3d9_texture_3d_QueryInterface,
    d3d9_texture_3d_AddRef,
    d3d9_texture_3d_Release,
    /* IDirect3DResource9 */
    d3d9_texture_3d_GetDevice,
    d3d9_texture_3d_SetPrivateData,
    d3d9_texture_3d_GetPrivateData,
    d3d9_texture_3d_FreePrivateData,
    d3d9_texture_3d_SetPriority,
    d3d9_texture_3d_GetPriority,
    d3d9_texture_3d_PreLoad,
    d3d9_texture_3d_GetType,
    /* IDirect3DBaseTexture9 */
    d3d9_texture_3d_SetLOD,
    d3d9_texture_3d_GetLOD,
    d3d9_texture_3d_GetLevelCount,
    d3d9_texture_3d_SetAutoGenFilterType,
    d3d9_texture_3d_GetAutoGenFilterType,
    d3d9_texture_3d_GenerateMipSubLevels,
    /* IDirect3DVolumeTexture9 */
    d3d9_texture_3d_GetLevelDesc,
    d3d9_texture_3d_GetVolumeLevel,
    d3d9_texture_3d_LockBox,
    d3d9_texture_3d_UnlockBox,
    d3d9_texture_3d_AddDirtyBox,
};

1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
struct d3d9_texture *unsafe_impl_from_IDirect3DBaseTexture9(IDirect3DBaseTexture9 *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_2d_vtbl
            || iface->lpVtbl == (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_cube_vtbl
            || iface->lpVtbl == (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_3d_vtbl);
    return CONTAINING_RECORD(iface, struct d3d9_texture, IDirect3DBaseTexture9_iface);
}

1261 1262
static void STDMETHODCALLTYPE d3d9_texture_wined3d_object_destroyed(void *parent)
{
1263 1264 1265
    struct d3d9_texture *texture = parent;
    d3d9_resource_cleanup(&texture->resource);
    HeapFree(GetProcessHeap(), 0, texture);
1266 1267 1268 1269 1270 1271 1272
}

static const struct wined3d_parent_ops d3d9_texture_wined3d_parent_ops =
{
    d3d9_texture_wined3d_object_destroyed,
};

1273
HRESULT texture_init(struct d3d9_texture *texture, struct d3d9_device *device,
1274 1275
        UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
1276
    struct wined3d_resource_desc desc;
1277
    DWORD surface_flags = 0;
1278
    HRESULT hr;
1279

1280
    texture->IDirect3DBaseTexture9_iface.lpVtbl = (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_2d_vtbl;
1281
    d3d9_resource_init(&texture->resource);
1282
    list_init(&texture->rtv_list);
1283

1284 1285 1286 1287 1288
    desc.resource_type = WINED3D_RTYPE_TEXTURE;
    desc.format = wined3dformat_from_d3dformat(format);
    desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
    desc.multisample_quality = 0;
    desc.usage = usage & WINED3DUSAGE_MASK;
1289
    desc.usage |= WINED3DUSAGE_TEXTURE;
1290 1291 1292 1293 1294 1295
    desc.pool = pool;
    desc.width = width;
    desc.height = height;
    desc.depth = 1;
    desc.size = 0;

1296 1297 1298
    if (pool != D3DPOOL_DEFAULT || (usage & D3DUSAGE_DYNAMIC))
        surface_flags |= WINED3D_SURFACE_MAPPABLE;

1299 1300 1301 1302 1303 1304 1305 1306
    if (!levels)
    {
        if (usage & D3DUSAGE_AUTOGENMIPMAP)
            levels = 1;
        else
            levels = wined3d_log2i(max(width, height)) + 1;
    }

1307
    wined3d_mutex_lock();
1308
    hr = wined3d_texture_create(device->wined3d_device, &desc, levels, surface_flags,
1309
            NULL, texture, &d3d9_texture_wined3d_parent_ops, &texture->wined3d_texture);
1310
    wined3d_mutex_unlock();
1311
    if (FAILED(hr))
1312
    {
1313 1314 1315 1316
        WARN("Failed to create wined3d texture, hr %#x.\n", hr);
        return hr;
    }

1317 1318
    texture->parent_device = &device->IDirect3DDevice9Ex_iface;
    IDirect3DDevice9Ex_AddRef(texture->parent_device);
1319 1320

    return D3D_OK;
1321
}
1322

1323
HRESULT cubetexture_init(struct d3d9_texture *texture, struct d3d9_device *device,
1324 1325
        UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
1326
    struct wined3d_resource_desc desc;
1327
    DWORD surface_flags = 0;
1328 1329 1330
    HRESULT hr;

    texture->IDirect3DBaseTexture9_iface.lpVtbl = (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_cube_vtbl;
1331
    d3d9_resource_init(&texture->resource);
1332
    list_init(&texture->rtv_list);
1333

1334 1335 1336 1337 1338
    desc.resource_type = WINED3D_RTYPE_CUBE_TEXTURE;
    desc.format = wined3dformat_from_d3dformat(format);
    desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
    desc.multisample_quality = 0;
    desc.usage = usage & WINED3DUSAGE_MASK;
1339
    desc.usage |= WINED3DUSAGE_TEXTURE;
1340 1341 1342 1343 1344 1345
    desc.pool = pool;
    desc.width = edge_length;
    desc.height = edge_length;
    desc.depth = 1;
    desc.size = 0;

1346 1347 1348
    if (pool != D3DPOOL_DEFAULT || (usage & D3DUSAGE_DYNAMIC))
        surface_flags |= WINED3D_SURFACE_MAPPABLE;

1349 1350 1351 1352 1353 1354 1355 1356
    if (!levels)
    {
        if (usage & D3DUSAGE_AUTOGENMIPMAP)
            levels = 1;
        else
            levels = wined3d_log2i(edge_length) + 1;
    }

1357
    wined3d_mutex_lock();
1358
    hr = wined3d_texture_create(device->wined3d_device, &desc, levels, surface_flags,
1359
            NULL, texture, &d3d9_texture_wined3d_parent_ops, &texture->wined3d_texture);
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d cube texture, hr %#x.\n", hr);
        return hr;
    }

    texture->parent_device = &device->IDirect3DDevice9Ex_iface;
    IDirect3DDevice9Ex_AddRef(texture->parent_device);

    return D3D_OK;
}
1372

1373
HRESULT volumetexture_init(struct d3d9_texture *texture, struct d3d9_device *device,
1374 1375
        UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
{
1376
    struct wined3d_resource_desc desc;
1377 1378 1379
    HRESULT hr;

    texture->IDirect3DBaseTexture9_iface.lpVtbl = (const IDirect3DBaseTexture9Vtbl *)&d3d9_texture_3d_vtbl;
1380
    d3d9_resource_init(&texture->resource);
1381
    list_init(&texture->rtv_list);
1382

1383 1384 1385 1386 1387
    desc.resource_type = WINED3D_RTYPE_VOLUME_TEXTURE;
    desc.format = wined3dformat_from_d3dformat(format);
    desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
    desc.multisample_quality = 0;
    desc.usage = usage & WINED3DUSAGE_MASK;
1388
    desc.usage |= WINED3DUSAGE_TEXTURE;
1389 1390 1391 1392 1393 1394
    desc.pool = pool;
    desc.width = width;
    desc.height = height;
    desc.depth = depth;
    desc.size = 0;

1395 1396 1397 1398 1399 1400 1401 1402
    if (!levels)
    {
        if (usage & D3DUSAGE_AUTOGENMIPMAP)
            levels = 1;
        else
            levels = wined3d_log2i(max(max(width, height), depth)) + 1;
    }

1403
    wined3d_mutex_lock();
1404
    hr = wined3d_texture_create(device->wined3d_device, &desc, levels, 0,
1405
            NULL, texture, &d3d9_texture_wined3d_parent_ops, &texture->wined3d_texture);
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
    wined3d_mutex_unlock();
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d volume texture, hr %#x.\n", hr);
        return hr;
    }

    texture->parent_device = &device->IDirect3DDevice9Ex_iface;
    IDirect3DDevice9Ex_AddRef(texture->parent_device);

    return D3D_OK;
}