GpuAccessor.cs 9.0 KB

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