metal-vertexbuffer.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /// Creates a new ``MetalVertexBuffer`` instance with the given vertex buffer data and usage flags
  15. /// - Parameters:
  16. /// - device: Opaque pointer to ``MetalDevice`` instance shared with `libobs`
  17. /// - data: Pointer to `gs_vb_data` vertex buffer data created by `libobs`
  18. /// - flags: Usage flags encoded as `libobs` bitmask
  19. /// - Returns: Opaque pointer to a new ``MetalVertexBuffer`` instance if successful, `nil` otherwise
  20. ///
  21. /// > Note: The ownership of the memory pointed to by `data` is implicitly transferred to the ``MetalVertexBuffer``
  22. /// instance, but is not managed by Swift.
  23. @_cdecl("device_vertexbuffer_create")
  24. public func device_vertexbuffer_create(device: UnsafeRawPointer, data: UnsafeMutablePointer<gs_vb_data>, flags: UInt32)
  25. -> OpaquePointer
  26. {
  27. let device: MetalDevice = unretained(device)
  28. let vertexBuffer = MetalVertexBuffer(
  29. device: device,
  30. data: data,
  31. dynamic: (Int32(flags) & GS_DYNAMIC) != 0
  32. )
  33. return vertexBuffer.getRetained()
  34. }
  35. /// Requests the deinitialization of a shared ``MetalVertexBuffer`` instance
  36. /// - Parameter indexBuffer: Opaque pointer to ``MetalVertexBuffer`` instance shared with `libobs`
  37. ///
  38. /// The deinitialization is handled automatically by Swift after the ownership of the instance has been transferred
  39. /// into the function and becomes the last strong reference to it. After the function leaves its scope, the object will
  40. /// be deinitialized and deallocated automatically.
  41. ///
  42. /// > Note: The vertex buffer data memory is implicitly owned by the ``MetalVertexBuffer`` instance and will be
  43. /// manually cleaned up and deallocated by the instance's ``deinit`` method.
  44. @_cdecl("gs_vertexbuffer_destroy")
  45. public func gs_vertexbuffer_destroy(vertBuffer: UnsafeRawPointer) {
  46. let _ = retained(vertBuffer) as MetalVertexBuffer
  47. }
  48. /// Sets up a ``MetalVertexBuffer`` as the vertex buffer for the current pipeline
  49. /// - Parameters:
  50. /// - device: Opaque pointer to ``MetalDevice`` instance shared with `libobs`
  51. /// - vertbuffer: Opaque pointer to ``MetalVertexBuffer`` instance shared with `libobs`
  52. ///
  53. /// > Note: The reference count of the ``MetalVertexBuffer`` instance will not be increased by this call.
  54. ///
  55. /// > Important: If a `nil` pointer is provided as the vertex buffer, the index buffer will be _unset_.
  56. @_cdecl("device_load_vertexbuffer")
  57. public func device_load_vertexbuffer(device: UnsafeRawPointer, vertBuffer: UnsafeMutableRawPointer?) {
  58. let device: MetalDevice = unretained(device)
  59. if let vertBuffer {
  60. device.renderState.vertexBuffer = unretained(vertBuffer)
  61. } else {
  62. device.renderState.vertexBuffer = nil
  63. }
  64. }
  65. /// Requests the vertex buffer's current data to be transferred into GPU memory
  66. /// - Parameter vertBuffer: Opaque pointer to ``MetalVertexBuffer`` instance shared with `libobs`
  67. ///
  68. /// This function will call `gs_vertexbuffer_flush_direct` with a `nil` pointer as the data pointer.
  69. @_cdecl("gs_vertexbuffer_flush")
  70. public func gs_vertexbuffer_flush(vertbuffer: UnsafeRawPointer) {
  71. gs_vertexbuffer_flush_direct(vertbuffer: vertbuffer, data: nil)
  72. }
  73. /// Requests the vertex buffer to be updated with the provided data and then transferred into GPU memory
  74. /// - Parameters:
  75. /// - vertBuffer: Opaque pointer to ``MetalVertexBuffer`` instance shared with `libobs`
  76. /// - data: Opaque pointer to vertex buffer data set up by `libobs`
  77. ///
  78. /// This function is called to ensure that the vertex buffer data that is contained in the memory pointed at by the
  79. /// `data` argument is uploaded into GPU memory.
  80. ///
  81. /// If a `nil` pointer is provided instead, the data provided to the instance during creation will be used instead.
  82. @_cdecl("gs_vertexbuffer_flush_direct")
  83. public func gs_vertexbuffer_flush_direct(vertbuffer: UnsafeRawPointer, data: UnsafeMutablePointer<gs_vb_data>?) {
  84. let vertexBuffer: MetalVertexBuffer = unretained(vertbuffer)
  85. vertexBuffer.setupBuffers(data: data)
  86. }
  87. /// Returns an opaque pointer to the vertex buffer data associated with the ``MetalVertexBuffer`` instance
  88. /// - Parameter vertBuffer: Opaque pointer to ``MetalVertexBuffer`` instance shared with `libobs`
  89. /// - Returns: Opaque pointer to index buffer data in memory
  90. ///
  91. /// The returned opaque pointer represents the unchanged memory address that was provided for the creation of the index
  92. /// buffer object.
  93. ///
  94. /// > Warning: There is only limited memory safety associated with this pointer. It is implicitly owned and its
  95. /// lifetime is managed by the ``MetalVertexBuffer``
  96. /// instance, but it was originally created by `libobs`.
  97. @_cdecl("gs_vertexbuffer_get_data")
  98. public func gs_vertexbuffer_get_data(vertBuffer: UnsafeRawPointer) -> UnsafeMutablePointer<gs_vb_data>? {
  99. let vertexBuffer: MetalVertexBuffer = unretained(vertBuffer)
  100. return vertexBuffer.vertexData
  101. }