metal-texture3d.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /// Creates a three-dimensional ``MetalTexture`` instance with the specified usage options and the raw image data
  17. /// (if provided)
  18. /// - Parameters:
  19. /// - device: Opaque pointer to ``MetalDevice`` instance shared with `libobs`
  20. /// - size: Desired size of the texture
  21. /// - color_format: Desired color format of the texture as described by `gs_color_format`
  22. /// - levels: Amount of mip map levels to generate for the texture
  23. /// - data: Optional pointer to raw pixel data per mip map level
  24. /// - flags: Texture resource use information encoded as `libobs` bitfield
  25. /// - Returns: Opaque pointer to a created ``MetalTexture`` instance or a `NULL` pointer on error
  26. ///
  27. /// This function will create a new ``MTLTexture`` wrapped within a ``MetalTexture`` class and also upload any pixel
  28. /// data if non-`NULL` pointers have been provided via the `data` argument.
  29. ///
  30. /// > Important: If mipmap generation is requested, execution will be blocked by waiting for the blit command encoder
  31. /// to generate the mipmaps.
  32. @_cdecl("device_voltexture_create")
  33. public func device_voltexture_create(
  34. device: UnsafeRawPointer, width: UInt32, height: UInt32, depth: UInt32, color_format: gs_color_format,
  35. levels: UInt32, data: UnsafePointer<UnsafePointer<UInt8>?>?, flags: UInt32
  36. ) -> OpaquePointer? {
  37. let device = Unmanaged<MetalDevice>.fromOpaque(device).takeUnretainedValue()
  38. let descriptor = MTLTextureDescriptor.init(
  39. type: .type3D,
  40. width: width,
  41. height: height,
  42. depth: depth,
  43. colorFormat: color_format,
  44. levels: levels,
  45. flags: flags
  46. )
  47. guard let descriptor, let texture = MetalTexture(device: device, descriptor: descriptor) else {
  48. return nil
  49. }
  50. if let data {
  51. texture.upload(data: data, mipmapLevels: descriptor.mipmapLevelCount)
  52. }
  53. return texture.getRetained()
  54. }
  55. /// Requests deinitialization of the ``MetalTexture`` instance shared with `libobs`
  56. /// - Parameter texture: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  57. ///
  58. /// The ownership of the shared pointer is transferred into this function and the instance is placed under
  59. /// Swift's memory management again.
  60. @_cdecl("gs_voltexture_destroy")
  61. public func gs_voltexture_destroy(voltex: UnsafeRawPointer) {
  62. let _ = retained(voltex) as MetalTexture
  63. }
  64. /// Gets the width of the texture wrapped by the ``MetalTexture`` instance
  65. /// - Parameter voltex: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  66. /// - Returns: Width of the texture
  67. @_cdecl("gs_voltexture_get_width")
  68. public func gs_voltexture_get_width(voltex: UnsafeRawPointer) -> UInt32 {
  69. let texture: MetalTexture = unretained(voltex)
  70. return UInt32(texture.texture.width)
  71. }
  72. /// Gets the height of the texture wrapped by the ``MetalTexture`` instance
  73. /// - Parameter voltex: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  74. /// - Returns: Height of the texture
  75. @_cdecl("gs_voltexture_get_height")
  76. public func gs_voltexture_get_height(voltex: UnsafeRawPointer) -> UInt32 {
  77. let texture: MetalTexture = unretained(voltex)
  78. return UInt32(texture.texture.height)
  79. }
  80. /// Gets the depth of the texture wrapped by the ``Metaltexture`` instance
  81. /// - Parameter voltex: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  82. /// - Returns: Depth of the texture
  83. @_cdecl("gs_voltexture_get_depth")
  84. public func gs_voltexture_get_depth(voltex: UnsafeRawPointer) -> UInt32 {
  85. let texture: MetalTexture = unretained(voltex)
  86. return UInt32(texture.texture.depth)
  87. }
  88. /// Gets the color format of the texture wrapped by the ``MetalTexture`` instance
  89. /// - Parameter voltex: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  90. /// - Returns: Color format as defined by the `gs_color_format` enumeration
  91. @_cdecl("gs_voltexture_get_color_format")
  92. public func gs_voltexture_get_color_format(voltex: UnsafeRawPointer) -> gs_color_format {
  93. let texture: MetalTexture = unretained(voltex)
  94. return texture.texture.pixelFormat.gsColorFormat
  95. }