view.c 53.1 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
/*
 * Copyright 2009 Henri Verbeet for CodeWeavers
 *
 * 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 "wine/port.h"

23
#define NONAMELESSUNION
24
#include "d3d11_private.h"
25

26
WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
27

28
static HRESULT set_dsdesc_from_resource(D3D11_DEPTH_STENCIL_VIEW_DESC *desc, ID3D11Resource *resource)
29
{
30
    D3D11_RESOURCE_DIMENSION dimension;
31

32
    ID3D11Resource_GetType(resource, &dimension);
33

34 35
    desc->Flags = 0;

36 37
    switch (dimension)
    {
38
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
39
        {
40 41
            D3D11_TEXTURE1D_DESC texture_desc;
            ID3D11Texture1D *texture;
42

43
            if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
44
            {
45
                ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
46 47 48
                return E_INVALIDARG;
            }

49 50
            ID3D11Texture1D_GetDesc(texture, &texture_desc);
            ID3D11Texture1D_Release(texture);
51 52 53 54

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
55
                desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1D;
56 57 58 59
                desc->u.Texture1D.MipSlice = 0;
            }
            else
            {
60
                desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE1DARRAY;
61 62 63 64 65 66 67 68
                desc->u.Texture1DArray.MipSlice = 0;
                desc->u.Texture1DArray.FirstArraySlice = 0;
                desc->u.Texture1DArray.ArraySize = 1;
            }

            return S_OK;
        }

69
        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
70
        {
71 72
            D3D11_TEXTURE2D_DESC texture_desc;
            ID3D11Texture2D *texture;
73

74
            if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
75
            {
76
                ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
77 78 79
                return E_INVALIDARG;
            }

80 81
            ID3D11Texture2D_GetDesc(texture, &texture_desc);
            ID3D11Texture2D_Release(texture);
82 83 84 85 86 87

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
88
                    desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
89 90 91 92
                    desc->u.Texture2D.MipSlice = 0;
                }
                else
                {
93
                    desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
94 95 96 97 98 99
                }
            }
            else
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
100
                    desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY;
101 102 103 104 105 106
                    desc->u.Texture2DArray.MipSlice = 0;
                    desc->u.Texture2DArray.FirstArraySlice = 0;
                    desc->u.Texture2DArray.ArraySize = 1;
                }
                else
                {
107
                    desc->ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY;
108 109 110 111 112 113 114 115 116 117
                    desc->u.Texture2DMSArray.FirstArraySlice = 0;
                    desc->u.Texture2DMSArray.ArraySize = 1;
                }
            }

            return S_OK;
        }

        default:
            FIXME("Unhandled resource dimension %#x.\n", dimension);
118 119
        case D3D11_RESOURCE_DIMENSION_BUFFER:
        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
120 121 122 123
            return E_INVALIDARG;
    }
}

124
static HRESULT set_rtdesc_from_resource(D3D11_RENDER_TARGET_VIEW_DESC *desc, ID3D11Resource *resource)
125
{
126
    D3D11_RESOURCE_DIMENSION dimension;
127 128
    HRESULT hr;

129
    ID3D11Resource_GetType(resource, &dimension);
130 131 132

    switch(dimension)
    {
133
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
134
        {
135 136
            ID3D11Texture1D *texture;
            D3D11_TEXTURE1D_DESC texture_desc;
137

138
            hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture);
139 140
            if (FAILED(hr))
            {
141
                ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D?\n");
142 143 144
                return E_INVALIDARG;
            }

145 146
            ID3D11Texture1D_GetDesc(texture, &texture_desc);
            ID3D11Texture1D_Release(texture);
147 148 149 150

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
151
                desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1D;
152 153 154 155
                desc->u.Texture1D.MipSlice = 0;
            }
            else
            {
156
                desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE1DARRAY;
157 158 159 160 161 162 163 164
                desc->u.Texture1DArray.MipSlice = 0;
                desc->u.Texture1DArray.FirstArraySlice = 0;
                desc->u.Texture1DArray.ArraySize = 1;
            }

            return S_OK;
        }

165
        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
166
        {
167 168
            ID3D11Texture2D *texture;
            D3D11_TEXTURE2D_DESC texture_desc;
169

170
            hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture);
171 172
            if (FAILED(hr))
            {
173
                ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D?\n");
174 175 176
                return E_INVALIDARG;
            }

177 178
            ID3D11Texture2D_GetDesc(texture, &texture_desc);
            ID3D11Texture2D_Release(texture);
179 180 181 182 183 184

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
185
                    desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
186 187 188 189
                    desc->u.Texture2D.MipSlice = 0;
                }
                else
                {
190
                    desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS;
191 192 193 194 195 196
                }
            }
            else
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
197
                    desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
198 199 200 201 202 203
                    desc->u.Texture2DArray.MipSlice = 0;
                    desc->u.Texture2DArray.FirstArraySlice = 0;
                    desc->u.Texture2DArray.ArraySize = 1;
                }
                else
                {
204
                    desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY;
205 206 207 208 209 210 211 212
                    desc->u.Texture2DMSArray.FirstArraySlice = 0;
                    desc->u.Texture2DMSArray.ArraySize = 1;
                }
            }

            return S_OK;
        }

213
        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
214
        {
215 216
            ID3D11Texture3D *texture;
            D3D11_TEXTURE3D_DESC texture_desc;
217

218
            hr = ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture);
219 220
            if (FAILED(hr))
            {
221
                ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D?\n");
222 223 224
                return E_INVALIDARG;
            }

225 226
            ID3D11Texture3D_GetDesc(texture, &texture_desc);
            ID3D11Texture3D_Release(texture);
227 228

            desc->Format = texture_desc.Format;
229
            desc->ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D;
230 231 232 233 234 235 236 237 238 239 240 241 242
            desc->u.Texture3D.MipSlice = 0;
            desc->u.Texture3D.FirstWSlice = 0;
            desc->u.Texture3D.WSize = 1;

            return S_OK;
        }

        default:
            FIXME("Unhandled resource dimension %#x.\n", dimension);
            return E_INVALIDARG;
    }
}

243
static HRESULT set_srdesc_from_resource(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11Resource *resource)
244
{
245
    D3D11_RESOURCE_DIMENSION dimension;
246

247
    ID3D11Resource_GetType(resource, &dimension);
248 249 250

    switch (dimension)
    {
251
        case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
252
        {
253 254
            D3D11_TEXTURE1D_DESC texture_desc;
            ID3D11Texture1D *texture;
255

256
            if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture1D, (void **)&texture)))
257
            {
258
                ERR("Resource of type TEXTURE1D doesn't implement ID3D11Texture1D.\n");
259 260 261
                return E_INVALIDARG;
            }

262 263
            ID3D11Texture1D_GetDesc(texture, &texture_desc);
            ID3D11Texture1D_Release(texture);
264 265 266 267

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
268
                desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D;
269 270 271 272 273
                desc->u.Texture1D.MostDetailedMip = 0;
                desc->u.Texture1D.MipLevels = texture_desc.MipLevels;
            }
            else
            {
274
                desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY;
275 276 277 278 279 280 281 282 283
                desc->u.Texture1DArray.MostDetailedMip = 0;
                desc->u.Texture1DArray.MipLevels = texture_desc.MipLevels;
                desc->u.Texture1DArray.FirstArraySlice = 0;
                desc->u.Texture1DArray.ArraySize = texture_desc.ArraySize;
            }

            return S_OK;
        }

284
        case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
285
        {
286 287
            D3D11_TEXTURE2D_DESC texture_desc;
            ID3D11Texture2D *texture;
288

289
            if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture)))
290
            {
291
                ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n");
292 293 294
                return E_INVALIDARG;
            }

295 296
            ID3D11Texture2D_GetDesc(texture, &texture_desc);
            ID3D11Texture2D_Release(texture);
297 298 299 300 301 302

            desc->Format = texture_desc.Format;
            if (texture_desc.ArraySize == 1)
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
303
                    desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
304 305 306 307 308
                    desc->u.Texture2D.MostDetailedMip = 0;
                    desc->u.Texture2D.MipLevels = texture_desc.MipLevels;
                }
                else
                {
309
                    desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMS;
310 311 312 313 314 315
                }
            }
            else
            {
                if (texture_desc.SampleDesc.Count == 1)
                {
316
                    desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
317 318 319 320 321 322 323
                    desc->u.Texture2DArray.MostDetailedMip = 0;
                    desc->u.Texture2DArray.MipLevels = texture_desc.MipLevels;
                    desc->u.Texture2DArray.FirstArraySlice = 0;
                    desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize;
                }
                else
                {
324
                    desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY;
325 326 327 328 329 330 331 332
                    desc->u.Texture2DMSArray.FirstArraySlice = 0;
                    desc->u.Texture2DMSArray.ArraySize = texture_desc.ArraySize;
                }
            }

            return S_OK;
        }

333
        case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
334
        {
335 336
            D3D11_TEXTURE3D_DESC texture_desc;
            ID3D11Texture3D *texture;
337

338
            if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture)))
339
            {
340
                ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n");
341 342 343
                return E_INVALIDARG;
            }

344 345
            ID3D11Texture3D_GetDesc(texture, &texture_desc);
            ID3D11Texture3D_Release(texture);
346 347

            desc->Format = texture_desc.Format;
348
            desc->ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D;
349 350 351 352 353 354 355 356
            desc->u.Texture3D.MostDetailedMip = 0;
            desc->u.Texture3D.MipLevels = texture_desc.MipLevels;

            return S_OK;
        }

        default:
            FIXME("Unhandled resource dimension %#x.\n", dimension);
357
        case D3D11_RESOURCE_DIMENSION_BUFFER:
358 359 360 361
            return E_INVALIDARG;
    }
}

362 363 364
/* ID3D11DepthStencilView methods */

static inline struct d3d_depthstencil_view *impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
365
{
366
    return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D11DepthStencilView_iface);
367 368
}

369
static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_QueryInterface(ID3D11DepthStencilView *iface,
370 371
        REFIID riid, void **object)
{
372 373
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

374 375
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

376 377 378
    if (IsEqualGUID(riid, &IID_ID3D11DepthStencilView)
            || IsEqualGUID(riid, &IID_ID3D11View)
            || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
379 380
            || IsEqualGUID(riid, &IID_IUnknown))
    {
381
        ID3D11DepthStencilView_AddRef(iface);
382 383 384 385
        *object = iface;
        return S_OK;
    }

386 387 388 389 390 391 392 393 394
    if (IsEqualGUID(riid, &IID_ID3D10DepthStencilView)
            || IsEqualGUID(riid, &IID_ID3D10View)
            || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
    {
        ID3D10DepthStencilView_AddRef(&view->ID3D10DepthStencilView_iface);
        *object = &view->ID3D10DepthStencilView_iface;
        return S_OK;
    }

395 396 397 398 399 400
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

    *object = NULL;
    return E_NOINTERFACE;
}

401
static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_AddRef(ID3D11DepthStencilView *iface)
402
{
403 404
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
    ULONG refcount = InterlockedIncrement(&view->refcount);
405

406
    TRACE("%p increasing refcount to %u.\n", view, refcount);
407 408 409 410

    return refcount;
}

411
static ULONG STDMETHODCALLTYPE d3d11_depthstencil_view_Release(ID3D11DepthStencilView *iface)
412
{
413 414
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);
    ULONG refcount = InterlockedDecrement(&view->refcount);
415

416
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
417 418 419

    if (!refcount)
    {
420
        wined3d_mutex_lock();
421
        wined3d_rendertarget_view_decref(view->wined3d_view);
422
        ID3D11Resource_Release(view->resource);
423
        ID3D11Device_Release(view->device);
424
        wined3d_private_store_cleanup(&view->private_store);
425
        wined3d_mutex_unlock();
426
        HeapFree(GetProcessHeap(), 0, view);
427 428 429 430 431
    }

    return refcount;
}

432 433 434
static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDevice(ID3D11DepthStencilView *iface,
        ID3D11Device **device)
{
435 436 437 438 439 440
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

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

    *device = view->device;
    ID3D11Device_AddRef(*device);
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 466 467 468 469 470 471 472 473 474 475
}

static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_GetPrivateData(ID3D11DepthStencilView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

    TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_get_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateData(ID3D11DepthStencilView *iface,
        REFGUID guid, UINT data_size, const void *data)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

    TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_set_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_depthstencil_view_SetPrivateDataInterface(ID3D11DepthStencilView *iface,
        REFGUID guid, const IUnknown *data)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

    return d3d_set_private_data_interface(&view->private_store, guid, data);
}

static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetResource(ID3D11DepthStencilView *iface,
        ID3D11Resource **resource)
{
476 477 478 479 480 481
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

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

    *resource = view->resource;
    ID3D11Resource_AddRef(*resource);
482 483 484 485 486
}

static void STDMETHODCALLTYPE d3d11_depthstencil_view_GetDesc(ID3D11DepthStencilView *iface,
        D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
{
487 488 489 490 491
    struct d3d_depthstencil_view *view = impl_from_ID3D11DepthStencilView(iface);

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

    *desc = view->desc;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 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 536 537 538 539 540 541 542 543 544 545 546 547
}

static const struct ID3D11DepthStencilViewVtbl d3d11_depthstencil_view_vtbl =
{
    /* IUnknown methods */
    d3d11_depthstencil_view_QueryInterface,
    d3d11_depthstencil_view_AddRef,
    d3d11_depthstencil_view_Release,
    /* ID3D11DeviceChild methods */
    d3d11_depthstencil_view_GetDevice,
    d3d11_depthstencil_view_GetPrivateData,
    d3d11_depthstencil_view_SetPrivateData,
    d3d11_depthstencil_view_SetPrivateDataInterface,
    /* ID3D11View methods */
    d3d11_depthstencil_view_GetResource,
    /* ID3D11DepthStencilView methods */
    d3d11_depthstencil_view_GetDesc,
};

/* ID3D10DepthStencilView methods */

static inline struct d3d_depthstencil_view *impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
{
    return CONTAINING_RECORD(iface, struct d3d_depthstencil_view, ID3D10DepthStencilView_iface);
}

/* IUnknown methods */

static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_QueryInterface(ID3D10DepthStencilView *iface,
        REFIID riid, void **object)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);

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

    return d3d11_depthstencil_view_QueryInterface(&view->ID3D11DepthStencilView_iface, riid, object);
}

static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_AddRef(ID3D10DepthStencilView *iface)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);

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

    return d3d11_depthstencil_view_AddRef(&view->ID3D11DepthStencilView_iface);
}

static ULONG STDMETHODCALLTYPE d3d10_depthstencil_view_Release(ID3D10DepthStencilView *iface)
{
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);

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

    return d3d11_depthstencil_view_Release(&view->ID3D11DepthStencilView_iface);
}

548 549 550 551
/* ID3D10DeviceChild methods */

static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDevice(ID3D10DepthStencilView *iface, ID3D10Device **device)
{
552
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
553 554 555

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

556
    ID3D11Device_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
557 558 559 560 561
}

static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_GetPrivateData(ID3D10DepthStencilView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
562
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
563 564

    TRACE("iface %p, guid %s, data_size %p, data %p.\n",
565 566
            iface, debugstr_guid(guid), data_size, data);

567
    return d3d_get_private_data(&view->private_store, guid, data_size, data);
568 569 570 571 572
}

static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateData(ID3D10DepthStencilView *iface,
        REFGUID guid, UINT data_size, const void *data)
{
573
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
574 575

    TRACE("iface %p, guid %s, data_size %u, data %p.\n",
576 577
            iface, debugstr_guid(guid), data_size, data);

578
    return d3d_set_private_data(&view->private_store, guid, data_size, data);
579 580 581 582 583
}

static HRESULT STDMETHODCALLTYPE d3d10_depthstencil_view_SetPrivateDataInterface(ID3D10DepthStencilView *iface,
        REFGUID guid, const IUnknown *data)
{
584
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
585

586 587
    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

588
    return d3d_set_private_data_interface(&view->private_store, guid, data);
589 590 591 592 593 594 595
}

/* ID3D10View methods */

static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetResource(ID3D10DepthStencilView *iface,
        ID3D10Resource **resource)
{
596
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
597 598 599

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

600
    ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
601 602 603 604 605 606 607
}

/* ID3D10DepthStencilView methods */

static void STDMETHODCALLTYPE d3d10_depthstencil_view_GetDesc(ID3D10DepthStencilView *iface,
        D3D10_DEPTH_STENCIL_VIEW_DESC *desc)
{
608
    struct d3d_depthstencil_view *view = impl_from_ID3D10DepthStencilView(iface);
609
    const D3D11_DEPTH_STENCIL_VIEW_DESC *d3d11_desc = &view->desc;
610 611 612

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

613 614 615
    desc->Format = d3d11_desc->Format;
    desc->ViewDimension = (D3D10_DSV_DIMENSION)d3d11_desc->ViewDimension;
    memcpy(&desc->u, &d3d11_desc->u, sizeof(desc->u));
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
}

static const struct ID3D10DepthStencilViewVtbl d3d10_depthstencil_view_vtbl =
{
    /* IUnknown methods */
    d3d10_depthstencil_view_QueryInterface,
    d3d10_depthstencil_view_AddRef,
    d3d10_depthstencil_view_Release,
    /* ID3D10DeviceChild methods */
    d3d10_depthstencil_view_GetDevice,
    d3d10_depthstencil_view_GetPrivateData,
    d3d10_depthstencil_view_SetPrivateData,
    d3d10_depthstencil_view_SetPrivateDataInterface,
    /* ID3D10View methods */
    d3d10_depthstencil_view_GetResource,
    /* ID3D10DepthStencilView methods */
    d3d10_depthstencil_view_GetDesc,
};

635 636
static void wined3d_depth_stencil_view_desc_from_d3d11(struct wined3d_rendertarget_view_desc *wined3d_desc,
        const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
637 638 639
{
    wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);

640 641 642
    if (desc->Flags)
        FIXME("Unhandled depth stencil view flags %#x.\n", desc->Flags);

643 644
    switch (desc->ViewDimension)
    {
645
        case D3D11_DSV_DIMENSION_TEXTURE1D:
646 647 648 649 650
            wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

651
        case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
652 653 654 655 656
            wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
            break;

657
        case D3D11_DSV_DIMENSION_TEXTURE2D:
658 659 660 661 662
            wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

663
        case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
664 665 666 667 668
            wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
            break;

669
        case D3D11_DSV_DIMENSION_TEXTURE2DMS:
670 671 672 673 674
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

675
        case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
676 677 678 679 680 681 682 683 684 685 686 687 688 689
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
            break;

        default:
            FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;
    }
}

690
static HRESULT d3d_depthstencil_view_init(struct d3d_depthstencil_view *view, struct d3d_device *device,
691
        ID3D11Resource *resource, const D3D11_DEPTH_STENCIL_VIEW_DESC *desc)
692
{
693 694
    struct wined3d_rendertarget_view_desc wined3d_desc;
    struct wined3d_resource *wined3d_resource;
695 696
    HRESULT hr;

697
    view->ID3D11DepthStencilView_iface.lpVtbl = &d3d11_depthstencil_view_vtbl;
698
    view->ID3D10DepthStencilView_iface.lpVtbl = &d3d10_depthstencil_view_vtbl;
699 700
    view->refcount = 1;

701 702 703 704 705 706 707 708 709 710
    if (!desc)
    {
        if (FAILED(hr = set_dsdesc_from_resource(&view->desc, resource)))
            return hr;
    }
    else
    {
        view->desc = *desc;
    }

711
    wined3d_mutex_lock();
712
    if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
713
    {
714
        wined3d_mutex_unlock();
715
        ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
716 717 718
        return E_FAIL;
    }

719
    wined3d_depth_stencil_view_desc_from_d3d11(&wined3d_desc, &view->desc);
720
    if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
721
            view, &d3d_null_wined3d_parent_ops, &view->wined3d_view)))
722
    {
723
        wined3d_mutex_unlock();
724 725 726 727
        WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
        return hr;
    }

728
    wined3d_private_store_init(&view->private_store);
729
    wined3d_mutex_unlock();
730
    view->resource = resource;
731
    ID3D11Resource_AddRef(resource);
732 733
    view->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(view->device);
734

735 736 737
    return S_OK;
}

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
HRESULT d3d_depthstencil_view_create(struct d3d_device *device, ID3D11Resource *resource,
        const D3D11_DEPTH_STENCIL_VIEW_DESC *desc, struct d3d_depthstencil_view **view)
{
    struct d3d_depthstencil_view *object;
    HRESULT hr;

    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;

    if (FAILED(hr = d3d_depthstencil_view_init(object, device, resource, desc)))
    {
        WARN("Failed to initialize depthstencil view, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created depthstencil view %p.\n", object);
    *view = object;

    return S_OK;
}

760
struct d3d_depthstencil_view *unsafe_impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
761 762 763 764 765 766 767 768
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d10_depthstencil_view_vtbl);

    return impl_from_ID3D10DepthStencilView(iface);
}

769 770 771
/* ID3D11RenderTargetView methods */

static inline struct d3d_rendertarget_view *impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
772
{
773
    return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D11RenderTargetView_iface);
774 775
}

776
static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_QueryInterface(ID3D11RenderTargetView *iface,
777 778
        REFIID riid, void **object)
{
779
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
780

781 782 783 784 785
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

    if (IsEqualGUID(riid, &IID_ID3D11RenderTargetView)
            || IsEqualGUID(riid, &IID_ID3D11View)
            || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
786 787
            || IsEqualGUID(riid, &IID_IUnknown))
    {
788
        ID3D11RenderTargetView_AddRef(iface);
789 790 791 792
        *object = iface;
        return S_OK;
    }

793 794 795 796 797 798 799 800 801 802
    if (IsEqualGUID(riid, &IID_ID3D10RenderTargetView)
            || IsEqualGUID(riid, &IID_ID3D10View)
            || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
    {
        ID3D10RenderTargetView_AddRef(&view->ID3D10RenderTargetView_iface);
        *object = &view->ID3D10RenderTargetView_iface;
        return S_OK;
    }

    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
803 804 805 806 807

    *object = NULL;
    return E_NOINTERFACE;
}

808
static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_AddRef(ID3D11RenderTargetView *iface)
809
{
810 811
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
    ULONG refcount = InterlockedIncrement(&view->refcount);
812

813
    TRACE("%p increasing refcount to %u.\n", view, refcount);
814 815 816 817

    return refcount;
}

818
static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_Release(ID3D11RenderTargetView *iface)
819
{
820 821
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
    ULONG refcount = InterlockedDecrement(&view->refcount);
822

823
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
824 825 826

    if (!refcount)
    {
827
        wined3d_mutex_lock();
828
        wined3d_rendertarget_view_decref(view->wined3d_view);
829
        ID3D11Resource_Release(view->resource);
830
        ID3D11Device_Release(view->device);
831
        wined3d_private_store_cleanup(&view->private_store);
832
        wined3d_mutex_unlock();
833
        HeapFree(GetProcessHeap(), 0, view);
834 835 836 837 838
    }

    return refcount;
}

839 840 841
static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDevice(ID3D11RenderTargetView *iface,
        ID3D11Device **device)
{
842 843 844 845 846 847
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *device = view->device;
    ID3D11Device_AddRef(*device);
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 873 874 875 876 877 878 879 880 881 882
}

static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_GetPrivateData(ID3D11RenderTargetView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

    TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_get_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateData(ID3D11RenderTargetView *iface,
        REFGUID guid, UINT data_size, const void *data)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

    TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_set_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_SetPrivateDataInterface(ID3D11RenderTargetView *iface,
        REFGUID guid, const IUnknown *data)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

    return d3d_set_private_data_interface(&view->private_store, guid, data);
}

static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetResource(ID3D11RenderTargetView *iface,
        ID3D11Resource **resource)
{
883 884 885 886 887 888
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *resource = view->resource;
    ID3D11Resource_AddRef(*resource);
889 890 891 892 893
}

static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDesc(ID3D11RenderTargetView *iface,
        D3D11_RENDER_TARGET_VIEW_DESC *desc)
{
894 895 896 897 898
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *desc = view->desc;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 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 953 954
}

static const struct ID3D11RenderTargetViewVtbl d3d11_rendertarget_view_vtbl =
{
    /* IUnknown methods */
    d3d11_rendertarget_view_QueryInterface,
    d3d11_rendertarget_view_AddRef,
    d3d11_rendertarget_view_Release,
    /* ID3D11DeviceChild methods */
    d3d11_rendertarget_view_GetDevice,
    d3d11_rendertarget_view_GetPrivateData,
    d3d11_rendertarget_view_SetPrivateData,
    d3d11_rendertarget_view_SetPrivateDataInterface,
    /* ID3D11View methods */
    d3d11_rendertarget_view_GetResource,
    /* ID3D11RenderTargetView methods */
    d3d11_rendertarget_view_GetDesc,
};

/* ID3D10RenderTargetView methods */

static inline struct d3d_rendertarget_view *impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
{
    return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D10RenderTargetView_iface);
}

/* IUnknown methods */

static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_QueryInterface(ID3D10RenderTargetView *iface,
        REFIID riid, void **object)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);

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

    return d3d11_rendertarget_view_QueryInterface(&view->ID3D11RenderTargetView_iface, riid, object);
}

static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_AddRef(ID3D10RenderTargetView *iface)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);

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

    return d3d11_rendertarget_view_AddRef(&view->ID3D11RenderTargetView_iface);
}

static ULONG STDMETHODCALLTYPE d3d10_rendertarget_view_Release(ID3D10RenderTargetView *iface)
{
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);

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

    return d3d11_rendertarget_view_Release(&view->ID3D11RenderTargetView_iface);
}

955 956 957 958
/* ID3D10DeviceChild methods */

static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
{
959
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
960 961 962

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

963
    ID3D11Device_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
964 965 966 967 968
}

static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
969
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
970 971

    TRACE("iface %p, guid %s, data_size %p, data %p.\n",
972 973
            iface, debugstr_guid(guid), data_size, data);

974
    return d3d_get_private_data(&view->private_store, guid, data_size, data);
975 976 977 978 979
}

static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateData(ID3D10RenderTargetView *iface,
        REFGUID guid, UINT data_size, const void *data)
{
980
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
981 982

    TRACE("iface %p, guid %s, data_size %u, data %p.\n",
983 984
            iface, debugstr_guid(guid), data_size, data);

985
    return d3d_set_private_data(&view->private_store, guid, data_size, data);
986 987 988 989 990
}

static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_SetPrivateDataInterface(ID3D10RenderTargetView *iface,
        REFGUID guid, const IUnknown *data)
{
991
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
992

993 994
    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

995
    return d3d_set_private_data_interface(&view->private_store, guid, data);
996 997 998 999 1000 1001 1002
}

/* ID3D10View methods */

static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
        ID3D10Resource **resource)
{
1003
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1004 1005 1006

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

1007
    ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1008 1009 1010 1011
}

/* ID3D10RenderTargetView methods */

1012
static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
1013 1014
        D3D10_RENDER_TARGET_VIEW_DESC *desc)
{
1015
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1016 1017 1018

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

1019
    memcpy(desc, &view->desc, sizeof(*desc));
1020 1021
}

1022
static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
{
    /* IUnknown methods */
    d3d10_rendertarget_view_QueryInterface,
    d3d10_rendertarget_view_AddRef,
    d3d10_rendertarget_view_Release,
    /* ID3D10DeviceChild methods */
    d3d10_rendertarget_view_GetDevice,
    d3d10_rendertarget_view_GetPrivateData,
    d3d10_rendertarget_view_SetPrivateData,
    d3d10_rendertarget_view_SetPrivateDataInterface,
    /* ID3D10View methods */
    d3d10_rendertarget_view_GetResource,
    /* ID3D10RenderTargetView methods */
    d3d10_rendertarget_view_GetDesc,
};
1038

1039 1040
static void wined3d_rendertarget_view_desc_from_d3d11(struct wined3d_rendertarget_view_desc *wined3d_desc,
        const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1041 1042 1043 1044 1045
{
    wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);

    switch (desc->ViewDimension)
    {
1046 1047 1048
        case D3D11_RTV_DIMENSION_BUFFER:
            wined3d_desc->u.buffer.start_idx = desc->u.Buffer.u1.FirstElement;
            wined3d_desc->u.buffer.count = desc->u.Buffer.u2.NumElements;
1049 1050
            break;

1051
        case D3D11_RTV_DIMENSION_TEXTURE1D:
1052 1053 1054 1055 1056
            wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

1057
        case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
1058 1059 1060 1061 1062
            wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize;
            break;

1063
        case D3D11_RTV_DIMENSION_TEXTURE2D:
1064 1065 1066 1067 1068
            wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

1069
        case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
1070 1071 1072 1073 1074
            wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize;
            break;

1075
        case D3D11_RTV_DIMENSION_TEXTURE2DMS:
1076 1077 1078 1079 1080
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

1081
        case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
1082 1083 1084 1085 1086
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture2DMSArray.FirstArraySlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture2DMSArray.ArraySize;
            break;

1087
        case D3D11_RTV_DIMENSION_TEXTURE3D:
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
            wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice;
            wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice;
            wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize;
            break;

        default:
            FIXME("Unhandled view dimension %#x.\n", desc->ViewDimension);
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;
    }
}

1102
static HRESULT d3d_rendertarget_view_init(struct d3d_rendertarget_view *view, struct d3d_device *device,
1103
        ID3D11Resource *resource, const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1104
{
1105
    struct wined3d_rendertarget_view_desc wined3d_desc;
1106
    struct wined3d_resource *wined3d_resource;
1107 1108
    HRESULT hr;

1109
    view->ID3D11RenderTargetView_iface.lpVtbl = &d3d11_rendertarget_view_vtbl;
1110
    view->ID3D10RenderTargetView_iface.lpVtbl = &d3d10_rendertarget_view_vtbl;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    view->refcount = 1;

    if (!desc)
    {
        HRESULT hr = set_rtdesc_from_resource(&view->desc, resource);
        if (FAILED(hr)) return hr;
    }
    else
    {
        view->desc = *desc;
    }

1123
    wined3d_mutex_lock();
1124
    if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1125
    {
1126
        wined3d_mutex_unlock();
1127
        ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1128 1129 1130
        return E_FAIL;
    }

1131
    wined3d_rendertarget_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1132
    if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1133
            view, &d3d_null_wined3d_parent_ops, &view->wined3d_view)))
1134
    {
1135
        wined3d_mutex_unlock();
1136 1137 1138 1139
        WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
        return hr;
    }

1140
    wined3d_private_store_init(&view->private_store);
1141
    wined3d_mutex_unlock();
1142
    view->resource = resource;
1143
    ID3D11Resource_AddRef(resource);
1144 1145
    view->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(view->device);
1146

1147
    return S_OK;
1148 1149
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
HRESULT d3d_rendertarget_view_create(struct d3d_device *device, ID3D11Resource *resource,
        const D3D11_RENDER_TARGET_VIEW_DESC *desc, struct d3d_rendertarget_view **view)
{
    struct d3d_rendertarget_view *object;
    HRESULT hr;

    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;

    if (FAILED(hr = d3d_rendertarget_view_init(object, device, resource, desc)))
    {
        WARN("Failed to initialize rendertarget view, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created rendertarget view %p.\n", object);
    *view = object;

    return S_OK;
}

1172 1173 1174 1175 1176 1177 1178 1179 1180
struct d3d_rendertarget_view *unsafe_impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d11_rendertarget_view_vtbl);

    return impl_from_ID3D11RenderTargetView(iface);
}

1181
struct d3d_rendertarget_view *unsafe_impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1182 1183 1184 1185 1186 1187
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d10_rendertarget_view_vtbl);

    return impl_from_ID3D10RenderTargetView(iface);
1188
}
1189

1190 1191
/* ID3D11ShaderResourceView methods */

1192
static inline struct d3d_shader_resource_view *impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
1193
{
1194
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D11ShaderResourceView_iface);
1195 1196
}

1197
static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_QueryInterface(ID3D11ShaderResourceView *iface,
1198 1199
        REFIID riid, void **object)
{
1200 1201
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

1202 1203
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);

1204 1205 1206
    if (IsEqualGUID(riid, &IID_ID3D11ShaderResourceView)
            || IsEqualGUID(riid, &IID_ID3D11View)
            || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1207 1208
            || IsEqualGUID(riid, &IID_IUnknown))
    {
1209
        ID3D11ShaderResourceView_AddRef(iface);
1210 1211 1212 1213
        *object = iface;
        return S_OK;
    }

1214 1215
    if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView1)
            || IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
1216 1217 1218
            || IsEqualGUID(riid, &IID_ID3D10View)
            || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
    {
1219 1220
        ID3D10ShaderResourceView1_AddRef(&view->ID3D10ShaderResourceView1_iface);
        *object = &view->ID3D10ShaderResourceView1_iface;
1221 1222 1223
        return S_OK;
    }

1224 1225 1226 1227 1228 1229
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

    *object = NULL;
    return E_NOINTERFACE;
}

1230
static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_AddRef(ID3D11ShaderResourceView *iface)
1231
{
1232 1233
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
    ULONG refcount = InterlockedIncrement(&view->refcount);
1234

1235
    TRACE("%p increasing refcount to %u.\n", view, refcount);
1236 1237 1238 1239

    return refcount;
}

1240
static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_Release(ID3D11ShaderResourceView *iface)
1241
{
1242 1243
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
    ULONG refcount = InterlockedDecrement(&view->refcount);
1244

1245
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
1246 1247 1248

    if (!refcount)
    {
1249
        wined3d_mutex_lock();
1250
        wined3d_shader_resource_view_decref(view->wined3d_view);
1251
        ID3D11Resource_Release(view->resource);
1252
        ID3D11Device_Release(view->device);
1253
        wined3d_private_store_cleanup(&view->private_store);
1254
        wined3d_mutex_unlock();
1255
        HeapFree(GetProcessHeap(), 0, view);
1256 1257 1258 1259 1260
    }

    return refcount;
}

1261 1262 1263
static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDevice(ID3D11ShaderResourceView *iface,
        ID3D11Device **device)
{
1264 1265 1266 1267 1268 1269
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *device = view->device;
    ID3D11Device_AddRef(*device);
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
}

static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_GetPrivateData(ID3D11ShaderResourceView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

    TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_get_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateData(ID3D11ShaderResourceView *iface,
        REFGUID guid, UINT data_size, const void *data)
{
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

    TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);

    return d3d_set_private_data(&view->private_store, guid, data_size, data);
}

static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_SetPrivateDataInterface(ID3D11ShaderResourceView *iface,
        REFGUID guid, const IUnknown *data)
{
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

    return d3d_set_private_data_interface(&view->private_store, guid, data);
}

static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetResource(ID3D11ShaderResourceView *iface,
        ID3D11Resource **resource)
{
1305 1306 1307 1308 1309 1310
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *resource = view->resource;
    ID3D11Resource_AddRef(*resource);
1311 1312 1313 1314 1315
}

static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDesc(ID3D11ShaderResourceView *iface,
        D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
{
1316 1317 1318 1319 1320
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *desc = view->desc;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
}

static const struct ID3D11ShaderResourceViewVtbl d3d11_shader_resource_view_vtbl =
{
    /* IUnknown methods */
    d3d11_shader_resource_view_QueryInterface,
    d3d11_shader_resource_view_AddRef,
    d3d11_shader_resource_view_Release,
    /* ID3D11DeviceChild methods */
    d3d11_shader_resource_view_GetDevice,
    d3d11_shader_resource_view_GetPrivateData,
    d3d11_shader_resource_view_SetPrivateData,
    d3d11_shader_resource_view_SetPrivateDataInterface,
    /* ID3D11View methods */
    d3d11_shader_resource_view_GetResource,
    /* ID3D11ShaderResourceView methods */
    d3d11_shader_resource_view_GetDesc,
};

/* ID3D10ShaderResourceView methods */

1342
static inline struct d3d_shader_resource_view *impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView1 *iface)
1343
{
1344
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1345 1346 1347 1348
}

/* IUnknown methods */

1349
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView1 *iface,
1350 1351 1352 1353 1354 1355 1356 1357 1358
        REFIID riid, void **object)
{
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);

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

    return d3d11_shader_resource_view_QueryInterface(&view->ID3D11ShaderResourceView_iface, riid, object);
}

1359
static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(ID3D10ShaderResourceView1 *iface)
1360 1361 1362 1363 1364 1365 1366 1367
{
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);

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

    return d3d11_shader_resource_view_AddRef(&view->ID3D11ShaderResourceView_iface);
}

1368
static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView1 *iface)
1369 1370 1371 1372 1373 1374 1375 1376
{
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);

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

    return d3d11_shader_resource_view_Release(&view->ID3D11ShaderResourceView_iface);
}

1377 1378
/* ID3D10DeviceChild methods */

1379
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView1 *iface,
1380 1381
        ID3D10Device **device)
{
1382
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1383 1384 1385

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

1386
    ID3D11Device_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1387 1388
}

1389
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView1 *iface,
1390 1391
        REFGUID guid, UINT *data_size, void *data)
{
1392
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1393 1394

    TRACE("iface %p, guid %s, data_size %p, data %p.\n",
1395 1396
            iface, debugstr_guid(guid), data_size, data);

1397
    return d3d_get_private_data(&view->private_store, guid, data_size, data);
1398 1399
}

1400
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView1 *iface,
1401 1402
        REFGUID guid, UINT data_size, const void *data)
{
1403
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1404 1405

    TRACE("iface %p, guid %s, data_size %u, data %p.\n",
1406 1407
            iface, debugstr_guid(guid), data_size, data);

1408
    return d3d_set_private_data(&view->private_store, guid, data_size, data);
1409 1410
}

1411
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView1 *iface,
1412 1413
        REFGUID guid, const IUnknown *data)
{
1414
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1415

1416 1417
    TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);

1418
    return d3d_set_private_data_interface(&view->private_store, guid, data);
1419 1420 1421 1422
}

/* ID3D10View methods */

1423
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView1 *iface,
1424 1425
        ID3D10Resource **resource)
{
1426
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1427 1428 1429

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

1430
    ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1431 1432 1433 1434
}

/* ID3D10ShaderResourceView methods */

1435
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView1 *iface,
1436 1437
        D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
{
1438
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1439 1440 1441

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

1442
    memcpy(desc, &view->desc, sizeof(*desc));
1443 1444
}

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc1(ID3D10ShaderResourceView1 *iface,
        D3D10_SHADER_RESOURCE_VIEW_DESC1 *desc)
{
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);

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

    memcpy(desc, &view->desc, sizeof(*desc));
}

static const struct ID3D10ShaderResourceView1Vtbl d3d10_shader_resource_view_vtbl =
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
{
    /* IUnknown methods */
    d3d10_shader_resource_view_QueryInterface,
    d3d10_shader_resource_view_AddRef,
    d3d10_shader_resource_view_Release,
    /* ID3D10DeviceChild methods */
    d3d10_shader_resource_view_GetDevice,
    d3d10_shader_resource_view_GetPrivateData,
    d3d10_shader_resource_view_SetPrivateData,
    d3d10_shader_resource_view_SetPrivateDataInterface,
    /* ID3D10View methods */
    d3d10_shader_resource_view_GetResource,
    /* ID3D10ShaderResourceView methods */
    d3d10_shader_resource_view_GetDesc,
1470 1471
    /* ID3D10ShaderResourceView1 methods */
    d3d10_shader_resource_view_GetDesc1,
1472 1473
};

1474
static HRESULT d3d_shader_resource_view_init(struct d3d_shader_resource_view *view, struct d3d_device *device,
1475
        ID3D11Resource *resource, const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
1476
{
1477
    struct wined3d_resource *wined3d_resource;
1478 1479
    HRESULT hr;

1480
    view->ID3D11ShaderResourceView_iface.lpVtbl = &d3d11_shader_resource_view_vtbl;
1481
    view->ID3D10ShaderResourceView1_iface.lpVtbl = &d3d10_shader_resource_view_vtbl;
1482 1483
    view->refcount = 1;

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
    if (!desc)
    {
        if (FAILED(hr = set_srdesc_from_resource(&view->desc, resource)))
            return hr;
    }
    else
    {
        view->desc = *desc;
    }

1494
    wined3d_mutex_lock();
1495
    if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1496 1497 1498 1499 1500 1501
    {
        ERR("Failed to get wined3d resource for d3d10 resource %p.\n", resource);
        return E_FAIL;
    }

    if (FAILED(hr = wined3d_shader_resource_view_create(wined3d_resource,
1502
            view, &d3d_null_wined3d_parent_ops, &view->wined3d_view)))
1503 1504 1505 1506 1507
    {
        WARN("Failed to create wined3d shader resource view, hr %#x.\n", hr);
        return hr;
    }

1508
    wined3d_private_store_init(&view->private_store);
1509
    wined3d_mutex_unlock();
1510
    view->resource = resource;
1511
    ID3D11Resource_AddRef(resource);
1512 1513
    view->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(view->device);
1514

1515 1516
    return S_OK;
}
1517

1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
HRESULT d3d_shader_resource_view_create(struct d3d_device *device, ID3D11Resource *resource,
        const D3D11_SHADER_RESOURCE_VIEW_DESC *desc, struct d3d_shader_resource_view **view)
{
    struct d3d_shader_resource_view *object;
    HRESULT hr;

    if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
        return E_OUTOFMEMORY;

    if (FAILED(hr = d3d_shader_resource_view_init(object, device, resource, desc)))
    {
        WARN("Failed to initialize shader resource view, hr %#x.\n", hr);
        HeapFree(GetProcessHeap(), 0, object);
        return hr;
    }

    TRACE("Created shader resource view %p.\n", object);
    *view = object;

    return S_OK;
}

1540
struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView *iface)
1541 1542 1543
{
    if (!iface)
        return NULL;
1544 1545
    assert(iface->lpVtbl == (ID3D10ShaderResourceViewVtbl *)&d3d10_shader_resource_view_vtbl);
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1546
}