zakarumych / gpu-alloc

Implementation agnostic memory allocator for Vulkan-like APIs
Other
86 stars 18 forks source link

Handle `VkPhysicalDeviceLimits::bufferImageGranularity` #51

Open Kangz opened 3 years ago

Kangz commented 3 years ago

The Vulkan specification has text has VkPhysicalDeviceLimits::bufferImageGranularity which allows drivers to request that buffers and images are put in different memory pages. This is useful at least on Nvidia where I've seen buffer memory be garbled when in the same page as a samplable image.

The Config doesn't have anything looking like this granularity and wgpu-core doesn't seem to handle this limit at all. It should really be handled somewhere otherwise there will be weird bugs.

zakarumych commented 3 years ago

I see how it would be particularly hard for gpu-alloc user to uphold this requirement other than increasing alignment requirement for each memory request to be at least as large as VkPhysicalDeviceLimits::bufferImageGranularity. But this can be not sub optimal as more memory would be wasted on this alignment when not needed.

One way how I see this working is to add two new flags into UsageFlags, which would specify what type of resources are going to be bound to the memory range - linear and non-linear. Then gpu-alloc will add a guarantee that memory ranges allocated with Linear flag never reside in the same "page" as memory ranges allocated with NonLinear flag. And memory ranges allocated with both flags will be aligned by "page" size. If user's intention is to bind both type of resources then upholding granularity requirement is up to them.

kvark commented 3 years ago

For WebGPU the general case of allocation (the permanent GPU memory for resources) would include both buffers and images. You are saying we have to take the granularity into account ourselves? Could this be turned the other way around - the allocation would have flags for BUFFER and IMAGE for us to provide, and then you can internally know when to align the offset?

Kangz commented 3 years ago

I don't have an opinion since Dawn has its own memory allocator. I'm just raising this here because it was quite hard to figure out that this was an issue ^^

zakarumych commented 3 years ago

@kvark I can't see how your suggestion differs from mine. To clarify. If one allocation is used for multiple resources, at least one of which is LINEAR and at least one is NON_LINEAR, then gpu-alloc has no way to uphold the granularity requirement. Caller would have to bind resources at different "pages". But gpu-alloc would be able to keep that allocation on different "page" than any other allocation as both LINEAR and NON_LINEAR flags would be specified. For allocations made to be used for single resource, or many resources with same linearity, one of the flags (NON_LINEAR for non-linear images and LINEAR for anything else) would instruct gpu-alloc to keep that allocation on different "page" than any allocation with another flag set.

kvark commented 3 years ago

Ok, let's see how this plays out.