mtl_gpu/resource.rs
1//! Resource protocol.
2//!
3//! Corresponds to `Metal/MTLResource.hpp`.
4//!
5//! This module defines the Resource trait which corresponds to the
6//! `MTL::Resource` protocol in metal-cpp. Resource types like Buffer
7//! and Texture already have these methods as inherent methods, so this
8//! trait serves primarily for documentation and generic programming.
9
10use crate::Device;
11use crate::enums::{
12 CPUCacheMode, HazardTrackingMode, PurgeableState, ResourceOptions, StorageMode,
13};
14use crate::heap::Heap;
15use mtl_foundation::UInteger;
16
17/// Protocol for GPU-accessible resources.
18///
19/// C++ equivalent: `MTL::Resource`
20///
21/// This trait corresponds to the Objective-C protocol that resource types
22/// conform to. In Rust, concrete types like [`Buffer`](crate::Buffer) and
23/// [`Texture`](crate::Texture) implement these methods directly.
24///
25/// # Example
26///
27/// ```ignore
28/// let buffer = device.new_buffer(1024, ResourceOptions::default()).unwrap();
29/// println!("Label: {:?}", buffer.label());
30/// println!("Storage mode: {:?}", buffer.storage_mode());
31/// ```
32pub trait Resource {
33 /// Get the allocated size of this resource in bytes.
34 ///
35 /// C++ equivalent: `NS::UInteger allocatedSize() const`
36 fn allocated_size(&self) -> UInteger;
37 /// Get the CPU cache mode for this resource.
38 ///
39 /// C++ equivalent: `CPUCacheMode cpuCacheMode() const`
40 fn cpu_cache_mode(&self) -> CPUCacheMode;
41
42 /// Get the device that created this resource.
43 ///
44 /// C++ equivalent: `Device* device() const`
45 fn device(&self) -> Device;
46
47 /// Get the hazard tracking mode for this resource.
48 ///
49 /// C++ equivalent: `HazardTrackingMode hazardTrackingMode() const`
50 fn hazard_tracking_mode(&self) -> HazardTrackingMode;
51
52 /// Get the heap this resource was allocated from.
53 ///
54 /// Returns `None` if the resource was not allocated from a heap.
55 ///
56 /// C++ equivalent: `Heap* heap() const`
57 fn heap(&self) -> Option<Heap>;
58
59 /// Get the offset within the heap where this resource is allocated.
60 ///
61 /// C++ equivalent: `NS::UInteger heapOffset() const`
62 fn heap_offset(&self) -> UInteger;
63
64 /// Check if this resource can be aliased with other resources.
65 ///
66 /// C++ equivalent: `bool isAliasable()`
67 fn is_aliasable(&self) -> bool;
68
69 /// Get the debug label for this resource.
70 ///
71 /// C++ equivalent: `NS::String* label() const`
72 fn label(&self) -> Option<String>;
73
74 /// Make this resource aliasable with other resources.
75 ///
76 /// C++ equivalent: `void makeAliasable()`
77 fn make_aliasable(&self);
78
79 /// Get the resource options used to create this resource.
80 ///
81 /// C++ equivalent: `ResourceOptions resourceOptions() const`
82 fn resource_options(&self) -> ResourceOptions;
83
84 /// Set the debug label for this resource.
85 ///
86 /// C++ equivalent: `void setLabel(const NS::String*)`
87 fn set_label(&self, label: &str);
88
89 /// Set the purgeable state for this resource.
90 ///
91 /// Returns the previous purgeable state.
92 ///
93 /// C++ equivalent: `PurgeableState setPurgeableState(PurgeableState)`
94 fn set_purgeable_state(&self, state: PurgeableState) -> PurgeableState;
95
96 /// Get the storage mode for this resource.
97 ///
98 /// C++ equivalent: `StorageMode storageMode() const`
99 fn storage_mode(&self) -> StorageMode;
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn test_resource_trait() {
108 // Verify the trait is object-safe
109 fn _check_object_safety(_: &dyn Resource) {}
110 }
111}