Repository navigation
Add Metal 4 support - #271
Conversation
- CMake: applied Clang warning options to AppleClang - Validation: allowed only "Layout::INPUT_ATTACHMENT" texture barriers inside rendering - Validation: rejected "ResolveOp::AVERAGE" for integer and stencil formats, stencil resolves require "features.resolveOpMinMax", validated sample counts and layer counts of whole-texture resolves - Validation: tracked acceleration structure type, flags and size, validated build sources and destinations, instance counts and updates - Validation: required "tiers.rayTracing >= 3" for micromaps (fixed the "MICROMAP_COMPACTED_SIZE" query check) - Validation: validated vertex attribute count against "shaderStage.vertex.attributeMaxNum" - Validation: "MUTABLE" descriptor ranges require "features.mutableDescriptorType" - Validation: validated swap chain windows and "SWAPCHAIN_SEMAPHORE" fences in "AcquireNextTexture" and "QueuePresent" (header comments updated) - Validation: rejected "ResolveOp::AVERAGE" for "depth" resolves of depth-stencil formats without a separate "stencil" attachment (the stencil plane is resolved with "depth.resolveOp"), documented that depth "AVERAGE" resolves are optional in VK - VK: the CPU time domain for calibrated timestamps is selected from the supported ones, "features.calibratedTimestamps" requires it (MoltenVK exposes only "CLOCK_MONOTONIC_RAW") - VK: enabled portability subset features, "VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT" is set for 3D textures only if "imageView2DOn3DImage" is supported - VK: "CmdResolveTexture" with "maintenance10" sets resolve modes only for the referenced depth-stencil aspects, color resolves use "AVERAGE" or "SAMPLE_ZERO" (as required by the spec), non-integer color "MIN/MAX" is reported as unsupported
- added "GraphicsAPI::METAL", available if "NRI_ENABLE_METAL_SUPPORT = ON" in CMake (macOS 26+, Xcode 26+ with the Metal toolchain, metal-cpp is fetched automatically)
- added optional DXIL support through Metal Shader Converter ("NRI_ENABLE_METAL_SHADER_CONVERTER", enabled by default if installed), native ".metallib" shaders follow the ABI described in "NRI.metal"
- added "DeviceDesc::features::shaderBytecodeMETALLIB" and "ShaderDesc::threadGroupSizeX/Y/Z" (required for native Metal compute, mesh and task shaders)
- added "NRIWrapperMetal.h" and "nriCreateDeviceFromMetalDevice"
- Metal 4 command queues, allocators, argument tables, residency sets and barriers, "MTL4Compiler" pipelines, precompiled internal shaders
- supported: bindless and directly indexed descriptor heaps, root constants and descriptors, mesh shaders, multiview, indirect and count draws, draw parameters emulation, MIN/MAX and region resolves, occlusion and timestamp queries, programmable sample locations, depth bounds (Apple10+), geometry and tessellation emulation through Metal Shader Converter (Apple9+), NIS (with Metal Shader Converter), ImGui extension
- unsupported: pipeline statistics, shading rate, conservative rasterization, logic operations, low latency, video
- Validation: Metal wrappers, native shader thread group sizes (Metal depth resolve limits are reported by the backend)
- "MTL4Archive" pipeline caches ("features.pipelineCache" and "pipelineCacheControl"): misses are compiled once by the device compiler, "GetPipelineCacheData" captures recorded pipelines into a new archive
- pipeline caches also store Metal Shader Converter output
- pre-converted DXIL: ShaderMake Metal converter bundles are accepted by any Metal backend build, checked against the pipeline layout via reflection
- added "WrapperMetalInterface::GetRootSignatureMetal" for offline conversion
- Validation: Metal converter bundle structure, "GetRootSignatureMetal" arguments
- ray tracing with DXIL pipelines through Metal Shader Converter (Apple9+, "tiers.rayTracing = 2"), native shaders can use inline ray tracing - acceleration structures: builds, updates, copies, compaction and size queries, TLAS instance conversion by internal kernels, indirect ray dispatches - acceleration structure descriptors point to "NriAccelerationStructureHeader" (see "NRI.metal") - unsupported: micromaps
- added MetalFX upscalers: "UpscalerType::METALFX_SPATIAL", "METALFX_TEMPORAL" and "METALFX_DENOISED" (created on the first dispatch, encoded directly into the native command buffer) - Validation: MetalFX inputs
- added iOS/iPadOS 26+ support ("CMAKE_SYSTEM_NAME = iOS", device and simulator SDKs, static library by default, no VK, WebGPU, NVTX and NIS)
- Metal Shader Converter on iOS devices ("/usr/local/lib_iOS"), no MetalFX and Metal Shader Converter in the simulator
- shared builds copy the Converter library next to "libNRI" (found via "@loader_path" or the app "Frameworks" folder), "3-PrepareSDK.sh" puts it into "Lib" on macOS
|
The resolve changes need a few fixes before merge. The broader resolve API improvements can be handled separately. This feedback refers to head Required fixes for merge
Follow-up work
These API extensions are not prerequisites for this PR once unsupported combinations are correctly rejected. Answers to the three questions
|
|
Hey, Selim! Thanks for the titanic work. You rock! I would like to merge this PR today, because in 9h I will be on vacation until October 19. Please, either do minimal fixes (and hopefully hook up at least |
|
Not a problem, I asked my buddy to jump in and do some polishing... |
working on it! |
- VK: integer color formats don't report "MULTISAMPLE_RESOLVE" (they can be resolved only with "SAMPLE_ZERO", which is not exposed)
- VK: "CmdResolveTexture" passes the requested color resolve mode instead of substituting "SAMPLE_ZERO" for integer formats
- Validation: VK color resolves require "ResolveOp::AVERAGE" ("CmdResolveTexture" and attachments)
- Metal: stencil is not resolved with "the sample selected by the depth filter", which doesn't match "MIN" or "MAX"
- Validation: Metal rejects depth "AVERAGE" resolves, stencil resolves (also through a combined depth-stencil "depth" attachment without a separate "stencil" attachment) and depth-stencil "CmdResolveTexture", the corresponding checks are removed from the backend
- documented the per-API limits in "ResolveOp"
Keep unsupported resolve checks in Validation and preserve depth-only AVERAGE resolves on combined formats. Document the Metal command restriction.
|
OK, on your command. I put my buddy on hold. |
|
At head Selim implemented (
Finishing changes (
Still left as optional follow-ups:
These API extensions are not prerequisites for merging the resolve fixes. |
|
Please, let me know when you are done. My personal wish is to get "Explicit ResolveOp::SAMPLE_ZERO / SAMPLE_0" implemented in this PR. Can't wait to click Merge! |
- added "features.resolveOpAverageDepth": VK from "supportedDepthResolveModes", D3D12 always (the depth plane can be averaged), other backends don't support it - Validation: depth "AVERAGE" resolves (attachments and "CmdResolveTexture") require "features.resolveOpAverageDepth", it replaces the Metal-specific check - Validation: "CmdResolveTexture" source and destination must resolve the same planes (a missing region or "PlaneBits::ALL" covers all planes)
- "CmdUploadBufferToTexture" and "CmdReadbackTextureToBuffer" computed "bufferRowLength" from the whole depth-stencil texel size (8 bytes for "D32_SFLOAT_S8_UINT"), but buffer texels of the stencil aspect are 1 byte and of the depth aspect 4 bytes
- added "ResolveOp::SAMPLE_ZERO" and "features.resolveOpSampleZero"
- VK: supported for depth and stencil, color only for integer formats, integer color formats report "MULTISAMPLE_RESOLVE" with "maintenance10" ("vkCmdResolveImage" without it doesn't guarantee the sample 0) and dynamic rendering (legacy render passes have no color resolve modes)
- Metal: native "sample 0" depth and stencil resolve filters (stencil can be resolved now, explicitly or through a combined "depth" attachment), color is resolved by a shader
- VK: "features.resolveOpSampleZero" and "features.resolveOpAverageDepth" require "maintenance10", without it no depth-stencil or integer color format is resolvable
- D3D12, D3D11 and WebGPU: unsupported
- Validation: "MIN/MAX" require "features.resolveOpMinMax" and "SAMPLE_ZERO" requires "features.resolveOpSampleZero" (instead of "everything except AVERAGE requires 'resolveOpMinMax'"), VK color resolves require "AVERAGE" for non-integer and "SAMPLE_ZERO" for integer formats, Metal rejects only "MIN/MAX" stencil resolves
"DeviceDesc::resolve" reports supported resolve ops for attachments and "CmdResolveTexture", separately for color, integer color, depth and stencil, and whether depth and stencil can be resolved independently. It replaces "features.resolveOpMinMax", "features.resolveOpAverageDepth" and "features.resolveOpSampleZero". - VK: depth and stencil ops come from "supportedDepthResolveModes" and "supportedStencilResolveModes", independence from "independentResolve" and "independentResolveNone" - Validation: checks the masks instead of per-API rules
|
Should be ok now
I skipped the depth-selected stencil op because only Metal has it. Should we add that? No, right? Also, D3D12 has no sample 0 resolve mode, so it doesn't report |
|
Thanks!
I would say - yes. Unless this is really needed...
No emulation needed. Reviewing... |
|
Two independent read-only reviews of head Required fixes for merge
Coding-standard cleanup — non-blocking for runtime correctness
Verification Windows Debug/Release builds passed for D3D11, D3D12, Vulkan, WGPU, NONE, and Validation, including Agility on/off and confirmed Windows SDK 10.0.20348 headers for the non-Agility build. CRLF and 3 required fixes, 1 grouped non-blocker. FAIL / NO-GO. |
…ve resolve support
- temporary pipeline arrays use "Scratch" - stateless helpers are "static inline" and defined before class methods, redundant "Metal" suffixes removed - "IsAtomicFormat" is a free helper - backend-wide slot and queue constants moved to "SharedMetal.h" - parenthesized compound ternary conditions
|
Thanks, all fixed:
Cleanup items are done too. |
|
Remaining coding-standard cleanup at head
|
- parenthesized the resolve planes ternary in Validation - "GetRegionSize" is a file-local helper - "UpscalerVal" MetalFX input size is private - converted vertex inputs use stack arrays, too many inputs fail the conversion - descriptor entry and converted shader declarations moved to "SharedMetal.h" - removed redundant "Metal" suffixes from pipeline and acceleration structure helpers
- Validation: removed the per-API window check, it rejected Android windows (the per-platform check already covers all platforms)
- NONE: reports all resolve ops
- Metal: upload failures are recorded as "OUT_OF_MEMORY", acceleration structure and ray dispatch paths don't use null addresses
- Metal: texture atomics format support matches texture creation ("RG32_UINT" on Apple8+)
- Metal: counter heap creation failures are reported
- Metal: MetalFX support is checked per device in "IsUpscalerSupported"
- Metal: unsupported pipeline and sampler configurations report a warning, documented Metal topology limits
- methods called by "ImplMetal.cpp" wrappers are defined with "NRI_INLINE" - memory binding, "GetMemoryDesc2" and "GetFormatSupport" moved into "DeviceMetal" - destroy and factory wrappers match other backends
- FNV-1a hash shared with D3D12 ("Fnv1a64" in "SharedExternal.h")
- removed redundant suffixes from "NewFunctionDescriptor" and "FillAdapterDesc"
- the native command buffer lookup is a helper, the upscaler hooks are at the end of "ImplMetal.cpp"
- internal argument slots, layout asserts and "ColorTypeMetal" moved next to related declarations
- parenthesized compound ternary conditions and arithmetic comparison operands
- removed trailing periods from comments, aligned "NRIWrapperMetal.h" comments
- tessellator tables use a lock-free fast path, the emulation object shader name uses "Scratch"
- "CmdCopyTexture": explicit planes must exist in the format and match between source and destination - "CmdResolveTexture" and color attachment resolves: source and destination formats must match - Metal converter bundles: no geometry / tessellation shaders, "sampleMask = ALL" and no "POINT_LIST" are validated - native metallib thread group sizes are checked against "shaderStage" limits, keyed on "features.shaderBytecodeMETALLIB" - Metal: single-plane depth-stencil copies with different or multisampled formats report a warning (documented)
|
Congratulations! Merged! Please, note - I won't be able to work on NRI framework and NRI samples until October 19. |
|
@dzhdanNV Thanks! Have a great vacation! |
|
No, Selim, thanks to you! |
This adds a Metal 4 backend built on the
MTL4API and metal-cpp. HLSL runs through Metal Shader Converter, either at runtime or offline as ShaderMake.metalbundlefiles, and native.metallibshaders work too.Metal has no pipeline statistics, shading rate, conservative rasterization, logic ops, micromaps, 64-bit atomics, low latency or video.
New API:
GraphicsAPI::METAL,features.shaderBytecodeMETALLIB,ShaderDesc::threadGroupSizeX/Y/Z(native compute, mesh and task shaders),UpscalerType::METALFX_*,NRIWrapperMetal.handInclude/NRI.metal. I didn't bumpNRI_VERSION.New CMake options:
NRI_ENABLE_METAL_SUPPORT(on by default on Apple with Xcode 26+),NRI_ENABLE_METAL_SHADER_CONVERTER(on if the converter is installed) andNRI_METAL_CPP_PRIVATE_IMPLEMENTATION.Questions:
MIN/MAXresolves aren't possible, soresolveOpMinMaxonly covers depth/stencil there. Should the feature be split, or just documented?AVERAGEfor integer formats, but VK can only resolve them withSAMPLE_ZERO. I map integer color toSAMPLE_ZERO. Is that OK, or would you addResolveOp::SAMPLE_ZERO?AVERAGEresolves. The check used to be agraphicsAPI == METALbranch in validation, so I moved it into the backend. Would you prefer a feature flag that validation can check?Thanks to @onurpro for testing and fixing rt and @arshgarcha for windowing fixes