Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,12 +790,28 @@ impl RenderingBackend for MetalContext {
msg_send_![descriptor, setPixelFormat: view_pixel_format];
}
msg_send_![descriptor, setStorageMode: MTLStorageMode::Private];
msg_send_![
descriptor,
setUsage: MTLTextureUsage::RenderTarget as u64
| MTLTextureUsage::ShaderRead as u64
| MTLTextureUsage::ShaderWrite as u64
];
if params.sample_count > 1 {
// MSAA target — render-only, no mipmaps, no
// shader-sample (the resolve texture is sampled
// instead).
msg_send_![
descriptor,
setTextureType: MTLTextureType::D2Multisample
];
msg_send_![descriptor, setSampleCount: params.sample_count as u64];
msg_send_![descriptor, setMipmapLevelCount: 1u64];
msg_send_![
descriptor,
setUsage: MTLTextureUsage::RenderTarget as u64
];
} else {
msg_send_![
descriptor,
setUsage: MTLTextureUsage::RenderTarget as u64
| MTLTextureUsage::ShaderRead as u64
| MTLTextureUsage::ShaderWrite as u64
];
}
} else {
#[cfg(target_os = "macos")]
{
Expand Down Expand Up @@ -1072,6 +1088,11 @@ impl RenderingBackend for MetalContext {
setStencilAttachmentPixelFormat: view_depth_stencil_format
];
}
// Pipeline sampleCount must match every render-pass
// color attachment it binds to; use the view's as the
// canonical app-wide value.
let view_sample_count: u64 = msg_send![self.view, sampleCount];
msg_send_![descriptor, setSampleCount: view_sample_count];

let mut error: ObjcId = nil;
let pipeline_state: ObjcId = msg_send![
Expand Down Expand Up @@ -1288,7 +1309,19 @@ impl RenderingBackend for MetalContext {
let color_attachments = msg_send_![descriptor, colorAttachments];
let color_attachment = msg_send_![color_attachments, objectAtIndexedSubscript: 0];

msg_send_![color_attachment, setStoreAction: MTLStoreAction::Store];
// If the attachment has a resolve texture, use
// `StoreAndMultisampleResolve` (not just
// `MultisampleResolve`) so the multisample texture
// survives target-switch boundaries within a frame —
// a later `Load` on the same target would otherwise
// read discarded memory.
let resolve_texture: ObjcId = msg_send![color_attachment, resolveTexture];
let store_action = if resolve_texture.is_null() {
MTLStoreAction::Store
} else {
MTLStoreAction::StoreAndMultisampleResolve
};
msg_send_![color_attachment, setStoreAction: store_action];

match action {
PassAction::Clear { color, .. } => {
Expand Down
5 changes: 4 additions & 1 deletion src/native/ios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ unsafe fn create_opengl_view(screen_rect: NSRect, _sample_count: i32, high_dpi:
}
}

unsafe fn create_metal_view(screen_rect: NSRect, _sample_count: i32, _high_dpi: bool) -> View {
unsafe fn create_metal_view(screen_rect: NSRect, sample_count: i32, _high_dpi: bool) -> View {
let mtk_view_obj: ObjcId = msg_send![define_glk_or_mtk_view(class!(MTKView)), alloc];
let mtk_view_obj: ObjcId = msg_send![mtk_view_obj, initWithFrame: screen_rect];

Expand All @@ -524,6 +524,9 @@ unsafe fn create_metal_view(screen_rect: NSRect, _sample_count: i32, _high_dpi:
let device = MTLCreateSystemDefaultDevice();
msg_send_![mtk_view_obj, setDevice: device];
msg_send_![mtk_view_obj, setUserInteractionEnabled: YES];
if sample_count > 1 {
msg_send_![mtk_view_obj, setSampleCount: sample_count as u64];
}

View {
view: mtk_view_obj,
Expand Down
Loading