MTLTextureDescriptor+Extensions.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Metal
  15. extension MTLTextureDescriptor {
  16. /// Convenience initializer for a texture descriptor with `libobs` data
  17. /// - Parameters:
  18. /// - type: Metal texture type
  19. /// - width: Width of texture
  20. /// - height: Height of texture
  21. /// - depth: Depth of texture
  22. /// - colorFormat: `libobs` color format for the texture
  23. /// - levels: Mip map levels
  24. /// - flags: Additional usage flags as `libobs` bitfield
  25. convenience init?(
  26. type: MTLTextureType, width: UInt32, height: UInt32, depth: UInt32, colorFormat: gs_color_format,
  27. levels: UInt32, flags: UInt32
  28. ) {
  29. let arrayLength: Int
  30. switch type {
  31. case .type2D:
  32. arrayLength = 1
  33. case .type3D:
  34. arrayLength = 1
  35. case .typeCube:
  36. arrayLength = 6
  37. default:
  38. assertionFailure("MTLTextureDescriptor: Unsupported texture type for libobs initializer")
  39. return nil
  40. }
  41. self.init()
  42. self.textureType = type
  43. self.pixelFormat = colorFormat.mtlFormat
  44. self.width = Int(width)
  45. self.height = Int(height)
  46. self.depth = Int(depth)
  47. self.sampleCount = 1
  48. self.arrayLength = arrayLength
  49. self.cpuCacheMode = .defaultCache
  50. self.allowGPUOptimizedContents = true
  51. self.hazardTrackingMode = .default
  52. if (Int32(flags) & GS_BUILD_MIPMAPS) != 0 {
  53. self.mipmapLevelCount = Int(levels)
  54. } else {
  55. self.mipmapLevelCount = 1
  56. }
  57. if (Int32(flags) & GS_RENDER_TARGET) != 0 {
  58. self.storageMode = .private
  59. self.usage = [.shaderRead, .renderTarget]
  60. } else {
  61. self.storageMode = .shared
  62. self.usage = [.shaderRead]
  63. }
  64. }
  65. convenience init?(width: UInt32, height: UInt32, colorFormat: gs_zstencil_format) {
  66. self.init()
  67. self.textureType = .type2D
  68. self.pixelFormat = colorFormat.mtlFormat
  69. self.width = Int(width)
  70. self.height = Int(height)
  71. self.depth = 1
  72. self.sampleCount = 1
  73. self.arrayLength = 1
  74. self.cpuCacheMode = .defaultCache
  75. self.allowGPUOptimizedContents = true
  76. self.hazardTrackingMode = .default
  77. self.mipmapLevelCount = 1
  78. self.storageMode = .private
  79. self.usage = [.shaderRead]
  80. }
  81. }