surface_params.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <string>
  6. #include <tuple>
  7. #include "common/alignment.h"
  8. #include "common/bit_util.h"
  9. #include "core/core.h"
  10. #include "video_core/engines/shader_bytecode.h"
  11. #include "video_core/surface.h"
  12. #include "video_core/texture_cache/format_lookup_table.h"
  13. #include "video_core/texture_cache/surface_params.h"
  14. namespace VideoCommon {
  15. using VideoCore::Surface::PixelFormat;
  16. using VideoCore::Surface::PixelFormatFromDepthFormat;
  17. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  18. using VideoCore::Surface::SurfaceTarget;
  19. using VideoCore::Surface::SurfaceTargetFromTextureType;
  20. using VideoCore::Surface::SurfaceType;
  21. namespace {
  22. SurfaceTarget TextureTypeToSurfaceTarget(Tegra::Shader::TextureType type, bool is_array) {
  23. switch (type) {
  24. case Tegra::Shader::TextureType::Texture1D:
  25. return is_array ? SurfaceTarget::Texture1DArray : SurfaceTarget::Texture1D;
  26. case Tegra::Shader::TextureType::Texture2D:
  27. return is_array ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D;
  28. case Tegra::Shader::TextureType::Texture3D:
  29. ASSERT(!is_array);
  30. return SurfaceTarget::Texture3D;
  31. case Tegra::Shader::TextureType::TextureCube:
  32. return is_array ? SurfaceTarget::TextureCubeArray : SurfaceTarget::TextureCubemap;
  33. default:
  34. UNREACHABLE();
  35. return SurfaceTarget::Texture2D;
  36. }
  37. }
  38. SurfaceTarget ImageTypeToSurfaceTarget(Tegra::Shader::ImageType type) {
  39. switch (type) {
  40. case Tegra::Shader::ImageType::Texture1D:
  41. return SurfaceTarget::Texture1D;
  42. case Tegra::Shader::ImageType::TextureBuffer:
  43. return SurfaceTarget::TextureBuffer;
  44. case Tegra::Shader::ImageType::Texture1DArray:
  45. return SurfaceTarget::Texture1DArray;
  46. case Tegra::Shader::ImageType::Texture2D:
  47. return SurfaceTarget::Texture2D;
  48. case Tegra::Shader::ImageType::Texture2DArray:
  49. return SurfaceTarget::Texture2DArray;
  50. case Tegra::Shader::ImageType::Texture3D:
  51. return SurfaceTarget::Texture3D;
  52. default:
  53. UNREACHABLE();
  54. return SurfaceTarget::Texture2D;
  55. }
  56. }
  57. constexpr u32 GetMipmapSize(bool uncompressed, u32 mip_size, u32 tile) {
  58. return uncompressed ? mip_size : std::max(1U, (mip_size + tile - 1) / tile);
  59. }
  60. } // Anonymous namespace
  61. SurfaceParams SurfaceParams::CreateForTexture(const FormatLookupTable& lookup_table,
  62. const Tegra::Texture::TICEntry& tic,
  63. const VideoCommon::Shader::Sampler& entry) {
  64. SurfaceParams params;
  65. params.is_tiled = tic.IsTiled();
  66. params.srgb_conversion = tic.IsSrgbConversionEnabled();
  67. params.block_width = params.is_tiled ? tic.BlockWidth() : 0;
  68. params.block_height = params.is_tiled ? tic.BlockHeight() : 0;
  69. params.block_depth = params.is_tiled ? tic.BlockDepth() : 0;
  70. params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1;
  71. params.pixel_format = lookup_table.GetPixelFormat(
  72. tic.format, params.srgb_conversion, tic.r_type, tic.g_type, tic.b_type, tic.a_type);
  73. params.type = GetFormatType(params.pixel_format);
  74. if (entry.is_shadow && params.type == SurfaceType::ColorTexture) {
  75. switch (params.pixel_format) {
  76. case PixelFormat::R16_UNORM:
  77. case PixelFormat::R16_FLOAT:
  78. params.pixel_format = PixelFormat::D16_UNORM;
  79. break;
  80. case PixelFormat::R32_FLOAT:
  81. params.pixel_format = PixelFormat::D32_FLOAT;
  82. break;
  83. default:
  84. UNIMPLEMENTED_MSG("Unimplemented shadow convert format: {}",
  85. static_cast<u32>(params.pixel_format));
  86. }
  87. params.type = GetFormatType(params.pixel_format);
  88. }
  89. // TODO: on 1DBuffer we should use the tic info.
  90. if (tic.IsBuffer()) {
  91. params.target = SurfaceTarget::TextureBuffer;
  92. params.width = tic.Width();
  93. params.pitch = params.width * params.GetBytesPerPixel();
  94. params.height = 1;
  95. params.depth = 1;
  96. params.num_levels = 1;
  97. params.emulated_levels = 1;
  98. params.is_layered = false;
  99. } else {
  100. params.target = TextureTypeToSurfaceTarget(entry.type, entry.is_array);
  101. params.width = tic.Width();
  102. params.height = tic.Height();
  103. params.depth = tic.Depth();
  104. params.pitch = params.is_tiled ? 0 : tic.Pitch();
  105. if (params.target == SurfaceTarget::TextureCubemap ||
  106. params.target == SurfaceTarget::TextureCubeArray) {
  107. params.depth *= 6;
  108. }
  109. params.num_levels = tic.max_mip_level + 1;
  110. params.emulated_levels = std::min(params.num_levels, params.MaxPossibleMipmap());
  111. params.is_layered = params.IsLayered();
  112. }
  113. return params;
  114. }
  115. SurfaceParams SurfaceParams::CreateForImage(const FormatLookupTable& lookup_table,
  116. const Tegra::Texture::TICEntry& tic,
  117. const VideoCommon::Shader::Image& entry) {
  118. SurfaceParams params;
  119. params.is_tiled = tic.IsTiled();
  120. params.srgb_conversion = tic.IsSrgbConversionEnabled();
  121. params.block_width = params.is_tiled ? tic.BlockWidth() : 0;
  122. params.block_height = params.is_tiled ? tic.BlockHeight() : 0;
  123. params.block_depth = params.is_tiled ? tic.BlockDepth() : 0;
  124. params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1;
  125. params.pixel_format = lookup_table.GetPixelFormat(
  126. tic.format, params.srgb_conversion, tic.r_type, tic.g_type, tic.b_type, tic.a_type);
  127. params.type = GetFormatType(params.pixel_format);
  128. params.target = ImageTypeToSurfaceTarget(entry.type);
  129. // TODO: on 1DBuffer we should use the tic info.
  130. if (tic.IsBuffer()) {
  131. params.target = SurfaceTarget::TextureBuffer;
  132. params.width = tic.Width();
  133. params.pitch = params.width * params.GetBytesPerPixel();
  134. params.height = 1;
  135. params.depth = 1;
  136. params.num_levels = 1;
  137. params.emulated_levels = 1;
  138. params.is_layered = false;
  139. } else {
  140. params.width = tic.Width();
  141. params.height = tic.Height();
  142. params.depth = tic.Depth();
  143. params.pitch = params.is_tiled ? 0 : tic.Pitch();
  144. if (params.target == SurfaceTarget::TextureCubemap ||
  145. params.target == SurfaceTarget::TextureCubeArray) {
  146. params.depth *= 6;
  147. }
  148. params.num_levels = tic.max_mip_level + 1;
  149. params.emulated_levels = std::min(params.num_levels, params.MaxPossibleMipmap());
  150. params.is_layered = params.IsLayered();
  151. }
  152. return params;
  153. }
  154. SurfaceParams SurfaceParams::CreateForDepthBuffer(Tegra::Engines::Maxwell3D& maxwell3d) {
  155. const auto& regs = maxwell3d.regs;
  156. const auto block_depth = std::min(regs.zeta.memory_layout.block_depth.Value(), 5U);
  157. const bool is_layered = regs.zeta_layers > 1 && block_depth == 0;
  158. const auto pixel_format = PixelFormatFromDepthFormat(regs.zeta.format);
  159. return {
  160. .is_tiled = regs.zeta.memory_layout.type ==
  161. Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear,
  162. .srgb_conversion = false,
  163. .is_layered = is_layered,
  164. .block_width = std::min(regs.zeta.memory_layout.block_width.Value(), 5U),
  165. .block_height = std::min(regs.zeta.memory_layout.block_height.Value(), 5U),
  166. .block_depth = block_depth,
  167. .tile_width_spacing = 1,
  168. .width = regs.zeta_width,
  169. .height = regs.zeta_height,
  170. .depth = is_layered ? regs.zeta_layers.Value() : 1U,
  171. .pitch = 0,
  172. .num_levels = 1,
  173. .emulated_levels = 1,
  174. .pixel_format = pixel_format,
  175. .type = GetFormatType(pixel_format),
  176. .target = is_layered ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D,
  177. };
  178. }
  179. SurfaceParams SurfaceParams::CreateForFramebuffer(Tegra::Engines::Maxwell3D& maxwell3d,
  180. std::size_t index) {
  181. const auto& config{maxwell3d.regs.rt[index]};
  182. SurfaceParams params;
  183. params.is_tiled =
  184. config.memory_layout.type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
  185. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::B8G8R8A8_SRGB ||
  186. config.format == Tegra::RenderTargetFormat::A8B8G8R8_SRGB;
  187. params.block_width = config.memory_layout.block_width;
  188. params.block_height = config.memory_layout.block_height;
  189. params.block_depth = config.memory_layout.block_depth;
  190. params.tile_width_spacing = 1;
  191. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  192. params.type = GetFormatType(params.pixel_format);
  193. if (params.is_tiled) {
  194. params.pitch = 0;
  195. params.width = config.width;
  196. } else {
  197. const u32 bpp = GetFormatBpp(params.pixel_format) / CHAR_BIT;
  198. params.pitch = config.width;
  199. params.width = params.pitch / bpp;
  200. }
  201. params.height = config.height;
  202. params.num_levels = 1;
  203. params.emulated_levels = 1;
  204. if (config.memory_layout.is_3d != 0) {
  205. params.depth = config.layers.Value();
  206. params.is_layered = false;
  207. params.target = SurfaceTarget::Texture3D;
  208. } else if (config.layers > 1) {
  209. params.depth = config.layers.Value();
  210. params.is_layered = true;
  211. params.target = SurfaceTarget::Texture2DArray;
  212. } else {
  213. params.depth = 1;
  214. params.is_layered = false;
  215. params.target = SurfaceTarget::Texture2D;
  216. }
  217. return params;
  218. }
  219. SurfaceParams SurfaceParams::CreateForFermiCopySurface(
  220. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  221. const bool is_tiled = !config.linear;
  222. const auto pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  223. SurfaceParams params{
  224. .is_tiled = is_tiled,
  225. .srgb_conversion = config.format == Tegra::RenderTargetFormat::B8G8R8A8_SRGB ||
  226. config.format == Tegra::RenderTargetFormat::A8B8G8R8_SRGB,
  227. .is_layered = false,
  228. .block_width = is_tiled ? std::min(config.BlockWidth(), 5U) : 0U,
  229. .block_height = is_tiled ? std::min(config.BlockHeight(), 5U) : 0U,
  230. .block_depth = is_tiled ? std::min(config.BlockDepth(), 5U) : 0U,
  231. .tile_width_spacing = 1,
  232. .width = config.width,
  233. .height = config.height,
  234. .depth = 1,
  235. .pitch = config.pitch,
  236. .num_levels = 1,
  237. .emulated_levels = 1,
  238. .pixel_format = pixel_format,
  239. .type = GetFormatType(pixel_format),
  240. // TODO(Rodrigo): Try to guess texture arrays from parameters
  241. .target = SurfaceTarget::Texture2D,
  242. };
  243. params.is_layered = params.IsLayered();
  244. return params;
  245. }
  246. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  247. const VideoCommon::Shader::Sampler& entry) {
  248. return TextureTypeToSurfaceTarget(entry.type, entry.is_array);
  249. }
  250. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  251. const VideoCommon::Shader::Image& entry) {
  252. return ImageTypeToSurfaceTarget(entry.type);
  253. }
  254. bool SurfaceParams::IsLayered() const {
  255. switch (target) {
  256. case SurfaceTarget::Texture1DArray:
  257. case SurfaceTarget::Texture2DArray:
  258. case SurfaceTarget::TextureCubemap:
  259. case SurfaceTarget::TextureCubeArray:
  260. return true;
  261. default:
  262. return false;
  263. }
  264. }
  265. // Auto block resizing algorithm from:
  266. // https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
  267. u32 SurfaceParams::GetMipBlockHeight(u32 level) const {
  268. if (level == 0) {
  269. return this->block_height;
  270. }
  271. const u32 height_new{GetMipHeight(level)};
  272. const u32 default_block_height{GetDefaultBlockHeight()};
  273. const u32 blocks_in_y{(height_new + default_block_height - 1) / default_block_height};
  274. const u32 block_height_new = Common::Log2Ceil32(blocks_in_y);
  275. return std::clamp(block_height_new, 3U, 7U) - 3U;
  276. }
  277. u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
  278. if (level == 0) {
  279. return this->block_depth;
  280. }
  281. if (is_layered) {
  282. return 0;
  283. }
  284. const u32 depth_new{GetMipDepth(level)};
  285. const u32 block_depth_new = Common::Log2Ceil32(depth_new);
  286. if (block_depth_new > 4) {
  287. return 5 - (GetMipBlockHeight(level) >= 2);
  288. }
  289. return block_depth_new;
  290. }
  291. std::size_t SurfaceParams::GetGuestMipmapLevelOffset(u32 level) const {
  292. std::size_t offset = 0;
  293. for (u32 i = 0; i < level; i++) {
  294. offset += GetInnerMipmapMemorySize(i, false, false);
  295. }
  296. return offset;
  297. }
  298. std::size_t SurfaceParams::GetHostMipmapLevelOffset(u32 level, bool is_converted) const {
  299. std::size_t offset = 0;
  300. if (is_converted) {
  301. for (u32 i = 0; i < level; ++i) {
  302. offset += GetConvertedMipmapSize(i) * GetNumLayers();
  303. }
  304. } else {
  305. for (u32 i = 0; i < level; ++i) {
  306. offset += GetInnerMipmapMemorySize(i, true, false) * GetNumLayers();
  307. }
  308. }
  309. return offset;
  310. }
  311. std::size_t SurfaceParams::GetConvertedMipmapSize(u32 level) const {
  312. constexpr std::size_t rgba8_bpp = 4ULL;
  313. const std::size_t mip_width = GetMipWidth(level);
  314. const std::size_t mip_height = GetMipHeight(level);
  315. const std::size_t mip_depth = is_layered ? 1 : GetMipDepth(level);
  316. return mip_width * mip_height * mip_depth * rgba8_bpp;
  317. }
  318. std::size_t SurfaceParams::GetLayerSize(bool as_host_size, bool uncompressed) const {
  319. std::size_t size = 0;
  320. for (u32 level = 0; level < num_levels; ++level) {
  321. size += GetInnerMipmapMemorySize(level, as_host_size, uncompressed);
  322. }
  323. if (is_tiled && is_layered) {
  324. return Common::AlignBits(size, Tegra::Texture::GOB_SIZE_SHIFT + block_height + block_depth);
  325. }
  326. return size;
  327. }
  328. std::size_t SurfaceParams::GetInnerMipmapMemorySize(u32 level, bool as_host_size,
  329. bool uncompressed) const {
  330. const u32 mip_width{GetMipmapSize(uncompressed, GetMipWidth(level), GetDefaultBlockWidth())};
  331. const u32 mip_height{GetMipmapSize(uncompressed, GetMipHeight(level), GetDefaultBlockHeight())};
  332. const u32 mip_depth{is_layered ? 1U : GetMipDepth(level)};
  333. if (is_tiled) {
  334. return Tegra::Texture::CalculateSize(!as_host_size, GetBytesPerPixel(), mip_width,
  335. mip_height, mip_depth, GetMipBlockHeight(level),
  336. GetMipBlockDepth(level));
  337. } else if (as_host_size || IsBuffer()) {
  338. return GetBytesPerPixel() * mip_width * mip_height * mip_depth;
  339. } else {
  340. // Linear Texture Case
  341. return pitch * mip_height * mip_depth;
  342. }
  343. }
  344. bool SurfaceParams::operator==(const SurfaceParams& rhs) const {
  345. return std::tie(is_tiled, block_width, block_height, block_depth, tile_width_spacing, width,
  346. height, depth, pitch, num_levels, pixel_format, type, target) ==
  347. std::tie(rhs.is_tiled, rhs.block_width, rhs.block_height, rhs.block_depth,
  348. rhs.tile_width_spacing, rhs.width, rhs.height, rhs.depth, rhs.pitch,
  349. rhs.num_levels, rhs.pixel_format, rhs.type, rhs.target);
  350. }
  351. std::string SurfaceParams::TargetName() const {
  352. switch (target) {
  353. case SurfaceTarget::Texture1D:
  354. return "1D";
  355. case SurfaceTarget::TextureBuffer:
  356. return "TexBuffer";
  357. case SurfaceTarget::Texture2D:
  358. return "2D";
  359. case SurfaceTarget::Texture3D:
  360. return "3D";
  361. case SurfaceTarget::Texture1DArray:
  362. return "1DArray";
  363. case SurfaceTarget::Texture2DArray:
  364. return "2DArray";
  365. case SurfaceTarget::TextureCubemap:
  366. return "Cube";
  367. case SurfaceTarget::TextureCubeArray:
  368. return "CubeArray";
  369. default:
  370. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
  371. UNREACHABLE();
  372. return fmt::format("TUK({})", target);
  373. }
  374. }
  375. u32 SurfaceParams::GetBlockSize() const {
  376. const u32 x = 64U << block_width;
  377. const u32 y = 8U << block_height;
  378. const u32 z = 1U << block_depth;
  379. return x * y * z;
  380. }
  381. std::pair<u32, u32> SurfaceParams::GetBlockXY() const {
  382. const u32 x_pixels = 64U / GetBytesPerPixel();
  383. const u32 x = x_pixels << block_width;
  384. const u32 y = 8U << block_height;
  385. return {x, y};
  386. }
  387. std::tuple<u32, u32, u32> SurfaceParams::GetBlockOffsetXYZ(u32 offset) const {
  388. const auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
  389. const u32 block_size = GetBlockSize();
  390. const u32 block_index = offset / block_size;
  391. const u32 gob_offset = offset % block_size;
  392. const u32 gob_index = gob_offset / static_cast<u32>(Tegra::Texture::GOB_SIZE);
  393. const u32 x_gob_pixels = 64U / GetBytesPerPixel();
  394. const u32 x_block_pixels = x_gob_pixels << block_width;
  395. const u32 y_block_pixels = 8U << block_height;
  396. const u32 z_block_pixels = 1U << block_depth;
  397. const u32 x_blocks = div_ceil(width, x_block_pixels);
  398. const u32 y_blocks = div_ceil(height, y_block_pixels);
  399. const u32 z_blocks = div_ceil(depth, z_block_pixels);
  400. const u32 base_x = block_index % x_blocks;
  401. const u32 base_y = (block_index / x_blocks) % y_blocks;
  402. const u32 base_z = (block_index / (x_blocks * y_blocks)) % z_blocks;
  403. u32 x = base_x * x_block_pixels;
  404. u32 y = base_y * y_block_pixels;
  405. u32 z = base_z * z_block_pixels;
  406. z += gob_index >> block_height;
  407. y += (gob_index * 8U) % y_block_pixels;
  408. return {x, y, z};
  409. }
  410. } // namespace VideoCommon