CachedGpuAccessor.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 GpuContext _context;
  12. private readonly ReadOnlyMemory<byte> _data;
  13. private readonly ReadOnlyMemory<byte> _cb1Data;
  14. private readonly GuestGpuAccessorHeader _header;
  15. private readonly Dictionary<int, GuestTextureDescriptor> _textureDescriptors;
  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. Dictionary<int, GuestTextureDescriptor> guestTextureDescriptors)
  30. {
  31. _context = context;
  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. }
  41. /// <summary>
  42. /// Reads data from the constant buffer 1.
  43. /// </summary>
  44. /// <param name="offset">Offset in bytes to read from</param>
  45. /// <returns>Value at the given offset</returns>
  46. public uint ConstantBuffer1Read(int offset)
  47. {
  48. return MemoryMarshal.Cast<byte, uint>(_cb1Data.Span.Slice(offset))[0];
  49. }
  50. /// <summary>
  51. /// Prints a log message.
  52. /// </summary>
  53. /// <param name="message">Message to print</param>
  54. public void Log(string message)
  55. {
  56. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  57. }
  58. /// <summary>
  59. /// Reads data from GPU memory.
  60. /// </summary>
  61. /// <typeparam name="T">Type of the data to be read</typeparam>
  62. /// <param name="address">GPU virtual address of the data</param>
  63. /// <returns>Data at the memory location</returns>
  64. public override T MemoryRead<T>(ulong address)
  65. {
  66. return MemoryMarshal.Cast<byte, T>(_data.Span.Slice((int)address))[0];
  67. }
  68. /// <summary>
  69. /// Checks if a given memory address is mapped.
  70. /// </summary>
  71. /// <param name="address">GPU virtual address to be checked</param>
  72. /// <returns>True if the address is mapped, false otherwise</returns>
  73. public bool MemoryMapped(ulong address)
  74. {
  75. return address < (ulong)_data.Length;
  76. }
  77. /// <summary>
  78. /// Queries Local Size X for compute shaders.
  79. /// </summary>
  80. /// <returns>Local Size X</returns>
  81. public int QueryComputeLocalSizeX()
  82. {
  83. return _header.ComputeLocalSizeX;
  84. }
  85. /// <summary>
  86. /// Queries Local Size Y for compute shaders.
  87. /// </summary>
  88. /// <returns>Local Size Y</returns>
  89. public int QueryComputeLocalSizeY()
  90. {
  91. return _header.ComputeLocalSizeY;
  92. }
  93. /// <summary>
  94. /// Queries Local Size Z for compute shaders.
  95. /// </summary>
  96. /// <returns>Local Size Z</returns>
  97. public int QueryComputeLocalSizeZ()
  98. {
  99. return _header.ComputeLocalSizeZ;
  100. }
  101. /// <summary>
  102. /// Queries Local Memory size in bytes for compute shaders.
  103. /// </summary>
  104. /// <returns>Local Memory size in bytes</returns>
  105. public int QueryComputeLocalMemorySize()
  106. {
  107. return _header.ComputeLocalMemorySize;
  108. }
  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()
  114. {
  115. return _header.ComputeSharedMemorySize;
  116. }
  117. /// <summary>
  118. /// Queries current primitive topology for geometry shaders.
  119. /// </summary>
  120. /// <returns>Current primitive topology</returns>
  121. public InputTopology QueryPrimitiveTopology()
  122. {
  123. return _header.PrimitiveTopology;
  124. }
  125. /// <summary>
  126. /// Queries host storage buffer alignment required.
  127. /// </summary>
  128. /// <returns>Host storage buffer alignment in bytes</returns>
  129. public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  130. /// <summary>
  131. /// Queries host support for readable images without a explicit format declaration on the shader.
  132. /// </summary>
  133. /// <returns>True if formatted image load is supported, false otherwise</returns>
  134. public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  135. /// <summary>
  136. /// Queries host GPU non-constant texture offset support.
  137. /// </summary>
  138. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  139. public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  140. /// <summary>
  141. /// Gets the texture descriptor for a given texture on the pool.
  142. /// </summary>
  143. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  144. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  145. /// <returns>Texture descriptor</returns>
  146. public override Image.ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  147. {
  148. if (!_textureDescriptors.TryGetValue(handle, out GuestTextureDescriptor textureDescriptor))
  149. {
  150. throw new ArgumentException();
  151. }
  152. return textureDescriptor;
  153. }
  154. /// <summary>
  155. /// Queries if host state forces early depth testing.
  156. /// </summary>
  157. /// <returns>True if early depth testing is forced</returns>
  158. public bool QueryEarlyZForce()
  159. {
  160. return (_header.StateFlags & GuestGpuStateFlags.EarlyZForce) != 0;
  161. }
  162. }
  163. }