GpuAccessor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Gpu.State;
  5. using Ryujinx.Graphics.Shader;
  6. namespace Ryujinx.Graphics.Gpu.Shader
  7. {
  8. /// <summary>
  9. /// Represents a GPU state and memory accessor.
  10. /// </summary>
  11. class GpuAccessor : IGpuAccessor
  12. {
  13. private readonly GpuContext _context;
  14. private readonly GpuState _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. /// <summary>
  23. /// Creates a new instance of the GPU state accessor for graphics shader translation.
  24. /// </summary>
  25. /// <param name="context">GPU context</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, GpuState state, int stageIndex)
  29. {
  30. _context = context;
  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="state">Current GPU state</param>
  39. /// <param name="localSizeX">Local group size X of the compute shader</param>
  40. /// <param name="localSizeY">Local group size Y of the compute shader</param>
  41. /// <param name="localSizeZ">Local group size Z of the compute shader</param>
  42. /// <param name="localMemorySize">Local memory size of the compute shader</param>
  43. /// <param name="sharedMemorySize">Shared memory size of the compute shader</param>
  44. public GpuAccessor(
  45. GpuContext context,
  46. GpuState state,
  47. int localSizeX,
  48. int localSizeY,
  49. int localSizeZ,
  50. int localMemorySize,
  51. int sharedMemorySize)
  52. {
  53. _context = context;
  54. _state = state;
  55. _compute = true;
  56. _localSizeX = localSizeX;
  57. _localSizeY = localSizeY;
  58. _localSizeZ = localSizeZ;
  59. _localMemorySize = localMemorySize;
  60. _sharedMemorySize = sharedMemorySize;
  61. }
  62. /// <summary>
  63. /// Prints a log message.
  64. /// </summary>
  65. /// <param name="message">Message to print</param>
  66. public void Log(string message)
  67. {
  68. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  69. }
  70. /// <summary>
  71. /// Reads data from GPU memory.
  72. /// </summary>
  73. /// <typeparam name="T">Type of the data to be read</typeparam>
  74. /// <param name="address">GPU virtual address of the data</param>
  75. /// <returns>Data at the memory location</returns>
  76. public T MemoryRead<T>(ulong address) where T : unmanaged
  77. {
  78. return _context.MemoryManager.Read<T>(address);
  79. }
  80. /// <summary>
  81. /// Checks if a given memory address is mapped.
  82. /// </summary>
  83. /// <param name="address">GPU virtual address to be checked</param>
  84. /// <returns>True if the address is mapped, false otherwise</returns>
  85. public bool MemoryMapped(ulong address)
  86. {
  87. return _context.MemoryManager.IsMapped(address);
  88. }
  89. /// <summary>
  90. /// Queries Local Size X for compute shaders.
  91. /// </summary>
  92. /// <returns>Local Size X</returns>
  93. public int QueryComputeLocalSizeX() => _localSizeX;
  94. /// <summary>
  95. /// Queries Local Size Y for compute shaders.
  96. /// </summary>
  97. /// <returns>Local Size Y</returns>
  98. public int QueryComputeLocalSizeY() => _localSizeY;
  99. /// <summary>
  100. /// Queries Local Size Z for compute shaders.
  101. /// </summary>
  102. /// <returns>Local Size Z</returns>
  103. public int QueryComputeLocalSizeZ() => _localSizeZ;
  104. /// <summary>
  105. /// Queries Local Memory size in bytes for compute shaders.
  106. /// </summary>
  107. /// <returns>Local Memory size in bytes</returns>
  108. public int QueryComputeLocalMemorySize() => _localMemorySize;
  109. /// <summary>
  110. /// Queries Shared Memory size in bytes for compute shaders.
  111. /// </summary>
  112. /// <returns>Shared Memory size in bytes</returns>
  113. public int QueryComputeSharedMemorySize() => _sharedMemorySize;
  114. /// <summary>
  115. /// Queries Constant Buffer usage information.
  116. /// </summary>
  117. /// <returns>A mask where each bit set indicates a bound constant buffer</returns>
  118. public uint QueryConstantBufferUse()
  119. {
  120. return _compute
  121. ? _context.Methods.BufferManager.GetComputeUniformBufferUseMask()
  122. : _context.Methods.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  123. }
  124. /// <summary>
  125. /// Queries texture target information.
  126. /// </summary>
  127. /// <param name="handle">Texture handle</param>
  128. /// <returns>True if the texture is a buffer texture, false otherwise</returns>
  129. public bool QueryIsTextureBuffer(int handle)
  130. {
  131. return GetTextureDescriptor(handle).UnpackTextureTarget() == TextureTarget.TextureBuffer;
  132. }
  133. /// <summary>
  134. /// Queries texture target information.
  135. /// </summary>
  136. /// <param name="handle">Texture handle</param>
  137. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  138. public bool QueryIsTextureRectangle(int handle)
  139. {
  140. var descriptor = GetTextureDescriptor(handle);
  141. TextureTarget target = descriptor.UnpackTextureTarget();
  142. bool is2DTexture = target == TextureTarget.Texture2D ||
  143. target == TextureTarget.Texture2DRect;
  144. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  145. }
  146. /// <summary>
  147. /// Queries current primitive topology for geometry shaders.
  148. /// </summary>
  149. /// <returns>Current primitive topology</returns>
  150. public InputTopology QueryPrimitiveTopology()
  151. {
  152. switch (_context.Methods.Topology)
  153. {
  154. case PrimitiveTopology.Points:
  155. return InputTopology.Points;
  156. case PrimitiveTopology.Lines:
  157. case PrimitiveTopology.LineLoop:
  158. case PrimitiveTopology.LineStrip:
  159. return InputTopology.Lines;
  160. case PrimitiveTopology.LinesAdjacency:
  161. case PrimitiveTopology.LineStripAdjacency:
  162. return InputTopology.LinesAdjacency;
  163. case PrimitiveTopology.Triangles:
  164. case PrimitiveTopology.TriangleStrip:
  165. case PrimitiveTopology.TriangleFan:
  166. return InputTopology.Triangles;
  167. case PrimitiveTopology.TrianglesAdjacency:
  168. case PrimitiveTopology.TriangleStripAdjacency:
  169. return InputTopology.TrianglesAdjacency;
  170. }
  171. return InputTopology.Points;
  172. }
  173. /// <summary>
  174. /// Queries host storage buffer alignment required.
  175. /// </summary>
  176. /// <returns>Host storage buffer alignment in bytes</returns>
  177. public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  178. /// <summary>
  179. /// Queries host support for readable images without a explicit format declaration on the shader.
  180. /// </summary>
  181. /// <returns>True if formatted image load is supported, false otherwise</returns>
  182. public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  183. /// <summary>
  184. /// Queries host GPU non-constant texture offset support.
  185. /// </summary>
  186. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  187. public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  188. /// <summary>
  189. /// Queries texture format information, for shaders using image load or store.
  190. /// </summary>
  191. /// <remarks>
  192. /// This only returns non-compressed color formats.
  193. /// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
  194. /// </remarks>
  195. /// <param name="handle">Texture handle</param>
  196. /// <returns>Color format of the non-compressed texture</returns>
  197. public TextureFormat QueryTextureFormat(int handle)
  198. {
  199. var descriptor = GetTextureDescriptor(handle);
  200. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  201. {
  202. return TextureFormat.Unknown;
  203. }
  204. return formatInfo.Format switch
  205. {
  206. Format.R8Unorm => TextureFormat.R8Unorm,
  207. Format.R8Snorm => TextureFormat.R8Snorm,
  208. Format.R8Uint => TextureFormat.R8Uint,
  209. Format.R8Sint => TextureFormat.R8Sint,
  210. Format.R16Float => TextureFormat.R16Float,
  211. Format.R16Unorm => TextureFormat.R16Unorm,
  212. Format.R16Snorm => TextureFormat.R16Snorm,
  213. Format.R16Uint => TextureFormat.R16Uint,
  214. Format.R16Sint => TextureFormat.R16Sint,
  215. Format.R32Float => TextureFormat.R32Float,
  216. Format.R32Uint => TextureFormat.R32Uint,
  217. Format.R32Sint => TextureFormat.R32Sint,
  218. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  219. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  220. Format.R8G8Uint => TextureFormat.R8G8Uint,
  221. Format.R8G8Sint => TextureFormat.R8G8Sint,
  222. Format.R16G16Float => TextureFormat.R16G16Float,
  223. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  224. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  225. Format.R16G16Uint => TextureFormat.R16G16Uint,
  226. Format.R16G16Sint => TextureFormat.R16G16Sint,
  227. Format.R32G32Float => TextureFormat.R32G32Float,
  228. Format.R32G32Uint => TextureFormat.R32G32Uint,
  229. Format.R32G32Sint => TextureFormat.R32G32Sint,
  230. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  231. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  232. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  233. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  234. Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
  235. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  236. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  237. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  238. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  239. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  240. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  241. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  242. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  243. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  244. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  245. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  246. _ => TextureFormat.Unknown
  247. };
  248. }
  249. /// <summary>
  250. /// Gets the texture descriptor for a given texture on the pool.
  251. /// </summary>
  252. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  253. /// <returns>Texture descriptor</returns>
  254. private Image.TextureDescriptor GetTextureDescriptor(int handle)
  255. {
  256. if (_compute)
  257. {
  258. return _context.Methods.TextureManager.GetComputeTextureDescriptor(_state, handle);
  259. }
  260. else
  261. {
  262. return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(_state, _stageIndex, handle);
  263. }
  264. }
  265. }
  266. }