GpuAccessor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 int QueryBindingConstantBuffer(int index)
  63. {
  64. return _state.ResourceCounts.UniformBuffersCount++;
  65. }
  66. /// <inheritdoc/>
  67. public int QueryBindingStorageBuffer(int index)
  68. {
  69. return _state.ResourceCounts.StorageBuffersCount++;
  70. }
  71. /// <inheritdoc/>
  72. public int QueryBindingTexture(int index)
  73. {
  74. return _state.ResourceCounts.TexturesCount++;
  75. }
  76. /// <inheritdoc/>
  77. public int QueryBindingImage(int index)
  78. {
  79. return _state.ResourceCounts.ImagesCount++;
  80. }
  81. /// <inheritdoc/>
  82. public int QueryComputeLocalSizeX() => _state.ComputeState.LocalSizeX;
  83. /// <inheritdoc/>
  84. public int QueryComputeLocalSizeY() => _state.ComputeState.LocalSizeY;
  85. /// <inheritdoc/>
  86. public int QueryComputeLocalSizeZ() => _state.ComputeState.LocalSizeZ;
  87. /// <inheritdoc/>
  88. public int QueryComputeLocalMemorySize() => _state.ComputeState.LocalMemorySize;
  89. /// <inheritdoc/>
  90. public int QueryComputeSharedMemorySize() => _state.ComputeState.SharedMemorySize;
  91. /// <inheritdoc/>
  92. public uint QueryConstantBufferUse()
  93. {
  94. uint useMask = _compute
  95. ? _channel.BufferManager.GetComputeUniformBufferUseMask()
  96. : _channel.BufferManager.GetGraphicsUniformBufferUseMask(_stageIndex);
  97. _state.SpecializationState?.RecordConstantBufferUse(_stageIndex, useMask);
  98. return useMask;
  99. }
  100. /// <inheritdoc/>
  101. public InputTopology QueryPrimitiveTopology()
  102. {
  103. _state.SpecializationState?.RecordPrimitiveTopology();
  104. return ConvertToInputTopology(_state.GraphicsState.Topology, _state.GraphicsState.TessellationMode);
  105. }
  106. /// <inheritdoc/>
  107. public bool QueryTessCw()
  108. {
  109. return _state.GraphicsState.TessellationMode.UnpackCw();
  110. }
  111. /// <inheritdoc/>
  112. public TessPatchType QueryTessPatchType()
  113. {
  114. return _state.GraphicsState.TessellationMode.UnpackPatchType();
  115. }
  116. /// <inheritdoc/>
  117. public TessSpacing QueryTessSpacing()
  118. {
  119. return _state.GraphicsState.TessellationMode.UnpackSpacing();
  120. }
  121. //// <inheritdoc/>
  122. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  123. {
  124. _state.SpecializationState?.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  125. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  126. return ConvertToTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb());
  127. }
  128. /// <inheritdoc/>
  129. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  130. {
  131. _state.SpecializationState?.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  132. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
  133. }
  134. /// <inheritdoc/>
  135. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  136. {
  137. _state.SpecializationState?.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  138. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureCoordNormalized();
  139. }
  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. private Image.TextureDescriptor GetTextureDescriptor(int handle, int cbufSlot)
  147. {
  148. if (_compute)
  149. {
  150. return _channel.TextureManager.GetComputeTextureDescriptor(
  151. _state.PoolState.TexturePoolGpuVa,
  152. _state.PoolState.TextureBufferIndex,
  153. _state.PoolState.TexturePoolMaximumId,
  154. handle,
  155. cbufSlot);
  156. }
  157. else
  158. {
  159. return _channel.TextureManager.GetGraphicsTextureDescriptor(
  160. _state.PoolState.TexturePoolGpuVa,
  161. _state.PoolState.TextureBufferIndex,
  162. _state.PoolState.TexturePoolMaximumId,
  163. _stageIndex,
  164. handle,
  165. cbufSlot);
  166. }
  167. }
  168. /// <inheritdoc/>
  169. public bool QueryTransformFeedbackEnabled()
  170. {
  171. return _state.TransformFeedbackDescriptors != null;
  172. }
  173. /// <inheritdoc/>
  174. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  175. {
  176. return _state.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  177. }
  178. /// <inheritdoc/>
  179. public int QueryTransformFeedbackStride(int bufferIndex)
  180. {
  181. return _state.TransformFeedbackDescriptors[bufferIndex].Stride;
  182. }
  183. /// <inheritdoc/>
  184. public bool QueryEarlyZForce()
  185. {
  186. _state.SpecializationState?.RecordEarlyZForce();
  187. return _state.GraphicsState.EarlyZForce;
  188. }
  189. /// <inheritdoc/>
  190. public bool QueryViewportTransformDisable()
  191. {
  192. return _state.GraphicsState.ViewportTransformDisable;
  193. }
  194. /// <inheritdoc/>
  195. public void RegisterTexture(int handle, int cbufSlot)
  196. {
  197. _state.SpecializationState?.RegisterTexture(_stageIndex, handle, cbufSlot, GetTextureDescriptor(handle, cbufSlot));
  198. }
  199. }
  200. }