CachedGpuAccessor.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 GuestGpuAccessorHeader _header;
  14. private readonly Dictionary<int, GuestTextureDescriptor> _textureDescriptors;
  15. /// <summary>
  16. /// Creates a new instance of the cached GPU state accessor for shader translation.
  17. /// </summary>
  18. /// <param name="context">GPU context</param>
  19. /// <param name="data">The data of the shader</param>
  20. /// <param name="header">The cache of the GPU accessor</param>
  21. /// <param name="guestTextureDescriptors">The cache of the texture descriptors</param>
  22. public CachedGpuAccessor(GpuContext context, ReadOnlyMemory<byte> data, GuestGpuAccessorHeader header, Dictionary<int, GuestTextureDescriptor> guestTextureDescriptors)
  23. {
  24. _context = context;
  25. _data = data;
  26. _header = header;
  27. _textureDescriptors = new Dictionary<int, GuestTextureDescriptor>();
  28. foreach (KeyValuePair<int, GuestTextureDescriptor> guestTextureDescriptor in guestTextureDescriptors)
  29. {
  30. _textureDescriptors.Add(guestTextureDescriptor.Key, guestTextureDescriptor.Value);
  31. }
  32. }
  33. /// <summary>
  34. /// Prints a log message.
  35. /// </summary>
  36. /// <param name="message">Message to print</param>
  37. public void Log(string message)
  38. {
  39. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  40. }
  41. /// <summary>
  42. /// Reads data from GPU memory.
  43. /// </summary>
  44. /// <typeparam name="T">Type of the data to be read</typeparam>
  45. /// <param name="address">GPU virtual address of the data</param>
  46. /// <returns>Data at the memory location</returns>
  47. public override T MemoryRead<T>(ulong address)
  48. {
  49. return MemoryMarshal.Cast<byte, T>(_data.Span.Slice((int)address))[0];
  50. }
  51. /// <summary>
  52. /// Checks if a given memory address is mapped.
  53. /// </summary>
  54. /// <param name="address">GPU virtual address to be checked</param>
  55. /// <returns>True if the address is mapped, false otherwise</returns>
  56. public bool MemoryMapped(ulong address)
  57. {
  58. return address < (ulong)_data.Length;
  59. }
  60. /// <summary>
  61. /// Queries Local Size X for compute shaders.
  62. /// </summary>
  63. /// <returns>Local Size X</returns>
  64. public int QueryComputeLocalSizeX()
  65. {
  66. return _header.ComputeLocalSizeX;
  67. }
  68. /// <summary>
  69. /// Queries Local Size Y for compute shaders.
  70. /// </summary>
  71. /// <returns>Local Size Y</returns>
  72. public int QueryComputeLocalSizeY()
  73. {
  74. return _header.ComputeLocalSizeY;
  75. }
  76. /// <summary>
  77. /// Queries Local Size Z for compute shaders.
  78. /// </summary>
  79. /// <returns>Local Size Z</returns>
  80. public int QueryComputeLocalSizeZ()
  81. {
  82. return _header.ComputeLocalSizeZ;
  83. }
  84. /// <summary>
  85. /// Queries Local Memory size in bytes for compute shaders.
  86. /// </summary>
  87. /// <returns>Local Memory size in bytes</returns>
  88. public int QueryComputeLocalMemorySize()
  89. {
  90. return _header.ComputeLocalMemorySize;
  91. }
  92. /// <summary>
  93. /// Queries Shared Memory size in bytes for compute shaders.
  94. /// </summary>
  95. /// <returns>Shared Memory size in bytes</returns>
  96. public int QueryComputeSharedMemorySize()
  97. {
  98. return _header.ComputeSharedMemorySize;
  99. }
  100. /// <summary>
  101. /// Queries current primitive topology for geometry shaders.
  102. /// </summary>
  103. /// <returns>Current primitive topology</returns>
  104. public InputTopology QueryPrimitiveTopology()
  105. {
  106. return _header.PrimitiveTopology;
  107. }
  108. /// <summary>
  109. /// Queries host storage buffer alignment required.
  110. /// </summary>
  111. /// <returns>Host storage buffer alignment in bytes</returns>
  112. public int QueryStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  113. /// <summary>
  114. /// Queries host support for readable images without a explicit format declaration on the shader.
  115. /// </summary>
  116. /// <returns>True if formatted image load is supported, false otherwise</returns>
  117. public bool QuerySupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  118. /// <summary>
  119. /// Queries host GPU non-constant texture offset support.
  120. /// </summary>
  121. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  122. public bool QuerySupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  123. /// <summary>
  124. /// Gets the texture descriptor for a given texture on the pool.
  125. /// </summary>
  126. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  127. /// <returns>Texture descriptor</returns>
  128. public override Image.ITextureDescriptor GetTextureDescriptor(int handle)
  129. {
  130. if (!_textureDescriptors.TryGetValue(handle, out GuestTextureDescriptor textureDescriptor))
  131. {
  132. throw new ArgumentException();
  133. }
  134. return textureDescriptor;
  135. }
  136. /// <summary>
  137. /// Queries if host state forces early depth testing.
  138. /// </summary>
  139. /// <returns>True if early depth testing is forced</returns>
  140. public bool QueryEarlyZForce()
  141. {
  142. return (_header.StateFlags & GuestGpuStateFlags.EarlyZForce) != 0;
  143. }
  144. }
  145. }