use objc2::{Message, extern_protocol, msg_send, rc::Retained, runtime::ProtocolObject};
use objc2_foundation::NSString;
use crate::{
MTLAccelerationStructure, MTLAccelerationStructureDescriptor, MTLAllocation, MTLBuffer, MTLCPUCacheMode,
MTLHazardTrackingMode, MTLPurgeableState, MTLResourceOptions, MTLStorageMode, MTLTexture, MTLTextureDescriptor,
};
extern_protocol!(
pub unsafe trait MTLHeap: MTLAllocation {
#[unsafe(method(device))]
#[unsafe(method_family = none)]
fn device(&self) -> Retained<ProtocolObject<dyn crate::MTLDevice>>;
#[unsafe(method(storageMode))]
#[unsafe(method_family = none)]
fn storage_mode(&self) -> MTLStorageMode;
#[unsafe(method(cpuCacheMode))]
#[unsafe(method_family = none)]
fn cpu_cache_mode(&self) -> MTLCPUCacheMode;
#[unsafe(method(hazardTrackingMode))]
#[unsafe(method_family = none)]
fn hazard_tracking_mode(&self) -> MTLHazardTrackingMode;
#[unsafe(method(resourceOptions))]
#[unsafe(method_family = none)]
fn resource_options(&self) -> MTLResourceOptions;
#[unsafe(method(size))]
#[unsafe(method_family = none)]
fn size(&self) -> usize;
#[unsafe(method(usedSize))]
#[unsafe(method_family = none)]
fn used_size(&self) -> usize;
#[unsafe(method(currentAllocatedSize))]
#[unsafe(method_family = none)]
fn current_allocated_size(&self) -> usize;
#[unsafe(method(maxAvailableSizeWithAlignment:))]
#[unsafe(method_family = none)]
fn max_available_size_with_alignment(
&self,
alignment: usize,
) -> usize;
#[unsafe(method(newBufferWithLength:options:))]
#[unsafe(method_family = new)]
fn new_buffer(
&self,
length: usize,
options: MTLResourceOptions,
) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>>;
#[unsafe(method(newTextureWithDescriptor:))]
#[unsafe(method_family = new)]
fn new_texture(
&self,
descriptor: &MTLTextureDescriptor,
) -> Option<Retained<ProtocolObject<dyn MTLTexture>>>;
#[unsafe(method(setPurgeableState:))]
#[unsafe(method_family = none)]
fn set_purgeable_state(
&self,
state: MTLPurgeableState,
) -> MTLPurgeableState;
#[unsafe(method(type))]
#[unsafe(method_family = none)]
fn r#type(&self) -> super::MTLHeapType;
#[unsafe(method(newBufferWithLength:options:offset:))]
#[unsafe(method_family = new)]
fn new_buffer_with_offset(
&self,
length: usize,
options: MTLResourceOptions,
offset: usize,
) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>>;
#[unsafe(method(newTextureWithDescriptor:offset:))]
#[unsafe(method_family = new)]
fn new_texture_with_offset(
&self,
descriptor: &MTLTextureDescriptor,
offset: usize,
) -> Option<Retained<ProtocolObject<dyn MTLTexture>>>;
#[unsafe(method(newAccelerationStructureWithSize:))]
#[unsafe(method_family = new)]
fn new_acceleration_structure_with_size(
&self,
size: usize,
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
#[unsafe(method(newAccelerationStructureWithDescriptor:))]
#[unsafe(method_family = new)]
fn new_acceleration_structure_with_descriptor(
&self,
descriptor: &MTLAccelerationStructureDescriptor,
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
#[unsafe(method(newAccelerationStructureWithSize:offset:))]
#[unsafe(method_family = new)]
fn new_acceleration_structure_with_size_offset(
&self,
size: usize,
offset: usize,
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
#[unsafe(method(newAccelerationStructureWithDescriptor:offset:))]
#[unsafe(method_family = new)]
fn new_acceleration_structure_with_descriptor_offset(
&self,
descriptor: &MTLAccelerationStructureDescriptor,
offset: usize,
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
}
);
#[allow(unused)]
pub trait MTLHeapExt: MTLHeap + Message {
fn label(&self) -> Option<String>;
fn set_label(
&self,
label: Option<&str>,
);
}
impl MTLHeapExt for ProtocolObject<dyn MTLHeap> {
fn label(&self) -> Option<String> {
let label: Option<Retained<NSString>> = unsafe { msg_send![self, label] };
label.map(|s| s.to_string())
}
fn set_label(
&self,
label: Option<&str>,
) {
unsafe {
let _: () = msg_send![self, setLabel: label.map(NSString::from_str).as_deref()];
}
}
}