TexturePool.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gpu.Image
  6. {
  7. /// <summary>
  8. /// Texture pool.
  9. /// </summary>
  10. class TexturePool : Pool<Texture>
  11. {
  12. private int _sequenceNumber;
  13. /// <summary>
  14. /// Intrusive linked list node used on the texture pool cache.
  15. /// </summary>
  16. public LinkedListNode<TexturePool> CacheNode { get; set; }
  17. /// <summary>
  18. /// Constructs a new instance of the texture pool.
  19. /// </summary>
  20. /// <param name="context">GPU context that the texture pool belongs to</param>
  21. /// <param name="address">Address of the texture pool in guest memory</param>
  22. /// <param name="maximumId">Maximum texture ID of the texture pool (equal to maximum textures minus one)</param>
  23. public TexturePool(GpuContext context, ulong address, int maximumId) : base(context, address, maximumId) { }
  24. /// <summary>
  25. /// Gets the texture with the given ID.
  26. /// </summary>
  27. /// <param name="id">ID of the texture. This is effectively a zero-based index</param>
  28. /// <returns>The texture with the given ID</returns>
  29. public override Texture Get(int id)
  30. {
  31. if ((uint)id >= Items.Length)
  32. {
  33. return null;
  34. }
  35. if (_sequenceNumber != Context.SequenceNumber)
  36. {
  37. _sequenceNumber = Context.SequenceNumber;
  38. SynchronizeMemory();
  39. }
  40. Texture texture = Items[id];
  41. if (texture == null)
  42. {
  43. TextureDescriptor descriptor = GetDescriptor(id);
  44. TextureInfo info = GetInfo(descriptor);
  45. // Bad address. We can't add a texture with a invalid address
  46. // to the cache.
  47. if (info.Address == MemoryManager.BadAddress)
  48. {
  49. return null;
  50. }
  51. texture = Context.Methods.TextureManager.FindOrCreateTexture(info, TextureSearchFlags.ForSampler);
  52. texture.IncrementReferenceCount();
  53. Items[id] = texture;
  54. }
  55. else
  56. {
  57. // Memory is automatically synchronized on texture creation.
  58. texture.SynchronizeMemory();
  59. }
  60. return texture;
  61. }
  62. /// <summary>
  63. /// Gets the texture descriptor from a given texture ID.
  64. /// </summary>
  65. /// <param name="id">ID of the texture. This is effectively a zero-based index</param>
  66. /// <returns>The texture descriptor</returns>
  67. public TextureDescriptor GetDescriptor(int id)
  68. {
  69. return Context.PhysicalMemory.Read<TextureDescriptor>(Address + (ulong)id * DescriptorSize);
  70. }
  71. /// <summary>
  72. /// Implementation of the texture pool range invalidation.
  73. /// </summary>
  74. /// <param name="address">Start address of the range of the texture pool</param>
  75. /// <param name="size">Size of the range being invalidated</param>
  76. protected override void InvalidateRangeImpl(ulong address, ulong size)
  77. {
  78. ulong endAddress = address + size;
  79. for (; address < endAddress; address += DescriptorSize)
  80. {
  81. int id = (int)((address - Address) / DescriptorSize);
  82. Texture texture = Items[id];
  83. if (texture != null)
  84. {
  85. TextureDescriptor descriptor = Context.PhysicalMemory.Read<TextureDescriptor>(address);
  86. // If the descriptors are the same, the texture is the same,
  87. // we don't need to remove as it was not modified. Just continue.
  88. if (texture.IsPerfectMatch(GetInfo(descriptor), TextureSearchFlags.Strict))
  89. {
  90. continue;
  91. }
  92. texture.DecrementReferenceCount();
  93. Items[id] = null;
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// Gets texture information from a texture descriptor.
  99. /// </summary>
  100. /// <param name="descriptor">The texture descriptor</param>
  101. /// <returns>The texture information</returns>
  102. private TextureInfo GetInfo(TextureDescriptor descriptor)
  103. {
  104. ulong address = Context.MemoryManager.Translate(descriptor.UnpackAddress());
  105. int width = descriptor.UnpackWidth();
  106. int height = descriptor.UnpackHeight();
  107. int depthOrLayers = descriptor.UnpackDepth();
  108. int levels = descriptor.UnpackLevels();
  109. TextureMsaaMode msaaMode = descriptor.UnpackTextureMsaaMode();
  110. int samplesInX = msaaMode.SamplesInX();
  111. int samplesInY = msaaMode.SamplesInY();
  112. int stride = descriptor.UnpackStride();
  113. TextureDescriptorType descriptorType = descriptor.UnpackTextureDescriptorType();
  114. bool isLinear = descriptorType == TextureDescriptorType.Linear;
  115. Target target = descriptor.UnpackTextureTarget().Convert((samplesInX | samplesInY) != 1);
  116. // We use 2D targets for 1D textures as that makes texture cache
  117. // management easier. We don't know the target for render target
  118. // and copies, so those would normally use 2D targets, which are
  119. // not compatible with 1D targets. By doing that we also allow those
  120. // to match when looking for compatible textures on the cache.
  121. if (target == Target.Texture1D)
  122. {
  123. target = Target.Texture2D;
  124. height = 1;
  125. }
  126. else if (target == Target.Texture1DArray)
  127. {
  128. target = Target.Texture2DArray;
  129. height = 1;
  130. }
  131. uint format = descriptor.UnpackFormat();
  132. bool srgb = descriptor.UnpackSrgb();
  133. if (!FormatTable.TryGetTextureFormat(format, srgb, out FormatInfo formatInfo))
  134. {
  135. if ((long)address > 0L && (int)format > 0)
  136. {
  137. Logger.Error?.Print(LogClass.Gpu, $"Invalid texture format 0x{format:X} (sRGB: {srgb}).");
  138. }
  139. formatInfo = FormatInfo.Default;
  140. }
  141. int gobBlocksInY = descriptor.UnpackGobBlocksInY();
  142. int gobBlocksInZ = descriptor.UnpackGobBlocksInZ();
  143. int gobBlocksInTileX = descriptor.UnpackGobBlocksInTileX();
  144. SwizzleComponent swizzleR = descriptor.UnpackSwizzleR().Convert();
  145. SwizzleComponent swizzleG = descriptor.UnpackSwizzleG().Convert();
  146. SwizzleComponent swizzleB = descriptor.UnpackSwizzleB().Convert();
  147. SwizzleComponent swizzleA = descriptor.UnpackSwizzleA().Convert();
  148. DepthStencilMode depthStencilMode = GetDepthStencilMode(
  149. formatInfo.Format,
  150. swizzleR,
  151. swizzleG,
  152. swizzleB,
  153. swizzleA);
  154. if (formatInfo.Format.IsDepthOrStencil())
  155. {
  156. swizzleR = SwizzleComponent.Red;
  157. swizzleG = SwizzleComponent.Red;
  158. swizzleB = SwizzleComponent.Red;
  159. if (depthStencilMode == DepthStencilMode.Depth)
  160. {
  161. swizzleA = SwizzleComponent.One;
  162. }
  163. else
  164. {
  165. swizzleA = SwizzleComponent.Red;
  166. }
  167. }
  168. return new TextureInfo(
  169. address,
  170. width,
  171. height,
  172. depthOrLayers,
  173. levels,
  174. samplesInX,
  175. samplesInY,
  176. stride,
  177. isLinear,
  178. gobBlocksInY,
  179. gobBlocksInZ,
  180. gobBlocksInTileX,
  181. target,
  182. formatInfo,
  183. depthStencilMode,
  184. swizzleR,
  185. swizzleG,
  186. swizzleB,
  187. swizzleA);
  188. }
  189. /// <summary>
  190. /// Gets the texture depth-stencil mode, based on the swizzle components of each color channel.
  191. /// The depth-stencil mode is determined based on how the driver sets those parameters.
  192. /// </summary>
  193. /// <param name="format">The format of the texture</param>
  194. /// <param name="components">The texture swizzle components</param>
  195. /// <returns>The depth-stencil mode</returns>
  196. private static DepthStencilMode GetDepthStencilMode(Format format, params SwizzleComponent[] components)
  197. {
  198. // R = Depth, G = Stencil.
  199. // On 24-bits depth formats, this is inverted (Stencil is R etc).
  200. // NVN setup:
  201. // For depth, A is set to 1.0f, the other components are set to Depth.
  202. // For stencil, all components are set to Stencil.
  203. SwizzleComponent component = components[0];
  204. for (int index = 1; index < 4 && !IsRG(component); index++)
  205. {
  206. component = components[index];
  207. }
  208. if (!IsRG(component))
  209. {
  210. return DepthStencilMode.Depth;
  211. }
  212. if (format == Format.D24X8Unorm || format == Format.D24UnormS8Uint)
  213. {
  214. return component == SwizzleComponent.Red
  215. ? DepthStencilMode.Stencil
  216. : DepthStencilMode.Depth;
  217. }
  218. else
  219. {
  220. return component == SwizzleComponent.Red
  221. ? DepthStencilMode.Depth
  222. : DepthStencilMode.Stencil;
  223. }
  224. }
  225. /// <summary>
  226. /// Checks if the swizzle component is equal to the red or green channels.
  227. /// </summary>
  228. /// <param name="component">The swizzle component to check</param>
  229. /// <returns>True if the swizzle component is equal to the red or green, false otherwise</returns>
  230. private static bool IsRG(SwizzleComponent component)
  231. {
  232. return component == SwizzleComponent.Red ||
  233. component == SwizzleComponent.Green;
  234. }
  235. /// <summary>
  236. /// Decrements the reference count of the texture.
  237. /// This indicates that the texture pool is not using it anymore.
  238. /// </summary>
  239. /// <param name="item">The texture to be deleted</param>
  240. protected override void Delete(Texture item)
  241. {
  242. item?.DecrementReferenceCount();
  243. }
  244. }
  245. }