surface_base.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "common/common_types.h"
  6. #include "common/microprofile.h"
  7. #include "video_core/memory_manager.h"
  8. #include "video_core/texture_cache/surface_base.h"
  9. #include "video_core/texture_cache/surface_params.h"
  10. #include "video_core/textures/convert.h"
  11. namespace VideoCommon {
  12. MICROPROFILE_DEFINE(GPU_Load_Texture, "GPU", "Texture Load", MP_RGB(128, 192, 128));
  13. MICROPROFILE_DEFINE(GPU_Flush_Texture, "GPU", "Texture Flush", MP_RGB(128, 192, 128));
  14. using Tegra::Texture::ConvertFromGuestToHost;
  15. using VideoCore::MortonSwizzleMode;
  16. SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params)
  17. : params{params}, gpu_addr{gpu_addr}, layer_size{params.GetGuestLayerSize()},
  18. guest_memory_size{params.GetGuestSizeInBytes()}, host_memory_size{
  19. params.GetHostSizeInBytes()} {
  20. mipmap_offsets.reserve(params.num_levels);
  21. mipmap_sizes.reserve(params.num_levels);
  22. std::size_t offset = 0;
  23. for (u32 level = 0; level < params.num_levels; ++level) {
  24. const std::size_t mipmap_size{params.GetGuestMipmapSize(level)};
  25. mipmap_sizes.push_back(mipmap_size);
  26. mipmap_offsets.push_back(offset);
  27. offset += mipmap_size;
  28. }
  29. }
  30. void SurfaceBaseImpl::SwizzleFunc(MortonSwizzleMode mode, u8* memory, const SurfaceParams& params,
  31. u8* buffer, u32 level) {
  32. const u32 width{params.GetMipWidth(level)};
  33. const u32 height{params.GetMipHeight(level)};
  34. const u32 block_height{params.GetMipBlockHeight(level)};
  35. const u32 block_depth{params.GetMipBlockDepth(level)};
  36. std::size_t guest_offset{mipmap_offsets[level]};
  37. if (params.is_layered) {
  38. std::size_t host_offset{0};
  39. const std::size_t guest_stride = layer_size;
  40. const std::size_t host_stride = params.GetHostLayerSize(level);
  41. for (u32 layer = 0; layer < params.depth; ++layer) {
  42. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth, 1,
  43. params.tile_width_spacing, buffer + host_offset, memory + guest_offset);
  44. guest_offset += guest_stride;
  45. host_offset += host_stride;
  46. }
  47. } else {
  48. MortonSwizzle(mode, params.pixel_format, width, block_height, height, block_depth,
  49. params.GetMipDepth(level), params.tile_width_spacing, buffer,
  50. memory + guest_offset);
  51. }
  52. }
  53. void SurfaceBaseImpl::LoadBuffer(Tegra::MemoryManager& memory_manager,
  54. std::vector<u8>& staging_buffer) {
  55. MICROPROFILE_SCOPE(GPU_Load_Texture);
  56. const auto host_ptr{memory_manager.GetPointer(gpu_addr)};
  57. if (params.is_tiled) {
  58. ASSERT_MSG(params.block_width == 1, "Block width is defined as {} on texture target {}",
  59. params.block_width, static_cast<u32>(params.target));
  60. for (u32 level = 0; level < params.num_levels; ++level) {
  61. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};
  62. SwizzleFunc(MortonSwizzleMode::MortonToLinear, host_ptr, params,
  63. staging_buffer.data() + host_offset, level);
  64. }
  65. } else {
  66. ASSERT_MSG(params.num_levels == 1, "Linear mipmap loading is not implemented");
  67. const u32 bpp{params.GetBytesPerPixel()};
  68. const u32 block_width{params.GetDefaultBlockWidth()};
  69. const u32 block_height{params.GetDefaultBlockHeight()};
  70. const u32 width{(params.width + block_width - 1) / block_width};
  71. const u32 height{(params.height + block_height - 1) / block_height};
  72. const u32 copy_size{width * bpp};
  73. if (params.pitch == copy_size) {
  74. std::memcpy(staging_buffer.data(), host_ptr, params.GetHostSizeInBytes());
  75. } else {
  76. const u8* start{host_ptr};
  77. u8* write_to{staging_buffer.data()};
  78. for (u32 h = height; h > 0; --h) {
  79. std::memcpy(write_to, start, copy_size);
  80. start += params.pitch;
  81. write_to += copy_size;
  82. }
  83. }
  84. }
  85. for (u32 level = 0; level < params.num_levels; ++level) {
  86. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};
  87. ConvertFromGuestToHost(staging_buffer.data() + host_offset, params.pixel_format,
  88. params.GetMipWidth(level), params.GetMipHeight(level),
  89. params.GetMipDepth(level), true, true);
  90. }
  91. }
  92. void SurfaceBaseImpl::FlushBuffer(Tegra::MemoryManager& memory_manager,
  93. std::vector<u8>& staging_buffer) {
  94. MICROPROFILE_SCOPE(GPU_Flush_Texture);
  95. auto host_ptr = memory_manager.GetPointer(gpu_addr);
  96. if (params.is_tiled) {
  97. ASSERT_MSG(params.block_width == 1, "Block width is defined as {}", params.block_width);
  98. for (u32 level = 0; level < params.num_levels; ++level) {
  99. const std::size_t host_offset{params.GetHostMipmapLevelOffset(level)};
  100. SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params,
  101. staging_buffer.data() + host_offset, level);
  102. }
  103. } else {
  104. UNIMPLEMENTED();
  105. /*
  106. ASSERT(params.target == SurfaceTarget::Texture2D);
  107. ASSERT(params.num_levels == 1);
  108. const u32 bpp{params.GetFormatBpp() / 8};
  109. const u32 copy_size{params.width * bpp};
  110. if (params.pitch == copy_size) {
  111. std::memcpy(host_ptr, staging_buffer.data(), memory_size);
  112. } else {
  113. u8* start{host_ptr};
  114. const u8* read_to{staging_buffer.data()};
  115. for (u32 h = params.GetHeight(); h > 0; --h) {
  116. std::memcpy(start, read_to, copy_size);
  117. start += params.GetPitch();
  118. read_to += copy_size;
  119. }
  120. }
  121. */
  122. }
  123. }
  124. } // namespace VideoCommon