use super::device::{MetalDevice, MetalError};
use super::enums::*;
use super::objc::{
alloc_init, class, error_message, msg0, msg1, msg2, msg3, sel, AutoreleasePool, Id, Owned, NIL,
YES,
};
pub(crate) fn fullscreen_pipeline_with(
device: &MetalDevice,
vertex: &str,
fragment: &str,
color_format: NSUInteger,
) -> Result<Owned, MetalError> {
fullscreen_pipeline_blend(device, vertex, fragment, color_format, false)
}
pub(crate) fn fullscreen_pipeline_blend(
device: &MetalDevice,
vertex: &str,
fragment: &str,
color_format: NSUInteger,
blend: bool,
) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
let vs = device.function(vertex)?;
let fs = device.function(fragment)?;
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLRenderPipelineDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLRenderPipelineDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setVertexFunction:"), vs.id());
let _: () = msg1(desc.id(), sel!("setFragmentFunction:"), fs.id());
let attachments: Id = msg0(desc.id(), sel!("colorAttachments"));
let color: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), 0usize);
let _: () = msg1(color, sel!("setPixelFormat:"), color_format);
if blend {
let _: () = msg1(color, sel!("setBlendingEnabled:"), YES);
let _: () = msg1(color, sel!("setRgbBlendOperation:"), blend_op::ADD);
let _: () = msg1(color, sel!("setAlphaBlendOperation:"), blend_op::ADD);
let _: () = msg1(color, sel!("setSourceRGBBlendFactor:"), blend_factor::ONE);
let _: () = msg1(
color,
sel!("setDestinationRGBBlendFactor:"),
blend_factor::ONE_MINUS_SOURCE_ALPHA,
);
let _: () = msg1(color, sel!("setSourceAlphaBlendFactor:"), blend_factor::ONE);
let _: () = msg1(
color,
sel!("setDestinationAlphaBlendFactor:"),
blend_factor::ONE_MINUS_SOURCE_ALPHA,
);
}
let mut err: Id = NIL;
let state: Id = msg2(
device.device_id(),
sel!("newRenderPipelineStateWithDescriptor:error:"),
desc.id(),
&mut err as *mut Id,
);
Owned::from_retained(state).ok_or_else(|| MetalError::PipelineCreation(error_message(err)))
}
}
pub(crate) fn encode_fullscreen(
cmd: Id,
pipeline: Id,
dst: Id,
_color_format: NSUInteger,
bind: impl FnOnce(Id),
) -> Result<(), MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let desc = render_pass_descriptor(dst);
let enc: Id = msg1(cmd, sel!("renderCommandEncoderWithDescriptor:"), desc);
if enc.is_null() {
return Err(MetalError::Unsupported(
"fullscreen postfx render encoder failed".into(),
));
}
let _: () = msg1(enc, sel!("setRenderPipelineState:"), pipeline);
bind(enc);
let _: () = msg3(
enc,
sel!("drawPrimitives:vertexStart:vertexCount:"),
primitive::TRIANGLE,
0usize,
3usize,
);
let _: () = msg0(enc, sel!("endEncoding"));
}
Ok(())
}
pub(crate) fn render_pass_descriptor(dst: Id) -> Id {
unsafe {
let desc: Id = msg0(
class!("MTLRenderPassDescriptor"),
sel!("renderPassDescriptor"),
);
let attachments: Id = msg0(desc, sel!("colorAttachments"));
let color: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), 0usize);
let _: () = msg1(color, sel!("setTexture:"), dst);
let _: () = msg1(color, sel!("setLoadAction:"), load_action::CLEAR);
let _: () = msg1(color, sel!("setStoreAction:"), store_action::STORE);
let clear = MTLClearColor {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
};
let _: () = msg1(color, sel!("setClearColor:"), clear);
desc
}
}
pub fn new_texture_2d(
device: &MetalDevice,
width: u32,
height: u32,
pixel_format: NSUInteger,
usage: NSUInteger,
storage: NSUInteger,
sample_count: u32,
mip_levels: u32,
) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLTextureDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLTextureDescriptor".into()))?;
let ty = if sample_count > 1 {
texture_type::TYPE_2D_MULTISAMPLE
} else {
texture_type::TYPE_2D
};
let _: () = msg1(desc.id(), sel!("setTextureType:"), ty);
let _: () = msg1(desc.id(), sel!("setPixelFormat:"), pixel_format);
let _: () = msg1(desc.id(), sel!("setWidth:"), width.max(1) as NSUInteger);
let _: () = msg1(desc.id(), sel!("setHeight:"), height.max(1) as NSUInteger);
let _: () = msg1(desc.id(), sel!("setDepth:"), 1usize);
let _: () = msg1(
desc.id(),
sel!("setMipmapLevelCount:"),
mip_levels.max(1) as NSUInteger,
);
let _: () = msg1(
desc.id(),
sel!("setSampleCount:"),
sample_count.max(1) as NSUInteger,
);
let _: () = msg1(desc.id(), sel!("setUsage:"), usage);
let _: () = msg1(desc.id(), sel!("setStorageMode:"), storage);
let tex: Id = msg1(
device.device_id(),
sel!("newTextureWithDescriptor:"),
desc.id(),
);
Owned::from_retained(tex).ok_or_else(|| {
MetalError::Allocation(format!(
"{width}x{height} texture, format {pixel_format}, {sample_count}x MSAA"
))
})
}
}