use std::ffi::c_void;
use super::device::{MetalDevice, MetalError};
use super::enums::*;
use super::objc::{
alloc_init, class, error_message, msg0, msg1, msg2, msg4, msg6, sel, AutoreleasePool, Bool, Id,
Owned, Sel, NIL, NO, YES,
};
use crate::textures::{Texture, TextureFilter, TextureFormat, TextureWrap};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct PipelineKey {
pub point_sprites: bool,
pub fur: bool,
pub custom: u64,
pub fx_surface: bool,
pub slim_lines: bool,
pub wide_lines: bool,
pub blend: bool,
pub additive: bool,
pub layered: bool,
pub skinned: bool,
pub topology: NSUInteger,
pub color_format: NSUInteger,
pub depth_format: NSUInteger,
pub sample_count: u32,
pub color_write: bool,
pub oit_accum: bool,
}
const READS_FRAMEBUFFER: bool = !cfg!(target_abi = "sim");
pub fn render_pipeline(device: &MetalDevice, key: PipelineKey) -> Result<Owned, MetalError> {
if (key.blend || key.additive)
&& pixel_format::is_srgb(key.color_format)
&& READS_FRAMEBUFFER
&& let Ok(p) = build_render_pipeline(device, key, true, None)
{
return Ok(p);
}
build_render_pipeline(device, key, false, None)
}
pub(crate) fn custom_render_pipeline(
device: &MetalDevice,
key: PipelineKey,
library: Id,
) -> Result<Owned, MetalError> {
if (key.blend || key.additive)
&& pixel_format::is_srgb(key.color_format)
&& READS_FRAMEBUFFER
&& let Ok(p) = build_render_pipeline(device, key, true, Some(library))
{
return Ok(p);
}
build_render_pipeline(device, key, false, Some(library))
}
pub(crate) fn library_function(library: Id, name: &str) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let f: Id = msg1(library, sel!("newFunctionWithName:"), super::objc::nsstring(name));
Owned::from_retained(f).ok_or_else(|| MetalError::MissingFunction(name.into()))
}
}
fn build_render_pipeline(
device: &MetalDevice,
key: PipelineKey,
encoded_blend: bool,
library: Option<Id>,
) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
let (vs, fs) = match (key.point_sprites, key.layered) {
(false, false) if key.wide_lines => ("vs_line_quad", "fs_line_quad"),
(false, false) if key.slim_lines => ("vs_line", "fs_mesh"),
(false, false) if key.skinned => ("vs_skinned", "fs_mesh"),
(false, false) if key.fur => ("vs_fur", "fs_fur"),
(false, false) if key.fx_surface => ("vs_fx_surface", "fs_fx_surface"),
(false, false) => ("vs_mesh", "fs_mesh"),
(false, true) => ("vs_mesh_layered", "fs_mesh"),
(true, false) => ("vs_point", "fs_point"),
(true, true) => ("vs_point_layered", "fs_point"),
};
let encoded_blend = encoded_blend
&& (fs == "fs_mesh" || fs == "fs_point" || (fs == "fs_fx_surface" && !key.additive));
if encoded_blend && !key.blend && !key.additive {
return Err(MetalError::PipelineCreation("no encoded blend without blending".into()));
}
let fs = match (encoded_blend, fs) {
(true, "fs_point") if key.additive => "fs_point_encoded_add",
(true, "fs_mesh") if key.additive => "fs_mesh_encoded_add",
(true, "fs_point") => "fs_point_encoded_blend",
(true, "fs_fx_surface") => "fs_fx_surface_encoded_blend",
(true, _) => "fs_mesh_encoded_blend",
(false, fs) => fs,
};
let fs = if key.oit_accum { "fs_oit_accum" } else { fs };
let (vertex_fn, fragment_fn) = match library {
Some(lib) => (
library_function(lib, "vs_custom_entry").or_else(|_| library_function(lib, vs))?,
library_function(
lib,
match (encoded_blend, key.additive) {
(true, true) => "fs_custom_eb_add",
(true, false) => "fs_custom_eb",
(false, _) => "fs_custom",
},
)?,
),
None => (device.function(vs)?, device.function(fs)?),
};
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLRenderPipelineDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLRenderPipelineDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setVertexFunction:"), vertex_fn.id());
let _: () = msg1(desc.id(), sel!("setFragmentFunction:"), fragment_fn.id());
let _: () = msg1(
desc.id(),
sel!("setRasterSampleCount:"),
key.sample_count.max(1) as NSUInteger,
);
if key.depth_format != 0 {
let _: () = msg1(
desc.id(),
sel!("setDepthAttachmentPixelFormat:"),
key.depth_format,
);
}
if key.topology != topology_class::UNSPECIFIED {
let _: () = msg1(desc.id(), sel!("setInputPrimitiveTopology:"), key.topology);
}
let attachments: Id = msg0(desc.id(), sel!("colorAttachments"));
let color: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), 0usize);
let _: () = msg1(color, sel!("setPixelFormat:"), key.color_format);
if key.oit_accum {
for (index, (src, dst)) in [
(blend_factor::ONE, blend_factor::ONE),
(blend_factor::ZERO, blend_factor::ONE_MINUS_SOURCE_COLOR),
]
.into_iter()
.enumerate()
{
let a: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), index);
let _: () = msg1(a, sel!("setPixelFormat:"), pixel_format::RGBA16_FLOAT);
let _: () = msg1(a, sel!("setBlendingEnabled:"), YES);
let _: () = msg1(a, sel!("setRgbBlendOperation:"), blend_op::ADD);
let _: () = msg1(a, sel!("setAlphaBlendOperation:"), blend_op::ADD);
let _: () = msg1(a, sel!("setSourceRGBBlendFactor:"), src);
let _: () = msg1(a, sel!("setDestinationRGBBlendFactor:"), dst);
let alpha_dst = if index == 0 { blend_factor::ONE } else { blend_factor::ONE_MINUS_SOURCE_ALPHA };
let _: () = msg1(a, sel!("setSourceAlphaBlendFactor:"), src);
let _: () = msg1(a, sel!("setDestinationAlphaBlendFactor:"), alpha_dst);
}
} else if key.additive && !encoded_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::SOURCE_ALPHA);
let _: () = msg1(color, sel!("setDestinationRGBBlendFactor:"), blend_factor::ONE);
let _: () = msg1(color, sel!("setSourceAlphaBlendFactor:"), blend_factor::SOURCE_ALPHA);
let _: () = msg1(color, sel!("setDestinationAlphaBlendFactor:"), blend_factor::ONE);
} else if key.blend && !encoded_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::SOURCE_ALPHA,
);
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,
);
} else {
let _: () = msg1(color, sel!("setBlendingEnabled:"), NO);
}
if !key.color_write {
let _: () = msg1(color, sel!("setWriteMask:"), 0 as NSUInteger);
}
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)))
}
}
#[allow(clippy::too_many_arguments)]
pub fn new_texture_3d(
device: &MetalDevice,
[width, height, depth]: [u32; 3],
pixel_format: NSUInteger,
usage: NSUInteger,
storage: NSUInteger,
) -> 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 _: () = msg1(desc.id(), sel!("setTextureType:"), texture_type::TYPE_3D);
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:"), depth.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}x{depth} 3D texture, format {pixel_format}"))
})
}
}
pub use super::pass::new_texture_2d;
pub fn new_texture_array(
device: &MetalDevice,
width: u32,
height: u32,
pixel_format: NSUInteger,
usage: NSUInteger,
slices: u32,
) -> Result<Owned, MetalError> {
new_texture_array_levels(device, width, height, pixel_format, usage, slices, 1)
}
pub fn new_texture_array_levels(
device: &MetalDevice,
width: u32,
height: u32,
pixel_format: NSUInteger,
usage: NSUInteger,
slices: u32,
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 _: () = msg1(
desc.id(),
sel!("setTextureType:"),
texture_type::TYPE_2D_ARRAY,
);
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!("setArrayLength:"),
slices.max(1) as NSUInteger,
);
let _: () = msg1(desc.id(), sel!("setMipmapLevelCount:"), levels.max(1) as NSUInteger);
let _: () = msg1(desc.id(), sel!("setSampleCount:"), 1usize);
let _: () = msg1(desc.id(), sel!("setUsage:"), usage);
let _: () = msg1(desc.id(), sel!("setStorageMode:"), storage_mode::PRIVATE);
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 array of {slices} slices, format {pixel_format}"
))
})
}
}
fn pixel_format_for(format: TextureFormat) -> NSUInteger {
use TextureFormat::*;
match format {
Rgba8UnormSrgb => pixel_format::RGBA8_UNORM_SRGB,
Rgba8Unorm => pixel_format::RGBA8_UNORM,
R8Unorm => pixel_format::R8_UNORM,
Rg8Unorm => pixel_format::RG8_UNORM,
R16Unorm => pixel_format::R16_UNORM,
Rgba16Unorm => pixel_format::RGBA16_UNORM,
Rgb10a2Unorm => pixel_format::RGB10A2_UNORM,
Rgba16Float => pixel_format::RGBA16_FLOAT,
R16Float => pixel_format::R16_FLOAT,
Rg16Float => pixel_format::RG16_FLOAT,
R32Float => pixel_format::R32_FLOAT,
Rg32Float => pixel_format::RG32_FLOAT,
Rgba32Float => pixel_format::RGBA32_FLOAT,
Rg11b10Ufloat => pixel_format::RG11B10_FLOAT,
Rgb9e5Ufloat => pixel_format::RGB9E5_FLOAT,
Bc1RgbaUnormSrgb => pixel_format::BC1_RGBA_SRGB,
Bc1RgbaUnorm => pixel_format::BC1_RGBA,
Bc3RgbaUnormSrgb => pixel_format::BC3_RGBA_SRGB,
Bc3RgbaUnorm => pixel_format::BC3_RGBA,
Bc4RUnorm => pixel_format::BC4_R_UNORM,
Bc5RgUnorm => pixel_format::BC5_RG_UNORM,
Bc6hRgbUfloat => pixel_format::BC6H_RGB_UFLOAT,
Bc6hRgbFloat => pixel_format::BC6H_RGB_FLOAT,
Bc7RgbaUnormSrgb => pixel_format::BC7_RGBA_SRGB,
Bc7RgbaUnorm => pixel_format::BC7_RGBA,
Etc2Rgb8UnormSrgb => pixel_format::ETC2_RGB8_SRGB,
Etc2Rgb8Unorm => pixel_format::ETC2_RGB8,
Etc2Rgba8UnormSrgb => pixel_format::EAC_RGBA8_SRGB,
Etc2Rgba8Unorm => pixel_format::EAC_RGBA8,
Astc { block, srgb } => {
let i = crate::textures::AstcBlock::ALL
.iter()
.position(|b| *b == block)
.unwrap();
pixel_format::astc(i, srgb)
}
}
}
pub(crate) fn cpu_writable_storage(device: &MetalDevice) -> NSUInteger {
#[cfg(any(target_os = "macos", target_abi = "macabi"))]
{
if device.has_unified_memory() {
storage_mode::SHARED
} else {
storage_mode::MANAGED
}
}
#[cfg(not(any(target_os = "macos", target_abi = "macabi")))]
{
let _ = device;
storage_mode::SHARED
}
}
pub fn support(device: &MetalDevice) -> crate::textures::codec::Support {
let apple = supports_family(device, GPU_FAMILY_APPLE2);
crate::textures::codec::Support {
bc: supports_bc(device),
etc2: apple,
astc: apple,
unorm16: true,
float32_filterable: responds_and(device, sel!("supports32BitFloatFiltering")),
}
}
const GPU_FAMILY_APPLE2: isize = 1002;
fn supports_family(device: &MetalDevice, family: isize) -> bool {
unsafe {
let responds: Bool = msg1(
device.device_id(),
sel!("respondsToSelector:"),
sel!("supportsFamily:"),
);
if responds == 0 {
return false;
}
let ok: Bool = msg1(device.device_id(), sel!("supportsFamily:"), family);
ok != 0
}
}
fn responds_and(device: &MetalDevice, property: Sel) -> bool {
unsafe {
let responds: Bool = msg1(device.device_id(), sel!("respondsToSelector:"), property);
if responds == 0 {
return false;
}
let ok: Bool = msg0(device.device_id(), property);
ok != 0
}
}
pub fn supports_bc(device: &MetalDevice) -> bool {
unsafe {
let responds: Bool = msg1(
device.device_id(),
sel!("respondsToSelector:"),
sel!("supportsBCTextureCompression"),
);
if responds == 0 {
return false;
}
let ok: Bool = msg0(device.device_id(), sel!("supportsBCTextureCompression"));
ok != 0
}
}
pub fn upload_texture(device: &MetalDevice, src: &Texture) -> Result<Owned, MetalError> {
upload_texture_as(device, src, false)
}
pub fn upload_texture_as(
device: &MetalDevice,
src: &Texture,
linear: bool,
) -> Result<Owned, MetalError> {
let adapted = crate::textures::codec::for_device(src, support(device));
let src = &*adapted;
let bc = src.format.is_block_compressed();
let format = if linear {
src.format.with_srgb(false).unwrap_or(src.format)
} else {
src.format
};
let pixel_format = pixel_format_for(format);
if bc && src.flip_y {
return Err(MetalError::Unsupported(
"flip_y on a block-compressed texture: rows of 4x4 blocks cannot be reordered \
per row — pre-flip the source"
.into(),
));
}
let cpu_mips = if src.format == TextureFormat::Rgb9e5Ufloat && src.mips.is_empty() {
crate::renderer::gpu_texture::cpu_mip_chain(src)
} else {
Vec::new()
};
let src = &*if cpu_mips.is_empty() {
std::borrow::Cow::Borrowed(src)
} else {
let mut t = src.clone();
t.mips = cpu_mips
.into_iter()
.map(|(_, _, d)| std::sync::Arc::new(d))
.collect();
std::borrow::Cow::Owned(t)
};
let supplied_mips = src.mips.len() as u32;
let mip_levels = if supplied_mips > 0 {
supplied_mips + 1
} else if bc || src.width <= 1 || src.height <= 1 || (src.stream.is_some() && src.mipmap_filter.is_none()) {
1
} else {
32 - src.width.max(src.height).leading_zeros()
};
let usage = if src.stream.is_some() {
texture_usage::SHADER_READ | texture_usage::RENDER_TARGET
} else {
texture_usage::SHADER_READ
};
let tex = new_texture_2d(
device,
src.width,
src.height,
pixel_format,
usage,
cpu_writable_storage(device),
1,
mip_levels,
)?;
if src.stream.is_some() {
return Ok(tex);
}
let (bw, bh) = src.format.block_dim();
let bytes_per_block = src.format.bytes_per_block();
let level0 = flip_rows_if_needed(src);
upload_level(
&tex,
0,
src.width,
src.height,
(bw, bh),
bytes_per_block,
&level0,
)?;
for (i, level) in src.mips.iter().enumerate() {
let l = i as u32 + 1;
let w = (src.width >> l).max(1);
let h = (src.height >> l).max(1);
upload_level(&tex, l, w, h, (bw, bh), bytes_per_block, level)?;
}
if supplied_mips == 0 && mip_levels > 1 {
generate_mipmaps(device, &tex);
}
Ok(tex)
}
fn upload_level(
tex: &Owned,
level: u32,
width: u32,
height: u32,
block: (u32, u32),
bytes_per_block: usize,
bytes: &[u8],
) -> Result<(), MetalError> {
let rows = height.div_ceil(block.1) as usize;
let bytes_per_row = width.div_ceil(block.0) as usize * bytes_per_block;
let needed = rows * bytes_per_row;
if bytes.len() < needed {
return Err(MetalError::Unsupported(format!(
"texture level {level} is {} bytes, needs {needed} for {width}x{height}",
bytes.len()
)));
}
unsafe {
let _: () = msg4(
tex.id(),
sel!("replaceRegion:mipmapLevel:withBytes:bytesPerRow:"),
MTLRegion::image_2d(width, height),
level as NSUInteger,
bytes.as_ptr() as *const c_void,
bytes_per_row as NSUInteger,
);
}
Ok(())
}
fn generate_mipmaps(device: &MetalDevice, tex: &Owned) {
let _pool = AutoreleasePool::new();
unsafe {
let cmd = device.command_buffer();
if cmd.is_null() {
return;
}
let blit: Id = msg0(cmd, sel!("blitCommandEncoder"));
if blit.is_null() {
return;
}
let _: () = msg1(blit, sel!("generateMipmapsForTexture:"), tex.id());
let _: () = msg0(blit, sel!("endEncoding"));
let _: () = msg0(cmd, sel!("commit"));
let _: () = msg0(cmd, sel!("waitUntilCompleted"));
}
}
fn flip_rows_if_needed(src: &Texture) -> std::borrow::Cow<'_, [u8]> {
if !src.flip_y || src.height <= 1 {
return std::borrow::Cow::Borrowed(&src.data[..]);
}
let stride = src.width as usize * src.format.bytes_per_block();
let rows = src.height as usize;
if src.data.len() < stride * rows {
return std::borrow::Cow::Borrowed(&src.data[..]);
}
let mut out = vec![0u8; stride * rows];
for y in 0..rows {
let dst = y * stride;
let s = (rows - 1 - y) * stride;
out[dst..dst + stride].copy_from_slice(&src.data[s..s + stride]);
}
std::borrow::Cow::Owned(out)
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct SamplerKey {
pub mag_linear: bool,
pub min_linear: bool,
pub mipmapped: bool,
pub mip_linear: bool,
pub wrap_s: u8,
pub wrap_t: u8,
}
impl SamplerKey {
pub fn from_texture(t: &Texture) -> Self {
Self {
mag_linear: t.mag_filter == TextureFilter::Linear,
min_linear: t.min_filter == TextureFilter::Linear,
mipmapped: t.mipmap_filter.is_some()
&& !t.format.is_block_compressed()
&& t.width > 1
&& t.height > 1,
mip_linear: t.mipmap_filter != Some(TextureFilter::Nearest),
wrap_s: wrap_code(t.wrap_s),
wrap_t: wrap_code(t.wrap_t),
}
}
pub fn from_cube(c: &crate::textures::CubeTexture) -> Self {
Self {
mag_linear: c.mag_filter == TextureFilter::Linear,
min_linear: c.min_filter == TextureFilter::Linear,
mipmapped: c.mipmap_filter.is_some(),
mip_linear: c.mipmap_filter != Some(TextureFilter::Nearest),
wrap_s: 0,
wrap_t: 0,
}
}
pub fn environment() -> Self {
Self {
mag_linear: true,
min_linear: true,
mipmapped: true,
mip_linear: true,
wrap_s: 0,
wrap_t: 0,
}
}
pub fn default_linear() -> Self {
Self {
mag_linear: true,
min_linear: true,
mipmapped: false,
mip_linear: true,
wrap_s: 0,
wrap_t: 0,
}
}
}
fn wrap_code(w: TextureWrap) -> u8 {
match w {
TextureWrap::ClampToEdge => 0,
TextureWrap::Repeat => 1,
TextureWrap::MirroredRepeat => 2,
}
}
fn address_mode(code: u8) -> NSUInteger {
match code {
1 => address_mode::REPEAT,
2 => address_mode::MIRROR_REPEAT,
_ => address_mode::CLAMP_TO_EDGE,
}
}
pub fn empty_shadow_map(device: &MetalDevice) -> Result<Owned, MetalError> {
new_texture_array_levels(
device,
1,
1,
pixel_format::DEPTH32_FLOAT,
texture_usage::RENDER_TARGET | texture_usage::SHADER_READ,
1,
1,
)
}
pub fn shadow_sampler(device: &MetalDevice) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLSamplerDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLSamplerDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setMagFilter:"), filter::LINEAR);
let _: () = msg1(desc.id(), sel!("setMinFilter:"), filter::LINEAR);
let _: () = msg1(desc.id(), sel!("setSAddressMode:"), address_mode::CLAMP_TO_EDGE);
let _: () = msg1(desc.id(), sel!("setTAddressMode:"), address_mode::CLAMP_TO_EDGE);
let _: () = msg1(desc.id(), sel!("setCompareFunction:"), compare::LESS_EQUAL);
let sampler: Id = msg1(
device.device_id(),
sel!("newSamplerStateWithDescriptor:"),
desc.id(),
);
Owned::from_retained(sampler)
.ok_or_else(|| MetalError::Allocation("shadow sampler".into()))
}
}
pub fn shadow_pipeline(device: &MetalDevice) -> Result<Owned, MetalError> {
shadow_pipeline_named(device, "vs_shadow", None)
}
pub fn point_shadow_pipeline(device: &MetalDevice) -> Result<Owned, MetalError> {
shadow_pipeline_named(device, "vs_shadow_point", Some("fs_shadow_point"))
}
pub fn cut_shadow_pipeline(device: &MetalDevice) -> Result<Owned, MetalError> {
shadow_pipeline_named(device, "vs_shadow_cut", Some("fs_shadow_cut"))
}
fn shadow_pipeline_named(
device: &MetalDevice,
vs: &str,
fs: Option<&str>,
) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLRenderPipelineDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLRenderPipelineDescriptor".into()))?;
let vertex_fn = device.function(vs)?;
let _: () = msg1(desc.id(), sel!("setVertexFunction:"), vertex_fn.id());
if let Some(fs) = fs {
let fragment_fn = device.function(fs)?;
let _: () = msg1(desc.id(), sel!("setFragmentFunction:"), fragment_fn.id());
}
let _: () = msg1(
desc.id(),
sel!("setDepthAttachmentPixelFormat:"),
pixel_format::DEPTH32_FLOAT,
);
let mut err: Id = NIL;
let pipeline: Id = msg2(
device.device_id(),
sel!("newRenderPipelineStateWithDescriptor:error:"),
desc.id(),
&mut err as *mut Id,
);
Owned::from_retained(pipeline).ok_or_else(|| {
MetalError::PipelineCreation(error_message(err))
})
}
}
pub fn new_sampler(device: &MetalDevice, key: SamplerKey) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLSamplerDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLSamplerDescriptor".into()))?;
let filt = |linear: bool| {
if linear {
filter::LINEAR
} else {
filter::NEAREST
}
};
let _: () = msg1(desc.id(), sel!("setMagFilter:"), filt(key.mag_linear));
let _: () = msg1(desc.id(), sel!("setMinFilter:"), filt(key.min_linear));
let _: () = msg1(
desc.id(),
sel!("setMipFilter:"),
if key.mipmapped && key.mip_linear {
filter::MIP_LINEAR
} else if key.mipmapped {
filter::MIP_NEAREST
} else {
filter::MIP_NOT_MIPMAPPED
},
);
let _: () = msg1(
desc.id(),
sel!("setSAddressMode:"),
address_mode(key.wrap_s),
);
let _: () = msg1(
desc.id(),
sel!("setTAddressMode:"),
address_mode(key.wrap_t),
);
let _: () = msg1(desc.id(), sel!("setMaxAnisotropy:"), 1usize);
let state: Id = msg1(
device.device_id(),
sel!("newSamplerStateWithDescriptor:"),
desc.id(),
);
Owned::from_retained(state).ok_or_else(|| MetalError::Allocation("MTLSamplerState".into()))
}
}
pub fn black_texture_array(device: &MetalDevice) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
let tex = unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLTextureDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLTextureDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setTextureType:"), texture_type::TYPE_2D_ARRAY);
let _: () = msg1(desc.id(), sel!("setPixelFormat:"), pixel_format::RGBA16_FLOAT);
let _: () = msg1(desc.id(), sel!("setWidth:"), 1usize);
let _: () = msg1(desc.id(), sel!("setHeight:"), 1usize);
let _: () = msg1(desc.id(), sel!("setDepth:"), 1usize);
let _: () = msg1(desc.id(), sel!("setArrayLength:"), 1usize);
let _: () = msg1(desc.id(), sel!("setMipmapLevelCount:"), 1usize);
let _: () = msg1(desc.id(), sel!("setUsage:"), texture_usage::SHADER_READ);
let _: () = msg1(desc.id(), sel!("setStorageMode:"), cpu_writable_storage(device));
let tex: Id = msg1(device.device_id(), sel!("newTextureWithDescriptor:"), desc.id());
Owned::from_retained(tex).ok_or_else(|| MetalError::Allocation("capture stand-in".into()))?
};
let zero = [0u16; 4];
unsafe {
let _: () = msg6(
tex.id(),
sel!("replaceRegion:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:"),
MTLRegion::image_2d(1, 1),
0usize,
0usize,
zero.as_ptr() as *const c_void,
8 as NSUInteger,
0usize,
);
}
Ok(tex)
}
pub fn upload_ltc(device: &MetalDevice) -> Result<Owned, MetalError> {
const LTC_1: &[u8] = include_bytes!("../renderer/ltc/ltc_float_1.bin");
const LTC_2: &[u8] = include_bytes!("../renderer/ltc/ltc_float_2.bin");
let _pool = AutoreleasePool::new();
let tex = unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLTextureDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLTextureDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setTextureType:"), texture_type::TYPE_2D_ARRAY);
let _: () = msg1(desc.id(), sel!("setPixelFormat:"), pixel_format::RGBA16_FLOAT);
let _: () = msg1(desc.id(), sel!("setWidth:"), 64usize);
let _: () = msg1(desc.id(), sel!("setHeight:"), 64usize);
let _: () = msg1(desc.id(), sel!("setDepth:"), 1usize);
let _: () = msg1(desc.id(), sel!("setArrayLength:"), 2usize);
let _: () = msg1(desc.id(), sel!("setMipmapLevelCount:"), 1usize);
let _: () = msg1(desc.id(), sel!("setUsage:"), texture_usage::SHADER_READ);
let _: () = msg1(desc.id(), sel!("setStorageMode:"), cpu_writable_storage(device));
let tex: Id = msg1(device.device_id(), sel!("newTextureWithDescriptor:"), desc.id());
Owned::from_retained(tex).ok_or_else(|| MetalError::Allocation("LTC table texture".into()))?
};
for (slice, table) in [LTC_1, LTC_2].into_iter().enumerate() {
let half: Vec<u16> = table
.as_chunks::<4>()
.0
.iter()
.map(|c| {
crate::renderer::gpu_texture::f32_to_f16_bits(f32::from_le_bytes([
c[0], c[1], c[2], c[3],
]))
})
.collect();
unsafe {
let _: () = msg6(
tex.id(),
sel!("replaceRegion:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:"),
MTLRegion::image_2d(64, 64),
0usize,
slice as NSUInteger,
half.as_ptr() as *const c_void,
(64 * 8) as NSUInteger,
0usize,
);
}
}
Ok(tex)
}
fn new_texture_cube(
device: &MetalDevice,
size: u32,
pixel_format: NSUInteger,
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 _: () = msg1(desc.id(), sel!("setTextureType:"), texture_type::TYPE_CUBE);
let _: () = msg1(desc.id(), sel!("setPixelFormat:"), pixel_format);
let _: () = msg1(desc.id(), sel!("setWidth:"), size.max(1) as NSUInteger);
let _: () = msg1(desc.id(), sel!("setHeight:"), size.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!("setUsage:"), texture_usage::SHADER_READ);
let _: () = msg1(desc.id(), sel!("setStorageMode:"), cpu_writable_storage(device));
let tex: Id = msg1(
device.device_id(),
sel!("newTextureWithDescriptor:"),
desc.id(),
);
Owned::from_retained(tex)
.ok_or_else(|| MetalError::Allocation(format!("{size}x{size} cube texture")))
}
}
fn upload_cube_level(
tex: &Owned,
face: u32,
level: u32,
size: u32,
bytes: &[u8],
) -> Result<(), MetalError> {
let bytes_per_row = size as usize * 4;
let needed = bytes_per_row * size as usize;
if bytes.len() < needed {
return Err(MetalError::Unsupported(format!(
"cube face {face} level {level} is {} bytes, needs {needed}",
bytes.len()
)));
}
unsafe {
let _: () = msg6(
tex.id(),
sel!("replaceRegion:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:"),
MTLRegion::image_2d(size, size),
level as NSUInteger,
face as NSUInteger,
bytes.as_ptr() as *const c_void,
bytes_per_row as NSUInteger,
0usize,
);
}
Ok(())
}
pub fn upload_cube_uv_atlas(
device: &MetalDevice,
atlas: &crate::textures::CubeUvAtlas,
srgb: bool,
) -> Result<Owned, MetalError> {
let usage = texture_usage::SHADER_READ;
let storage = cpu_writable_storage(device);
match &atlas.pixels_f32 {
Some(f) => {
let tex = new_texture_2d(device, atlas.width, atlas.height, pixel_format::RGBA16_FLOAT, usage, storage, 1, 1)?;
let halves: Vec<u8> = f
.iter()
.flat_map(|v| crate::renderer::gpu_texture::f32_to_f16_bits(*v).to_le_bytes())
.collect();
upload_level(&tex, 0, atlas.width, atlas.height, (1, 1), 8, &halves)?;
Ok(tex)
}
None => {
let format = if srgb { pixel_format::RGBA8_UNORM_SRGB } else { pixel_format::RGBA8_UNORM };
let tex = new_texture_2d(device, atlas.width, atlas.height, format, usage, storage, 1, 1)?;
upload_level(&tex, 0, atlas.width, atlas.height, (1, 1), 4, &atlas.pixels)?;
Ok(tex)
}
}
}
pub fn upload_cube(
device: &MetalDevice,
cube: &crate::textures::CubeTexture,
) -> Result<Owned, MetalError> {
let (levels, sizes) = crate::renderer::gpu_texture::mip_chain(cube.format, cube.size, &cube.faces);
let format = match cube.format {
TextureFormat::Rgba8UnormSrgb => pixel_format::RGBA8_UNORM_SRGB,
_ => pixel_format::RGBA8_UNORM,
};
let tex = new_texture_cube(device, cube.size, format, levels.len() as u32)?;
for (level, (faces, size)) in levels.iter().zip(&sizes).enumerate() {
for (face, data) in faces.iter().enumerate() {
upload_cube_level(&tex, face as u32, level as u32, *size, data)?;
}
}
Ok(tex)
}
pub fn black_cube(device: &MetalDevice) -> Result<Owned, MetalError> {
let tex = new_texture_cube(device, 1, pixel_format::RGBA8_UNORM, 1)?;
for face in 0..6 {
upload_cube_level(&tex, face, 0, 1, &[0, 0, 0, 255])?;
}
Ok(tex)
}
pub fn white_texture(device: &MetalDevice) -> Result<Owned, MetalError> {
let tex = new_texture_2d(
device,
1,
1,
pixel_format::RGBA8_UNORM,
texture_usage::SHADER_READ,
cpu_writable_storage(device),
1,
1,
)?;
upload_level(&tex, 0, 1, 1, (1, 1), 4, &[255, 255, 255, 255])?;
Ok(tex)
}
pub fn zero_volume(device: &MetalDevice) -> Result<Owned, MetalError> {
let tex = new_texture_3d(
device,
[1, 1, 1],
pixel_format::RGBA8_UNORM,
texture_usage::SHADER_READ,
cpu_writable_storage(device),
)?;
unsafe {
let _: () = super::objc::msg6(
tex.id(),
sel!("replaceRegion:mipmapLevel:slice:withBytes:bytesPerRow:bytesPerImage:"),
MTLRegion::image_2d(1, 1),
0usize,
0usize,
[0u8; 4].as_ptr() as *const c_void,
4usize,
4usize,
);
}
Ok(tex)
}
pub fn oit_composite_pipeline(
device: &MetalDevice,
color_format: NSUInteger,
sample_count: u32,
light: bool,
) -> Result<Owned, MetalError> {
let _pool = AutoreleasePool::new();
let vertex_fn = device.function("vs_oit_fullscreen")?;
let fragment_fn = device.function(if light { "fs_oit_light" } else { "fs_oit_tint" })?;
unsafe {
let desc = Owned::from_retained(alloc_init(class!("MTLRenderPipelineDescriptor")))
.ok_or_else(|| MetalError::Allocation("MTLRenderPipelineDescriptor".into()))?;
let _: () = msg1(desc.id(), sel!("setVertexFunction:"), vertex_fn.id());
let _: () = msg1(desc.id(), sel!("setFragmentFunction:"), fragment_fn.id());
let _: () = msg1(desc.id(), sel!("setRasterSampleCount:"), sample_count.max(1) as NSUInteger);
let attachments: Id = msg0(desc.id(), sel!("colorAttachments"));
let color: Id = msg1(attachments, sel!("objectAtIndexedSubscript:"), 0usize);
let _: () = msg1(color, sel!("setPixelFormat:"), color_format);
let _: () = msg1(color, sel!("setBlendingEnabled:"), YES);
let _: () = msg1(color, sel!("setRgbBlendOperation:"), blend_op::ADD);
let _: () = msg1(color, sel!("setAlphaBlendOperation:"), blend_op::ADD);
let (rgb_src, rgb_dst, a_src, a_dst) = if light {
(blend_factor::ONE, blend_factor::ONE, blend_factor::ONE, blend_factor::ONE_MINUS_SOURCE_ALPHA)
} else {
(blend_factor::ZERO, blend_factor::SOURCE_COLOR, blend_factor::ZERO, blend_factor::ONE)
};
let _: () = msg1(color, sel!("setSourceRGBBlendFactor:"), rgb_src);
let _: () = msg1(color, sel!("setDestinationRGBBlendFactor:"), rgb_dst);
let _: () = msg1(color, sel!("setSourceAlphaBlendFactor:"), a_src);
let _: () = msg1(color, sel!("setDestinationAlphaBlendFactor:"), a_dst);
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)))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn tex(w: u32, h: u32, flip: bool) -> Texture {
let mut t = Texture::new(
w,
h,
TextureFormat::Rgba8UnormSrgb,
(0..w * h * 4).map(|i| (i % 251) as u8).collect(),
);
t.flip_y = flip;
t
}
#[test]
fn flip_moves_whole_rows() {
let t = tex(2, 3, true);
let flipped = flip_rows_if_needed(&t);
let stride = 8;
assert_eq!(&flipped[0..stride], &t.data[2 * stride..3 * stride]);
assert_eq!(&flipped[2 * stride..3 * stride], &t.data[0..stride]);
}
#[test]
fn flip_borrows_when_disabled() {
let t = tex(2, 3, false);
assert!(matches!(
flip_rows_if_needed(&t),
std::borrow::Cow::Borrowed(_)
));
}
#[test]
fn pipeline_keys_distinguish_blend_and_format() {
let a = PipelineKey {
oit_accum: false,
fur: false,
fx_surface: false,
custom: 0,
additive: false,
point_sprites: false,
slim_lines: false,
wide_lines: false,
blend: false,
layered: false,
skinned: false,
topology: topology_class::UNSPECIFIED,
color_format: pixel_format::RGBA8_UNORM_SRGB,
depth_format: pixel_format::DEPTH32_FLOAT,
sample_count: 1,
color_write: true,
};
let b = PipelineKey { blend: true, ..a };
assert_ne!(a, b);
assert_ne!(a, PipelineKey { color_write: false, ..a });
let c = PipelineKey {
sample_count: 4,
..a
};
assert_ne!(a, c);
let d = PipelineKey {
layered: true,
topology: topology_class::TRIANGLE,
..a
};
assert_ne!(a, d);
assert_ne!(
d,
PipelineKey {
topology: topology_class::POINT,
..d
}
);
assert_ne!(a, PipelineKey { slim_lines: true, ..a });
assert_ne!(a, PipelineKey { wide_lines: true, ..a });
assert_eq!(topology_class::of(primitive::POINT), topology_class::POINT);
assert_eq!(topology_class::of(primitive::LINE), topology_class::LINE);
assert_eq!(
topology_class::of(primitive::TRIANGLE),
topology_class::TRIANGLE
);
}
#[test]
fn sampler_key_tracks_wrap_and_filter() {
let mut t = tex(4, 4, false);
t.wrap_s = TextureWrap::Repeat;
t.min_filter = TextureFilter::Nearest;
let k = SamplerKey::from_texture(&t);
assert_eq!(k.wrap_s, 1);
assert!(!k.min_linear);
assert!(k.mag_linear);
assert!(k.mipmapped);
}
}