DiskCacheGpuAccessor.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 int QueryBindingConstantBuffer(int index)
  65. {
  66. return _resourceCounts.UniformBuffersCount++;
  67. }
  68. /// <inheritdoc/>
  69. public int QueryBindingStorageBuffer(int index)
  70. {
  71. return _resourceCounts.StorageBuffersCount++;
  72. }
  73. /// <inheritdoc/>
  74. public int QueryBindingTexture(int index)
  75. {
  76. return _resourceCounts.TexturesCount++;
  77. }
  78. /// <inheritdoc/>
  79. public int QueryBindingImage(int index)
  80. {
  81. return _resourceCounts.ImagesCount++;
  82. }
  83. /// <inheritdoc/>
  84. public int QueryComputeLocalSizeX() => _oldSpecState.ComputeState.LocalSizeX;
  85. /// <inheritdoc/>
  86. public int QueryComputeLocalSizeY() => _oldSpecState.ComputeState.LocalSizeY;
  87. /// <inheritdoc/>
  88. public int QueryComputeLocalSizeZ() => _oldSpecState.ComputeState.LocalSizeZ;
  89. /// <inheritdoc/>
  90. public int QueryComputeLocalMemorySize() => _oldSpecState.ComputeState.LocalMemorySize;
  91. /// <inheritdoc/>
  92. public int QueryComputeSharedMemorySize() => _oldSpecState.ComputeState.SharedMemorySize;
  93. /// <inheritdoc/>
  94. public uint QueryConstantBufferUse()
  95. {
  96. _newSpecState.RecordConstantBufferUse(_stageIndex, _oldSpecState.ConstantBufferUse[_stageIndex]);
  97. return _oldSpecState.ConstantBufferUse[_stageIndex];
  98. }
  99. /// <inheritdoc/>
  100. public InputTopology QueryPrimitiveTopology()
  101. {
  102. _newSpecState.RecordPrimitiveTopology();
  103. return ConvertToInputTopology(_oldSpecState.GraphicsState.Topology, _oldSpecState.GraphicsState.TessellationMode);
  104. }
  105. /// <inheritdoc/>
  106. public bool QueryTessCw()
  107. {
  108. return _oldSpecState.GraphicsState.TessellationMode.UnpackCw();
  109. }
  110. /// <inheritdoc/>
  111. public TessPatchType QueryTessPatchType()
  112. {
  113. return _oldSpecState.GraphicsState.TessellationMode.UnpackPatchType();
  114. }
  115. /// <inheritdoc/>
  116. public TessSpacing QueryTessSpacing()
  117. {
  118. return _oldSpecState.GraphicsState.TessellationMode.UnpackSpacing();
  119. }
  120. /// <inheritdoc/>
  121. public TextureFormat QueryTextureFormat(int handle, int cbufSlot)
  122. {
  123. _newSpecState.RecordTextureFormat(_stageIndex, handle, cbufSlot);
  124. (uint format, bool formatSrgb) = _oldSpecState.GetFormat(_stageIndex, handle, cbufSlot);
  125. return ConvertToTextureFormat(format, formatSrgb);
  126. }
  127. /// <inheritdoc/>
  128. public SamplerType QuerySamplerType(int handle, int cbufSlot)
  129. {
  130. _newSpecState.RecordTextureSamplerType(_stageIndex, handle, cbufSlot);
  131. return _oldSpecState.GetTextureTarget(_stageIndex, handle, cbufSlot).ConvertSamplerType();
  132. }
  133. /// <inheritdoc/>
  134. public bool QueryTextureCoordNormalized(int handle, int cbufSlot)
  135. {
  136. _newSpecState.RecordTextureCoordNormalized(_stageIndex, handle, cbufSlot);
  137. return _oldSpecState.GetCoordNormalized(_stageIndex, handle, cbufSlot);
  138. }
  139. /// <inheritdoc/>
  140. public bool QueryTransformFeedbackEnabled()
  141. {
  142. return _oldSpecState.TransformFeedbackDescriptors != null;
  143. }
  144. /// <inheritdoc/>
  145. public ReadOnlySpan<byte> QueryTransformFeedbackVaryingLocations(int bufferIndex)
  146. {
  147. return _oldSpecState.TransformFeedbackDescriptors[bufferIndex].AsSpan();
  148. }
  149. /// <inheritdoc/>
  150. public int QueryTransformFeedbackStride(int bufferIndex)
  151. {
  152. return _oldSpecState.TransformFeedbackDescriptors[bufferIndex].Stride;
  153. }
  154. /// <inheritdoc/>
  155. public bool QueryEarlyZForce()
  156. {
  157. _newSpecState.RecordEarlyZForce();
  158. return _oldSpecState.GraphicsState.EarlyZForce;
  159. }
  160. /// <inheritdoc/>
  161. public bool QueryViewportTransformDisable()
  162. {
  163. return _oldSpecState.GraphicsState.ViewportTransformDisable;
  164. }
  165. /// <inheritdoc/>
  166. public void RegisterTexture(int handle, int cbufSlot)
  167. {
  168. if (!_oldSpecState.TextureRegistered(_stageIndex, handle, cbufSlot))
  169. {
  170. throw new DiskCacheLoadException(DiskCacheLoadResult.MissingTextureDescriptor);
  171. }
  172. (uint format, bool formatSrgb) = _oldSpecState.GetFormat(_stageIndex, handle, cbufSlot);
  173. TextureTarget target = _oldSpecState.GetTextureTarget(_stageIndex, handle, cbufSlot);
  174. bool coordNormalized = _oldSpecState.GetCoordNormalized(_stageIndex, handle, cbufSlot);
  175. _newSpecState.RegisterTexture(_stageIndex, handle, cbufSlot, format, formatSrgb, target, coordNormalized);
  176. }
  177. }
  178. }