GpuAccessor.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Shader;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Shader
  7. {
  8. /// <summary>
  9. /// Represents a GPU state and memory accessor.
  10. /// </summary>
  11. class GpuAccessor : GpuAccessorBase, IGpuAccessor
  12. {
  13. private readonly GpuChannel _channel;
  14. private readonly GpuAccessorState _state;
  15. private readonly int _stageIndex;
  16. private readonly bool _compute;
  17. /// <summary>
  18. /// Creates a new instance of the GPU state accessor for graphics shader translation.
  19. /// </summary>
  20. /// <param name="context">GPU context</param>
  21. /// <param name="channel">GPU channel</param>
  22. /// <param name="state">Current GPU state</param>
  23. /// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
  24. public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state, int stageIndex) : base(context)
  25. {
  26. _channel = channel;
  27. _state = state;
  28. _stageIndex = stageIndex;
  29. }
  30. /// <summary>
  31. /// Creates a new instance of the GPU state accessor for compute shader translation.
  32. /// </summary>
  33. /// <param name="context">GPU context</param>
  34. /// <param name="channel">GPU channel</param>
  35. /// <param name="state">Current GPU state</param>
  36. public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state) : base(context)
  37. {
  38. _channel = channel;
  39. _state = state;
  40. _compute = true;
  41. }
  42. /// <inheritdoc/>
  43. public uint ConstantBuffer1Read(int offset)
  44. {
  45. ulong baseAddress = _compute
  46. ? _channel.BufferManager.GetComputeUniformBufferAddress(1)
  47. : _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1);
  48. return _channel.MemoryManager.Physical.Read<uint>(baseAddress + (ulong)offset);
  49. }
  50. /// <inheritdoc/>
  51. public void Log(string message)
  52. {
  53. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  54. }
  55. /// <inheritdoc/>
  56. public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  57. {
  58. int size = Math.Max(minimumSize, 0x1000 - (int)(address & 0xfff));
  59. return MemoryMarshal.Cast<byte, ulong>(_channel.MemoryManager.GetSpan(address, size));
  60. }
  61. /// <inheritdoc/>
  62. public bool QueryAlphaToCoverageDitherEnable()
  63. {
  64. return _state.GraphicsState.AlphaToCoverageEnable && _state.GraphicsState.AlphaToCoverageDitherEnable;
  65. }
  66. /// <inheritdoc/>
  67. public int QueryBindingConstantBuffer(int index)
  68. {
  69. return _state.ResourceCounts.UniformBuffersCount++;
  70. }
  71. /// <inheritdoc/>
  72. public int QueryBindingStorageBuffer(int index)
  73. {
  74. return _state.ResourceCounts.StorageBuffersCount++;
  75. }
  76. /// <inheritdoc/>
  77. public int QueryBindingTexture(int index)
  78. {
  79. return _state.ResourceCounts.TexturesCount++;
  80. }
  81. /// <inheritdoc/>
  82. public int QueryBindingImage(int index)
  83. {
  84. return _state.ResourceCounts.ImagesCount++;
  85. }
  86. /// <inheritdoc/>
  87. public int QueryComputeLocalSizeX() => _state.ComputeState.LocalSizeX;
  88. /// <inheritdoc/>
  89. public int QueryComputeLocalSizeY() => _state.ComputeState.LocalSizeY;
  90. /// <inheritdoc/>
  91. public int QueryComputeLocalSizeZ() => _state.ComputeState.LocalSizeZ;
  92. /// <inheritdoc/>
  93. public int QueryComputeLocalMemorySize() => _state.ComputeState.LocalMemorySize;
  94. /// <inheritdoc/>
  95. public int QueryComputeSharedMemorySize() => _state.ComputeState.SharedMemorySize;
  96. /// <inheritdoc/>
  97. public uint QueryConstantBufferUse()
  98. {
  99. uint useMask = _compute
  100. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  101. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  102. _state.SpecializationState?.RecordConstantBufferUse(_stageIndex, useMask);
  103. return useMask;
  104. }
  105. /// <inheritdoc/>
  106. public InputTopology QueryPrimitiveTopology()
  107. {
  108. _state.SpecializationState?.RecordPrimitiveTopology();
  109. return ConvertToInputTopology(_state.GraphicsState.Topology, _state.GraphicsState.TessellationMode);
  110. }
  111. /// <inheritdoc/>
  112. public bool QueryTessCw()
  113. {
  114. return _state.GraphicsState.TessellationMode.UnpackCw();
  115. }
  116. /// <inheritdoc/>
  117. public TessPatchType QueryTessPatchType()
  118. {
  119. return _state.GraphicsState.TessellationMode.UnpackPatchType();
  120. }
  121. /// <inheritdoc/>
  122. public TessSpacing QueryTessSpacing()
  123. {
  124. return _state.GraphicsState.TessellationMode.UnpackSpacing();
  125. }
  126. //// <inheritdoc/>
  127. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  128. {
  129. _state.SpecializationState?.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  130. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  131. return ConvertToTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb());
  132. }
  133. /// <inheritdoc/>
  134. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  135. {
  136. _state.SpecializationState?.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  137. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
  138. }
  139. /// <inheritdoc/>
  140. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  141. {
  142. _state.SpecializationState?.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  143. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureCoordNormalized();
  144. }
  145. /// <summary>
  146. /// Gets the texture descriptor for a given texture on the pool.
  147. /// </summary>
  148. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  149. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  150. /// <returns>Texture descriptor</returns>
  151. private Image.TextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  152. {
  153. if (_compute)
  154. {
  155. return _channel.TextureManager.GetComputeTextureDescriptor(
  156. _state.PoolState.TexturePoolGpuVa,
  157. _state.PoolState.TextureBufferIndex,
  158. _state.PoolState.TexturePoolMaximumId,
  159. handle,
  160. cbufSlot);
  161. }
  162. else
  163. {
  164. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  165. _state.PoolState.TexturePoolGpuVa,
  166. _state.PoolState.TextureBufferIndex,
  167. _state.PoolState.TexturePoolMaximumId,
  168. _stageIndex,
  169. handle,
  170. cbufSlot);
  171. }
  172. }
  173. /// <inheritdoc/>
  174. public bool QueryTransformFeedbackEnabled()
  175. {
  176. return _state.TransformFeedbackDescriptors != null;
  177. }
  178. /// <inheritdoc/>
  179. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  180. {
  181. return _state.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  182. }
  183. /// <inheritdoc/>
  184. public int QueryTransformFeedbackStride(int bufferIndex)
  185. {
  186. return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
  187. }
  188. /// <inheritdoc/>
  189. public bool QueryEarlyZForce()
  190. {
  191. _state.SpecializationState?.RecordEarlyZForce();
  192. return _state.GraphicsState.EarlyZForce;
  193. }
  194. /// <inheritdoc/>
  195. public bool QueryViewportTransformDisable()
  196. {
  197. return _state.GraphicsState.ViewportTransformDisable;
  198. }
  199. /// <inheritdoc/>
  200. public void RegisterTexture(int handle, int cbufSlot)
  201. {
  202. _state.SpecializationState?.RegisterTexture(_stageIndex, handle, cbufSlot, GetTextureDescriptor(handle, cbufSlot));
  203. }
  204. }
  205. }