surface_params.cpp 13 KB

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