#![deny(unsafe_op_in_unsafe_fn)]
use objc2::runtime::ProtocolObject;
use crate::metal::context::MtlContext;
use crate::metal::texture::{upload_texture, upload_texture_image};
impl MtlContext {
pub(in crate::metal) fn normal_pool_texture(
&self,
normal_map_slot: usize,
) -> &ProtocolObject<dyn objc2_metal::MTLTexture> {
if normal_map_slot == crate::gfx::render_types::NO_NORMAL_MAP_SLOT {
self.fallback_textures[0].as_ref()
} else {
let last = self.textures.len().saturating_sub(1);
self.textures[normal_map_slot.min(last)].as_ref()
}
}
pub(in crate::metal) fn albedo_pool_texture(
&self,
texture_slot: usize,
) -> &ProtocolObject<dyn objc2_metal::MTLTexture> {
if texture_slot == crate::gfx::render_types::NO_ALBEDO_SLOT {
self.fallback_textures[1].as_ref()
} else {
let last = self.textures.len().saturating_sub(1);
self.textures[texture_slot.min(last)].as_ref()
}
}
pub(crate) fn update_texture_slot(
&mut self,
slot: usize,
image: &concinnity_core::bake::texture::TextureImage,
) -> Result<(), String> {
if slot >= self.textures.len() {
return Err(format!(
"update_texture_slot: slot {} out of range (pool size {})",
slot,
self.textures.len()
));
}
self.textures[slot] = upload_texture_image(&self.allocator, image)?;
Ok(())
}
pub(crate) fn evict_texture_slot(&mut self, slot: usize) -> Result<(), String> {
if slot >= self.textures.len() {
return Err(format!(
"evict_texture_slot: slot {} out of range (pool size {})",
slot,
self.textures.len()
));
}
self.textures[slot] = upload_texture(&self.allocator, 1, 1, &[128, 128, 128, 255])?;
Ok(())
}
pub(crate) fn update_color_lut(&mut self, size: u32, data: &[u8]) -> Result<(), String> {
let tex = crate::metal::texture::upload_color_lut(&self.allocator, size, data)?;
self.color_lut = tex;
Ok(())
}
pub(crate) fn update_environment_map(&mut self, payload: &[u8]) -> Result<(), String> {
let view = crate::bake::environment_map::deserialise(payload)
.map_err(|e| format!("envmap hot-reload payload malformed: {}", e))?;
let new_env = crate::metal::texture::upload_environment_map(
&self.allocator,
view.irradiance_face,
view.irradiance_bytes,
view.prefilter_face,
&view.prefilter_mip_bytes,
)?;
self.env_map = new_env;
Ok(())
}
}