GpuAccessor.cs 8.4 KB

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