yshui / index_camera_passthrough

Experimental Valve Index camera passthrough for Linux
MIT License
48 stars 2 forks source link

Failure to allocate image on GTX 1080 #7

Open Mon-Ouie opened 10 months ago

Mon-Ouie commented 10 months ago

For some reason the allocation of yuv_texture in pipeline.rs fails:

Error: a non-validation error occurred

Caused by:
    0: allocating memory for the image failed
    1: finding a suitable memory type failed

The Vulkan wrapper isn't finding the right memory type to create the image. I might be overlooking a flag but it seems like a suitable memory type is present though:

$ vulkaninfo
...
memoryHeaps: count = 3
        memoryHeaps[0]:
                size   = 8589934592 (0x200000000) (8.00 GiB)
                budget = 4746379264 (0x11ae80000) (4.42 GiB)
                usage  = 0 (0x00000000) (0.00 B)
                flags: count = 1
                        MEMORY_HEAP_DEVICE_LOCAL_BIT
        memoryHeaps[1]:
                size   = 25176649728 (0x5dca53000) (23.45 GiB)
                budget = 25176649728 (0x5dca53000) (23.45 GiB)
                usage  = 0 (0x00000000) (0.00 B)
                flags:
                        None
        memoryHeaps[2]:
                size   = 257949696 (0x0f600000) (246.00 MiB)
                budget = 198508544 (0x0bd50000) (189.31 MiB)
                usage  = 59441152 (0x038b0000) (56.69 MiB)
                flags: count = 1
                        MEMORY_HEAP_DEVICE_LOCAL_BIT
memoryTypes: count = 11
        # ... more memory types ...
        memoryTypes[8]:
                heapIndex     = 1
                propertyFlags = 0x0006: count = 2
                        MEMORY_PROPERTY_HOST_VISIBLE_BIT
                        MEMORY_PROPERTY_HOST_COHERENT_BIT
                usable for:
                        IMAGE_TILING_OPTIMAL:
                                None
                        IMAGE_TILING_LINEAR:
                                color images
                                (non-sparse, non-transient)
        memoryTypes[9]:
                heapIndex     = 1
                propertyFlags = 0x000e: count = 3
                        MEMORY_PROPERTY_HOST_VISIBLE_BIT
                        MEMORY_PROPERTY_HOST_COHERENT_BIT
                        MEMORY_PROPERTY_HOST_CACHED_BIT
                usable for:
                        IMAGE_TILING_OPTIMAL:
                                None
                        IMAGE_TILING_LINEAR:
                                color images
                                (non-sparse, non-transient)
        memoryTypes[10]:
                heapIndex     = 2
                propertyFlags = 0x0007: count = 3
                        MEMORY_PROPERTY_DEVICE_LOCAL_BIT
                        MEMORY_PROPERTY_HOST_VISIBLE_BIT
                        MEMORY_PROPERTY_HOST_COHERENT_BIT
                usable for:
                        IMAGE_TILING_OPTIMAL:
                                None
                        IMAGE_TILING_LINEAR:
                                color images
                                (non-sparse, non-transient)

The overlay does work if I just change the memory type filter for this one allocation to force the allocation to run:

diff --git a/src/pipeline.rs b/src/pipeline.rs
index c632b4b..b6b17bd 100644
--- a/src/pipeline.rs
+++ b/src/pipeline.rs
@@ -226,8 +226,7 @@ impl Pipeline {
                 ..Default::default()
             },
             AllocationCreateInfo {
-                memory_type_filter: MemoryTypeFilter::HOST_SEQUENTIAL_WRITE
-                    | MemoryTypeFilter::PREFER_DEVICE,
+                memory_type_filter: MemoryTypeFilter::PREFER_DEVICE,
                 ..Default::default()
             },
         )?;

System information for reference:

yshui commented 10 months ago

You might need to look into vulkano's code.

https://github.com/vulkano-rs/vulkano/blob/94f50f18bd25971ea123adb8b5782ad65a8f085c/vulkano/src/memory/allocator/mod.rs#L1110-L1135

Add some logging statement here might be helpful.

Mon-Ouie commented 10 months ago

Okay, the part I was missing is that vkGetImageMemoryRequirements indicates the image must be allocated on either memory type 1 (propertyFlags = 0x0000) or memory type 7 (propertyFlags = 0x0001: MEMORY_PROPERTY_DEVICE_LOCAL_BIT).

yshui commented 10 months ago

hmm, so maybe we need to allocate buffer on host and device, and do buffer to buffer copy?

6661620a commented 9 months ago

I am getting the same error as @Mon-Ouie on Ubuntu 22.04. I tried to make the changes to the code that he suggested, ran cargo clean, cargo build --release but I still receive the error :/ Any ideas how to make it work?

Mon-Ouie commented 9 months ago

hmm, so maybe we need to allocate buffer on host and device, and do buffer to buffer copy?

Probably, or if you want to be fancy and avoid the copy when appropriate memory types are available you can see how AMD recommends people to handle these cases in their VulkanMemoryAllocator library: https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/usage_patterns.html (see "Advanced data uploading") — presumably vulkano allows similar flags to be used