TexturePool.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.GAL.Texture;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Gpu.Image
  8. {
  9. class TexturePool : Pool<Texture>
  10. {
  11. public LinkedListNode<TexturePool> CacheNode { get; set; }
  12. private int _sequenceNumber;
  13. public TexturePool(
  14. GpuContext context,
  15. ulong address,
  16. int maximumId) : base(context, address, maximumId) { }
  17. public override Texture Get(int id)
  18. {
  19. if ((uint)id >= Items.Length)
  20. {
  21. return null;
  22. }
  23. if (_sequenceNumber != Context.SequenceNumber)
  24. {
  25. _sequenceNumber = Context.SequenceNumber;
  26. SynchronizeMemory();
  27. }
  28. Texture texture = Items[id];
  29. if (texture == null)
  30. {
  31. TextureDescriptor descriptor = GetDescriptor(id);
  32. TextureInfo info = GetInfo(descriptor);
  33. // Bad address. We can't add a texture with a invalid address
  34. // to the cache.
  35. if (info.Address == MemoryManager.BadAddress)
  36. {
  37. return null;
  38. }
  39. texture = Context.Methods.TextureManager.FindOrCreateTexture(info, TextureSearchFlags.Sampler);
  40. texture.IncrementReferenceCount();
  41. Items[id] = texture;
  42. }
  43. else
  44. {
  45. // Memory is automatically synchronized on texture creation.
  46. texture.SynchronizeMemory();
  47. }
  48. return texture;
  49. }
  50. public TextureDescriptor GetDescriptor(int id)
  51. {
  52. ulong address = Address + (ulong)(uint)id * DescriptorSize;
  53. Span<byte> data = Context.PhysicalMemory.Read(address, DescriptorSize);
  54. return MemoryMarshal.Cast<byte, TextureDescriptor>(data)[0];
  55. }
  56. protected override void InvalidateRangeImpl(ulong address, ulong size)
  57. {
  58. ulong endAddress = address + size;
  59. for (; address < endAddress; address += DescriptorSize)
  60. {
  61. int id = (int)((address - Address) / DescriptorSize);
  62. Texture texture = Items[id];
  63. if (texture != null)
  64. {
  65. Span<byte> data = Context.PhysicalMemory.Read(address, DescriptorSize);
  66. TextureDescriptor descriptor = MemoryMarshal.Cast<byte, TextureDescriptor>(data)[0];
  67. // If the descriptors are the same, the texture is the same,
  68. // we don't need to remove as it was not modified. Just continue.
  69. if (texture.IsPerfectMatch(GetInfo(descriptor), TextureSearchFlags.Strict))
  70. {
  71. continue;
  72. }
  73. texture.DecrementReferenceCount();
  74. Items[id] = null;
  75. }
  76. }
  77. }
  78. private TextureInfo GetInfo(TextureDescriptor descriptor)
  79. {
  80. ulong address = Context.MemoryManager.Translate(descriptor.UnpackAddress());
  81. int width = descriptor.UnpackWidth();
  82. int height = descriptor.UnpackHeight();
  83. int depthOrLayers = descriptor.UnpackDepth();
  84. int levels = descriptor.UnpackLevels();
  85. TextureMsaaMode msaaMode = descriptor.UnpackTextureMsaaMode();
  86. int samplesInX = msaaMode.SamplesInX();
  87. int samplesInY = msaaMode.SamplesInY();
  88. int stride = descriptor.UnpackStride();
  89. TextureDescriptorType descriptorType = descriptor.UnpackTextureDescriptorType();
  90. bool isLinear = descriptorType == TextureDescriptorType.Linear;
  91. Target target = descriptor.UnpackTextureTarget().Convert((samplesInX | samplesInY) != 1);
  92. uint format = descriptor.UnpackFormat();
  93. bool srgb = descriptor.UnpackSrgb();
  94. if (!FormatTable.TryGetTextureFormat(format, srgb, out FormatInfo formatInfo))
  95. {
  96. // TODO: Warning.
  97. formatInfo = FormatInfo.Default;
  98. }
  99. int gobBlocksInY = descriptor.UnpackGobBlocksInY();
  100. int gobBlocksInZ = descriptor.UnpackGobBlocksInZ();
  101. int gobBlocksInTileX = descriptor.UnpackGobBlocksInTileX();
  102. SwizzleComponent swizzleR = descriptor.UnpackSwizzleR().Convert();
  103. SwizzleComponent swizzleG = descriptor.UnpackSwizzleG().Convert();
  104. SwizzleComponent swizzleB = descriptor.UnpackSwizzleB().Convert();
  105. SwizzleComponent swizzleA = descriptor.UnpackSwizzleA().Convert();
  106. DepthStencilMode depthStencilMode = GetDepthStencilMode(
  107. formatInfo.Format,
  108. swizzleR,
  109. swizzleG,
  110. swizzleB,
  111. swizzleA);
  112. return new TextureInfo(
  113. address,
  114. width,
  115. height,
  116. depthOrLayers,
  117. levels,
  118. samplesInX,
  119. samplesInY,
  120. stride,
  121. isLinear,
  122. gobBlocksInY,
  123. gobBlocksInZ,
  124. gobBlocksInTileX,
  125. target,
  126. formatInfo,
  127. depthStencilMode,
  128. swizzleR,
  129. swizzleG,
  130. swizzleB,
  131. swizzleA);
  132. }
  133. private static DepthStencilMode GetDepthStencilMode(Format format, params SwizzleComponent[] components)
  134. {
  135. // R = Depth, G = Stencil.
  136. // On 24-bits depth formats, this is inverted (Stencil is R etc).
  137. // NVN setup:
  138. // For depth, A is set to 1.0f, the other components are set to Depth.
  139. // For stencil, all components are set to Stencil.
  140. SwizzleComponent component = components[0];
  141. for (int index = 1; index < 4 && !IsRG(component); index++)
  142. {
  143. component = components[index];
  144. }
  145. if (!IsRG(component))
  146. {
  147. return DepthStencilMode.Depth;
  148. }
  149. if (format == Format.D24X8Unorm || format == Format.D24UnormS8Uint)
  150. {
  151. return component == SwizzleComponent.Red
  152. ? DepthStencilMode.Stencil
  153. : DepthStencilMode.Depth;
  154. }
  155. else
  156. {
  157. return component == SwizzleComponent.Red
  158. ? DepthStencilMode.Depth
  159. : DepthStencilMode.Stencil;
  160. }
  161. }
  162. private static bool IsRG(SwizzleComponent component)
  163. {
  164. return component == SwizzleComponent.Red ||
  165. component == SwizzleComponent.Green;
  166. }
  167. protected override void Delete(Texture item)
  168. {
  169. item?.DecrementReferenceCount();
  170. }
  171. }
  172. }