metal-zstencilbuffer.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 ``MetalTexture`` for use as a depth stencil attachment
  17. /// - Parameters:
  18. /// - device: Opaque pointer to ``MetalDevice`` instance shared with `libobs`
  19. /// - width: Desired width of the texture
  20. /// - height: Desired height of the texture
  21. /// - color_format: Desired color format of the depth stencil attachment as described by `gs_zstencil_format`
  22. /// - Returns: Opaque pointer to a created ``MetalTexture`` instance or a `NULL` pointer on error
  23. @_cdecl("device_zstencil_create")
  24. public func device_zstencil_create(device: UnsafeRawPointer, width: UInt32, height: UInt32, format: gs_zstencil_format)
  25. -> OpaquePointer?
  26. {
  27. let device: MetalDevice = unretained(device)
  28. let descriptor = MTLTextureDescriptor.init(
  29. width: width,
  30. height: height,
  31. colorFormat: format
  32. )
  33. guard let descriptor, let texture = MetalTexture(device: device, descriptor: descriptor) else {
  34. return nil
  35. }
  36. return texture.getRetained()
  37. }
  38. /// Gets the ``MetalTexture`` instance used as the depth stencil attachment for the current pipeline
  39. /// - Parameter device: Opaque pointer to ``MetalDevice`` instance shared with `libobs`
  40. /// - Returns: Opaque pointer to ``MetalTexture`` instance if any is set, `nil` otherwise
  41. @_cdecl("device_get_zstencil_target")
  42. public func device_get_zstencil_target(device: UnsafeRawPointer) -> OpaquePointer? {
  43. let device: MetalDevice = unretained(device)
  44. guard let stencilAttachment = device.renderState.depthStencilAttachment else {
  45. return nil
  46. }
  47. return stencilAttachment.getUnretained()
  48. }
  49. /// Requests deinitialization of the ``MetalTexture`` instance shared with `libobs`
  50. /// - Parameter zstencil: Opaque pointer to ``MetalTexture`` instance shared with `libobs`
  51. ///
  52. /// The ownership of the shared pointer is transferred into this function and the instance is placed under Swift's
  53. /// memory management again.
  54. @_cdecl("gs_zstencil_destroy")
  55. public func gs_zstencil_destroy(zstencil: UnsafeRawPointer) {
  56. let _ = retained(zstencil) as MetalTexture
  57. }