EncoderState.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Ryujinx.Common.Memory;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Metal.State;
  4. using Ryujinx.Graphics.Shader;
  5. using SharpMetal.Metal;
  6. using System;
  7. using System.Runtime.Versioning;
  8. namespace Ryujinx.Graphics.Metal
  9. {
  10. [Flags]
  11. enum DirtyFlags
  12. {
  13. None = 0,
  14. RenderPipeline = 1 << 0,
  15. ComputePipeline = 1 << 1,
  16. DepthStencil = 1 << 2,
  17. DepthClamp = 1 << 3,
  18. DepthBias = 1 << 4,
  19. CullMode = 1 << 5,
  20. FrontFace = 1 << 6,
  21. StencilRef = 1 << 7,
  22. Viewports = 1 << 8,
  23. Scissors = 1 << 9,
  24. Uniforms = 1 << 10,
  25. Storages = 1 << 11,
  26. Textures = 1 << 12,
  27. Images = 1 << 13,
  28. ArgBuffers = Uniforms | Storages | Textures | Images,
  29. RenderAll = RenderPipeline | DepthStencil | DepthClamp | DepthBias | CullMode | FrontFace | StencilRef | Viewports | Scissors | ArgBuffers,
  30. ComputeAll = ComputePipeline | ArgBuffers,
  31. All = RenderAll | ComputeAll,
  32. }
  33. record struct BufferRef
  34. {
  35. public Auto<DisposableBuffer> Buffer;
  36. public BufferRange? Range;
  37. public BufferRef(Auto<DisposableBuffer> buffer)
  38. {
  39. Buffer = buffer;
  40. }
  41. public BufferRef(Auto<DisposableBuffer> buffer, ref BufferRange range)
  42. {
  43. Buffer = buffer;
  44. Range = range;
  45. }
  46. }
  47. record struct TextureRef
  48. {
  49. public ShaderStage Stage;
  50. public TextureBase Storage;
  51. public Auto<DisposableSampler> Sampler;
  52. public Format ImageFormat;
  53. public TextureRef(ShaderStage stage, TextureBase storage, Auto<DisposableSampler> sampler)
  54. {
  55. Stage = stage;
  56. Storage = storage;
  57. Sampler = sampler;
  58. }
  59. }
  60. record struct ImageRef
  61. {
  62. public ShaderStage Stage;
  63. public Texture Storage;
  64. public ImageRef(ShaderStage stage, Texture storage)
  65. {
  66. Stage = stage;
  67. Storage = storage;
  68. }
  69. }
  70. struct PredrawState
  71. {
  72. public MTLCullMode CullMode;
  73. public DepthStencilUid DepthStencilUid;
  74. public PrimitiveTopology Topology;
  75. public MTLViewport[] Viewports;
  76. }
  77. struct RenderTargetCopy
  78. {
  79. public MTLScissorRect[] Scissors;
  80. public Texture DepthStencil;
  81. public Texture[] RenderTargets;
  82. }
  83. [SupportedOSPlatform("macos")]
  84. class EncoderState
  85. {
  86. public Program RenderProgram = null;
  87. public Program ComputeProgram = null;
  88. public PipelineState Pipeline;
  89. public DepthStencilUid DepthStencilUid;
  90. public readonly record struct ArrayRef<T>(ShaderStage Stage, T Array);
  91. public readonly BufferRef[] UniformBufferRefs = new BufferRef[Constants.MaxUniformBufferBindings];
  92. public readonly BufferRef[] StorageBufferRefs = new BufferRef[Constants.MaxStorageBufferBindings];
  93. public readonly TextureRef[] TextureRefs = new TextureRef[Constants.MaxTextureBindings * 2];
  94. public readonly ImageRef[] ImageRefs = new ImageRef[Constants.MaxImageBindings * 2];
  95. public ArrayRef<TextureArray>[] TextureArrayRefs = [];
  96. public ArrayRef<ImageArray>[] ImageArrayRefs = [];
  97. public ArrayRef<TextureArray>[] TextureArrayExtraRefs = [];
  98. public ArrayRef<ImageArray>[] ImageArrayExtraRefs = [];
  99. public IndexBufferState IndexBuffer = default;
  100. public MTLDepthClipMode DepthClipMode = MTLDepthClipMode.Clip;
  101. public float DepthBias;
  102. public float SlopeScale;
  103. public float Clamp;
  104. public int BackRefValue = 0;
  105. public int FrontRefValue = 0;
  106. public PrimitiveTopology Topology = PrimitiveTopology.Triangles;
  107. public MTLCullMode CullMode = MTLCullMode.None;
  108. public MTLWinding Winding = MTLWinding.CounterClockwise;
  109. public bool CullBoth = false;
  110. public MTLViewport[] Viewports = new MTLViewport[Constants.MaxViewports];
  111. public MTLScissorRect[] Scissors = new MTLScissorRect[Constants.MaxViewports];
  112. // Changes to attachments take recreation!
  113. public Texture DepthStencil;
  114. public Texture[] RenderTargets = new Texture[Constants.MaxColorAttachments];
  115. public ITexture PreMaskDepthStencil = default;
  116. public ITexture[] PreMaskRenderTargets;
  117. public bool FramebufferUsingColorWriteMask;
  118. public Array8<ColorBlendStateUid> StoredBlend;
  119. public ColorF BlendColor = new();
  120. public readonly VertexBufferState[] VertexBuffers = new VertexBufferState[Constants.MaxVertexBuffers];
  121. public readonly VertexAttribDescriptor[] VertexAttribs = new VertexAttribDescriptor[Constants.MaxVertexAttributes];
  122. // Dirty flags
  123. public DirtyFlags Dirty = DirtyFlags.None;
  124. // Only to be used for present
  125. public bool ClearLoadAction = false;
  126. public RenderEncoderBindings RenderEncoderBindings = new();
  127. public ComputeEncoderBindings ComputeEncoderBindings = new();
  128. public EncoderState()
  129. {
  130. Pipeline.Initialize();
  131. DepthStencilUid.DepthCompareFunction = MTLCompareFunction.Always;
  132. }
  133. public RenderTargetCopy InheritForClear(EncoderState other, bool depth, int singleIndex = -1)
  134. {
  135. // Inherit render target related information without causing a render encoder split.
  136. var oldState = new RenderTargetCopy
  137. {
  138. Scissors = other.Scissors,
  139. RenderTargets = other.RenderTargets,
  140. DepthStencil = other.DepthStencil
  141. };
  142. Scissors = other.Scissors;
  143. RenderTargets = other.RenderTargets;
  144. DepthStencil = other.DepthStencil;
  145. Pipeline.ColorBlendAttachmentStateCount = other.Pipeline.ColorBlendAttachmentStateCount;
  146. Pipeline.Internal.ColorBlendState = other.Pipeline.Internal.ColorBlendState;
  147. Pipeline.DepthStencilFormat = other.Pipeline.DepthStencilFormat;
  148. ref var blendStates = ref Pipeline.Internal.ColorBlendState;
  149. // Mask out irrelevant attachments.
  150. for (int i = 0; i < blendStates.Length; i++)
  151. {
  152. if (depth || (singleIndex != -1 && singleIndex != i))
  153. {
  154. blendStates[i].WriteMask = MTLColorWriteMask.None;
  155. }
  156. }
  157. return oldState;
  158. }
  159. public void Restore(RenderTargetCopy copy)
  160. {
  161. Scissors = copy.Scissors;
  162. RenderTargets = copy.RenderTargets;
  163. DepthStencil = copy.DepthStencil;
  164. Pipeline.Internal.ResetColorState();
  165. }
  166. }
  167. }