GpuState.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Gpu.State
  6. {
  7. class GpuState
  8. {
  9. private const int RegistersCount = 0xe00;
  10. public delegate void MethodCallback(int argument);
  11. private int[] _backingMemory;
  12. private struct Register
  13. {
  14. public MethodCallback Callback;
  15. public StateWriteFlags WriteFlag;
  16. }
  17. private Register[] _registers;
  18. public StateWriteFlags StateWriteFlags { get; set; }
  19. public GpuState()
  20. {
  21. _backingMemory = new int[RegistersCount];
  22. _registers = new Register[RegistersCount];
  23. StateWriteFlags = StateWriteFlags.Any;
  24. InitializeDefaultState();
  25. InitializeStateWatchers();
  26. }
  27. public bool ExitEarly;
  28. public void CallMethod(MethodParams meth)
  29. {
  30. if (ExitEarly)
  31. {
  32. return;
  33. }
  34. Register register = _registers[meth.Method];
  35. if (_backingMemory[meth.Method] != meth.Argument)
  36. {
  37. StateWriteFlags |= register.WriteFlag;
  38. }
  39. _backingMemory[meth.Method] = meth.Argument;
  40. MethodCallback callback = register.Callback;
  41. if (callback != null)
  42. {
  43. callback(meth.Argument);
  44. }
  45. }
  46. public int Read(int offset)
  47. {
  48. return _backingMemory[offset];
  49. }
  50. public void RegisterCopyBufferCallback(MethodCallback callback)
  51. {
  52. RegisterCallback(0xc0, callback);
  53. }
  54. public void RegisterCopyTextureCallback(MethodCallback callback)
  55. {
  56. RegisterCallback(0x237, callback);
  57. }
  58. public void RegisterDrawEndCallback(MethodCallback callback)
  59. {
  60. RegisterCallback(0x585, callback);
  61. }
  62. public void RegisterDrawBeginCallback(MethodCallback callback)
  63. {
  64. RegisterCallback(0x586, callback);
  65. }
  66. public void RegisterSetIndexCountCallback(MethodCallback callback)
  67. {
  68. RegisterCallback(0x5f8, callback);
  69. }
  70. public void RegisterClearCallback(MethodCallback callback)
  71. {
  72. RegisterCallback(0x674, callback);
  73. }
  74. public void RegisterReportCallback(MethodCallback callback)
  75. {
  76. RegisterCallback(0x6c3, callback);
  77. }
  78. public void RegisterUniformBufferUpdateCallback(MethodCallback callback)
  79. {
  80. for (int index = 0; index < 16; index++)
  81. {
  82. RegisterCallback(0x8e4 + index, callback);
  83. }
  84. }
  85. public void RegisterUniformBufferBind0Callback(MethodCallback callback)
  86. {
  87. RegisterCallback(0x904, callback);
  88. }
  89. public void RegisterUniformBufferBind1Callback(MethodCallback callback)
  90. {
  91. RegisterCallback(0x90c, callback);
  92. }
  93. public void RegisterUniformBufferBind2Callback(MethodCallback callback)
  94. {
  95. RegisterCallback(0x914, callback);
  96. }
  97. public void RegisterUniformBufferBind3Callback(MethodCallback callback)
  98. {
  99. RegisterCallback(0x91c, callback);
  100. }
  101. public void RegisterUniformBufferBind4Callback(MethodCallback callback)
  102. {
  103. RegisterCallback(0x924, callback);
  104. }
  105. public CopyTexture GetCopyDstTexture()
  106. {
  107. return Get<CopyTexture>(MethodOffset.CopyDstTexture);
  108. }
  109. public CopyTexture GetCopySrcTexture()
  110. {
  111. return Get<CopyTexture>(MethodOffset.CopySrcTexture);
  112. }
  113. public RtColorState GetRtColorState(int index)
  114. {
  115. return Get<RtColorState>(MethodOffset.RtColorState + 16 * index);
  116. }
  117. public CopyTextureControl GetCopyTextureControl()
  118. {
  119. return Get<CopyTextureControl>(MethodOffset.CopyTextureControl);
  120. }
  121. public CopyRegion GetCopyRegion()
  122. {
  123. return Get<CopyRegion>(MethodOffset.CopyRegion);
  124. }
  125. public ViewportTransform GetViewportTransform(int index)
  126. {
  127. return Get<ViewportTransform>(MethodOffset.ViewportTransform + 8 * index);
  128. }
  129. public ViewportExtents GetViewportExtents(int index)
  130. {
  131. return Get<ViewportExtents>(MethodOffset.ViewportExtents + 4 * index);
  132. }
  133. public VertexBufferDrawState GetVertexBufferDrawState()
  134. {
  135. return Get<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState);
  136. }
  137. public ClearColors GetClearColors()
  138. {
  139. return Get<ClearColors>(MethodOffset.ClearColors);
  140. }
  141. public float GetClearDepthValue()
  142. {
  143. return Get<float>(MethodOffset.ClearDepthValue);
  144. }
  145. public int GetClearStencilValue()
  146. {
  147. return _backingMemory[(int)MethodOffset.ClearStencilValue];
  148. }
  149. public StencilBackMasks GetStencilBackMasks()
  150. {
  151. return Get<StencilBackMasks>(MethodOffset.StencilBackMasks);
  152. }
  153. public RtDepthStencilState GetRtDepthStencilState()
  154. {
  155. return Get<RtDepthStencilState>(MethodOffset.RtDepthStencilState);
  156. }
  157. public VertexAttribState GetVertexAttribState(int index)
  158. {
  159. return Get<VertexAttribState>(MethodOffset.VertexAttribState + index);
  160. }
  161. public Size3D GetRtDepthStencilSize()
  162. {
  163. return Get<Size3D>(MethodOffset.RtDepthStencilSize);
  164. }
  165. public Bool GetDepthTestEnable()
  166. {
  167. return Get<Bool>(MethodOffset.DepthTestEnable);
  168. }
  169. public CompareOp GetDepthTestFunc()
  170. {
  171. return Get<CompareOp>(MethodOffset.DepthTestFunc);
  172. }
  173. public Bool GetDepthWriteEnable()
  174. {
  175. return Get<Bool>(MethodOffset.DepthWriteEnable);
  176. }
  177. public Bool GetBlendEnable(int index)
  178. {
  179. return Get<Bool>(MethodOffset.BlendEnable + index);
  180. }
  181. public StencilTestState GetStencilTestState()
  182. {
  183. return Get<StencilTestState>(MethodOffset.StencilTestState);
  184. }
  185. public int GetBaseVertex()
  186. {
  187. return _backingMemory[(int)MethodOffset.FirstVertex];
  188. }
  189. public int GetBaseInstance()
  190. {
  191. return _backingMemory[(int)MethodOffset.FirstInstance];
  192. }
  193. public PoolState GetSamplerPoolState()
  194. {
  195. return Get<PoolState>(MethodOffset.SamplerPoolState);
  196. }
  197. public PoolState GetTexturePoolState()
  198. {
  199. return Get<PoolState>(MethodOffset.TexturePoolState);
  200. }
  201. public StencilBackTestState GetStencilBackTestState()
  202. {
  203. return Get<StencilBackTestState>(MethodOffset.StencilBackTestState);
  204. }
  205. public TextureMsaaMode GetRtMsaaMode()
  206. {
  207. return Get<TextureMsaaMode>(MethodOffset.RtMsaaMode);
  208. }
  209. public GpuVa GetShaderBaseAddress()
  210. {
  211. return Get<GpuVa>(MethodOffset.ShaderBaseAddress);
  212. }
  213. public PrimitiveRestartState GetPrimitiveRestartState()
  214. {
  215. return Get<PrimitiveRestartState>(MethodOffset.PrimitiveRestartState);
  216. }
  217. public IndexBufferState GetIndexBufferState()
  218. {
  219. return Get<IndexBufferState>(MethodOffset.IndexBufferState);
  220. }
  221. public FaceState GetFaceState()
  222. {
  223. return Get<FaceState>(MethodOffset.FaceState);
  224. }
  225. public ReportState GetReportState()
  226. {
  227. return Get<ReportState>(MethodOffset.ReportState);
  228. }
  229. public VertexBufferState GetVertexBufferState(int index)
  230. {
  231. return Get<VertexBufferState>(MethodOffset.VertexBufferState + 4 * index);
  232. }
  233. public BlendState GetBlendState(int index)
  234. {
  235. return Get<BlendState>(MethodOffset.BlendState + 8 * index);
  236. }
  237. public GpuVa GetVertexBufferEndAddress(int index)
  238. {
  239. return Get<GpuVa>(MethodOffset.VertexBufferEndAddress + 2 * index);
  240. }
  241. public ShaderState GetShaderState(int index)
  242. {
  243. return Get<ShaderState>(MethodOffset.ShaderState + 16 * index);
  244. }
  245. public UniformBufferState GetUniformBufferState()
  246. {
  247. return Get<UniformBufferState>(MethodOffset.UniformBufferState);
  248. }
  249. public void SetUniformBufferOffset(int offset)
  250. {
  251. _backingMemory[(int)MethodOffset.UniformBufferState + 3] = offset;
  252. }
  253. public int GetTextureBufferIndex()
  254. {
  255. return _backingMemory[(int)MethodOffset.TextureBufferIndex];
  256. }
  257. private void InitializeDefaultState()
  258. {
  259. // Depth ranges.
  260. for (int index = 0; index < 8; index++)
  261. {
  262. _backingMemory[(int)MethodOffset.ViewportExtents + index * 4 + 2] = 0;
  263. _backingMemory[(int)MethodOffset.ViewportExtents + index * 4 + 3] = 0x3F800000;
  264. }
  265. // Default front stencil mask.
  266. _backingMemory[0x4e7] = 0xff;
  267. // Default color mask.
  268. _backingMemory[(int)MethodOffset.RtColorMask] = 0x1111;
  269. }
  270. private void InitializeStateWatchers()
  271. {
  272. SetWriteStateFlag(MethodOffset.RtColorState, StateWriteFlags.RtColorState, 16 * 8);
  273. SetWriteStateFlag(MethodOffset.ViewportTransform, StateWriteFlags.ViewportTransform, 8 * 8);
  274. SetWriteStateFlag(MethodOffset.ViewportExtents, StateWriteFlags.ViewportTransform, 4 * 8);
  275. SetWriteStateFlag<VertexBufferDrawState>(MethodOffset.VertexBufferDrawState, StateWriteFlags.VertexBufferState);
  276. SetWriteStateFlag<DepthBiasState>(MethodOffset.DepthBiasState, StateWriteFlags.DepthBiasState);
  277. SetWriteStateFlag(MethodOffset.DepthBiasFactor, StateWriteFlags.DepthBiasState, 1);
  278. SetWriteStateFlag(MethodOffset.DepthBiasUnits, StateWriteFlags.DepthBiasState, 1);
  279. SetWriteStateFlag(MethodOffset.DepthBiasClamp, StateWriteFlags.DepthBiasState, 1);
  280. SetWriteStateFlag<RtDepthStencilState>(MethodOffset.RtDepthStencilState, StateWriteFlags.RtDepthStencilState);
  281. SetWriteStateFlag<Size3D> (MethodOffset.RtDepthStencilSize, StateWriteFlags.RtDepthStencilState);
  282. SetWriteStateFlag(MethodOffset.DepthTestEnable, StateWriteFlags.DepthTestState, 1);
  283. SetWriteStateFlag(MethodOffset.DepthWriteEnable, StateWriteFlags.DepthTestState, 1);
  284. SetWriteStateFlag(MethodOffset.DepthTestFunc, StateWriteFlags.DepthTestState, 1);
  285. SetWriteStateFlag(MethodOffset.VertexAttribState, StateWriteFlags.VertexAttribState, 16);
  286. SetWriteStateFlag<StencilBackMasks> (MethodOffset.StencilBackMasks, StateWriteFlags.StencilTestState);
  287. SetWriteStateFlag<StencilTestState> (MethodOffset.StencilTestState, StateWriteFlags.StencilTestState);
  288. SetWriteStateFlag<StencilBackTestState>(MethodOffset.StencilBackTestState, StateWriteFlags.StencilTestState);
  289. SetWriteStateFlag<PoolState>(MethodOffset.SamplerPoolState, StateWriteFlags.SamplerPoolState);
  290. SetWriteStateFlag<PoolState>(MethodOffset.TexturePoolState, StateWriteFlags.TexturePoolState);
  291. SetWriteStateFlag<ShaderState>(MethodOffset.ShaderBaseAddress, StateWriteFlags.ShaderState);
  292. SetWriteStateFlag<PrimitiveRestartState>(MethodOffset.PrimitiveRestartState, StateWriteFlags.PrimitiveRestartState);
  293. SetWriteStateFlag<IndexBufferState>(MethodOffset.IndexBufferState, StateWriteFlags.IndexBufferState);
  294. SetWriteStateFlag<FaceState>(MethodOffset.FaceState, StateWriteFlags.FaceState);
  295. SetWriteStateFlag<RtColorMask>(MethodOffset.RtColorMask, StateWriteFlags.RtColorMask);
  296. SetWriteStateFlag(MethodOffset.VertexBufferInstanced, StateWriteFlags.VertexBufferState, 16);
  297. SetWriteStateFlag(MethodOffset.VertexBufferState, StateWriteFlags.VertexBufferState, 4 * 16);
  298. SetWriteStateFlag(MethodOffset.VertexBufferEndAddress, StateWriteFlags.VertexBufferState, 2 * 16);
  299. SetWriteStateFlag(MethodOffset.BlendEnable, StateWriteFlags.BlendState, 8);
  300. SetWriteStateFlag(MethodOffset.BlendState, StateWriteFlags.BlendState, 8 * 8);
  301. SetWriteStateFlag(MethodOffset.ShaderState, StateWriteFlags.ShaderState, 16 * 6);
  302. SetWriteStateFlag(MethodOffset.TextureBufferIndex, StateWriteFlags.TexturePoolState, 1);
  303. }
  304. private void SetWriteStateFlag<T>(MethodOffset offset, StateWriteFlags flag)
  305. {
  306. SetWriteStateFlag(offset, flag, Marshal.SizeOf<T>());
  307. }
  308. private void SetWriteStateFlag(MethodOffset offset, StateWriteFlags flag, int size)
  309. {
  310. for (int index = 0; index < size; index++)
  311. {
  312. _registers[(int)offset + index].WriteFlag = flag;
  313. }
  314. }
  315. public void RegisterCallback(MethodOffset offset, MethodCallback callback)
  316. {
  317. _registers[(int)offset].Callback = callback;
  318. }
  319. private void RegisterCallback(int offset, MethodCallback callback)
  320. {
  321. _registers[offset].Callback = callback;
  322. }
  323. public T Get<T>(MethodOffset offset) where T : struct
  324. {
  325. return MemoryMarshal.Cast<int, T>(_backingMemory.AsSpan().Slice((int)offset))[0];
  326. }
  327. }
  328. }