surface_params.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. const bool is_layered = config.layers > 1 && params.block_depth == 0;
  204. params.is_layered = is_layered;
  205. params.depth = is_layered ? config.layers.Value() : 1;
  206. params.target = is_layered ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D;
  207. return params;
  208. }
  209. SurfaceParams SurfaceParams::CreateForFermiCopySurface(
  210. const Tegra::Engines::Fermi2D::Regs::Surface& config) {
  211. SurfaceParams params{};
  212. params.is_tiled = !config.linear;
  213. params.srgb_conversion = config.format == Tegra::RenderTargetFormat::BGRA8_SRGB ||
  214. config.format == Tegra::RenderTargetFormat::RGBA8_SRGB;
  215. params.block_width = params.is_tiled ? std::min(config.BlockWidth(), 5U) : 0,
  216. params.block_height = params.is_tiled ? std::min(config.BlockHeight(), 5U) : 0,
  217. params.block_depth = params.is_tiled ? std::min(config.BlockDepth(), 5U) : 0,
  218. params.tile_width_spacing = 1;
  219. params.pixel_format = PixelFormatFromRenderTargetFormat(config.format);
  220. params.type = GetFormatType(params.pixel_format);
  221. params.width = config.width;
  222. params.height = config.height;
  223. params.pitch = config.pitch;
  224. // TODO(Rodrigo): Try to guess the surface target from depth and layer parameters
  225. params.target = SurfaceTarget::Texture2D;
  226. params.depth = 1;
  227. params.num_levels = 1;
  228. params.emulated_levels = 1;
  229. params.is_layered = params.IsLayered();
  230. return params;
  231. }
  232. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  233. const VideoCommon::Shader::Sampler& entry) {
  234. return TextureTypeToSurfaceTarget(entry.type, entry.is_array);
  235. }
  236. VideoCore::Surface::SurfaceTarget SurfaceParams::ExpectedTarget(
  237. const VideoCommon::Shader::Image& entry) {
  238. return ImageTypeToSurfaceTarget(entry.type);
  239. }
  240. bool SurfaceParams::IsLayered() const {
  241. switch (target) {
  242. case SurfaceTarget::Texture1DArray:
  243. case SurfaceTarget::Texture2DArray:
  244. case SurfaceTarget::TextureCubemap:
  245. case SurfaceTarget::TextureCubeArray:
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. // Auto block resizing algorithm from:
  252. // https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
  253. u32 SurfaceParams::GetMipBlockHeight(u32 level) const {
  254. if (level == 0) {
  255. return this->block_height;
  256. }
  257. const u32 height_new{GetMipHeight(level)};
  258. const u32 default_block_height{GetDefaultBlockHeight()};
  259. const u32 blocks_in_y{(height_new + default_block_height - 1) / default_block_height};
  260. const u32 block_height_new = Common::Log2Ceil32(blocks_in_y);
  261. return std::clamp(block_height_new, 3U, 7U) - 3U;
  262. }
  263. u32 SurfaceParams::GetMipBlockDepth(u32 level) const {
  264. if (level == 0) {
  265. return this->block_depth;
  266. }
  267. if (is_layered) {
  268. return 0;
  269. }
  270. const u32 depth_new{GetMipDepth(level)};
  271. const u32 block_depth_new = Common::Log2Ceil32(depth_new);
  272. if (block_depth_new > 4) {
  273. return 5 - (GetMipBlockHeight(level) >= 2);
  274. }
  275. return block_depth_new;
  276. }
  277. std::size_t SurfaceParams::GetGuestMipmapLevelOffset(u32 level) const {
  278. std::size_t offset = 0;
  279. for (u32 i = 0; i < level; i++) {
  280. offset += GetInnerMipmapMemorySize(i, false, false);
  281. }
  282. return offset;
  283. }
  284. std::size_t SurfaceParams::GetHostMipmapLevelOffset(u32 level, bool is_converted) const {
  285. std::size_t offset = 0;
  286. if (is_converted) {
  287. for (u32 i = 0; i < level; ++i) {
  288. offset += GetConvertedMipmapSize(i) * GetNumLayers();
  289. }
  290. } else {
  291. for (u32 i = 0; i < level; ++i) {
  292. offset += GetInnerMipmapMemorySize(i, true, false) * GetNumLayers();
  293. }
  294. }
  295. return offset;
  296. }
  297. std::size_t SurfaceParams::GetConvertedMipmapSize(u32 level) const {
  298. constexpr std::size_t rgba8_bpp = 4ULL;
  299. const std::size_t mip_width = GetMipWidth(level);
  300. const std::size_t mip_height = GetMipHeight(level);
  301. const std::size_t mip_depth = is_layered ? 1 : GetMipDepth(level);
  302. return mip_width * mip_height * mip_depth * rgba8_bpp;
  303. }
  304. std::size_t SurfaceParams::GetLayerSize(bool as_host_size, bool uncompressed) const {
  305. std::size_t size = 0;
  306. for (u32 level = 0; level < num_levels; ++level) {
  307. size += GetInnerMipmapMemorySize(level, as_host_size, uncompressed);
  308. }
  309. if (is_tiled && is_layered) {
  310. return Common::AlignBits(size,
  311. Tegra::Texture::GetGOBSizeShift() + block_height + block_depth);
  312. }
  313. return size;
  314. }
  315. std::size_t SurfaceParams::GetInnerMipmapMemorySize(u32 level, bool as_host_size,
  316. bool uncompressed) const {
  317. const u32 width{GetMipmapSize(uncompressed, GetMipWidth(level), GetDefaultBlockWidth())};
  318. const u32 height{GetMipmapSize(uncompressed, GetMipHeight(level), GetDefaultBlockHeight())};
  319. const u32 depth{is_layered ? 1U : GetMipDepth(level)};
  320. if (is_tiled) {
  321. return Tegra::Texture::CalculateSize(!as_host_size, GetBytesPerPixel(), width, height,
  322. depth, GetMipBlockHeight(level),
  323. GetMipBlockDepth(level));
  324. } else if (as_host_size || IsBuffer()) {
  325. return GetBytesPerPixel() * width * height * depth;
  326. } else {
  327. // Linear Texture Case
  328. return pitch * height * depth;
  329. }
  330. }
  331. bool SurfaceParams::operator==(const SurfaceParams& rhs) const {
  332. return std::tie(is_tiled, block_width, block_height, block_depth, tile_width_spacing, width,
  333. height, depth, pitch, num_levels, pixel_format, type, target) ==
  334. std::tie(rhs.is_tiled, rhs.block_width, rhs.block_height, rhs.block_depth,
  335. rhs.tile_width_spacing, rhs.width, rhs.height, rhs.depth, rhs.pitch,
  336. rhs.num_levels, rhs.pixel_format, rhs.type, rhs.target);
  337. }
  338. std::string SurfaceParams::TargetName() const {
  339. switch (target) {
  340. case SurfaceTarget::Texture1D:
  341. return "1D";
  342. case SurfaceTarget::TextureBuffer:
  343. return "TexBuffer";
  344. case SurfaceTarget::Texture2D:
  345. return "2D";
  346. case SurfaceTarget::Texture3D:
  347. return "3D";
  348. case SurfaceTarget::Texture1DArray:
  349. return "1DArray";
  350. case SurfaceTarget::Texture2DArray:
  351. return "2DArray";
  352. case SurfaceTarget::TextureCubemap:
  353. return "Cube";
  354. case SurfaceTarget::TextureCubeArray:
  355. return "CubeArray";
  356. default:
  357. LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", static_cast<u32>(target));
  358. UNREACHABLE();
  359. return fmt::format("TUK({})", static_cast<u32>(target));
  360. }
  361. }
  362. u32 SurfaceParams::GetBlockSize() const {
  363. const u32 x = 64U << block_width;
  364. const u32 y = 8U << block_height;
  365. const u32 z = 1U << block_depth;
  366. return x * y * z;
  367. }
  368. std::pair<u32, u32> SurfaceParams::GetBlockXY() const {
  369. const u32 x_pixels = 64U / GetBytesPerPixel();
  370. const u32 x = x_pixels << block_width;
  371. const u32 y = 8U << block_height;
  372. return {x, y};
  373. }
  374. std::tuple<u32, u32, u32> SurfaceParams::GetBlockOffsetXYZ(u32 offset) const {
  375. const auto div_ceil = [](const u32 x, const u32 y) { return ((x + y - 1) / y); };
  376. const u32 block_size = GetBlockSize();
  377. const u32 block_index = offset / block_size;
  378. const u32 gob_offset = offset % block_size;
  379. const u32 gob_index = gob_offset / static_cast<u32>(Tegra::Texture::GetGOBSize());
  380. const u32 x_gob_pixels = 64U / GetBytesPerPixel();
  381. const u32 x_block_pixels = x_gob_pixels << block_width;
  382. const u32 y_block_pixels = 8U << block_height;
  383. const u32 z_block_pixels = 1U << block_depth;
  384. const u32 x_blocks = div_ceil(width, x_block_pixels);
  385. const u32 y_blocks = div_ceil(height, y_block_pixels);
  386. const u32 z_blocks = div_ceil(depth, z_block_pixels);
  387. const u32 base_x = block_index % x_blocks;
  388. const u32 base_y = (block_index / x_blocks) % y_blocks;
  389. const u32 base_z = (block_index / (x_blocks * y_blocks)) % z_blocks;
  390. u32 x = base_x * x_block_pixels;
  391. u32 y = base_y * y_block_pixels;
  392. u32 z = base_z * z_block_pixels;
  393. z += gob_index >> block_height;
  394. y += (gob_index * 8U) % y_block_pixels;
  395. return {x, y, z};
  396. }
  397. } // namespace VideoCommon