Skip to content
Open
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 @@ -715,12 +715,28 @@ impl RenderingBackend for MetalContext {
msg_send_![descriptor, setPixelFormat: 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 @@ -985,6 +1001,11 @@ impl RenderingBackend for MetalContext {
descriptor,
setStencilAttachmentPixelFormat: MTLPixelFormat::Depth32Float_Stencil8
];
// 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 @@ -1190,7 +1211,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 @@ -451,7 +451,7 @@
}
}

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 @@ -470,6 +470,9 @@
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 Expand Up @@ -499,7 +502,7 @@
_: ObjcId,
) -> BOOL {
unsafe {
let (f, conf) = RUN_ARGS.take().unwrap();

Check warning on line 505 in src/native/ios.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-ios)

creating a mutable reference to mutable static

Check warning on line 505 in src/native/ios.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, aarch64-apple-ios)

creating a mutable reference to mutable static

let main_screen: ObjcId = msg_send![class!(UIScreen), mainScreen];
let screen_rect: NSRect = msg_send![main_screen, bounds];
Expand Down
Loading