2021-01-28 12:28:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Józef Kucia for CodeWeavers
|
|
|
|
* Copyright 2019 Conor McCarthy for CodeWeavers
|
|
|
|
* Copyright 2021 Philip Rebohle for Valve Corporation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define VKD3D_DBG_CHANNEL VKD3D_DBG_CHANNEL_API
|
|
|
|
|
|
|
|
#include "vkd3d_private.h"
|
|
|
|
|
|
|
|
/* ID3D12Heap */
|
2021-02-04 17:59:10 +00:00
|
|
|
static inline struct d3d12_heap *impl_from_ID3D12Heap(d3d12_heap_iface *iface)
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
return CONTAINING_RECORD(iface, struct d3d12_heap, ID3D12Heap_iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_QueryInterface(d3d12_heap_iface *iface,
|
|
|
|
REFIID iid, void **object)
|
|
|
|
{
|
|
|
|
TRACE("iface %p, iid %s, object %p.\n", iface, debugstr_guid(iid), object);
|
|
|
|
|
|
|
|
if (IsEqualGUID(iid, &IID_ID3D12Heap)
|
|
|
|
|| IsEqualGUID(iid, &IID_ID3D12Heap1)
|
|
|
|
|| IsEqualGUID(iid, &IID_ID3D12Pageable)
|
|
|
|
|| IsEqualGUID(iid, &IID_ID3D12DeviceChild)
|
|
|
|
|| IsEqualGUID(iid, &IID_ID3D12Object)
|
|
|
|
|| IsEqualGUID(iid, &IID_IUnknown))
|
|
|
|
{
|
|
|
|
ID3D12Heap_AddRef(iface);
|
|
|
|
*object = iface;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
|
|
|
|
|
|
|
|
*object = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ULONG STDMETHODCALLTYPE d3d12_heap_AddRef(d3d12_heap_iface *iface)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
ULONG refcount = InterlockedIncrement(&heap->refcount);
|
|
|
|
|
|
|
|
TRACE("%p increasing refcount to %u.\n", heap, refcount);
|
|
|
|
return refcount;
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
static void d3d12_heap_destroy(struct d3d12_heap *heap)
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
|
|
|
TRACE("Destroying heap %p.\n", heap);
|
|
|
|
|
2021-02-04 18:02:18 +00:00
|
|
|
vkd3d_free_memory(heap->device, &heap->device->memory_allocator, &heap->allocation);
|
2021-01-28 12:28:50 +00:00
|
|
|
vkd3d_private_store_destroy(&heap->private_store);
|
|
|
|
d3d12_device_release(heap->device);
|
|
|
|
vkd3d_free(heap);
|
|
|
|
}
|
|
|
|
|
2021-02-25 06:59:33 +00:00
|
|
|
static void d3d12_heap_set_name(struct d3d12_heap *heap, const char *name)
|
|
|
|
{
|
|
|
|
if (!heap->allocation.chunk)
|
2021-07-14 13:35:42 +01:00
|
|
|
vkd3d_set_vk_object_name(heap->device, (uint64_t)heap->allocation.device_allocation.vk_memory,
|
2021-02-25 06:59:33 +00:00
|
|
|
VK_OBJECT_TYPE_DEVICE_MEMORY, name);
|
|
|
|
}
|
|
|
|
|
2021-01-28 12:28:50 +00:00
|
|
|
static ULONG STDMETHODCALLTYPE d3d12_heap_Release(d3d12_heap_iface *iface)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
ULONG refcount = InterlockedDecrement(&heap->refcount);
|
|
|
|
|
|
|
|
TRACE("%p decreasing refcount to %u.\n", heap, refcount);
|
|
|
|
|
|
|
|
if (!refcount)
|
|
|
|
d3d12_heap_destroy(heap);
|
|
|
|
|
|
|
|
return refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_GetPrivateData(d3d12_heap_iface *iface,
|
|
|
|
REFGUID guid, UINT *data_size, void *data)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
|
|
|
|
TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
|
|
|
|
|
|
|
|
return vkd3d_get_private_data(&heap->private_store, guid, data_size, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_SetPrivateData(d3d12_heap_iface *iface,
|
|
|
|
REFGUID guid, UINT data_size, const void *data)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
|
|
|
|
TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
|
|
|
|
|
2021-02-25 06:59:33 +00:00
|
|
|
return vkd3d_set_private_data(&heap->private_store, guid, data_size, data,
|
|
|
|
(vkd3d_set_name_callback) d3d12_heap_set_name, heap);
|
2021-01-28 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_SetPrivateDataInterface(d3d12_heap_iface *iface,
|
|
|
|
REFGUID guid, const IUnknown *data)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
|
|
|
|
TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
|
|
|
|
|
2021-02-25 06:59:33 +00:00
|
|
|
return vkd3d_set_private_data_interface(&heap->private_store, guid, data,
|
|
|
|
(vkd3d_set_name_callback) d3d12_heap_set_name, heap);
|
2021-01-28 12:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_GetDevice(d3d12_heap_iface *iface, REFIID iid, void **device)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
|
|
|
|
TRACE("iface %p, iid %s, device %p.\n", iface, debugstr_guid(iid), device);
|
|
|
|
|
|
|
|
return d3d12_device_query_interface(heap->device, iid, device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static D3D12_HEAP_DESC * STDMETHODCALLTYPE d3d12_heap_GetDesc(d3d12_heap_iface *iface,
|
|
|
|
D3D12_HEAP_DESC *desc)
|
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *heap = impl_from_ID3D12Heap(iface);
|
2021-01-28 12:28:50 +00:00
|
|
|
|
|
|
|
TRACE("iface %p, desc %p.\n", iface, desc);
|
|
|
|
|
|
|
|
*desc = heap->desc;
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE d3d12_heap_GetProtectedResourceSession(d3d12_heap_iface *iface,
|
|
|
|
REFIID iid, void **protected_session)
|
|
|
|
{
|
|
|
|
FIXME("iface %p, iid %s, protected_session %p stub!", iface, debugstr_guid(iid), protected_session);
|
|
|
|
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CONST_VTBL struct ID3D12Heap1Vtbl d3d12_heap_vtbl =
|
|
|
|
{
|
|
|
|
/* IUnknown methods */
|
|
|
|
d3d12_heap_QueryInterface,
|
|
|
|
d3d12_heap_AddRef,
|
|
|
|
d3d12_heap_Release,
|
|
|
|
/* ID3D12Object methods */
|
|
|
|
d3d12_heap_GetPrivateData,
|
|
|
|
d3d12_heap_SetPrivateData,
|
|
|
|
d3d12_heap_SetPrivateDataInterface,
|
2021-02-25 06:59:33 +00:00
|
|
|
(void *)d3d12_object_SetName,
|
2021-01-28 12:28:50 +00:00
|
|
|
/* ID3D12DeviceChild methods */
|
|
|
|
d3d12_heap_GetDevice,
|
|
|
|
/* ID3D12Heap methods */
|
|
|
|
d3d12_heap_GetDesc,
|
|
|
|
/* ID3D12Heap1 methods */
|
|
|
|
d3d12_heap_GetProtectedResourceSession,
|
|
|
|
};
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
static struct d3d12_heap *unsafe_impl_from_ID3D12Heap1(ID3D12Heap1 *iface)
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
|
|
|
if (!iface)
|
|
|
|
return NULL;
|
|
|
|
assert(iface->lpVtbl == &d3d12_heap_vtbl);
|
|
|
|
return impl_from_ID3D12Heap(iface);
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *unsafe_impl_from_ID3D12Heap(ID3D12Heap *iface)
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
|
|
|
return unsafe_impl_from_ID3D12Heap1((ID3D12Heap1 *)iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT validate_heap_desc(const D3D12_HEAP_DESC *desc)
|
|
|
|
{
|
|
|
|
if (!desc->SizeInBytes)
|
|
|
|
{
|
|
|
|
WARN("Invalid size %"PRIu64".\n", desc->SizeInBytes);
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->Alignment != D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT
|
|
|
|
&& desc->Alignment != D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT)
|
|
|
|
{
|
|
|
|
WARN("Invalid alignment %"PRIu64".\n", desc->Alignment);
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->Flags & D3D12_HEAP_FLAG_ALLOW_DISPLAY)
|
|
|
|
{
|
|
|
|
WARN("D3D12_HEAP_FLAG_ALLOW_DISPLAY is only for committed resources.\n");
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
static HRESULT d3d12_heap_init(struct d3d12_heap *heap, struct d3d12_device *device,
|
2021-01-28 12:28:50 +00:00
|
|
|
const D3D12_HEAP_DESC *desc, void* host_address)
|
|
|
|
{
|
|
|
|
struct vkd3d_allocate_heap_memory_info alloc_info;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
memset(heap, 0, sizeof(*heap));
|
|
|
|
heap->ID3D12Heap_iface.lpVtbl = &d3d12_heap_vtbl;
|
|
|
|
heap->refcount = 1;
|
|
|
|
heap->desc = *desc;
|
|
|
|
heap->device = device;
|
|
|
|
|
|
|
|
if (!heap->desc.Properties.CreationNodeMask)
|
|
|
|
heap->desc.Properties.CreationNodeMask = 1;
|
|
|
|
if (!heap->desc.Properties.VisibleNodeMask)
|
|
|
|
heap->desc.Properties.VisibleNodeMask = 1;
|
|
|
|
if (!heap->desc.Alignment)
|
|
|
|
heap->desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
|
|
|
|
|
|
|
|
if (FAILED(hr = validate_heap_desc(&heap->desc)))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
alloc_info.heap_desc = heap->desc;
|
|
|
|
alloc_info.host_ptr = host_address;
|
|
|
|
|
|
|
|
if (FAILED(hr = vkd3d_private_store_init(&heap->private_store)))
|
|
|
|
return hr;
|
|
|
|
|
2021-02-04 18:02:18 +00:00
|
|
|
if (FAILED(hr = vkd3d_allocate_heap_memory(device,
|
2021-01-28 12:28:50 +00:00
|
|
|
&device->memory_allocator, &alloc_info, &heap->allocation)))
|
|
|
|
{
|
|
|
|
vkd3d_private_store_destroy(&heap->private_store);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
d3d12_device_add_ref(heap->device);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
HRESULT d3d12_heap_create(struct d3d12_device *device, const D3D12_HEAP_DESC *desc,
|
|
|
|
void* host_address, struct d3d12_heap **heap)
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
2021-02-04 17:59:10 +00:00
|
|
|
struct d3d12_heap *object;
|
2021-01-28 12:28:50 +00:00
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (!(object = vkd3d_malloc(sizeof(*object))))
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2021-02-04 17:59:10 +00:00
|
|
|
if (FAILED(hr = d3d12_heap_init(object, device, desc, host_address)))
|
2021-01-28 12:28:50 +00:00
|
|
|
{
|
|
|
|
vkd3d_free(object);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Created heap %p.\n", object);
|
|
|
|
|
|
|
|
*heap = object;
|
|
|
|
return S_OK;
|
|
|
|
}
|