GpuAccessor.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Shader;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Shader
  7. {
  8. /// <summary>
  9. /// Represents a GPU state and memory accessor.
  10. /// </summary>
  11. class GpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
  12. {
  13. private readonly GpuChannel _channel;
  14. private readonly GpuAccessorState _state;
  15. private readonly int _stageIndex;
  16. private readonly bool _compute;
  17. private readonly int _localSizeX;
  18. private readonly int _localSizeY;
  19. private readonly int _localSizeZ;
  20. private readonly int _localMemorySize;
  21. private readonly int _sharedMemorySize;
  22. public int Cb1DataSize { get; private set; }
  23. /// <summary>
  24. /// Creates a new instance of the GPU state accessor for graphics shader translation.
  25. /// </summary>
  26. /// <param name="context">GPU context</param>
  27. /// <param name="channel">GPU channel</param>
  28. /// <param name="state">Current GPU state</param>
  29. /// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
  30. public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state, int stageIndex) : base(context)
  31. {
  32. _channel = channel;
  33. _state = state;
  34. _stageIndex = stageIndex;
  35. }
  36. /// <summary>
  37. /// Creates a new instance of the GPU state accessor for compute shader translation.
  38. /// </summary>
  39. /// <param name="context">GPU context</param>
  40. /// <param name="channel">GPU channel</param>
  41. /// <param name="state">Current GPU state</param>
  42. /// <param name="localSizeX">Local group size X of the compute shader</param>
  43. /// <param name="localSizeY">Local group size Y of the compute shader</param>
  44. /// <param name="localSizeZ">Local group size Z of the compute shader</param>
  45. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  46. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  47. public GpuAccessor(
  48. GpuContext context,
  49. GpuChannel channel,
  50. GpuAccessorState state,
  51. int localSizeX,
  52. int localSizeY,
  53. int localSizeZ,
  54. int localMemorySize,
  55. int sharedMemorySize) : base(context)
  56. {
  57. _channel = channel;
  58. _state = state;
  59. _compute = true;
  60. _localSizeX = localSizeX;
  61. _localSizeY = localSizeY;
  62. _localSizeZ = localSizeZ;
  63. _localMemorySize = localMemorySize;
  64. _sharedMemorySize = sharedMemorySize;
  65. }
  66. /// <summary>
  67. /// Reads data from the constant buffer 1.
  68. /// </summary>
  69. /// <param name="offset">Offset in bytes to read from</param>
  70. /// <returns>Value at the given offset</returns>
  71. public uint ConstantBuffer1Read(int offset)
  72. {
  73. if (Cb1DataSize < offset + 4)
  74. {
  75. Cb1DataSize = offset + 4;
  76. }
  77. ulong baseAddress = _compute
  78. ? _channel.BufferManager.GetComputeUniformBufferAddress(1)
  79. : _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1);
  80. return _channel.MemoryManager.Physical.Read<uint>(baseAddress + (ulong)offset);
  81. }
  82. /// <summary>
  83. /// Prints a log message.
  84. /// </summary>
  85. /// <param name="message">Message to print</param>
  86. public void Log(string message)
  87. {
  88. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  89. }
  90. /// <summary>
  91. /// Gets a span of the specified memory location, containing shader code.
  92. /// </summary>
  93. /// <param name="address">GPU virtual address of the data</param>
  94. /// <param name="minimumSize">Minimum size that the returned span may have</param>
  95. /// <returns>Span of the memory location</returns>
  96. public override ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  97. {
  98. int size = Math.Max(minimumSize, 0x1000 - (int)(address & 0xfff));
  99. return MemoryMarshal.Cast<byte, ulong>(_channel.MemoryManager.GetSpan(address, size));
  100. }
  101. /// <summary>
  102. /// Queries Local Size X for compute shaders.
  103. /// </summary>
  104. /// <returns>Local Size X</returns>
  105. public int QueryComputeLocalSizeX() => _localSizeX;
  106. /// <summary>
  107. /// Queries Local Size Y for compute shaders.
  108. /// </summary>
  109. /// <returns>Local Size Y</returns>
  110. public int QueryComputeLocalSizeY() => _localSizeY;
  111. /// <summary>
  112. /// Queries Local Size Z for compute shaders.
  113. /// </summary>
  114. /// <returns>Local Size Z</returns>
  115. public int QueryComputeLocalSizeZ() => _localSizeZ;
  116. /// <summary>
  117. /// Queries Local Memory size in bytes for compute shaders.
  118. /// </summary>
  119. /// <returns>Local Memory size in bytes</returns>
  120. public int QueryComputeLocalMemorySize() => _localMemorySize;
  121. /// <summary>
  122. /// Queries Shared Memory size in bytes for compute shaders.
  123. /// </summary>
  124. /// <returns>Shared Memory size in bytes</returns>
  125. public int QueryComputeSharedMemorySize() => _sharedMemorySize;
  126. /// <summary>
  127. /// Queries Constant Buffer usage information.
  128. /// </summary>
  129. /// <returns>A mask where each bit set indicates a bound constant buffer</returns>
  130. public uint QueryConstantBufferUse()
  131. {
  132. return _compute
  133. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  134. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  135. }
  136. /// <summary>
  137. /// Queries current primitive topology for geometry shaders.
  138. /// </summary>
  139. /// <returns>Current primitive topology</returns>
  140. public InputTopology QueryPrimitiveTopology()
  141. {
  142. return _state.Topology switch
  143. {
  144. PrimitiveTopology.Points => InputTopology.Points,
  145. PrimitiveTopology.Lines or
  146. PrimitiveTopology.LineLoop or
  147. PrimitiveTopology.LineStrip => InputTopology.Lines,
  148. PrimitiveTopology.LinesAdjacency or
  149. PrimitiveTopology.LineStripAdjacency => InputTopology.LinesAdjacency,
  150. PrimitiveTopology.Triangles or
  151. PrimitiveTopology.TriangleStrip or
  152. PrimitiveTopology.TriangleFan => InputTopology.Triangles,
  153. PrimitiveTopology.TrianglesAdjacency or
  154. PrimitiveTopology.TriangleStripAdjacency => InputTopology.TrianglesAdjacency,
  155. PrimitiveTopology.Patches => _state.TessellationMode.UnpackPatchType() == TessPatchType.Isolines
  156. ? InputTopology.Lines
  157. : InputTopology.Triangles,
  158. _ => InputTopology.Points
  159. };
  160. }
  161. /// <summary>
  162. /// Queries the tessellation evaluation shader primitive winding order.
  163. /// </summary>
  164. /// <returns>True if the primitive winding order is clockwise, false if counter-clockwise</returns>
  165. public bool QueryTessCw() => _state.TessellationMode.UnpackCw();
  166. /// <summary>
  167. /// Queries the tessellation evaluation shader abstract patch type.
  168. /// </summary>
  169. /// <returns>Abstract patch type</returns>
  170. public TessPatchType QueryTessPatchType() => _state.TessellationMode.UnpackPatchType();
  171. /// <summary>
  172. /// Queries the tessellation evaluation shader spacing between tessellated vertices of the patch.
  173. /// </summary>
  174. /// <returns>Spacing between tessellated vertices of the patch</returns>
  175. public TessSpacing QueryTessSpacing() => _state.TessellationMode.UnpackSpacing();
  176. /// <summary>
  177. /// Gets the texture descriptor for a given texture on the pool.
  178. /// </summary>
  179. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  180. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  181. /// <returns>Texture descriptor</returns>
  182. public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  183. {
  184. if (_compute)
  185. {
  186. return _channel.TextureManager.GetComputeTextureDescriptor(
  187. _state.TexturePoolGpuVa,
  188. _state.TextureBufferIndex,
  189. _state.TexturePoolMaximumId,
  190. handle,
  191. cbufSlot);
  192. }
  193. else
  194. {
  195. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  196. _state.TexturePoolGpuVa,
  197. _state.TextureBufferIndex,
  198. _state.TexturePoolMaximumId,
  199. _stageIndex,
  200. handle,
  201. cbufSlot);
  202. }
  203. }
  204. /// <summary>
  205. /// Queries if host state forces early depth testing.
  206. /// </summary>
  207. /// <returns>True if early depth testing is forced</returns>
  208. public bool QueryEarlyZForce()
  209. {
  210. return _state.EarlyZForce;
  211. }
  212. }
  213. }