MetalRenderState.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. import simd
  17. /// The MetalRenderState struct emulates a state object like Direct3D's `ID3D11DeviceContext`, holding references to
  18. /// elements of a render pipeline that would be considered the "current" variant of each.
  19. ///
  20. /// Typical "current" state elements include (but are not limited to):
  21. ///
  22. /// * Variant of the render target for linear color writes
  23. /// * Variant of the render target for color writes with automatic sRGB gamma encoding
  24. /// * View matrix and view projection matrix
  25. /// * Vertex buffer and optional index buffer
  26. /// * Depth stencil attachment
  27. /// * Vertex shader
  28. /// * Fragment shader
  29. /// * View port size
  30. /// * Cull mode
  31. ///
  32. /// These references are swapped out by OBS for each "scene" and "scene items" within it before issuing draw calls,
  33. /// thus actual pipelines need to be created "on demand" based on the pipeline descriptor and stored in a cache to
  34. /// avoid the cost of pipeline validation on consecutive render passes.
  35. struct MetalRenderState {
  36. var viewMatrix: matrix_float4x4
  37. var projectionMatrix: matrix_float4x4
  38. var viewProjectionMatrix: matrix_float4x4
  39. var renderTarget: MetalTexture?
  40. var sRGBrenderTarget: MetalTexture?
  41. var depthStencilAttachment: MetalTexture?
  42. var isRendertargetChanged = false
  43. var vertexBuffer: MetalVertexBuffer?
  44. var indexBuffer: MetalIndexBuffer?
  45. var vertexShader: MetalShader?
  46. var fragmentShader: MetalShader?
  47. var viewPort = MTLViewport()
  48. var cullMode = MTLCullMode.none
  49. var scissorRectEnabled: Bool
  50. var scissorRect: MTLScissorRect?
  51. var gsColorSpace: gs_color_space
  52. var useSRGBGamma = false
  53. var swapChain: OBSSwapChain?
  54. var isInDisplaysRenderStage = false
  55. var pipelineDescriptor = MTLRenderPipelineDescriptor()
  56. var clearPipelineDescriptor = MTLRenderPipelineDescriptor()
  57. var renderPassDescriptor = MTLRenderPassDescriptor()
  58. var depthStencilDescriptor = MTLDepthStencilDescriptor()
  59. var commandBuffer: MTLCommandBuffer?
  60. var textures = [MTLTexture?](repeating: nil, count: Int(GS_MAX_TEXTURES))
  61. var samplers = [MTLSamplerState?](repeating: nil, count: Int(GS_MAX_TEXTURES))
  62. var projections = [matrix_float4x4]()
  63. var inFlightRenderTargets = Set<MetalTexture>()
  64. }