GpuAccessor.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Shader;
  5. using Ryujinx.Graphics.Shader.Translation;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace Ryujinx.Graphics.Gpu.Shader
  9. {
  10. /// <summary>
  11. /// Represents a GPU state and memory accessor.
  12. /// </summary>
  13. class GpuAccessor : GpuAccessorBase, IGpuAccessor
  14. {
  15. private readonly GpuChannel _channel;
  16. private readonly GpuAccessorState _state;
  17. private readonly int _stageIndex;
  18. private readonly bool _compute;
  19. private readonly bool _isVulkan;
  20. /// <summary>
  21. /// Creates a new instance of the GPU state accessor for graphics shader translation.
  22. /// </summary>
  23. /// <param name="context">GPU context</param>
  24. /// <param name="channel">GPU channel</param>
  25. /// <param name="state">Current GPU state</param>
  26. /// <param name="stageIndex">Graphics shader stage index (0 = Vertex, 4 = Fragment)</param>
  27. public GpuAccessor(
  28. GpuContext context,
  29. GpuChannel channel,
  30. GpuAccessorState state,
  31. int stageIndex) : base(context, state.ResourceCounts, stageIndex)
  32. {
  33. _isVulkan = context.Capabilities.Api == TargetApi.Vulkan;
  34. _channel = channel;
  35. _state = state;
  36. _stageIndex = stageIndex;
  37. }
  38. /// <summary>
  39. /// Creates a new instance of the GPU state accessor for compute shader translation.
  40. /// </summary>
  41. /// <param name="context">GPU context</param>
  42. /// <param name="channel">GPU channel</param>
  43. /// <param name="state">Current GPU state</param>
  44. public GpuAccessor(GpuContext context, GpuChannel channel, GpuAccessorState state) : base(context, state.ResourceCounts, 0)
  45. {
  46. _channel = channel;
  47. _state = state;
  48. _compute = true;
  49. }
  50. /// <inheritdoc/>
  51. public uint ConstantBuffer1Read(int offset)
  52. {
  53. ulong baseAddress = _compute
  54. ? _channel.BufferManager.GetComputeUniformBufferAddress(1)
  55. : _channel.BufferManager.GetGraphicsUniformBufferAddress(_stageIndex, 1);
  56. return _channel.MemoryManager.Physical.Read<uint>(baseAddress + (ulong)offset);
  57. }
  58. /// <inheritdoc/>
  59. public void Log(string message)
  60. {
  61. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  62. }
  63. /// <inheritdoc/>
  64. public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  65. {
  66. int size = Math.Max(minimumSize, 0x1000 - (int)(address & 0xfff));
  67. return MemoryMarshal.Cast<byte, ulong>(_channel.MemoryManager.GetSpan(address, size));
  68. }
  69. /// <inheritdoc/>
  70. public bool QueryAlphaToCoverageDitherEnable()
  71. {
  72. return _state.GraphicsState.AlphaToCoverageEnable && _state.GraphicsState.AlphaToCoverageDitherEnable;
  73. }
  74. /// <inheritdoc/>
  75. public AlphaTestOp QueryAlphaTestCompare()
  76. {
  77. if (!_isVulkan || !_state.GraphicsState.AlphaTestEnable)
  78. {
  79. return AlphaTestOp.Always;
  80. }
  81. return _state.GraphicsState.AlphaTestCompare switch
  82. {
  83. CompareOp.Never or CompareOp.NeverGl => AlphaTestOp.Never,
  84. CompareOp.Less or CompareOp.LessGl => AlphaTestOp.Less,
  85. CompareOp.Equal or CompareOp.EqualGl => AlphaTestOp.Equal,
  86. CompareOp.LessOrEqual or CompareOp.LessOrEqualGl => AlphaTestOp.LessOrEqual,
  87. CompareOp.Greater or CompareOp.GreaterGl => AlphaTestOp.Greater,
  88. CompareOp.NotEqual or CompareOp.NotEqualGl => AlphaTestOp.NotEqual,
  89. CompareOp.GreaterOrEqual or CompareOp.GreaterOrEqualGl => AlphaTestOp.GreaterOrEqual,
  90. _ => AlphaTestOp.Always
  91. };
  92. }
  93. /// <inheritdoc/>
  94. public float QueryAlphaTestReference()
  95. {
  96. return _state.GraphicsState.AlphaTestReference;
  97. }
  98. /// <inheritdoc/>
  99. public AttributeType QueryAttributeType(int location)
  100. {
  101. return _state.GraphicsState.AttributeTypes[location];
  102. }
  103. /// <inheritdoc/>
  104. public AttributeType QueryFragmentOutputType(int location)
  105. {
  106. return _state.GraphicsState.FragmentOutputTypes[location];
  107. }
  108. /// <inheritdoc/>
  109. public int QueryComputeLocalSizeX() => _state.ComputeState.LocalSizeX;
  110. /// <inheritdoc/>
  111. public int QueryComputeLocalSizeY() => _state.ComputeState.LocalSizeY;
  112. /// <inheritdoc/>
  113. public int QueryComputeLocalSizeZ() => _state.ComputeState.LocalSizeZ;
  114. /// <inheritdoc/>
  115. public int QueryComputeLocalMemorySize() => _state.ComputeState.LocalMemorySize;
  116. /// <inheritdoc/>
  117. public int QueryComputeSharedMemorySize() => _state.ComputeState.SharedMemorySize;
  118. /// <inheritdoc/>
  119. public uint QueryConstantBufferUse()
  120. {
  121. uint useMask = _compute
  122. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  123. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  124. _state.SpecializationState?.RecordConstantBufferUse(_stageIndex, useMask);
  125. return useMask;
  126. }
  127. /// <inheritdoc/>
  128. public bool QueryHasConstantBufferDrawParameters()
  129. {
  130. return _state.GraphicsState.HasConstantBufferDrawParameters;
  131. }
  132. /// <inheritdoc/>
  133. public bool QueryHasUnalignedStorageBuffer()
  134. {
  135. return _state.GraphicsState.HasUnalignedStorageBuffer || _state.ComputeState.HasUnalignedStorageBuffer;
  136. }
  137. /// <inheritdoc/>
  138. public bool QueryDualSourceBlendEnable()
  139. {
  140. return _state.GraphicsState.DualSourceBlendEnable;
  141. }
  142. /// <inheritdoc/>
  143. public InputTopology QueryPrimitiveTopology()
  144. {
  145. _state.SpecializationState?.RecordPrimitiveTopology();
  146. return ConvertToInputTopology(_state.GraphicsState.Topology, _state.GraphicsState.TessellationMode);
  147. }
  148. /// <inheritdoc/>
  149. public bool QueryProgramPointSize()
  150. {
  151. return _state.GraphicsState.ProgramPointSizeEnable;
  152. }
  153. /// <inheritdoc/>
  154. public float QueryPointSize()
  155. {
  156. return _state.GraphicsState.PointSize;
  157. }
  158. /// <inheritdoc/>
  159. public bool QueryTessCw()
  160. {
  161. return _state.GraphicsState.TessellationMode.UnpackCw();
  162. }
  163. /// <inheritdoc/>
  164. public TessPatchType QueryTessPatchType()
  165. {
  166. return _state.GraphicsState.TessellationMode.UnpackPatchType();
  167. }
  168. /// <inheritdoc/>
  169. public TessSpacing QueryTessSpacing()
  170. {
  171. return _state.GraphicsState.TessellationMode.UnpackSpacing();
  172. }
  173. //// <inheritdoc/>
  174. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  175. {
  176. _state.SpecializationState?.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  177. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  178. return ConvertToTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb());
  179. }
  180. /// <inheritdoc/>
  181. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  182. {
  183. _state.SpecializationState?.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  184. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
  185. }
  186. /// <inheritdoc/>
  187. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  188. {
  189. _state.SpecializationState?.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  190. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureCoordNormalized();
  191. }
  192. /// <summary>
  193. /// Gets the texture descriptor for a given texture on the pool.
  194. /// </summary>
  195. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  196. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  197. /// <returns>Texture descriptor</returns>
  198. private Image.TextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  199. {
  200. if (_compute)
  201. {
  202. return _channel.TextureManager.GetComputeTextureDescriptor(
  203. _state.PoolState.TexturePoolGpuVa,
  204. _state.PoolState.TextureBufferIndex,
  205. _state.PoolState.TexturePoolMaximumId,
  206. handle,
  207. cbufSlot);
  208. }
  209. else
  210. {
  211. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  212. _state.PoolState.TexturePoolGpuVa,
  213. _state.PoolState.TextureBufferIndex,
  214. _state.PoolState.TexturePoolMaximumId,
  215. _stageIndex,
  216. handle,
  217. cbufSlot);
  218. }
  219. }
  220. /// <inheritdoc/>
  221. public bool QueryTransformDepthMinusOneToOne()
  222. {
  223. return _state.GraphicsState.DepthMode;
  224. }
  225. /// <inheritdoc/>
  226. public bool QueryTransformFeedbackEnabled()
  227. {
  228. return _state.TransformFeedbackDescriptors != null;
  229. }
  230. /// <inheritdoc/>
  231. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  232. {
  233. return _state.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  234. }
  235. /// <inheritdoc/>
  236. public int QueryTransformFeedbackStride(int bufferIndex)
  237. {
  238. return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
  239. }
  240. /// <inheritdoc/>
  241. public bool QueryEarlyZForce()
  242. {
  243. _state.SpecializationState?.RecordEarlyZForce();
  244. return _state.GraphicsState.EarlyZForce;
  245. }
  246. /// <inheritdoc/>
  247. public bool QueryViewportTransformDisable()
  248. {
  249. return _state.GraphicsState.ViewportTransformDisable;
  250. }
  251. /// <inheritdoc/>
  252. public void RegisterTexture(int handle, int cbufSlot)
  253. {
  254. _state.SpecializationState?.RegisterTexture(_stageIndex, handle, cbufSlot, GetTextureDescriptor(handle, cbufSlot));
  255. }
  256. }
  257. }