Skip to content

Comments

Fix buffer and heap out-of-sync in initExternalMemory()#2667

Merged
billhollings merged 1 commit intoKhronosGroup:mainfrom
utmapp:main
Dec 12, 2025
Merged

Fix buffer and heap out-of-sync in initExternalMemory()#2667
billhollings merged 1 commit intoKhronosGroup:mainfrom
utmapp:main

Conversation

@osy
Copy link
Contributor

@osy osy commented Nov 26, 2025

If a buffer is created before a heap in MVKDeviceMemory, the buffer would
be used in some places (for example MVKDeviceMemory::map and the heap
would be used in other places (for example MVKBuffer::getMTLBuffer()).
This leads to inconsistent memory mappings.

Specifically, there are two cases when this happens:

  1. When vkAllocateMemory is called with an imported buffer.
  2. When vkAllocateMemory expects HANDLE_TYPE_MTLBUFFER to be exported.

In either case, ensureMTLBuffer() gets called before ensureMTLHeap()
which sets _mtlBuffer to a buffer not allocated from the placement heap.

This change fixes this by making sure that if a placement heap is needed,
we will always create it prior to allocating the buffer. In cases where
the heap is not used at all (imported buffer), we will return error if the
user tries to specify a HANDLE_TYPE_MTLHEAP for export.

If a buffer is created before a heap in MVKDeviceMemory, the buffer would
be used in some places (for example `MVKDeviceMemory::map` and the heap
would be used in other places (for example `MVKBuffer::getMTLBuffer()`).
This leads to inconsistent memory mappings.

Specifically, there are two cases when this happens:
1. When `vkAllocateMemory` is called with an imported buffer.
2. When `vkAllocateMemory` expects `HANDLE_TYPE_MTLBUFFER` to be exported.

In either case, `ensureMTLBuffer()` gets called before `ensureMTLHeap()`
which sets `_mtlBuffer` to a buffer not allocated from the placement heap.

This change fixes this by making sure that if a placement heap is needed,
we will always create it prior to allocating the buffer. In cases where
the heap is not used at all (imported buffer), we will return error if the
user tries to specify a `HANDLE_TYPE_MTLHEAP` for export.
@CLAassistant
Copy link

CLAassistant commented Nov 26, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@billhollings billhollings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting this change. While it may fix certain use cases, it also causes regression elsewhere. See my inline comments for more details.


if (_image->_is2DViewOn3DImageCompatible && !dvcMem->ensureMTLHeap()) {
if (_image->_is2DViewOn3DImageCompatible && !dvcMem->getMTLHeap()) {
MVKAssert(0, "Creating a 2D view of a 3D texture currently requires a placement heap, which is not available.");
Copy link
Contributor

@billhollings billhollings Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm...looks like this needs a bit more work for robustness. This update causes some regression.

This CTS test (among others) now triggers on this assertion, where it didn't before:

dEQP-VK.pipeline.monolithic.render_to_image.dedicated_allocation.3d.small.r8g8b8a8_unorm

Screenshot 2025-11-30 at 5 22 50 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that is an existing bug that's now being triggered due to the original check being incorrect:

if (_image->_is2DViewOn3DImageCompatible && !dvcMem->ensureMTLHeap()) {

ensureMTLHeap() will always return true here regardless if a heap exists. This is counter to what the assertion requires.

So there is another underlying bug that this commit exposes. However, I am not familiar with this part of the code to say confidently what it is. Do you think it makes sense if we drop the second commit and I'll file an issue on it instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and dropped the commit and filed #2668

Copy link
Contributor

@billhollings billhollings Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This no longer crashes those CTS tests.

Copy link
Contributor

@billhollings billhollings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. LGTM now.

@billhollings billhollings merged commit d6d49e0 into KhronosGroup:main Dec 12, 2025
9 checks passed
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.

3 participants