surface_params.h 11 KB

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