| 1 |
// Copyright 2017 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 |
use objc::runtime::{NO, YES}; |
| 11 |
|
| 12 |
/// See <https://developer.apple.com/documentation/metal/mtlattributeformat> |
| 13 |
#[repr(u64)] |
| 14 |
#[allow(non_camel_case_types)] |
| 15 |
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] |
| 16 |
pub enum MTLAttributeFormat { |
| 17 |
Invalid = 0, |
| 18 |
UChar2 = 1, |
| 19 |
UChar3 = 2, |
| 20 |
UChar4 = 3, |
| 21 |
Char2 = 4, |
| 22 |
Char3 = 5, |
| 23 |
Char4 = 6, |
| 24 |
UChar2Normalized = 7, |
| 25 |
UChar3Normalized = 8, |
| 26 |
UChar4Normalized = 9, |
| 27 |
Char2Normalized = 10, |
| 28 |
Char3Normalized = 11, |
| 29 |
Char4Normalized = 12, |
| 30 |
UShort2 = 13, |
| 31 |
UShort3 = 14, |
| 32 |
UShort4 = 15, |
| 33 |
Short2 = 16, |
| 34 |
Short3 = 17, |
| 35 |
Short4 = 18, |
| 36 |
UShort2Normalized = 19, |
| 37 |
UShort3Normalized = 20, |
| 38 |
UShort4Normalized = 21, |
| 39 |
Short2Normalized = 22, |
| 40 |
Short3Normalized = 23, |
| 41 |
Short4Normalized = 24, |
| 42 |
Half2 = 25, |
| 43 |
Half3 = 26, |
| 44 |
Half4 = 27, |
| 45 |
Float = 28, |
| 46 |
Float2 = 29, |
| 47 |
Float3 = 30, |
| 48 |
Float4 = 31, |
| 49 |
Int = 32, |
| 50 |
Int2 = 33, |
| 51 |
Int3 = 34, |
| 52 |
Int4 = 35, |
| 53 |
UInt = 36, |
| 54 |
UInt2 = 37, |
| 55 |
UInt3 = 38, |
| 56 |
UInt4 = 39, |
| 57 |
Int1010102Normalized = 40, |
| 58 |
UInt1010102Normalized = 41, |
| 59 |
UChar4Normalized_BGRA = 42, |
| 60 |
UChar = 45, |
| 61 |
Char = 46, |
| 62 |
UCharNormalized = 47, |
| 63 |
CharNormalized = 48, |
| 64 |
UShort = 49, |
| 65 |
Short = 50, |
| 66 |
UShortNormalized = 51, |
| 67 |
ShortNormalized = 52, |
| 68 |
Half = 53, |
| 69 |
} |
| 70 |
|
| 71 |
/// See <https://developer.apple.com/documentation/metal/mtlstepfunction> |
| 72 |
#[repr(u64)] |
| 73 |
#[allow(non_camel_case_types)] |
| 74 |
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] |
| 75 |
pub enum MTLStepFunction { |
| 76 |
Constant = 0, |
| 77 |
PerInstance = 1, |
| 78 |
PerPatch = 2, |
| 79 |
PerPatchControlPoint = 3, |
| 80 |
PerVertex = 4, |
| 81 |
ThreadPositionInGridX = 5, |
| 82 |
ThreadPositionInGridXIndexed = 6, |
| 83 |
ThreadPositionInGridY = 7, |
| 84 |
ThreadPositionInGridYIndexed = 8, |
| 85 |
} |
| 86 |
|
| 87 |
/// See <https://developer.apple.com/documentation/metal/mtlcomputepipelinedescriptor> |
| 88 |
pub enum MTLComputePipelineDescriptor {} |
| 89 |
|
| 90 |
foreign_obj_type! { |
| 91 |
type CType = MTLComputePipelineDescriptor; |
| 92 |
pub struct ComputePipelineDescriptor; |
| 93 |
} |
| 94 |
|
| 95 |
impl ComputePipelineDescriptor { |
| 96 |
pub fn new() -> Self { |
| 97 |
unsafe { |
| 98 |
let class = class!(MTLComputePipelineDescriptor); |
| 99 |
msg_send![class, new] |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl ComputePipelineDescriptorRef { |
| 105 |
pub fn label(&self) -> &str { |
| 106 |
unsafe { |
| 107 |
let label = msg_send![self, label]; |
| 108 |
crate::nsstring_as_str(label) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
pub fn set_label(&self, label: &str) { |
| 113 |
unsafe { |
| 114 |
let nslabel = crate::nsstring_from_str(label); |
| 115 |
let () = msg_send![self, setLabel: nslabel]; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
pub fn compute_function(&self) -> Option<&FunctionRef> { |
| 120 |
unsafe { msg_send![self, computeFunction] } |
| 121 |
} |
| 122 |
|
| 123 |
pub fn set_compute_function(&self, function: Option<&FunctionRef>) { |
| 124 |
unsafe { msg_send![self, setComputeFunction: function] } |
| 125 |
} |
| 126 |
|
| 127 |
pub fn thread_group_size_is_multiple_of_thread_execution_width(&self) -> bool { |
| 128 |
unsafe { msg_send_bool![self, threadGroupSizeIsMultipleOfThreadExecutionWidth] } |
| 129 |
} |
| 130 |
|
| 131 |
pub fn set_thread_group_size_is_multiple_of_thread_execution_width( |
| 132 |
&self, |
| 133 |
size_is_multiple_of_width: bool, |
| 134 |
) { |
| 135 |
unsafe { |
| 136 |
msg_send![ |
| 137 |
self, |
| 138 |
setThreadGroupSizeIsMultipleOfThreadExecutionWidth: size_is_multiple_of_width |
| 139 |
] |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/// API_AVAILABLE(macos(10.14), ios(12.0)); |
| 144 |
pub fn max_total_threads_per_threadgroup(&self) -> NSUInteger { |
| 145 |
unsafe { msg_send![self, maxTotalThreadsPerThreadgroup] } |
| 146 |
} |
| 147 |
|
| 148 |
/// API_AVAILABLE(macos(10.14), ios(12.0)); |
| 149 |
pub fn set_max_total_threads_per_threadgroup(&self, max_total_threads: NSUInteger) { |
| 150 |
unsafe { msg_send![self, setMaxTotalThreadsPerThreadgroup: max_total_threads] } |
| 151 |
} |
| 152 |
|
| 153 |
/// API_AVAILABLE(ios(13.0),macos(11.0)); |
| 154 |
pub fn support_indirect_command_buffers(&self) -> bool { |
| 155 |
unsafe { msg_send_bool![self, supportIndirectCommandBuffers] } |
| 156 |
} |
| 157 |
|
| 158 |
/// API_AVAILABLE(ios(13.0),macos(11.0)); |
| 159 |
pub fn set_support_indirect_command_buffers(&self, support: bool) { |
| 160 |
unsafe { msg_send![self, setSupportIndirectCommandBuffers: support] } |
| 161 |
} |
| 162 |
|
| 163 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 164 |
pub fn support_adding_binary_functions(&self) -> bool { |
| 165 |
unsafe { msg_send_bool![self, supportAddingBinaryFunctions] } |
| 166 |
} |
| 167 |
|
| 168 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 169 |
pub fn set_support_adding_binary_functions(&self, support: bool) { |
| 170 |
unsafe { msg_send![self, setSupportAddingBinaryFunctions: support] } |
| 171 |
} |
| 172 |
|
| 173 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 174 |
pub fn max_call_stack_depth(&self) -> NSUInteger { |
| 175 |
unsafe { msg_send![self, maxCallStackDepth] } |
| 176 |
} |
| 177 |
|
| 178 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 179 |
pub fn set_max_call_stack_depth(&self, depth: NSUInteger) { |
| 180 |
unsafe { msg_send![self, setMaxCallStackDepth: depth] } |
| 181 |
} |
| 182 |
|
| 183 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 184 |
/// Marshal to Rust Vec |
| 185 |
pub fn insert_libraries(&self) -> Vec<DynamicLibrary> { |
| 186 |
unsafe { |
| 187 |
let libraries: *mut Object = msg_send![self, insertLibraries]; |
| 188 |
let count: NSUInteger = msg_send![libraries, count]; |
| 189 |
(0..count) |
| 190 |
.map(|i| { |
| 191 |
let lib = msg_send![libraries, objectAtIndex: i]; |
| 192 |
DynamicLibrary::from_ptr(lib) |
| 193 |
}) |
| 194 |
.collect() |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/// Marshal from Rust slice |
| 199 |
pub fn set_insert_libraries(&self, libraries: &[&DynamicLibraryRef]) { |
| 200 |
let ns_array = Array::<DynamicLibrary>::from_slice(libraries); |
| 201 |
unsafe { msg_send![self, setInsertLibraries: ns_array] } |
| 202 |
} |
| 203 |
|
| 204 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 205 |
/// Marshal to Rust Vec |
| 206 |
pub fn binary_archives(&self) -> Vec<BinaryArchive> { |
| 207 |
unsafe { |
| 208 |
let archives: *mut Object = msg_send![self, binaryArchives]; |
| 209 |
let count: NSUInteger = msg_send![archives, count]; |
| 210 |
(0..count) |
| 211 |
.map(|i| { |
| 212 |
let a = msg_send![archives, objectAtIndex: i]; |
| 213 |
BinaryArchive::from_ptr(a) |
| 214 |
}) |
| 215 |
.collect() |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 220 |
/// Marshal from Rust slice |
| 221 |
pub fn set_binary_archives(&self, archives: &[&BinaryArchiveRef]) { |
| 222 |
let ns_array = Array::<BinaryArchive>::from_slice(archives); |
| 223 |
unsafe { msg_send![self, setBinaryArchives: ns_array] } |
| 224 |
} |
| 225 |
|
| 226 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 227 |
pub fn linked_functions(&self) -> &LinkedFunctionsRef { |
| 228 |
unsafe { msg_send![self, linkedFunctions] } |
| 229 |
} |
| 230 |
|
| 231 |
/// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 232 |
pub fn set_linked_functions(&self, functions: &LinkedFunctionsRef) { |
| 233 |
unsafe { msg_send![self, setLinkedFunctions: functions] } |
| 234 |
} |
| 235 |
|
| 236 |
pub fn stage_input_descriptor(&self) -> Option<&StageInputOutputDescriptorRef> { |
| 237 |
unsafe { msg_send![self, stageInputDescriptor] } |
| 238 |
} |
| 239 |
|
| 240 |
pub fn set_stage_input_descriptor(&self, descriptor: Option<&StageInputOutputDescriptorRef>) { |
| 241 |
unsafe { msg_send![self, setStageInputDescriptor: descriptor] } |
| 242 |
} |
| 243 |
|
| 244 |
pub fn buffers(&self) -> Option<&PipelineBufferDescriptorArrayRef> { |
| 245 |
unsafe { msg_send![self, buffers] } |
| 246 |
} |
| 247 |
|
| 248 |
pub fn reset(&self) { |
| 249 |
unsafe { msg_send![self, reset] } |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/// See <https://developer.apple.com/documentation/metal/mtlcomputepipelinestate> |
| 254 |
pub enum MTLComputePipelineState {} |
| 255 |
|
| 256 |
foreign_obj_type! { |
| 257 |
type CType = MTLComputePipelineState; |
| 258 |
pub struct ComputePipelineState; |
| 259 |
} |
| 260 |
|
| 261 |
impl ComputePipelineStateRef { |
| 262 |
pub fn label(&self) -> &str { |
| 263 |
unsafe { |
| 264 |
let label = msg_send![self, label]; |
| 265 |
crate::nsstring_as_str(label) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
pub fn max_total_threads_per_threadgroup(&self) -> NSUInteger { |
| 270 |
unsafe { msg_send![self, maxTotalThreadsPerThreadgroup] } |
| 271 |
} |
| 272 |
|
| 273 |
pub fn thread_execution_width(&self) -> NSUInteger { |
| 274 |
unsafe { msg_send![self, threadExecutionWidth] } |
| 275 |
} |
| 276 |
|
| 277 |
pub fn static_threadgroup_memory_length(&self) -> NSUInteger { |
| 278 |
unsafe { msg_send![self, staticThreadgroupMemoryLength] } |
| 279 |
} |
| 280 |
|
| 281 |
/// Only available on (ios(11.0), macos(11.0), macCatalyst(14.0)) NOT available on (tvos) |
| 282 |
pub fn imageblock_memory_length_for_dimensions(&self, dimensions: MTLSize) -> NSUInteger { |
| 283 |
unsafe { msg_send![self, imageblockMemoryLengthForDimensions: dimensions] } |
| 284 |
} |
| 285 |
|
| 286 |
/// Only available on (ios(13.0), macos(11.0)) |
| 287 |
pub fn support_indirect_command_buffers(&self) -> bool { |
| 288 |
unsafe { msg_send_bool![self, supportIndirectCommandBuffers] } |
| 289 |
} |
| 290 |
|
| 291 |
/// Only available on (macos(11.0), ios(14.0)) |
| 292 |
pub fn function_handle_with_function( |
| 293 |
&self, |
| 294 |
function: &FunctionRef, |
| 295 |
) -> Option<&FunctionHandleRef> { |
| 296 |
unsafe { msg_send![self, functionHandleWithFunction: function] } |
| 297 |
} |
| 298 |
|
| 299 |
// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 300 |
// TODO: newComputePipelineStateWithAdditionalBinaryFunctions |
| 301 |
// - (nullable id <MTLComputePipelineState>)newComputePipelineStateWithAdditionalBinaryFunctions:(nonnull NSArray<id<MTLFunction>> *)functions error:(__autoreleasing NSError **)error |
| 302 |
|
| 303 |
// API_AVAILABLE(macos(11.0), ios(14.0)); |
| 304 |
pub fn new_visible_function_table_with_descriptor( |
| 305 |
&self, |
| 306 |
descriptor: &VisibleFunctionTableDescriptorRef, |
| 307 |
) -> VisibleFunctionTable { |
| 308 |
unsafe { msg_send![self, newVisibleFunctionTableWithDescriptor: descriptor ] } |
| 309 |
} |
| 310 |
|
| 311 |
/// Only available on (macos(11.0), ios(14.0)) |
| 312 |
pub fn new_intersection_function_table_with_descriptor( |
| 313 |
&self, |
| 314 |
descriptor: &IntersectionFunctionTableDescriptorRef, |
| 315 |
) -> IntersectionFunctionTable { |
| 316 |
unsafe { msg_send![self, newIntersectionFunctionTableWithDescriptor: descriptor] } |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/// See <https://developer.apple.com/documentation/metal/mtlstageinputoutputdescriptor> |
| 321 |
pub enum MTLStageInputOutputDescriptor {} |
| 322 |
|
| 323 |
foreign_obj_type! { |
| 324 |
type CType = MTLStageInputOutputDescriptor; |
| 325 |
pub struct StageInputOutputDescriptor; |
| 326 |
} |
| 327 |
|
| 328 |
impl StageInputOutputDescriptor { |
| 329 |
pub fn new<'a>() -> &'a StageInputOutputDescriptorRef { |
| 330 |
unsafe { |
| 331 |
let class = class!(MTLStageInputOutputDescriptor); |
| 332 |
msg_send![class, stageInputOutputDescriptor] |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
impl StageInputOutputDescriptorRef { |
| 338 |
pub fn attributes(&self) -> Option<&AttributeDescriptorArrayRef> { |
| 339 |
unsafe { msg_send![self, attributes] } |
| 340 |
} |
| 341 |
|
| 342 |
pub fn index_buffer_index(&self) -> NSUInteger { |
| 343 |
unsafe { msg_send![self, indexBufferIndex] } |
| 344 |
} |
| 345 |
|
| 346 |
pub fn set_index_buffer_index(&self, idx_buffer_idx: NSUInteger) { |
| 347 |
unsafe { msg_send![self, setIndexBufferIndex: idx_buffer_idx] } |
| 348 |
} |
| 349 |
|
| 350 |
pub fn index_type(&self) -> MTLIndexType { |
| 351 |
unsafe { msg_send![self, indexType] } |
| 352 |
} |
| 353 |
|
| 354 |
pub fn set_index_type(&self, index_ty: MTLIndexType) { |
| 355 |
unsafe { msg_send![self, setIndexType: index_ty] } |
| 356 |
} |
| 357 |
|
| 358 |
pub fn layouts(&self) -> Option<&BufferLayoutDescriptorArrayRef> { |
| 359 |
unsafe { msg_send![self, layouts] } |
| 360 |
} |
| 361 |
|
| 362 |
pub fn reset(&self) { |
| 363 |
unsafe { msg_send![self, reset] } |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/// See <https://developer.apple.com/documentation/metal/mtlattributedescriptorarray> |
| 368 |
pub enum MTLAttributeDescriptorArray {} |
| 369 |
|
| 370 |
foreign_obj_type! { |
| 371 |
type CType = MTLAttributeDescriptorArray; |
| 372 |
pub struct AttributeDescriptorArray; |
| 373 |
} |
| 374 |
|
| 375 |
impl AttributeDescriptorArrayRef { |
| 376 |
pub fn object_at(&self, index: NSUInteger) -> Option<&AttributeDescriptorRef> { |
| 377 |
unsafe { msg_send![self, objectAtIndexedSubscript: index] } |
| 378 |
} |
| 379 |
|
| 380 |
pub fn set_object_at(&self, index: NSUInteger, buffer_desc: Option<&AttributeDescriptorRef>) { |
| 381 |
unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] } |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/// See <https://developer.apple.com/documentation/metal/mtlattributedescriptor> |
| 386 |
pub enum MTLAttributeDescriptor {} |
| 387 |
|
| 388 |
foreign_obj_type! { |
| 389 |
type CType = MTLAttributeDescriptor; |
| 390 |
pub struct AttributeDescriptor; |
| 391 |
} |
| 392 |
|
| 393 |
impl AttributeDescriptorRef { |
| 394 |
pub fn buffer_index(&self) -> NSUInteger { |
| 395 |
unsafe { msg_send![self, bufferIndex] } |
| 396 |
} |
| 397 |
|
| 398 |
pub fn set_buffer_index(&self, buffer_index: NSUInteger) { |
| 399 |
unsafe { msg_send![self, setBufferIndex: buffer_index] } |
| 400 |
} |
| 401 |
|
| 402 |
pub fn format(&self) -> MTLAttributeFormat { |
| 403 |
unsafe { msg_send![self, format] } |
| 404 |
} |
| 405 |
|
| 406 |
pub fn set_format(&self, format: MTLAttributeFormat) { |
| 407 |
unsafe { msg_send![self, setFormat: format] } |
| 408 |
} |
| 409 |
|
| 410 |
pub fn offset(&self) -> NSUInteger { |
| 411 |
unsafe { msg_send![self, offset] } |
| 412 |
} |
| 413 |
|
| 414 |
pub fn set_offset(&self, offset: NSUInteger) { |
| 415 |
unsafe { msg_send![self, setOffset: offset] } |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/// See <https://developer.apple.com/documentation/metal/mtlbufferlayoutdescriptorarray> |
| 420 |
pub enum MTLBufferLayoutDescriptorArray {} |
| 421 |
|
| 422 |
foreign_obj_type! { |
| 423 |
type CType = MTLBufferLayoutDescriptorArray; |
| 424 |
pub struct BufferLayoutDescriptorArray; |
| 425 |
} |
| 426 |
|
| 427 |
impl BufferLayoutDescriptorArrayRef { |
| 428 |
pub fn object_at(&self, index: NSUInteger) -> Option<&BufferLayoutDescriptorRef> { |
| 429 |
unsafe { msg_send![self, objectAtIndexedSubscript: index] } |
| 430 |
} |
| 431 |
|
| 432 |
pub fn set_object_at( |
| 433 |
&self, |
| 434 |
index: NSUInteger, |
| 435 |
buffer_desc: Option<&BufferLayoutDescriptorRef>, |
| 436 |
) { |
| 437 |
unsafe { msg_send![self, setObject:buffer_desc atIndexedSubscript:index] } |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/// See <https://developer.apple.com/documentation/metal/mtlbufferlayoutdescriptor> |
| 442 |
pub enum MTLBufferLayoutDescriptor {} |
| 443 |
|
| 444 |
foreign_obj_type! { |
| 445 |
type CType = MTLBufferLayoutDescriptor; |
| 446 |
pub struct BufferLayoutDescriptor; |
| 447 |
} |
| 448 |
|
| 449 |
impl BufferLayoutDescriptorRef { |
| 450 |
pub fn step_function(&self) -> MTLStepFunction { |
| 451 |
unsafe { msg_send![self, stepFunction] } |
| 452 |
} |
| 453 |
|
| 454 |
pub fn set_step_function(&self, step_function: MTLStepFunction) { |
| 455 |
unsafe { msg_send![self, setStepFunction: step_function] } |
| 456 |
} |
| 457 |
|
| 458 |
pub fn step_rate(&self) -> NSUInteger { |
| 459 |
unsafe { msg_send![self, stepRate] } |
| 460 |
} |
| 461 |
|
| 462 |
pub fn set_step_rate(&self, step_rate: NSUInteger) { |
| 463 |
unsafe { msg_send![self, setStepRate: step_rate] } |
| 464 |
} |
| 465 |
|
| 466 |
pub fn stride(&self) -> NSUInteger { |
| 467 |
unsafe { msg_send![self, stride] } |
| 468 |
} |
| 469 |
|
| 470 |
pub fn set_stride(&self, stride: NSUInteger) { |
| 471 |
unsafe { msg_send![self, setStride: stride] } |
| 472 |
} |
| 473 |
} |