TextureDescriptorCapableGpuAccessor.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Image;
  3. using Ryujinx.Graphics.Shader;
  4. namespace Ryujinx.Graphics.Gpu.Shader
  5. {
  6. abstract class TextureDescriptorCapableGpuAccessor : IGpuAccessor
  7. {
  8. private readonly GpuContext _context;
  9. public TextureDescriptorCapableGpuAccessor(GpuContext context)
  10. {
  11. _context = context;
  12. }
  13. public abstract T MemoryRead<T>(ulong address) where T : unmanaged;
  14. public abstract ITextureDescriptor GetTextureDescriptor(int handle, int cbufSlot);
  15. /// <summary>
  16. /// Queries host about the presence of the FrontFacing built-in variable bug.
  17. /// </summary>
  18. /// <returns>True if the bug is present on the host device used, false otherwise</returns>
  19. public bool QueryHostHasFrontFacingBug() => _context.Capabilities.HasFrontFacingBug;
  20. /// <summary>
  21. /// Queries host about the presence of the vector indexing bug.
  22. /// </summary>
  23. /// <returns>True if the bug is present on the host device used, false otherwise</returns>
  24. public bool QueryHostHasVectorIndexingBug() => _context.Capabilities.HasVectorIndexingBug;
  25. /// <summary>
  26. /// Queries host storage buffer alignment required.
  27. /// </summary>
  28. /// <returns>Host storage buffer alignment in bytes</returns>
  29. public int QueryHostStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  30. /// <summary>
  31. /// Queries host support for readable images without a explicit format declaration on the shader.
  32. /// </summary>
  33. /// <returns>True if formatted image load is supported, false otherwise</returns>
  34. public bool QueryHostSupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  35. /// <summary>
  36. /// Queries host GPU non-constant texture offset support.
  37. /// </summary>
  38. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  39. public bool QueryHostSupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  40. /// <summary>
  41. /// Queries host GPU texture shadow LOD support.
  42. /// </summary>
  43. /// <returns>True if the GPU and driver supports texture shadow LOD, false otherwise</returns>
  44. public bool QueryHostSupportsTextureShadowLod() => _context.Capabilities.SupportsTextureShadowLod;
  45. /// <summary>
  46. /// Queries texture format information, for shaders using image load or store.
  47. /// </summary>
  48. /// <remarks>
  49. /// This only returns non-compressed color formats.
  50. /// If the format of the texture is a compressed, depth or unsupported format, then a default value is returned.
  51. /// </remarks>
  52. /// <param name="handle">Texture handle</param>
  53. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  54. /// <returns>Color format of the non-compressed texture</returns>
  55. public TextureFormat QueryTextureFormat(int handle, int cbufSlot = -1)
  56. {
  57. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  58. if (!FormatTable.TryGetTextureFormat(descriptor.UnpackFormat(), descriptor.UnpackSrgb(), out FormatInfo formatInfo))
  59. {
  60. return TextureFormat.Unknown;
  61. }
  62. return formatInfo.Format switch
  63. {
  64. Format.R8Unorm => TextureFormat.R8Unorm,
  65. Format.R8Snorm => TextureFormat.R8Snorm,
  66. Format.R8Uint => TextureFormat.R8Uint,
  67. Format.R8Sint => TextureFormat.R8Sint,
  68. Format.R16Float => TextureFormat.R16Float,
  69. Format.R16Unorm => TextureFormat.R16Unorm,
  70. Format.R16Snorm => TextureFormat.R16Snorm,
  71. Format.R16Uint => TextureFormat.R16Uint,
  72. Format.R16Sint => TextureFormat.R16Sint,
  73. Format.R32Float => TextureFormat.R32Float,
  74. Format.R32Uint => TextureFormat.R32Uint,
  75. Format.R32Sint => TextureFormat.R32Sint,
  76. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  77. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  78. Format.R8G8Uint => TextureFormat.R8G8Uint,
  79. Format.R8G8Sint => TextureFormat.R8G8Sint,
  80. Format.R16G16Float => TextureFormat.R16G16Float,
  81. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  82. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  83. Format.R16G16Uint => TextureFormat.R16G16Uint,
  84. Format.R16G16Sint => TextureFormat.R16G16Sint,
  85. Format.R32G32Float => TextureFormat.R32G32Float,
  86. Format.R32G32Uint => TextureFormat.R32G32Uint,
  87. Format.R32G32Sint => TextureFormat.R32G32Sint,
  88. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  89. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  90. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  91. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  92. Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
  93. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  94. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  95. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  96. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  97. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  98. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  99. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  100. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  101. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  102. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  103. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  104. _ => TextureFormat.Unknown
  105. };
  106. }
  107. /// <summary>
  108. /// Queries sampler type information.
  109. /// </summary>
  110. /// <param name="handle">Texture handle</param>
  111. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  112. /// <returns>The sampler type value for the given handle</returns>
  113. public SamplerType QuerySamplerType(int handle, int cbufSlot = -1)
  114. {
  115. return GetTextureDescriptor(handle, cbufSlot).UnpackTextureTarget().ConvertSamplerType();
  116. }
  117. /// <summary>
  118. /// Queries texture target information.
  119. /// </summary>
  120. /// <param name="handle">Texture handle</param>
  121. /// <param name="cbufSlot">Constant buffer slot for the texture handle</param>
  122. /// <returns>True if the texture is a rectangle texture, false otherwise</returns>
  123. public bool QueryIsTextureRectangle(int handle, int cbufSlot = -1)
  124. {
  125. var descriptor = GetTextureDescriptor(handle, cbufSlot);
  126. TextureTarget target = descriptor.UnpackTextureTarget();
  127. bool is2DTexture = target == TextureTarget.Texture2D ||
  128. target == TextureTarget.Texture2DRect;
  129. return !descriptor.UnpackTextureCoordNormalized() && is2DTexture;
  130. }
  131. }
  132. }