surface_params.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/alignment.h"
  6. #include "common/bit_util.h"
  7. #include "common/cityhash.h"
  8. #include "common/common_types.h"
  9. #include "video_core/engines/fermi_2d.h"
  10. #include "video_core/engines/maxwell_3d.h"
  11. #include "video_core/shader/shader_ir.h"
  12. #include "video_core/surface.h"
  13. #include "video_core/textures/decoders.h"
  14. namespace VideoCommon {
  15. class FormatLookupTable;
  16. using VideoCore::Surface::SurfaceCompression;
  17. class SurfaceParams {
  18. public:
  19. /// Creates SurfaceCachedParams from a texture configuration.
  20. static SurfaceParams CreateForTexture(const FormatLookupTable& lookup_table,
  21. const Tegra::Texture::TICEntry& tic,
  22. const VideoCommon::Shader::Sampler& entry);
  23. /// Creates SurfaceCachedParams from an image configuration.
  24. static SurfaceParams CreateForImage(const FormatLookupTable& lookup_table,
  25. const Tegra::Texture::TICEntry& tic,
  26. const VideoCommon::Shader::Image& entry);
  27. /// Creates SurfaceCachedParams for a depth buffer configuration.
  28. static SurfaceParams CreateForDepthBuffer(
  29. Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format,
  30. u32 block_width, u32 block_height, u32 block_depth,
  31. Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type);
  32. /// Creates SurfaceCachedParams from a framebuffer configuration.
  33. static SurfaceParams CreateForFramebuffer(Core::System& system, std::size_t index);
  34. /// Creates SurfaceCachedParams from a Fermi2D surface configuration.
  35. static SurfaceParams CreateForFermiCopySurface(
  36. const Tegra::Engines::Fermi2D::Regs::Surface& config);
  37. /// Obtains the texture target from a shader's sampler entry.
  38. static VideoCore::Surface::SurfaceTarget ExpectedTarget(
  39. const VideoCommon::Shader::Sampler& entry);
  40. /// Obtains the texture target from a shader's sampler entry.
  41. static VideoCore::Surface::SurfaceTarget ExpectedTarget(
  42. const VideoCommon::Shader::Image& entry);
  43. std::size_t Hash() const {
  44. return static_cast<std::size_t>(
  45. Common::CityHash64(reinterpret_cast<const char*>(this), sizeof(*this)));
  46. }
  47. bool operator==(const SurfaceParams& rhs) const;
  48. bool operator!=(const SurfaceParams& rhs) const {
  49. return !operator==(rhs);
  50. }
  51. std::size_t GetGuestSizeInBytes() const {
  52. return GetInnerMemorySize(false, false, false);
  53. }
  54. std::size_t GetHostSizeInBytes() const {
  55. std::size_t host_size_in_bytes;
  56. if (GetCompressionType() == SurfaceCompression::Converted) {
  57. // ASTC is uncompressed in software, in emulated as RGBA8
  58. host_size_in_bytes = 0;
  59. for (u32 level = 0; level < num_levels; ++level) {
  60. host_size_in_bytes += GetConvertedMipmapSize(level);
  61. }
  62. } else {
  63. host_size_in_bytes = GetInnerMemorySize(true, false, false);
  64. }
  65. return host_size_in_bytes;
  66. }
  67. u32 GetBlockAlignedWidth() const {
  68. return Common::AlignUp(width, 64 / GetBytesPerPixel());
  69. }
  70. /// Returns the width of a given mipmap level.
  71. u32 GetMipWidth(u32 level) const {
  72. return std::max(1U, width >> level);
  73. }
  74. /// Returns the height of a given mipmap level.
  75. u32 GetMipHeight(u32 level) const {
  76. return std::max(1U, height >> level);
  77. }
  78. /// Returns the depth of a given mipmap level.
  79. u32 GetMipDepth(u32 level) const {
  80. return is_layered ? depth : std::max(1U, depth >> level);
  81. }
  82. /// Returns the block height of a given mipmap level.
  83. u32 GetMipBlockHeight(u32 level) const;
  84. /// Returns the block depth of a given mipmap level.
  85. u32 GetMipBlockDepth(u32 level) const;
  86. /// Returns the best possible row/pitch alignment for the surface.
  87. u32 GetRowAlignment(u32 level) const {
  88. const u32 bpp =
  89. GetCompressionType() == SurfaceCompression::Converted ? 4 : GetBytesPerPixel();
  90. return 1U << Common::CountTrailingZeroes32(GetMipWidth(level) * bpp);
  91. }
  92. /// Returns the offset in bytes in guest memory of a given mipmap level.
  93. std::size_t GetGuestMipmapLevelOffset(u32 level) const;
  94. /// Returns the offset in bytes in host memory (linear) of a given mipmap level.
  95. std::size_t GetHostMipmapLevelOffset(u32 level) const;
  96. /// Returns the offset in bytes in host memory (linear) of a given mipmap level
  97. /// for a texture that is converted in host gpu.
  98. std::size_t GetConvertedMipmapOffset(u32 level) const;
  99. /// Returns the size in bytes in guest memory of a given mipmap level.
  100. std::size_t GetGuestMipmapSize(u32 level) const {
  101. return GetInnerMipmapMemorySize(level, false, false);
  102. }
  103. /// Returns the size in bytes in host memory (linear) of a given mipmap level.
  104. std::size_t GetHostMipmapSize(u32 level) const {
  105. return GetInnerMipmapMemorySize(level, true, false) * GetNumLayers();
  106. }
  107. std::size_t GetConvertedMipmapSize(u32 level) const;
  108. /// Returns the size of a layer in bytes in guest memory.
  109. std::size_t GetGuestLayerSize() const {
  110. return GetLayerSize(false, false);
  111. }
  112. /// Returns the size of a layer in bytes in host memory for a given mipmap level.
  113. std::size_t GetHostLayerSize(u32 level) const {
  114. ASSERT(target != VideoCore::Surface::SurfaceTarget::Texture3D);
  115. return GetInnerMipmapMemorySize(level, true, false);
  116. }
  117. /// Returns the max possible mipmap that the texture can have in host gpu
  118. u32 MaxPossibleMipmap() const {
  119. const u32 max_mipmap_w = Common::Log2Ceil32(width) + 1U;
  120. const u32 max_mipmap_h = Common::Log2Ceil32(height) + 1U;
  121. const u32 max_mipmap = std::max(max_mipmap_w, max_mipmap_h);
  122. if (target != VideoCore::Surface::SurfaceTarget::Texture3D)
  123. return max_mipmap;
  124. return std::max(max_mipmap, Common::Log2Ceil32(depth) + 1U);
  125. }
  126. /// Returns if the guest surface is a compressed surface.
  127. bool IsCompressed() const {
  128. return GetDefaultBlockHeight() > 1 || GetDefaultBlockWidth() > 1;
  129. }
  130. /// Returns the default block width.
  131. u32 GetDefaultBlockWidth() const {
  132. return VideoCore::Surface::GetDefaultBlockWidth(pixel_format);
  133. }
  134. /// Returns the default block height.
  135. u32 GetDefaultBlockHeight() const {
  136. return VideoCore::Surface::GetDefaultBlockHeight(pixel_format);
  137. }
  138. /// Returns the bits per pixel.
  139. u32 GetBitsPerPixel() const {
  140. return VideoCore::Surface::GetFormatBpp(pixel_format);
  141. }
  142. /// Returns the bytes per pixel.
  143. u32 GetBytesPerPixel() const {
  144. return VideoCore::Surface::GetBytesPerPixel(pixel_format);
  145. }
  146. /// Returns true if the pixel format is a depth and/or stencil format.
  147. bool IsPixelFormatZeta() const {
  148. return pixel_format >= VideoCore::Surface::PixelFormat::MaxColorFormat &&
  149. pixel_format < VideoCore::Surface::PixelFormat::MaxDepthStencilFormat;
  150. }
  151. /// Returns how the compression should be handled for this texture.
  152. SurfaceCompression GetCompressionType() const {
  153. return VideoCore::Surface::GetFormatCompressionType(pixel_format);
  154. }
  155. /// Returns is the surface is a TextureBuffer type of surface.
  156. bool IsBuffer() const {
  157. return target == VideoCore::Surface::SurfaceTarget::TextureBuffer;
  158. }
  159. /// Returns the debug name of the texture for use in graphic debuggers.
  160. std::string TargetName() const;
  161. // Helper used for out of class size calculations
  162. static std::size_t AlignLayered(const std::size_t out_size, const u32 block_height,
  163. const u32 block_depth) {
  164. return Common::AlignBits(out_size,
  165. Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);
  166. }
  167. /// Converts a width from a type of surface into another. This helps represent the
  168. /// equivalent value between compressed/non-compressed textures.
  169. static u32 ConvertWidth(u32 width, VideoCore::Surface::PixelFormat pixel_format_from,
  170. VideoCore::Surface::PixelFormat pixel_format_to) {
  171. const u32 bw1 = VideoCore::Surface::GetDefaultBlockWidth(pixel_format_from);
  172. const u32 bw2 = VideoCore::Surface::GetDefaultBlockWidth(pixel_format_to);
  173. return (width * bw2 + bw1 - 1) / bw1;
  174. }
  175. /// Converts a height from a type of surface into another. This helps represent the
  176. /// equivalent value between compressed/non-compressed textures.
  177. static u32 ConvertHeight(u32 height, VideoCore::Surface::PixelFormat pixel_format_from,
  178. VideoCore::Surface::PixelFormat pixel_format_to) {
  179. const u32 bh1 = VideoCore::Surface::GetDefaultBlockHeight(pixel_format_from);
  180. const u32 bh2 = VideoCore::Surface::GetDefaultBlockHeight(pixel_format_to);
  181. return (height * bh2 + bh1 - 1) / bh1;
  182. }
  183. // Finds the maximun possible width between 2 2D layers of different formats
  184. static u32 IntersectWidth(const SurfaceParams& src_params, const SurfaceParams& dst_params,
  185. const u32 src_level, const u32 dst_level) {
  186. const u32 bw1 = src_params.GetDefaultBlockWidth();
  187. const u32 bw2 = dst_params.GetDefaultBlockWidth();
  188. const u32 t_src_width = (src_params.GetMipWidth(src_level) * bw2 + bw1 - 1) / bw1;
  189. const u32 t_dst_width = (dst_params.GetMipWidth(dst_level) * bw1 + bw2 - 1) / bw2;
  190. return std::min(t_src_width, t_dst_width);
  191. }
  192. // Finds the maximun possible height between 2 2D layers of different formats
  193. static u32 IntersectHeight(const SurfaceParams& src_params, const SurfaceParams& dst_params,
  194. const u32 src_level, const u32 dst_level) {
  195. const u32 bh1 = src_params.GetDefaultBlockHeight();
  196. const u32 bh2 = dst_params.GetDefaultBlockHeight();
  197. const u32 t_src_height = (src_params.GetMipHeight(src_level) * bh2 + bh1 - 1) / bh1;
  198. const u32 t_dst_height = (dst_params.GetMipHeight(dst_level) * bh1 + bh2 - 1) / bh2;
  199. return std::min(t_src_height, t_dst_height);
  200. }
  201. bool is_tiled;
  202. bool srgb_conversion;
  203. bool is_layered;
  204. u32 block_width;
  205. u32 block_height;
  206. u32 block_depth;
  207. u32 tile_width_spacing;
  208. u32 width;
  209. u32 height;
  210. u32 depth;
  211. u32 pitch;
  212. u32 num_levels;
  213. u32 emulated_levels;
  214. VideoCore::Surface::PixelFormat pixel_format;
  215. VideoCore::Surface::SurfaceType type;
  216. VideoCore::Surface::SurfaceTarget target;
  217. private:
  218. /// Returns the size of a given mipmap level inside a layer.
  219. std::size_t GetInnerMipmapMemorySize(u32 level, bool as_host_size, bool uncompressed) const;
  220. /// Returns the size of all mipmap levels and aligns as needed.
  221. std::size_t GetInnerMemorySize(bool as_host_size, bool layer_only, bool uncompressed) const {
  222. return GetLayerSize(as_host_size, uncompressed) * (layer_only ? 1U : depth);
  223. }
  224. /// Returns the size of a layer
  225. std::size_t GetLayerSize(bool as_host_size, bool uncompressed) const;
  226. std::size_t GetNumLayers() const {
  227. return is_layered ? depth : 1;
  228. }
  229. /// Returns true if these parameters are from a layered surface.
  230. bool IsLayered() const;
  231. };
  232. } // namespace VideoCommon
  233. namespace std {
  234. template <>
  235. struct hash<VideoCommon::SurfaceParams> {
  236. std::size_t operator()(const VideoCommon::SurfaceParams& k) const noexcept {
  237. return k.Hash();
  238. }
  239. };
  240. } // namespace std