MTLResource.hpp
1 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 // 3 // Metal/MTLResource.hpp 4 // 5 // Copyright 2020-2024 Apple Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 // 19 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 20 21 #pragma once 22 23 #include <mach/mach.h> 24 25 #include "MTLDefines.hpp" 26 #include "MTLHeaderBridge.hpp" 27 #include "MTLPrivate.hpp" 28 29 #include <Foundation/Foundation.hpp> 30 31 #include "MTLAllocation.hpp" 32 #include "MTLResource.hpp" 33 34 namespace MTL 35 { 36 _MTL_ENUM(NS::UInteger, PurgeableState) { 37 PurgeableStateKeepCurrent = 1, 38 PurgeableStateNonVolatile = 2, 39 PurgeableStateVolatile = 3, 40 PurgeableStateEmpty = 4, 41 }; 42 43 _MTL_ENUM(NS::UInteger, CPUCacheMode) { 44 CPUCacheModeDefaultCache = 0, 45 CPUCacheModeWriteCombined = 1, 46 }; 47 48 _MTL_ENUM(NS::UInteger, StorageMode) { 49 StorageModeShared = 0, 50 StorageModeManaged = 1, 51 StorageModePrivate = 2, 52 StorageModeMemoryless = 3, 53 }; 54 55 _MTL_ENUM(NS::UInteger, HazardTrackingMode) { 56 HazardTrackingModeDefault = 0, 57 HazardTrackingModeUntracked = 1, 58 HazardTrackingModeTracked = 2, 59 }; 60 61 _MTL_OPTIONS(NS::UInteger, ResourceOptions) { 62 ResourceCPUCacheModeDefaultCache = 0, 63 ResourceCPUCacheModeWriteCombined = 1, 64 ResourceStorageModeShared = 0, 65 ResourceStorageModeManaged = 16, 66 ResourceStorageModePrivate = 32, 67 ResourceStorageModeMemoryless = 48, 68 ResourceHazardTrackingModeDefault = 0, 69 ResourceHazardTrackingModeUntracked = 256, 70 ResourceHazardTrackingModeTracked = 512, 71 ResourceOptionCPUCacheModeDefault = 0, 72 ResourceOptionCPUCacheModeWriteCombined = 1, 73 }; 74 75 class Resource : public NS::Referencing<Resource, Allocation> 76 { 77 public: 78 NS::String* label() const; 79 void setLabel(const NS::String* label); 80 81 class Device* device() const; 82 83 MTL::CPUCacheMode cpuCacheMode() const; 84 85 MTL::StorageMode storageMode() const; 86 87 MTL::HazardTrackingMode hazardTrackingMode() const; 88 89 MTL::ResourceOptions resourceOptions() const; 90 91 MTL::PurgeableState setPurgeableState(MTL::PurgeableState state); 92 93 class Heap* heap() const; 94 95 NS::UInteger heapOffset() const; 96 97 NS::UInteger allocatedSize() const; 98 99 void makeAliasable(); 100 101 bool isAliasable(); 102 103 kern_return_t setOwner(task_id_token_t task_id_token); 104 }; 105 106 } 107 108 // property: label 109 _MTL_INLINE NS::String* MTL::Resource::label() const 110 { 111 return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label)); 112 } 113 114 _MTL_INLINE void MTL::Resource::setLabel(const NS::String* label) 115 { 116 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label); 117 } 118 119 // property: device 120 _MTL_INLINE MTL::Device* MTL::Resource::device() const 121 { 122 return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device)); 123 } 124 125 // property: cpuCacheMode 126 _MTL_INLINE MTL::CPUCacheMode MTL::Resource::cpuCacheMode() const 127 { 128 return Object::sendMessage<MTL::CPUCacheMode>(this, _MTL_PRIVATE_SEL(cpuCacheMode)); 129 } 130 131 // property: storageMode 132 _MTL_INLINE MTL::StorageMode MTL::Resource::storageMode() const 133 { 134 return Object::sendMessage<MTL::StorageMode>(this, _MTL_PRIVATE_SEL(storageMode)); 135 } 136 137 // property: hazardTrackingMode 138 _MTL_INLINE MTL::HazardTrackingMode MTL::Resource::hazardTrackingMode() const 139 { 140 return Object::sendMessage<MTL::HazardTrackingMode>(this, _MTL_PRIVATE_SEL(hazardTrackingMode)); 141 } 142 143 // property: resourceOptions 144 _MTL_INLINE MTL::ResourceOptions MTL::Resource::resourceOptions() const 145 { 146 return Object::sendMessage<MTL::ResourceOptions>(this, _MTL_PRIVATE_SEL(resourceOptions)); 147 } 148 149 // method: setPurgeableState: 150 _MTL_INLINE MTL::PurgeableState MTL::Resource::setPurgeableState(MTL::PurgeableState state) 151 { 152 return Object::sendMessage<MTL::PurgeableState>(this, _MTL_PRIVATE_SEL(setPurgeableState_), state); 153 } 154 155 // property: heap 156 _MTL_INLINE MTL::Heap* MTL::Resource::heap() const 157 { 158 return Object::sendMessage<MTL::Heap*>(this, _MTL_PRIVATE_SEL(heap)); 159 } 160 161 // property: heapOffset 162 _MTL_INLINE NS::UInteger MTL::Resource::heapOffset() const 163 { 164 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(heapOffset)); 165 } 166 167 // property: allocatedSize 168 _MTL_INLINE NS::UInteger MTL::Resource::allocatedSize() const 169 { 170 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(allocatedSize)); 171 } 172 173 // method: makeAliasable 174 _MTL_INLINE void MTL::Resource::makeAliasable() 175 { 176 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(makeAliasable)); 177 } 178 179 // method: isAliasable 180 _MTL_INLINE bool MTL::Resource::isAliasable() 181 { 182 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(isAliasable)); 183 } 184 185 // method: setOwnerWithIdentity: 186 _MTL_INLINE kern_return_t MTL::Resource::setOwner(task_id_token_t task_id_token) 187 { 188 return Object::sendMessage<kern_return_t>(this, _MTL_PRIVATE_SEL(setOwnerWithIdentity_), task_id_token); 189 }