1
0

MTLTexture+Extensions.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /******************************************************************************
  2. Copyright (C) 2024 by Patrick Heyer <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. import Foundation
  15. import Metal
  16. extension MTLTexture {
  17. /// Creates an opaque pointer of a ``MTLTexture`` instance and increases the reference count.
  18. /// - Returns: Opaque pointer for the ``MTLTexture``
  19. func getRetained() -> OpaquePointer {
  20. let retained = Unmanaged.passRetained(self).toOpaque()
  21. return OpaquePointer(retained)
  22. }
  23. /// Creates an opaque pointer of a ``MTLTexture`` instance without increasing the reference count.
  24. /// - Returns: Opaque pointer for the ``MTLTexture``
  25. func getUnretained() -> OpaquePointer {
  26. let unretained = Unmanaged.passUnretained(self).toOpaque()
  27. return OpaquePointer(unretained)
  28. }
  29. }
  30. extension MTLTexture {
  31. /// Convenience property to get the texture's size as a ``MTLSize`` object
  32. var size: MTLSize {
  33. .init(
  34. width: self.width,
  35. height: self.height,
  36. depth: self.depth
  37. )
  38. }
  39. /// Convenience property to get the texture's region as a ``MTLRegion`` object
  40. var region: MTLRegion {
  41. .init(
  42. origin: .init(x: 0, y: 0, z: 0),
  43. size: self.size
  44. )
  45. }
  46. /// Gets a new ``MTLTextureDescriptor`` instance with the properties of the texture
  47. var descriptor: MTLTextureDescriptor {
  48. let descriptor = MTLTextureDescriptor()
  49. descriptor.textureType = self.textureType
  50. descriptor.pixelFormat = self.pixelFormat
  51. descriptor.width = self.width
  52. descriptor.height = self.height
  53. descriptor.depth = self.depth
  54. descriptor.mipmapLevelCount = self.mipmapLevelCount
  55. descriptor.sampleCount = self.sampleCount
  56. descriptor.arrayLength = self.arrayLength
  57. descriptor.storageMode = self.storageMode
  58. descriptor.cpuCacheMode = self.cpuCacheMode
  59. descriptor.usage = self.usage
  60. descriptor.allowGPUOptimizedContents = self.allowGPUOptimizedContents
  61. return descriptor
  62. }
  63. }