Skip to content

Commit a4aa140

Browse files
committed
implement depth resolve for metal
1 parent ce9f13e commit a4aa140

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

drivers/metal/metal_device_properties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MetalFeatures {
9797
bool supports_image_atomic_32_bit = false; /**< If true, 32-bit atomic operations on images are supported by the GPU. */
9898
bool supports_image_atomic_64_bit = false; /**< If true, 64-bit atomic operations on images are supported by the GPU. */
9999
bool supports_native_image_atomics = false; /**< If true, native image atomic operations are supported by the OS. */
100+
bool supports_msaa_depth_resolve = false;
100101
};
101102

102103
struct MetalLimits {

drivers/metal/metal_device_properties.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
features.supports_native_image_atomics = false;
131131
}
132132

133+
features.supports_msaa_depth_resolve = [p_device supportsFamily:MTLGPUFamilyApple3] || [p_device supportsFamily:MTLGPUFamilyMac2];
134+
if (@available(macos 13.0, ios 16.0, *)) {
135+
features.supports_msaa_depth_resolve = features.supports_msaa_depth_resolve || [p_device supportsFamily:MTLGPUFamilyMetal3];
136+
}
137+
133138
if (@available(macOS 13.0, iOS 16.0, tvOS 16.0, *)) {
134139
features.needs_arg_encoders = !([p_device supportsFamily:MTLGPUFamilyMetal3] && features.argument_buffers_tier == MTLArgumentBuffersTier2);
135140
}

drivers/metal/metal_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ struct MDSubpass {
239239
LocalVector<RDD::AttachmentReference> color_references;
240240
RDD::AttachmentReference depth_stencil_reference;
241241
LocalVector<RDD::AttachmentReference> resolve_references;
242+
RDD::AttachmentReference depth_stencil_resolve_reference;
242243

243244
MTLFmtCaps getRequiredFmtCapsForAttachmentAt(uint32_t p_index) const;
244245
};

drivers/metal/metal_objects.mm

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
/**************************************************************************/
5050

5151
#import "metal_objects.h"
52+
#include <cstdint>
5253

5354
#import "metal_utils.h"
5455
#import "pixel_formats.h"
@@ -640,6 +641,12 @@
640641
// see: https://github.com/KhronosGroup/MoltenVK/blob/d20d13fe2735adb845636a81522df1b9d89c0fba/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.mm#L407
641642
}
642643

644+
uint32_t depth_resolve_index = subpass.depth_stencil_resolve_reference.attachment;
645+
if (depth_resolve_index != RDD::AttachmentReference::UNUSED && fb_info.has_texture(depth_resolve_index)) {
646+
id<MTLTexture> resolve_tex = fb_info.get_texture(depth_resolve_index);
647+
CRASH_COND_MSG(!flags::all(pf.getCapabilities(resolve_tex.pixelFormat), kMTLFmtCapsResolve), "not implemented: unresolvable texture types");
648+
}
649+
643650
render.end_encoding();
644651
}
645652

@@ -751,9 +758,24 @@
751758
MDAttachment const &attachment = pass.attachments[idx];
752759
id<MTLTexture> tex = fb.get_texture(idx);
753760
ERR_FAIL_NULL_MSG(tex, "Frame buffer depth / stencil texture is null.");
761+
762+
uint32_t depth_resolve_index = subpass.depth_stencil_resolve_reference.attachment;
763+
bool has_resolve = depth_resolve_index != RDD::AttachmentReference::UNUSED;
764+
bool can_resolve = true;
765+
if (has_resolve) {
766+
MTLRenderPassDepthAttachmentDescriptor *da = desc.depthAttachment;
767+
id<MTLTexture> resolve_tex = fb.get_texture(depth_resolve_index);
768+
can_resolve = flags::all(pf.getCapabilities(resolve_tex.pixelFormat), kMTLFmtCapsResolve);
769+
if (can_resolve) {
770+
da.resolveTexture = resolve_tex;
771+
} else {
772+
CRASH_NOW_MSG("unimplemented: using a texture format that is not supported for resolve");
773+
}
774+
}
775+
754776
if (attachment.type & MDAttachmentType::Depth) {
755777
MTLRenderPassDepthAttachmentDescriptor *da = desc.depthAttachment;
756-
if (attachment.configureDescriptor(da, pf, subpass, tex, render.is_rendering_entire_area, false, false, false)) {
778+
if (attachment.configureDescriptor(da, pf, subpass, tex, render.is_rendering_entire_area, has_resolve, can_resolve, false)) {
757779
da.clearDepth = render.clear_values[idx].depth;
758780
}
759781
}
@@ -1727,6 +1749,10 @@
17271749
flags::set(caps, kMTLFmtCapsDSAtt);
17281750
}
17291751

1752+
if (depth_stencil_resolve_reference.attachment == p_index) {
1753+
flags::set(caps, kMTLFmtCapsResolve);
1754+
}
1755+
17301756
return caps;
17311757
}
17321758

drivers/metal/rendering_device_driver_metal.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
/**************************************************************************/
5050

5151
#import "rendering_device_driver_metal.h"
52+
#include "servers/rendering/rendering_device_commons.h"
5253

5354
#import "pixel_formats.h"
5455
#import "rendering_context_driver_metal.h"
@@ -1843,6 +1844,7 @@ bool isArrayTexture(MTLTextureType p_type) {
18431844
subpass.color_references = p_subpasses[i].color_references;
18441845
subpass.depth_stencil_reference = p_subpasses[i].depth_stencil_reference;
18451846
subpass.resolve_references = p_subpasses[i].resolve_references;
1847+
subpass.depth_stencil_resolve_reference = p_subpasses[i].depth_resolve_reference;
18461848
}
18471849

18481850
static const MTLLoadAction LOAD_ACTIONS[] = {
@@ -2772,6 +2774,8 @@ bool isArrayTexture(MTLTextureType p_type) {
27722774
return device_properties->features.supports_native_image_atomics;
27732775
case SUPPORTS_VULKAN_MEMORY_MODEL:
27742776
return true;
2777+
case SUPPORTS_FRAMEBUFFER_DEPTH_RESOLVE:
2778+
return device_properties->features.supports_msaa_depth_resolve;
27752779
default:
27762780
return false;
27772781
}

platform/visionos/app_visionos.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct ContentStageConfiguration: CompositorLayerConfiguration {
5656

5757
GDTAppDelegateServiceVisionOS.layerRendererCapabilities = capabilities as __CP_OBJECT_cp_layer_renderer_capabilities
5858

59-
configuration.depthFormat = .depth32Float
59+
configuration.depthFormat = .depth32Float_stencil8
6060
configuration.colorFormat = .rgba16Float
6161

6262
let foveationEnabled = capabilities.supportsFoveation

0 commit comments

Comments
 (0)