view.c 53.6 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 761 762 763 764 765 766 767 768
struct d3d_depthstencil_view *unsafe_impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d11_depthstencil_view_vtbl);

    return impl_from_ID3D11DepthStencilView(iface);
}

769
struct d3d_depthstencil_view *unsafe_impl_from_ID3D10DepthStencilView(ID3D10DepthStencilView *iface)
770 771 772 773 774 775 776 777
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d10_depthstencil_view_vtbl);

    return impl_from_ID3D10DepthStencilView(iface);
}

778 779 780
/* ID3D11RenderTargetView methods */

static inline struct d3d_rendertarget_view *impl_from_ID3D11RenderTargetView(ID3D11RenderTargetView *iface)
781
{
782
    return CONTAINING_RECORD(iface, struct d3d_rendertarget_view, ID3D11RenderTargetView_iface);
783 784
}

785
static HRESULT STDMETHODCALLTYPE d3d11_rendertarget_view_QueryInterface(ID3D11RenderTargetView *iface,
786 787
        REFIID riid, void **object)
{
788
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
789

790 791 792 793 794
    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)
795 796
            || IsEqualGUID(riid, &IID_IUnknown))
    {
797
        ID3D11RenderTargetView_AddRef(iface);
798 799 800 801
        *object = iface;
        return S_OK;
    }

802 803 804 805 806 807 808 809 810 811
    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));
812 813 814 815 816

    *object = NULL;
    return E_NOINTERFACE;
}

817
static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_AddRef(ID3D11RenderTargetView *iface)
818
{
819 820
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
    ULONG refcount = InterlockedIncrement(&view->refcount);
821

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

    return refcount;
}

827
static ULONG STDMETHODCALLTYPE d3d11_rendertarget_view_Release(ID3D11RenderTargetView *iface)
828
{
829 830
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);
    ULONG refcount = InterlockedDecrement(&view->refcount);
831

832
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
833 834 835

    if (!refcount)
    {
836
        wined3d_mutex_lock();
837
        wined3d_rendertarget_view_decref(view->wined3d_view);
838
        ID3D11Resource_Release(view->resource);
839
        ID3D11Device_Release(view->device);
840
        wined3d_private_store_cleanup(&view->private_store);
841
        wined3d_mutex_unlock();
842
        HeapFree(GetProcessHeap(), 0, view);
843 844 845 846 847
    }

    return refcount;
}

848 849 850
static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDevice(ID3D11RenderTargetView *iface,
        ID3D11Device **device)
{
851 852 853 854 855 856
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *device = view->device;
    ID3D11Device_AddRef(*device);
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 883 884 885 886 887 888 889 890 891
}

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)
{
892 893 894 895 896 897
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *resource = view->resource;
    ID3D11Resource_AddRef(*resource);
898 899 900 901 902
}

static void STDMETHODCALLTYPE d3d11_rendertarget_view_GetDesc(ID3D11RenderTargetView *iface,
        D3D11_RENDER_TARGET_VIEW_DESC *desc)
{
903 904 905 906 907
    struct d3d_rendertarget_view *view = impl_from_ID3D11RenderTargetView(iface);

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

    *desc = view->desc;
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 955 956 957 958 959 960 961 962 963
}

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);
}

964 965 966 967
/* ID3D10DeviceChild methods */

static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDevice(ID3D10RenderTargetView *iface, ID3D10Device **device)
{
968
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
969 970 971

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

972
    ID3D11Device_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
973 974 975 976 977
}

static HRESULT STDMETHODCALLTYPE d3d10_rendertarget_view_GetPrivateData(ID3D10RenderTargetView *iface,
        REFGUID guid, UINT *data_size, void *data)
{
978
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
979 980

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

983
    return d3d_get_private_data(&view->private_store, guid, data_size, data);
984 985 986 987 988
}

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

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

994
    return d3d_set_private_data(&view->private_store, guid, data_size, data);
995 996 997 998 999
}

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

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

1004
    return d3d_set_private_data_interface(&view->private_store, guid, data);
1005 1006 1007 1008 1009 1010 1011
}

/* ID3D10View methods */

static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetResource(ID3D10RenderTargetView *iface,
        ID3D10Resource **resource)
{
1012
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1013 1014 1015

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

1016
    ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1017 1018 1019 1020
}

/* ID3D10RenderTargetView methods */

1021
static void STDMETHODCALLTYPE d3d10_rendertarget_view_GetDesc(ID3D10RenderTargetView *iface,
1022 1023
        D3D10_RENDER_TARGET_VIEW_DESC *desc)
{
1024
    struct d3d_rendertarget_view *view = impl_from_ID3D10RenderTargetView(iface);
1025 1026 1027

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

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

1031
static const struct ID3D10RenderTargetViewVtbl d3d10_rendertarget_view_vtbl =
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
{
    /* 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,
};
1047

1048 1049
static void wined3d_rendertarget_view_desc_from_d3d11(struct wined3d_rendertarget_view_desc *wined3d_desc,
        const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1050 1051 1052 1053 1054
{
    wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format);

    switch (desc->ViewDimension)
    {
1055 1056 1057
        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;
1058 1059
            break;

1060
        case D3D11_RTV_DIMENSION_TEXTURE1D:
1061 1062 1063 1064 1065
            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;

1066
        case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
1067 1068 1069 1070 1071
            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;

1072
        case D3D11_RTV_DIMENSION_TEXTURE2D:
1073 1074 1075 1076 1077
            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;

1078
        case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
1079 1080 1081 1082 1083
            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;

1084
        case D3D11_RTV_DIMENSION_TEXTURE2DMS:
1085 1086 1087 1088 1089
            wined3d_desc->u.texture.level_idx = 0;
            wined3d_desc->u.texture.layer_idx = 0;
            wined3d_desc->u.texture.layer_count = 1;
            break;

1090
        case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
1091 1092 1093 1094 1095
            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;

1096
        case D3D11_RTV_DIMENSION_TEXTURE3D:
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
            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;
    }
}

1111
static HRESULT d3d_rendertarget_view_init(struct d3d_rendertarget_view *view, struct d3d_device *device,
1112
        ID3D11Resource *resource, const D3D11_RENDER_TARGET_VIEW_DESC *desc)
1113
{
1114
    struct wined3d_rendertarget_view_desc wined3d_desc;
1115
    struct wined3d_resource *wined3d_resource;
1116 1117
    HRESULT hr;

1118
    view->ID3D11RenderTargetView_iface.lpVtbl = &d3d11_rendertarget_view_vtbl;
1119
    view->ID3D10RenderTargetView_iface.lpVtbl = &d3d10_rendertarget_view_vtbl;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    view->refcount = 1;

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

1132
    wined3d_mutex_lock();
1133
    if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1134
    {
1135
        wined3d_mutex_unlock();
1136
        ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource);
1137 1138 1139
        return E_FAIL;
    }

1140
    wined3d_rendertarget_view_desc_from_d3d11(&wined3d_desc, &view->desc);
1141
    if (FAILED(hr = wined3d_rendertarget_view_create(&wined3d_desc, wined3d_resource,
1142
            view, &d3d_null_wined3d_parent_ops, &view->wined3d_view)))
1143
    {
1144
        wined3d_mutex_unlock();
1145 1146 1147 1148
        WARN("Failed to create a wined3d rendertarget view, hr %#x.\n", hr);
        return hr;
    }

1149
    wined3d_private_store_init(&view->private_store);
1150
    wined3d_mutex_unlock();
1151
    view->resource = resource;
1152
    ID3D11Resource_AddRef(resource);
1153 1154
    view->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(view->device);
1155

1156
    return S_OK;
1157 1158
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
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;
}

1181 1182 1183 1184 1185 1186 1187 1188 1189
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);
}

1190
struct d3d_rendertarget_view *unsafe_impl_from_ID3D10RenderTargetView(ID3D10RenderTargetView *iface)
1191 1192 1193 1194 1195 1196
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d10_rendertarget_view_vtbl);

    return impl_from_ID3D10RenderTargetView(iface);
1197
}
1198

1199 1200
/* ID3D11ShaderResourceView methods */

1201
static inline struct d3d_shader_resource_view *impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
1202
{
1203
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D11ShaderResourceView_iface);
1204 1205
}

1206
static HRESULT STDMETHODCALLTYPE d3d11_shader_resource_view_QueryInterface(ID3D11ShaderResourceView *iface,
1207 1208
        REFIID riid, void **object)
{
1209 1210
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

1213 1214 1215
    if (IsEqualGUID(riid, &IID_ID3D11ShaderResourceView)
            || IsEqualGUID(riid, &IID_ID3D11View)
            || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
1216 1217
            || IsEqualGUID(riid, &IID_IUnknown))
    {
1218
        ID3D11ShaderResourceView_AddRef(iface);
1219 1220 1221 1222
        *object = iface;
        return S_OK;
    }

1223 1224
    if (IsEqualGUID(riid, &IID_ID3D10ShaderResourceView1)
            || IsEqualGUID(riid, &IID_ID3D10ShaderResourceView)
1225 1226 1227
            || IsEqualGUID(riid, &IID_ID3D10View)
            || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
    {
1228 1229
        ID3D10ShaderResourceView1_AddRef(&view->ID3D10ShaderResourceView1_iface);
        *object = &view->ID3D10ShaderResourceView1_iface;
1230 1231 1232
        return S_OK;
    }

1233 1234 1235 1236 1237 1238
    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));

    *object = NULL;
    return E_NOINTERFACE;
}

1239
static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_AddRef(ID3D11ShaderResourceView *iface)
1240
{
1241 1242
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
    ULONG refcount = InterlockedIncrement(&view->refcount);
1243

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

    return refcount;
}

1249
static ULONG STDMETHODCALLTYPE d3d11_shader_resource_view_Release(ID3D11ShaderResourceView *iface)
1250
{
1251 1252
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);
    ULONG refcount = InterlockedDecrement(&view->refcount);
1253

1254
    TRACE("%p decreasing refcount to %u.\n", view, refcount);
1255 1256 1257

    if (!refcount)
    {
1258
        wined3d_mutex_lock();
1259
        wined3d_shader_resource_view_decref(view->wined3d_view);
1260
        ID3D11Resource_Release(view->resource);
1261
        ID3D11Device_Release(view->device);
1262
        wined3d_private_store_cleanup(&view->private_store);
1263
        wined3d_mutex_unlock();
1264
        HeapFree(GetProcessHeap(), 0, view);
1265 1266 1267 1268 1269
    }

    return refcount;
}

1270 1271 1272
static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDevice(ID3D11ShaderResourceView *iface,
        ID3D11Device **device)
{
1273 1274 1275 1276 1277 1278
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *device = view->device;
    ID3D11Device_AddRef(*device);
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 1305 1306 1307 1308 1309 1310 1311 1312 1313
}

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)
{
1314 1315 1316 1317 1318 1319
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *resource = view->resource;
    ID3D11Resource_AddRef(*resource);
1320 1321 1322 1323 1324
}

static void STDMETHODCALLTYPE d3d11_shader_resource_view_GetDesc(ID3D11ShaderResourceView *iface,
        D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
{
1325 1326 1327 1328 1329
    struct d3d_shader_resource_view *view = impl_from_ID3D11ShaderResourceView(iface);

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

    *desc = view->desc;
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
}

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 */

1351
static inline struct d3d_shader_resource_view *impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView1 *iface)
1352
{
1353
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1354 1355 1356 1357
}

/* IUnknown methods */

1358
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_QueryInterface(ID3D10ShaderResourceView1 *iface,
1359 1360 1361 1362 1363 1364 1365 1366 1367
        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);
}

1368
static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_AddRef(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_AddRef(&view->ID3D11ShaderResourceView_iface);
}

1377
static ULONG STDMETHODCALLTYPE d3d10_shader_resource_view_Release(ID3D10ShaderResourceView1 *iface)
1378 1379 1380 1381 1382 1383 1384 1385
{
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);

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

    return d3d11_shader_resource_view_Release(&view->ID3D11ShaderResourceView_iface);
}

1386 1387
/* ID3D10DeviceChild methods */

1388
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDevice(ID3D10ShaderResourceView1 *iface,
1389 1390
        ID3D10Device **device)
{
1391
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1392 1393 1394

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

1395
    ID3D11Device_QueryInterface(view->device, &IID_ID3D10Device, (void **)device);
1396 1397
}

1398
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_GetPrivateData(ID3D10ShaderResourceView1 *iface,
1399 1400
        REFGUID guid, UINT *data_size, void *data)
{
1401
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1402 1403

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

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

1409
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateData(ID3D10ShaderResourceView1 *iface,
1410 1411
        REFGUID guid, UINT data_size, const void *data)
{
1412
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1413 1414

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

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

1420
static HRESULT STDMETHODCALLTYPE d3d10_shader_resource_view_SetPrivateDataInterface(ID3D10ShaderResourceView1 *iface,
1421 1422
        REFGUID guid, const IUnknown *data)
{
1423
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1424

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

1427
    return d3d_set_private_data_interface(&view->private_store, guid, data);
1428 1429 1430 1431
}

/* ID3D10View methods */

1432
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetResource(ID3D10ShaderResourceView1 *iface,
1433 1434
        ID3D10Resource **resource)
{
1435
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1436 1437 1438

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

1439
    ID3D11Resource_QueryInterface(view->resource, &IID_ID3D10Resource, (void **)resource);
1440 1441 1442 1443
}

/* ID3D10ShaderResourceView methods */

1444
static void STDMETHODCALLTYPE d3d10_shader_resource_view_GetDesc(ID3D10ShaderResourceView1 *iface,
1445 1446
        D3D10_SHADER_RESOURCE_VIEW_DESC *desc)
{
1447
    struct d3d_shader_resource_view *view = impl_from_ID3D10ShaderResourceView(iface);
1448 1449 1450

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

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

1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
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 =
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
{
    /* 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,
1479 1480
    /* ID3D10ShaderResourceView1 methods */
    d3d10_shader_resource_view_GetDesc1,
1481 1482
};

1483
static HRESULT d3d_shader_resource_view_init(struct d3d_shader_resource_view *view, struct d3d_device *device,
1484
        ID3D11Resource *resource, const D3D11_SHADER_RESOURCE_VIEW_DESC *desc)
1485
{
1486
    struct wined3d_resource *wined3d_resource;
1487 1488
    HRESULT hr;

1489
    view->ID3D11ShaderResourceView_iface.lpVtbl = &d3d11_shader_resource_view_vtbl;
1490
    view->ID3D10ShaderResourceView1_iface.lpVtbl = &d3d10_shader_resource_view_vtbl;
1491 1492
    view->refcount = 1;

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    if (!desc)
    {
        if (FAILED(hr = set_srdesc_from_resource(&view->desc, resource)))
            return hr;
    }
    else
    {
        view->desc = *desc;
    }

1503
    wined3d_mutex_lock();
1504
    if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource)))
1505 1506 1507 1508 1509 1510
    {
        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,
1511
            view, &d3d_null_wined3d_parent_ops, &view->wined3d_view)))
1512 1513 1514 1515 1516
    {
        WARN("Failed to create wined3d shader resource view, hr %#x.\n", hr);
        return hr;
    }

1517
    wined3d_private_store_init(&view->private_store);
1518
    wined3d_mutex_unlock();
1519
    view->resource = resource;
1520
    ID3D11Resource_AddRef(resource);
1521 1522
    view->device = &device->ID3D11Device_iface;
    ID3D11Device_AddRef(view->device);
1523

1524 1525
    return S_OK;
}
1526

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
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;
}

1549 1550 1551 1552 1553 1554 1555 1556
struct d3d_shader_resource_view *unsafe_impl_from_ID3D11ShaderResourceView(ID3D11ShaderResourceView *iface)
{
    if (!iface)
        return NULL;
    assert(iface->lpVtbl == &d3d11_shader_resource_view_vtbl);
    return impl_from_ID3D11ShaderResourceView(iface);
}

1557
struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView(ID3D10ShaderResourceView *iface)
1558 1559 1560
{
    if (!iface)
        return NULL;
1561 1562
    assert(iface->lpVtbl == (ID3D10ShaderResourceViewVtbl *)&d3d10_shader_resource_view_vtbl);
    return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface);
1563
}