Skip to main content

mtl_gpu/encoder/render_encoder/
mesh.rs

1//! Object and mesh shader bindings.
2//!
3//! This module contains methods for binding resources to object and mesh shaders.
4
5use std::ffi::c_void;
6
7use mtl_foundation::{Referencing, UInteger};
8use mtl_sys::sel;
9
10use crate::Buffer;
11use crate::Texture;
12
13use super::RenderCommandEncoder;
14
15impl RenderCommandEncoder {
16    // =========================================================================
17    // Object Shader Bindings
18    // =========================================================================
19
20    /// Set an object buffer.
21    ///
22    /// C++ equivalent: `void setObjectBuffer(const Buffer*, NS::UInteger, NS::UInteger)`
23    #[inline]
24    pub fn set_object_buffer(&self, buffer: &Buffer, offset: UInteger, index: UInteger) {
25        unsafe {
26            mtl_sys::msg_send_3::<(), *const c_void, UInteger, UInteger>(
27                self.as_ptr(),
28                sel!(setObjectBuffer: offset: atIndex:),
29                buffer.as_ptr(),
30                offset,
31                index,
32            );
33        }
34    }
35
36    /// Set an object texture.
37    ///
38    /// C++ equivalent: `void setObjectTexture(const Texture*, NS::UInteger)`
39    #[inline]
40    pub fn set_object_texture(&self, texture: &Texture, index: UInteger) {
41        unsafe {
42            mtl_sys::msg_send_2::<(), *const c_void, UInteger>(
43                self.as_ptr(),
44                sel!(setObjectTexture: atIndex:),
45                texture.as_ptr(),
46                index,
47            );
48        }
49    }
50
51    /// Set an object sampler state.
52    ///
53    /// C++ equivalent: `void setObjectSamplerState(const SamplerState*, NS::UInteger)`
54    #[inline]
55    pub fn set_object_sampler_state(&self, sampler: &crate::SamplerState, index: UInteger) {
56        unsafe {
57            mtl_sys::msg_send_2::<(), *const c_void, UInteger>(
58                self.as_ptr(),
59                sel!(setObjectSamplerState: atIndex:),
60                sampler.as_ptr(),
61                index,
62            );
63        }
64    }
65
66    /// Set the object threadgroup memory length.
67    ///
68    /// C++ equivalent: `void setObjectThreadgroupMemoryLength(NS::UInteger, NS::UInteger)`
69    #[inline]
70    pub fn set_object_threadgroup_memory_length(&self, length: UInteger, index: UInteger) {
71        unsafe {
72            mtl_sys::msg_send_2::<(), UInteger, UInteger>(
73                self.as_ptr(),
74                sel!(setObjectThreadgroupMemoryLength: atIndex:),
75                length,
76                index,
77            );
78        }
79    }
80
81    /// Set the object buffer offset.
82    ///
83    /// C++ equivalent: `void setObjectBufferOffset(NS::UInteger, NS::UInteger)`
84    #[inline]
85    pub fn set_object_buffer_offset(&self, offset: UInteger, index: UInteger) {
86        unsafe {
87            mtl_sys::msg_send_2::<(), UInteger, UInteger>(
88                self.as_ptr(),
89                sel!(setObjectBufferOffset: atIndex:),
90                offset,
91                index,
92            );
93        }
94    }
95
96    /// Set inline object bytes.
97    ///
98    /// C++ equivalent: `void setObjectBytes(const void*, NS::UInteger, NS::UInteger)`
99    #[inline]
100    pub fn set_object_bytes(&self, bytes: &[u8], index: UInteger) {
101        unsafe {
102            mtl_sys::msg_send_3::<(), *const c_void, UInteger, UInteger>(
103                self.as_ptr(),
104                sel!(setObjectBytes: length: atIndex:),
105                bytes.as_ptr() as *const c_void,
106                bytes.len() as UInteger,
107                index,
108            );
109        }
110    }
111
112    /// Set multiple object buffers at a range of indices (raw pointer version).
113    ///
114    /// C++ equivalent: `void setObjectBuffers(const Buffer* const*, const NS::UInteger*, NS::Range)`
115    ///
116    /// # Safety
117    ///
118    /// The buffers and offsets pointers must be valid arrays with at least `range.length` elements.
119    #[inline]
120    pub unsafe fn set_object_buffers_ptr(
121        &self,
122        buffers: *const *const c_void,
123        offsets: *const UInteger,
124        range_location: UInteger,
125        range_length: UInteger,
126    ) {
127        let range = mtl_foundation::Range::new(range_location, range_length);
128        unsafe {
129            mtl_sys::msg_send_3::<
130                (),
131                *const *const c_void,
132                *const UInteger,
133                mtl_foundation::Range,
134            >(
135                self.as_ptr(),
136                sel!(setObjectBuffers: offsets: withRange:),
137                buffers,
138                offsets,
139                range,
140            );
141        }
142    }
143
144    /// Set multiple object textures at a range of indices (raw pointer version).
145    ///
146    /// C++ equivalent: `void setObjectTextures(const Texture* const*, NS::Range)`
147    ///
148    /// # Safety
149    ///
150    /// The textures pointer must be a valid array with at least `range.length` elements.
151    #[inline]
152    pub unsafe fn set_object_textures_ptr(
153        &self,
154        textures: *const *const c_void,
155        range_location: UInteger,
156        range_length: UInteger,
157    ) {
158        let range = mtl_foundation::Range::new(range_location, range_length);
159        unsafe {
160            mtl_sys::msg_send_2::<(), *const *const c_void, mtl_foundation::Range>(
161                self.as_ptr(),
162                sel!(setObjectTextures: withRange:),
163                textures,
164                range,
165            );
166        }
167    }
168
169    /// Set an object sampler state with LOD clamps.
170    ///
171    /// C++ equivalent: `void setObjectSamplerState(const SamplerState*, float, float, NS::UInteger)`
172    #[inline]
173    pub fn set_object_sampler_state_with_lod_clamps(
174        &self,
175        sampler: &crate::SamplerState,
176        lod_min_clamp: f32,
177        lod_max_clamp: f32,
178        index: UInteger,
179    ) {
180        unsafe {
181            mtl_sys::msg_send_4::<(), *const c_void, f32, f32, UInteger>(
182                self.as_ptr(),
183                sel!(setObjectSamplerState: lodMinClamp: lodMaxClamp: atIndex:),
184                sampler.as_ptr(),
185                lod_min_clamp,
186                lod_max_clamp,
187                index,
188            );
189        }
190    }
191
192    /// Set multiple object sampler states at a range of indices (raw pointer version).
193    ///
194    /// C++ equivalent: `void setObjectSamplerStates(const SamplerState* const*, NS::Range)`
195    ///
196    /// # Safety
197    ///
198    /// The samplers pointer must be a valid array with at least `range.length` elements.
199    #[inline]
200    pub unsafe fn set_object_sampler_states_ptr(
201        &self,
202        samplers: *const *const c_void,
203        range_location: UInteger,
204        range_length: UInteger,
205    ) {
206        let range = mtl_foundation::Range::new(range_location, range_length);
207        unsafe {
208            mtl_sys::msg_send_2::<(), *const *const c_void, mtl_foundation::Range>(
209                self.as_ptr(),
210                sel!(setObjectSamplerStates: withRange:),
211                samplers,
212                range,
213            );
214        }
215    }
216
217    /// Set multiple object sampler states with LOD clamps at a range of indices (raw pointer version).
218    ///
219    /// C++ equivalent: `void setObjectSamplerStates(const SamplerState* const*, const float*, const float*, NS::Range)`
220    ///
221    /// # Safety
222    ///
223    /// The samplers, lod_min_clamps, and lod_max_clamps pointers must be valid arrays with at least `range.length` elements.
224    #[inline]
225    pub unsafe fn set_object_sampler_states_with_lod_clamps_ptr(
226        &self,
227        samplers: *const *const c_void,
228        lod_min_clamps: *const f32,
229        lod_max_clamps: *const f32,
230        range_location: UInteger,
231        range_length: UInteger,
232    ) {
233        let range = mtl_foundation::Range::new(range_location, range_length);
234        unsafe {
235            mtl_sys::msg_send_4::<
236                (),
237                *const *const c_void,
238                *const f32,
239                *const f32,
240                mtl_foundation::Range,
241            >(
242                self.as_ptr(),
243                sel!(setObjectSamplerStates: lodMinClamps: lodMaxClamps: withRange:),
244                samplers,
245                lod_min_clamps,
246                lod_max_clamps,
247                range,
248            );
249        }
250    }
251
252    // =========================================================================
253    // Mesh Shader Bindings
254    // =========================================================================
255
256    /// Set a mesh buffer.
257    ///
258    /// C++ equivalent: `void setMeshBuffer(const Buffer*, NS::UInteger, NS::UInteger)`
259    #[inline]
260    pub fn set_mesh_buffer(&self, buffer: &Buffer, offset: UInteger, index: UInteger) {
261        unsafe {
262            mtl_sys::msg_send_3::<(), *const c_void, UInteger, UInteger>(
263                self.as_ptr(),
264                sel!(setMeshBuffer: offset: atIndex:),
265                buffer.as_ptr(),
266                offset,
267                index,
268            );
269        }
270    }
271
272    /// Set a mesh texture.
273    ///
274    /// C++ equivalent: `void setMeshTexture(const Texture*, NS::UInteger)`
275    #[inline]
276    pub fn set_mesh_texture(&self, texture: &Texture, index: UInteger) {
277        unsafe {
278            mtl_sys::msg_send_2::<(), *const c_void, UInteger>(
279                self.as_ptr(),
280                sel!(setMeshTexture: atIndex:),
281                texture.as_ptr(),
282                index,
283            );
284        }
285    }
286
287    /// Set a mesh sampler state.
288    ///
289    /// C++ equivalent: `void setMeshSamplerState(const SamplerState*, NS::UInteger)`
290    #[inline]
291    pub fn set_mesh_sampler_state(&self, sampler: &crate::SamplerState, index: UInteger) {
292        unsafe {
293            mtl_sys::msg_send_2::<(), *const c_void, UInteger>(
294                self.as_ptr(),
295                sel!(setMeshSamplerState: atIndex:),
296                sampler.as_ptr(),
297                index,
298            );
299        }
300    }
301
302    /// Set the mesh buffer offset.
303    ///
304    /// C++ equivalent: `void setMeshBufferOffset(NS::UInteger, NS::UInteger)`
305    #[inline]
306    pub fn set_mesh_buffer_offset(&self, offset: UInteger, index: UInteger) {
307        unsafe {
308            mtl_sys::msg_send_2::<(), UInteger, UInteger>(
309                self.as_ptr(),
310                sel!(setMeshBufferOffset: atIndex:),
311                offset,
312                index,
313            );
314        }
315    }
316
317    /// Set inline mesh bytes.
318    ///
319    /// C++ equivalent: `void setMeshBytes(const void*, NS::UInteger, NS::UInteger)`
320    #[inline]
321    pub fn set_mesh_bytes(&self, bytes: &[u8], index: UInteger) {
322        unsafe {
323            mtl_sys::msg_send_3::<(), *const c_void, UInteger, UInteger>(
324                self.as_ptr(),
325                sel!(setMeshBytes: length: atIndex:),
326                bytes.as_ptr() as *const c_void,
327                bytes.len() as UInteger,
328                index,
329            );
330        }
331    }
332
333    /// Set multiple mesh buffers at a range of indices (raw pointer version).
334    ///
335    /// C++ equivalent: `void setMeshBuffers(const Buffer* const*, const NS::UInteger*, NS::Range)`
336    ///
337    /// # Safety
338    ///
339    /// The buffers and offsets pointers must be valid arrays with at least `range.length` elements.
340    #[inline]
341    pub unsafe fn set_mesh_buffers_ptr(
342        &self,
343        buffers: *const *const c_void,
344        offsets: *const UInteger,
345        range_location: UInteger,
346        range_length: UInteger,
347    ) {
348        let range = mtl_foundation::Range::new(range_location, range_length);
349        unsafe {
350            mtl_sys::msg_send_3::<
351                (),
352                *const *const c_void,
353                *const UInteger,
354                mtl_foundation::Range,
355            >(
356                self.as_ptr(),
357                sel!(setMeshBuffers: offsets: withRange:),
358                buffers,
359                offsets,
360                range,
361            );
362        }
363    }
364
365    /// Set multiple mesh textures at a range of indices (raw pointer version).
366    ///
367    /// C++ equivalent: `void setMeshTextures(const Texture* const*, NS::Range)`
368    ///
369    /// # Safety
370    ///
371    /// The textures pointer must be a valid array with at least `range.length` elements.
372    #[inline]
373    pub unsafe fn set_mesh_textures_ptr(
374        &self,
375        textures: *const *const c_void,
376        range_location: UInteger,
377        range_length: UInteger,
378    ) {
379        let range = mtl_foundation::Range::new(range_location, range_length);
380        unsafe {
381            mtl_sys::msg_send_2::<(), *const *const c_void, mtl_foundation::Range>(
382                self.as_ptr(),
383                sel!(setMeshTextures: withRange:),
384                textures,
385                range,
386            );
387        }
388    }
389
390    /// Set a mesh sampler state with LOD clamps.
391    ///
392    /// C++ equivalent: `void setMeshSamplerState(const SamplerState*, float, float, NS::UInteger)`
393    #[inline]
394    pub fn set_mesh_sampler_state_with_lod_clamps(
395        &self,
396        sampler: &crate::SamplerState,
397        lod_min_clamp: f32,
398        lod_max_clamp: f32,
399        index: UInteger,
400    ) {
401        unsafe {
402            mtl_sys::msg_send_4::<(), *const c_void, f32, f32, UInteger>(
403                self.as_ptr(),
404                sel!(setMeshSamplerState: lodMinClamp: lodMaxClamp: atIndex:),
405                sampler.as_ptr(),
406                lod_min_clamp,
407                lod_max_clamp,
408                index,
409            );
410        }
411    }
412
413    /// Set multiple mesh sampler states at a range of indices (raw pointer version).
414    ///
415    /// C++ equivalent: `void setMeshSamplerStates(const SamplerState* const*, NS::Range)`
416    ///
417    /// # Safety
418    ///
419    /// The samplers pointer must be a valid array with at least `range.length` elements.
420    #[inline]
421    pub unsafe fn set_mesh_sampler_states_ptr(
422        &self,
423        samplers: *const *const c_void,
424        range_location: UInteger,
425        range_length: UInteger,
426    ) {
427        let range = mtl_foundation::Range::new(range_location, range_length);
428        unsafe {
429            mtl_sys::msg_send_2::<(), *const *const c_void, mtl_foundation::Range>(
430                self.as_ptr(),
431                sel!(setMeshSamplerStates: withRange:),
432                samplers,
433                range,
434            );
435        }
436    }
437
438    /// Set multiple mesh sampler states with LOD clamps at a range of indices (raw pointer version).
439    ///
440    /// C++ equivalent: `void setMeshSamplerStates(const SamplerState* const*, const float*, const float*, NS::Range)`
441    ///
442    /// # Safety
443    ///
444    /// The samplers, lod_min_clamps, and lod_max_clamps pointers must be valid arrays with at least `range.length` elements.
445    #[inline]
446    pub unsafe fn set_mesh_sampler_states_with_lod_clamps_ptr(
447        &self,
448        samplers: *const *const c_void,
449        lod_min_clamps: *const f32,
450        lod_max_clamps: *const f32,
451        range_location: UInteger,
452        range_length: UInteger,
453    ) {
454        let range = mtl_foundation::Range::new(range_location, range_length);
455        unsafe {
456            mtl_sys::msg_send_4::<
457                (),
458                *const *const c_void,
459                *const f32,
460                *const f32,
461                mtl_foundation::Range,
462            >(
463                self.as_ptr(),
464                sel!(setMeshSamplerStates: lodMinClamps: lodMaxClamps: withRange:),
465                samplers,
466                lod_min_clamps,
467                lod_max_clamps,
468                range,
469            );
470        }
471    }
472}