From 372bd56ade17481e6b169154335292fc7e1ce821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zef=20Kucia?= Date: Fri, 2 Feb 2018 15:39:21 +0100 Subject: [PATCH] libs/vkd3d: Add support for device parent. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order to hold a reference to IDXGIAdapter. Signed-off-by: Józef Kucia Signed-off-by: Henri Verbeet Signed-off-by: Alexandre Julliard --- include/vkd3d.h | 2 + libs/vkd3d/device.c | 12 +++++ libs/vkd3d/vkd3d.map | 1 + libs/vkd3d/vkd3d_private.h | 1 + tests/vkd3d_api.c | 89 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+) diff --git a/include/vkd3d.h b/include/vkd3d.h index 8f2c9b52..32e81234 100644 --- a/include/vkd3d.h +++ b/include/vkd3d.h @@ -69,6 +69,7 @@ struct vkd3d_device_create_info const char * const *device_extensions; uint32_t device_extension_count; + IUnknown *parent; LUID adapter_luid; }; @@ -84,6 +85,7 @@ ULONG vkd3d_instance_incref(struct vkd3d_instance *instance); HRESULT vkd3d_create_device(const struct vkd3d_device_create_info *create_info, REFIID iid, void **device); +IUnknown *vkd3d_get_device_parent(ID3D12Device *device); VkDevice vkd3d_get_vk_device(ID3D12Device *device); VkPhysicalDevice vkd3d_get_vk_physical_device(ID3D12Device *device); struct vkd3d_instance *vkd3d_instance_from_device(ID3D12Device *device); diff --git a/libs/vkd3d/device.c b/libs/vkd3d/device.c index 86580146..c59ba879 100644 --- a/libs/vkd3d/device.c +++ b/libs/vkd3d/device.c @@ -1216,6 +1216,8 @@ static ULONG STDMETHODCALLTYPE d3d12_device_Release(ID3D12Device *iface) VK_CALL(vkDestroyPipelineCache(device->vk_device, device->vk_pipeline_cache, NULL)); d3d12_device_destroy_vkd3d_queues(device); VK_CALL(vkDestroyDevice(device->vk_device, NULL)); + if (device->parent) + IUnknown_Release(device->parent); vkd3d_instance_decref(device->vkd3d_instance); vkd3d_free(device); @@ -2174,6 +2176,9 @@ static HRESULT d3d12_device_init(struct d3d12_device *device, d3d12_device_init_pipeline_cache(device); + if ((device->parent = create_info->parent)) + IUnknown_AddRef(device->parent); + return S_OK; } @@ -2199,6 +2204,13 @@ HRESULT d3d12_device_create(struct vkd3d_instance *instance, return S_OK; } +IUnknown *vkd3d_get_device_parent(ID3D12Device *device) +{ + struct d3d12_device *d3d12_device = impl_from_ID3D12Device(device); + + return d3d12_device->parent; +} + VkDevice vkd3d_get_vk_device(ID3D12Device *device) { struct d3d12_device *d3d12_device = impl_from_ID3D12Device(device); diff --git a/libs/vkd3d/vkd3d.map b/libs/vkd3d/vkd3d.map index 246275b9..7d0f8388 100644 --- a/libs/vkd3d/vkd3d.map +++ b/libs/vkd3d/vkd3d.map @@ -6,6 +6,7 @@ global: vkd3d_create_image_resource; vkd3d_create_instance; vkd3d_create_root_signature_deserializer; + vkd3d_get_device_parent; vkd3d_get_vk_device; vkd3d_get_vk_format; vkd3d_get_vk_physical_device; diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index 69130515..acf7824a 100644 --- a/libs/vkd3d/vkd3d_private.h +++ b/libs/vkd3d/vkd3d_private.h @@ -666,6 +666,7 @@ struct d3d12_device vkd3d_create_thread_pfn create_thread; vkd3d_join_thread_pfn join_thread; + IUnknown *parent; LUID adapter_luid; }; diff --git a/tests/vkd3d_api.c b/tests/vkd3d_api.c index fc8d9859..71357377 100644 --- a/tests/vkd3d_api.c +++ b/tests/vkd3d_api.c @@ -24,6 +24,13 @@ #include "vkd3d_test.h" #include +static ULONG get_refcount(void *iface) +{ + IUnknown *unk = iface; + IUnknown_AddRef(unk); + return IUnknown_Release(unk); +} + static ULONG resource_get_internal_refcount(ID3D12Resource *resource) { vkd3d_resource_incref(resource); @@ -561,6 +568,87 @@ static void test_adapter_luid(void) ok(!refcount, "Device has %u references left.\n", refcount); } +struct parent +{ + IUnknown IUnknown_iface; + LONG refcount; +}; + +static struct parent *parent_from_IUnknown(IUnknown *iface) +{ + return CONTAINING_RECORD(iface, struct parent, IUnknown_iface); +} + +static HRESULT STDMETHODCALLTYPE parent_QueryInterface(IUnknown *iface, + REFIID iid, void **object) +{ + if (IsEqualGUID(iid, &IID_IUnknown)) + { + IUnknown_AddRef(iface); + *object = iface; + return S_OK; + } + + ok(false, "Unexpected QueryInterface() call.\n"); + return E_NOINTERFACE; +} + +static ULONG STDMETHODCALLTYPE parent_AddRef(IUnknown *iface) +{ + struct parent *parent = parent_from_IUnknown(iface); + + return InterlockedIncrement(&parent->refcount); +} + +static ULONG STDMETHODCALLTYPE parent_Release(IUnknown *iface) +{ + struct parent *parent = parent_from_IUnknown(iface); + + return InterlockedDecrement(&parent->refcount); +} + +static const struct IUnknownVtbl parent_vtbl = +{ + parent_QueryInterface, + parent_AddRef, + parent_Release, +}; + +static void test_device_parent(void) +{ + struct vkd3d_device_create_info create_info; + struct parent parent_impl; + ID3D12Device *device; + IUnknown *unknown; + IUnknown *parent; + ULONG refcount; + HRESULT hr; + + parent_impl.IUnknown_iface.lpVtbl = &parent_vtbl; + parent_impl.refcount = 1; + parent = &parent_impl.IUnknown_iface; + + refcount = get_refcount(parent); + ok(refcount == 1, "Got unexpected refcount %u.\n", refcount); + + create_info = device_default_create_info; + create_info.parent = parent; + hr = vkd3d_create_device(&create_info, &IID_ID3D12Device, (void **)&device); + ok(hr == S_OK, "Failed to create device, hr %#x.\n", hr); + + refcount = get_refcount(parent); + ok(refcount == 2, "Got unexpected refcount %u.\n", refcount); + + unknown = vkd3d_get_device_parent(device); + ok(unknown == parent, "Got device parent %p, expected %p.\n", unknown, parent); + + refcount = ID3D12Device_Release(device); + ok(!refcount, "Device has %u references left.\n", refcount); + + refcount = get_refcount(parent); + ok(refcount == 1, "Got unexpected refcount %u.\n", refcount); +} + static void test_vkd3d_queue(void) { ID3D12CommandQueue *direct_queue, *compute_queue, *copy_queue; @@ -666,6 +754,7 @@ START_TEST(vkd3d_api) run_test(test_additional_device_extensions); run_test(test_physical_device); run_test(test_adapter_luid); + run_test(test_device_parent); run_test(test_vkd3d_queue); run_test(test_resource_internal_refcount); }