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 <post@arntzen-software.no>
This commit is contained in:
Hans-Kristian Arntzen 2021-10-04 19:09:59 +02:00
parent 9415191111
commit 9065f312d5
3 changed files with 35 additions and 11 deletions

View File

@ -164,8 +164,33 @@ CONST_VTBL struct ID3D12Heap1Vtbl d3d12_heap_vtbl =
d3d12_heap_GetProtectedResourceSession, 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) if (!desc->SizeInBytes)
{ {
WARN("Invalid size %"PRIu64".\n", 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; return E_INVALIDARG;
} }
if (FAILED(hr = d3d12_device_validate_custom_heap_type(device, &desc->Properties)))
return hr;
return S_OK; return S_OK;
} }
@ -207,7 +235,7 @@ static HRESULT d3d12_heap_init(struct d3d12_heap *heap, struct d3d12_device *dev
if (!heap->desc.Alignment) if (!heap->desc.Alignment)
heap->desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_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; return hr;
alloc_info.heap_desc = heap->desc; alloc_info.heap_desc = heap->desc;

View File

@ -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) 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) switch (heap_properties->Type)
{ {
case D3D12_HEAP_TYPE_DEFAULT: case D3D12_HEAP_TYPE_DEFAULT:
@ -85,13 +86,8 @@ static HRESULT vkd3d_select_memory_flags(struct d3d12_device *device, const D3D1
break; break;
case D3D12_HEAP_TYPE_CUSTOM: case D3D12_HEAP_TYPE_CUSTOM:
if (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_UNKNOWN if (FAILED(hr = d3d12_device_validate_custom_heap_type(device, heap_properties)))
|| (heap_properties->MemoryPoolPreference == D3D12_MEMORY_POOL_L1 return hr;
&& (is_cpu_accessible_heap(heap_properties) || d3d12_device_is_uma(device, NULL))))
{
WARN("Invalid memory pool preference.\n");
return E_INVALIDARG;
}
switch (heap_properties->CPUPageProperty) 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: case D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE:
*type_flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; *type_flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
break; break;
case D3D12_CPU_PAGE_PROPERTY_UNKNOWN:
default: default:
WARN("Invalid CPU page property.\n");
return E_INVALIDARG; return E_INVALIDARG;
} }
break; break;

View File

@ -762,6 +762,8 @@ struct d3d12_heap
HRESULT d3d12_heap_create(struct d3d12_device *device, const D3D12_HEAP_DESC *desc, HRESULT d3d12_heap_create(struct d3d12_device *device, const D3D12_HEAP_DESC *desc,
void *host_address, struct d3d12_heap **heap); 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) static inline struct d3d12_heap *impl_from_ID3D12Heap1(ID3D12Heap1 *iface)
{ {