GpuAccessor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.PrintWarning(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.MemoryAccessor.Read<T>(address);
  79. }
  80. /// <summary>
  81. /// Queries Local Size X for compute shaders.
  82. /// </summary>
  83. /// <returns>Local Size X</returns>
  84. public int QueryComputeLocalSizeX() => _localSizeX;
  85. /// <summary>
  86. /// Queries Local Size Y for compute shaders.
  87. /// </summary>
  88. /// <returns>Local Size Y</returns>
  89. public int QueryComputeLocalSizeY() => _localSizeY;
  90. /// <summary>
  91. /// Queries Local Size Z for compute shaders.
  92. /// </summary>
  93. /// <returns>Local Size Z</returns>
  94. public int QueryComputeLocalSizeZ() => _localSizeZ;
  95. /// <summary>
  96. /// Queries Local Memory size in bytes for compute shaders.
  97. /// </summary>
  98. /// <returns>Local Memory size in bytes</returns>
  99. public int QueryComputeLocalMemorySize() => _localMemorySize;
  100. /// <summary>
  101. /// Queries Shared Memory size in bytes for compute shaders.
  102. /// </summary>
  103. /// <returns>Shared Memory size in bytes</returns>
  104. public int QueryComputeSharedMemorySize() => _sharedMemorySize;
  105. /// <summary>
  106. /// Queries texture target information.
  107. /// </summary>
  108. /// <param name="handle">Texture handle</param>
  109. /// <returns>True if the texture is a buffer texture, false otherwise</returns>
  110. public bool QueryIsTextureBuffer(int handle)
  111. {
  112. return GetTextureDescriptor(handle).UnpackTextureTarget() == TextureTarget.TextureBuffer;
  113. }
  114. /// <summary>
  115. /// Queries texture target information.
  116. /// </summary>
  117. /// <param name="handle">Texture handle</param>
  118. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  119. public bool QueryIsTextureRectangle(int handle)
  120. {
  121. var descriptor = GetTextureDescriptor(handle);
  122. TextureTarget target = descriptor.UnpackTextureTarget();
  123. bool is2DTexture = target == TextureTarget.Texture2D ||
  124. target == TextureTarget.Texture2DRect;
  125. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  126. }
  127. /// <summary>
  128. /// Queries current primitive topology for geometry shaders.
  129. /// </summary>
  130. /// <returns>Current primitive topology</returns>
  131. public InputTopology QueryPrimitiveTopology()
  132. {
  133. switch (_context.Methods.PrimitiveType)
  134. {
  135. case PrimitiveType.Points:
  136. return InputTopology.Points;
  137. case PrimitiveType.Lines:
  138. case PrimitiveType.LineLoop:
  139. case PrimitiveType.LineStrip:
  140. return InputTopology.Lines;
  141. case PrimitiveType.LinesAdjacency:
  142. case PrimitiveType.LineStripAdjacency:
  143. return InputTopology.LinesAdjacency;
  144. case PrimitiveType.Triangles:
  145. case PrimitiveType.TriangleStrip:
  146. case PrimitiveType.TriangleFan:
  147. return InputTopology.Triangles;
  148. case PrimitiveType.TrianglesAdjacency:
  149. case PrimitiveType.TriangleStripAdjacency:
  150. return InputTopology.TrianglesAdjacency;
  151. }
  152. return InputTopology.Points;
  153. }
  154. /// <summary>
  155. /// Queries host storage buffer alignment required.
  156. /// </summary>
  157. /// <returns>Host storage buffer alignment in bytes</returns>
  158. public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  159. /// <summary>
  160. /// Queries host GPU non-constant texture offset support.
  161. /// </summary>
  162. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  163. public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  164. /// <summary>
  165. /// Queries texture format information, for shaders using image load or store.
  166. /// </summary>
  167. /// <remarks>
  168. /// This only returns non-compressed color formats.
  169. /// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
  170. /// </remarks>
  171. /// <param name="handle">Texture handle</param>
  172. /// <returns>Color format of the non-compressed texture</returns>
  173. public TextureFormat QueryTextureFormat(int handle)
  174. {
  175. var descriptor = GetTextureDescriptor(handle);
  176. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  177. {
  178. return TextureFormat.Unknown;
  179. }
  180. return formatInfo.Format switch
  181. {
  182. Format.R8Unorm => TextureFormat.R8Unorm,
  183. Format.R8Snorm => TextureFormat.R8Snorm,
  184. Format.R8Uint => TextureFormat.R8Uint,
  185. Format.R8Sint => TextureFormat.R8Sint,
  186. Format.R16Float => TextureFormat.R16Float,
  187. Format.R16Unorm => TextureFormat.R16Unorm,
  188. Format.R16Snorm => TextureFormat.R16Snorm,
  189. Format.R16Uint => TextureFormat.R16Uint,
  190. Format.R16Sint => TextureFormat.R16Sint,
  191. Format.R32Float => TextureFormat.R32Float,
  192. Format.R32Uint => TextureFormat.R32Uint,
  193. Format.R32Sint => TextureFormat.R32Sint,
  194. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  195. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  196. Format.R8G8Uint => TextureFormat.R8G8Uint,
  197. Format.R8G8Sint => TextureFormat.R8G8Sint,
  198. Format.R16G16Float => TextureFormat.R16G16Float,
  199. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  200. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  201. Format.R16G16Uint => TextureFormat.R16G16Uint,
  202. Format.R16G16Sint => TextureFormat.R16G16Sint,
  203. Format.R32G32Float => TextureFormat.R32G32Float,
  204. Format.R32G32Uint => TextureFormat.R32G32Uint,
  205. Format.R32G32Sint => TextureFormat.R32G32Sint,
  206. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  207. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  208. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  209. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  210. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  211. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  212. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  213. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  214. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  215. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  216. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  217. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  218. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  219. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  220. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  221. _ => TextureFormat.Unknown
  222. };
  223. }
  224. /// <summary>
  225. /// Gets the texture descriptor for a given texture on the pool.
  226. /// </summary>
  227. /// <param name="handle">Index of the texture (this is the shader "fake" handle)</param>
  228. /// <returns>Texture descriptor</returns>
  229. private Image.TextureDescriptor GetTextureDescriptor(int handle)
  230. {
  231. if (_compute)
  232. {
  233. return _context.Methods.TextureManager.GetComputeTextureDescriptor(_state, handle);
  234. }
  235. else
  236. {
  237. return _context.Methods.TextureManager.GetGraphicsTextureDescriptor(_state, _stageIndex, handle);
  238. }
  239. }
  240. }
  241. }