Skip to content

Commit

Permalink
Put resources in residency sets
Browse files Browse the repository at this point in the history
  • Loading branch information
js6i committed Dec 17, 2024
1 parent c20828d commit 0464099
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 5 deletions.
1 change: 1 addition & 0 deletions MoltenVK/MoltenVK/Commands/MVKCommandEncoderState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ - (void)setDepthBoundsTestAMD:(BOOL)enable minDepth:(float)minDepth maxDepth:(fl
auto* dslBind = dsLayout->getBindingAt(dslBindIdx);
if (dslBind->getApplyToStage(stage) && shaderBindingUsage.getBit(dslBindIdx)) {
shouldBindArgBuffToStage = true;
if (getDevice()->hasResidencySet()) continue;
uint32_t elemCnt = dslBind->getDescriptorCount(descSet->getVariableDescriptorCount());
for (uint32_t elemIdx = 0; elemIdx < elemCnt; elemIdx++) {
uint32_t descIdx = dslBind->getDescriptorIndex(elemIdx);
Expand Down
2 changes: 2 additions & 0 deletions MoltenVK/MoltenVK/Commands/MVKMTLBufferAllocation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
void MVKMTLBufferAllocationPool::addMTLBuffer() {
MTLResourceOptions mbOpts = (_mtlStorageMode << MTLResourceStorageModeShift) | MTLResourceCPUCacheModeDefaultCache;
_mtlBuffers.push_back({ [getMTLDevice() newBufferWithLength: _mtlBufferLength options: mbOpts], 0 });
getDevice()->makeResident(_mtlBuffers.back().mtlBuffer);
_nextOffset = 0;
}

Expand Down Expand Up @@ -106,6 +107,7 @@

MVKMTLBufferAllocationPool::~MVKMTLBufferAllocationPool() {
for (uint32_t bufferIndex = 0; bufferIndex < _mtlBuffers.size(); ++bufferIndex) {
getDevice()->removeResidency(_mtlBuffers[bufferIndex].mtlBuffer);
[_mtlBuffers[bufferIndex].mtlBuffer release];
}
_mtlBuffers.clear();
Expand Down
6 changes: 6 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
_mtlBuffer = [_deviceMemory->getMTLHeap() newBufferWithLength: getByteCount()
options: _deviceMemory->getMTLResourceOptions()
offset: _deviceMemoryOffset]; // retained
getDevice()->makeResident(_mtlBuffer);
propagateDebugName();
return _mtlBuffer;
} else {
Expand All @@ -202,6 +203,7 @@

_mtlBufferCache = [getMTLDevice() newBufferWithLength: getByteCount()
options: MTLResourceStorageModeManaged]; // retained
getDevice()->makeResident(_mtlBufferCache);
flushToDevice(_deviceMemoryOffset, _byteCount);
}
#endif
Expand Down Expand Up @@ -268,8 +270,10 @@
void MVKBuffer::detachMemory() {
if (_deviceMemory) { _deviceMemory->removeBuffer(this); }
_deviceMemory = nullptr;
if (_mtlBuffer) getDevice()->removeResidency(_mtlBuffer);
[_mtlBuffer release];
_mtlBuffer = nil;
if (_mtlBufferCache) getDevice()->removeResidency(_mtlBufferCache);
[_mtlBufferCache release];
_mtlBufferCache = nil;
}
Expand Down Expand Up @@ -327,6 +331,7 @@
_mtlTexture = [mtlBuff newTextureWithDescriptor: mtlTexDesc
offset: mtlBuffOffset
bytesPerRow: _mtlBytesPerRow];
getDevice()->makeResident(_mtlTexture);
propagateDebugName();
}
return _mtlTexture;
Expand Down Expand Up @@ -390,6 +395,7 @@

// Potentially called twice, from destroy() and destructor, so ensure everything is nulled out.
void MVKBufferView::detachMemory() {
if (_mtlTexture) getDevice()->removeResidency(_mtlTexture);
[_mtlTexture release];
_mtlTexture = nil;
}
38 changes: 38 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,41 @@ class MVKDevice : public MVKDispatchableVulkanAPIObject {
/** Returns the Metal objects underpinning the Vulkan objects indicated in the pNext chain of pMetalObjectsInfo. */
void getMetalObjects(VkExportMetalObjectsInfoEXT* pMetalObjectsInfo);

#if !MVK_XCODE_16
inline void makeResident(id allocation) {}
#else
inline void makeResident(id<MTLAllocation> allocation) {
@synchronized(_residencySet) {
[_residencySet addAllocation: allocation];
[_residencySet commit];
}
}
#endif

#if !MVK_XCODE_16
inline void removeResidency(id allocation) {}
#else
inline void removeResidency(id<MTLAllocation> allocation) {
@synchronized(_residencySet) {
[_residencySet removeAllocation:allocation];
[_residencySet commit];
}
}
#endif

inline void addResidencySet(id<MTLCommandQueue> queue) {
#if MVK_XCODE_16
if (_residencySet) [queue addResidencySet:_residencySet];
#endif
}

inline bool hasResidencySet() {
#if MVK_XCODE_16
return _residencySet != nil;
#else
return false;
#endif
}

#pragma mark Construction

Expand Down Expand Up @@ -912,6 +947,9 @@ class MVKDevice : public MVKDispatchableVulkanAPIObject {
id<MTLBuffer> _globalVisibilityResultMTLBuffer = nil;
id<MTLSamplerState> _defaultMTLSamplerState = nil;
id<MTLBuffer> _dummyBlitMTLBuffer = nil;
#if MVK_XCODE_16
id<MTLResidencySet> _residencySet = nil;
#endif
uint32_t _globalVisibilityQueryCount = 0;
int _capturePipeFileDesc = -1;
bool _isPerformanceTracking = false;
Expand Down
18 changes: 18 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5113,6 +5113,21 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope

// Create the command queues
void MVKDevice::initQueues(const VkDeviceCreateInfo* pCreateInfo) {
#if MVK_XCODE_16
MTLResidencySetDescriptor *setDescriptor;
setDescriptor = [MTLResidencySetDescriptor new];
setDescriptor.label = @"Primary residency set";
setDescriptor.initialCapacity = 256;

NSError *error;
_residencySet = [_physicalDevice->getMTLDevice() newResidencySetWithDescriptor:setDescriptor
error:&error];
if (error) {
reportMessage(MVK_CONFIG_LOG_LEVEL_ERROR, "Error allocating residency set: %s", error.description.UTF8String);
}
[setDescriptor release];
#endif

auto qFams = _physicalDevice->getQueueFamilies();
uint32_t qrCnt = pCreateInfo->queueCreateInfoCount;
for (uint32_t qrIdx = 0; qrIdx < qrCnt; qrIdx++) {
Expand Down Expand Up @@ -5178,6 +5193,9 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope

for (auto &fences: _barrierFences) for (auto fence: fences) [fence release];

#if MVK_XCODE_16
[_residencySet release];
#endif
[_globalVisibilityResultMTLBuffer release];
[_defaultMTLSamplerState release];
[_dummyBlitMTLBuffer release];
Expand Down
3 changes: 2 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKDeviceMemory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
}
if (!_mtlBuffer) { return false; }
_pMemory = isMemoryHostAccessible() ? _mtlBuffer.contents : nullptr;

getDevice()->makeResident(_mtlBuffer);
propagateDebugName();

return true;
Expand Down Expand Up @@ -425,6 +425,7 @@
auto imgCopies = _imageMemoryBindings;
for (auto& img : imgCopies) { img->bindDeviceMemory(nullptr, 0); }

if (_mtlBuffer) getDevice()->removeResidency(_mtlBuffer);
[_mtlBuffer release];
_mtlBuffer = nil;

Expand Down
12 changes: 8 additions & 4 deletions MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}

[mtlTexDesc release]; // temp release

_image->getDevice()->makeResident(_mtlTexture);
propagateDebugName();
}
return _mtlTexture;
Expand All @@ -101,6 +101,7 @@
}

void MVKImagePlane::releaseMTLTexture() {
if (_mtlTexture) _image->getDevice()->removeResidency(_mtlTexture);
[_mtlTexture release];
_mtlTexture = nil;

Expand Down Expand Up @@ -436,15 +437,15 @@
}
if (!_mtlTexelBuffer) {
return reportError(VK_ERROR_OUT_OF_DEVICE_MEMORY, "Could not create an MTLBuffer for an image that requires a buffer backing store. Images that can be used for atomic accesses must have a texel buffer backing them.");
}
}
getDevice()->makeResident(_mtlTexelBuffer);
_mtlTexelBufferOffset = 0;
_ownsTexelBuffer = true;
}
} else if (usesTexelBuffer && _deviceMemory->_mtlBuffer) {
_mtlTexelBuffer = _deviceMemory->_mtlBuffer;
_mtlTexelBufferOffset = getDeviceMemoryOffset();
}

flushToDevice(getDeviceMemoryOffset(), getByteCount());
return _deviceMemory->addImageMemoryBinding(this);
}
Expand Down Expand Up @@ -541,7 +542,10 @@

MVKImageMemoryBinding::~MVKImageMemoryBinding() {
if (_deviceMemory) { _deviceMemory->removeImageMemoryBinding(this); }
if (_ownsTexelBuffer) { [_mtlTexelBuffer release]; }
if (_ownsTexelBuffer) {
if (_ownsTexelBuffer) _image->getDevice()->removeResidency(_mtlTexelBuffer);
[_mtlTexelBuffer release];
}
}


Expand Down
1 change: 1 addition & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKQueue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@
// Retrieves and initializes the Metal command queue and Xcode GPU capture scopes
void MVKQueue::initMTLCommandQueue() {
_mtlQueue = _queueFamily->getMTLCommandQueue(_index); // not retained (cached in queue family)
_device->addResidencySet(_mtlQueue);

_submissionCaptureScope = new MVKGPUCaptureScope(this);
if (_queueFamily->getIndex() == getMVKConfig().defaultGPUCaptureScopeQueueFamilyIndex &&
Expand Down

0 comments on commit 0464099

Please sign in to comment.