| 1 |
// Copyright 2016 GFX developers |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
| 4 |
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 |
// http://opensource.org/licenses/MIT>, at your option. This file may not be |
| 6 |
// copied, modified, or distributed except according to those terms. |
| 7 |
|
| 8 |
use super::*; |
| 9 |
|
| 10 |
/// Only available on macos(10.15), ios(13.0) |
| 11 |
/// |
| 12 |
/// See <https://developer.apple.com/documentation/metal/mtlheaptype/> |
| 13 |
#[repr(u64)] |
| 14 |
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] |
| 15 |
pub enum MTLHeapType { |
| 16 |
Automatic = 0, |
| 17 |
Placement = 1, |
| 18 |
/// Only available on macos(11.0), macCatalyst(14.0) |
| 19 |
Sparse = 2, |
| 20 |
} |
| 21 |
|
| 22 |
/// See <https://developer.apple.com/documentation/metal/mtlheap/> |
| 23 |
pub enum MTLHeap {} |
| 24 |
|
| 25 |
foreign_obj_type! { |
| 26 |
type CType = MTLHeap; |
| 27 |
pub struct Heap; |
| 28 |
} |
| 29 |
|
| 30 |
impl HeapRef { |
| 31 |
pub fn device(&self) -> &DeviceRef { |
| 32 |
unsafe { msg_send![self, device] } |
| 33 |
} |
| 34 |
|
| 35 |
pub fn label(&self) -> &str { |
| 36 |
unsafe { |
| 37 |
let label = msg_send![self, label]; |
| 38 |
crate::nsstring_as_str(label) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
pub fn set_label(&self, label: &str) { |
| 43 |
unsafe { |
| 44 |
let nslabel = crate::nsstring_from_str(label); |
| 45 |
let () = msg_send![self, setLabel: nslabel]; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode { |
| 50 |
unsafe { msg_send![self, cpuCacheMode] } |
| 51 |
} |
| 52 |
|
| 53 |
pub fn storage_mode(&self) -> MTLStorageMode { |
| 54 |
unsafe { msg_send![self, storageMode] } |
| 55 |
} |
| 56 |
|
| 57 |
/// Only available on macos(10.15), ios(13.0) |
| 58 |
pub fn hazard_tracking_mode(&self) -> MTLHazardTrackingMode { |
| 59 |
unsafe { msg_send![self, hazardTrackingMode] } |
| 60 |
} |
| 61 |
|
| 62 |
/// Only available on macos(10.15), ios(13.0) |
| 63 |
pub fn resource_options(&self) -> MTLResourceOptions { |
| 64 |
unsafe { msg_send![self, resourceOptions] } |
| 65 |
} |
| 66 |
|
| 67 |
pub fn set_purgeable_state(&self, state: MTLPurgeableState) -> MTLPurgeableState { |
| 68 |
unsafe { msg_send![self, setPurgeableState: state] } |
| 69 |
} |
| 70 |
|
| 71 |
pub fn size(&self) -> NSUInteger { |
| 72 |
unsafe { msg_send![self, size] } |
| 73 |
} |
| 74 |
|
| 75 |
pub fn used_size(&self) -> NSUInteger { |
| 76 |
unsafe { msg_send![self, usedSize] } |
| 77 |
} |
| 78 |
|
| 79 |
/// Only available on macos(10.15), ios(13.0) |
| 80 |
pub fn heap_type(&self) -> MTLHeapType { |
| 81 |
unsafe { msg_send![self, type] } |
| 82 |
} |
| 83 |
|
| 84 |
/// Only available on macos(10.13), ios(11.0) |
| 85 |
pub fn current_allocated_size(&self) -> NSUInteger { |
| 86 |
unsafe { msg_send![self, currentAllocatedSize] } |
| 87 |
} |
| 88 |
|
| 89 |
pub fn max_available_size_with_alignment(&self, alignment: NSUInteger) -> NSUInteger { |
| 90 |
unsafe { msg_send![self, maxAvailableSizeWithAlignment: alignment] } |
| 91 |
} |
| 92 |
|
| 93 |
pub fn new_buffer(&self, length: u64, options: MTLResourceOptions) -> Option<Buffer> { |
| 94 |
unsafe { |
| 95 |
let ptr: *mut MTLBuffer = msg_send![self, newBufferWithLength:length |
| 96 |
options:options]; |
| 97 |
if !ptr.is_null() { |
| 98 |
Some(Buffer::from_ptr(ptr)) |
| 99 |
} else { |
| 100 |
None |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pub fn new_texture(&self, descriptor: &TextureDescriptorRef) -> Option<Texture> { |
| 106 |
unsafe { |
| 107 |
let ptr: *mut MTLTexture = msg_send![self, newTextureWithDescriptor: descriptor]; |
| 108 |
if !ptr.is_null() { |
| 109 |
Some(Texture::from_ptr(ptr)) |
| 110 |
} else { |
| 111 |
None |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/// Only available on macOS 10.15+ & iOS 13.0+ |
| 117 |
pub fn new_buffer_with_offset( |
| 118 |
&self, |
| 119 |
length: u64, |
| 120 |
options: MTLResourceOptions, |
| 121 |
offset: u64, |
| 122 |
) -> Option<Buffer> { |
| 123 |
unsafe { |
| 124 |
let ptr: *mut MTLBuffer = msg_send![self, newBufferWithLength:length |
| 125 |
options:options |
| 126 |
offset:offset]; |
| 127 |
if !ptr.is_null() { |
| 128 |
Some(Buffer::from_ptr(ptr)) |
| 129 |
} else { |
| 130 |
None |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/// Only available on macOS 10.15+ & iOS 13.0+ |
| 136 |
pub fn new_texture_with_offset( |
| 137 |
&self, |
| 138 |
descriptor: &TextureDescriptorRef, |
| 139 |
offset: u64, |
| 140 |
) -> Option<Texture> { |
| 141 |
unsafe { |
| 142 |
let ptr: *mut MTLTexture = msg_send![self, newTextureWithDescriptor:descriptor |
| 143 |
offset:offset]; |
| 144 |
if !ptr.is_null() { |
| 145 |
Some(Texture::from_ptr(ptr)) |
| 146 |
} else { |
| 147 |
None |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/// Only available on macOS 13.0+ & iOS 16.0+ |
| 153 |
pub fn new_acceleration_structure_with_descriptor( |
| 154 |
&self, |
| 155 |
descriptor: &AccelerationStructureDescriptorRef, |
| 156 |
) -> Option<AccelerationStructure> { |
| 157 |
unsafe { |
| 158 |
let ptr: *mut MTLAccelerationStructure = |
| 159 |
msg_send![self, newAccelerationStructureWithDescriptor: descriptor]; |
| 160 |
if !ptr.is_null() { |
| 161 |
Some(AccelerationStructure::from_ptr(ptr)) |
| 162 |
} else { |
| 163 |
None |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/// Only available on macOS 13.0+ & iOS 16.0+ |
| 169 |
pub fn new_acceleration_structure_with_descriptor_offset( |
| 170 |
&self, |
| 171 |
descriptor: &AccelerationStructureDescriptorRef, |
| 172 |
offset: u64, |
| 173 |
) -> Option<AccelerationStructure> { |
| 174 |
unsafe { |
| 175 |
let ptr: *mut MTLAccelerationStructure = msg_send![self, newAccelerationStructureWithDescriptor:descriptor |
| 176 |
offset:offset]; |
| 177 |
if !ptr.is_null() { |
| 178 |
Some(AccelerationStructure::from_ptr(ptr)) |
| 179 |
} else { |
| 180 |
None |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/// Only available on macOS 13.0+ & iOS 16.0+ |
| 186 |
pub fn new_acceleration_structure_with_size(&self, size: u64) -> Option<AccelerationStructure> { |
| 187 |
unsafe { |
| 188 |
let ptr: *mut MTLAccelerationStructure = |
| 189 |
msg_send![self, newAccelerationStructureWithSize:size]; |
| 190 |
if !ptr.is_null() { |
| 191 |
Some(AccelerationStructure::from_ptr(ptr)) |
| 192 |
} else { |
| 193 |
None |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/// Only available on macOS 13.0+ & iOS 16.0+ |
| 199 |
pub fn new_acceleration_structure_with_size_offset( |
| 200 |
&self, |
| 201 |
size: u64, |
| 202 |
offset: u64, |
| 203 |
) -> Option<AccelerationStructure> { |
| 204 |
unsafe { |
| 205 |
let ptr: *mut MTLAccelerationStructure = msg_send![self, newAccelerationStructureWithSize:size |
| 206 |
offset:offset]; |
| 207 |
if !ptr.is_null() { |
| 208 |
Some(AccelerationStructure::from_ptr(ptr)) |
| 209 |
} else { |
| 210 |
None |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/// See <https://developer.apple.com/documentation/metal/mtlheapdescriptor/> |
| 217 |
pub enum MTLHeapDescriptor {} |
| 218 |
|
| 219 |
foreign_obj_type! { |
| 220 |
type CType = MTLHeapDescriptor; |
| 221 |
pub struct HeapDescriptor; |
| 222 |
} |
| 223 |
|
| 224 |
impl HeapDescriptor { |
| 225 |
pub fn new() -> Self { |
| 226 |
unsafe { |
| 227 |
let class = class!(MTLHeapDescriptor); |
| 228 |
msg_send![class, new] |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
impl HeapDescriptorRef { |
| 234 |
pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode { |
| 235 |
unsafe { msg_send![self, cpuCacheMode] } |
| 236 |
} |
| 237 |
|
| 238 |
pub fn set_cpu_cache_mode(&self, mode: MTLCPUCacheMode) { |
| 239 |
unsafe { msg_send![self, setCpuCacheMode: mode] } |
| 240 |
} |
| 241 |
|
| 242 |
pub fn storage_mode(&self) -> MTLStorageMode { |
| 243 |
unsafe { msg_send![self, storageMode] } |
| 244 |
} |
| 245 |
|
| 246 |
pub fn set_storage_mode(&self, mode: MTLStorageMode) { |
| 247 |
unsafe { msg_send![self, setStorageMode: mode] } |
| 248 |
} |
| 249 |
|
| 250 |
pub fn size(&self) -> NSUInteger { |
| 251 |
unsafe { msg_send![self, size] } |
| 252 |
} |
| 253 |
|
| 254 |
pub fn set_size(&self, size: NSUInteger) { |
| 255 |
unsafe { msg_send![self, setSize: size] } |
| 256 |
} |
| 257 |
|
| 258 |
/// Only available on macos(10.15), ios(13.0) |
| 259 |
pub fn hazard_tracking_mode(&self) -> MTLHazardTrackingMode { |
| 260 |
unsafe { msg_send![self, hazardTrackingMode] } |
| 261 |
} |
| 262 |
|
| 263 |
/// Only available on macos(10.15), ios(13.0) |
| 264 |
pub fn set_hazard_tracking_mode(&self, hazard_tracking_mode: MTLHazardTrackingMode) { |
| 265 |
unsafe { msg_send![self, setHazardTrackingMode: hazard_tracking_mode] } |
| 266 |
} |
| 267 |
|
| 268 |
/// Only available on macos(10.15), ios(13.0) |
| 269 |
pub fn resource_options(&self) -> MTLResourceOptions { |
| 270 |
unsafe { msg_send![self, resourceOptions] } |
| 271 |
} |
| 272 |
|
| 273 |
/// Only available on macos(10.15), ios(13.0) |
| 274 |
pub fn heap_type(&self) -> MTLHeapType { |
| 275 |
unsafe { msg_send![self, type] } |
| 276 |
} |
| 277 |
/// Only available on macos(10.15), ios(13.0) |
| 278 |
pub fn set_heap_type(&self, type_: MTLHeapType) { |
| 279 |
unsafe { msg_send![self, setType: type_] } |
| 280 |
} |
| 281 |
} |