Skip to content

Fix calculation of memory footprint for images. - #53

Open
jimblacklercorp wants to merge 3 commits into
android-graphics:mainfrom
jimblacklercorp:fix-ahb-size-and-clamp-b578430447
Open

jimblacklercorp wants to merge 3 commits into
android-graphics:mainfrom
jimblacklercorp:fix-ahb-size-and-clamp-b578430447

Conversation

@jimblacklercorp

Copy link
Copy Markdown

Fixes over-reporting of bound resource sizes in VkLayer_DeviceMemoryReport when vkGet*MemoryRequirements returns a worst-case upper bound larger than the backing VkDeviceMemory allocation (such as for AHardwareBuffer-backed images).

  • Clamp bound resource sizes to their backing VkDeviceMemory allocation: Clamp suballocation sizes, usage counters (vulkan.mem.app.usage.*), and BIND trace events to allocation.total_size - offset at bind time, and retroactively clamp already-bound suballocations (emitting a balancing DESTROY + clamped BIND event) if the backing allocation size is reported after the bind call.
  • Query fallback resource sizes after vkBind*Memory: Move the fallback vkGetBufferMemoryRequirements and vkGetImageMemoryRequirements calls in vkBindBufferMemory and vkBindImageMemory to run after the downstream bind call succeeds, matching vkBindBufferMemory2 and vkBindImageMemory2.
  • Skip pre-bind vkGetImageMemoryRequirements for AHardwareBuffer images: Avoid calling vkGetImageMemoryRequirements during vkCreateImage when VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID is present (VUID-vkGetImageMemoryRequirements-image-04004), deferring the query until the image is bound to memory.

http://b/578430447

Clamp bound suballocation sizes and BIND trace events to the backing
VkDeviceMemory allocation size both at bind time and when the allocation
size arrives later via VK_EXT_device_memory_report or vkAllocateMemory.
Bindings recorded against a placeholder that extend past the allocation
are unbound and re-bound through the regular bind path once the size is
known, after the CREATE event, so trace consumers see a balancing
DESTROY followed by the clamped BIND.

Stored bindings now always lie within their allocation; assert this when
computing the unbound counter while still clamping in release builds.

BUG=b/578430447
Route vkBindBufferMemory and vkBindImageMemory through the same
RecordBufferBindings / RecordImageBinds helpers as vkBindBufferMemory2
and vkBindImageMemory2, using a single-element bind info after the
downstream bind succeeds. The fallback vkGet*MemoryRequirements query
for resources with an unknown size now runs only after binding, and the
query-after-bind ordering lives in one place per resource type.

BUG=b/578430447
Stop calling vkGetImageMemoryRequirements from vkCreateImage. Images
created with VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
must be bound before their memory requirements are queried
(VUID-vkGetImageMemoryRequirements-image-04004), and drivers otherwise
return a worst-case size. Every bind path already queries an unknown
image size after a successful bind, and nothing reads the size between
creation and binding, so the create-time query is unnecessary.

BUG=b/578430447
@jimblacklercorp
jimblacklercorp force-pushed the fix-ahb-size-and-clamp-b578430447 branch from 6882171 to 119943d Compare October 27, 2026 23:59
@olehkuznetsov
olehkuznetsov self-requested a review October 28, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants