Skip to content

Commit 49b97f2

Browse files
authored
Merge pull request #2509 from billhollings/amd-disable-mtlheap
Disable MVK_CONFIG_USE_MTLHEAP for AMD devices by default.
2 parents a3ea65e + f4e9367 commit 49b97f2

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

Docs/MoltenVK_Configuration_Parameters.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -661,17 +661,25 @@ This option is not available unless **MoltenVK** was built with `MVK_USE_METAL_P
661661
---------------------------------------
662662
#### MVK_CONFIG_USE_MTLHEAP
663663

664-
##### Type: Boolean
664+
##### Type: Enumeration
665+
- `0`: Do not use `MTLHeap` for allocating resources.
666+
- `1`: Use `MTLHeap` for allocating resources, where safe to do so. On AMD GPUs, this is the same as `0`,
667+
due to potential challenges with `MTLHeap` usage on those platforms. On other GPUs this is the same as `2`.
668+
- `2`: Use `MTLHeap` for allocating resources.
669+
665670
##### Default: `1`
666671

667-
Controls whether **MoltenVK** should use `MTLHeaps` for allocating textures and buffers from device memory.
668-
If this setting is enabled, and placement `MTLHeaps` are available on the platform, **MoltenVK** will allocate a
669-
placement `MTLHeap` for each `VkDeviceMemory` instance, and allocate textures and buffers from that placement heap.
670-
If this parameter is disabled, **MoltenVK** will allocate textures and buffers from general device memory.
672+
Controls whether **MoltenVK** should use `MTLHeap` for allocating textures and buffers from device memory.
673+
If this setting is active, **MoltenVK** will allocate a placement `MTLHeap` for each `VkDeviceMemory` instance,
674+
and allocate textures and buffers from that placement heap. If this parameter is not active, **MoltenVK** will
675+
allocate textures and buffers from general device memory.
671676

672-
Vulkan extension `VK_EXT_image_2d_view_of_3d` requires this parameter to be enabled,
677+
Vulkan extension `VK_EXT_image_2d_view_of_3d` requires this parameter to be active,
673678
to allow aliasing of texture memory between the 3D image and the 2D view.
674679

680+
To force `MTLHeap` to be used on AMD GPUs, set this parameter to `2`.
681+
To disable the use of `MTLHeap` on any GPU, set this parameter to `0`.
682+
675683

676684
---------------------------------------
677685
#### MVK_CONFIG_VK_SEMAPHORE_SUPPORT_STYLE

Docs/MoltenVK_Runtime_UserGuide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ In addition to core *Vulkan* functionality, **MoltenVK** also supports the foll
253253
- `VK_KHR_get_surface_capabilities2`
254254
- `VK_KHR_imageless_framebuffer`
255255
- `VK_EXT_image_2d_view_of_3d`
256-
- *Requires MVK_CONFIG_USE_MTLHEAP to be enabled.*
256+
- *Requires `MVK_CONFIG_USE_MTLHEAP` to be active.*
257257
- `VK_KHR_image_format_list`
258258
- `VK_KHR_incremental_present`
259259
- `VK_KHR_index_type_uint8`

Docs/Whats_New.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Released 2025-04-27
3838
- `VK_EXT_pipeline_robustness`
3939
- `VK_EXT_tooling_info`
4040
- Add support for `B5G6R5_UNORM_PACK16` `B5G5R5A1_UNORM_PACK16`, and `B8G8R8A8` formats using swizzle.
41-
- Enable `MVK_CONFIG_USE_MTLHEAP` by default to support `VK_EXT_image_2d_view_of_3d`.
41+
- Convert `MVK_CONFIG_USE_MTLHEAP` to enumeration, and set active by default to support
42+
`VK_EXT_image_2d_view_of_3d`, except on _AMD_ devices.
4243
- Remove `glslang` as dependency library.
4344
- `MoltenVKShaderConverter` no longer accepts GLSL` shaders as input.
4445
- Fixes to managing descriptor set allocation in a Metal argument buffer.

MoltenVK/MoltenVK/API/mvk_private_api.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ typedef enum MVKConfigActivityPerformanceLoggingStyle {
185185
MVK_CONFIG_ACTIVITY_PERFORMANCE_LOGGING_STYLE_MAX_ENUM = 0x7FFFFFFF,
186186
} MVKConfigActivityPerformanceLoggingStyle;
187187

188+
/** Identifies when MTLHeap is used to allocate buffer and image resources. */
189+
typedef enum MVKConfigUseMTLHeap {
190+
MVK_CONFIG_USE_MTLHEAP_NEVER = 0, /**< Do not use MTLHeap for allocating resources. */
191+
MVK_CONFIG_USE_MTLHEAP_WHERE_SAFE = 1, /**< Use MTLHeap for allocating resources, where safe to do so. On AMD GPUs, this is the same as MVK_CONFIG_USE_MTLHEAP_NEVER, due to potential challenges with MTLHeap usage on those platforms. On other GPUs this is the same as MVK_CONFIG_USE_MTLHEAP_ALWAYS. */
192+
MVK_CONFIG_USE_MTLHEAP_ALWAYS = 2, /**< Use MTLHeap for allocating resources. */
193+
MVK_CONFIG_USE_MTLHEAP_MAX_ENUM = 0x7FFFFFFF
194+
} MVKConfigUseMTLHeap;
195+
188196
/**
189197
* MoltenVK configuration. You can retrieve a copy of this structure using the vkGetMoltenVKConfigurationMVK() function.
190198
*
@@ -227,7 +235,7 @@ typedef struct {
227235
VkBool32 texture1DAs2D; /**< MVK_CONFIG_TEXTURE_1D_AS_2D */
228236
VkBool32 preallocateDescriptors; /**< Obsolete, deprecated, and ignored. */
229237
VkBool32 useCommandPooling; /**< MVK_CONFIG_USE_COMMAND_POOLING */
230-
VkBool32 useMTLHeap; /**< MVK_CONFIG_USE_MTLHEAP */
238+
MVKConfigUseMTLHeap useMTLHeap; /**< MVK_CONFIG_USE_MTLHEAP */
231239
MVKConfigActivityPerformanceLoggingStyle activityPerformanceLoggingStyle; /**< MVK_CONFIG_ACTIVITY_PERFORMANCE_LOGGING_STYLE */
232240
uint32_t apiVersionToAdvertise; /**< MVK_CONFIG_API_VERSION_TO_ADVERTISE */
233241
MVKConfigAdvertiseExtensions advertiseExtensions; /**< MVK_CONFIG_ADVERTISE_EXTENSIONS */

MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@
513513
portabilityFeatures->imageViewFormatReinterpretation = true;
514514
portabilityFeatures->imageViewFormatSwizzle = (_metalFeatures.nativeTextureSwizzle ||
515515
getMVKConfig().fullImageViewSwizzle);
516-
portabilityFeatures->imageView2DOn3DImage = getMVKConfig().useMTLHeap;
516+
portabilityFeatures->imageView2DOn3DImage = _metalFeatures.placementHeaps;
517517
portabilityFeatures->multisampleArrayImage = _metalFeatures.multisampleArrayTextures;
518518
portabilityFeatures->mutableComparisonSamplers = _metalFeatures.depthSampleCompare;
519519
portabilityFeatures->pointPolygons = false;
@@ -611,11 +611,9 @@
611611
break;
612612
}
613613
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_2D_VIEW_OF_3D_FEATURES_EXT: {
614-
if (getMVKConfig().useMTLHeap) {
615-
auto* extFeatures = (VkPhysicalDeviceImage2DViewOf3DFeaturesEXT*)next;
616-
extFeatures->image2DViewOf3D = true;
617-
extFeatures->sampler2DViewOf3D = true;
618-
}
614+
auto* extFeatures = (VkPhysicalDeviceImage2DViewOf3DFeaturesEXT*)next;
615+
extFeatures->image2DViewOf3D = _metalFeatures.placementHeaps;
616+
extFeatures->sampler2DViewOf3D = _metalFeatures.placementHeaps;
619617
break;
620618
}
621619
case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_ROBUSTNESS_FEATURES_EXT: {
@@ -2244,6 +2242,12 @@
22442242
break;
22452243
}
22462244

2245+
// AMD support for MTLHeap is buggy.
2246+
auto cfgUseMTLHeap = getMVKConfig().useMTLHeap;
2247+
bool useMTLHeap = (_properties.vendorID == kAMDVendorId
2248+
? cfgUseMTLHeap == MVK_CONFIG_USE_MTLHEAP_ALWAYS
2249+
: cfgUseMTLHeap != MVK_CONFIG_USE_MTLHEAP_NEVER);
2250+
22472251
#if MVK_TVOS
22482252
_metalFeatures.mslVersionEnum = MTLLanguageVersion2_0;
22492253
_metalFeatures.mtlBufferAlignment = 64;
@@ -2282,7 +2286,7 @@
22822286

22832287
if ( mvkOSVersionIsAtLeast(13.0) ) {
22842288
_metalFeatures.mslVersionEnum = MTLLanguageVersion2_2;
2285-
_metalFeatures.placementHeaps = getMVKConfig().useMTLHeap;
2289+
_metalFeatures.placementHeaps = useMTLHeap;
22862290
_metalFeatures.nativeTextureSwizzle = true;
22872291
if (supportsMTLGPUFamily(Apple3)) {
22882292
_metalFeatures.native3DCompressedTextures = true;
@@ -2384,7 +2388,7 @@
23842388

23852389
if ( mvkOSVersionIsAtLeast(13.0) ) {
23862390
_metalFeatures.mslVersionEnum = MTLLanguageVersion2_2;
2387-
_metalFeatures.placementHeaps = getMVKConfig().useMTLHeap;
2391+
_metalFeatures.placementHeaps = useMTLHeap;
23882392
_metalFeatures.nativeTextureSwizzle = true;
23892393

23902394
if (supportsMTLGPUFamily(Apple3)) {
@@ -2495,7 +2499,7 @@
24952499
}
24962500
if (supportsMTLGPUFamily(Mac2)) {
24972501
_metalFeatures.nativeTextureSwizzle = true;
2498-
_metalFeatures.placementHeaps = getMVKConfig().useMTLHeap;
2502+
_metalFeatures.placementHeaps = useMTLHeap;
24992503
_metalFeatures.renderWithoutAttachments = true;
25002504
}
25012505
}
@@ -3623,7 +3627,7 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope
36233627
if (!_metalFeatures.arrayOfTextures || !_metalFeatures.arrayOfSamplers) {
36243628
pWritableExtns->vk_EXT_descriptor_indexing.enabled = false;
36253629
}
3626-
if (!getMVKConfig().useMTLHeap) {
3630+
if (!_metalFeatures.placementHeaps) {
36273631
pWritableExtns->vk_EXT_image_2d_view_of_3d.enabled = false;
36283632
}
36293633

MoltenVK/MoltenVK/Utility/MVKConfigMembers.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ MVK_CONFIG_MEMBER_STRING(autoGPUCaptureOutputFilepath, const char*,
7474
MVK_CONFIG_MEMBER(texture1DAs2D, VkBool32, TEXTURE_1D_AS_2D)
7575
MVK_CONFIG_MEMBER(preallocateDescriptors, VkBool32, PREALLOCATE_DESCRIPTORS) // Deprecated legacy
7676
MVK_CONFIG_MEMBER(useCommandPooling, VkBool32, USE_COMMAND_POOLING)
77-
MVK_CONFIG_MEMBER(useMTLHeap, VkBool32, USE_MTLHEAP)
77+
MVK_CONFIG_MEMBER(useMTLHeap, MVKConfigUseMTLHeap, USE_MTLHEAP)
7878
MVK_CONFIG_MEMBER(apiVersionToAdvertise, uint32_t, API_VERSION_TO_ADVERTISE)
7979
MVK_CONFIG_MEMBER(advertiseExtensions, uint32_t, ADVERTISE_EXTENSIONS)
8080
MVK_CONFIG_MEMBER(resumeLostDevice, VkBool32, RESUME_LOST_DEVICE)

MoltenVK/MoltenVK/Utility/MVKEnvironment.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,12 @@ void mvkSetConfig(MVKConfiguration& dstMVKConfig, const MVKConfiguration& srcMVK
311311
# define MVK_CONFIG_USE_COMMAND_POOLING 1
312312
#endif
313313

314-
/** Use MTLHeaps where possible when allocating MTLBuffers and MTLTextures. Enabled by default. */
314+
/**
315+
* Use MTLHeap when allocating MTLBuffers and MTLTextures.
316+
* Enabled by default where safe to use MTLHeap on the platform.
317+
*/
315318
#ifndef MVK_CONFIG_USE_MTLHEAP
316-
# define MVK_CONFIG_USE_MTLHEAP 1
319+
# define MVK_CONFIG_USE_MTLHEAP MVK_CONFIG_USE_MTLHEAP_WHERE_SAFE
317320
#endif
318321

319322
/** The Vulkan API version to advertise. Defaults to MVK_VULKAN_API_VERSION. */

0 commit comments

Comments
 (0)