objc2_metal_kit/generated/
MTKModel.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7use objc2_metal::*;
8#[cfg(feature = "objc2-model-io")]
9use objc2_model_io::*;
10
11use crate::*;
12
13/// MTKModelErrors
14///
15/// See also [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmodelerror?language=objc)
16// NS_TYPED_ENUM
17pub type MTKModelError = NSString;
18
19extern "C" {
20    /// [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmodelerrordomain?language=objc)
21    pub static MTKModelErrorDomain: &'static MTKModelError;
22}
23
24extern "C" {
25    /// [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmodelerrorkey?language=objc)
26    pub static MTKModelErrorKey: &'static MTKModelError;
27}
28
29extern_class!(
30    /// Allocator passed to MDLAsset init method to load vertex and index data directly into Metal buffers.
31    ///
32    /// See also [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmeshbufferallocator?language=objc)
33    #[unsafe(super(NSObject))]
34    #[derive(Debug, PartialEq, Eq, Hash)]
35    pub struct MTKMeshBufferAllocator;
36);
37
38extern_conformance!(
39    unsafe impl NSObjectProtocol for MTKMeshBufferAllocator {}
40);
41
42impl MTKMeshBufferAllocator {
43    extern_methods!(
44        /// Must initialize with device
45        #[unsafe(method(init))]
46        #[unsafe(method_family = init)]
47        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
48
49        /// Initialize the allocator with a device to be used to create buffers.
50        ///
51        /// The designated initializer for this class.
52        #[unsafe(method(initWithDevice:))]
53        #[unsafe(method_family = init)]
54        pub fn initWithDevice(
55            this: Allocated<Self>,
56            device: &ProtocolObject<dyn MTLDevice>,
57        ) -> Retained<Self>;
58
59        /// Device used to create buffers.
60        #[unsafe(method(device))]
61        #[unsafe(method_family = none)]
62        pub fn device(&self) -> Retained<ProtocolObject<dyn MTLDevice>>;
63    );
64}
65
66/// Methods declared on superclass `NSObject`.
67impl MTKMeshBufferAllocator {
68    extern_methods!(
69        #[unsafe(method(new))]
70        #[unsafe(method_family = new)]
71        pub unsafe fn new() -> Retained<Self>;
72    );
73}
74
75extern_class!(
76    /// Mesh buffer created by MTKMeshBufferAllocator when Model I/O needs to memory for vertex or index data backing.
77    ///
78    /// Memory backing these buffer are Metal buffers.  Model I/O will load index and vertex data from from a model asset directly in to the Metal buffer.
79    ///
80    /// See also [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmeshbuffer?language=objc)
81    #[unsafe(super(NSObject))]
82    #[derive(Debug, PartialEq, Eq, Hash)]
83    pub struct MTKMeshBuffer;
84);
85
86extern_conformance!(
87    unsafe impl NSCopying for MTKMeshBuffer {}
88);
89
90unsafe impl CopyingHelper for MTKMeshBuffer {
91    type Result = Self;
92}
93
94extern_conformance!(
95    unsafe impl NSObjectProtocol for MTKMeshBuffer {}
96);
97
98impl MTKMeshBuffer {
99    extern_methods!(
100        /// Only an MTKMeshBufferAllocator object can initilize a MTKMeshBuffer object
101        #[unsafe(method(init))]
102        #[unsafe(method_family = init)]
103        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
104
105        /// Size in bytes of the buffer allocation.
106        #[unsafe(method(length))]
107        #[unsafe(method_family = none)]
108        pub fn length(&self) -> NSUInteger;
109
110        /// Allocator object used to create this buffer.
111        ///
112        /// This allcoator is stored so that it can be used by Model I/O for copy and relayout operations (such as when a new vertex descriptor is applied to a vertex buffer).
113        #[unsafe(method(allocator))]
114        #[unsafe(method_family = none)]
115        pub fn allocator(&self) -> Retained<MTKMeshBufferAllocator>;
116
117        #[cfg(feature = "objc2-model-io")]
118        /// Zone from which this buffer was created (if it was created from a zone).
119        ///
120        /// A single MetalBuffer is allocated for each zone.  Each zone could have many MTKMeshBuffers, each with it's own offset.  If a MTKMeshBufferAllocator is used, Model I/O will attempt to load all vertex and index data of a single mesh into a single zone.  This allows the GPU to achieve a higher cache hit rate when drawing the mesh.  So although there maybe many MTKMeshBuffers for a model they will be backed with the same contigous MetalBuffer.
121        #[unsafe(method(zone))]
122        #[unsafe(method_family = none)]
123        pub fn zone(&self) -> Option<Retained<ProtocolObject<dyn MDLMeshBufferZone>>>;
124
125        /// Metal Buffer backing vertex/index data.
126        ///
127        /// Many MTKMeshBuffers may reference the same buffer, but each with it's own offset.  (i.e. Many MTKMeshBuffers may be suballocated from a single buffer)
128        #[unsafe(method(buffer))]
129        #[unsafe(method_family = none)]
130        pub fn buffer(&self) -> Retained<ProtocolObject<dyn MTLBuffer>>;
131
132        /// Byte offset of the data within the metal buffer.
133        #[unsafe(method(offset))]
134        #[unsafe(method_family = none)]
135        pub fn offset(&self) -> NSUInteger;
136
137        #[cfg(feature = "objc2-model-io")]
138        /// the intended type of the buffer
139        #[unsafe(method(type))]
140        #[unsafe(method_family = none)]
141        pub fn r#type(&self) -> MDLMeshBufferType;
142    );
143}
144
145/// Methods declared on superclass `NSObject`.
146impl MTKMeshBuffer {
147    extern_methods!(
148        #[unsafe(method(new))]
149        #[unsafe(method_family = new)]
150        pub unsafe fn new() -> Retained<Self>;
151    );
152}
153
154extern_class!(
155    /// A segment of a mesh and properties to render the segement.
156    ///
157    /// Container for data that can be rendered in a single draw call. 1:1 mapping to MDLSubmesh.  Each submesh contains an index Buffer with which the parents mesh data can be rendered.  Actual vertex data resides in the submesh's parent MTKMesh object.
158    ///
159    /// See also [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtksubmesh?language=objc)
160    #[unsafe(super(NSObject))]
161    #[derive(Debug, PartialEq, Eq, Hash)]
162    pub struct MTKSubmesh;
163);
164
165extern_conformance!(
166    unsafe impl NSObjectProtocol for MTKSubmesh {}
167);
168
169impl MTKSubmesh {
170    extern_methods!(
171        /// Applicatiohs must not explicity allocate or initialize.  Must initialize as part of MTKMesh object.
172        #[unsafe(method(init))]
173        #[unsafe(method_family = init)]
174        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
175
176        /// Metal primitive type with which to draw this object.
177        ///
178        /// Value to use for primitiveType parameter in a [MTLRenderCommandEncoder drawIndexedPrimitives] call.
179        #[unsafe(method(primitiveType))]
180        #[unsafe(method_family = none)]
181        pub fn primitiveType(&self) -> MTLPrimitiveType;
182
183        /// Metal index type of data in indexBuffer.
184        ///
185        /// Value to use for indexType parameter in a [MTLRenderCommandEncoder drawIndexedPrimitives] call.
186        #[unsafe(method(indexType))]
187        #[unsafe(method_family = none)]
188        pub fn indexType(&self) -> MTLIndexType;
189
190        /// IndexBuffer (including indexCount) to render the object.
191        ///
192        /// The MTLBuffer to use for indexBuffer parameter in a [MTLRenderCommandEncoder drawIndexedPrimitives] call.
193        #[unsafe(method(indexBuffer))]
194        #[unsafe(method_family = none)]
195        pub fn indexBuffer(&self) -> Retained<MTKMeshBuffer>;
196
197        /// Number of indicies in indexBuffer.
198        ///
199        /// Value to use for indexCount parameter in a [MTLRenderCommandEncoder drawIndexedPrimitives] call.
200        #[unsafe(method(indexCount))]
201        #[unsafe(method_family = none)]
202        pub fn indexCount(&self) -> NSUInteger;
203
204        /// Parent MTKMesh object containing vertex data of this object.
205        ///
206        /// The buffer of this parent mesh should be set in the encoder before a drawIndexedPrimitives call is made.
207        #[unsafe(method(mesh))]
208        #[unsafe(method_family = none)]
209        pub fn mesh(&self) -> Option<Retained<MTKMesh>>;
210
211        /// Name from the original MDLSubmesh object.
212        ///
213        /// Although not directly used by this object, the application may use this to identify the submesh in the renderer/scene/world.
214        #[unsafe(method(name))]
215        #[unsafe(method_family = none)]
216        pub fn name(&self) -> Retained<NSString>;
217
218        /// Setter for [`name`][Self::name].
219        ///
220        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
221        #[unsafe(method(setName:))]
222        #[unsafe(method_family = none)]
223        pub fn setName(&self, name: &NSString);
224    );
225}
226
227/// Methods declared on superclass `NSObject`.
228impl MTKSubmesh {
229    extern_methods!(
230        #[unsafe(method(new))]
231        #[unsafe(method_family = new)]
232        pub unsafe fn new() -> Retained<Self>;
233    );
234}
235
236extern_class!(
237    /// Container for vertex data of a mesh and submeshes to render it.
238    ///
239    /// See also [Apple's documentation](https://developer.apple.com/documentation/metalkit/mtkmesh?language=objc)
240    #[unsafe(super(NSObject))]
241    #[derive(Debug, PartialEq, Eq, Hash)]
242    pub struct MTKMesh;
243);
244
245extern_conformance!(
246    unsafe impl NSObjectProtocol for MTKMesh {}
247);
248
249impl MTKMesh {
250    extern_methods!(
251        /// Cannot use default init.  Must initialize with mesh and metal device.
252        #[unsafe(method(init))]
253        #[unsafe(method_family = init)]
254        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
255
256        #[cfg(feature = "objc2-model-io")]
257        /// Initialize the mesh and the mesh's submeshes.
258        ///
259        /// Parameter `mesh`: Model I/O Mesh from which to create this MetalKit mesh
260        ///
261        /// Parameter `device`: Metal device on which to create mesh resources
262        ///
263        /// Parameter `error`: Pointer to an NSError object set if an error occurred
264        ///
265        /// The designated initializer for this class.  This does NOT initialize any meshes that are children of the Model I/O mesh, only submeshes that are part of the given mesh.  An exception is raised if vertexBuffer objects in the given mesh and the indexBuffer of any submesh in this mesh have not been created with a MTKMeshBufferAllocator object.  If a submesh using MDLGeometryTypeQuads or MDLGeometryTypeTopology is used, that submesh will be copied, and recreated to use MDLGeometryTypeTriangles, before this routine creates the MTKSubmesh.
266        #[unsafe(method(initWithMesh:device:error:_))]
267        #[unsafe(method_family = init)]
268        pub fn initWithMesh_device_error(
269            this: Allocated<Self>,
270            mesh: &MDLMesh,
271            device: &ProtocolObject<dyn MTLDevice>,
272        ) -> Result<Retained<Self>, Retained<NSError>>;
273
274        #[cfg(feature = "objc2-model-io")]
275        /// Initialize all meshes in a Model I/O asset.
276        ///
277        /// Parameter `asset`: Model I/O asset from which to create MetalKit meshes
278        ///
279        /// Parameter `device`: Metal device on which to create mesh resources
280        ///
281        /// Parameter `sourceMeshes`: Array built by this method containing MDLMesh objects corresponding the returned MTKMesh objects
282        ///
283        /// Parameter `error`: Pointer to an NSError object set if an error occurred
284        ///
285        /// Returns: MetalKit Meshes created from the Model I/O asset
286        ///
287        /// A convenience method to create MetalKit meshes from each mesh in a Model I/O asset.  resulting meshes are returned while the corresponding Model I/O meshes from which they were generated will appear in the sourceMeshes array.  All vertexBuffer objects in each MDLMesh object in the asset and the indexBuffer of each submesh within each of these meshes must have been created using a MTKMeshBufferAllocator object.  Thus
288        #[unsafe(method(newMeshesFromAsset:device:sourceMeshes:error:_))]
289        #[unsafe(method_family = new)]
290        pub fn newMeshesFromAsset_device_sourceMeshes_error(
291            asset: &MDLAsset,
292            device: &ProtocolObject<dyn MTLDevice>,
293            source_meshes: Option<&mut Option<Retained<NSArray<MDLMesh>>>>,
294        ) -> Result<Retained<NSArray<MTKMesh>>, Retained<NSError>>;
295
296        /// Array of buffers in which mesh vertex data resides.
297        ///
298        /// This is filled with mesh buffer objects using the layout described by the vertexDescriptor property.  Elements in this array can be [NSNull null] if the vertexDescriptor does not specify elements for buffer for the given index
299        #[unsafe(method(vertexBuffers))]
300        #[unsafe(method_family = none)]
301        pub fn vertexBuffers(&self) -> Retained<NSArray<MTKMeshBuffer>>;
302
303        #[cfg(feature = "objc2-model-io")]
304        /// Model I/O vertex descriptor specifying the layout of data in vertexBuffers.
305        ///
306        /// This is not directly used by this object, but the application can use this information to determine rendering state or create a Metal vertex descriptor to build a RenderPipelineState object capable of interpreting data in 'vertexBuffers'.  Changing propties in the object will not result in the relayout data in vertex descriptor and thus will make the vertex descriptor no loger describe the layout of vertes data and verticies. (i.e. don't change properties in this vertexDescriptor)
307        #[unsafe(method(vertexDescriptor))]
308        #[unsafe(method_family = none)]
309        pub fn vertexDescriptor(&self) -> Retained<MDLVertexDescriptor>;
310
311        /// Submeshes containing index buffers to rendering mesh vertices.
312        #[unsafe(method(submeshes))]
313        #[unsafe(method_family = none)]
314        pub fn submeshes(&self) -> Retained<NSArray<MTKSubmesh>>;
315
316        /// Number of vertices in the vertexBuffers.
317        #[unsafe(method(vertexCount))]
318        #[unsafe(method_family = none)]
319        pub fn vertexCount(&self) -> NSUInteger;
320
321        /// Name of the mesh copies from the originating Model I/O mesh.
322        ///
323        /// Can be used by the app to identify the mesh in its scene/world/renderer etc.
324        #[unsafe(method(name))]
325        #[unsafe(method_family = none)]
326        pub fn name(&self) -> Retained<NSString>;
327
328        /// Setter for [`name`][Self::name].
329        ///
330        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
331        #[unsafe(method(setName:))]
332        #[unsafe(method_family = none)]
333        pub fn setName(&self, name: &NSString);
334    );
335}
336
337/// Methods declared on superclass `NSObject`.
338impl MTKMesh {
339    extern_methods!(
340        #[unsafe(method(new))]
341        #[unsafe(method_family = new)]
342        pub unsafe fn new() -> Retained<Self>;
343    );
344}
345
346/// Partially converts a Metal vertex descriptor to a Model I/O vertex descriptor
347///
348/// This method can only set vertex format, offset, bufferIndex, and stride information in the produced Model I/O vertex descriptor.  It does not add any semantic information such at attributes names.  Names must be set in the returned Model I/O vertex descriptor before it can be applied to a a Model I/O mesh.
349#[cfg(feature = "objc2-model-io")]
350#[inline]
351pub extern "C-unwind" fn MTKModelIOVertexDescriptorFromMetal(
352    metal_descriptor: &MTLVertexDescriptor,
353) -> Retained<MDLVertexDescriptor> {
354    extern "C-unwind" {
355        fn MTKModelIOVertexDescriptorFromMetal(
356            metal_descriptor: &MTLVertexDescriptor,
357        ) -> *mut MDLVertexDescriptor;
358    }
359    let ret = unsafe { MTKModelIOVertexDescriptorFromMetal(metal_descriptor) };
360    unsafe { Retained::retain_autoreleased(ret) }
361        .expect("function was marked as returning non-null, but actually returned NULL")
362}
363
364/// Partially converts a Metal vertex descriptor to a Model I/O vertex descriptor
365///
366/// This method can only set vertex format, offset, bufferIndex, and stride information in the produced Model I/O vertex descriptor.  It does not add any semantic information such at attributes names.  Names must be set in the returned Model I/O vertex descriptor before it can be applied to a a Model I/O mesh. If error is nonnull, and the conversion cannot be made, it will be set.
367///
368/// # Safety
369///
370/// `error` must be a valid pointer or null.
371#[cfg(feature = "objc2-model-io")]
372#[inline]
373pub unsafe extern "C-unwind" fn MTKModelIOVertexDescriptorFromMetalWithError(
374    metal_descriptor: &MTLVertexDescriptor,
375    error: *mut *mut NSError,
376) -> Retained<MDLVertexDescriptor> {
377    extern "C-unwind" {
378        fn MTKModelIOVertexDescriptorFromMetalWithError(
379            metal_descriptor: &MTLVertexDescriptor,
380            error: *mut *mut NSError,
381        ) -> *mut MDLVertexDescriptor;
382    }
383    let ret = unsafe { MTKModelIOVertexDescriptorFromMetalWithError(metal_descriptor, error) };
384    unsafe { Retained::retain_autoreleased(ret) }
385        .expect("function was marked as returning non-null, but actually returned NULL")
386}
387
388/// Partially converts a Model I/O vertex descriptor to a Metal vertex descriptor
389///
390/// This method can only set vertex format, offset, bufferIndex, and stride information in the produced Metal vertex descriptor. It simply copies attributes 1 for 1. Thus attributes in the given Model I/O vertex descriptor must be arranged in the correct order for the resulting descriptor to properly map mesh data to vertex shader inputs.  Layout stepFunction and stepRates for the resulting MTLVertexDescriptor must also be set by application.
391#[cfg(feature = "objc2-model-io")]
392#[inline]
393pub extern "C-unwind" fn MTKMetalVertexDescriptorFromModelIO(
394    model_io_descriptor: &MDLVertexDescriptor,
395) -> Option<Retained<MTLVertexDescriptor>> {
396    extern "C-unwind" {
397        fn MTKMetalVertexDescriptorFromModelIO(
398            model_io_descriptor: &MDLVertexDescriptor,
399        ) -> *mut MTLVertexDescriptor;
400    }
401    let ret = unsafe { MTKMetalVertexDescriptorFromModelIO(model_io_descriptor) };
402    unsafe { Retained::retain_autoreleased(ret) }
403}
404
405/// Partially converts a Model I/O vertex descriptor to a Metal vertex descriptor
406///
407/// This method can only set vertex format, offset, bufferIndex, and stride information in the produced Metal vertex descriptor. It simply copies attributes 1 for 1. Thus attributes in the given Model I/O vertex descriptor must be arranged in the correct order for the resulting descriptor to properly map mesh data to vertex shader inputs.  Layout stepFunction and stepRates for the resulting MTLVertexDescriptor must also be set by application.  If error is nonnull, and the conversion cannot be made, it will be set.
408///
409/// # Safety
410///
411/// `error` must be a valid pointer or null.
412#[cfg(feature = "objc2-model-io")]
413#[inline]
414pub unsafe extern "C-unwind" fn MTKMetalVertexDescriptorFromModelIOWithError(
415    model_io_descriptor: &MDLVertexDescriptor,
416    error: *mut *mut NSError,
417) -> Option<Retained<MTLVertexDescriptor>> {
418    extern "C-unwind" {
419        fn MTKMetalVertexDescriptorFromModelIOWithError(
420            model_io_descriptor: &MDLVertexDescriptor,
421            error: *mut *mut NSError,
422        ) -> *mut MTLVertexDescriptor;
423    }
424    let ret = unsafe { MTKMetalVertexDescriptorFromModelIOWithError(model_io_descriptor, error) };
425    unsafe { Retained::retain_autoreleased(ret) }
426}
427
428/// Converts a Metal vertex format to a Model I/O vertex format
429///
430/// Returns: A Model I/O vertexformat correspoinding to the given Metal vertex format.  Returns MDLVertexFormatInvalid if no matching Model I/O vertex format exists.
431#[cfg(feature = "objc2-model-io")]
432#[inline]
433pub extern "C-unwind" fn MTKModelIOVertexFormatFromMetal(
434    vertex_format: MTLVertexFormat,
435) -> MDLVertexFormat {
436    extern "C-unwind" {
437        fn MTKModelIOVertexFormatFromMetal(vertex_format: MTLVertexFormat) -> MDLVertexFormat;
438    }
439    unsafe { MTKModelIOVertexFormatFromMetal(vertex_format) }
440}
441
442/// Converts a Model I/O vertex format to a Metal vertex format
443///
444/// Returns: A Metal vertexformat correspoinding to the given Model I/O vertex format.  Returns MTLVertexFormatInvalid if no matching Metal vertex format exists.
445#[cfg(feature = "objc2-model-io")]
446#[inline]
447pub extern "C-unwind" fn MTKMetalVertexFormatFromModelIO(
448    vertex_format: MDLVertexFormat,
449) -> MTLVertexFormat {
450    extern "C-unwind" {
451        fn MTKMetalVertexFormatFromModelIO(vertex_format: MDLVertexFormat) -> MTLVertexFormat;
452    }
453    unsafe { MTKMetalVertexFormatFromModelIO(vertex_format) }
454}