TextureDescriptorCapableGpuAccessor.cs 8.6 KB

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