GpuAccessorBase.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Ryujinx.Graphics.GAL;
  2. using Ryujinx.Graphics.Gpu.Engine.Threed;
  3. using Ryujinx.Graphics.Gpu.Image;
  4. using Ryujinx.Graphics.Shader;
  5. namespace Ryujinx.Graphics.Gpu.Shader
  6. {
  7. /// <summary>
  8. /// GPU accessor.
  9. /// </summary>
  10. class GpuAccessorBase
  11. {
  12. private readonly GpuContext _context;
  13. /// <summary>
  14. /// Creates a new GPU accessor.
  15. /// </summary>
  16. /// <param name="context">GPU context</param>
  17. public GpuAccessorBase(GpuContext context)
  18. {
  19. _context = context;
  20. }
  21. /// <summary>
  22. /// Queries host about the presence of the FrontFacing built-in variable bug.
  23. /// </summary>
  24. /// <returns>True if the bug is present on the host device used, false otherwise</returns>
  25. public bool QueryHostHasFrontFacingBug() => _context.Capabilities.HasFrontFacingBug;
  26. /// <summary>
  27. /// Queries host about the presence of the vector indexing bug.
  28. /// </summary>
  29. /// <returns>True if the bug is present on the host device used, false otherwise</returns>
  30. public bool QueryHostHasVectorIndexingBug() => _context.Capabilities.HasVectorIndexingBug;
  31. /// <summary>
  32. /// Queries host storage buffer alignment required.
  33. /// </summary>
  34. /// <returns>Host storage buffer alignment in bytes</returns>
  35. public int QueryHostStorageBufferOffsetAlignment() => _context.Capabilities.StorageBufferOffsetAlignment;
  36. /// <summary>
  37. /// Queries host support for texture formats with BGRA component order (such as BGRA8).
  38. /// </summary>
  39. /// <returns>True if BGRA formats are supported, false otherwise</returns>
  40. public bool QueryHostSupportsBgraFormat() => _context.Capabilities.SupportsBgraFormat;
  41. /// <summary>
  42. /// Queries host support for fragment shader ordering critical sections on the shader code.
  43. /// </summary>
  44. /// <returns>True if fragment shader interlock is supported, false otherwise</returns>
  45. public bool QueryHostSupportsFragmentShaderInterlock() => _context.Capabilities.SupportsFragmentShaderInterlock;
  46. /// <summary>
  47. /// Queries host support for fragment shader ordering scoped critical sections on the shader code.
  48. /// </summary>
  49. /// <returns>True if fragment shader ordering is supported, false otherwise</returns>
  50. public bool QueryHostSupportsFragmentShaderOrderingIntel() => _context.Capabilities.SupportsFragmentShaderOrderingIntel;
  51. /// <summary>
  52. /// Queries host support for readable images without a explicit format declaration on the shader.
  53. /// </summary>
  54. /// <returns>True if formatted image load is supported, false otherwise</returns>
  55. public bool QueryHostSupportsImageLoadFormatted() => _context.Capabilities.SupportsImageLoadFormatted;
  56. /// <summary>
  57. /// Queries host GPU non-constant texture offset support.
  58. /// </summary>
  59. /// <returns>True if the GPU and driver supports non-constant texture offsets, false otherwise</returns>
  60. public bool QueryHostSupportsNonConstantTextureOffset() => _context.Capabilities.SupportsNonConstantTextureOffset;
  61. /// <summary>
  62. /// Queries host GPU shader ballot support.
  63. /// </summary>
  64. /// <returns>True if the GPU and driver supports shader ballot, false otherwise</returns>
  65. public bool QueryHostSupportsShaderBallot() => _context.Capabilities.SupportsShaderBallot;
  66. /// <summary>
  67. /// Queries host GPU texture shadow LOD support.
  68. /// </summary>
  69. /// <returns>True if the GPU and driver supports texture shadow LOD, false otherwise</returns>
  70. public bool QueryHostSupportsTextureShadowLod() => _context.Capabilities.SupportsTextureShadowLod;
  71. /// <summary>
  72. /// Converts a packed Maxwell texture format to the shader translator texture format.
  73. /// </summary>
  74. /// <param name="format">Packed maxwell format</param>
  75. /// <param name="formatSrgb">Indicates if the format is sRGB</param>
  76. /// <returns>Shader translator texture format</returns>
  77. protected static TextureFormat ConvertToTextureFormat(uint format, bool formatSrgb)
  78. {
  79. if (!FormatTable.TryGetTextureFormat(format, formatSrgb, out FormatInfo formatInfo))
  80. {
  81. return TextureFormat.Unknown;
  82. }
  83. return formatInfo.Format switch
  84. {
  85. Format.R8Unorm => TextureFormat.R8Unorm,
  86. Format.R8Snorm => TextureFormat.R8Snorm,
  87. Format.R8Uint => TextureFormat.R8Uint,
  88. Format.R8Sint => TextureFormat.R8Sint,
  89. Format.R16Float => TextureFormat.R16Float,
  90. Format.R16Unorm => TextureFormat.R16Unorm,
  91. Format.R16Snorm => TextureFormat.R16Snorm,
  92. Format.R16Uint => TextureFormat.R16Uint,
  93. Format.R16Sint => TextureFormat.R16Sint,
  94. Format.R32Float => TextureFormat.R32Float,
  95. Format.R32Uint => TextureFormat.R32Uint,
  96. Format.R32Sint => TextureFormat.R32Sint,
  97. Format.R8G8Unorm => TextureFormat.R8G8Unorm,
  98. Format.R8G8Snorm => TextureFormat.R8G8Snorm,
  99. Format.R8G8Uint => TextureFormat.R8G8Uint,
  100. Format.R8G8Sint => TextureFormat.R8G8Sint,
  101. Format.R16G16Float => TextureFormat.R16G16Float,
  102. Format.R16G16Unorm => TextureFormat.R16G16Unorm,
  103. Format.R16G16Snorm => TextureFormat.R16G16Snorm,
  104. Format.R16G16Uint => TextureFormat.R16G16Uint,
  105. Format.R16G16Sint => TextureFormat.R16G16Sint,
  106. Format.R32G32Float => TextureFormat.R32G32Float,
  107. Format.R32G32Uint => TextureFormat.R32G32Uint,
  108. Format.R32G32Sint => TextureFormat.R32G32Sint,
  109. Format.R8G8B8A8Unorm => TextureFormat.R8G8B8A8Unorm,
  110. Format.R8G8B8A8Snorm => TextureFormat.R8G8B8A8Snorm,
  111. Format.R8G8B8A8Uint => TextureFormat.R8G8B8A8Uint,
  112. Format.R8G8B8A8Sint => TextureFormat.R8G8B8A8Sint,
  113. Format.R8G8B8A8Srgb => TextureFormat.R8G8B8A8Unorm,
  114. Format.R16G16B16A16Float => TextureFormat.R16G16B16A16Float,
  115. Format.R16G16B16A16Unorm => TextureFormat.R16G16B16A16Unorm,
  116. Format.R16G16B16A16Snorm => TextureFormat.R16G16B16A16Snorm,
  117. Format.R16G16B16A16Uint => TextureFormat.R16G16B16A16Uint,
  118. Format.R16G16B16A16Sint => TextureFormat.R16G16B16A16Sint,
  119. Format.R32G32B32A32Float => TextureFormat.R32G32B32A32Float,
  120. Format.R32G32B32A32Uint => TextureFormat.R32G32B32A32Uint,
  121. Format.R32G32B32A32Sint => TextureFormat.R32G32B32A32Sint,
  122. Format.R10G10B10A2Unorm => TextureFormat.R10G10B10A2Unorm,
  123. Format.R10G10B10A2Uint => TextureFormat.R10G10B10A2Uint,
  124. Format.R11G11B10Float => TextureFormat.R11G11B10Float,
  125. _ => TextureFormat.Unknown
  126. };
  127. }
  128. /// <summary>
  129. /// Converts the Maxwell primitive topology to the shader translator topology.
  130. /// </summary>
  131. /// <param name="topology">Maxwell primitive topology</param>
  132. /// <param name="tessellationMode">Maxwell tessellation mode</param>
  133. /// <returns>Shader translator topology</returns>
  134. protected static InputTopology ConvertToInputTopology(PrimitiveTopology topology, TessMode tessellationMode)
  135. {
  136. return topology switch
  137. {
  138. PrimitiveTopology.Points => InputTopology.Points,
  139. PrimitiveTopology.Lines or
  140. PrimitiveTopology.LineLoop or
  141. PrimitiveTopology.LineStrip => InputTopology.Lines,
  142. PrimitiveTopology.LinesAdjacency or
  143. PrimitiveTopology.LineStripAdjacency => InputTopology.LinesAdjacency,
  144. PrimitiveTopology.Triangles or
  145. PrimitiveTopology.TriangleStrip or
  146. PrimitiveTopology.TriangleFan => InputTopology.Triangles,
  147. PrimitiveTopology.TrianglesAdjacency or
  148. PrimitiveTopology.TriangleStripAdjacency => InputTopology.TrianglesAdjacency,
  149. PrimitiveTopology.Patches => tessellationMode.UnpackPatchType() == TessPatchType.Isolines
  150. ? InputTopology.Lines
  151. : InputTopology.Triangles,
  152. _ => InputTopology.Points
  153. };
  154. }
  155. }
  156. }