| 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 |
|
| 10 |
use objc::runtime::{NO, YES}; |
| 11 |
|
| 12 |
/// See <https://developer.apple.com/documentation/metal/mtltexturetype> |
| 13 |
#[repr(u64)] |
| 14 |
#[allow(non_camel_case_types)] |
| 15 |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] |
| 16 |
pub enum MTLTextureType { |
| 17 |
D1 = 0, |
| 18 |
D1Array = 1, |
| 19 |
D2 = 2, |
| 20 |
D2Array = 3, |
| 21 |
D2Multisample = 4, |
| 22 |
Cube = 5, |
| 23 |
CubeArray = 6, |
| 24 |
D3 = 7, |
| 25 |
D2MultisampleArray = 8, |
| 26 |
} |
| 27 |
|
| 28 |
/// See <https://developer.apple.com/documentation/metal/mtltexturecompressiontype> |
| 29 |
#[repr(u64)] |
| 30 |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] |
| 31 |
pub enum MTLTextureCompressionType { |
| 32 |
Lossless = 0, |
| 33 |
Lossy = 1, |
| 34 |
} |
| 35 |
|
| 36 |
bitflags::bitflags! { |
| 37 |
/// See <https://developer.apple.com/documentation/metal/mtltextureusage> |
| 38 |
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] |
| 39 |
pub struct MTLTextureUsage: NSUInteger { |
| 40 |
const Unknown = 0x0000; |
| 41 |
const ShaderRead = 0x0001; |
| 42 |
const ShaderWrite = 0x0002; |
| 43 |
const RenderTarget = 0x0004; |
| 44 |
const PixelFormatView = 0x0010; |
| 45 |
const ShaderAtomic = 0x0020; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// See <https://developer.apple.com/documentation/metal/mtltexturedescriptor> |
| 50 |
pub enum MTLTextureDescriptor {} |
| 51 |
|
| 52 |
foreign_obj_type! { |
| 53 |
type CType = MTLTextureDescriptor; |
| 54 |
pub struct TextureDescriptor; |
| 55 |
} |
| 56 |
|
| 57 |
impl TextureDescriptor { |
| 58 |
pub fn new() -> Self { |
| 59 |
unsafe { |
| 60 |
let class = class!(MTLTextureDescriptor); |
| 61 |
msg_send![class, new] |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl TextureDescriptorRef { |
| 67 |
pub fn texture_type(&self) -> MTLTextureType { |
| 68 |
unsafe { msg_send![self, textureType] } |
| 69 |
} |
| 70 |
|
| 71 |
pub fn set_texture_type(&self, texture_type: MTLTextureType) { |
| 72 |
unsafe { msg_send![self, setTextureType: texture_type] } |
| 73 |
} |
| 74 |
|
| 75 |
pub fn pixel_format(&self) -> MTLPixelFormat { |
| 76 |
unsafe { msg_send![self, pixelFormat] } |
| 77 |
} |
| 78 |
|
| 79 |
pub fn set_pixel_format(&self, pixel_format: MTLPixelFormat) { |
| 80 |
unsafe { msg_send![self, setPixelFormat: pixel_format] } |
| 81 |
} |
| 82 |
|
| 83 |
pub fn width(&self) -> NSUInteger { |
| 84 |
unsafe { msg_send![self, width] } |
| 85 |
} |
| 86 |
|
| 87 |
pub fn set_width(&self, width: NSUInteger) { |
| 88 |
unsafe { msg_send![self, setWidth: width] } |
| 89 |
} |
| 90 |
|
| 91 |
pub fn height(&self) -> NSUInteger { |
| 92 |
unsafe { msg_send![self, height] } |
| 93 |
} |
| 94 |
|
| 95 |
pub fn set_height(&self, height: NSUInteger) { |
| 96 |
unsafe { msg_send![self, setHeight: height] } |
| 97 |
} |
| 98 |
|
| 99 |
pub fn depth(&self) -> NSUInteger { |
| 100 |
unsafe { msg_send![self, depth] } |
| 101 |
} |
| 102 |
|
| 103 |
pub fn set_depth(&self, depth: NSUInteger) { |
| 104 |
unsafe { msg_send![self, setDepth: depth] } |
| 105 |
} |
| 106 |
|
| 107 |
pub fn mipmap_level_count(&self) -> NSUInteger { |
| 108 |
unsafe { msg_send![self, mipmapLevelCount] } |
| 109 |
} |
| 110 |
|
| 111 |
pub fn set_mipmap_level_count(&self, count: NSUInteger) { |
| 112 |
unsafe { msg_send![self, setMipmapLevelCount: count] } |
| 113 |
} |
| 114 |
|
| 115 |
pub fn set_mipmap_level_count_for_size(&self, size: MTLSize) { |
| 116 |
let MTLSize { |
| 117 |
width, |
| 118 |
height, |
| 119 |
depth, |
| 120 |
} = size; |
| 121 |
let count = (width.max(height).max(depth) as f64).log2().ceil() as u64; |
| 122 |
self.set_mipmap_level_count(count); |
| 123 |
} |
| 124 |
|
| 125 |
pub fn sample_count(&self) -> NSUInteger { |
| 126 |
unsafe { msg_send![self, sampleCount] } |
| 127 |
} |
| 128 |
|
| 129 |
pub fn set_sample_count(&self, count: NSUInteger) { |
| 130 |
unsafe { msg_send![self, setSampleCount: count] } |
| 131 |
} |
| 132 |
|
| 133 |
pub fn array_length(&self) -> NSUInteger { |
| 134 |
unsafe { msg_send![self, arrayLength] } |
| 135 |
} |
| 136 |
|
| 137 |
pub fn set_array_length(&self, length: NSUInteger) { |
| 138 |
unsafe { msg_send![self, setArrayLength: length] } |
| 139 |
} |
| 140 |
|
| 141 |
pub fn resource_options(&self) -> MTLResourceOptions { |
| 142 |
unsafe { msg_send![self, resourceOptions] } |
| 143 |
} |
| 144 |
|
| 145 |
pub fn set_resource_options(&self, options: MTLResourceOptions) { |
| 146 |
unsafe { msg_send![self, setResourceOptions: options] } |
| 147 |
} |
| 148 |
|
| 149 |
pub fn cpu_cache_mode(&self) -> MTLCPUCacheMode { |
| 150 |
unsafe { msg_send![self, cpuCacheMode] } |
| 151 |
} |
| 152 |
|
| 153 |
pub fn set_cpu_cache_mode(&self, mode: MTLCPUCacheMode) { |
| 154 |
unsafe { msg_send![self, setCpuCacheMode: mode] } |
| 155 |
} |
| 156 |
|
| 157 |
pub fn storage_mode(&self) -> MTLStorageMode { |
| 158 |
unsafe { msg_send![self, storageMode] } |
| 159 |
} |
| 160 |
|
| 161 |
pub fn set_storage_mode(&self, mode: MTLStorageMode) { |
| 162 |
unsafe { msg_send![self, setStorageMode: mode] } |
| 163 |
} |
| 164 |
|
| 165 |
pub fn usage(&self) -> MTLTextureUsage { |
| 166 |
unsafe { msg_send![self, usage] } |
| 167 |
} |
| 168 |
|
| 169 |
pub fn set_usage(&self, usage: MTLTextureUsage) { |
| 170 |
unsafe { msg_send![self, setUsage: usage] } |
| 171 |
} |
| 172 |
|
| 173 |
pub fn compression_type(&self) -> MTLTextureCompressionType { |
| 174 |
unsafe { msg_send![self, compressionType] } |
| 175 |
} |
| 176 |
|
| 177 |
pub fn set_compression_type(&self, compression_type: MTLTextureCompressionType) { |
| 178 |
unsafe { msg_send![self, setCompressionType: compression_type] } |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/// See <https://developer.apple.com/documentation/metal/mtltexture> |
| 183 |
pub enum MTLTexture {} |
| 184 |
|
| 185 |
foreign_obj_type! { |
| 186 |
type CType = MTLTexture; |
| 187 |
pub struct Texture; |
| 188 |
type ParentType = Resource; |
| 189 |
} |
| 190 |
|
| 191 |
impl TextureRef { |
| 192 |
#[deprecated(since = "0.13.0")] |
| 193 |
pub fn root_resource(&self) -> Option<&ResourceRef> { |
| 194 |
unsafe { msg_send![self, rootResource] } |
| 195 |
} |
| 196 |
|
| 197 |
pub fn parent_texture(&self) -> Option<&TextureRef> { |
| 198 |
unsafe { msg_send![self, parentTexture] } |
| 199 |
} |
| 200 |
|
| 201 |
pub fn parent_relative_level(&self) -> NSUInteger { |
| 202 |
unsafe { msg_send![self, parentRelativeLevel] } |
| 203 |
} |
| 204 |
|
| 205 |
pub fn parent_relative_slice(&self) -> NSUInteger { |
| 206 |
unsafe { msg_send![self, parentRelativeSlice] } |
| 207 |
} |
| 208 |
|
| 209 |
pub fn buffer(&self) -> Option<&BufferRef> { |
| 210 |
unsafe { msg_send![self, buffer] } |
| 211 |
} |
| 212 |
|
| 213 |
pub fn buffer_offset(&self) -> NSUInteger { |
| 214 |
unsafe { msg_send![self, bufferOffset] } |
| 215 |
} |
| 216 |
|
| 217 |
pub fn buffer_stride(&self) -> NSUInteger { |
| 218 |
unsafe { msg_send![self, bufferBytesPerRow] } |
| 219 |
} |
| 220 |
|
| 221 |
pub fn texture_type(&self) -> MTLTextureType { |
| 222 |
unsafe { msg_send![self, textureType] } |
| 223 |
} |
| 224 |
|
| 225 |
pub fn pixel_format(&self) -> MTLPixelFormat { |
| 226 |
unsafe { msg_send![self, pixelFormat] } |
| 227 |
} |
| 228 |
|
| 229 |
pub fn width(&self) -> NSUInteger { |
| 230 |
unsafe { msg_send![self, width] } |
| 231 |
} |
| 232 |
|
| 233 |
pub fn height(&self) -> NSUInteger { |
| 234 |
unsafe { msg_send![self, height] } |
| 235 |
} |
| 236 |
|
| 237 |
pub fn depth(&self) -> NSUInteger { |
| 238 |
unsafe { msg_send![self, depth] } |
| 239 |
} |
| 240 |
|
| 241 |
pub fn mipmap_level_count(&self) -> NSUInteger { |
| 242 |
unsafe { msg_send![self, mipmapLevelCount] } |
| 243 |
} |
| 244 |
|
| 245 |
pub fn sample_count(&self) -> NSUInteger { |
| 246 |
unsafe { msg_send![self, sampleCount] } |
| 247 |
} |
| 248 |
|
| 249 |
pub fn array_length(&self) -> NSUInteger { |
| 250 |
unsafe { msg_send![self, arrayLength] } |
| 251 |
} |
| 252 |
|
| 253 |
pub fn usage(&self) -> MTLTextureUsage { |
| 254 |
unsafe { msg_send![self, usage] } |
| 255 |
} |
| 256 |
|
| 257 |
/// [framebufferOnly Apple Docs](https://developer.apple.com/documentation/metal/mtltexture/1515749-framebufferonly?language=objc) |
| 258 |
pub fn framebuffer_only(&self) -> bool { |
| 259 |
unsafe { msg_send_bool![self, isFramebufferOnly] } |
| 260 |
} |
| 261 |
|
| 262 |
pub fn get_bytes( |
| 263 |
&self, |
| 264 |
bytes: *mut std::ffi::c_void, |
| 265 |
stride: NSUInteger, |
| 266 |
region: MTLRegion, |
| 267 |
mipmap_level: NSUInteger, |
| 268 |
) { |
| 269 |
unsafe { |
| 270 |
msg_send![self, getBytes:bytes |
| 271 |
bytesPerRow:stride |
| 272 |
fromRegion:region |
| 273 |
mipmapLevel:mipmap_level] |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
pub fn get_bytes_in_slice( |
| 278 |
&self, |
| 279 |
bytes: *mut std::ffi::c_void, |
| 280 |
stride: NSUInteger, |
| 281 |
image_stride: NSUInteger, |
| 282 |
region: MTLRegion, |
| 283 |
mipmap_level: NSUInteger, |
| 284 |
slice: NSUInteger, |
| 285 |
) { |
| 286 |
unsafe { |
| 287 |
msg_send![self, getBytes:bytes |
| 288 |
bytesPerRow:stride |
| 289 |
bytesPerImage:image_stride |
| 290 |
fromRegion:region |
| 291 |
mipmapLevel:mipmap_level |
| 292 |
slice:slice] |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
pub fn replace_region( |
| 297 |
&self, |
| 298 |
region: MTLRegion, |
| 299 |
mipmap_level: NSUInteger, |
| 300 |
bytes: *const std::ffi::c_void, |
| 301 |
stride: NSUInteger, |
| 302 |
) { |
| 303 |
unsafe { |
| 304 |
msg_send![self, replaceRegion:region |
| 305 |
mipmapLevel:mipmap_level |
| 306 |
withBytes:bytes |
| 307 |
bytesPerRow:stride] |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
pub fn replace_region_in_slice( |
| 312 |
&self, |
| 313 |
region: MTLRegion, |
| 314 |
mipmap_level: NSUInteger, |
| 315 |
slice: NSUInteger, |
| 316 |
bytes: *const std::ffi::c_void, |
| 317 |
stride: NSUInteger, |
| 318 |
image_stride: NSUInteger, |
| 319 |
) { |
| 320 |
unsafe { |
| 321 |
msg_send![self, replaceRegion:region |
| 322 |
mipmapLevel:mipmap_level |
| 323 |
slice:slice |
| 324 |
withBytes:bytes |
| 325 |
bytesPerRow:stride |
| 326 |
bytesPerImage:image_stride] |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
pub fn new_texture_view(&self, pixel_format: MTLPixelFormat) -> Texture { |
| 331 |
unsafe { msg_send![self, newTextureViewWithPixelFormat: pixel_format] } |
| 332 |
} |
| 333 |
|
| 334 |
pub fn new_texture_view_from_slice( |
| 335 |
&self, |
| 336 |
pixel_format: MTLPixelFormat, |
| 337 |
texture_type: MTLTextureType, |
| 338 |
mipmap_levels: crate::NSRange, |
| 339 |
slices: crate::NSRange, |
| 340 |
) -> Texture { |
| 341 |
unsafe { |
| 342 |
msg_send![self, newTextureViewWithPixelFormat:pixel_format |
| 343 |
textureType:texture_type |
| 344 |
levels:mipmap_levels |
| 345 |
slices:slices] |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
pub fn gpu_resource_id(&self) -> MTLResourceID { |
| 350 |
unsafe { msg_send![self, gpuResourceID] } |
| 351 |
} |
| 352 |
} |