objc2_metal/generated/MTLHeap.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::*;
7
8use crate::*;
9
10/// Describes the mode of operation for an MTLHeap.
11///
12/// In this mode, resources are placed in the heap automatically.
13/// Automatically placed resources have optimal GPU-specific layout, and may perform better than MTLHeapTypePlacement.
14/// This heap type is recommended when the heap primarily contains temporary write-often resources.
15///
16/// In this mode, the app places resources in the heap.
17/// Manually placed resources allow the app to control memory usage and heap fragmentation directly.
18/// This heap type is recommended when the heap primarily contains persistent write-rarely resources.
19///
20/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtlheaptype?language=objc)
21// NS_ENUM
22#[repr(transparent)]
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
24pub struct MTLHeapType(pub NSInteger);
25impl MTLHeapType {
26 #[doc(alias = "MTLHeapTypeAutomatic")]
27 pub const Automatic: Self = Self(0);
28 #[doc(alias = "MTLHeapTypePlacement")]
29 pub const Placement: Self = Self(1);
30 #[doc(alias = "MTLHeapTypeSparse")]
31 pub const Sparse: Self = Self(2);
32}
33
34unsafe impl Encode for MTLHeapType {
35 const ENCODING: Encoding = NSInteger::ENCODING;
36}
37
38unsafe impl RefEncode for MTLHeapType {
39 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
40}
41
42extern_class!(
43 /// [Apple's documentation](https://developer.apple.com/documentation/metal/mtlheapdescriptor?language=objc)
44 #[unsafe(super(NSObject))]
45 #[derive(Debug, PartialEq, Eq, Hash)]
46 pub struct MTLHeapDescriptor;
47);
48
49extern_conformance!(
50 unsafe impl NSCopying for MTLHeapDescriptor {}
51);
52
53unsafe impl CopyingHelper for MTLHeapDescriptor {
54 type Result = Self;
55}
56
57extern_conformance!(
58 unsafe impl NSObjectProtocol for MTLHeapDescriptor {}
59);
60
61impl MTLHeapDescriptor {
62 extern_methods!(
63 /// Requested size of the heap's backing memory.
64 ///
65 /// The size may be rounded up to GPU page granularity.
66 #[unsafe(method(size))]
67 #[unsafe(method_family = none)]
68 pub fn size(&self) -> NSUInteger;
69
70 /// Setter for [`size`][Self::size].
71 #[unsafe(method(setSize:))]
72 #[unsafe(method_family = none)]
73 pub fn setSize(&self, size: NSUInteger);
74
75 #[cfg(feature = "MTLResource")]
76 /// Storage mode for the heap. Default is MTLStorageModePrivate.
77 ///
78 /// All resources created from this heap share the same storage mode.
79 /// MTLStorageModeManaged and MTLStorageModeMemoryless are disallowed.
80 #[unsafe(method(storageMode))]
81 #[unsafe(method_family = none)]
82 pub fn storageMode(&self) -> MTLStorageMode;
83
84 #[cfg(feature = "MTLResource")]
85 /// Setter for [`storageMode`][Self::storageMode].
86 #[unsafe(method(setStorageMode:))]
87 #[unsafe(method_family = none)]
88 pub fn setStorageMode(&self, storage_mode: MTLStorageMode);
89
90 #[cfg(feature = "MTLResource")]
91 /// CPU cache mode for the heap. Default is MTLCPUCacheModeDefaultCache.
92 ///
93 /// All resources created from this heap share the same cache mode.
94 /// CPU cache mode is ignored for MTLStorageModePrivate.
95 #[unsafe(method(cpuCacheMode))]
96 #[unsafe(method_family = none)]
97 pub fn cpuCacheMode(&self) -> MTLCPUCacheMode;
98
99 #[cfg(feature = "MTLResource")]
100 /// Setter for [`cpuCacheMode`][Self::cpuCacheMode].
101 #[unsafe(method(setCpuCacheMode:))]
102 #[unsafe(method_family = none)]
103 pub fn setCpuCacheMode(&self, cpu_cache_mode: MTLCPUCacheMode);
104
105 #[cfg(feature = "MTLResource")]
106 /// The sparse page size to use for resources created from the heap.
107 #[unsafe(method(sparsePageSize))]
108 #[unsafe(method_family = none)]
109 pub fn sparsePageSize(&self) -> MTLSparsePageSize;
110
111 #[cfg(feature = "MTLResource")]
112 /// Setter for [`sparsePageSize`][Self::sparsePageSize].
113 #[unsafe(method(setSparsePageSize:))]
114 #[unsafe(method_family = none)]
115 pub fn setSparsePageSize(&self, sparse_page_size: MTLSparsePageSize);
116
117 #[cfg(feature = "MTLResource")]
118 /// Set hazard tracking mode for the heap. The default value is MTLHazardTrackingModeDefault.
119 ///
120 /// For heaps, MTLHazardTrackingModeDefault is treated as MTLHazardTrackingModeUntracked.
121 /// Setting hazardTrackingMode to MTLHazardTrackingModeTracked causes hazard tracking to be enabled heap.
122 /// When a resource on a hazard tracked heap is modified, reads and writes from all resources suballocated on that heap will be delayed until the modification is complete.
123 /// Similarly, modifying heap resources will be delayed until all in-flight reads and writes from all resources suballocated on that heap have completed.
124 /// For optimal performance, perform hazard tracking manually through MTLFence or MTLEvent instead.
125 /// All resources created from this heap shared the same hazard tracking mode.
126 #[unsafe(method(hazardTrackingMode))]
127 #[unsafe(method_family = none)]
128 pub fn hazardTrackingMode(&self) -> MTLHazardTrackingMode;
129
130 #[cfg(feature = "MTLResource")]
131 /// Setter for [`hazardTrackingMode`][Self::hazardTrackingMode].
132 #[unsafe(method(setHazardTrackingMode:))]
133 #[unsafe(method_family = none)]
134 pub fn setHazardTrackingMode(&self, hazard_tracking_mode: MTLHazardTrackingMode);
135
136 #[cfg(feature = "MTLResource")]
137 /// A packed tuple of the storageMode, cpuCacheMode and hazardTrackingMode properties.
138 ///
139 /// Modifications to this property are reflected in the other properties and vice versa.
140 #[unsafe(method(resourceOptions))]
141 #[unsafe(method_family = none)]
142 pub fn resourceOptions(&self) -> MTLResourceOptions;
143
144 #[cfg(feature = "MTLResource")]
145 /// Setter for [`resourceOptions`][Self::resourceOptions].
146 #[unsafe(method(setResourceOptions:))]
147 #[unsafe(method_family = none)]
148 pub fn setResourceOptions(&self, resource_options: MTLResourceOptions);
149
150 /// The type of the heap. The default value is MTLHeapTypeAutomatic.
151 ///
152 /// This constrains the resource creation functions that are available.
153 #[unsafe(method(type))]
154 #[unsafe(method_family = none)]
155 pub fn r#type(&self) -> MTLHeapType;
156
157 /// Setter for [`type`][Self::type].
158 #[unsafe(method(setType:))]
159 #[unsafe(method_family = none)]
160 pub fn setType(&self, r#type: MTLHeapType);
161
162 #[cfg(feature = "MTLResource")]
163 /// Specifies the largest sparse page size that the Metal heap supports.
164 ///
165 /// This parameter only affects the heap if you set the ``type`` property of this descriptor
166 /// to ``MTLHeapType/MTLHeapTypePlacement``.
167 ///
168 /// The value you assign to this property determines the compatibility of the Metal heap with with placement sparse
169 /// resources, because placement sparse resources require that their sparse page size be less than or equal to the
170 /// placement sparse page of the Metal heap that this property controls.
171 #[unsafe(method(maxCompatiblePlacementSparsePageSize))]
172 #[unsafe(method_family = none)]
173 pub fn maxCompatiblePlacementSparsePageSize(&self) -> MTLSparsePageSize;
174
175 #[cfg(feature = "MTLResource")]
176 /// Setter for [`maxCompatiblePlacementSparsePageSize`][Self::maxCompatiblePlacementSparsePageSize].
177 #[unsafe(method(setMaxCompatiblePlacementSparsePageSize:))]
178 #[unsafe(method_family = none)]
179 pub fn setMaxCompatiblePlacementSparsePageSize(
180 &self,
181 max_compatible_placement_sparse_page_size: MTLSparsePageSize,
182 );
183 );
184}
185
186/// Methods declared on superclass `NSObject`.
187impl MTLHeapDescriptor {
188 extern_methods!(
189 #[unsafe(method(init))]
190 #[unsafe(method_family = init)]
191 pub fn init(this: Allocated<Self>) -> Retained<Self>;
192
193 #[unsafe(method(new))]
194 #[unsafe(method_family = new)]
195 pub fn new() -> Retained<Self>;
196 );
197}
198
199impl DefaultRetained for MTLHeapDescriptor {
200 #[inline]
201 fn default_retained() -> Retained<Self> {
202 Self::new()
203 }
204}
205
206extern_protocol!(
207 /// [Apple's documentation](https://developer.apple.com/documentation/metal/mtlheap?language=objc)
208 #[cfg(feature = "MTLAllocation")]
209 pub unsafe trait MTLHeap: MTLAllocation {
210 /// A string to help identify this heap.
211 #[unsafe(method(label))]
212 #[unsafe(method_family = none)]
213 fn label(&self) -> Option<Retained<NSString>>;
214
215 /// Setter for [`label`][Self::label].
216 ///
217 /// This is [copied][objc2_foundation::NSCopying::copy] when set.
218 #[unsafe(method(setLabel:))]
219 #[unsafe(method_family = none)]
220 fn setLabel(&self, label: Option<&NSString>);
221
222 #[cfg(feature = "MTLDevice")]
223 /// The device this heap was created against. This heap can only be used with this device.
224 #[unsafe(method(device))]
225 #[unsafe(method_family = none)]
226 fn device(&self) -> Retained<ProtocolObject<dyn MTLDevice>>;
227
228 #[cfg(feature = "MTLResource")]
229 /// Current heap storage mode, default is MTLStorageModePrivate.
230 ///
231 /// All resources created from this heap share the same storage mode.
232 #[unsafe(method(storageMode))]
233 #[unsafe(method_family = none)]
234 fn storageMode(&self) -> MTLStorageMode;
235
236 #[cfg(feature = "MTLResource")]
237 /// CPU cache mode for the heap. Default is MTLCPUCacheModeDefaultCache.
238 ///
239 /// All resources created from this heap share the same cache mode.
240 #[unsafe(method(cpuCacheMode))]
241 #[unsafe(method_family = none)]
242 fn cpuCacheMode(&self) -> MTLCPUCacheMode;
243
244 #[cfg(feature = "MTLResource")]
245 /// Whether or not the heap is hazard tracked.
246 ///
247 /// When a resource on a hazard tracked heap is modified, reads and writes from any other resource on that heap will be delayed until the modification is complete.
248 /// Similarly, modifying heap resources will be delayed until all in-flight reads and writes from resources suballocated on that heap have completed.
249 /// For optimal performance, perform hazard tracking manually through MTLFence or MTLEvent instead.
250 /// Resources on the heap may opt-out of hazard tracking individually when the heap is hazard tracked,
251 /// however resources cannot opt-in to hazard tracking when the heap is not hazard tracked.
252 #[unsafe(method(hazardTrackingMode))]
253 #[unsafe(method_family = none)]
254 fn hazardTrackingMode(&self) -> MTLHazardTrackingMode;
255
256 #[cfg(feature = "MTLResource")]
257 /// A packed tuple of the storageMode, cpuCacheMode and hazardTrackingMode properties.
258 #[unsafe(method(resourceOptions))]
259 #[unsafe(method_family = none)]
260 fn resourceOptions(&self) -> MTLResourceOptions;
261
262 /// Heap size in bytes, specified at creation time and rounded up to device specific alignment.
263 #[unsafe(method(size))]
264 #[unsafe(method_family = none)]
265 fn size(&self) -> NSUInteger;
266
267 /// The size in bytes, of all resources allocated from the heap.
268 #[unsafe(method(usedSize))]
269 #[unsafe(method_family = none)]
270 fn usedSize(&self) -> NSUInteger;
271
272 /// The size in bytes of the current heap allocation.
273 #[unsafe(method(currentAllocatedSize))]
274 #[unsafe(method_family = none)]
275 fn currentAllocatedSize(&self) -> NSUInteger;
276
277 /// The maximum size that can be successfully allocated from the heap in bytes, taking into notice given alignment. Alignment needs to be zero, or power of two.
278 ///
279 /// Provides a measure of fragmentation within the heap.
280 #[unsafe(method(maxAvailableSizeWithAlignment:))]
281 #[unsafe(method_family = none)]
282 fn maxAvailableSizeWithAlignment(&self, alignment: NSUInteger) -> NSUInteger;
283
284 #[cfg(all(feature = "MTLBuffer", feature = "MTLResource"))]
285 /// Create a new buffer backed by heap memory.
286 ///
287 /// The requested storage and CPU cache modes must match the storage and CPU cache modes of the heap.
288 ///
289 /// Returns: The buffer or nil if heap is full.
290 #[unsafe(method(newBufferWithLength:options:))]
291 #[unsafe(method_family = new)]
292 fn newBufferWithLength_options(
293 &self,
294 length: NSUInteger,
295 options: MTLResourceOptions,
296 ) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>>;
297
298 #[cfg(all(feature = "MTLResource", feature = "MTLTexture"))]
299 /// Create a new texture backed by heap memory.
300 ///
301 /// The requested storage and CPU cache modes must match the storage and CPU cache modes of the heap, with the exception that the requested storage mode can be MTLStorageModeMemoryless.
302 ///
303 /// Returns: The texture or nil if heap is full.
304 #[unsafe(method(newTextureWithDescriptor:))]
305 #[unsafe(method_family = new)]
306 fn newTextureWithDescriptor(
307 &self,
308 descriptor: &MTLTextureDescriptor,
309 ) -> Option<Retained<ProtocolObject<dyn MTLTexture>>>;
310
311 #[cfg(feature = "MTLResource")]
312 /// Set or query the purgeability state of the heap.
313 #[unsafe(method(setPurgeableState:))]
314 #[unsafe(method_family = none)]
315 fn setPurgeableState(&self, state: MTLPurgeableState) -> MTLPurgeableState;
316
317 /// The type of the heap. The default value is MTLHeapTypeAutomatic.
318 ///
319 /// This constrains the resource creation functions that are available on the heap.
320 #[unsafe(method(type))]
321 #[unsafe(method_family = none)]
322 fn r#type(&self) -> MTLHeapType;
323
324 #[cfg(all(feature = "MTLBuffer", feature = "MTLResource"))]
325 /// Create a new buffer backed by heap memory at the specified placement offset.
326 ///
327 /// This method can only be used when heapType is set to MTLHeapTypePlacement.
328 /// Use "MTLDevice heapBufferSizeAndAlignWithLength:options:" to determine requiredSize and requiredAlignment.
329 /// Any resources that exist in this heap at overlapping half-open range [offset, offset + requiredSize) are implicitly aliased with the new resource.
330 ///
331 /// Parameter `length`: The requested size of the buffer, in bytes.
332 ///
333 /// Parameter `options`: The requested options of the buffer, of which the storage and CPU cache mode must match these of the heap.
334 ///
335 /// Parameter `offset`: The requested offset of the buffer inside the heap, in bytes. Behavior is undefined if "offset + requiredSize > heap.size" or "offset % requiredAlignment != 0".
336 ///
337 /// Returns: The buffer, or nil if the heap is not a placement heap
338 ///
339 /// # Safety
340 ///
341 /// `offset` might not be bounds-checked.
342 #[unsafe(method(newBufferWithLength:options:offset:))]
343 #[unsafe(method_family = new)]
344 unsafe fn newBufferWithLength_options_offset(
345 &self,
346 length: NSUInteger,
347 options: MTLResourceOptions,
348 offset: NSUInteger,
349 ) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>>;
350
351 #[cfg(all(feature = "MTLResource", feature = "MTLTexture"))]
352 /// Create a new texture backed by heap memory at the specified placement offset.
353 ///
354 /// This method can only be used when heapType is set to MTLHeapTypePlacement.
355 /// Use "MTLDevice heapTextureSizeAndAlignWithDescriptor:" to determine requiredSize and requiredAlignment.
356 /// Any resources that exist in this heap at overlapping half-open range [offset, offset + requiredSize) are implicitly aliased with the new resource.
357 ///
358 /// Parameter `descriptor`: The requested properties of the texture, of which the storage and CPU cache mode must match those of the heap.
359 ///
360 /// Parameter `offset`: The requested offset of the texture inside the heap, in bytes. Behavior is undefined if "offset + requiredSize > heap.size" and "offset % requiredAlignment != 0".
361 ///
362 /// Returns: The texture, or nil if the heap is not a placement heap.
363 ///
364 /// # Safety
365 ///
366 /// `offset` might not be bounds-checked.
367 #[unsafe(method(newTextureWithDescriptor:offset:))]
368 #[unsafe(method_family = new)]
369 unsafe fn newTextureWithDescriptor_offset(
370 &self,
371 descriptor: &MTLTextureDescriptor,
372 offset: NSUInteger,
373 ) -> Option<Retained<ProtocolObject<dyn MTLTexture>>>;
374
375 #[cfg(all(feature = "MTLAccelerationStructure", feature = "MTLResource"))]
376 /// Create a new acceleration structure backed by heap memory.
377 ///
378 /// Returns: The acceleration structure or nil if heap is full. Note that the MTLAccelerationStructure merely represents storage for an acceleration structure. It will still need to be populated via a build, copy, refit, etc.
379 ///
380 /// # Safety
381 ///
382 /// `size` might not be bounds-checked.
383 #[unsafe(method(newAccelerationStructureWithSize:))]
384 #[unsafe(method_family = new)]
385 unsafe fn newAccelerationStructureWithSize(
386 &self,
387 size: NSUInteger,
388 ) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
389
390 #[cfg(all(feature = "MTLAccelerationStructure", feature = "MTLResource"))]
391 /// Create a new acceleration structure backed by heap memory.
392 ///
393 /// This is a convenience method which creates the acceleration structure backed by heap memory. The acceleration structure size is inferred based on the descriptor.
394 ///
395 /// Returns: The acceleration structure or nil if heap is full. Note that the MTLAccelerationStructure merely represents storage for an acceleration structure. It will still need to be populated via a build, copy, refit, etc.
396 #[unsafe(method(newAccelerationStructureWithDescriptor:))]
397 #[unsafe(method_family = new)]
398 fn newAccelerationStructureWithDescriptor(
399 &self,
400 descriptor: &MTLAccelerationStructureDescriptor,
401 ) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
402
403 #[cfg(all(feature = "MTLAccelerationStructure", feature = "MTLResource"))]
404 /// Create a new acceleration structure backed by heap memory at the specified placement offset.
405 ///
406 /// This method can only be used when heapType is set to MTLHeapTypePlacement.
407 /// Use "MTLDevice heapAccelerationStructureSizeAndAlignWithSize:" or "MTLDevice heapAccelerationStructureSizeAndAlignWithDescriptor:" to determine requiredSize and requiredAlignment.
408 /// Any resources that exist in this heap at overlapping half-open range [offset, offset + requiredSize) are implicitly aliased with the new resource.
409 ///
410 /// Parameter `size`: The requested size of the acceleration structure, in bytes.
411 ///
412 /// Parameter `offset`: The requested offset of the acceleration structure inside the heap, in bytes. Behavior is undefined if "offset + requiredSize > heap.size" or "offset % requiredAlignment != 0".
413 ///
414 /// Returns: The acceleration structure, or nil if the heap is not a placement heap
415 ///
416 /// # Safety
417 ///
418 /// - `size` might not be bounds-checked.
419 /// - `offset` might not be bounds-checked.
420 #[unsafe(method(newAccelerationStructureWithSize:offset:))]
421 #[unsafe(method_family = new)]
422 unsafe fn newAccelerationStructureWithSize_offset(
423 &self,
424 size: NSUInteger,
425 offset: NSUInteger,
426 ) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
427
428 #[cfg(all(feature = "MTLAccelerationStructure", feature = "MTLResource"))]
429 /// Create a new acceleration structure backed by heap memory at the specified placement offset.
430 ///
431 /// This is a convenience method which computes the acceleration structure size based on the descriptor.
432 /// This method can only be used when heapType is set to MTLHeapTypePlacement.
433 /// Use "MTLDevice heapAccelerationStructureSizeAndAlignWithSize:" or "MTLDevice heapAccelerationStructureSizeAndAlignWithDescriptor:" to determine requiredSize and requiredAlignment.
434 /// Any resources that exist in this heap at overlapping half-open range [offset, offset + requiredSize) are implicitly aliased with the new resource.
435 ///
436 /// Parameter `descriptor`: The acceleration structure descriptor
437 ///
438 /// Parameter `offset`: The requested offset of the acceleration structure inside the heap, in bytes. Behavior is undefined if "offset + requiredSize > heap.size" or "offset % requiredAlignment != 0".
439 ///
440 /// Returns: The acceleration structure, or nil if the heap is not a placement heap
441 ///
442 /// # Safety
443 ///
444 /// `offset` might not be bounds-checked.
445 #[unsafe(method(newAccelerationStructureWithDescriptor:offset:))]
446 #[unsafe(method_family = new)]
447 unsafe fn newAccelerationStructureWithDescriptor_offset(
448 &self,
449 descriptor: &MTLAccelerationStructureDescriptor,
450 offset: NSUInteger,
451 ) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>>;
452 }
453);