GpuAccessor.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 int QueryComputeLocalSizeX() => _state.ComputeState.LocalSizeX;
  105. /// <inheritdoc/>
  106. public int QueryComputeLocalSizeY() => _state.ComputeState.LocalSizeY;
  107. /// <inheritdoc/>
  108. public int QueryComputeLocalSizeZ() => _state.ComputeState.LocalSizeZ;
  109. /// <inheritdoc/>
  110. public int QueryComputeLocalMemorySize() => _state.ComputeState.LocalMemorySize;
  111. /// <inheritdoc/>
  112. public int QueryComputeSharedMemorySize() => _state.ComputeState.SharedMemorySize;
  113. /// <inheritdoc/>
  114. public uint QueryConstantBufferUse()
  115. {
  116. uint useMask = _compute
  117. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  118. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  119. _state.SpecializationState?.RecordConstantBufferUse(_stageIndex, useMask);
  120. return useMask;
  121. }
  122. /// <inheritdoc/>
  123. public InputTopology QueryPrimitiveTopology()
  124. {
  125. _state.SpecializationState?.RecordPrimitiveTopology();
  126. return ConvertToInputTopology(_state.GraphicsState.Topology, _state.GraphicsState.TessellationMode);
  127. }
  128. /// <inheritdoc/>
  129. public bool QueryProgramPointSize()
  130. {
  131. return _state.GraphicsState.ProgramPointSizeEnable;
  132. }
  133. /// <inheritdoc/>
  134. public float QueryPointSize()
  135. {
  136. return _state.GraphicsState.PointSize;
  137. }
  138. /// <inheritdoc/>
  139. public bool QueryTessCw()
  140. {
  141. return _state.GraphicsState.TessellationMode.UnpackCw();
  142. }
  143. /// <inheritdoc/>
  144. public TessPatchType QueryTessPatchType()
  145. {
  146. return _state.GraphicsState.TessellationMode.UnpackPatchType();
  147. }
  148. /// <inheritdoc/>
  149. public TessSpacing QueryTessSpacing()
  150. {
  151. return _state.GraphicsState.TessellationMode.UnpackSpacing();
  152. }
  153. //// <inheritdoc/>
  154. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  155. {
  156. _state.SpecializationState?.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  157. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  158. return ConvertToTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb());
  159. }
  160. /// <inheritdoc/>
  161. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  162. {
  163. _state.SpecializationState?.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  164. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
  165. }
  166. /// <inheritdoc/>
  167. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  168. {
  169. _state.SpecializationState?.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  170. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureCoordNormalized();
  171. }
  172. /// <summary>
  173. /// Gets the texture descriptor for a given texture on the pool.
  174. /// </summary>
  175. /// <param name="handle">Index of the texture (this is the word offset of the handle in the constant buffer)</param>
  176. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  177. /// <returns>Texture descriptor</returns>
  178. private Image.TextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  179. {
  180. if (_compute)
  181. {
  182. return _channel.TextureManager.GetComputeTextureDescriptor(
  183. _state.PoolState.TexturePoolGpuVa,
  184. _state.PoolState.TextureBufferIndex,
  185. _state.PoolState.TexturePoolMaximumId,
  186. handle,
  187. cbufSlot);
  188. }
  189. else
  190. {
  191. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  192. _state.PoolState.TexturePoolGpuVa,
  193. _state.PoolState.TextureBufferIndex,
  194. _state.PoolState.TexturePoolMaximumId,
  195. _stageIndex,
  196. handle,
  197. cbufSlot);
  198. }
  199. }
  200. /// <inheritdoc/>
  201. public bool QueryTransformDepthMinusOneToOne()
  202. {
  203. return _state.GraphicsState.DepthMode;
  204. }
  205. /// <inheritdoc/>
  206. public bool QueryTransformFeedbackEnabled()
  207. {
  208. return _state.TransformFeedbackDescriptors != null;
  209. }
  210. /// <inheritdoc/>
  211. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  212. {
  213. return _state.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  214. }
  215. /// <inheritdoc/>
  216. public int QueryTransformFeedbackStride(int bufferIndex)
  217. {
  218. return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
  219. }
  220. /// <inheritdoc/>
  221. public bool QueryEarlyZForce()
  222. {
  223. _state.SpecializationState?.RecordEarlyZForce();
  224. return _state.GraphicsState.EarlyZForce;
  225. }
  226. /// <inheritdoc/>
  227. public bool QueryViewportTransformDisable()
  228. {
  229. return _state.GraphicsState.ViewportTransformDisable;
  230. }
  231. /// <inheritdoc/>
  232. public void RegisterTexture(int handle, int cbufSlot)
  233. {
  234. _state.SpecializationState?.RegisterTexture(_stageIndex, handle, cbufSlot, GetTextureDescriptor(handle, cbufSlot));
  235. }
  236. }
  237. }