surface_params.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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::R16U:
  77. case PixelFormat::R16F:
  78. params.pixel_format = PixelFormat::Z16;
  79. break;
  80. case PixelFormat::R32F:
  81. params.pixel_format = PixelFormat::Z32F;
  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. params.type = GetFormatType(params.pixel_format);
  90. // TODO: on 1DBuffer we should use the tic info.
  91. if (tic.IsBuffer()) {
  92. params.target = SurfaceTarget::TextureBuffer;
  93. params.width = tic.Width();
  94. params.pitch = params.width * params.GetBytesPerPixel();
  95. params.height = 1;
  96. params.depth = 1;
  97. params.num_levels = 1;
  98. params.emulated_levels = 1;
  99. params.is_layered = false;
  100. } else {
  101. params.target = TextureTypeToSurfaceTarget(entry.type, entry.is_array);
  102. params.width = tic.Width();
  103. params.height = tic.Height();
  104. params.depth = tic.Depth();
  105. params.pitch = params.is_tiled ? 0 : tic.Pitch();
  106. if (params.target == SurfaceTarget::TextureCubemap ||
  107. params.target == SurfaceTarget::TextureCubeArray) {
  108. params.depth *= 6;
  109. }
  110. params.num_levels = tic.max_mip_level + 1;
  111. params.emulated_levels = std::min(params.num_levels, params.MaxPossibleMipmap());
  112. params.is_layered = params.IsLayered();
  113. }
  114. return params;
  115. }
  116. SurfaceParams SurfaceParams::CreateForImage(const FormatLookupTable& lookup_table,
  117. const Tegra::Texture::TICEntry& tic,
  118. const VideoCommon::Shader::Image& entry) {
  119. SurfaceParams params;
  120. params.is_tiled = tic.IsTiled();
  121. params.srgb_conversion = tic.IsSrgbConversionEnabled();
  122. params.block_width = params.is_tiled ? tic.BlockWidth() : 0,
  123. params.block_height = params.is_tiled ? tic.BlockHeight() : 0,
  124. params.block_depth = params.is_tiled ? tic.BlockDepth() : 0,
  125. params.tile_width_spacing = params.is_tiled ? (1 << tic.tile_width_spacing.Value()) : 1;
  126. params.pixel_format = lookup_table.GetPixelFormat(
  127. tic.format, params.srgb_conversion, tic.r_type, tic.g_type, tic.b_type, tic.a_type);
  128. params.type = GetFormatType(params.pixel_format);
  129. params.type = GetFormatType(params.pixel_format);
  130. params.target = ImageTypeToSurfaceTarget(entry.type);
  131. // TODO: on 1DBuffer we should use the tic info.
  132. if (tic.IsBuffer()) {
  133. params.target = SurfaceTarget::TextureBuffer;
  134. params.width = tic.Width();
  135. params.pitch = params.width * params.GetBytesPerPixel();
  136. params.height = 1;
  137. params.depth = 1;
  138. params.num_levels = 1;
  139. params.emulated_levels = 1;
  140. params.is_layered = false;
  141. } else {
  142. params.width = tic.Width();
  143. params.height = tic.Height();
  144. params.depth = tic.Depth();
  145. params.pitch = params.is_tiled ? 0 : tic.Pitch();
  146. if (params.target == SurfaceTarget::TextureCubemap ||
  147. params.target == SurfaceTarget::TextureCubeArray) {
  148. params.depth *= 6;
  149. }
  150. params.num_levels = tic.max_mip_level + 1;
  151. params.emulated_levels = std::min(params.num_levels, params.MaxPossibleMipmap());
  152. params.is_layered = params.IsLayered();
  153. }
  154. return params;
  155. }
  156. SurfaceParams SurfaceParams::CreateForDepthBuffer(Core::System& system) {
  157. const auto& regs = system.GPU().Maxwell3D().regs;
  158. SurfaceParams params;
  159. params.is_tiled = regs.zeta.memory_layout.type ==
  160. Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
  161. params.srgb_conversion = false;
  162. params.block_width = std::min(regs.zeta.memory_layout.block_width.Value(), 5U);
  163. params.block_height = std::min(regs.zeta.memory_layout.block_height.Value(), 5U);
  164. params.block_depth = std::min(regs.zeta.memory_layout.block_depth.Value(), 5U);
  165. params.tile_width_spacing = 1;
  166. params.pixel_format = PixelFormatFromDepthFormat(regs.zeta.format);
  167. params.type = GetFormatType(params.pixel_format);
  168. params.width = regs.zeta_width;
  169. params.height = regs.zeta_height;
  170. params.pitch = 0;
  171. params.num_levels = 1;
  172. params.emulated_levels = 1;
  173. const bool is_layered = regs.zeta_layers > 1 && params.block_depth == 0;
  174. params.is_layered = is_layered;
  175. params.target = is_layered ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D;
  176. params.depth = is_layered ? regs.zeta_layers.Value() : 1U;
  177. return params;
  178. }
  179. SurfaceParams SurfaceParams::CreateForFramebuffer(Core::System& system, std::size_t index) {
  180. const auto& config{system.GPU().Maxwell3D().regs.rt[index]};
  181. SurfaceParams params;
  182. params.is_tiled =
  183. config.memory_layout.type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
  184. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
  185. config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
  186. params.block_width = config.memory_layout.block_width;
  187. params.block_height = config.memory_layout.block_height;
  188. params.block_depth = config.memory_layout.block_depth;
  189. params.tile_width_spacing = 1;
  190. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  191. params.type = GetFormatType(params.pixel_format);
  192. if (params.is_tiled) {
  193. params.pitch = 0;
  194. params.width = config.width;
  195. } else {
  196. const u32 bpp = GetFormatBpp(params.pixel_format) / CHAR_BIT;
  197. params.pitch = config.width;
  198. params.width = params.pitch / bpp;
  199. }
  200. params.height = config.height;
  201. params.num_levels = 1;
  202. params.emulated_levels = 1;
  203. if (config.memory_layout.is_3d != 0) {
  204. params.depth = config.layers.Value();
  205. params.is_layered = false;
  206. params.target = SurfaceTarget::Texture3D;
  207. } else if (config.layers > 1) {
  208. params.depth = config.layers.Value();
  209. params.is_layered = true;
  210. params.target = SurfaceTarget::Texture2DArray;
  211. } else {
  212. params.depth = 1;
  213. params.is_layered = false;
  214. params.target = SurfaceTarget::Texture2D;
  215. }
  216. return params;
  217. }
  218. SurfaceParams SurfaceParams::CreateForFermiCopySurface(
  219. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  220. SurfaceParams params{};
  221. params.is_tiled = !config.linear;
  222. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
  223. config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
  224. params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 5U) : 0,
  225. params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 5U) : 0,
  226. params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 5U) : 0,
  227. params.tile_width_spacing = 1;
  228. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  229. params.type = GetFormatType(params.pixel_format);
  230. params.width = config.width;
  231. params.height = config.height;
  232. params.pitch = config.pitch;
  233. // TODO(Rodrigo): Try to guess texture arrays from parameters
  234. params.target = SurfaceTarget::Texture2D;
  235. params.depth = 1;
  236. params.num_levels = 1;
  237. params.emulated_levels = 1;
  238. params.is_layered = params.IsLayered();
  239. return params;
  240. }
  241. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  242. const VideoCommon::Shader::Sampler& entry) {
  243. return TextureTypeToSurfaceTarget(entry.type, entry.is_array);
  244. }
  245. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  246. const VideoCommon::Shader::Image& entry) {
  247. return ImageTypeToSurfaceTarget(entry.type);
  248. }
  249. bool SurfaceParams::IsLayered() const {
  250. switch (target) {
  251. case SurfaceTarget::Texture1DArray:
  252. case SurfaceTarget::Texture2DArray:
  253. case SurfaceTarget::TextureCubemap:
  254. case SurfaceTarget::TextureCubeArray:
  255. return true;
  256. default:
  257. return false;
  258. }
  259. }
  260. // Auto block resizing algorithm from:
  261. // https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
  262. u32 SurfaceParams::GetMipBlockHeight(u32 level) const {
  263. if (level == 0) {
  264. return this->block_height;
  265. }
  266. const u32 height_new{GetMipHeight(level)};
  267. const u32 default_block_height{GetDefaultBlockHeight()};
  268. const u32 blocks_in_y{(height_new + default_block_height - 1) / default_block_height};
  269. const u32 block_height_new = Common::Log2Ceil32(blocks_in_y);
  270. return std::clamp(block_height_new, 3U, 7U) - 3U;
  271. }
  272. u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
  273. if (level == 0) {
  274. return this->block_depth;
  275. }
  276. if (is_layered) {
  277. return 0;
  278. }
  279. const u32 depth_new{GetMipDepth(level)};
  280. const u32 block_depth_new = Common::Log2Ceil32(depth_new);
  281. if (block_depth_new > 4) {
  282. return 5 - (GetMipBlockHeight(level) >= 2);
  283. }
  284. return block_depth_new;
  285. }
  286. std::size_t SurfaceParams::GetGuestMipmapLevelOffset(u32 level) const {
  287. std::size_t offset = 0;
  288. for (u32 i = 0; i < level; i++) {
  289. offset += GetInnerMipmapMemorySize(i, false, false);
  290. }
  291. return offset;
  292. }
  293. std::size_t SurfaceParams::GetHostMipmapLevelOffset(u32 level, bool is_converted) const {
  294. std::size_t offset = 0;
  295. if (is_converted) {
  296. for (u32 i = 0; i < level; ++i) {
  297. offset += GetConvertedMipmapSize(i) * GetNumLayers();
  298. }
  299. } else {
  300. for (u32 i = 0; i < level; ++i) {
  301. offset += GetInnerMipmapMemorySize(i, true, false) * GetNumLayers();
  302. }
  303. }
  304. return offset;
  305. }
  306. std::size_t SurfaceParams::GetConvertedMipmapSize(u32 level) const {
  307. constexpr std::size_t rgba8_bpp = 4ULL;
  308. const std::size_t mip_width = GetMipWidth(level);
  309. const std::size_t mip_height = GetMipHeight(level);
  310. const std::size_t mip_depth = is_layered ? 1 : GetMipDepth(level);
  311. return mip_width * mip_height * mip_depth * rgba8_bpp;
  312. }
  313. std::size_t SurfaceParams::GetLayerSize(bool as_host_size, bool uncompressed) const {
  314. std::size_t size = 0;
  315. for (u32 level = 0; level < num_levels; ++level) {
  316. size += GetInnerMipmapMemorySize(level, as_host_size, uncompressed);
  317. }
  318. if (is_tiled && is_layered) {
  319. return Common::AlignBits(size, Tegra::Texture::GOB_SIZE_SHIFT + block_height + block_depth);
  320. }
  321. return size;
  322. }
  323. std::size_t SurfaceParams::GetInnerMipmapMemorySize(u32 level, bool as_host_size,
  324. bool uncompressed) const {
  325. const u32 width{GetMipmapSize(uncompressed, GetMipWidth(level), GetDefaultBlockWidth())};
  326. const u32 height{GetMipmapSize(uncompressed, GetMipHeight(level), GetDefaultBlockHeight())};
  327. const u32 depth{is_layered ? 1U : GetMipDepth(level)};
  328. if (is_tiled) {
  329. return Tegra::Texture::CalculateSize(!as_host_size, GetBytesPerPixel(), width, height,
  330. depth, GetMipBlockHeight(level),
  331. GetMipBlockDepth(level));
  332. } else if (as_host_size || IsBuffer()) {
  333. return GetBytesPerPixel() * width * height * depth;
  334. } else {
  335. // Linear Texture Case
  336. return pitch * height * depth;
  337. }
  338. }
  339. bool SurfaceParams::operator==(const SurfaceParams& rhs) const {
  340. return std::tie(is_tiled, block_width, block_height, block_depth, tile_width_spacing, width,
  341. height, depth, pitch, num_levels, pixel_format, type, target) ==
  342. std::tie(rhs.is_tiled, rhs.block_width, rhs.block_height, rhs.block_depth,
  343. rhs.tile_width_spacing, rhs.width, rhs.height, rhs.depth, rhs.pitch,
  344. rhs.num_levels, rhs.pixel_format, rhs.type, rhs.target);
  345. }
  346. std::string SurfaceParams::TargetName() const {
  347. switch (target) {
  348. case SurfaceTarget::Texture1D:
  349. return "1D";
  350. case SurfaceTarget::TextureBuffer:
  351. return "TexBuffer";
  352. case SurfaceTarget::Texture2D:
  353. return "2D";
  354. case SurfaceTarget::Texture3D:
  355. return "3D";
  356. case SurfaceTarget::Texture1DArray:
  357. return "1DArray";
  358. case SurfaceTarget::Texture2DArray:
  359. return "2DArray";
  360. case SurfaceTarget::TextureCubemap:
  361. return "Cube";
  362. case SurfaceTarget::TextureCubeArray:
  363. return "CubeArray";
  364. default:
  365. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
  366. UNREACHABLE();
  367. return fmt::format("TUK({})", static_cast<u32>(target));
  368. }
  369. }
  370. u32 SurfaceParams::GetBlockSize() const {
  371. const u32 x = 64U << block_width;
  372. const u32 y = 8U << block_height;
  373. const u32 z = 1U << block_depth;
  374. return x * y * z;
  375. }
  376. std::pair<u32, u32> SurfaceParams::GetBlockXY() const {
  377. const u32 x_pixels = 64U / GetBytesPerPixel();
  378. const u32 x = x_pixels << block_width;
  379. const u32 y = 8U << block_height;
  380. return {x, y};
  381. }
  382. std::tuple<u32, u32, u32> SurfaceParams::GetBlockOffsetXYZ(u32 offset) const {
  383. const auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
  384. const u32 block_size = GetBlockSize();
  385. const u32 block_index = offset / block_size;
  386. const u32 gob_offset = offset % block_size;
  387. const u32 gob_index = gob_offset / static_cast<u32>(Tegra::Texture::GOB_SIZE);
  388. const u32 x_gob_pixels = 64U / GetBytesPerPixel();
  389. const u32 x_block_pixels = x_gob_pixels << block_width;
  390. const u32 y_block_pixels = 8U << block_height;
  391. const u32 z_block_pixels = 1U << block_depth;
  392. const u32 x_blocks = div_ceil(width, x_block_pixels);
  393. const u32 y_blocks = div_ceil(height, y_block_pixels);
  394. const u32 z_blocks = div_ceil(depth, z_block_pixels);
  395. const u32 base_x = block_index % x_blocks;
  396. const u32 base_y = (block_index / x_blocks) % y_blocks;
  397. const u32 base_z = (block_index / (x_blocks * y_blocks)) % z_blocks;
  398. u32 x = base_x * x_block_pixels;
  399. u32 y = base_y * y_block_pixels;
  400. u32 z = base_z * z_block_pixels;
  401. z += gob_index >> block_height;
  402. y += (gob_index * 8U) % y_block_pixels;
  403. return {x, y, z};
  404. }
  405. } // namespace VideoCommon