CachedGpuAccessor.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Gpu.Shader.Cache.Definition;
  3. using Ryujinx.Graphics.Shader;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Gpu.Shader
  8. {
  9. class CachedGpuAccessor : TextureDescriptorCapableGpuAccessor, IGpuAccessor
  10. {
  11. private readonly ReadOnlyMemory<byte> _data;
  12. private readonly ReadOnlyMemory<byte> _cb1Data;
  13. private readonly GuestGpuAccessorHeader _header;
  14. private readonly Dictionary<int, GuestTextureDescriptor> _textureDescriptors;
  15. private readonly TransformFeedbackDescriptor[] _tfd;
  16. /// <summary>
  17. /// Creates a new instance of the cached GPU state accessor for shader translation.
  18. /// </summary>
  19. /// <param name="context">GPU context</param>
  20. /// <param name="data">The data of the shader</param>
  21. /// <param name="cb1Data">The constant buffer 1 data of the shader</param>
  22. /// <param name="header">The cache of the GPU accessor</param>
  23. /// <param name="guestTextureDescriptors">The cache of the texture descriptors</param>
  24. public CachedGpuAccessor(
  25. GpuContext context,
  26. ReadOnlyMemory<byte> data,
  27. ReadOnlyMemory<byte> cb1Data,
  28. GuestGpuAccessorHeader header,
  29. IReadOnlyDictionary<int, GuestTextureDescriptor> guestTextureDescriptors,
  30. TransformFeedbackDescriptor[] tfd) : base(context)
  31. {
  32. _data = data;
  33. _cb1Data = cb1Data;
  34. _header = header;
  35. _textureDescriptors = new Dictionary<int, GuestTextureDescriptor>();
  36. foreach (KeyValuePair<int, GuestTextureDescriptor> guestTextureDescriptor in guestTextureDescriptors)
  37. {
  38. _textureDescriptors.Add(guestTextureDescriptor.Key, guestTextureDescriptor.Value);
  39. }
  40. _tfd = tfd;
  41. }
  42. /// <summary>
  43. /// Reads data from the constant buffer 1.
  44. /// </summary>
  45. /// <param name="offset">Offset in bytes to read from</param>
  46. /// <returns>Value at the given offset</returns>
  47. public uint ConstantBuffer1Read(int offset)
  48. {
  49. return MemoryMarshal.Cast<byte, uint>(_cb1Data.Span.Slice(offset))[0];
  50. }
  51. /// <summary>
  52. /// Prints a log message.
  53. /// </summary>
  54. /// <param name="message">Message to print</param>
  55. public void Log(string message)
  56. {
  57. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  58. }
  59. /// <summary>
  60. /// Gets a span of the specified memory location, containing shader code.
  61. /// </summary>
  62. /// <param name="address">GPU virtual address of the data</param>
  63. /// <param name="minimumSize">Minimum size that the returned span may have</param>
  64. /// <returns>Span of the memory location</returns>
  65. public override ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  66. {
  67. return MemoryMarshal.Cast<byte, ulong>(_data.Span.Slice((int)address));
  68. }
  69. /// <summary>
  70. /// Checks if a given memory address is mapped.
  71. /// </summary>
  72. /// <param name="address">GPU virtual address to be checked</param>
  73. /// <returns>True if the address is mapped, false otherwise</returns>
  74. public bool MemoryMapped(ulong address)
  75. {
  76. return address < (ulong)_data.Length;
  77. }
  78. /// <summary>
  79. /// Queries Local Size X for compute shaders.
  80. /// </summary>
  81. /// <returns>Local Size X</returns>
  82. public int QueryComputeLocalSizeX()
  83. {
  84. return _header.ComputeLocalSizeX;
  85. }
  86. /// <summary>
  87. /// Queries Local Size Y for compute shaders.
  88. /// </summary>
  89. /// <returns>Local Size Y</returns>
  90. public int QueryComputeLocalSizeY()
  91. {
  92. return _header.ComputeLocalSizeY;
  93. }
  94. /// <summary>
  95. /// Queries Local Size Z for compute shaders.
  96. /// </summary>
  97. /// <returns>Local Size Z</returns>
  98. public int QueryComputeLocalSizeZ()
  99. {
  100. return _header.ComputeLocalSizeZ;
  101. }
  102. /// <summary>
  103. /// Queries Local Memory size in bytes for compute shaders.
  104. /// </summary>
  105. /// <returns>Local Memory size in bytes</returns>
  106. public int QueryComputeLocalMemorySize()
  107. {
  108. return _header.ComputeLocalMemorySize;
  109. }
  110. /// <summary>
  111. /// Queries Shared Memory size in bytes for compute shaders.
  112. /// </summary>
  113. /// <returns>Shared Memory size in bytes</returns>
  114. public int QueryComputeSharedMemorySize()
  115. {
  116. return _header.ComputeSharedMemorySize;
  117. }
  118. /// <summary>
  119. /// Queries current primitive topology for geometry shaders.
  120. /// </summary>
  121. /// <returns>Current primitive topology</returns>
  122. public InputTopology QueryPrimitiveTopology()
  123. {
  124. return _header.PrimitiveTopology;
  125. }
  126. /// <summary>
  127. /// Queries the tessellation evaluation shader primitive winding order.
  128. /// </summary>
  129. /// <returns>True if the primitive winding order is clockwise, false if counter-clockwise</returns>
  130. public bool QueryTessCw()
  131. {
  132. return (_header.TessellationModePacked & 0x10) != 0;
  133. }
  134. /// <summary>
  135. /// Queries the tessellation evaluation shader abstract patch type.
  136. /// </summary>
  137. /// <returns>Abstract patch type</returns>
  138. public TessPatchType QueryTessPatchType()
  139. {
  140. return (TessPatchType)(_header.TessellationModePacked & 3);
  141. }
  142. /// <summary>
  143. /// Queries the tessellation evaluation shader spacing between tessellated vertices of the patch.
  144. /// </summary>
  145. /// <returns>Spacing between tessellated vertices of the patch</returns>
  146. public TessSpacing QueryTessSpacing()
  147. {
  148. return (TessSpacing)((_header.TessellationModePacked >> 2) & 3);
  149. }
  150. /// <summary>
  151. /// Gets the texture descriptor for a given texture on the pool.
  152. /// </summary>
  153. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  154. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  155. /// <returns>Texture descriptor</returns>
  156. public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  157. {
  158. if (!_textureDescriptors.TryGetValue(handle, out GuestTextureDescriptor textureDescriptor))
  159. {
  160. throw new ArgumentException();
  161. }
  162. return textureDescriptor;
  163. }
  164. /// <summary>
  165. /// Queries transform feedback enable state.
  166. /// </summary>
  167. /// <returns>True if the shader uses transform feedback, false otherwise</returns>
  168. public bool QueryTransformFeedbackEnabled()
  169. {
  170. return _tfd != null;
  171. }
  172. /// <summary>
  173. /// Queries the varying locations that should be written to the transform feedback buffer.
  174. /// </summary>
  175. /// <param name="bufferIndex">Index of the transform feedback buffer</param>
  176. /// <returns>Varying locations for the specified buffer</returns>
  177. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  178. {
  179. return _tfd[bufferIndex].VaryingLocations;
  180. }
  181. /// <summary>
  182. /// Queries the stride (in bytes) of the per vertex data written into the transform feedback buffer.
  183. /// </summary>
  184. /// <param name="bufferIndex">Index of the transform feedback buffer</param>
  185. /// <returns>Stride for the specified buffer</returns>
  186. public int QueryTransformFeedbackStride(int bufferIndex)
  187. {
  188. return _tfd[bufferIndex].Stride;
  189. }
  190. /// <summary>
  191. /// Queries if host state forces early depth testing.
  192. /// </summary>
  193. /// <returns>True if early depth testing is forced</returns>
  194. public bool QueryEarlyZForce()
  195. {
  196. return (_header.StateFlags & GuestGpuStateFlags.EarlyZForce) != 0;
  197. }
  198. }
  199. }