surface_params.h 11 KB

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