surface_params.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include "common/alignment.h"
  6. #include "common/cityhash.h"
  7. #include "core/core.h"
  8. #include "video_core/engines/shader_bytecode.h"
  9. #include "video_core/surface.h"
  10. #include "video_core/texture_cache/surface_params.h"
  11. namespace VideoCommon {
  12. using VideoCore::Surface::ComponentTypeFromDepthFormat;
  13. using VideoCore::Surface::ComponentTypeFromRenderTarget;
  14. using VideoCore::Surface::ComponentTypeFromTexture;
  15. using VideoCore::Surface::PixelFormatFromDepthFormat;
  16. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  17. using VideoCore::Surface::PixelFormatFromTextureFormat;
  18. using VideoCore::Surface::SurfaceTarget;
  19. using VideoCore::Surface::SurfaceTargetFromTextureType;
  20. SurfaceTarget TextureType2SurfaceTarget(Tegra::Shader::TextureType type, bool is_array) {
  21. switch (type) {
  22. case Tegra::Shader::TextureType::Texture1D: {
  23. if (is_array)
  24. return SurfaceTarget::Texture1DArray;
  25. else
  26. return SurfaceTarget::Texture1D;
  27. }
  28. case Tegra::Shader::TextureType::Texture2D: {
  29. if (is_array)
  30. return SurfaceTarget::Texture2DArray;
  31. else
  32. return SurfaceTarget::Texture2D;
  33. }
  34. case Tegra::Shader::TextureType::Texture3D: {
  35. ASSERT(!is_array);
  36. return SurfaceTarget::Texture3D;
  37. }
  38. case Tegra::Shader::TextureType::TextureCube: {
  39. if (is_array)
  40. return SurfaceTarget::TextureCubeArray;
  41. else
  42. return SurfaceTarget::TextureCubemap;
  43. }
  44. default: {
  45. UNREACHABLE();
  46. return SurfaceTarget::Texture2D;
  47. }
  48. }
  49. }
  50. namespace {
  51. constexpr u32 GetMipmapSize(bool uncompressed, u32 mip_size, u32 tile) {
  52. return uncompressed ? mip_size : std::max(1U, (mip_size + tile - 1) / tile);
  53. }
  54. } // Anonymous namespace
  55. SurfaceParams SurfaceParams::CreateForTexture(Core::System& system,
  56. const Tegra::Texture::FullTextureInfo& config,
  57. const VideoCommon::Shader::Sampler& entry) {
  58. SurfaceParams params;
  59. params.is_tiled = config.tic.IsTiled();
  60. params.srgb_conversion = config.tic.IsSrgbConversionEnabled();
  61. params.block_width = params.is_tiled ? config.tic.BlockWidth() : 0,
  62. params.block_height = params.is_tiled ? config.tic.BlockHeight() : 0,
  63. params.block_depth = params.is_tiled ? config.tic.BlockDepth() : 0,
  64. params.tile_width_spacing = params.is_tiled ? (1 << config.tic.tile_width_spacing.Value()) : 1;
  65. params.pixel_format = PixelFormatFromTextureFormat(config.tic.format, config.tic.r_type.Value(),
  66. params.srgb_conversion);
  67. params.component_type = ComponentTypeFromTexture(config.tic.r_type.Value());
  68. params.type = GetFormatType(params.pixel_format);
  69. // TODO: on 1DBuffer we should use the tic info.
  70. params.target = TextureType2SurfaceTarget(entry.GetType(), entry.IsArray());
  71. params.width = Common::AlignUp(config.tic.Width(), GetCompressionFactor(params.pixel_format));
  72. params.height = Common::AlignUp(config.tic.Height(), GetCompressionFactor(params.pixel_format));
  73. params.depth = config.tic.Depth();
  74. if (params.target == SurfaceTarget::TextureCubemap ||
  75. params.target == SurfaceTarget::TextureCubeArray) {
  76. params.depth *= 6;
  77. }
  78. params.pitch = params.is_tiled ? 0 : config.tic.Pitch();
  79. params.unaligned_height = config.tic.Height();
  80. params.num_levels = config.tic.max_mip_level + 1;
  81. params.is_layered = params.IsLayered();
  82. return params;
  83. }
  84. SurfaceParams SurfaceParams::CreateForDepthBuffer(
  85. Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format,
  86. u32 block_width, u32 block_height, u32 block_depth,
  87. Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type) {
  88. SurfaceParams params;
  89. params.is_tiled = type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
  90. params.srgb_conversion = false;
  91. params.block_width = std::min(block_width, 5U);
  92. params.block_height = std::min(block_height, 5U);
  93. params.block_depth = std::min(block_depth, 5U);
  94. params.tile_width_spacing = 1;
  95. params.pixel_format = PixelFormatFromDepthFormat(format);
  96. params.component_type = ComponentTypeFromDepthFormat(format);
  97. params.type = GetFormatType(params.pixel_format);
  98. params.width = zeta_width;
  99. params.height = zeta_height;
  100. params.unaligned_height = zeta_height;
  101. params.target = SurfaceTarget::Texture2D;
  102. params.depth = 1;
  103. params.num_levels = 1;
  104. params.is_layered = false;
  105. return params;
  106. }
  107. SurfaceParams SurfaceParams::CreateForFramebuffer(Core::System& system, std::size_t index) {
  108. const auto& config{system.GPU().Maxwell3D().regs.rt[index]};
  109. SurfaceParams params;
  110. params.is_tiled =
  111. config.memory_layout.type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
  112. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
  113. config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
  114. params.block_width = config.memory_layout.block_width;
  115. params.block_height = config.memory_layout.block_height;
  116. params.block_depth = config.memory_layout.block_depth;
  117. params.tile_width_spacing = 1;
  118. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  119. params.component_type = ComponentTypeFromRenderTarget(config.format);
  120. params.type = GetFormatType(params.pixel_format);
  121. if (params.is_tiled) {
  122. params.width = config.width;
  123. } else {
  124. const u32 bpp = GetFormatBpp(params.pixel_format) / CHAR_BIT;
  125. params.pitch = config.width;
  126. params.width = params.pitch / bpp;
  127. }
  128. params.height = config.height;
  129. params.depth = 1;
  130. params.unaligned_height = config.height;
  131. params.target = SurfaceTarget::Texture2D;
  132. params.num_levels = 1;
  133. params.is_layered = false;
  134. return params;
  135. }
  136. SurfaceParams SurfaceParams::CreateForFermiCopySurface(
  137. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  138. SurfaceParams params{};
  139. params.is_tiled = !config.linear;
  140. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
  141. config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
  142. params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 5U) : 0,
  143. params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 5U) : 0,
  144. params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 5U) : 0,
  145. params.tile_width_spacing = 1;
  146. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  147. params.component_type = ComponentTypeFromRenderTarget(config.format);
  148. params.type = GetFormatType(params.pixel_format);
  149. params.width = config.width;
  150. params.height = config.height;
  151. params.pitch = config.pitch;
  152. params.unaligned_height = config.height;
  153. // TODO(Rodrigo): Try to guess the surface target from depth and layer parameters
  154. params.target = SurfaceTarget::Texture2D;
  155. params.depth = 1;
  156. params.num_levels = 1;
  157. params.is_layered = params.IsLayered();
  158. return params;
  159. }
  160. bool SurfaceParams::IsLayered() const {
  161. switch (target) {
  162. case SurfaceTarget::Texture1DArray:
  163. case SurfaceTarget::Texture2DArray:
  164. case SurfaceTarget::TextureCubemap:
  165. case SurfaceTarget::TextureCubeArray:
  166. return true;
  167. default:
  168. return false;
  169. }
  170. }
  171. u32 SurfaceParams::GetMipBlockHeight(u32 level) const {
  172. // Auto block resizing algorithm from:
  173. // https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
  174. if (level == 0) {
  175. return this->block_height;
  176. }
  177. const u32 height{GetMipHeight(level)};
  178. const u32 default_block_height{GetDefaultBlockHeight()};
  179. const u32 blocks_in_y{(height + default_block_height - 1) / default_block_height};
  180. u32 block_height = 4;
  181. while (block_height > 0 && blocks_in_y <= (1U << block_height) * 4) {
  182. --block_height;
  183. }
  184. return block_height;
  185. }
  186. u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
  187. if (level == 0) {
  188. return this->block_depth;
  189. }
  190. if (is_layered) {
  191. return 0;
  192. }
  193. const u32 depth{GetMipDepth(level)};
  194. u32 block_depth = 5;
  195. while (block_depth > 0 && depth * 2 <= (1U << block_depth)) {
  196. --block_depth;
  197. }
  198. if (block_depth == 5 && GetMipBlockHeight(level) >= 2) {
  199. return 4;
  200. }
  201. return block_depth;
  202. }
  203. std::size_t SurfaceParams::GetGuestMipmapLevelOffset(u32 level) const {
  204. std::size_t offset = 0;
  205. for (u32 i = 0; i < level; i++) {
  206. offset += GetInnerMipmapMemorySize(i, false, false);
  207. }
  208. return offset;
  209. }
  210. std::size_t SurfaceParams::GetHostMipmapLevelOffset(u32 level) const {
  211. std::size_t offset = 0;
  212. for (u32 i = 0; i < level; i++) {
  213. offset += GetInnerMipmapMemorySize(i, true, false) * GetNumLayers();
  214. }
  215. return offset;
  216. }
  217. std::size_t SurfaceParams::GetGuestMipmapSize(u32 level) const {
  218. return GetInnerMipmapMemorySize(level, false, false);
  219. }
  220. std::size_t SurfaceParams::GetHostMipmapSize(u32 level) const {
  221. return GetInnerMipmapMemorySize(level, true, false) * GetNumLayers();
  222. }
  223. std::size_t SurfaceParams::GetGuestLayerSize() const {
  224. return GetLayerSize(false, false);
  225. }
  226. std::size_t SurfaceParams::GetLayerSize(bool as_host_size, bool uncompressed) const {
  227. std::size_t size = 0;
  228. for (u32 level = 0; level < num_levels; ++level) {
  229. size += GetInnerMipmapMemorySize(level, as_host_size, uncompressed);
  230. }
  231. if (is_tiled && is_layered) {
  232. return Common::AlignBits(size,
  233. Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);
  234. }
  235. return size;
  236. }
  237. std::size_t SurfaceParams::GetHostLayerSize(u32 level) const {
  238. ASSERT(target != SurfaceTarget::Texture3D);
  239. return GetInnerMipmapMemorySize(level, true, false);
  240. }
  241. bool SurfaceParams::IsPixelFormatZeta() const {
  242. return pixel_format >= VideoCore::Surface::PixelFormat::MaxColorFormat &&
  243. pixel_format < VideoCore::Surface::PixelFormat::MaxDepthStencilFormat;
  244. }
  245. std::size_t SurfaceParams::GetInnerMipmapMemorySize(u32 level, bool as_host_size,
  246. bool uncompressed) const {
  247. const bool tiled{as_host_size ? false : is_tiled};
  248. const u32 width{GetMipmapSize(uncompressed, GetMipWidth(level), GetDefaultBlockWidth())};
  249. const u32 height{GetMipmapSize(uncompressed, GetMipHeight(level), GetDefaultBlockHeight())};
  250. const u32 depth{is_layered ? 1U : GetMipDepth(level)};
  251. return Tegra::Texture::CalculateSize(tiled, GetBytesPerPixel(), width, height, depth,
  252. GetMipBlockHeight(level), GetMipBlockDepth(level));
  253. }
  254. std::size_t SurfaceParams::GetInnerMemorySize(bool as_host_size, bool layer_only,
  255. bool uncompressed) const {
  256. return GetLayerSize(as_host_size, uncompressed) * (layer_only ? 1U : depth);
  257. }
  258. std::size_t SurfaceParams::Hash() const {
  259. return static_cast<std::size_t>(
  260. Common::CityHash64(reinterpret_cast<const char*>(this), sizeof(*this)));
  261. }
  262. bool SurfaceParams::operator==(const SurfaceParams& rhs) const {
  263. return std::tie(is_tiled, block_width, block_height, block_depth, tile_width_spacing, width,
  264. height, depth, pitch, unaligned_height, num_levels, pixel_format,
  265. component_type, type, target) ==
  266. std::tie(rhs.is_tiled, rhs.block_width, rhs.block_height, rhs.block_depth,
  267. rhs.tile_width_spacing, rhs.width, rhs.height, rhs.depth, rhs.pitch,
  268. rhs.unaligned_height, rhs.num_levels, rhs.pixel_format, rhs.component_type,
  269. rhs.type, rhs.target);
  270. }
  271. std::string SurfaceParams::TargetName() const {
  272. switch (target) {
  273. case SurfaceTarget::Texture1D:
  274. return "1D";
  275. case SurfaceTarget::Texture2D:
  276. return "2D";
  277. case SurfaceTarget::Texture3D:
  278. return "3D";
  279. case SurfaceTarget::Texture1DArray:
  280. return "1DArray";
  281. case SurfaceTarget::Texture2DArray:
  282. return "2DArray";
  283. case SurfaceTarget::TextureCubemap:
  284. return "Cube";
  285. case SurfaceTarget::TextureCubeArray:
  286. return "CubeArray";
  287. default:
  288. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
  289. UNREACHABLE();
  290. return fmt::format("TUK({})", static_cast<u32>(target));
  291. }
  292. }
  293. } // namespace VideoCommon