Debian Codegraph @

third_party/rust/metal/src/resource.rs

raw
1 // Copyright 2016 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 use objc::runtime::{NO, YES};
10
11 /// See <https://developer.apple.com/documentation/metal/mtlpurgeablestate>
12 #[repr(u64)]
13 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
14 pub enum MTLPurgeableState {
15 KeepCurrent = 1,
16 NonVolatile = 2,
17 Volatile = 3,
18 Empty = 4,
19 }
20
21 /// See <https://developer.apple.com/documentation/metal/mtlcpucachemode>
22 #[repr(u64)]
23 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
24 pub enum MTLCPUCacheMode {
25 DefaultCache = 0,
26 WriteCombined = 1,
27 }
28
29 /// See <https://developer.apple.com/documentation/metal/mtlstoragemode>
30 #[repr(u64)]
31 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
32 pub enum MTLStorageMode {
33 Shared = 0,
34 Managed = 1,
35 Private = 2,
36 /// Only available on macos(11.0), macCatalyst(14.0), ios(10.0)
37 Memoryless = 3,
38 }
39
40 /// Only available on macos(10.15), ios(13.0)
41 ///
42 /// See <https://developer.apple.com/documentation/metal/mtlhazardtrackingmode>
43 #[repr(u64)]
44 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
45 pub enum MTLHazardTrackingMode {
46 Default = 0,
47 Untracked = 1,
48 Tracked = 2,
49 }
50
51 pub const MTLResourceCPUCacheModeShift: NSUInteger = 0;
52 pub const MTLResourceCPUCacheModeMask: NSUInteger = 0xf << MTLResourceCPUCacheModeShift;
53 pub const MTLResourceStorageModeShift: NSUInteger = 4;
54 pub const MTLResourceStorageModeMask: NSUInteger = 0xf << MTLResourceStorageModeShift;
55 pub const MTLResourceHazardTrackingModeShift: NSUInteger = 8;
56 pub const MTLResourceHazardTrackingModeMask: NSUInteger = 0x3 << MTLResourceHazardTrackingModeShift;
57
58 bitflags::bitflags! {
59 /// See <https://developer.apple.com/documentation/metal/mtlresourceoptions>
60 #[allow(non_upper_case_globals)]
61 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
62 pub struct MTLResourceOptions: NSUInteger {
63 const CPUCacheModeDefaultCache = (MTLCPUCacheMode::DefaultCache as NSUInteger) << MTLResourceCPUCacheModeShift;
64 const CPUCacheModeWriteCombined = (MTLCPUCacheMode::WriteCombined as NSUInteger) << MTLResourceCPUCacheModeShift;
65
66 const StorageModeShared = (MTLStorageMode::Shared as NSUInteger) << MTLResourceStorageModeShift;
67 const StorageModeManaged = (MTLStorageMode::Managed as NSUInteger) << MTLResourceStorageModeShift;
68 const StorageModePrivate = (MTLStorageMode::Private as NSUInteger) << MTLResourceStorageModeShift;
69 const StorageModeMemoryless = (MTLStorageMode::Memoryless as NSUInteger) << MTLResourceStorageModeShift;
70
71 /// Only available on macos(10.13), ios(10.0)
72 const HazardTrackingModeDefault = (MTLHazardTrackingMode::Default as NSUInteger) << MTLResourceHazardTrackingModeShift;
73 /// Only available on macos(10.13), ios(10.0)
74 const HazardTrackingModeUntracked = (MTLHazardTrackingMode::Untracked as NSUInteger) << MTLResourceHazardTrackingModeShift;
75 /// Only available on macos(10.15), ios(13.0)
76 const HazardTrackingModeTracked = (MTLHazardTrackingMode::Tracked as NSUInteger) << MTLResourceHazardTrackingModeShift;
77 }
78 }
79
80 bitflags::bitflags! {
81 /// Options that describe how a graphics or compute function uses an argument buffer’s resource.
82 ///
83 /// Enabling certain options for certain resources determines whether the Metal driver should
84 /// convert the resource to another format (for example, whether to decompress a color render target).
85 ///
86 /// See <https://developer.apple.com/documentation/metal/mtlresourceusage>
87 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
88 pub struct MTLResourceUsage: NSUInteger {
89 /// An option that enables reading from the resource.
90 const Read = 1 << 0;
91 /// An option that enables writing to the resource.
92 const Write = 1 << 1;
93 /// An option that enables sampling from the resource.
94 ///
95 /// Specify this option only if the resource is a texture.
96 const Sample = 1 << 2;
97 }
98 }
99
100 /// See <https://developer.apple.com/documentation/metal/mtlsizeandalign>
101 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
102 #[repr(C)]
103 pub struct MTLSizeAndAlign {
104 pub size: NSUInteger,
105 pub align: NSUInteger,
106 }
107
108 /// See <https://developer.apple.com/documentation/metal/mtlresource>
109 pub enum MTLResource {}
110
111 foreign_obj_type! {
112 type CType = MTLResource;
113 pub struct Resource;
114 type ParentType = NsObject;
115 }
116
117 impl ResourceRef {
118 pub fn device(&self) -> &DeviceRef {
119 unsafe { msg_send![self, device] }
120 }
121
122 pub fn label(&self) -> &str {
123 unsafe {
124 let label = msg_send![self, label];
125 crate::nsstring_as_str(label)
126 }
127 }
128
129 pub fn set_label(&self, label: &str) {
130 unsafe {
131 let nslabel = crate::nsstring_from_str(label);
132 let () = msg_send![self, setLabel: nslabel];
133 }
134 }
135
136 pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode {
137 unsafe { msg_send![self, cpuCacheMode] }
138 }
139
140 pub fn storage_mode(&self) -> MTLStorageMode {
141 unsafe { msg_send![self, storageMode] }
142 }
143
144 pub fn set_purgeable_state(&self, state: MTLPurgeableState) -> MTLPurgeableState {
145 unsafe { msg_send![self, setPurgeableState: state] }
146 }
147
148 /// Only available on macOS 10.13+ & iOS 10.11+
149 pub fn allocated_size(&self) -> NSUInteger {
150 unsafe { msg_send![self, allocatedSize] }
151 }
152
153 /// Only available on macos(10.15), ios(13.0)
154 pub fn hazard_tracking_mode(&self) -> MTLHazardTrackingMode {
155 unsafe { msg_send![self, hazardTrackingMode] }
156 }
157
158 /// Only available on macos(10.15), ios(13.0)
159 pub fn resource_options(&self) -> MTLResourceOptions {
160 unsafe { msg_send![self, resourceOptions] }
161 }
162
163 /// Only available on macos(10.13), ios(10.0)
164 pub fn heap(&self) -> &HeapRef {
165 unsafe { msg_send![self, heap] }
166 }
167
168 /// Only available on macos(10.15), ios(13.0)
169 pub fn heap_offset(&self) -> NSUInteger {
170 unsafe { msg_send![self, heapOffset] }
171 }
172
173 /// Only available on macos(10.13), ios(10.0)
174 pub fn make_aliasable(&self) {
175 unsafe { msg_send![self, makeAliasable] }
176 }
177
178 /// Only available on macos(10.13), ios(10.0)
179 pub fn is_aliasable(&self) -> bool {
180 unsafe { msg_send_bool![self, isAliasable] }
181 }
182 }