From 9065f312d5af60162ffde45605ce8e729d462b72 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Mon, 4 Oct 2021 19:09:59 +0200 Subject: [PATCH] vkd3d: Refactor out validation of CUSTOM heap types. Don't attempt to enter memory allocation when we can invalidate a heap allocation up front. Avoids some dumb edge cases later. Signed-off-by: Hans-Kristian Arntzen --- libs/vkd3d/heap.c | 32 ++++++++++++++++++++++++++++++-- libs/vkd3d/memory.c | 12 +++--------- libs/vkd3d/vkd3d_private.h | 2 ++ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/libs/vkd3d/heap.c b/libs/vkd3d/heap.c index 138bd196..6640ebe7 100644 --- a/libs/vkd3d/heap.c +++ b/libs/vkd3d/heap.c @@ -164,8 +164,33 @@ CONST_VTBL struct ID3D12Heap1Vtbl d3d12_heap_vtbl = d3d12_heap_GetProtectedResourceSession, }; -static HRESULT validate_heap_desc(const D3D12_HEAP_DESC *desc) +HRESULT d3d12_device_validate_custom_heap_type(struct d3d12_device *device, + const D3D12_HEAP_PROPERTIES *heap_properties) { + if (heap_properties->Type != D3D12_HEAP_TYPE_CUSTOM) + return S_OK; + + if (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_UNKNOWN + || (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_L1 + && (is_cpu_accessible_heap(heap_properties) || d3d12_device_is_uma(device, NULL)))) + { + WARN("Invalid memory pool preference.\n"); + return E_INVALIDARG; + } + + if (heap_properties->CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_UNKNOWN) + { + WARN("Must have explicit CPU page property for CUSTOM heap type.\n"); + return E_INVALIDARG; + } + + return S_OK; +} + +static HRESULT validate_heap_desc(struct d3d12_device *device, const D3D12_HEAP_DESC *desc) +{ + HRESULT hr; + if (!desc->SizeInBytes) { WARN("Invalid size %"PRIu64".\n", desc->SizeInBytes); @@ -185,6 +210,9 @@ static HRESULT validate_heap_desc(const D3D12_HEAP_DESC *desc) return E_INVALIDARG; } + if (FAILED(hr = d3d12_device_validate_custom_heap_type(device, &desc->Properties))) + return hr; + return S_OK; } @@ -207,7 +235,7 @@ static HRESULT d3d12_heap_init(struct d3d12_heap *heap, struct d3d12_device *dev if (!heap->desc.Alignment) heap->desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; - if (FAILED(hr = validate_heap_desc(&heap->desc))) + if (FAILED(hr = validate_heap_desc(device, &heap->desc))) return hr; alloc_info.heap_desc = heap->desc; diff --git a/libs/vkd3d/memory.c b/libs/vkd3d/memory.c index 48f5810b..0352fbeb 100644 --- a/libs/vkd3d/memory.c +++ b/libs/vkd3d/memory.c @@ -66,6 +66,7 @@ static uint32_t vkd3d_find_memory_types_with_flags(struct d3d12_device *device, static HRESULT vkd3d_select_memory_flags(struct d3d12_device *device, const D3D12_HEAP_PROPERTIES *heap_properties, VkMemoryPropertyFlags *type_flags) { + HRESULT hr; switch (heap_properties->Type) { case D3D12_HEAP_TYPE_DEFAULT: @@ -85,13 +86,8 @@ static HRESULT vkd3d_select_memory_flags(struct d3d12_device *device, const D3D1 break; case D3D12_HEAP_TYPE_CUSTOM: - if (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_UNKNOWN - || (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_L1 - && (is_cpu_accessible_heap(heap_properties) || d3d12_device_is_uma(device, NULL)))) - { - WARN("Invalid memory pool preference.\n"); - return E_INVALIDARG; - } + if (FAILED(hr = d3d12_device_validate_custom_heap_type(device, heap_properties))) + return hr; switch (heap_properties->CPUPageProperty) { @@ -106,9 +102,7 @@ static HRESULT vkd3d_select_memory_flags(struct d3d12_device *device, const D3D1 case D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE: *type_flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; break; - case D3D12_CPU_PAGE_PROPERTY_UNKNOWN: default: - WARN("Invalid CPU page property.\n"); return E_INVALIDARG; } break; diff --git a/libs/vkd3d/vkd3d_private.h b/libs/vkd3d/vkd3d_private.h index 299879e0..eaf5dad2 100644 --- a/libs/vkd3d/vkd3d_private.h +++ b/libs/vkd3d/vkd3d_private.h @@ -762,6 +762,8 @@ struct d3d12_heap HRESULT d3d12_heap_create(struct d3d12_device *device, const D3D12_HEAP_DESC *desc, void *host_address, struct d3d12_heap **heap); +HRESULT d3d12_device_validate_custom_heap_type(struct d3d12_device *device, + const D3D12_HEAP_PROPERTIES *heap_properties); static inline struct d3d12_heap *impl_from_ID3D12Heap1(ID3D12Heap1 *iface) {