TexturePool.cs 13 KB

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