diff --git a/libs/vkd3d/memory.c b/libs/vkd3d/memory.c index 0352fbeb..f54042ff 100644 --- a/libs/vkd3d/memory.c +++ b/libs/vkd3d/memory.c @@ -263,6 +263,17 @@ static HRESULT vkd3d_try_allocate_device_memory(struct d3d12_device *device, return E_OUTOFMEMORY; } +static bool vkd3d_memory_info_type_mask_covers_multiple_memory_heaps( + const struct VkPhysicalDeviceMemoryProperties *props, uint32_t type_mask) +{ + uint32_t heap_mask = 0; + if (!type_mask) + return false; + while (type_mask) + heap_mask |= 1u << props->memoryTypes[vkd3d_bitmask_iter32(&type_mask)].heapIndex; + return !!(heap_mask & (heap_mask - 1u)); +} + HRESULT vkd3d_allocate_device_memory(struct d3d12_device *device, VkDeviceSize size, VkMemoryPropertyFlags type_flags, uint32_t type_mask, void *pNext, struct vkd3d_device_memory_allocation *allocation) @@ -275,9 +286,24 @@ HRESULT vkd3d_allocate_device_memory(struct d3d12_device *device, if (FAILED(hr) && (type_flags & optional_flags)) { - WARN("Memory allocation failed, falling back to system memory.\n"); - hr = vkd3d_try_allocate_device_memory(device, size, - type_flags & ~optional_flags, type_mask, pNext, allocation); + if (vkd3d_memory_info_type_mask_covers_multiple_memory_heaps(&device->memory_properties, type_mask)) + { + WARN("Memory allocation failed, falling back to system memory.\n"); + hr = vkd3d_try_allocate_device_memory(device, size, + type_flags & ~optional_flags, type_mask, pNext, allocation); + } + else if (device->memory_properties.memoryHeapCount > 1) + { + /* It might be the case (NV with RT/DS heap) that we just cannot fall back in any meaningful way. + * E.g. there exists no memory type that is not DEVICE_LOCAL and covers both RT and DS. + * For this case, we have no choice but to not allocate, + * and defer actual memory allocation to CreatePlacedResource() time. */ + WARN("Memory allocation failed, but it is not possible to fallback to system memory here. Deferring allocation.\n"); + return hr; + } + + /* If we fail to allocate, and only have one heap to work with (iGPU), + * falling back is meaningless, just fail. */ } if (FAILED(hr))