DiskCacheGpuAccessor.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.DiskCache
  7. {
  8. /// <summary>
  9. /// Represents a GPU state and memory accessor.
  10. /// </summary>
  11. class DiskCacheGpuAccessor : GpuAccessorBase, IGpuAccessor
  12. {
  13. private readonly ReadOnlyMemory<byte> _data;
  14. private readonly ReadOnlyMemory<byte> _cb1Data;
  15. private readonly ShaderSpecializationState _oldSpecState;
  16. private readonly ShaderSpecializationState _newSpecState;
  17. private readonly int _stageIndex;
  18. private ResourceCounts _resourceCounts;
  19. /// <summary>
  20. /// Creates a new instance of the cached GPU state accessor for shader translation.
  21. /// </summary>
  22. /// <param name="context">GPU context</param>
  23. /// <param name="data">The data of the shader</param>
  24. /// <param name="cb1Data">The constant buffer 1 data of the shader</param>
  25. /// <param name="oldSpecState">Shader specialization state of the cached shader</param>
  26. /// <param name="newSpecState">Shader specialization state of the recompiled shader</param>
  27. /// <param name="stageIndex">Shader stage index</param>
  28. public DiskCacheGpuAccessor(
  29. GpuContext context,
  30. ReadOnlyMemory<byte> data,
  31. ReadOnlyMemory<byte> cb1Data,
  32. ShaderSpecializationState oldSpecState,
  33. ShaderSpecializationState newSpecState,
  34. ResourceCounts counts,
  35. int stageIndex) : base(context)
  36. {
  37. _data = data;
  38. _cb1Data = cb1Data;
  39. _oldSpecState = oldSpecState;
  40. _newSpecState = newSpecState;
  41. _stageIndex = stageIndex;
  42. _resourceCounts = counts;
  43. }
  44. /// <inheritdoc/>
  45. public uint ConstantBuffer1Read(int offset)
  46. {
  47. if (offset + sizeof(uint) > _cb1Data.Length)
  48. {
  49. throw new DiskCacheLoadException(DiskCacheLoadResult.InvalidCb1DataLength);
  50. }
  51. return MemoryMarshal.Cast<byte, uint>(_cb1Data.Span.Slice(offset))[0];
  52. }
  53. /// <inheritdoc/>
  54. public void Log(string message)
  55. {
  56. Logger.Warning?.Print(LogClass.Gpu, $"Shader translator: {message}");
  57. }
  58. /// <inheritdoc/>
  59. public ReadOnlySpan<ulong> GetCode(ulong address, int minimumSize)
  60. {
  61. return MemoryMarshal.Cast<byte, ulong>(_data.Span.Slice((int)address));
  62. }
  63. /// <inheritdoc/>
  64. public bool QueryAlphaToCoverageDitherEnable()
  65. {
  66. return _oldSpecState.GraphicsState.AlphaToCoverageEnable && _oldSpecState.GraphicsState.AlphaToCoverageDitherEnable;
  67. }
  68. /// <inheritdoc/>
  69. public int QueryBindingConstantBuffer(int index)
  70. {
  71. return _resourceCounts.UniformBuffersCount++;
  72. }
  73. /// <inheritdoc/>
  74. public int QueryBindingStorageBuffer(int index)
  75. {
  76. return _resourceCounts.StorageBuffersCount++;
  77. }
  78. /// <inheritdoc/>
  79. public int QueryBindingTexture(int index)
  80. {
  81. return _resourceCounts.TexturesCount++;
  82. }
  83. /// <inheritdoc/>
  84. public int QueryBindingImage(int index)
  85. {
  86. return _resourceCounts.ImagesCount++;
  87. }
  88. /// <inheritdoc/>
  89. public int QueryComputeLocalSizeX() => _oldSpecState.ComputeState.LocalSizeX;
  90. /// <inheritdoc/>
  91. public int QueryComputeLocalSizeY() => _oldSpecState.ComputeState.LocalSizeY;
  92. /// <inheritdoc/>
  93. public int QueryComputeLocalSizeZ() => _oldSpecState.ComputeState.LocalSizeZ;
  94. /// <inheritdoc/>
  95. public int QueryComputeLocalMemorySize() => _oldSpecState.ComputeState.LocalMemorySize;
  96. /// <inheritdoc/>
  97. public int QueryComputeSharedMemorySize() => _oldSpecState.ComputeState.SharedMemorySize;
  98. /// <inheritdoc/>
  99. public uint QueryConstantBufferUse()
  100. {
  101. _newSpecState.RecordConstantBufferUse(_stageIndex, _oldSpecState.ConstantBufferUse[_stageIndex]);
  102. return _oldSpecState.ConstantBufferUse[_stageIndex];
  103. }
  104. /// <inheritdoc/>
  105. public InputTopology QueryPrimitiveTopology()
  106. {
  107. _newSpecState.RecordPrimitiveTopology();
  108. return ConvertToInputTopology(_oldSpecState.GraphicsState.Topology, _oldSpecState.GraphicsState.TessellationMode);
  109. }
  110. /// <inheritdoc/>
  111. public bool QueryTessCw()
  112. {
  113. return _oldSpecState.GraphicsState.TessellationMode.UnpackCw();
  114. }
  115. /// <inheritdoc/>
  116. public TessPatchType QueryTessPatchType()
  117. {
  118. return _oldSpecState.GraphicsState.TessellationMode.UnpackPatchType();
  119. }
  120. /// <inheritdoc/>
  121. public TessSpacing QueryTessSpacing()
  122. {
  123. return _oldSpecState.GraphicsState.TessellationMode.UnpackSpacing();
  124. }
  125. /// <inheritdoc/>
  126. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  127. {
  128. _newSpecState.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  129. (uint format, bool formatSrgb) = _oldSpecState.GetFormat(_stageIndex, handle, cbufSlot);
  130. return ConvertToTextureFormat(format, formatSrgb);
  131. }
  132. /// <inheritdoc/>
  133. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  134. {
  135. _newSpecState.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  136. return _oldSpecState.GetTextureTarget(_stageIndex, handle, cbufSlot).ConvertSamplerType();
  137. }
  138. /// <inheritdoc/>
  139. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  140. {
  141. _newSpecState.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  142. return _oldSpecState.GetCoordNormalized(_stageIndex, handle, cbufSlot);
  143. }
  144. /// <inheritdoc/>
  145. public bool QueryTransformFeedbackEnabled()
  146. {
  147. return _oldSpecState.TransformFeedbackDescriptors != null;
  148. }
  149. /// <inheritdoc/>
  150. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  151. {
  152. return _oldSpecState.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  153. }
  154. /// <inheritdoc/>
  155. public int QueryTransformFeedbackStride(int bufferIndex)
  156. {
  157. return _oldSpecState.TransformFeedbackDescriptors[bufferIndex].Stride;
  158. }
  159. /// <inheritdoc/>
  160. public bool QueryEarlyZForce()
  161. {
  162. _newSpecState.RecordEarlyZForce();
  163. return _oldSpecState.GraphicsState.EarlyZForce;
  164. }
  165. /// <inheritdoc/>
  166. public bool QueryViewportTransformDisable()
  167. {
  168. return _oldSpecState.GraphicsState.ViewportTransformDisable;
  169. }
  170. /// <inheritdoc/>
  171. public void RegisterTexture(int handle, int cbufSlot)
  172. {
  173. if (!_oldSpecState.TextureRegistered(_stageIndex, handle, cbufSlot))
  174. {
  175. throw new DiskCacheLoadException(DiskCacheLoadResult.MissingTextureDescriptor);
  176. }
  177. (uint format, bool formatSrgb) = _oldSpecState.GetFormat(_stageIndex, handle, cbufSlot);
  178. TextureTarget target = _oldSpecState.GetTextureTarget(_stageIndex, handle, cbufSlot);
  179. bool coordNormalized = _oldSpecState.GetCoordNormalized(_stageIndex, handle, cbufSlot);
  180. _newSpecState.RegisterTexture(_stageIndex, handle, cbufSlot, format, formatSrgb, target, coordNormalized);
  181. }
  182. }
  183. }