use std::collections::HashMap;
use std::ffi::c_void;
use std::sync::Arc;
use super::device::{depth_stencil_state, MetalDevice, MetalError};
use super::enums::*;
use super::objc::{
class, msg0, msg1, msg2, msg3, msg4, msg6, sel, AutoreleasePool, Id, Owned, NIL,
};
use super::resources::{
new_sampler, render_pipeline, upload_texture_as, white_texture, PipelineKey, SamplerKey,
};
use crate::cameras::Camera;
use crate::core::{BufferGeometry, Layers, ObjectKind};
use crate::lights::Light;
use crate::materials::{Material, MaterialKind};
use crate::math::{Matrix4, Vector3};
use crate::renderer::viewport::{ClearMode, PixelRect};
use crate::scene::Scene;
use crate::textures::Texture;
pub const MAX_VIEWS: usize = 4;
use crate::lights::shadow::{Caster, MAX_SHADOW_RECORDS};
mod frame;
mod collect;
mod resources;
mod pipelines;
mod shadows;
mod capture;
const MAX_DIR_LIGHTS: usize = 4;
const POINT_SHADOW_SIZE: u32 = 512;
const MAX_POINT_LIGHTS: usize = 8;
const MAX_SPOT_LIGHTS: usize = 4;
const MAX_HEMI_LIGHTS: usize = 2;
const VB_VERTICES: NSUInteger = 0;
const VB_FRAME: NSUInteger = 1;
const VB_DRAW: NSUInteger = 2;
const VB_INSTANCES: NSUInteger = 3;
const VB_SKIN: NSUInteger = 4;
const VB_BONES: NSUInteger = 5;
const VB_MORPH_DATA: NSUInteger = 6;
const VB_MORPH: NSUInteger = 7;
const VB_UV_SETS: NSUInteger = 8;
const VB_FUR: NSUInteger = 9;
const FB_FUR: NSUInteger = 2;
const FB_FRAME: NSUInteger = 0;
const FB_DRAW: NSUInteger = 1;
const FRAMES_IN_FLIGHT: usize = 3;
pub const DEFAULT_CACHE_RETENTION: u64 = 120;
#[repr(C)]
#[derive(Clone, Copy, Default, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct Vertex {
pub position: [f32; 3],
pub normal: [f32; 3],
pub uv: [f32; 2],
pub color: [f32; 3],
pub alpha: f32,
}
fn slim_lines_enabled() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("THREERS_NO_SLIM_LINES").is_none())
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct LineVertex {
pub position: [f32; 3],
pub color: [f32; 3],
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct DirLightGpu {
direction: [f32; 4],
color: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct PointLightGpu {
position: [f32; 4],
color: [f32; 4],
params: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct SpotLightGpu {
position: [f32; 4],
direction: [f32; 4],
color: [f32; 4],
params: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct HemiLightGpu {
sky: [f32; 4],
ground: [f32; 4],
up: [f32; 4],
}
#[repr(C, align(16))]
#[derive(Clone, Copy)]
pub(crate) struct FrameUniforms {
view_proj: [[f32; 16]; MAX_VIEWS],
view: [[f32; 16]; MAX_VIEWS],
camera_pos: [[f32; 4]; MAX_VIEWS],
ambient: [f32; 4],
fog_color: [f32; 4],
fog_params: [f32; 4],
viewport: [f32; 4],
counts: [u32; 4],
view_info: [u32; 4],
dir: [DirLightGpu; MAX_DIR_LIGHTS],
points: [PointLightGpu; MAX_POINT_LIGHTS],
spots: [SpotLightGpu; MAX_SPOT_LIGHTS],
hemis: [HemiLightGpu; MAX_HEMI_LIGHTS],
rect_counts: [u32; 4],
rects: [RectLightGpu; MAX_RECT_LIGHTS],
shadow_info: [u32; 4],
shadows: [ShadowRecordGpu; MAX_SHADOW_RECORDS],
}
struct MetalCapture {
target: crate::metal::MetalRenderTarget,
mipped: Owned,
size: (u32, u32),
levels: u32,
slices: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct RectLightGpu {
position: [f32; 4],
color: [f32; 4],
right: [f32; 4],
up: [f32; 4],
}
const MAX_RECT_LIGHTS: usize = 4;
impl Default for FrameUniforms {
fn default() -> Self {
Self {
view_proj: [Matrix4::identity().elements; MAX_VIEWS],
view: [Matrix4::identity().elements; MAX_VIEWS],
camera_pos: [[0.0; 4]; MAX_VIEWS],
view_info: [1, 0, 0, 0],
ambient: [0.0; 4],
fog_color: [0.0; 4],
fog_params: [0.0; 4],
viewport: [1.0, 1.0, 1.0, 1.0],
counts: [0; 4],
dir: [DirLightGpu::default(); MAX_DIR_LIGHTS],
points: [PointLightGpu::default(); MAX_POINT_LIGHTS],
spots: [SpotLightGpu::default(); MAX_SPOT_LIGHTS],
hemis: [HemiLightGpu::default(); MAX_HEMI_LIGHTS],
rect_counts: [0; 4],
rects: [RectLightGpu::default(); MAX_RECT_LIGHTS],
shadow_info: [0; 4],
shadows: [ShadowRecordGpu::default(); MAX_SHADOW_RECORDS],
}
}
}
impl FrameUniforms {
fn promote_view(&mut self, index: usize) {
if index != 0 && index < MAX_VIEWS {
self.view_proj[0] = self.view_proj[index];
self.view[0] = self.view[index];
self.camera_pos[0] = self.camera_pos[index];
}
self.view_info[0] = 1;
}
}
struct EnvGpu {
cube: Owned,
atlas: Option<Owned>,
params: [f32; 4],
}
impl EnvGpu {
fn upload(device: &MetalDevice, cube: &crate::textures::CubeTexture) -> Result<Self, MetalError> {
let tex = crate::metal::resources::upload_cube(device, cube)?;
let (atlas, params) = match &cube.cube_uv_atlas {
Some(a) => (
Some(crate::metal::resources::upload_cube_uv_atlas(
device,
a,
cube.format == crate::TextureFormat::Rgba8UnormSrgb,
)?),
[a.texel_width, a.texel_height, a.lod_max as f32, 1.0],
),
None => (None, [0.0; 4]),
};
Ok(Self { cube: tex, atlas, params })
}
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct MorphUniforms {
info: [u32; 4],
base: [f32; 4],
weights: [[f32; 4]; 16],
}
struct MorphBuffer {
buffer: Owned,
count: u32,
stride: u32,
flags: u32,
vertex_count: u32,
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct SkinVertex {
joints: [f32; 4],
weights: [f32; 4],
}
#[repr(C, align(16))]
#[derive(Clone, Copy)]
pub(crate) struct DrawUniforms {
model: [f32; 16],
normal_mat: [f32; 16],
base_color: [f32; 4],
emissive: [f32; 4],
specular: [f32; 4],
pbr: [f32; 4],
misc: [f32; 4],
flags: [u32; 4],
maps: [u32; 4],
map_params: [f32; 4],
object: [u32; 4],
extra: [u32; 4],
map_scalars: [f32; 4],
env_params: [f32; 4],
env_dir: [[f32; 4]; 3],
map_samplers: [[u32; 4]; 6],
physical: [[f32; 4]; 7],
phys_flags: [u32; 4],
map_xform: [[f32; 4]; 2 * MAP_COUNT],
planet: [[f32; 4]; 5],
clip: [f32; 4],
}
const MAP_COUNT: usize = 22;
const MAP_PHYS0: usize = 10;
const PHYS_TEXTURE0: usize = 16;
const SAMPLER_POOL: usize = 10;
const USER_TEXTURE0: usize = 16;
const USER_SAMPLER_SLOT: usize = 15;
const USER_VOLUME0: usize = 20;
const USER_VOLUME_SAMPLER_SLOT: usize = 9;
const USER_VOLUME_SAMPLER: SamplerKey = SamplerKey {
mag_linear: true,
min_linear: true,
mipmapped: false,
mip_linear: false,
wrap_s: 0,
wrap_t: 0,
};
const USER_SAMPLER: SamplerKey = SamplerKey {
mag_linear: true,
min_linear: true,
mipmapped: true,
mip_linear: true,
wrap_s: 1,
wrap_t: 1,
};
fn physical_maps(slots: &crate::materials::MaterialTextureSlots) -> [(Option<&Arc<Texture>>, bool); 12] {
[
(slots.specular_intensity_map.as_ref(), true),
(slots.specular_color_map.as_ref(), false),
(slots.clearcoat_map.as_ref(), true),
(slots.clearcoat_roughness_map.as_ref(), true),
(slots.clearcoat_normal_map.as_ref(), true),
(slots.iridescence_map.as_ref(), true),
(slots.iridescence_thickness_map.as_ref(), true),
(slots.sheen_color_map.as_ref(), false),
(slots.sheen_roughness_map.as_ref(), true),
(slots.transmission_map.as_ref(), true),
(slots.thickness_map.as_ref(), true),
(slots.anisotropy_map.as_ref(), true),
]
}
const IDENTITY_XFORMS: [[f32; 4]; 2 * MAP_COUNT] = {
let mut x = [[0.0; 4]; 2 * MAP_COUNT];
let mut i = 0;
while i < MAP_COUNT {
x[2 * i] = [1.0, 0.0, 0.0, 0.0];
x[2 * i + 1] = [0.0, 1.0, 0.0, 0.0];
i += 1;
}
x
};
fn map_xforms(slots: &crate::materials::MaterialTextureSlots) -> [[f32; 4]; 2 * MAP_COUNT] {
let mut out = IDENTITY_XFORMS;
let mut maps = [None; MAP_COUNT];
maps[..10].copy_from_slice(&[
slots.map.as_ref(),
slots.emissive_map.as_ref(),
slots.normal_map.as_ref(),
slots.roughness_map.as_ref().or(slots.metalness_map.as_ref()),
slots.ao_map.as_ref(),
slots.alpha_map.as_ref(),
slots.light_map.as_ref(),
slots.bump_map.as_ref(),
slots.specular_map.as_ref(),
None,
]);
for (i, (t, _)) in physical_maps(slots).into_iter().enumerate() {
maps[MAP_PHYS0 + i] = t;
}
for (i, t) in maps.into_iter().enumerate() {
if let Some(t) = t {
let [r0, r1] = t.uv_transform_rows();
out[2 * i] = [r0[0], r0[1], r0[2], t.channel.min(3) as f32];
out[2 * i + 1] = [r1[0], r1[1], r1[2], 0.0];
}
}
out
}
impl Default for DrawUniforms {
fn default() -> Self {
Self {
model: Matrix4::identity().elements,
normal_mat: Matrix4::identity().elements,
base_color: [1.0, 1.0, 1.0, 1.0],
emissive: [0.0; 4],
specular: [0.0, 0.0, 0.0, 30.0],
pbr: [1.0, 0.0, 0.0, 0.0],
misc: [0.0, 1.0, 0.1, 1000.0],
flags: [0; 4],
maps: [0; 4],
map_params: [1.0, 1.0, 1.0, 0.0],
object: [0; 4],
extra: [0; 4],
map_scalars: [1.0, 1.0, 0.0, 0.0],
env_params: [0.0, 1.0, 0.98, 0.0],
env_dir: [[-1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]],
map_samplers: [[0; 4]; 6],
physical: [[0.0; 4]; 7],
phys_flags: [0; 4],
map_xform: IDENTITY_XFORMS,
planet: [[0.0; 4], [0.0; 4], [0.0; 4], [0.0; 4], [1.0, 0.0, 0.0, 0.0]],
clip: [0.0; 4],
}
}
}
#[derive(Clone, Copy)]
pub struct PassAttachments {
pub(crate) color: Id,
pub(crate) resolve: Id,
pub(crate) depth: Id,
pub(crate) depth_resolve: Id,
pub(crate) drawable: Id,
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) color_format: NSUInteger,
pub(crate) depth_format: NSUInteger,
pub(crate) sample_count: u32,
pub(crate) reverse_z: bool,
}
impl PassAttachments {
#[allow(clippy::too_many_arguments)]
pub unsafe fn from_raw(
color: Id,
resolve: Id,
depth: Id,
drawable: Id,
width: u32,
height: u32,
color_format: NSUInteger,
depth_format: NSUInteger,
sample_count: u32,
) -> Self {
Self {
color,
resolve,
depth,
depth_resolve: NIL,
drawable,
width,
height,
color_format,
depth_format,
sample_count,
reverse_z: false,
}
}
pub unsafe fn with_depth_resolve(mut self, texture: Id) -> Self {
self.depth_resolve = texture;
self
}
pub fn with_reverse_z(mut self, reverse: bool) -> Self {
self.reverse_z = reverse;
self
}
pub fn size(&self) -> (u32, u32) {
(self.width, self.height)
}
pub fn reverse_z(&self) -> bool {
self.reverse_z
}
}
#[derive(Clone, Debug)]
pub struct RenderView {
pub view_matrix: Matrix4,
pub projection_matrix: Matrix4,
pub position: Vector3,
pub layers: Layers,
pub slice: u32,
pub viewport: Option<MTLViewport>,
pub color: Id,
pub depth: Id,
}
impl RenderView {
pub fn from_camera(camera: &dyn Camera) -> Self {
Self {
view_matrix: camera.view_matrix(),
projection_matrix: camera.projection_matrix(),
position: camera.position(),
layers: camera.layers(),
slice: 0,
viewport: None,
color: NIL,
depth: NIL,
}
}
pub fn from_matrices(view_matrix: Matrix4, projection_matrix: Matrix4) -> Self {
let inverse = view_matrix.invert().elements;
Self {
view_matrix,
projection_matrix,
position: Vector3::new(inverse[12], inverse[13], inverse[14]),
layers: Layers::default(),
slice: 0,
viewport: None,
color: NIL,
depth: NIL,
}
}
pub fn with_slice(mut self, slice: u32) -> Self {
self.slice = slice;
self
}
pub fn with_viewport(mut self, viewport: MTLViewport) -> Self {
self.viewport = Some(viewport);
self
}
pub unsafe fn with_textures(mut self, color: Id, depth: Id) -> Self {
self.color = color;
self.depth = depth;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MetalRenderStats {
pub draw_calls: u32,
pub triangles: u32,
pub skipped: u32,
pub culled: u32,
pub geometry_uploads: u32,
pub texture_uploads: u32,
pub geometry_bytes: u64,
}
struct CachedGeometry {
_geometry: Arc<BufferGeometry>,
version: u32,
vertices: Owned,
skin: Option<Owned>,
indices: Option<Owned>,
index_count: usize,
vertex_count: usize,
wire: Option<(Owned, usize)>,
morph: Option<MorphBuffer>,
uv_sets: Option<(Owned, u32)>,
bytes: u64,
sphere: Option<crate::math::Sphere>,
slim: bool,
last_used: u64,
}
struct CachedTexture {
_texture: Arc<Texture>,
texture: Owned,
sampler_key: SamplerKey,
last_used: u64,
stream_frames: u64,
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct ShadowRecordGpu {
info: [u32; 4],
params: [f32; 4],
extra: [f32; 4],
pcss: [f32; 4],
vp: [[f32; 16]; 4],
}
#[repr(C)]
#[derive(Clone, Copy)]
struct ShadowPassGpu {
shadow_vp: [f32; 16],
point_shadow_pos: [f32; 4],
}
struct OitTargets {
size: (u32, u32, u32),
accum: Owned,
reveal: Owned,
accum_ms: Option<Owned>,
reveal_ms: Option<Owned>,
}
type Casters = Vec<Caster>;
#[derive(Clone)]
struct DrawItem {
fur: Option<[f32; 64]>,
fx_surface: Option<Box<[f32; crate::fx::surface::METAL_FLOATS]>>,
custom: bool,
geometry: Arc<BufferGeometry>,
material: Arc<Material>,
world: Matrix4,
layers: Layers,
primitive: NSUInteger,
instances: Option<Vec<f32>>,
instance_slot: Option<usize>,
instance_count: usize,
instance_morph: Option<(Vec<f32>, usize)>,
instance_morph_layout: Option<(u32, u32)>,
instance_colors: Option<Vec<f32>>,
instance_custom: Option<Vec<f32>>,
instance_color_offset: u32,
view_depth: f32,
render_order: i32,
clip: [f32; 4],
transparent: bool,
transmissive: bool,
blended_glass: bool,
oit: bool,
screen_space: bool,
capture_skip: bool,
side_override: Option<u32>,
state: crate::materials::RenderState,
bones: Option<Vec<f32>>,
bone_slot: Option<usize>,
cast_shadow: bool,
receive_shadow: bool,
range: (u32, u32),
morph_influences: Vec<f32>,
wire_span: Option<(usize, usize)>,
sprite: Option<[f32; 4]>,
}
pub struct MetalRenderer {
device: MetalDevice,
pipelines: HashMap<PipelineKey, Owned>,
samplers: HashMap<SamplerKey, Owned>,
geometries: HashMap<usize, CachedGeometry>,
textures: HashMap<usize, CachedTexture>,
streams: super::video::MetalStreams,
depth_states: HashMap<(NSUInteger, bool), Owned>,
white: Owned,
zero_volume: Owned,
volume_maps: crate::volume::VolumeMaps,
volume_gpu: Option<crate::marks::gpu::Gpu>,
ltc: Owned,
transmission: Option<MetalCapture>,
capture_bound: Option<Id>,
capture_dummy: Owned,
capture_depth_dummy: Owned,
transmission_sampler: Owned,
black_cube: Owned,
shadow_map: Option<Owned>,
shadow_size: u32,
spot_shadow_map: Option<Owned>,
spot_shadow_size: u32,
point_shadow_map: Option<Owned>,
shadow_layers: [usize; 3],
shadow_pass_hashes: Vec<Option<u64>>,
shadow_pipeline: Option<Owned>,
point_shadow_pipeline: Option<Owned>,
cut_shadow_pipeline: Option<Owned>,
shadow_sampler: Option<Owned>,
empty_shadow: Owned,
env_cube: Option<(crate::renderer::gpu_texture::CubeCacheKey, EnvGpu)>,
material_envs: HashMap<crate::renderer::gpu_texture::CubeCacheKey, EnvGpu>,
pmrem_cache: HashMap<crate::renderer::gpu_texture::CubeCacheKey, Arc<crate::textures::CubeTexture>>,
env_light: (f32, [[f32; 4]; 3]),
identity_instance: Owned,
transients: Vec<Vec<Owned>>,
frame: u64,
cache_retention: u64,
store_depth: bool,
keep_attachments: bool,
encoded_output: bool,
oit_active: bool,
oit_phase: u8,
oit_targets: Option<OitTargets>,
oit_composite: HashMap<(NSUInteger, u32), (Owned, Owned)>,
line_width: f32,
tone_mapping: (crate::ToneMapping, f32),
specular_antialiasing: bool,
reflective_transparency: bool,
shadow_type: crate::lights::ShadowType,
logged_geometry_bytes: std::sync::atomic::AtomicU64,
pub(super) viewport_state: super::viewport::MetalViewportState,
shadow_flags: crate::renderer::ShadowMap,
shadow_sig: Option<(usize, usize, usize, u32, u32)>,
shadow_updates: u64,
overlay: super::overlay::MetalOverlay,
custom_libs: HashMap<u64, Option<Owned>>,
translated: HashMap<u64, Option<Arc<str>>>,
}
impl MetalRenderer {
pub fn new() -> Result<Self, MetalError> {
Self::with_device(MetalDevice::new()?)
}
pub fn with_device(device: MetalDevice) -> Result<Self, MetalError> {
let mut depth_states = HashMap::new();
depth_states.insert(
(compare::LESS, true),
depth_stencil_state(&device, compare::LESS, true)?,
);
let white = white_texture(&device)?;
let zero_volume = crate::metal::resources::zero_volume(&device)?;
let ltc = crate::metal::resources::upload_ltc(&device)?;
let capture_dummy = crate::metal::resources::black_texture_array(&device)?;
let capture_depth_dummy = crate::metal::resources::new_texture_2d(
&device,
1,
1,
pixel_format::DEPTH32_FLOAT,
texture_usage::SHADER_READ | texture_usage::RENDER_TARGET,
storage_mode::PRIVATE,
1,
1,
)?;
let transmission_sampler = new_sampler(
&device,
SamplerKey { mag_linear: true, min_linear: true, mipmapped: true, mip_linear: true, wrap_s: 0, wrap_t: 0 },
)?;
let black_cube = crate::metal::resources::black_cube(&device)?;
let empty_shadow = crate::metal::resources::empty_shadow_map(&device)?;
let identity_instance =
device.new_buffer(bytemuck::cast_slice(&[Matrix4::identity().elements]))?;
let viewport_state = super::viewport::MetalViewportState::new(device.clone());
let mut samplers = HashMap::new();
for key in [SamplerKey::default_linear(), SamplerKey::environment()] {
samplers.insert(key, new_sampler(&device, key)?);
}
Ok(Self {
device,
pipelines: HashMap::new(),
samplers,
geometries: HashMap::new(),
textures: HashMap::new(),
streams: Default::default(),
depth_states,
white,
zero_volume,
volume_maps: Default::default(),
volume_gpu: None,
ltc,
capture_dummy,
capture_depth_dummy,
transmission: None,
capture_bound: None,
transmission_sampler,
black_cube,
material_envs: HashMap::new(),
pmrem_cache: HashMap::new(),
env_light: (1.0, crate::materials::environment_lookup_rows(Default::default(), true)),
env_cube: None,
shadow_map: None,
shadow_size: 0,
spot_shadow_map: None,
spot_shadow_size: 0,
point_shadow_map: None,
shadow_layers: [0; 3],
shadow_pass_hashes: Vec::new(),
shadow_pipeline: None,
point_shadow_pipeline: None,
cut_shadow_pipeline: None,
shadow_sampler: None,
empty_shadow,
identity_instance,
transients: (0..FRAMES_IN_FLIGHT).map(|_| Vec::new()).collect(),
frame: 0,
cache_retention: DEFAULT_CACHE_RETENTION,
store_depth: false,
keep_attachments: false,
encoded_output: false,
oit_active: false,
oit_phase: 0,
oit_targets: None,
oit_composite: HashMap::new(),
line_width: 0.0,
tone_mapping: (crate::ToneMapping::None, 1.0),
specular_antialiasing: false,
reflective_transparency: false,
shadow_type: crate::lights::ShadowType::default(),
logged_geometry_bytes: std::sync::atomic::AtomicU64::new(0),
viewport_state,
shadow_flags: Default::default(),
shadow_sig: None,
shadow_updates: 0,
overlay: Default::default(),
custom_libs: HashMap::new(),
translated: HashMap::new(),
})
}
pub fn shadow_map(&self) -> &crate::renderer::ShadowMap {
&self.shadow_flags
}
pub fn shadow_map_mut(&mut self) -> &mut crate::renderer::ShadowMap {
&mut self.shadow_flags
}
pub fn draw_overlay(
&mut self,
overlay: &mut crate::overlay::Overlay,
pass: &PassAttachments,
) -> Result<(), MetalError> {
let (width, height) = overlay.size();
let bands = overlay.dirty_bands();
self.overlay
.composite(&self.device, overlay.pixels(), width, height, Some(&bands), pass)?;
overlay.mark_uploaded();
Ok(())
}
pub fn draw_overlay_rgba8(
&mut self,
rgba: &[u8],
width: u32,
height: u32,
pass: &PassAttachments,
) -> Result<(), MetalError> {
self.overlay.composite(&self.device, rgba, width, height, None, pass)
}
pub fn draw_texture(&mut self, texture: Id, pass: &PassAttachments) -> Result<(), MetalError> {
self.overlay.draw_texture(&self.device, texture, pass)
}
pub fn blend_texture(&mut self, texture: Id, pass: &PassAttachments) -> Result<(), MetalError> {
self.overlay.blend_texture(&self.device, texture, pass)
}
pub fn shadow_map_updates(&self) -> u64 {
self.shadow_updates
}
pub fn device(&self) -> &MetalDevice {
&self.device
}
pub fn clear_caches(&mut self) {
self.geometries.clear();
self.textures.clear();
}
pub fn cache_sizes(&self) -> (usize, usize) {
(self.geometries.len(), self.textures.len())
}
pub fn set_line_width(&mut self, px: f32) {
self.line_width = px.max(0.0);
}
pub fn set_tone_mapping(&mut self, mode: crate::ToneMapping, exposure: f32) {
self.tone_mapping = (mode, exposure);
}
pub fn tone_mapping(&self) -> (crate::ToneMapping, f32) {
self.tone_mapping
}
pub fn set_specular_antialiasing(&mut self, on: bool) {
self.specular_antialiasing = on;
}
pub fn specular_antialiasing(&self) -> bool {
self.specular_antialiasing
}
pub fn set_shadow_type(&mut self, kind: crate::lights::ShadowType) {
self.shadow_type = kind;
}
pub fn shadow_type(&self) -> crate::lights::ShadowType {
self.shadow_type
}
pub fn set_reflective_transparency(&mut self, on: bool) {
self.reflective_transparency = on;
}
pub fn reflective_transparency(&self) -> bool {
self.reflective_transparency
}
pub fn set_store_depth(&mut self, on: bool) {
self.store_depth = on;
}
pub fn set_keep_attachments(&mut self, on: bool) {
self.keep_attachments = on;
}
pub fn set_encoded_output(&mut self, on: bool) {
self.encoded_output = on;
}
pub fn set_cache_retention(&mut self, frames: u64) {
self.cache_retention = frames;
}
}
pub(crate) fn clear_color(color: crate::math::Color, alpha: f32) -> MTLClearColor {
MTLClearColor {
red: color.r as f64,
green: color.g as f64,
blue: color.b as f64,
alpha: alpha as f64,
}
}
fn layered(views: &[RenderView], pass: &PassAttachments) -> bool {
if views.len() < 2 || !pass.resolve.is_null() {
return false;
}
let first = &views[0];
let same_viewport = |a: &Option<MTLViewport>, b: &Option<MTLViewport>| match (a, b) {
(None, None) => true,
(Some(a), Some(b)) => {
a.origin_x == b.origin_x
&& a.origin_y == b.origin_y
&& a.width == b.width
&& a.height == b.height
}
_ => false,
};
views.iter().all(|v| {
v.color.is_null() && v.depth.is_null() && same_viewport(&v.viewport, &first.viewport)
}) && views
.iter()
.enumerate()
.all(|(i, v)| views[..i].iter().all(|w| w.slice != v.slice))
}
fn custom_shader_id(msl: &str) -> u64 {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
msl.hash(&mut h);
h.finish().max(1)
}
unsafe fn set_bytes<T>(encoder: Id, vertex_stage: bool, value: &T, index: NSUInteger) {
debug_assert!(std::mem::size_of::<T>() <= 4096);
let sel = if vertex_stage {
sel!("setVertexBytes:length:atIndex:")
} else {
sel!("setFragmentBytes:length:atIndex:")
};
let _: () = unsafe { msg3(
encoder,
sel,
value as *const T as *const c_void,
std::mem::size_of::<T>() as NSUInteger,
index,
) };
}
fn scaled(color: crate::math::Color, intensity: f32) -> [f32; 4] {
[
color.r * intensity,
color.g * intensity,
color.b * intensity,
0.0,
]
}
fn metal_compare(func: crate::materials::DepthFunc) -> NSUInteger {
use crate::materials::DepthFunc;
match func {
DepthFunc::Never => compare::NEVER,
DepthFunc::Always => compare::ALWAYS,
DepthFunc::Less => compare::LESS,
DepthFunc::LessEqual => compare::LESS_EQUAL,
DepthFunc::Equal => compare::EQUAL,
DepthFunc::GreaterEqual => compare::GREATER_EQUAL,
DepthFunc::Greater => compare::GREATER,
DepthFunc::NotEqual => compare::NOT_EQUAL,
}
}
fn shadow_cull(side: u32, radial: bool) -> NSUInteger {
let (front, back) = if radial { (cull::BACK, cull::FRONT) } else { (cull::FRONT, cull::BACK) };
match side {
1 => back,
2 => cull::NONE,
_ => front,
}
}
fn is_transparent(material: &Material) -> bool {
material.transparent()
}
fn transform_direction(m: &Matrix4, v: Vector3) -> Vector3 {
let e = &m.elements;
Vector3::new(
e[0] * v.x + e[4] * v.y + e[8] * v.z,
e[1] * v.x + e[5] * v.y + e[9] * v.z,
e[2] * v.x + e[6] * v.y + e[10] * v.z,
)
}
pub(crate) fn normal_matrix(m: &Matrix4) -> [f32; 16] {
let inv = m.invert();
let e = inv.elements;
let mut t = [0.0f32; 16];
for col in 0..4 {
for row in 0..4 {
t[col * 4 + row] = e[row * 4 + col];
}
}
t
}
fn geometry_sphere(g: &BufferGeometry) -> Option<crate::math::Sphere> {
if let Some(s) = g.bounding_sphere {
return Some(s);
}
if g.gpu_writable {
return None;
}
let (mut lo, mut hi) = (Vector3::new(f32::MAX, f32::MAX, f32::MAX), Vector3::new(f32::MIN, f32::MIN, f32::MIN));
let mut any = false;
for p in g.positions()? {
lo = Vector3::new(lo.x.min(p.x), lo.y.min(p.y), lo.z.min(p.z));
hi = Vector3::new(hi.x.max(p.x), hi.y.max(p.y), hi.z.max(p.z));
any = true;
}
if !any {
return None;
}
let centre = (lo + hi) * 0.5;
let r = g.positions()?.map(|p| (p - centre).length_sq()).fold(0.0f32, f32::max).sqrt();
Some(crate::math::Sphere::new(centre, r))
}
fn shadow_fingerprint(items: &[DrawItem]) -> u64 {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
let mut mix = |x: u64| h = (h.rotate_left(5) ^ x).wrapping_mul(0x517c_c1b7_2722_0a95);
let floats = |mix: &mut dyn FnMut(u64), v: &[f32]| {
mix(v.len() as u64);
v.iter().for_each(|x| mix(x.to_bits() as u64));
};
for item in items.iter().filter(|i| i.cast_shadow && i.primitive == primitive::TRIANGLE) {
mix(Arc::as_ptr(&item.geometry) as usize as u64);
mix(item.geometry.geometry_version as u64);
item.world.elements.iter().for_each(|v| mix(v.to_bits() as u64));
let m = &item.material;
mix(m.side() as u64);
mix(m.alpha_test().to_bits() as u64);
let slots = m.texture_slots();
mix(slots.map.as_ref().map_or(0, |t| Arc::as_ptr(t) as usize as u64));
mix(slots.alpha_map.as_ref().map_or(0, |t| Arc::as_ptr(t) as usize as u64));
mix(((item.range.0 as u64) << 32) | item.range.1 as u64);
mix(item.instance_count as u64);
floats(&mut mix, item.instances.as_deref().unwrap_or(&[]));
floats(&mut mix, item.instance_morph.as_ref().map_or(&[][..], |(rows, _)| rows));
floats(&mut mix, item.bones.as_deref().unwrap_or(&[]));
floats(&mut mix, &item.morph_influences);
}
h
}
fn mirror_clip_x(m: Matrix4) -> Matrix4 {
let mut e = m.elements;
e[0] = -e[0];
e[4] = -e[4];
e[8] = -e[8];
e[12] = -e[12];
Matrix4 { elements: e }
}
fn build_skin(geometry: &BufferGeometry, vertex_count: usize) -> Vec<SkinVertex> {
use crate::core::BufferAttribute;
let joints = geometry.get_attribute("joint");
let weights = geometry.get_attribute("weight");
(0..vertex_count)
.map(|i| {
let take = |a: Option<&BufferAttribute>, fallback: [f32; 4]| -> [f32; 4] {
match a {
Some(a) if (i + 1) * 4 <= a.array.len() => {
[a.array[i * 4], a.array[i * 4 + 1], a.array[i * 4 + 2], a.array[i * 4 + 3]]
}
_ => fallback,
}
};
SkinVertex {
joints: take(joints, [0.0; 4]),
weights: take(weights, [1.0, 0.0, 0.0, 0.0]),
}
})
.collect()
}
fn morph_uniforms(
geometry: &BufferGeometry,
morph: Option<&MorphBuffer>,
influences: &[f32],
) -> MorphUniforms {
let (Some(m), Some((header, weights))) =
(morph, crate::core::morph_uniforms(geometry, influences))
else {
return MorphUniforms::default();
};
let mut u = MorphUniforms {
info: [header[0] as u32, m.stride, m.flags, m.vertex_count],
base: [header[1], 0.0, 0.0, 0.0],
weights: [[0.0; 4]; 16],
};
for (i, w) in weights.iter().enumerate() {
u.weights[i / 4][i % 4] = *w;
}
u
}
fn item_morph_uniforms(item: &DrawItem, morph: Option<&MorphBuffer>) -> MorphUniforms {
let Some((row_len, offset)) = item.instance_morph_layout else {
return morph_uniforms(&item.geometry, morph, &item.morph_influences);
};
let (Some(m), Some(header)) = (morph, crate::core::morph_header(&item.geometry)) else {
return MorphUniforms::default();
};
MorphUniforms {
info: [header[0] as u32, m.stride, m.flags | 16, m.vertex_count],
base: [0.0, row_len as f32, offset as f32, 0.0],
weights: [[0.0; 4]; 16],
}
}
fn texture_key(texture: &Arc<Texture>, linear: bool) -> usize {
(Arc::as_ptr(texture) as usize) | linear as usize
}
struct SamplerPool {
ids: Vec<Id>,
index: [u32; MAP_COUNT],
}
impl SamplerPool {
fn new(base: Id) -> Self {
Self { ids: vec![base], index: [0; MAP_COUNT] }
}
fn set(&mut self, map: usize, sampler: Id) {
let i = match self.ids.iter().position(|&s| s == sampler) {
Some(i) => i,
None if self.ids.len() < SAMPLER_POOL => {
self.ids.push(sampler);
self.ids.len() - 1
}
None => 0,
};
self.index[map] = i as u32;
}
fn slots(&self) -> [Id; SAMPLER_POOL] {
std::array::from_fn(|i| self.ids.get(i).copied().unwrap_or(self.ids[0]))
}
fn packed(&self) -> [[u32; 4]; 6] {
std::array::from_fn(|v| std::array::from_fn(|k| self.index.get(4 * v + k).copied().unwrap_or(0)))
}
}
fn draw_uniforms(
item: &DrawItem,
has_map: bool,
instanced: bool,
geometry: &BufferGeometry,
) -> DrawUniforms {
let material = &item.material;
let color = material.color();
let emissive = material.emissive();
let specular = material.specular();
let (depth_near, depth_far) = material.depth_range();
let slots = material.texture_slots();
let normal_mat = match item.sprite {
Some(p) => {
let mut m = [0.0f32; 16];
m[..4].copy_from_slice(&p);
m
}
None => normal_matrix(&item.world),
};
let is_sprite = item.sprite.is_some() as u32;
DrawUniforms {
model: item.world.elements,
normal_mat,
base_color: [color.r, color.g, color.b, material.opacity()],
emissive: [emissive.r, emissive.g, emissive.b, 0.0],
specular: [specular.r, specular.g, specular.b, material.shininess()],
pbr: [
material.roughness(),
material.metalness(),
material.alpha_test(),
material.texture_slots().gradient_map.is_some() as u32 as f32,
],
extra: [0; 4],
map_scalars: material.map_uniforms()[0],
env_params: material.map_uniforms()[1],
env_dir: {
let u = material.map_uniforms();
[u[2], u[3], u[4]]
},
map_xform: map_xforms(&slots),
planet: {
let atmos = material.atmosphere_params();
let (eclipse, sun_radius) = material.eclipse();
let scatter = material.scatter_params();
let twilight = material.twilight_color();
let cloud_map = slots.cloud_shadow_map.is_some() && atmos[1] > 0.0;
[
[material.emissive_night_side(), sun_radius, atmos[3], scatter[0]],
[twilight.r, twilight.g, twilight.b, scatter[1]],
[atmos[0], atmos[1], atmos[2], cloud_map as u32 as f32],
eclipse,
{
let (scale, bias) = material.displacement();
[material.normal_scale().y, scale, bias, slots.displacement_map.is_some() as u32 as f32]
},
]
},
clip: item.clip,
map_samplers: [[0; 4]; 6],
phys_flags: [0; 4],
physical: match &**material {
Material::Physical(m) => [
[m.ior, m.specular_intensity, m.clearcoat, m.clearcoat_roughness],
[m.specular_color.r, m.specular_color.g, m.specular_color.b, m.sheen],
[m.sheen_color.r, m.sheen_color.g, m.sheen_color.b, m.sheen_roughness],
[
m.iridescence,
m.iridescence_ior,
m.iridescence_thickness_range[0],
m.iridescence_thickness_range[1],
],
[m.anisotropy, m.anisotropy_rotation, m.clearcoat_normal_scale.x, m.clearcoat_normal_scale.y],
[
m.transmission,
m.thickness,
if m.attenuation_distance.is_finite() { m.attenuation_distance.max(0.0) } else { 0.0 },
m.dispersion,
],
[m.attenuation_color.r, m.attenuation_color.g, m.attenuation_color.b, 0.0],
],
Material::Atmosphere(m) => [
[m.planet_radius, m.atmosphere_radius, m.intensity, m.falloff],
[m.sunset_color.r, m.sunset_color.g, m.sunset_color.b, m.airglow],
material.scatter_params(),
material.aurora_params(),
[0.0; 4],
[0.0; 4],
[0.0; 4],
],
_ => [[0.0; 4]; 7],
},
maps: [0; 4],
object: [
item.receive_shadow as u32,
is_sprite,
material.shadow_only() as u32,
material.tone_mapped() as u32,
],
map_params: [
material.normal_scale().x,
material.ao_intensity(),
1.0,
0.0,
],
misc: [
0.0,
material.point_size(),
if material.point_size_attenuation() {
1.0
} else {
0.0
},
0.0,
],
flags: [
material.kind() as u32,
has_map as u32,
geometry.attributes.contains_key("color") as u32,
if instanced { 1 | (item.instance_color_offset << 1) } else { 0 },
],
}
.with_depth_range(depth_near, depth_far)
}
impl DrawUniforms {
fn with_depth_range(mut self, near: f32, far: f32) -> Self {
if self.flags[0] == MaterialKind::Depth as u32
|| self.flags[0] == MaterialKind::Distance as u32
{
self.misc[2] = near;
self.misc[3] = far;
}
self
}
}
pub(crate) fn build_vertices(geometry: &BufferGeometry) -> Vec<Vertex> {
let Some(positions) = geometry.attributes.get("position") else {
return Vec::new();
};
if positions.item_size < 3 {
return Vec::new();
}
let count = positions.array.len() / positions.item_size;
let normals = geometry
.attributes
.get("normal")
.filter(|a| a.item_size >= 3);
let uvs = geometry.attributes.get("uv").filter(|a| a.item_size >= 2);
let colors = geometry
.attributes
.get("color")
.filter(|a| a.item_size >= 3);
let derived = if normals.is_none() {
Some(derive_normals(geometry, count))
} else {
None
};
let mut out = Vec::with_capacity(count);
for i in 0..count {
let p = positions.item_size * i;
let normal = match (normals, &derived) {
(Some(n), _) => {
let j = n.item_size * i;
[n.array[j], n.array[j + 1], n.array[j + 2]]
}
(None, Some(d)) => d[i],
(None, None) => [0.0, 0.0, 1.0],
};
let uv = uvs
.map(|a| {
let j = a.item_size * i;
[a.array[j], a.array[j + 1]]
})
.unwrap_or([0.0, 0.0]);
let color = colors
.map(|a| {
let j = a.item_size * i;
[a.array[j], a.array[j + 1], a.array[j + 2]]
})
.unwrap_or([1.0, 1.0, 1.0]);
let alpha = colors
.filter(|a| a.item_size >= 4)
.map(|a| a.array[a.item_size * i + 3])
.unwrap_or(1.0);
out.push(Vertex {
position: [
positions.array[p],
positions.array[p + 1],
positions.array[p + 2],
],
normal,
uv,
color,
alpha,
});
}
out
}
pub(crate) fn build_line_vertices(geometry: &BufferGeometry) -> Vec<LineVertex> {
let Some(positions) = geometry.attributes.get("position") else {
return Vec::new();
};
if positions.item_size < 3 {
return Vec::new();
}
let count = positions.array.len() / positions.item_size;
let colors = geometry
.attributes
.get("color")
.filter(|a| a.item_size >= 3);
let mut out = Vec::with_capacity(count);
for i in 0..count {
let p = positions.item_size * i;
let color = colors
.map(|a| {
let j = a.item_size * i;
[a.array[j], a.array[j + 1], a.array[j + 2]]
})
.unwrap_or([1.0, 1.0, 1.0]);
out.push(LineVertex {
position: [
positions.array[p],
positions.array[p + 1],
positions.array[p + 2],
],
color,
});
}
out
}
fn derive_normals(geometry: &BufferGeometry, count: usize) -> Vec<[f32; 3]> {
let mut normals = vec![[0.0f32; 3]; count];
let positions = match geometry.attributes.get("position") {
Some(p) => p,
None => return normals,
};
let at = |i: usize| {
let j = positions.item_size * i;
Vector3::new(
positions.array[j],
positions.array[j + 1],
positions.array[j + 2],
)
};
let mut accumulate = |a: usize, b: usize, c: usize| {
if a >= count || b >= count || c >= count {
return;
}
let n = (at(b) - at(a)).cross(at(c) - at(a));
for i in [a, b, c] {
normals[i][0] += n.x;
normals[i][1] += n.y;
normals[i][2] += n.z;
}
};
match &geometry.index {
Some(idx) => {
for tri in idx.as_chunks::<3>().0 {
accumulate(tri[0] as usize, tri[1] as usize, tri[2] as usize);
}
}
None => {
for t in 0..count / 3 {
accumulate(t * 3, t * 3 + 1, t * 3 + 2);
}
}
}
for n in &mut normals {
let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
if len > 1e-12 {
*n = [n[0] / len, n[1] / len, n[2] / len];
} else {
*n = [0.0, 0.0, 1.0];
}
}
normals
}
fn sprite_geometry(_sprite: &crate::core::Sprite) -> Option<Arc<BufferGeometry>> {
static QUAD: std::sync::OnceLock<Arc<BufferGeometry>> = std::sync::OnceLock::new();
Some(
QUAD.get_or_init(|| Arc::new(crate::geometries::PlaneGeometry::new(1.0, 1.0)))
.clone(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::BufferAttribute;
#[test]
fn uniform_structs_match_the_shader() {
assert_eq!(std::mem::size_of::<Vertex>(), 48);
assert_eq!(std::mem::size_of::<DirLightGpu>(), 32);
assert_eq!(std::mem::size_of::<PointLightGpu>(), 48);
assert_eq!(std::mem::size_of::<SpotLightGpu>(), 64);
assert_eq!(std::mem::size_of::<HemiLightGpu>(), 48);
assert_eq!(
std::mem::size_of::<DrawUniforms>(),
64 + 64 + 9 * 16 + 6 * 16 + 6 * 16 + 7 * 16 + 16 + 2 * MAP_COUNT * 16 + 5 * 16 + 16
);
assert_eq!(
std::mem::size_of::<FrameUniforms>(),
MAX_VIEWS * (64 + 64 + 16)
+ 16 * 6
+ MAX_DIR_LIGHTS * 32
+ MAX_POINT_LIGHTS * 48
+ MAX_SPOT_LIGHTS * 64
+ MAX_HEMI_LIGHTS * 48
+ 16 + MAX_RECT_LIGHTS * 64 + 16 + MAX_SHADOW_RECORDS * (4 * 16 + 4 * 64) );
assert_eq!(std::mem::size_of::<ShadowPassGpu>(), 64 + 16);
assert!(std::mem::size_of::<DrawUniforms>() <= 4096);
}
fn triangle() -> BufferGeometry {
let mut g = BufferGeometry::new();
g.set_attribute(
"position",
BufferAttribute::new(vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0], 3),
);
g
}
#[test]
fn missing_normals_are_derived() {
let verts = build_vertices(&triangle());
assert_eq!(verts.len(), 3);
for v in &verts {
assert!((v.normal[2] - 1.0).abs() < 1e-5, "{:?}", v.normal);
}
}
#[test]
fn attributes_interleave_in_order() {
let mut g = triangle();
g.set_attribute(
"normal",
BufferAttribute::new(vec![0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0], 3),
);
g.set_attribute(
"uv",
BufferAttribute::new(vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0], 2),
);
g.set_attribute(
"color",
BufferAttribute::new(vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], 3),
);
let verts = build_vertices(&g);
assert_eq!(verts[1].position, [1.0, 0.0, 0.0]);
assert_eq!(verts[1].normal, [0.0, 1.0, 0.0]);
assert_eq!(verts[1].uv, [1.0, 0.0]);
assert_eq!(verts[2].color, [0.0, 0.0, 1.0]);
}
#[test]
fn geometry_without_positions_is_skipped() {
assert!(build_vertices(&BufferGeometry::new()).is_empty());
assert!(build_line_vertices(&BufferGeometry::new()).is_empty());
}
#[test]
fn line_vertices_are_half_the_size_and_carry_position_and_colour() {
assert_eq!(std::mem::size_of::<LineVertex>(), 24);
assert_eq!(std::mem::size_of::<Vertex>(), 48);
let mut g = triangle();
g.set_attribute(
"color",
BufferAttribute::new(vec![1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], 3),
);
let verts = build_line_vertices(&g);
assert_eq!(verts.len(), 3);
assert_eq!(verts[1].position, [1.0, 0.0, 0.0]);
assert_eq!(verts[2].color, [0.0, 0.0, 1.0]);
}
#[test]
fn line_vertices_default_to_white_without_a_colour_attribute() {
let verts = build_line_vertices(&triangle());
assert_eq!(verts[0].color, [1.0, 1.0, 1.0]);
}
#[test]
fn normal_matrix_is_inverse_transpose() {
let m = Matrix4::compose(
Vector3::new(1.0, 2.0, 3.0),
crate::math::Quaternion::identity(),
Vector3::new(2.0, 1.0, 0.5),
);
let n = normal_matrix(&m);
assert!((n[0] - 0.5).abs() < 1e-5, "{}", n[0]);
assert!((n[5] - 1.0).abs() < 1e-5, "{}", n[5]);
assert!((n[10] - 2.0).abs() < 1e-5, "{}", n[10]);
}
#[test]
fn transparency_follows_three_js() {
use crate::materials::{BasicMaterial, StandardMaterial};
use crate::math::Color;
let opaque = Material::Basic(BasicMaterial::default());
assert!(!is_transparent(&opaque));
let faded = Material::Basic(BasicMaterial::default().with_opacity(0.5));
assert!(!is_transparent(&faded));
let flagged = Material::Basic(BasicMaterial::default().with_transparent(true));
assert!(is_transparent(&flagged));
let mut lit = StandardMaterial::new(Color::WHITE);
lit.opacity = 0.5;
assert!(is_transparent(&Material::Standard(lit.clone())));
lit.transparent = Some(false);
assert!(!is_transparent(&Material::Standard(lit)));
}
}