GpuAccessor.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 GpuContext _context;
  12. private readonly GpuChannel _channel;
  13. private readonly GpuAccessorState _state;
  14. private readonly int _stageIndex;
  15. private readonly bool _compute;
  16. private readonly int _localSizeX;
  17. private readonly int _localSizeY;
  18. private readonly int _localSizeZ;
  19. private readonly int _localMemorySize;
  20. private readonly int _sharedMemorySize;
  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)
  29. {
  30. _context = context;
  31. _channel = channel;
  32. _state = state;
  33. _stageIndex = stageIndex;
  34. }
  35. /// <summary>
  36. /// Creates a new instance of the GPU state accessor for compute shader translation.
  37. /// </summary>
  38. /// <param name="context">GPU context</param>
  39. /// <param name="channel">GPU channel</param>
  40. /// <param name="state">Current GPU state</param>
  41. /// <param name="localSizeX">Local group size X of the compute shader</param>
  42. /// <param name="localSizeY">Local group size Y of the compute shader</param>
  43. /// <param name="localSizeZ">Local group size Z of the compute shader</param>
  44. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  45. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  46. public GpuAccessor(
  47. GpuContext context,
  48. GpuChannel channel,
  49. GpuAccessorState state,
  50. int localSizeX,
  51. int localSizeY,
  52. int localSizeZ,
  53. int localMemorySize,
  54. int sharedMemorySize)
  55. {
  56. _context = context;
  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. /// Prints a log message.
  68. /// </summary>
  69. /// <param name="message">Message to print</param>
  70. public void Log(string message)
  71. {
  72. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  73. }
  74. /// <summary>
  75. /// Reads data from GPU memory.
  76. /// </summary>
  77. /// <typeparam name="T">Type of the data to be read</typeparam>
  78. /// <param name="address">GPU virtual address of the data</param>
  79. /// <returns>Data at the memory location</returns>
  80. public override T MemoryRead<T>(ulong address)
  81. {
  82. return _channel.MemoryManager.Read<T>(address);
  83. }
  84. /// <summary>
  85. /// Checks if a given memory address is mapped.
  86. /// </summary>
  87. /// <param name="address">GPU virtual address to be checked</param>
  88. /// <returns>True if the address is mapped, false otherwise</returns>
  89. public bool MemoryMapped(ulong address)
  90. {
  91. return _channel.MemoryManager.IsMapped(address);
  92. }
  93. /// <summary>
  94. /// Queries Local Size X for compute shaders.
  95. /// </summary>
  96. /// <returns>Local Size X</returns>
  97. public int QueryComputeLocalSizeX() => _localSizeX;
  98. /// <summary>
  99. /// Queries Local Size Y for compute shaders.
  100. /// </summary>
  101. /// <returns>Local Size Y</returns>
  102. public int QueryComputeLocalSizeY() => _localSizeY;
  103. /// <summary>
  104. /// Queries Local Size Z for compute shaders.
  105. /// </summary>
  106. /// <returns>Local Size Z</returns>
  107. public int QueryComputeLocalSizeZ() => _localSizeZ;
  108. /// <summary>
  109. /// Queries Local Memory size in bytes for compute shaders.
  110. /// </summary>
  111. /// <returns>Local Memory size in bytes</returns>
  112. public int QueryComputeLocalMemorySize() => _localMemorySize;
  113. /// <summary>
  114. /// Queries Shared Memory size in bytes for compute shaders.
  115. /// </summary>
  116. /// <returns>Shared Memory size in bytes</returns>
  117. public int QueryComputeSharedMemorySize() => _sharedMemorySize;
  118. /// <summary>
  119. /// Queries Constant Buffer usage information.
  120. /// </summary>
  121. /// <returns>A mask where each bit set indicates a bound constant buffer</returns>
  122. public uint QueryConstantBufferUse()
  123. {
  124. return _compute
  125. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  126. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  127. }
  128. /// <summary>
  129. /// Queries current primitive topology for geometry shaders.
  130. /// </summary>
  131. /// <returns>Current primitive topology</returns>
  132. public InputTopology QueryPrimitiveTopology()
  133. {
  134. return _state.Topology switch
  135. {
  136. PrimitiveTopology.Points => InputTopology.Points,
  137. PrimitiveTopology.Lines or
  138. PrimitiveTopology.LineLoop or
  139. PrimitiveTopology.LineStrip => InputTopology.Lines,
  140. PrimitiveTopology.LinesAdjacency or
  141. PrimitiveTopology.LineStripAdjacency => InputTopology.LinesAdjacency,
  142. PrimitiveTopology.Triangles or
  143. PrimitiveTopology.TriangleStrip or
  144. PrimitiveTopology.TriangleFan => InputTopology.Triangles,
  145. PrimitiveTopology.TrianglesAdjacency or
  146. PrimitiveTopology.TriangleStripAdjacency => InputTopology.TrianglesAdjacency,
  147. _ => InputTopology.Points,
  148. };
  149. }
  150. /// <summary>
  151. /// Queries host storage buffer alignment required.
  152. /// </summary>
  153. /// <returns>Host storage buffer alignment in bytes</returns>
  154. public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  155. /// <summary>
  156. /// Queries host support for readable images without a explicit format declaration on the shader.
  157. /// </summary>
  158. /// <returns>True if formatted image load is supported, false otherwise</returns>
  159. public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  160. /// <summary>
  161. /// Queries host GPU non-constant texture offset support.
  162. /// </summary>
  163. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  164. public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  165. /// <summary>
  166. /// Queries host GPU texture shadow LOD support.
  167. /// </summary>
  168. /// <returns>True if the GPU and driver supports texture shadow LOD, false otherwise</returns>
  169. public bool QuerySupportsTextureShadowLod() => _context.Capabilities.SupportsTextureShadowLod;
  170. /// <summary>
  171. /// Gets the texture descriptor for a given texture on the pool.
  172. /// </summary>
  173. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  174. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  175. /// <returns>Texture descriptor</returns>
  176. public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  177. {
  178. if (_compute)
  179. {
  180. return _channel.TextureManager.GetComputeTextureDescriptor(
  181. _state.TexturePoolGpuVa,
  182. _state.TextureBufferIndex,
  183. _state.TexturePoolMaximumId,
  184. handle,
  185. cbufSlot);
  186. }
  187. else
  188. {
  189. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  190. _state.TexturePoolGpuVa,
  191. _state.TextureBufferIndex,
  192. _state.TexturePoolMaximumId,
  193. _stageIndex,
  194. handle,
  195. cbufSlot);
  196. }
  197. }
  198. /// <summary>
  199. /// Queries if host state forces early depth testing.
  200. /// </summary>
  201. /// <returns>True if early depth testing is forced</returns>
  202. public bool QueryEarlyZForce()
  203. {
  204. return _state.EarlyZForce;
  205. }
  206. }
  207. }